2 * Copyright 2009 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software")
6 * to deal in the software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * them Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Adam Jackson <ajax@redhat.com>
31 #include "pciaccess.h"
32 #include "pciaccess_private.h"
34 static struct pci_io_handle *
37 struct pci_io_handle *new;
39 new = malloc(sizeof(struct pci_io_handle));
47 delete_io_handle(struct pci_io_handle *handle)
59 * Open a handle to a PCI device I/O range. The \c base and \c size
60 * requested must fit entirely within a single I/O BAR on the device.
61 * \c size is in bytes.
64 * An opaque handle to the I/O BAR, or \c NULL on error.
66 struct pci_io_handle *
67 pci_device_open_io(struct pci_device *dev, pciaddr_t base, pciaddr_t size)
69 struct pci_io_handle *ret;
72 if (!pci_sys->methods->open_device_io)
75 for (bar = 0; bar < 6; bar++) {
76 struct pci_mem_region *region = &(dev->regions[bar]);
80 if (base < region->base_addr || base > (region->base_addr+region->size))
83 if ((base + size) > (region->base_addr + region->size))
86 ret = new_io_handle();
90 if (!pci_sys->methods->open_device_io(ret, dev, bar, base, size)) {
91 delete_io_handle(ret);
102 * Open a handle to the legacy I/O space for the PCI domain containing
103 * \c dev. \c size is in bytes.
106 * An opaque handle to the requested range, or \c NULL on error.
108 struct pci_io_handle *
109 pci_legacy_open_io(struct pci_device *dev, pciaddr_t base, pciaddr_t size)
111 struct pci_io_handle *ret;
113 if (!pci_sys->methods->open_legacy_io)
116 ret = new_io_handle();
120 if (!pci_sys->methods->open_legacy_io(ret, dev, base, size)) {
121 delete_io_handle(ret);
129 * Close an I/O handle.
132 pci_device_close_io(struct pci_device *dev, struct pci_io_handle *handle)
134 if (dev && handle && pci_sys->methods->close_io)
135 pci_sys->methods->close_io(dev, handle);
137 delete_io_handle(handle);
141 * Read a 32-bit value from the I/O space. \c reg is relative to the
142 * \c base specified when the handle was opened. Some platforms may
143 * require that \c reg be 32-bit-aligned.
146 * The value read from the I/O port, or undefined on any error.
149 pci_io_read32(struct pci_io_handle *handle, uint32_t reg)
151 if (reg + 4 > handle->size)
154 return pci_sys->methods->read32(handle, reg);
158 * Read a 16-bit value from the I/O space. \c reg is relative to the
159 * \c base specified when the handle was opened. Some platforms may
160 * require that \c reg be 16-bit-aligned.
163 * The value read from the I/O port, or undefined on any error.
166 pci_io_read16(struct pci_io_handle *handle, uint32_t reg)
168 if (reg + 2 > handle->size)
171 return pci_sys->methods->read16(handle, reg);
175 * Read a 8-bit value from the I/O space. \c reg is relative to the
176 * \c base specified when the handle was opened.
179 * The value read from the I/O port, or undefined on any error.
182 pci_io_read8(struct pci_io_handle *handle, uint32_t reg)
184 if (reg + 1 > handle->size)
187 return pci_sys->methods->read8(handle, reg);
191 * Write a 32-bit value to the I/O space. \c reg is relative to the
192 * \c base specified when the handle was opened. Some platforms may
193 * require that \c reg be 32-bit-aligned.
196 pci_io_write32(struct pci_io_handle *handle, uint32_t reg, uint32_t data)
198 if (reg + 4 > handle->size)
201 pci_sys->methods->write32(handle, reg, data);
205 * Write a 16-bit value to the I/O space. \c reg is relative to the
206 * \c base specified when the handle was opened. Some platforms may
207 * require that \c reg be 16-bit-aligned.
210 pci_io_write16(struct pci_io_handle *handle, uint32_t reg, uint16_t data)
212 if (reg + 2 > handle->size)
215 pci_sys->methods->write16(handle, reg, data);
219 * Write a 8-bit value to the I/O space. \c reg is relative to the
220 * \c base specified when the handle was opened.
223 pci_io_write8(struct pci_io_handle *handle, uint32_t reg, uint8_t data)
225 if (reg + 1 > handle->size)
228 pci_sys->methods->write8(handle, reg, data);