pci.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef PCI_H
  2. #define PCI_H
  3. /*
  4. * API for scanning a PCI bus for a given device, as well to access
  5. * BAR registers.
  6. *
  7. * Copyright (C) 2013, Red Hat Inc, Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.
  10. */
  11. #include "libcflat.h"
  12. typedef uint16_t pcidevaddr_t;
  13. enum {
  14. PCIDEVADDR_INVALID = 0xffff,
  15. };
  16. #define PCI_BAR_NUM 6
  17. #define PCI_DEVFN_MAX 256
  18. #define ASSERT_BAR_NUM(bar_num) \
  19. do { assert(bar_num >= 0 && bar_num < PCI_BAR_NUM); } while (0)
  20. #define PCI_BDF_GET_DEVFN(x) ((x) & 0xff)
  21. #define PCI_BDF_GET_BUS(x) (((x) >> 8) & 0xff)
  22. struct pci_dev {
  23. uint16_t bdf;
  24. uint16_t msi_offset;
  25. phys_addr_t resource[PCI_BAR_NUM];
  26. };
  27. extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf);
  28. extern void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr);
  29. typedef void (*pci_cap_handler_t)(struct pci_dev *dev, int cap_offset, int cap_id);
  30. extern void pci_cap_walk(struct pci_dev *dev, pci_cap_handler_t handler);
  31. extern void pci_enable_defaults(struct pci_dev *dev);
  32. extern bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr,
  33. uint32_t msi_data);
  34. typedef phys_addr_t iova_t;
  35. extern bool pci_probe(void);
  36. extern void pci_print(void);
  37. extern bool pci_dev_exists(pcidevaddr_t dev);
  38. extern pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id);
  39. /*
  40. * @bar_num in all BAR access functions below is the index of the 32-bit
  41. * register starting from the PCI_BASE_ADDRESS_0 offset.
  42. *
  43. * In cases where the BAR size is 64-bit, a caller should still provide
  44. * @bar_num in terms of 32-bit words. For example, if a device has a 64-bit
  45. * BAR#0 and a 32-bit BAR#1, then caller should provide 2 to address BAR#1,
  46. * not 1.
  47. *
  48. * It is expected the caller is aware of the device BAR layout and never
  49. * tries to address the middle of a 64-bit register.
  50. */
  51. extern phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num);
  52. extern void pci_bar_set_addr(struct pci_dev *dev, int bar_num, phys_addr_t addr);
  53. extern phys_addr_t pci_bar_size(struct pci_dev *dev, int bar_num);
  54. extern uint32_t pci_bar_get(struct pci_dev *dev, int bar_num);
  55. extern uint32_t pci_bar_mask(uint32_t bar);
  56. extern bool pci_bar_is64(struct pci_dev *dev, int bar_num);
  57. extern bool pci_bar_is_memory(struct pci_dev *dev, int bar_num);
  58. extern bool pci_bar_is_valid(struct pci_dev *dev, int bar_num);
  59. extern void pci_bar_print(struct pci_dev *dev, int bar_num);
  60. extern void pci_dev_print_id(struct pci_dev *dev);
  61. extern void pci_dev_print(struct pci_dev *dev);
  62. extern uint8_t pci_intx_line(struct pci_dev *dev);
  63. void pci_msi_set_enable(struct pci_dev *dev, bool enabled);
  64. extern int pci_testdev(void);
  65. /*
  66. * pci-testdev is a driver for the pci-testdev qemu pci device. The
  67. * device enables testing mmio and portio exits, and measuring their
  68. * speed.
  69. */
  70. #define PCI_VENDOR_ID_REDHAT 0x1b36
  71. #define PCI_DEVICE_ID_REDHAT_TEST 0x0005
  72. /*
  73. * pci-testdev supports at least three types of tests (via mmio and
  74. * portio BARs): no-eventfd, wildcard-eventfd and datamatch-eventfd
  75. */
  76. #define PCI_TESTDEV_BAR_MEM 0
  77. #define PCI_TESTDEV_BAR_IO 1
  78. #define PCI_TESTDEV_NUM_BARS 2
  79. #define PCI_TESTDEV_NUM_TESTS 3
  80. struct pci_test_dev_hdr {
  81. uint8_t test;
  82. uint8_t width;
  83. uint8_t pad0[2];
  84. uint32_t offset;
  85. uint32_t data;
  86. uint32_t count;
  87. uint8_t name[];
  88. };
  89. #define PCI_HEADER_TYPE_MASK 0x7f
  90. #endif /* PCI_H */