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

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