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>
36 #include "pciaccess.h"
37 #include "pciaccess_private.h"
39 #if defined(__linux__) || defined(__GLIBC__)
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 # define LETOH_16(x) bswap_16(x)
44 # define HTOLE_16(x) bswap_16(x)
45 # define LETOH_32(x) bswap_32(x)
46 # define HTOLE_32(x) bswap_32(x)
48 # define LETOH_16(x) (x)
49 # define HTOLE_16(x) (x)
50 # define LETOH_32(x) (x)
51 # define HTOLE_32(x) (x)
56 #include <sys/byteorder.h>
59 # define LETOH_16(x) BSWAP_16(x)
60 # define HTOLE_16(x) BSWAP_16(x)
61 # define LETOH_32(x) BSWAP_32(x)
62 # define HTOLE_32(x) BSWAP_32(x)
64 # define LETOH_16(x) (x)
65 # define HTOLE_16(x) (x)
66 # define LETOH_32(x) (x)
67 # define HTOLE_32(x) (x)
72 #include <sys/endian.h>
74 #define HTOLE_16(x) htole16(x)
75 #define HTOLE_32(x) htole32(x)
77 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
78 #define LETOH_16(x) le16toh(x)
79 #define LETOH_32(x) le32toh(x)
81 #define LETOH_16(x) letoh16(x)
82 #define LETOH_32(x) letoh32(x)
88 * Read a device's expansion ROM.
90 * Reads the device's expansion ROM and stores the data in the memory pointed
91 * to by \c buffer. The buffer must be at least \c pci_device::rom_size
94 * \param dev Device whose expansion ROM is to be read.
95 * \param buffer Memory in which to store the ROM.
98 * Zero on success or an \c errno value on failure.
101 pci_device_read_rom( struct pci_device * dev, void * buffer )
103 if ( (dev == NULL) || (buffer == NULL) ) {
108 return (pci_sys->methods->read_rom)( dev, buffer );
112 * Probe a PCI (VGA) device to determine if its the boot VGA device
114 * \param dev Device whose VGA status to query
116 * Zero if not the boot VGA, 1 if the boot VGA.
119 pci_device_is_boot_vga( struct pci_device * dev )
121 if (!pci_sys->methods->boot_vga)
123 return pci_sys->methods->boot_vga( dev );
127 * Probe a PCI device to determine if a kernel driver is attached.
129 * \param dev Device to query
131 * Zero if no driver attached, 1 if attached kernel drviver
134 pci_device_has_kernel_driver( struct pci_device * dev )
136 if (!pci_sys->methods->has_kernel_driver)
138 return pci_sys->methods->has_kernel_driver( dev );
142 * Probe a PCI device to learn information about the device.
144 * Probes a PCI device to learn various information about the device. Before
145 * calling this function, the only public fields in the \c pci_device
146 * structure that have valid values are \c pci_device::domain,
147 * \c pci_device::bus, \c pci_device::dev, and \c pci_device::func.
149 * \param dev Device to be probed.
152 * Zero on success or an \c errno value on failure.
155 pci_device_probe( struct pci_device * dev )
162 return (pci_sys->methods->probe)( dev );
167 * Map the specified BAR so that it can be accessed by the CPU.
169 * Maps the specified BAR for access by the processor. The pointer to the
170 * mapped region is stored in the \c pci_mem_region::memory pointer for the
173 * \param dev Device whose memory region is to be mapped.
174 * \param region Region, on the range [0, 5], that is to be mapped.
175 * \param write_enable Map for writing (non-zero).
178 * Zero on success or an \c errno value on failure.
180 * \sa pci_device_map_range, pci_device_unmap_range
184 pci_device_map_region(struct pci_device * dev, unsigned region,
187 const unsigned map_flags =
188 (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0;
190 if ((region > 5) || (dev->regions[region].size == 0)) {
194 if (dev->regions[region].memory != NULL) {
198 return pci_device_map_range(dev, dev->regions[region].base_addr,
199 dev->regions[region].size, map_flags,
200 &dev->regions[region].memory);
205 * Map the specified memory range so that it can be accessed by the CPU.
207 * Maps the specified memory range for access by the processor. The pointer
208 * to the mapped region is stored in \c addr. In addition, the
209 * \c pci_mem_region::memory pointer for the BAR will be updated.
211 * \param dev Device whose memory region is to be mapped.
212 * \param base Base address of the range to be mapped.
213 * \param size Size of the range to be mapped.
214 * \param write_enable Map for writing (non-zero).
215 * \param addr Location to store the mapped address.
218 * Zero on success or an \c errno value on failure.
220 * \sa pci_device_map_range
222 int pci_device_map_memory_range(struct pci_device *dev,
223 pciaddr_t base, pciaddr_t size,
224 int write_enable, void **addr)
226 return pci_device_map_range(dev, base, size,
227 (write_enable) ? PCI_DEV_MAP_FLAG_WRITABLE : 0,
233 * Map the specified memory range so that it can be accessed by the CPU.
235 * Maps the specified memory range for access by the processor. The pointer
236 * to the mapped region is stored in \c addr. In addition, the
237 * \c pci_mem_region::memory pointer for the BAR will be updated.
239 * \param dev Device whose memory region is to be mapped.
240 * \param base Base address of the range to be mapped.
241 * \param size Size of the range to be mapped.
242 * \param map_flags Flag bits controlling how the mapping is accessed.
243 * \param addr Location to store the mapped address.
246 * Zero on success or an \c errno value on failure.
248 * \sa pci_device_unmap_range
251 pci_device_map_range(struct pci_device *dev, pciaddr_t base,
252 pciaddr_t size, unsigned map_flags,
255 struct pci_device_private *const devp =
256 (struct pci_device_private *) dev;
257 struct pci_device_mapping *mappings;
270 for (region = 0; region < 6; region++) {
271 const struct pci_mem_region * const r = &dev->regions[region];
274 if ((r->base_addr <= base) && ((r->base_addr + r->size) > base)) {
275 if ((base + size) > (r->base_addr + r->size)) {
288 /* Make sure that there isn't already a mapping with the same base and
291 for (i = 0; i < devp->num_mappings; i++) {
292 if ((devp->mappings[i].base == base)
293 && (devp->mappings[i].size == size)) {
299 mappings = realloc(devp->mappings,
300 (sizeof(devp->mappings[0]) * (devp->num_mappings + 1)));
301 if (mappings == NULL) {
305 mappings[devp->num_mappings].base = base;
306 mappings[devp->num_mappings].size = size;
307 mappings[devp->num_mappings].region = region;
308 mappings[devp->num_mappings].flags = map_flags;
309 mappings[devp->num_mappings].memory = NULL;
311 if (dev->regions[region].memory == NULL) {
312 err = (*pci_sys->methods->map_range)(dev,
313 &mappings[devp->num_mappings]);
317 *addr = mappings[devp->num_mappings].memory;
318 devp->num_mappings++;
320 mappings = realloc(mappings,
321 (sizeof(mappings[0]) * devp->num_mappings));
324 devp->mappings = mappings;
331 * Unmap the specified BAR so that it can no longer be accessed by the CPU.
333 * Unmaps the specified BAR that was previously mapped via
334 * \c pci_device_map_region.
336 * \param dev Device whose memory region is to be mapped.
337 * \param region Region, on the range [0, 5], that is to be mapped.
340 * Zero on success or an \c errno value on failure.
342 * \sa pci_device_map_range, pci_device_unmap_range
346 pci_device_unmap_region( struct pci_device * dev, unsigned region )
354 if ((region > 5) || (dev->regions[region].size == 0)) {
358 err = pci_device_unmap_range(dev, dev->regions[region].memory,
359 dev->regions[region].size);
361 dev->regions[region].memory = NULL;
369 * Unmap the specified memory range so that it can no longer be accessed by the CPU.
371 * Unmaps the specified memory range that was previously mapped via
372 * \c pci_device_map_memory_range.
374 * \param dev Device whose memory is to be unmapped.
375 * \param memory Pointer to the base of the mapped range.
376 * \param size Size, in bytes, of the range to be unmapped.
379 * Zero on success or an \c errno value on failure.
381 * \sa pci_device_map_range, pci_device_unmap_range
385 pci_device_unmap_memory_range(struct pci_device *dev, void *memory,
388 return pci_device_unmap_range(dev, memory, size);
393 * Unmap the specified memory range so that it can no longer be accessed by the CPU.
395 * Unmaps the specified memory range that was previously mapped via
396 * \c pci_device_map_memory_range.
398 * \param dev Device whose memory is to be unmapped.
399 * \param memory Pointer to the base of the mapped range.
400 * \param size Size, in bytes, of the range to be unmapped.
403 * Zero on success or an \c errno value on failure.
405 * \sa pci_device_map_range
408 pci_device_unmap_range(struct pci_device *dev, void *memory,
411 struct pci_device_private *const devp =
412 (struct pci_device_private *) dev;
421 for (i = 0; i < devp->num_mappings; i++) {
422 if ((devp->mappings[i].memory == memory)
423 && (devp->mappings[i].size == size)) {
428 if (i == devp->num_mappings) {
433 err = (*pci_sys->methods->unmap_range)(dev, &devp->mappings[i]);
435 const unsigned entries_to_move = (devp->num_mappings - i) - 1;
437 if (entries_to_move > 0) {
438 (void) memmove(&devp->mappings[i],
439 &devp->mappings[i + 1],
440 entries_to_move * sizeof(devp->mappings[0]));
443 devp->num_mappings--;
444 devp->mappings = realloc(devp->mappings,
445 (sizeof(devp->mappings[0]) * devp->num_mappings));
453 * Read arbitrary bytes from device's PCI config space
455 * Reads data from the device's PCI configuration space. As with the system
456 * read command, less data may be returned, without an error, than was
457 * requested. This is particularly the case if a non-root user tries to read
458 * beyond the first 64-bytes of configuration space.
460 * \param dev Device whose PCI configuration data is to be read.
461 * \param data Location to store the data
462 * \param offset Initial byte offset to read
463 * \param size Total number of bytes to read
464 * \param bytes_read Location to store the actual number of bytes read. This
465 * pointer may be \c NULL.
468 * Zero on success or an errno value on failure.
471 * Data read from PCI configuration space using this routine is \b not
472 * byte-swapped to the host's byte order. PCI configuration data is always
473 * stored in little-endian order, and that is what this routine returns.
476 pci_device_cfg_read( struct pci_device * dev, void * data,
477 pciaddr_t offset, pciaddr_t size,
478 pciaddr_t * bytes_read )
482 if ( (dev == NULL) || (data == NULL) ) {
486 return pci_sys->methods->read( dev, data, offset, size,
488 ? & scratch : bytes_read );
493 pci_device_cfg_read_u8( struct pci_device * dev, uint8_t * data,
497 int err = pci_device_cfg_read( dev, data, offset, 1, & bytes );
499 if ( (err == 0) && (bytes != 1) ) {
508 pci_device_cfg_read_u16( struct pci_device * dev, uint16_t * data,
512 int err = pci_device_cfg_read( dev, data, offset, 2, & bytes );
514 if ( (err == 0) && (bytes != 2) ) {
518 *data = LETOH_16( *data );
524 pci_device_cfg_read_u32( struct pci_device * dev, uint32_t * data,
528 int err = pci_device_cfg_read( dev, data, offset, 4, & bytes );
530 if ( (err == 0) && (bytes != 4) ) {
534 *data = LETOH_32( *data );
540 * Write arbitrary bytes to device's PCI config space
542 * Writes data to the device's PCI configuration space. As with the system
543 * write command, less data may be written, without an error, than was
546 * \param dev Device whose PCI configuration data is to be written.
547 * \param data Location of the source data
548 * \param offset Initial byte offset to write
549 * \param size Total number of bytes to write
550 * \param bytes_read Location to store the actual number of bytes written.
551 * This pointer may be \c NULL.
554 * Zero on success or an errno value on failure.
557 * Data written to PCI configuration space using this routine is \b not
558 * byte-swapped from the host's byte order. PCI configuration data is always
559 * stored in little-endian order, so data written with this routine should be
560 * put in that order in advance.
563 pci_device_cfg_write( struct pci_device * dev, const void * data,
564 pciaddr_t offset, pciaddr_t size,
565 pciaddr_t * bytes_written )
569 if ( (dev == NULL) || (data == NULL) ) {
573 return pci_sys->methods->write( dev, data, offset, size,
574 (bytes_written == NULL)
575 ? & scratch : bytes_written );
580 pci_device_cfg_write_u8(struct pci_device *dev, uint8_t data,
584 int err = pci_device_cfg_write(dev, & data, offset, 1, & bytes);
586 if ( (err == 0) && (bytes != 1) ) {
596 pci_device_cfg_write_u16(struct pci_device *dev, uint16_t data,
600 const uint16_t temp = HTOLE_16(data);
601 int err = pci_device_cfg_write( dev, & temp, offset, 2, & bytes );
603 if ( (err == 0) && (bytes != 2) ) {
613 pci_device_cfg_write_u32(struct pci_device *dev, uint32_t data,
617 const uint32_t temp = HTOLE_32(data);
618 int err = pci_device_cfg_write( dev, & temp, offset, 4, & bytes );
620 if ( (err == 0) && (bytes != 4) ) {
630 pci_device_cfg_write_bits( struct pci_device * dev, uint32_t mask,
631 uint32_t data, pciaddr_t offset )
636 err = pci_device_cfg_read_u32( dev, & temp, offset );
641 err = pci_device_cfg_write_u32(dev, temp, offset);
648 pci_device_enable(struct pci_device *dev)
654 if (pci_sys->methods->enable)
655 pci_sys->methods->enable(dev);
659 * Map the legacy memory space for the PCI domain containing \c dev.
661 * \param dev Device whose memory region is to be mapped.
662 * \param base Base address of the range to be mapped.
663 * \param size Size of the range to be mapped.
664 * \param map_flags Flag bits controlling how the mapping is accessed.
665 * \param addr Location to store the mapped address.
668 * Zero on success or an \c errno value on failure.
671 pci_device_map_legacy(struct pci_device *dev, pciaddr_t base, pciaddr_t size,
672 unsigned map_flags, void **addr)
674 if (base > 0x100000 || base + size > 0x100000)
677 if (!pci_sys->methods->map_legacy)
680 return pci_sys->methods->map_legacy(dev, base, size, map_flags, addr);
684 * Unmap the legacy memory space for the PCI domain containing \c dev.
686 * \param dev Device whose memory region is to be unmapped.
687 * \param addr Location of the mapped address.
688 * \param size Size of the range to be unmapped.
691 * Zero on success or an \c errno value on failure.
694 pci_device_unmap_legacy(struct pci_device *dev, void *addr, pciaddr_t size)
696 if (!pci_sys->methods->unmap_legacy)
699 return pci_sys->methods->unmap_legacy(dev, addr, size);