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 "stdint.h"
  2. #include "string.h"
  3. #include "console.h"
  4. #include "pci.h"
  5. /*
  6. * Ensure that there is sufficient space in the shared dev_bus
  7. * structure for a struct pci_device.
  8. *
  9. */
  10. DEV_BUS( struct pci_device, pci_dev );
  11. static char pci_magic[0]; /* guaranteed unique symbol */
  12. /*
  13. * pci_io.c may know how many buses we have, in which case it can
  14. * overwrite this value.
  15. *
  16. */
  17. unsigned int pci_max_bus = 0xff;
  18. /*
  19. * Fill in parameters (vendor & device ids, class, membase etc.) for a
  20. * PCI device based on bus & devfn.
  21. *
  22. * Returns 1 if a device was found, 0 for no device present.
  23. */
  24. static int fill_pci_device ( struct pci_device *pci ) {
  25. static struct {
  26. uint16_t devfn0;
  27. int is_present;
  28. } cache = { 0, 1 };
  29. uint32_t l;
  30. int reg;
  31. /* Check bus is within range */
  32. if ( PCI_BUS ( pci->busdevfn ) > pci_max_bus ) {
  33. return 0;
  34. }
  35. /* Check to see if we've cached the result that this is a
  36. * non-zero function on a non-existent card. This is done to
  37. * increase scan speed by a factor of 8.
  38. */
  39. if ( ( PCI_FUNC ( pci->busdevfn ) != 0 ) &&
  40. ( PCI_FN0 ( pci->busdevfn ) == cache.devfn0 ) &&
  41. ( ! cache.is_present ) ) {
  42. return 0;
  43. }
  44. /* Check to see if there's anything physically present.
  45. */
  46. pci_read_config_dword ( pci, PCI_VENDOR_ID, &l );
  47. /* some broken boards return 0 if a slot is empty: */
  48. if ( ( l == 0xffffffff ) || ( l == 0x00000000 ) ) {
  49. if ( PCI_FUNC ( pci->busdevfn ) == 0 ) {
  50. /* Don't look for subsequent functions if the
  51. * card itself is not present.
  52. */
  53. cache.devfn0 = pci->busdevfn;
  54. cache.is_present = 0;
  55. }
  56. return 0;
  57. }
  58. pci->vendor = l & 0xffff;
  59. pci->dev_id = ( l >> 16 ) & 0xffff;
  60. /* Check that we're not a duplicate function on a
  61. * non-multifunction device.
  62. */
  63. if ( PCI_FUNC ( pci->busdevfn ) != 0 ) {
  64. uint16_t save_busdevfn = pci->busdevfn;
  65. uint8_t header_type;
  66. pci->busdevfn &= PCI_FN0 ( pci->busdevfn );
  67. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &header_type );
  68. pci->busdevfn = save_busdevfn;
  69. if ( ! ( header_type & 0x80 ) ) {
  70. return 0;
  71. }
  72. }
  73. /* Get device class */
  74. pci_read_config_word ( pci, PCI_SUBCLASS_CODE, &pci->class );
  75. /* Get revision */
  76. pci_read_config_byte ( pci, PCI_REVISION, &pci->revision );
  77. /* Get the "membase" */
  78. pci_read_config_dword ( pci, PCI_BASE_ADDRESS_1, &pci->membase );
  79. /* Get the "ioaddr" */
  80. pci->ioaddr = 0;
  81. for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
  82. pci_read_config_dword ( pci, reg, &pci->ioaddr );
  83. if ( pci->ioaddr & PCI_BASE_ADDRESS_SPACE_IO ) {
  84. pci->ioaddr &= PCI_BASE_ADDRESS_IO_MASK;
  85. if ( pci->ioaddr ) {
  86. break;
  87. }
  88. }
  89. pci->ioaddr = 0;
  90. }
  91. /* Get the irq */
  92. pci_read_config_byte ( pci, PCI_INTERRUPT_PIN, &pci->irq );
  93. if ( pci->irq ) {
  94. pci_read_config_byte ( pci, PCI_INTERRUPT_LINE, &pci->irq );
  95. }
  96. DBG ( "PCI found device %hhx:%hhx.%d Class %hx: %hx:%hx (rev %hhx)\n",
  97. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  98. PCI_FUNC ( pci->busdevfn ), pci->class, pci->vendor, pci->dev_id,
  99. pci->revision );
  100. return 1;
  101. }
  102. /*
  103. * Set device to be a busmaster in case BIOS neglected to do so. Also
  104. * adjust PCI latency timer to a reasonable value, 32.
  105. */
  106. void adjust_pci_device ( struct pci_device *pci ) {
  107. unsigned short new_command, pci_command;
  108. unsigned char pci_latency;
  109. pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
  110. new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  111. if ( pci_command != new_command ) {
  112. DBG ( "PCI BIOS has not enabled device %hhx:%hhx.%d! "
  113. "Updating PCI command %hX->%hX\n",
  114. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  115. PCI_FUNC ( pci->busdevfn ), pci_command, new_command );
  116. pci_write_config_word ( pci, PCI_COMMAND, new_command );
  117. }
  118. pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
  119. if ( pci_latency < 32 ) {
  120. DBG ( "PCI device %hhx:%hhx.%d latency timer is "
  121. "unreasonably low at %d. Setting to 32.\n",
  122. PCI_BUS ( pci->busdevfn ), PCI_DEV ( pci->busdevfn ),
  123. PCI_FUNC ( pci->busdevfn ), pci_latency );
  124. pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
  125. }
  126. }
  127. /*
  128. * Set PCI device to use.
  129. *
  130. * This routine can be called by e.g. the ROM prefix to specify that
  131. * the first device to be tried should be the device on which the ROM
  132. * was physically located.
  133. *
  134. */
  135. void set_pci_device ( uint16_t busdevfn ) {
  136. pci_dev.magic = pci_magic;
  137. pci_dev.busdevfn = busdevfn;
  138. pci_dev.already_tried = 0;
  139. }
  140. /*
  141. * Find a PCI device matching the specified driver
  142. *
  143. */
  144. int find_pci_device ( struct pci_device *pci,
  145. struct pci_driver *driver ) {
  146. int i;
  147. /* Initialise struct pci if it's the first time it's been used. */
  148. if ( pci->magic != pci_magic ) {
  149. memset ( pci, 0, sizeof ( *pci ) );
  150. pci->magic = pci_magic;
  151. }
  152. /* Iterate through all possible PCI bus:dev.fn combinations,
  153. * starting where we left off.
  154. */
  155. DBG ( "PCI searching for device matching driver %s\n", driver->name );
  156. do {
  157. /* If we've already used this device, skip it */
  158. if ( pci->already_tried ) {
  159. pci->already_tried = 0;
  160. continue;
  161. }
  162. /* Fill in device parameters, if device present */
  163. if ( ! fill_pci_device ( pci ) ) {
  164. continue;
  165. }
  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. }