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.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "etherboot.h"
  2. #include "pci.h"
  3. /*
  4. * Ensure that there is sufficient space in the shared dev_bus
  5. * structure for a struct pci_device.
  6. *
  7. */
  8. DEV_BUS( struct pci_device, pci_dev );
  9. static char pci_magic[0]; /* guaranteed unique symbol */
  10. /*
  11. * pci_io.c may know how many buses we have, in which case it can
  12. * overwrite this value.
  13. *
  14. */
  15. unsigned int pci_max_bus = 0xff;
  16. /*
  17. * Fill in parameters (vendor & device ids, class, membase etc.) for a
  18. * PCI device based on bus & devfn.
  19. *
  20. * Returns 1 if a device was found, 0 for no device present.
  21. */
  22. static int fill_pci_device ( struct pci_device *pci ) {
  23. uint32_t l;
  24. int reg;
  25. /* Check bus is within range */
  26. if ( PCI_BUS ( pci->busdevfn ) > pci_max_bus )
  27. return 0;
  28. /* Check to see if there's anything physically present.
  29. */
  30. pci_read_config_dword ( pci, PCI_VENDOR_ID, &l );
  31. /* some broken boards return 0 if a slot is empty: */
  32. if ( ( l == 0xffffffff ) || ( l == 0x00000000 ) ) {
  33. return 0;
  34. }
  35. pci->vendor = l & 0xffff;
  36. pci->dev_id = ( l >> 16 ) & 0xffff;
  37. /* Check that we're not a duplicate function on a
  38. * non-multifunction device.
  39. */
  40. if ( PCI_FUNC ( pci->busdevfn ) != 0 ) {
  41. uint16_t save_busdevfn = pci->busdevfn;
  42. uint8_t header_type;
  43. pci->busdevfn &= ~PCI_FUNC ( 0xffff );
  44. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &header_type );
  45. pci->busdevfn = save_busdevfn;
  46. if ( ! ( header_type & 0x80 ) ) {
  47. return 0;
  48. }
  49. }
  50. /* Get device class */
  51. pci_read_config_word ( pci, PCI_SUBCLASS_CODE, &pci->class );
  52. /* Get revision */
  53. pci_read_config_byte ( pci, PCI_REVISION, &pci->revision );
  54. /* Get the "membase" */
  55. pci_read_config_dword ( pci, PCI_BASE_ADDRESS_1, &pci->membase );
  56. /* Get the "ioaddr" */
  57. pci->ioaddr = 0;
  58. for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
  59. pci_read_config_dword ( pci, reg, &pci->ioaddr );
  60. if ( pci->ioaddr & PCI_BASE_ADDRESS_SPACE_IO ) {
  61. pci->ioaddr &= PCI_BASE_ADDRESS_IO_MASK;
  62. if ( pci->ioaddr ) {
  63. break;
  64. }
  65. }
  66. pci->ioaddr = 0;
  67. }
  68. /* Get the irq */
  69. pci_read_config_byte ( pci, PCI_INTERRUPT_PIN, &pci->irq );
  70. if ( pci->irq ) {
  71. pci_read_config_byte ( pci, PCI_INTERRUPT_LINE, &pci->irq );
  72. }
  73. DBG ( "PCI found device %hhx:%hhx.%d Class %hx: %hx:%hx (rev %hhx)\n",
  74. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  75. PCI_FUNC ( pci->busdevfn ), pci->class, pci->vendor, pci->dev_id,
  76. pci->revision );
  77. return 1;
  78. }
  79. /*
  80. * Set device to be a busmaster in case BIOS neglected to do so. Also
  81. * adjust PCI latency timer to a reasonable value, 32.
  82. */
  83. void adjust_pci_device ( struct pci_device *pci ) {
  84. unsigned short new_command, pci_command;
  85. unsigned char pci_latency;
  86. pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
  87. new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  88. if ( pci_command != new_command ) {
  89. DBG ( "PCI BIOS has not enabled device %hhx:%hhx.%d! "
  90. "Updating PCI command %hX->%hX\n",
  91. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  92. PCI_FUNC ( pci->busdevfn ), pci_command, new_command );
  93. pci_write_config_word ( pci, PCI_COMMAND, new_command );
  94. }
  95. pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
  96. if ( pci_latency < 32 ) {
  97. DBG ( "PCI device %hhx:%hhx.%d latency timer is "
  98. "unreasonably low at %d. Setting to 32.\n",
  99. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  100. PCI_FUNC ( pci->busdevfn ), pci_latency );
  101. pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
  102. }
  103. }
  104. /*
  105. * Set PCI device to use.
  106. *
  107. * This routine can be called by e.g. the ROM prefix to specify that
  108. * the first device to be tried should be the device on which the ROM
  109. * was physically located.
  110. *
  111. */
  112. void set_pci_device ( uint16_t busdevfn ) {
  113. pci_dev.magic = pci_magic;
  114. pci_dev.busdevfn = busdevfn;
  115. pci_dev.already_tried = 0;
  116. }
  117. /*
  118. * Find a PCI device matching the specified driver
  119. *
  120. */
  121. int find_pci_device ( struct pci_device *pci,
  122. struct pci_driver *driver ) {
  123. int i;
  124. /* Initialise struct pci if it's the first time it's been used. */
  125. if ( pci->magic != pci_magic ) {
  126. memset ( pci, 0, sizeof ( *pci ) );
  127. pci->magic = pci_magic;
  128. }
  129. /* Iterate through all possible PCI bus:dev.fn combinations,
  130. * starting where we left off.
  131. */
  132. DBG ( "PCI searching for device matching driver %s\n", driver->name );
  133. do {
  134. /* If we've already used this device, skip it */
  135. if ( pci->already_tried ) {
  136. pci->already_tried = 0;
  137. continue;
  138. }
  139. /* Fill in device parameters, if device present */
  140. if ( ! fill_pci_device ( pci ) ) {
  141. continue;
  142. }
  143. /* Fix up PCI device */
  144. adjust_pci_device ( pci );
  145. /* If driver has a class, and class matches, use it */
  146. if ( driver->class &&
  147. ( driver->class == pci->class ) ) {
  148. DBG ( "PCI found class %hx matching driver %s\n",
  149. driver->class, driver->name );
  150. pci->name = driver->name;
  151. pci->already_tried = 1;
  152. return 1;
  153. }
  154. /* If any of driver's IDs match, use it */
  155. for ( i = 0 ; i < driver->id_count; i++ ) {
  156. struct pci_id *id = &driver->ids[i];
  157. if ( ( pci->vendor == id->vendor ) &&
  158. ( pci->dev_id == id->dev_id ) ) {
  159. DBG ( "PCI found ID %hx:%hx (device %s) "
  160. "matching driver %s\n", id->vendor,
  161. id->dev_id, id->name, driver->name );
  162. pci->name = id->name;
  163. pci->already_tried = 1;
  164. return 1;
  165. }
  166. }
  167. } while ( ++pci->busdevfn );
  168. /* No device found */
  169. DBG ( "PCI found no device matching driver %s\n", driver->name );
  170. return 0;
  171. }
  172. /*
  173. * Find the next PCI device that can be used to boot using the
  174. * specified driver.
  175. *
  176. */
  177. int find_pci_boot_device ( struct dev *dev, struct pci_driver *driver ) {
  178. struct pci_device *pci = ( struct pci_device * )dev->bus;
  179. if ( ! find_pci_device ( pci, driver ) )
  180. return 0;
  181. dev->name = pci->name;
  182. dev->devid.bus_type = PCI_BUS_TYPE;
  183. dev->devid.vendor_id = pci->vendor;
  184. dev->devid.device_id = pci->dev_id;
  185. return 1;
  186. }
  187. /*
  188. * Find the start of a pci resource.
  189. */
  190. unsigned long pci_bar_start ( struct pci_device *pci, unsigned int index ) {
  191. uint32_t lo, hi;
  192. unsigned long bar;
  193. pci_read_config_dword ( pci, index, &lo );
  194. if ( lo & PCI_BASE_ADDRESS_SPACE_IO ) {
  195. bar = lo & PCI_BASE_ADDRESS_IO_MASK;
  196. } else {
  197. bar = 0;
  198. if ( ( lo & PCI_BASE_ADDRESS_MEM_TYPE_MASK ) ==
  199. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  200. pci_read_config_dword ( pci, index + 4, &hi );
  201. if ( hi ) {
  202. #if ULONG_MAX > 0xffffffff
  203. bar = hi;
  204. bar <<= 32;
  205. #else
  206. printf ( "Unhandled 64bit BAR\n" );
  207. return -1UL;
  208. #endif
  209. }
  210. }
  211. bar |= lo & PCI_BASE_ADDRESS_MEM_MASK;
  212. }
  213. return bar + pci_bus_base ( pci );
  214. }
  215. /*
  216. * Find the size of a pci resource.
  217. */
  218. unsigned long pci_bar_size ( struct pci_device *pci, unsigned int bar ) {
  219. uint32_t start, size;
  220. /* Save the original bar */
  221. pci_read_config_dword ( pci, bar, &start );
  222. /* Compute which bits can be set */
  223. pci_write_config_dword ( pci, bar, ~0 );
  224. pci_read_config_dword ( pci, bar, &size );
  225. /* Restore the original size */
  226. pci_write_config_dword ( pci, bar, start );
  227. /* Find the significant bits */
  228. if ( start & PCI_BASE_ADDRESS_SPACE_IO ) {
  229. size &= PCI_BASE_ADDRESS_IO_MASK;
  230. } else {
  231. size &= PCI_BASE_ADDRESS_MEM_MASK;
  232. }
  233. /* Find the lowest bit set */
  234. size = size & ~( size - 1 );
  235. return size;
  236. }
  237. /**
  238. * pci_find_capability - query for devices' capabilities
  239. * @pci: PCI device to query
  240. * @cap: capability code
  241. *
  242. * Tell if a device supports a given PCI capability.
  243. * Returns the address of the requested capability structure within the
  244. * device's PCI configuration space or 0 in case the device does not
  245. * support it. Possible values for @cap:
  246. *
  247. * %PCI_CAP_ID_PM Power Management
  248. *
  249. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  250. *
  251. * %PCI_CAP_ID_VPD Vital Product Data
  252. *
  253. * %PCI_CAP_ID_SLOTID Slot Identification
  254. *
  255. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  256. *
  257. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  258. */
  259. int pci_find_capability ( struct pci_device *pci, int cap ) {
  260. uint16_t status;
  261. uint8_t pos, id;
  262. uint8_t hdr_type;
  263. int ttl = 48;
  264. pci_read_config_word ( pci, PCI_STATUS, &status );
  265. if ( ! ( status & PCI_STATUS_CAP_LIST ) )
  266. return 0;
  267. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &hdr_type );
  268. switch ( hdr_type & 0x7F ) {
  269. case PCI_HEADER_TYPE_NORMAL:
  270. case PCI_HEADER_TYPE_BRIDGE:
  271. default:
  272. pci_read_config_byte ( pci, PCI_CAPABILITY_LIST, &pos );
  273. break;
  274. case PCI_HEADER_TYPE_CARDBUS:
  275. pci_read_config_byte ( pci, PCI_CB_CAPABILITY_LIST, &pos );
  276. break;
  277. }
  278. while ( ttl-- && pos >= 0x40 ) {
  279. pos &= ~3;
  280. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_ID, &id );
  281. DBG ( "Capability: %d\n", id );
  282. if ( id == 0xff )
  283. break;
  284. if ( id == cap )
  285. return pos;
  286. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_NEXT, &pos );
  287. }
  288. return 0;
  289. }