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.

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