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.3KB

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