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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_EA 0x14 /**< Enhanced Allocation */
  79. /** Next capability */
  80. #define PCI_CAP_NEXT 0x01
  81. /** Power management control and status */
  82. #define PCI_PM_CTRL 0x04
  83. #define PCI_PM_CTRL_STATE_MASK 0x0003 /**< Current power state */
  84. #define PCI_PM_CTRL_PME_ENABLE 0x0100 /**< PME pin enable */
  85. #define PCI_PM_CTRL_PME_STATUS 0x8000 /**< PME pin status */
  86. /** PCI Express */
  87. #define PCI_EXP_DEVCTL 0x08
  88. #define PCI_EXP_DEVCTL_FLR 0x8000 /**< Function level reset */
  89. /** Uncorrectable error status */
  90. #define PCI_ERR_UNCOR_STATUS 0x04
  91. /** Network controller */
  92. #define PCI_CLASS_NETWORK 0x02
  93. /** Serial bus controller */
  94. #define PCI_CLASS_SERIAL 0x0c
  95. #define PCI_CLASS_SERIAL_USB 0x03 /**< USB controller */
  96. #define PCI_CLASS_SERIAL_USB_UHCI 0x00 /**< UHCI USB controller */
  97. #define PCI_CLASS_SERIAL_USB_OHCI 0x10 /**< OHCI USB controller */
  98. #define PCI_CLASS_SERIAL_USB_EHCI 0x20 /**< ECHI USB controller */
  99. #define PCI_CLASS_SERIAL_USB_XHCI 0x30 /**< xHCI USB controller */
  100. /** Construct PCI class
  101. *
  102. * @v base Base class (or PCI_ANY_ID)
  103. * @v sub Subclass (or PCI_ANY_ID)
  104. * @v progif Programming interface (or PCI_ANY_ID)
  105. */
  106. #define PCI_CLASS( base, sub, progif ) \
  107. ( ( ( (base) & 0xff ) << 16 ) | ( ( (sub) & 0xff ) << 8 ) | \
  108. ( ( (progif) & 0xff) << 0 ) )
  109. /** PCI Express function level reset delay (in ms) */
  110. #define PCI_EXP_FLR_DELAY_MS 100
  111. /** A PCI device ID list entry */
  112. struct pci_device_id {
  113. /** Name */
  114. const char *name;
  115. /** PCI vendor ID */
  116. uint16_t vendor;
  117. /** PCI device ID */
  118. uint16_t device;
  119. /** Arbitrary driver data */
  120. unsigned long driver_data;
  121. };
  122. /** Match-anything ID */
  123. #define PCI_ANY_ID 0xffff
  124. /** A PCI class ID */
  125. struct pci_class_id {
  126. /** Class */
  127. uint32_t class;
  128. /** Class mask */
  129. uint32_t mask;
  130. };
  131. /** Construct PCI class ID
  132. *
  133. * @v base Base class (or PCI_ANY_ID)
  134. * @v sub Subclass (or PCI_ANY_ID)
  135. * @v progif Programming interface (or PCI_ANY_ID)
  136. */
  137. #define PCI_CLASS_ID( base, sub, progif ) { \
  138. .class = PCI_CLASS ( base, sub, progif ), \
  139. .mask = ( ( ( ( (base) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 16 ) | \
  140. ( ( ( (sub) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 8 ) | \
  141. ( ( ( (progif) == PCI_ANY_ID ) ? 0x00 : 0xff ) << 0 ) ), \
  142. }
  143. /** A PCI device */
  144. struct pci_device {
  145. /** Generic device */
  146. struct device dev;
  147. /** Memory base
  148. *
  149. * This is the physical address of the first valid memory BAR.
  150. */
  151. unsigned long membase;
  152. /**
  153. * I/O address
  154. *
  155. * This is the physical address of the first valid I/O BAR.
  156. */
  157. unsigned long ioaddr;
  158. /** Vendor ID */
  159. uint16_t vendor;
  160. /** Device ID */
  161. uint16_t device;
  162. /** Device class */
  163. uint32_t class;
  164. /** Interrupt number */
  165. uint8_t irq;
  166. /** Segment, bus, device, and function (bus:dev.fn) number */
  167. uint32_t busdevfn;
  168. /** Driver for this device */
  169. struct pci_driver *driver;
  170. /** Driver-private data
  171. *
  172. * Use pci_set_drvdata() and pci_get_drvdata() to access this
  173. * field.
  174. */
  175. void *priv;
  176. /** Driver device ID */
  177. struct pci_device_id *id;
  178. };
  179. /** A PCI driver */
  180. struct pci_driver {
  181. /** PCI ID table */
  182. struct pci_device_id *ids;
  183. /** Number of entries in PCI ID table */
  184. unsigned int id_count;
  185. /** PCI class ID */
  186. struct pci_class_id class;
  187. /**
  188. * Probe device
  189. *
  190. * @v pci PCI device
  191. * @ret rc Return status code
  192. */
  193. int ( * probe ) ( struct pci_device *pci );
  194. /**
  195. * Remove device
  196. *
  197. * @v pci PCI device
  198. */
  199. void ( * remove ) ( struct pci_device *pci );
  200. };
  201. /** PCI driver table */
  202. #define PCI_DRIVERS __table ( struct pci_driver, "pci_drivers" )
  203. /** Declare a PCI driver */
  204. #define __pci_driver __table_entry ( PCI_DRIVERS, 01 )
  205. /** Declare a fallback PCI driver */
  206. #define __pci_driver_fallback __table_entry ( PCI_DRIVERS, 02 )
  207. #define PCI_SEG( busdevfn ) ( ( (busdevfn) >> 16 ) & 0xffff )
  208. #define PCI_BUS( busdevfn ) ( ( (busdevfn) >> 8 ) & 0xff )
  209. #define PCI_SLOT( busdevfn ) ( ( (busdevfn) >> 3 ) & 0x1f )
  210. #define PCI_FUNC( busdevfn ) ( ( (busdevfn) >> 0 ) & 0x07 )
  211. #define PCI_BUSDEVFN( segment, bus, slot, func ) \
  212. ( ( (segment) << 16 ) | ( (bus) << 8 ) | \
  213. ( (slot) << 3 ) | ( (func) << 0 ) )
  214. #define PCI_FIRST_FUNC( busdevfn ) ( (busdevfn) & ~0x07 )
  215. #define PCI_LAST_FUNC( busdevfn ) ( (busdevfn) | 0x07 )
  216. #define PCI_BASE_CLASS( class ) ( (class) >> 16 )
  217. #define PCI_SUB_CLASS( class ) ( ( (class) >> 8 ) & 0xff )
  218. #define PCI_PROG_INTF( class ) ( (class) & 0xff )
  219. /*
  220. * PCI_ROM is used to build up entries in a struct pci_id array. It
  221. * is also parsed by parserom.pl to generate Makefile rules and files
  222. * for rom-o-matic.
  223. *
  224. * PCI_ID can be used to generate entries without creating a
  225. * corresponding ROM in the build process.
  226. */
  227. #define PCI_ID( _vendor, _device, _name, _description, _data ) { \
  228. .vendor = _vendor, \
  229. .device = _device, \
  230. .name = _name, \
  231. .driver_data = _data \
  232. }
  233. #define PCI_ROM( _vendor, _device, _name, _description, _data ) \
  234. PCI_ID( _vendor, _device, _name, _description, _data )
  235. /** PCI device debug message format */
  236. #define PCI_FMT "%04x:%02x:%02x.%x"
  237. /** PCI device debug message arguments */
  238. #define PCI_ARGS( pci ) \
  239. PCI_SEG ( (pci)->busdevfn ), PCI_BUS ( (pci)->busdevfn ), \
  240. PCI_SLOT ( (pci)->busdevfn ), PCI_FUNC ( (pci)->busdevfn )
  241. extern void adjust_pci_device ( struct pci_device *pci );
  242. extern unsigned long pci_bar_start ( struct pci_device *pci,
  243. unsigned int reg );
  244. extern int pci_read_config ( struct pci_device *pci );
  245. extern int pci_find_next ( struct pci_device *pci, unsigned int busdevfn );
  246. extern int pci_find_driver ( struct pci_device *pci );
  247. extern int pci_probe ( struct pci_device *pci );
  248. extern void pci_remove ( struct pci_device *pci );
  249. extern int pci_find_capability ( struct pci_device *pci, int capability );
  250. extern int pci_find_next_capability ( struct pci_device *pci,
  251. int pos, int capability );
  252. extern unsigned long pci_bar_size ( struct pci_device *pci, unsigned int reg );
  253. /**
  254. * Initialise PCI device
  255. *
  256. * @v pci PCI device
  257. * @v busdevfn PCI bus:dev.fn address
  258. */
  259. static inline void pci_init ( struct pci_device *pci, unsigned int busdevfn ) {
  260. pci->busdevfn = busdevfn;
  261. }
  262. /**
  263. * Set PCI driver
  264. *
  265. * @v pci PCI device
  266. * @v driver PCI driver
  267. * @v id PCI device ID
  268. */
  269. static inline void pci_set_driver ( struct pci_device *pci,
  270. struct pci_driver *driver,
  271. struct pci_device_id *id ) {
  272. pci->driver = driver;
  273. pci->id = id;
  274. pci->dev.driver_name = id->name;
  275. }
  276. /**
  277. * Set PCI driver-private data
  278. *
  279. * @v pci PCI device
  280. * @v priv Private data
  281. */
  282. static inline void pci_set_drvdata ( struct pci_device *pci, void *priv ) {
  283. pci->priv = priv;
  284. }
  285. /**
  286. * Get PCI driver-private data
  287. *
  288. * @v pci PCI device
  289. * @ret priv Private data
  290. */
  291. static inline void * pci_get_drvdata ( struct pci_device *pci ) {
  292. return pci->priv;
  293. }
  294. #endif /* _IPXE_PCI_H */