您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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