access.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <linux/pci.h>
  2. #include <linux/module.h>
  3. #include <linux/ioport.h>
  4. /*
  5. * This interrupt-safe spinlock protects all accesses to PCI
  6. * configuration space.
  7. */
  8. static DEFINE_SPINLOCK(pci_lock);
  9. /*
  10. * Wrappers for all PCI configuration access functions. They just check
  11. * alignment, do locking and call the low-level functions pointed to
  12. * by pci_dev->ops.
  13. */
  14. #define PCI_byte_BAD 0
  15. #define PCI_word_BAD (pos & 1)
  16. #define PCI_dword_BAD (pos & 3)
  17. #define PCI_OP_READ(size,type,len) \
  18. int pci_bus_read_config_##size \
  19. (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
  20. { \
  21. int res; \
  22. unsigned long flags; \
  23. u32 data = 0; \
  24. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  25. spin_lock_irqsave(&pci_lock, flags); \
  26. res = bus->ops->read(bus, devfn, pos, len, &data); \
  27. *value = (type)data; \
  28. spin_unlock_irqrestore(&pci_lock, flags); \
  29. return res; \
  30. }
  31. #define PCI_OP_WRITE(size,type,len) \
  32. int pci_bus_write_config_##size \
  33. (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
  34. { \
  35. int res; \
  36. unsigned long flags; \
  37. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  38. spin_lock_irqsave(&pci_lock, flags); \
  39. res = bus->ops->write(bus, devfn, pos, len, value); \
  40. spin_unlock_irqrestore(&pci_lock, flags); \
  41. return res; \
  42. }
  43. PCI_OP_READ(byte, u8, 1)
  44. PCI_OP_READ(word, u16, 2)
  45. PCI_OP_READ(dword, u32, 4)
  46. PCI_OP_WRITE(byte, u8, 1)
  47. PCI_OP_WRITE(word, u16, 2)
  48. PCI_OP_WRITE(dword, u32, 4)
  49. EXPORT_SYMBOL(pci_bus_read_config_byte);
  50. EXPORT_SYMBOL(pci_bus_read_config_word);
  51. EXPORT_SYMBOL(pci_bus_read_config_dword);
  52. EXPORT_SYMBOL(pci_bus_write_config_byte);
  53. EXPORT_SYMBOL(pci_bus_write_config_word);
  54. EXPORT_SYMBOL(pci_bus_write_config_dword);