您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "etherboot.h"
  2. #include "pci.h"
  3. #undef DBG
  4. #ifdef DEBUG_PCI
  5. #define DBG(...) printf ( __VA_ARGS__ )
  6. #else
  7. #define DBG(...)
  8. #endif
  9. /*
  10. * Set device to be a busmaster in case BIOS neglected to do so. Also
  11. * adjust PCI latency timer to a reasonable value, 32.
  12. */
  13. void adjust_pci_device ( struct pci_device *dev ) {
  14. unsigned short new_command, pci_command;
  15. unsigned char pci_latency;
  16. pci_read_config_word ( dev, PCI_COMMAND, &pci_command );
  17. new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  18. if ( pci_command != new_command ) {
  19. DBG ( "The PCI BIOS has not enabled this device!\n"
  20. "Updating PCI command %hX->%hX. bus %hhX dev_fn %hhX\n",
  21. pci_command, new_command, p->bus, p->devfn );
  22. pci_write_config_word ( dev, PCI_COMMAND, new_command );
  23. }
  24. pci_read_config_byte ( dev, PCI_LATENCY_TIMER, &pci_latency);
  25. if ( pci_latency < 32 ) {
  26. DBG ( "PCI latency timer (CFLT) is unreasonably low at %d. "
  27. "Setting to 32 clocks.\n", pci_latency );
  28. pci_write_config_byte ( dev, PCI_LATENCY_TIMER, 32);
  29. }
  30. }
  31. /*
  32. * Find the start of a pci resource.
  33. */
  34. unsigned long pci_bar_start ( struct pci_device *dev, unsigned int index ) {
  35. uint32_t lo, hi;
  36. unsigned long bar;
  37. pci_read_config_dword ( dev, index, &lo );
  38. if ( lo & PCI_BASE_ADDRESS_SPACE_IO ) {
  39. bar = lo & PCI_BASE_ADDRESS_IO_MASK;
  40. } else {
  41. bar = 0;
  42. if ( ( lo & PCI_BASE_ADDRESS_MEM_TYPE_MASK ) ==
  43. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  44. pci_read_config_dword ( dev, index + 4, &hi );
  45. if ( hi ) {
  46. #if ULONG_MAX > 0xffffffff
  47. bar = hi;
  48. bar <<= 32;
  49. #else
  50. printf ( "Unhandled 64bit BAR\n" );
  51. return -1UL;
  52. #endif
  53. }
  54. }
  55. bar |= lo & PCI_BASE_ADDRESS_MEM_MASK;
  56. }
  57. return bar + pcibios_bus_base ( dev->bus );
  58. }
  59. /*
  60. * Find the size of a pci resource.
  61. */
  62. unsigned long pci_bar_size ( struct pci_device *dev, unsigned int bar ) {
  63. uint32_t start, size;
  64. /* Save the original bar */
  65. pci_read_config_dword ( dev, bar, &start );
  66. /* Compute which bits can be set */
  67. pci_write_config_dword ( dev, bar, ~0 );
  68. pci_read_config_dword ( dev, bar, &size );
  69. /* Restore the original size */
  70. pci_write_config_dword ( dev, bar, start );
  71. /* Find the significant bits */
  72. if ( start & PCI_BASE_ADDRESS_SPACE_IO ) {
  73. size &= PCI_BASE_ADDRESS_IO_MASK;
  74. } else {
  75. size &= PCI_BASE_ADDRESS_MEM_MASK;
  76. }
  77. /* Find the lowest bit set */
  78. size = size & ~( size - 1 );
  79. return size;
  80. }
  81. /**
  82. * pci_find_capability - query for devices' capabilities
  83. * @dev: PCI device to query
  84. * @cap: capability code
  85. *
  86. * Tell if a device supports a given PCI capability.
  87. * Returns the address of the requested capability structure within the
  88. * device's PCI configuration space or 0 in case the device does not
  89. * support it. Possible values for @cap:
  90. *
  91. * %PCI_CAP_ID_PM Power Management
  92. *
  93. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  94. *
  95. * %PCI_CAP_ID_VPD Vital Product Data
  96. *
  97. * %PCI_CAP_ID_SLOTID Slot Identification
  98. *
  99. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  100. *
  101. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  102. */
  103. int pci_find_capability ( struct pci_device *dev, int cap ) {
  104. uint16_t status;
  105. uint8_t pos, id;
  106. uint8_t hdr_type;
  107. int ttl = 48;
  108. pci_read_config_word ( dev, PCI_STATUS, &status );
  109. if ( ! ( status & PCI_STATUS_CAP_LIST ) )
  110. return 0;
  111. pci_read_config_byte ( dev, PCI_HEADER_TYPE, &hdr_type );
  112. switch ( hdr_type & 0x7F ) {
  113. case PCI_HEADER_TYPE_NORMAL:
  114. case PCI_HEADER_TYPE_BRIDGE:
  115. default:
  116. pci_read_config_byte ( dev, PCI_CAPABILITY_LIST, &pos );
  117. break;
  118. case PCI_HEADER_TYPE_CARDBUS:
  119. pci_read_config_byte ( dev, PCI_CB_CAPABILITY_LIST, &pos );
  120. break;
  121. }
  122. while ( ttl-- && pos >= 0x40 ) {
  123. pos &= ~3;
  124. pci_read_config_byte ( dev, pos + PCI_CAP_LIST_ID, &id );
  125. DBG ( "Capability: %d\n", id );
  126. if ( id == 0xff )
  127. break;
  128. if ( id == cap )
  129. return pos;
  130. pci_read_config_byte ( dev, pos + PCI_CAP_LIST_NEXT, &pos );
  131. }
  132. return 0;
  133. }