2 * (C) Copyright IBM Corporation 2006
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
26 * \file common_interface.c
27 * Platform independent interface glue.
29 * \author Ian Romanick <idr@us.ibm.com>
39 #include "pciaccess.h"
40 #include "pciaccess_private.h"
42 #if defined(__linux__) || defined(__GLIBC__) || defined(__CYGWIN__)
45 #if __BYTE_ORDER == __BIG_ENDIAN
46 # define LETOH_16(x) bswap_16(x)
47 # define HTOLE_16(x) bswap_16(x)
48 # define LETOH_32(x) bswap_32(x)
49 # define HTOLE_32(x) bswap_32(x)
51 # define LETOH_16(x) (x)
52 # define HTOLE_16(x) (x)
53 # define LETOH_32(x) (x)
54 # define HTOLE_32(x) (x)
59 #include <sys/byteorder.h>
62 # define LETOH_16(x) BSWAP_16(x)
63 # define HTOLE_16(x) BSWAP_16(x)
64 # define LETOH_32(x) BSWAP_32(x)
65 # define HTOLE_32(x) BSWAP_32(x)
67 # define LETOH_16(x) (x)
68 # define HTOLE_16(x) (x)
69 # define LETOH_32(x) (x)
70 # define HTOLE_32(x) (x)
75 #include <sys/endian.h>
77 #define HTOLE_16(x) htole16(x)
78 #define HTOLE_32(x) htole32(x)
80 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
81 #define LETOH_16(x) le16toh(x)
82 #define LETOH_32(x) le32toh(x)
84 #define LETOH_16(x) letoh16(x)
85 #define LETOH_32(x) letoh32(x)
91 * Read a device's expansion ROM.
93 * Reads the device's expansion ROM and stores the data in the memory pointed
94 * to by \c buffer. The buffer must be at least \c pci_device::rom_size
97 * \param dev Device whose expansion ROM is to be read.
98 * \param buffer Memory in which to store the ROM.
101 * Zero on success or an \c errno value on failure.
104 pci_device_read_rom( struct pci_device * dev, void * buffer )
106 if ( (dev == NULL) || (buffer == NULL) ) {
111 return (pci_sys->methods->read_rom)( dev, buffer );
115 * Probe a PCI (VGA) device to determine if its the boot VGA device
117 * \param dev Device whose VGA status to query
119 * Zero if not the boot VGA, 1 if the boot VGA.
122 pci_device_is_boot_vga( struct pci_device * dev )
124 if (!pci_sys->methods->boot_vga)
126 return pci_sys->methods->boot_vga( dev );
130 * Probe a PCI device to determine if a kernel driver is attached.
132 * \param dev Device to query
134 * Zero if no driver attached, 1 if attached kernel drviver
137 pci_device_has_kernel_driver( struct pci_device * dev )
139 if (!pci_sys->methods->has_kernel_driver)
141 return pci_sys->methods->has_kernel_driver( dev );
145 * Probe a PCI device to learn information about the device.
147 * Probes a PCI device to learn various information about the device. Before
148 * calling this function, the only public fields in the \c pci_device
149 * structure that have valid values are \c pci_device::domain,
150 * \c pci_device::bus, \c pci_device::dev, and \c pci_device::func.
152 * \param dev Device to be probed.
155 * Zero on success or an \c errno value on failure.
158 pci_device_probe( struct pci_device * dev )
165 return (pci_sys->methods->probe)( dev );
170 * Map the specified BAR so that it can be accessed by the CPU.
172 * Maps the specified BAR for access by the processor. The pointer to the
173 * mapped region is stored in the \c pci_mem_region::memory pointer for the
176 * \param dev Device whose memory region is to be mapped.
177 * \param region Region, on the range [0, 5], that is to be mapped.
178 * \param write_enable Map for writing (non-zero).
181 * Zero on success or an \c errno value on failure.
183 * \sa pci_device_map_range, pci_device_unmap_range
187 pci_device_map_region(struct pci_device * dev, unsigned region,
190 const unsigned map_flags =
191 (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0;
193 if ((region > 5) || (dev->regions[region].size == 0)) {
197 if (dev->regions[region].memory != NULL) {
201 return pci_device_map_range(dev, dev->regions[region].base_addr,
202 dev->regions[region].size, map_flags,
203 &dev->regions[region].memory);
208 * Map the specified memory range so that it can be accessed by the CPU.
210 * Maps the specified memory range for access by the processor. The pointer
211 * to the mapped region is stored in \c addr. In addition, the
212 * \c pci_mem_region::memory pointer for the BAR will be updated.
214 * \param dev Device whose memory region is to be mapped.
215 * \param base Base address of the range to be mapped.
216 * \param size Size of the range to be mapped.
217 * \param write_enable Map for writing (non-zero).
218 * \param addr Location to store the mapped address.
221 * Zero on success or an \c errno value on failure.
223 * \sa pci_device_map_range
225 int pci_device_map_memory_range(struct pci_device *dev,
226 pciaddr_t base, pciaddr_t size,
227 int write_enable, void **addr)
229 return pci_device_map_range(dev, base, size,
230 (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0,
236 * Map the specified memory range so that it can be accessed by the CPU.
238 * Maps the specified memory range for access by the processor. The pointer
239 * to the mapped region is stored in \c addr. In addition, the
240 * \c pci_mem_region::memory pointer for the BAR will be updated.
242 * \param dev Device whose memory region is to be mapped.
243 * \param base Base address of the range to be mapped.
244 * \param size Size of the range to be mapped.
245 * \param map_flags Flag bits controlling how the mapping is accessed.
246 * \param addr Location to store the mapped address.
249 * Zero on success or an \c errno value on failure.
251 * \sa pci_device_unmap_range
254 pci_device_map_range(struct pci_device *dev, pciaddr_t base,
255 pciaddr_t size, unsigned map_flags,
258 struct pci_device_private *const devp =
259 (struct pci_device_private *) dev;
260 struct pci_device_mapping *mappings;
273 for (region = 0; region < 6; region++) {
274 const struct pci_mem_region * const r = &dev->regions[region];
277 if ((r->base_addr <= base) && ((r->base_addr + r->size) > base)) {
278 if ((base + size) > (r->base_addr + r->size)) {
291 /* Make sure that there isn't already a mapping with the same base and
294 for (i = 0; i < devp->num_mappings; i++) {
295 if ((devp->mappings[i].base == base)
296 && (devp->mappings[i].size == size)) {
302 mappings = realloc(devp->mappings,
303 (sizeof(devp->mappings[0]) * (devp->num_mappings + 1)));
304 if (mappings == NULL) {
308 mappings[devp->num_mappings].base = base;
309 mappings[devp->num_mappings].size = size;
310 mappings[devp->num_mappings].region = region;
311 mappings[devp->num_mappings].flags = map_flags;
312 mappings[devp->num_mappings].memory = NULL;
314 if (dev->regions[region].memory == NULL) {
315 err = (*pci_sys->methods->map_range)(dev,
316 &mappings[devp->num_mappings]);
320 *addr = mappings[devp->num_mappings].memory;
321 devp->num_mappings++;
323 mappings = realloc(mappings,
324 (sizeof(mappings[0]) * devp->num_mappings));
327 devp->mappings = mappings;
334 * Unmap the specified BAR so that it can no longer be accessed by the CPU.
336 * Unmaps the specified BAR that was previously mapped via
337 * \c pci_device_map_region.
339 * \param dev Device whose memory region is to be mapped.
340 * \param region Region, on the range [0, 5], that is to be mapped.
343 * Zero on success or an \c errno value on failure.
345 * \sa pci_device_map_range, pci_device_unmap_range
349 pci_device_unmap_region( struct pci_device * dev, unsigned region )
357 if ((region > 5) || (dev->regions[region].size == 0)) {
361 err = pci_device_unmap_range(dev, dev->regions[region].memory,
362 dev->regions[region].size);
364 dev->regions[region].memory = NULL;
372 * Unmap the specified memory range so that it can no longer be accessed by the CPU.
374 * Unmaps the specified memory range that was previously mapped via
375 * \c pci_device_map_memory_range.
377 * \param dev Device whose memory is to be unmapped.
378 * \param memory Pointer to the base of the mapped range.
379 * \param size Size, in bytes, of the range to be unmapped.
382 * Zero on success or an \c errno value on failure.
384 * \sa pci_device_map_range, pci_device_unmap_range
388 pci_device_unmap_memory_range(struct pci_device *dev, void *memory,
391 return pci_device_unmap_range(dev, memory, size);
396 * Unmap the specified memory range so that it can no longer be accessed by the CPU.
398 * Unmaps the specified memory range that was previously mapped via
399 * \c pci_device_map_memory_range.
401 * \param dev Device whose memory is to be unmapped.
402 * \param memory Pointer to the base of the mapped range.
403 * \param size Size, in bytes, of the range to be unmapped.
406 * Zero on success or an \c errno value on failure.
408 * \sa pci_device_map_range
411 pci_device_unmap_range(struct pci_device *dev, void *memory,
414 struct pci_device_private *const devp =
415 (struct pci_device_private *) dev;
424 for (i = 0; i < devp->num_mappings; i++) {
425 if ((devp->mappings[i].memory == memory)
426 && (devp->mappings[i].size == size)) {
431 if (i == devp->num_mappings) {
436 err = (*pci_sys->methods->unmap_range)(dev, &devp->mappings[i]);
438 const unsigned entries_to_move = (devp->num_mappings - i) - 1;
440 if (entries_to_move > 0) {
441 (void) memmove(&devp->mappings[i],
442 &devp->mappings[i + 1],
443 entries_to_move * sizeof(devp->mappings[0]));
446 devp->num_mappings--;
447 devp->mappings = realloc(devp->mappings,
448 (sizeof(devp->mappings[0]) * devp->num_mappings));
456 * Read arbitrary bytes from device's PCI config space
458 * Reads data from the device's PCI configuration space. As with the system
459 * read command, less data may be returned, without an error, than was
460 * requested. This is particularly the case if a non-root user tries to read
461 * beyond the first 64-bytes of configuration space.
463 * \param dev Device whose PCI configuration data is to be read.
464 * \param data Location to store the data
465 * \param offset Initial byte offset to read
466 * \param size Total number of bytes to read
467 * \param bytes_read Location to store the actual number of bytes read. This
468 * pointer may be \c NULL.
471 * Zero on success or an errno value on failure.
474 * Data read from PCI configuration space using this routine is \b not
475 * byte-swapped to the host's byte order. PCI configuration data is always
476 * stored in little-endian order, and that is what this routine returns.
479 pci_device_cfg_read( struct pci_device * dev, void * data,
480 pciaddr_t offset, pciaddr_t size,
481 pciaddr_t * bytes_read )
485 if ( (dev == NULL) || (data == NULL) ) {
489 return pci_sys->methods->read( dev, data, offset, size,
491 ? & scratch : bytes_read );
496 pci_device_cfg_read_u8( struct pci_device * dev, uint8_t * data,
500 int err = pci_device_cfg_read( dev, data, offset, 1, & bytes );
502 if ( (err == 0) && (bytes != 1) ) {
511 pci_device_cfg_read_u16( struct pci_device * dev, uint16_t * data,
515 int err = pci_device_cfg_read( dev, data, offset, 2, & bytes );
517 if ( (err == 0) && (bytes != 2) ) {
521 *data = LETOH_16( *data );
527 pci_device_cfg_read_u32( struct pci_device * dev, uint32_t * data,
531 int err = pci_device_cfg_read( dev, data, offset, 4, & bytes );
533 if ( (err == 0) && (bytes != 4) ) {
537 *data = LETOH_32( *data );
543 * Write arbitrary bytes to device's PCI config space
545 * Writes data to the device's PCI configuration space. As with the system
546 * write command, less data may be written, without an error, than was
549 * \param dev Device whose PCI configuration data is to be written.
550 * \param data Location of the source data
551 * \param offset Initial byte offset to write
552 * \param size Total number of bytes to write
553 * \param bytes_read Location to store the actual number of bytes written.
554 * This pointer may be \c NULL.
557 * Zero on success or an errno value on failure.
560 * Data written to PCI configuration space using this routine is \b not
561 * byte-swapped from the host's byte order. PCI configuration data is always
562 * stored in little-endian order, so data written with this routine should be
563 * put in that order in advance.
566 pci_device_cfg_write( struct pci_device * dev, const void * data,
567 pciaddr_t offset, pciaddr_t size,
568 pciaddr_t * bytes_written )
572 if ( (dev == NULL) || (data == NULL) ) {
576 return pci_sys->methods->write( dev, data, offset, size,
577 (bytes_written == NULL)
578 ? & scratch : bytes_written );
583 pci_device_cfg_write_u8(struct pci_device *dev, uint8_t data,
587 int err = pci_device_cfg_write(dev, & data, offset, 1, & bytes);
589 if ( (err == 0) && (bytes != 1) ) {
599 pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
603 const uint16_t temp = HTOLE_16(data);
604 int err = pci_device_cfg_write( dev, & temp, offset, 2, & bytes );
606 if ( (err == 0) && (bytes != 2) ) {
616 pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
620 const uint32_t temp = HTOLE_32(data);
621 int err = pci_device_cfg_write( dev, & temp, offset, 4, & bytes );
623 if ( (err == 0) && (bytes != 4) ) {
633 pci_device_cfg_write_bits( struct pci_device * dev, uint32_t mask,
634 uint32_t data, pciaddr_t offset )
639 err = pci_device_cfg_read_u32( dev, & temp, offset );
644 err = pci_device_cfg_write_u32(dev, temp, offset);
651 pci_device_enable(struct pci_device *dev)
657 if (pci_sys->methods->enable)
658 pci_sys->methods->enable(dev);
662 * Map the legacy memory space for the PCI domain containing \c dev.
664 * \param dev Device whose memory region is to be mapped.
665 * \param base Base address of the range to be mapped.
666 * \param size Size of the range to be mapped.
667 * \param map_flags Flag bits controlling how the mapping is accessed.
668 * \param addr Location to store the mapped address.
671 * Zero on success or an \c errno value on failure.
674 pci_device_map_legacy(struct pci_device *dev, pciaddr_t base, pciaddr_t size,
675 unsigned map_flags, void **addr)
677 if (base > 0x100000 || base + size > 0x100000)
680 if (!pci_sys->methods->map_legacy)
683 return pci_sys->methods->map_legacy(dev, base, size, map_flags, addr);
687 * Unmap the legacy memory space for the PCI domain containing \c dev.
689 * \param dev Device whose memory region is to be unmapped.
690 * \param addr Location of the mapped address.
691 * \param size Size of the range to be unmapped.
694 * Zero on success or an \c errno value on failure.
697 pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
699 if (!pci_sys->methods->unmap_legacy)
702 return pci_sys->methods->unmap_legacy(dev, addr, size);