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

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