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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "etherboot.h"
  2. #include "dev.h"
  3. #include "pci.h"
  4. #define DEBUG_PCI
  5. #undef DBG
  6. #ifdef DEBUG_PCI
  7. #define DBG(...) printf ( __VA_ARGS__ )
  8. #else
  9. #define DBG(...)
  10. #endif
  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. static 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 dev.pci
  101. */
  102. struct pci_device * pci_device ( struct dev *dev ) {
  103. struct pci_device *pci = &dev->pci;
  104. if ( dev->devid.bus_type != PCI_BUS_TYPE ) {
  105. memset ( pci, 0, sizeof ( *pci ) );
  106. }
  107. pci->dev = dev;
  108. return pci;
  109. }
  110. /*
  111. * Find a PCI device matching the specified driver
  112. *
  113. */
  114. int find_pci_device ( struct pci_device *pci,
  115. struct pci_driver *driver ) {
  116. int i;
  117. /* Iterate through all possible PCI bus:dev.fn combinations,
  118. * starting where we left off.
  119. */
  120. for ( ; pci->busdevfn <= 0xffff ; pci->busdevfn++ ) {
  121. /* If we've already used this device, skip it */
  122. if ( pci->already_tried ) {
  123. pci->already_tried = 0;
  124. continue;
  125. }
  126. /* Fill in device parameters, if device present */
  127. if ( ! fill_pci_device ( pci ) ) {
  128. continue;
  129. }
  130. /* Fix up PCI device */
  131. adjust_pci_device ( pci );
  132. /* Fill in dev structure, if present */
  133. if ( pci->dev ) {
  134. pci->dev->name = driver->name;
  135. pci->dev->devid.vendor_id = pci->vendor;
  136. pci->dev->devid.device_id = pci->dev_id;
  137. }
  138. /* If driver has a class, and class matches, use it */
  139. if ( driver->class &&
  140. ( driver->class == pci->class ) ) {
  141. DBG ( "Driver %s matches class %hx\n",
  142. driver->name, driver->class );
  143. pci->already_tried = 1;
  144. return 1;
  145. }
  146. /* If any of driver's IDs match, use it */
  147. for ( i = 0 ; i < driver->id_count; i++ ) {
  148. struct pci_id *id = &driver->ids[i];
  149. if ( ( pci->vendor == id->vendor ) &&
  150. ( pci->dev_id == id->dev_id ) ) {
  151. DBG ( "Device %s (driver %s) matches "
  152. "ID %hx:%hx\n", id->name, driver->name,
  153. id->vendor, id->dev_id );
  154. if ( pci->dev )
  155. pci->dev->name = id->name;
  156. pci->already_tried = 1;
  157. return 1;
  158. }
  159. }
  160. DBG ( "No match in driver %s\n", driver->name );
  161. }
  162. /* No device found */
  163. return 0;
  164. }
  165. /*
  166. * Find the start of a pci resource.
  167. */
  168. unsigned long pci_bar_start ( struct pci_device *pci, unsigned int index ) {
  169. uint32_t lo, hi;
  170. unsigned long bar;
  171. pci_read_config_dword ( pci, index, &lo );
  172. if ( lo & PCI_BASE_ADDRESS_SPACE_IO ) {
  173. bar = lo & PCI_BASE_ADDRESS_IO_MASK;
  174. } else {
  175. bar = 0;
  176. if ( ( lo & PCI_BASE_ADDRESS_MEM_TYPE_MASK ) ==
  177. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  178. pci_read_config_dword ( pci, index + 4, &hi );
  179. if ( hi ) {
  180. #if ULONG_MAX > 0xffffffff
  181. bar = hi;
  182. bar <<= 32;
  183. #else
  184. printf ( "Unhandled 64bit BAR\n" );
  185. return -1UL;
  186. #endif
  187. }
  188. }
  189. bar |= lo & PCI_BASE_ADDRESS_MEM_MASK;
  190. }
  191. return bar + pci_bus_base ( pci );
  192. }
  193. /*
  194. * Find the size of a pci resource.
  195. */
  196. unsigned long pci_bar_size ( struct pci_device *pci, unsigned int bar ) {
  197. uint32_t start, size;
  198. /* Save the original bar */
  199. pci_read_config_dword ( pci, bar, &start );
  200. /* Compute which bits can be set */
  201. pci_write_config_dword ( pci, bar, ~0 );
  202. pci_read_config_dword ( pci, bar, &size );
  203. /* Restore the original size */
  204. pci_write_config_dword ( pci, bar, start );
  205. /* Find the significant bits */
  206. if ( start & PCI_BASE_ADDRESS_SPACE_IO ) {
  207. size &= PCI_BASE_ADDRESS_IO_MASK;
  208. } else {
  209. size &= PCI_BASE_ADDRESS_MEM_MASK;
  210. }
  211. /* Find the lowest bit set */
  212. size = size & ~( size - 1 );
  213. return size;
  214. }
  215. /**
  216. * pci_find_capability - query for devices' capabilities
  217. * @pci: PCI device to query
  218. * @cap: capability code
  219. *
  220. * Tell if a device supports a given PCI capability.
  221. * Returns the address of the requested capability structure within the
  222. * device's PCI configuration space or 0 in case the device does not
  223. * support it. Possible values for @cap:
  224. *
  225. * %PCI_CAP_ID_PM Power Management
  226. *
  227. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  228. *
  229. * %PCI_CAP_ID_VPD Vital Product Data
  230. *
  231. * %PCI_CAP_ID_SLOTID Slot Identification
  232. *
  233. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  234. *
  235. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  236. */
  237. int pci_find_capability ( struct pci_device *pci, int cap ) {
  238. uint16_t status;
  239. uint8_t pos, id;
  240. uint8_t hdr_type;
  241. int ttl = 48;
  242. pci_read_config_word ( pci, PCI_STATUS, &status );
  243. if ( ! ( status & PCI_STATUS_CAP_LIST ) )
  244. return 0;
  245. pci_read_config_byte ( pci, PCI_HEADER_TYPE, &hdr_type );
  246. switch ( hdr_type & 0x7F ) {
  247. case PCI_HEADER_TYPE_NORMAL:
  248. case PCI_HEADER_TYPE_BRIDGE:
  249. default:
  250. pci_read_config_byte ( pci, PCI_CAPABILITY_LIST, &pos );
  251. break;
  252. case PCI_HEADER_TYPE_CARDBUS:
  253. pci_read_config_byte ( pci, PCI_CB_CAPABILITY_LIST, &pos );
  254. break;
  255. }
  256. while ( ttl-- && pos >= 0x40 ) {
  257. pos &= ~3;
  258. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_ID, &id );
  259. DBG ( "Capability: %d\n", id );
  260. if ( id == 0xff )
  261. break;
  262. if ( id == cap )
  263. return pos;
  264. pci_read_config_byte ( pci, pos + PCI_CAP_LIST_NEXT, &pos );
  265. }
  266. return 0;
  267. }