You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pci.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef _IPXE_PCI_H
  2. #define _IPXE_PCI_H
  3. /** @file
  4. *
  5. * PCI bus
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/device.h>
  11. #include <ipxe/tables.h>
  12. #include <ipxe/pci_io.h>
  13. /** PCI vendor ID */
  14. #define PCI_VENDOR_ID 0x00
  15. /** PCI device ID */
  16. #define PCI_DEVICE_ID 0x02
  17. /** PCI command */
  18. #define PCI_COMMAND 0x04
  19. #define PCI_COMMAND_IO 0x0001 /**< I/O space */
  20. #define PCI_COMMAND_MEM 0x0002 /**< Memory space */
  21. #define PCI_COMMAND_MASTER 0x0004 /**< Bus master */
  22. #define PCI_COMMAND_INVALIDATE 0x0010 /**< Mem. write & invalidate */
  23. #define PCI_COMMAND_PARITY 0x0040 /**< Parity error response */
  24. #define PCI_COMMAND_SERR 0x0100 /**< SERR# enable */
  25. #define PCI_COMMAND_INTX_DISABLE 0x0400 /**< Interrupt disable */
  26. /** PCI status */
  27. #define PCI_STATUS 0x06
  28. #define PCI_STATUS_CAP_LIST 0x0010 /**< Capabilities list */
  29. #define PCI_STATUS_PARITY 0x0100 /**< Master data parity error */
  30. #define PCI_STATUS_REC_TARGET_ABORT 0x1000 /**< Received target abort */
  31. #define PCI_STATUS_REC_MASTER_ABORT 0x2000 /**< Received master abort */
  32. #define PCI_STATUS_SIG_SYSTEM_ERROR 0x4000 /**< Signalled system error */
  33. #define PCI_STATUS_DETECTED_PARITY 0x8000 /**< Detected parity error */
  34. /** PCI revision */
  35. #define PCI_REVISION 0x08
  36. /** PCI cache line size */
  37. #define PCI_CACHE_LINE_SIZE 0x0c
  38. /** PCI latency timer */
  39. #define PCI_LATENCY_TIMER 0x0d
  40. /** PCI header type */
  41. #define PCI_HEADER_TYPE 0x0e
  42. #define PCI_HEADER_TYPE_NORMAL 0x00 /**< Normal header */
  43. #define PCI_HEADER_TYPE_BRIDGE 0x01 /**< PCI-to-PCI bridge header */
  44. #define PCI_HEADER_TYPE_CARDBUS 0x02 /**< CardBus header */
  45. #define PCI_HEADER_TYPE_MASK 0x7f /**< Header type mask */
  46. #define PCI_HEADER_TYPE_MULTI 0x80 /**< Multi-function device */
  47. /** PCI base address registers */
  48. #define PCI_BASE_ADDRESS(n) ( 0x10 + ( 4 * (n) ) )
  49. #define PCI_BASE_ADDRESS_0 PCI_BASE_ADDRESS ( 0 )
  50. #define PCI_BASE_ADDRESS_1 PCI_BASE_ADDRESS ( 1 )
  51. #define PCI_BASE_ADDRESS_2 PCI_BASE_ADDRESS ( 2 )
  52. #define PCI_BASE_ADDRESS_3 PCI_BASE_ADDRESS ( 3 )
  53. #define PCI_BASE_ADDRESS_4 PCI_BASE_ADDRESS ( 4 )
  54. #define PCI_BASE_ADDRESS_5 PCI_BASE_ADDRESS ( 5 )
  55. #define PCI_BASE_ADDRESS_SPACE_IO 0x00000001UL /**< I/O BAR */
  56. #define PCI_BASE_ADDRESS_IO_MASK 0x00000003UL /**< I/O BAR mask */
  57. #define PCI_BASE_ADDRESS_MEM_TYPE_64 0x00000004UL /**< 64-bit memory */
  58. #define PCI_BASE_ADDRESS_MEM_TYPE_MASK 0x00000006UL /**< Memory type mask */
  59. #define PCI_BASE_ADDRESS_MEM_MASK 0x0000000fUL /**< Memory BAR mask */
  60. /** PCI subsystem vendor ID */
  61. #define PCI_SUBSYSTEM_VENDOR_ID 0x2c
  62. /** PCI subsystem ID */
  63. #define PCI_SUBSYSTEM_ID 0x2e
  64. /** PCI expansion ROM base address */
  65. #define PCI_ROM_ADDRESS 0x30
  66. /** PCI capabilities pointer */
  67. #define PCI_CAPABILITY_LIST 0x34
  68. /** CardBus capabilities pointer */
  69. #define PCI_CB_CAPABILITY_LIST 0x14
  70. /** PCI interrupt line */
  71. #define PCI_INTERRUPT_LINE 0x3c
  72. /** Capability ID */
  73. #define PCI_CAP_ID 0x00
  74. #define PCI_CAP_ID_PM 0x01 /**< Power management */
  75. #define PCI_CAP_ID_VPD 0x03 /**< Vital product data */
  76. #define PCI_CAP_ID_VNDR 0x09 /**< Vendor-specific */
  77. #define PCI_CAP_ID_EXP 0x10 /**< PCI Express */
  78. #define PCI_CAP_ID_MSIX 0x11 /**< MSI-X */
  79. #define PCI_CAP_ID_EA 0x14 /**< Enhanced Allocation */
  80. /** Next capability */
  81. #define PCI_CAP_NEXT 0x01
  82. /** Power management control and status */
  83. #define PCI_PM_CTRL 0x04
  84. #define PCI_PM_CTRL_STATE_MASK 0x0003 /**< Current power state */
  85. #define PCI_PM_CTRL_PME_ENABLE 0x0100 /**< PME pin enable */
  86. #define PCI_PM_CTRL_PME_STATUS 0x8000 /**< PME pin status */
  87. /** PCI Express */
  88. #define PCI_EXP_DEVCTL 0x08
  89. #define PCI_EXP_DEVCTL_FLR 0x8000 /**< Function level reset */
  90. /** MSI-X interrupts */
  91. #define PCI_MSIX_CTRL 0x02
  92. #define PCI_MSIX_CTRL_ENABLE 0x8000 /**< Enable MSI-X */
  93. #define PCI_MSIX_CTRL_MASK 0x4000 /**< Mask all interrupts */
  94. #define PCI_MSIX_CTRL_SIZE(x) ( (x) & 0x07ff ) /**< Table size */
  95. #define PCI_MSIX_DESC_TABLE 0x04
  96. #define PCI_MSIX_DESC_PBA 0x08
  97. #define PCI_MSIX_DESC_BIR(x) ( (x) & 0x00000007 ) /**< BAR index */
  98. #define PCI_MSIX_DESC_OFFSET(x) ( (x) & 0xfffffff8 ) /**< BAR offset */
  99. /** Uncorrectable error status */
  100. #define PCI_ERR_UNCOR_STATUS 0x04
  101. /** Network controller */
  102. #define PCI_CLASS_NETWORK 0x02
  103. /** Serial bus controller */
  104. #define PCI_CLASS_SERIAL 0x0c
  105. #define PCI_CLASS_SERIAL_USB 0x03 /**< USB controller */
  106. #define PCI_CLASS_SERIAL_USB_UHCI 0x00 /**< UHCI USB controller */
  107. #define PCI_CLASS_SERIAL_USB_OHCI 0x10 /**< OHCI USB controller */
  108. #define PCI_CLASS_SERIAL_USB_EHCI 0x20 /**< ECHI USB controller */
  109. #define PCI_CLASS_SERIAL_USB_XHCI 0x30 /**< xHCI USB controller */
  110. /** Construct PCI class
  111. *
  112. * @v base Base class (or PCI_ANY_ID)
  113. * @v sub Subclass (or PCI_ANY_ID)
  114. * @v progif Programming interface (or PCI_ANY_ID)
  115. */
  116. #define PCI_CLASS( base, sub, progif ) \
  117. ( ( ( (base) & 0xff ) << 16 ) | ( ( (sub) & 0xff ) << 8 ) | \
  118. ( ( (progif) & 0xff) << 0 ) )
  119. /** PCI Express function level reset delay (in ms) */
  120. #define PCI_EXP_FLR_DELAY_MS 100
  121. /** A PCI device ID list entry */
  122. struct pci_device_id {
  123. /** Name */
  124. const char *name;
  125. /** PCI vendor ID */
  126. uint16_t vendor;
  127. /** PCI device ID */
  128. uint16_t device;
  129. /** Arbitrary driver data */
  130. unsigned long driver_data;
  131. };
  132. /** Match-anything ID */
  133. #define PCI_ANY_ID 0xffff
  134. /** A PCI class ID */
  135. struct pci_class_id {
  136. /** Class */
  137. uint32_t class;
  138. /** Class mask */
  139. uint32_t mask;
  140. };
  141. /** Construct PCI class ID
  142. *
  143. * @v base Base class (or PCI_ANY_ID)
  144. * @v sub Subclass (or PCI_ANY_ID)
  145. * @v progif Programming interface (or PCI_ANY_ID)
  146. */
  147. #define PCI_CLASS_ID( base, sub, progif ) { \
  148. .class = PCI_CLASS ( base, sub, progif ), \
  149. .mask = ( ( ( ( (base) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 16 ) | \
  150. ( ( ( (sub) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 8 ) | \
  151. ( ( ( (progif) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 0 ) ), \
  152. }
  153. /** A PCI device */
  154. struct pci_device {
  155. /** Generic device */
  156. struct device dev;
  157. /** Memory base
  158. *
  159. * This is the physical address of the first valid memory BAR.
  160. */
  161. unsigned long membase;
  162. /**
  163. * I/O address
  164. *
  165. * This is the physical address of the first valid I/O BAR.
  166. */
  167. unsigned long ioaddr;
  168. /** Vendor ID */
  169. uint16_t vendor;
  170. /** Device ID */
  171. uint16_t device;
  172. /** Device class */
  173. uint32_t class;
  174. /** Interrupt number */
  175. uint8_t irq;
  176. /** Segment, bus, device, and function (bus:dev.fn) number */
  177. uint32_t busdevfn;
  178. /** Driver for this device */
  179. struct pci_driver *driver;
  180. /** Driver-private data
  181. *
  182. * Use pci_set_drvdata() and pci_get_drvdata() to access this
  183. * field.
  184. */
  185. void *priv;
  186. /** Driver device ID */
  187. struct pci_device_id *id;
  188. };
  189. /** A PCI driver */
  190. struct pci_driver {
  191. /** PCI ID table */
  192. struct pci_device_id *ids;
  193. /** Number of entries in PCI ID table */
  194. unsigned int id_count;
  195. /** PCI class ID */
  196. struct pci_class_id class;
  197. /**
  198. * Probe device
  199. *
  200. * @v pci PCI device
  201. * @ret rc Return status code
  202. */
  203. int ( * probe ) ( struct pci_device *pci );
  204. /**
  205. * Remove device
  206. *
  207. * @v pci PCI device
  208. */
  209. void ( * remove ) ( struct pci_device *pci );
  210. };
  211. /** PCI driver table */
  212. #define PCI_DRIVERS __table ( struct pci_driver, "pci_drivers" )
  213. /** Declare a PCI driver */
  214. #define __pci_driver __table_entry ( PCI_DRIVERS, 01 )
  215. /** Declare a fallback PCI driver */
  216. #define __pci_driver_fallback __table_entry ( PCI_DRIVERS, 02 )
  217. #define PCI_SEG( busdevfn ) ( ( (busdevfn) >> 16 ) & 0xffff )
  218. #define PCI_BUS( busdevfn ) ( ( (busdevfn) >> 8 ) & 0xff )
  219. #define PCI_SLOT( busdevfn ) ( ( (busdevfn) >> 3 ) & 0x1f )
  220. #define PCI_FUNC( busdevfn ) ( ( (busdevfn) >> 0 ) & 0x07 )
  221. #define PCI_BUSDEVFN( segment, bus, slot, func ) \
  222. ( ( (segment) << 16 ) | ( (bus) << 8 ) | \
  223. ( (slot) << 3 ) | ( (func) << 0 ) )
  224. #define PCI_FIRST_FUNC( busdevfn ) ( (busdevfn) & ~0x07 )
  225. #define PCI_LAST_FUNC( busdevfn ) ( (busdevfn) | 0x07 )
  226. #define PCI_BASE_CLASS( class ) ( (class) >> 16 )
  227. #define PCI_SUB_CLASS( class ) ( ( (class) >> 8 ) & 0xff )
  228. #define PCI_PROG_INTF( class ) ( (class) & 0xff )
  229. /*
  230. * PCI_ROM is used to build up entries in a struct pci_id array. It
  231. * is also parsed by parserom.pl to generate Makefile rules and files
  232. * for rom-o-matic.
  233. *
  234. * PCI_ID can be used to generate entries without creating a
  235. * corresponding ROM in the build process.
  236. */
  237. #define PCI_ID( _vendor, _device, _name, _description, _data ) { \
  238. .vendor = _vendor, \
  239. .device = _device, \
  240. .name = _name, \
  241. .driver_data = _data \
  242. }
  243. #define PCI_ROM( _vendor, _device, _name, _description, _data ) \
  244. PCI_ID( _vendor, _device, _name, _description, _data )
  245. /** PCI device debug message format */
  246. #define PCI_FMT "%04x:%02x:%02x.%x"
  247. /** PCI device debug message arguments */
  248. #define PCI_ARGS( pci ) \
  249. PCI_SEG ( (pci)->busdevfn ), PCI_BUS ( (pci)->busdevfn ), \
  250. PCI_SLOT ( (pci)->busdevfn ), PCI_FUNC ( (pci)->busdevfn )
  251. extern void adjust_pci_device ( struct pci_device *pci );
  252. extern unsigned long pci_bar_start ( struct pci_device *pci,
  253. unsigned int reg );
  254. extern int pci_read_config ( struct pci_device *pci );
  255. extern int pci_find_next ( struct pci_device *pci, unsigned int busdevfn );
  256. extern int pci_find_driver ( struct pci_device *pci );
  257. extern int pci_probe ( struct pci_device *pci );
  258. extern void pci_remove ( struct pci_device *pci );
  259. extern int pci_find_capability ( struct pci_device *pci, int capability );
  260. extern int pci_find_next_capability ( struct pci_device *pci,
  261. int pos, int capability );
  262. extern unsigned long pci_bar_size ( struct pci_device *pci, unsigned int reg );
  263. /**
  264. * Initialise PCI device
  265. *
  266. * @v pci PCI device
  267. * @v busdevfn PCI bus:dev.fn address
  268. */
  269. static inline void pci_init ( struct pci_device *pci, unsigned int busdevfn ) {
  270. pci->busdevfn = busdevfn;
  271. }
  272. /**
  273. * Set PCI driver
  274. *
  275. * @v pci PCI device
  276. * @v driver PCI driver
  277. * @v id PCI device ID
  278. */
  279. static inline void pci_set_driver ( struct pci_device *pci,
  280. struct pci_driver *driver,
  281. struct pci_device_id *id ) {
  282. pci->driver = driver;
  283. pci->id = id;
  284. pci->dev.driver_name = id->name;
  285. }
  286. /**
  287. * Set PCI driver-private data
  288. *
  289. * @v pci PCI device
  290. * @v priv Private data
  291. */
  292. static inline void pci_set_drvdata ( struct pci_device *pci, void *priv ) {
  293. pci->priv = priv;
  294. }
  295. /**
  296. * Get PCI driver-private data
  297. *
  298. * @v pci PCI device
  299. * @ret priv Private data
  300. */
  301. static inline void * pci_get_drvdata ( struct pci_device *pci ) {
  302. return pci->priv;
  303. }
  304. #endif /* _IPXE_PCI_H */