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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "stdint.h"
  2. #include "string.h"
  3. #include "console.h"
  4. #include "nic.h"
  5. #include <gpxe/pci.h>
  6. /*
  7. * pci_io.c may know how many buses we have, in which case it can
  8. * overwrite this value.
  9. *
  10. */
  11. unsigned int pci_max_bus = 0xff;
  12. /*
  13. * Increment a bus_loc structure to the next possible PCI location.
  14. * Leave the structure zeroed and return 0 if there are no more valid
  15. * locations.
  16. *
  17. */
  18. static int pci_next_location ( struct bus_loc *bus_loc ) {
  19. struct pci_loc *pci_loc = ( struct pci_loc * ) bus_loc;
  20. /*
  21. * Ensure that there is sufficient space in the shared bus
  22. * structures for a struct pci_loc and a struct
  23. * pci_dev, as mandated by bus.h.
  24. *
  25. */
  26. BUS_LOC_CHECK ( struct pci_loc );
  27. BUS_DEV_CHECK ( struct pci_device );
  28. return ( ++pci_loc->busdevfn );
  29. }
  30. /*
  31. * Fill in parameters (vendor & device ids, class, membase etc.) for a
  32. * PCI device based on bus & devfn.
  33. *
  34. * Returns 1 if a device was found, 0 for no device present.
  35. *
  36. */
  37. static int pci_fill_device ( struct bus_dev *bus_dev,
  38. struct bus_loc *bus_loc ) {
  39. struct pci_loc *pci_loc = ( struct pci_loc * ) bus_loc;
  40. struct pci_device *pci = ( struct pci_device * ) bus_dev;
  41. uint16_t busdevfn = pci_loc->busdevfn;
  42. static struct {
  43. uint16_t busdevfn0;
  44. int is_present;
  45. } cache = { 0, 1 };
  46. uint32_t l;
  47. int reg;
  48. /* Store busdevfn in struct pci_device and set default values */
  49. pci->busdevfn = busdevfn;
  50. pci->name = "?";
  51. /* Check bus is within range */
  52. if ( PCI_BUS ( busdevfn ) > pci_max_bus ) {
  53. return 0;
  54. }
  55. /* Check to see if we've cached the result that this is a
  56. * non-zero function on a non-existent card. This is done to
  57. * increase scan speed by a factor of 8.
  58. */
  59. if ( ( PCI_FUNC ( busdevfn ) != 0 ) &&
  60. ( PCI_FN0 ( busdevfn ) == cache.busdevfn0 ) &&
  61. ( ! cache.is_present ) ) {
  62. return 0;
  63. }
  64. /* Check to see if there's anything physically present.
  65. */
  66. pci_read_config_dword ( pci, PCI_VENDOR_ID, &l );
  67. /* some broken boards return 0 if a slot is empty: */
  68. if ( ( l == 0xffffffff ) || ( l == 0x00000000 ) ) {
  69. if ( PCI_FUNC ( busdevfn ) == 0 ) {
  70. /* Don't look for subsequent functions if the
  71. * card itself is not present.
  72. */
  73. cache.busdevfn0 = busdevfn;
  74. cache.is_present = 0;
  75. }
  76. return 0;
  77. }
  78. pci->vendor_id = l & 0xffff;
  79. pci->device_id = ( l >> 16 ) & 0xffff;
  80. /* Check that we're not a duplicate function on a
  81. * non-multifunction device.
  82. */
  83. if ( PCI_FUNC ( busdevfn ) != 0 ) {
  84. uint8_t header_type;
  85. pci->busdevfn &= PCI_FN0 ( busdevfn );
  86. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &header_type );
  87. pci->busdevfn = busdevfn;
  88. if ( ! ( header_type & 0x80 ) ) {
  89. return 0;
  90. }
  91. }
  92. /* Get device class */
  93. pci_read_config_word ( pci, PCI_SUBCLASS_CODE, &pci->class );
  94. /* Get revision */
  95. pci_read_config_byte ( pci, PCI_REVISION, &pci->revision );
  96. /* Get the "membase" */
  97. pci_read_config_dword ( pci, PCI_BASE_ADDRESS_1, &pci->membase );
  98. /* Get the "ioaddr" */
  99. pci->ioaddr = 0;
  100. for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
  101. pci_read_config_dword ( pci, reg, &pci->ioaddr );
  102. if ( pci->ioaddr & PCI_BASE_ADDRESS_SPACE_IO ) {
  103. pci->ioaddr &= PCI_BASE_ADDRESS_IO_MASK;
  104. if ( pci->ioaddr ) {
  105. break;
  106. }
  107. }
  108. pci->ioaddr = 0;
  109. }
  110. /* Get the irq */
  111. pci_read_config_byte ( pci, PCI_INTERRUPT_PIN, &pci->irq );
  112. if ( pci->irq ) {
  113. pci_read_config_byte ( pci, PCI_INTERRUPT_LINE, &pci->irq );
  114. }
  115. DBG ( "PCI found device %hhx:%hhx.%d Class %hx: %hx:%hx (rev %hhx)\n",
  116. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  117. PCI_FUNC ( pci->busdevfn ), pci->class, pci->vendor_id,
  118. pci->device_id, pci->revision );
  119. return 1;
  120. }
  121. /*
  122. * Test whether or not a driver is capable of driving the device.
  123. *
  124. */
  125. static int pci_check_driver ( struct bus_dev *bus_dev,
  126. struct device_driver *device_driver ) {
  127. struct pci_device *pci = ( struct pci_device * ) bus_dev;
  128. struct pci_driver *pci_driver
  129. = ( struct pci_driver * ) device_driver->bus_driver_info;
  130. unsigned int i;
  131. /* If driver has a class, and class matches, use it */
  132. if ( pci_driver->class &&
  133. ( pci_driver->class == pci->class ) ) {
  134. DBG ( "PCI driver %s matches class %hx\n",
  135. device_driver->name, pci_driver->class );
  136. pci->name = device_driver->name;
  137. return 1;
  138. }
  139. /* If any of driver's IDs match, use it */
  140. for ( i = 0 ; i < pci_driver->id_count; i++ ) {
  141. struct pci_id *id = &pci_driver->ids[i];
  142. if ( ( pci->vendor_id == id->vendor_id ) &&
  143. ( pci->device_id == id->device_id ) ) {
  144. DBG ( "PCI driver %s device %s matches ID %hx:%hx\n",
  145. device_driver->name, id->name,
  146. id->vendor_id, id->device_id );
  147. pci->name = id->name;
  148. return 1;
  149. }
  150. }
  151. return 0;
  152. }
  153. /*
  154. * Describe a PCI device
  155. *
  156. */
  157. static char * pci_describe_device ( struct bus_dev *bus_dev ) {
  158. struct pci_device *pci = ( struct pci_device * ) bus_dev;
  159. static char pci_description[] = "PCI 00:00.0";
  160. sprintf ( pci_description + 4, "%hhx:%hhx.%d",
  161. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  162. PCI_FUNC ( pci->busdevfn ) );
  163. return pci_description;
  164. }
  165. /*
  166. * Name a PCI device
  167. *
  168. */
  169. static const char * pci_name_device ( struct bus_dev *bus_dev ) {
  170. struct pci_device *pci = ( struct pci_device * ) bus_dev;
  171. return pci->name;
  172. }
  173. /*
  174. * PCI bus operations table
  175. *
  176. */
  177. struct bus_driver pci_driver __bus_driver = {
  178. .name = "PCI",
  179. .next_location = pci_next_location,
  180. .fill_device = pci_fill_device,
  181. .check_driver = pci_check_driver,
  182. .describe_device = pci_describe_device,
  183. .name_device = pci_name_device,
  184. };
  185. /*
  186. * Set device to be a busmaster in case BIOS neglected to do so. Also
  187. * adjust PCI latency timer to a reasonable value, 32.
  188. */
  189. void adjust_pci_device ( struct pci_device *pci ) {
  190. unsigned short new_command, pci_command;
  191. unsigned char pci_latency;
  192. pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
  193. new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  194. if ( pci_command != new_command ) {
  195. DBG ( "PCI BIOS has not enabled device %hhx:%hhx.%d! "
  196. "Updating PCI command %hX->%hX\n",
  197. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  198. PCI_FUNC ( pci->busdevfn ), pci_command, new_command );
  199. pci_write_config_word ( pci, PCI_COMMAND, new_command );
  200. }
  201. pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
  202. if ( pci_latency < 32 ) {
  203. DBG ( "PCI device %hhx:%hhx.%d latency timer is "
  204. "unreasonably low at %d. Setting to 32.\n",
  205. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  206. PCI_FUNC ( pci->busdevfn ), pci_latency );
  207. pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
  208. }
  209. }
  210. /*
  211. * Find the start of a pci resource.
  212. */
  213. unsigned long pci_bar_start ( struct pci_device *pci, unsigned int index ) {
  214. uint32_t lo, hi;
  215. unsigned long bar;
  216. pci_read_config_dword ( pci, index, &lo );
  217. if ( lo & PCI_BASE_ADDRESS_SPACE_IO ) {
  218. bar = lo & PCI_BASE_ADDRESS_IO_MASK;
  219. } else {
  220. bar = 0;
  221. if ( ( lo & PCI_BASE_ADDRESS_MEM_TYPE_MASK ) ==
  222. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  223. pci_read_config_dword ( pci, index + 4, &hi );
  224. if ( hi ) {
  225. #if ULONG_MAX > 0xffffffff
  226. bar = hi;
  227. bar <<= 32;
  228. #else
  229. printf ( "Unhandled 64bit BAR %08x:%08x\n",
  230. hi, lo );
  231. return -1UL;
  232. #endif
  233. }
  234. }
  235. bar |= lo & PCI_BASE_ADDRESS_MEM_MASK;
  236. }
  237. return bar + pci_bus_base ( pci );
  238. }
  239. /*
  240. * Find the size of a pci resource.
  241. */
  242. unsigned long pci_bar_size ( struct pci_device *pci, unsigned int bar ) {
  243. uint32_t start, size;
  244. /* Save the original bar */
  245. pci_read_config_dword ( pci, bar, &start );
  246. /* Compute which bits can be set */
  247. pci_write_config_dword ( pci, bar, ~0 );
  248. pci_read_config_dword ( pci, bar, &size );
  249. /* Restore the original size */
  250. pci_write_config_dword ( pci, bar, start );
  251. /* Find the significant bits */
  252. if ( start & PCI_BASE_ADDRESS_SPACE_IO ) {
  253. size &= PCI_BASE_ADDRESS_IO_MASK;
  254. } else {
  255. size &= PCI_BASE_ADDRESS_MEM_MASK;
  256. }
  257. /* Find the lowest bit set */
  258. size = size & ~( size - 1 );
  259. return size;
  260. }
  261. /**
  262. * pci_find_capability - query for devices' capabilities
  263. * @pci: PCI device to query
  264. * @cap: capability code
  265. *
  266. * Tell if a device supports a given PCI capability.
  267. * Returns the address of the requested capability structure within the
  268. * device's PCI configuration space or 0 in case the device does not
  269. * support it. Possible values for @cap:
  270. *
  271. * %PCI_CAP_ID_PM Power Management
  272. *
  273. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  274. *
  275. * %PCI_CAP_ID_VPD Vital Product Data
  276. *
  277. * %PCI_CAP_ID_SLOTID Slot Identification
  278. *
  279. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  280. *
  281. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  282. */
  283. int pci_find_capability ( struct pci_device *pci, int cap ) {
  284. uint16_t status;
  285. uint8_t pos, id;
  286. uint8_t hdr_type;
  287. int ttl = 48;
  288. pci_read_config_word ( pci, PCI_STATUS, &status );
  289. if ( ! ( status & PCI_STATUS_CAP_LIST ) )
  290. return 0;
  291. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &hdr_type );
  292. switch ( hdr_type & 0x7F ) {
  293. case PCI_HEADER_TYPE_NORMAL:
  294. case PCI_HEADER_TYPE_BRIDGE:
  295. default:
  296. pci_read_config_byte ( pci, PCI_CAPABILITY_LIST, &pos );
  297. break;
  298. case PCI_HEADER_TYPE_CARDBUS:
  299. pci_read_config_byte ( pci, PCI_CB_CAPABILITY_LIST, &pos );
  300. break;
  301. }
  302. while ( ttl-- && pos >= 0x40 ) {
  303. pos &= ~3;
  304. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_ID, &id );
  305. DBG ( "PCI Capability: %d\n", id );
  306. if ( id == 0xff )
  307. break;
  308. if ( id == cap )
  309. return pos;
  310. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_NEXT, &pos );
  311. }
  312. return 0;
  313. }
  314. /*
  315. * Fill in a nic structure
  316. *
  317. */
  318. void pci_fill_nic ( struct nic *nic, struct pci_device *pci ) {
  319. /* Fill in ioaddr and irqno */
  320. nic->ioaddr = pci->ioaddr;
  321. nic->irqno = pci->irq;
  322. /* Fill in DHCP device ID structure */
  323. nic->dhcp_dev_id.bus_type = PCI_BUS_TYPE;
  324. nic->dhcp_dev_id.vendor_id = htons ( pci->vendor_id );
  325. nic->dhcp_dev_id.device_id = htons ( pci->device_id );
  326. }