Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pci.c 7.8KB

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