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.0KB

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