1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*
* Copyright 2009 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software")
* to deal in the software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* them Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Author:
* Adam Jackson <ajax@redhat.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "pciaccess.h"
#include "pciaccess_private.h"
static struct pci_io_handle *
new_io_handle(void)
{
struct pci_io_handle *new;
new = malloc(sizeof(struct pci_io_handle));
if (!new)
return NULL;
return new;
}
static void
delete_io_handle(struct pci_io_handle *handle)
{
free(handle);
return;
}
_pci_hidden void
pci_io_cleanup(void)
{
}
/**
* Open a handle to a PCI device I/O range. The \c base and \c size
* requested must fit entirely within a single I/O BAR on the device.
* \c size is in bytes.
*
* \returns
* An opaque handle to the I/O BAR, or \c NULL on error.
*/
struct pci_io_handle *
pci_device_open_io(struct pci_device *dev, pciaddr_t base, pciaddr_t size)
{
struct pci_io_handle *ret;
int bar;
if (!pci_sys->methods->open_device_io)
return NULL;
for (bar = 0; bar < 6; bar++) {
struct pci_mem_region *region = &(dev->regions[bar]);
if (!region->is_IO)
continue;
if (base < region->base_addr || base > (region->base_addr+region->size))
continue;
if ((base + size) > (region->base_addr + region->size))
continue;
ret = new_io_handle();
if (!ret)
return NULL;
if (!pci_sys->methods->open_device_io(ret, dev, bar, base, size)) {
delete_io_handle(ret);
return NULL;
}
return ret;
}
return NULL;
}
/**
* Open a handle to the legacy I/O space for the PCI domain containing
* \c dev. \c size is in bytes.
*
* \returns
* An opaque handle to the requested range, or \c NULL on error.
*/
struct pci_io_handle *
pci_legacy_open_io(struct pci_device *dev, pciaddr_t base, pciaddr_t size)
{
struct pci_io_handle *ret;
if (!pci_sys->methods->open_legacy_io)
return NULL;
ret = new_io_handle();
if (!ret)
return NULL;
if (!pci_sys->methods->open_legacy_io(ret, dev, base, size)) {
delete_io_handle(ret);
return NULL;
}
return ret;
}
/**
* Close an I/O handle.
*/
void
pci_device_close_io(struct pci_device *dev, struct pci_io_handle *handle)
{
if (dev && handle && pci_sys->methods->close_io)
pci_sys->methods->close_io(dev, handle);
delete_io_handle(handle);
}
/**
* Read a 32-bit value from the I/O space. \c reg is relative to the
* \c base specified when the handle was opened. Some platforms may
* require that \c reg be 32-bit-aligned.
*
* \returns
* The value read from the I/O port, or undefined on any error.
*/
uint32_t
pci_io_read32(struct pci_io_handle *handle, uint32_t reg)
{
if (reg + 4 > handle->size)
return UINT32_MAX;
return pci_sys->methods->read32(handle, reg);
}
/**
* Read a 16-bit value from the I/O space. \c reg is relative to the
* \c base specified when the handle was opened. Some platforms may
* require that \c reg be 16-bit-aligned.
*
* \returns
* The value read from the I/O port, or undefined on any error.
*/
uint16_t
pci_io_read16(struct pci_io_handle *handle, uint32_t reg)
{
if (reg + 2 > handle->size)
return UINT16_MAX;
return pci_sys->methods->read16(handle, reg);
}
/**
* Read a 8-bit value from the I/O space. \c reg is relative to the
* \c base specified when the handle was opened.
*
* \returns
* The value read from the I/O port, or undefined on any error.
*/
uint8_t
pci_io_read8(struct pci_io_handle *handle, uint32_t reg)
{
if (reg + 1 > handle->size)
return UINT8_MAX;
return pci_sys->methods->read8(handle, reg);
}
/**
* Write a 32-bit value to the I/O space. \c reg is relative to the
* \c base specified when the handle was opened. Some platforms may
* require that \c reg be 32-bit-aligned.
*/
void
pci_io_write32(struct pci_io_handle *handle, uint32_t reg, uint32_t data)
{
if (reg + 4 > handle->size)
return;
pci_sys->methods->write32(handle, reg, data);
}
/**
* Write a 16-bit value to the I/O space. \c reg is relative to the
* \c base specified when the handle was opened. Some platforms may
* require that \c reg be 16-bit-aligned.
*/
void
pci_io_write16(struct pci_io_handle *handle, uint32_t reg, uint16_t data)
{
if (reg + 2 > handle->size)
return;
pci_sys->methods->write16(handle, reg, data);
}
/**
* Write a 8-bit value to the I/O space. \c reg is relative to the
* \c base specified when the handle was opened.
*/
void
pci_io_write8(struct pci_io_handle *handle, uint32_t reg, uint8_t data)
{
if (reg + 1 > handle->size)
return;
pci_sys->methods->write8(handle, reg, data);
}
|