PCI.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <Kernel/PCI.h>
  2. #include <Kernel/IO.h>
  3. #define PCI_VENDOR_ID 0x00 // word
  4. #define PCI_DEVICE_ID 0x02 // word
  5. #define PCI_COMMAND 0x04 // word
  6. #define PCI_STATUS 0x06 // word
  7. #define PCI_REVISION_ID 0x08 // byte
  8. #define PCI_PROG_IF 0x09 // byte
  9. #define PCI_SUBCLASS 0x0a // byte
  10. #define PCI_CLASS 0x0b // byte
  11. #define PCI_CACHE_LINE_SIZE 0x0c // byte
  12. #define PCI_LATENCY_TIMER 0x0d // byte
  13. #define PCI_HEADER_TYPE 0x0e // byte
  14. #define PCI_BIST 0x0f // byte
  15. #define PCI_BAR0 0x10 // dword
  16. #define PCI_BAR1 0x14 // dword
  17. #define PCI_BAR2 0x18 // dword
  18. #define PCI_BAR3 0x1C // dword
  19. #define PCI_BAR4 0x20 // dword
  20. #define PCI_BAR5 0x24 // dword
  21. #define PCI_INTERRUPT_LINE 0x3C // byte
  22. #define PCI_SECONDARY_BUS 0x19 // byte
  23. #define PCI_HEADER_TYPE_DEVICE 0
  24. #define PCI_HEADER_TYPE_BRIDGE 1
  25. #define PCI_TYPE_BRIDGE 0x0604
  26. #define PCI_ADDRESS_PORT 0xCF8
  27. #define PCI_VALUE_PORT 0xCFC
  28. #define PCI_NONE 0xFFFF
  29. namespace PCI {
  30. template<typename T>
  31. T read_field(Address address, dword field)
  32. {
  33. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  34. if constexpr (sizeof(T) == 4)
  35. return IO::in32(PCI_VALUE_PORT);
  36. if constexpr (sizeof(T) == 2)
  37. return IO::in16(PCI_VALUE_PORT + (field & 2));
  38. if constexpr (sizeof(T) == 1)
  39. return IO::in8(PCI_VALUE_PORT + (field & 3));
  40. }
  41. template<typename T>
  42. void write_field(Address address, dword field, T value)
  43. {
  44. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  45. if constexpr (sizeof(T) == 4)
  46. IO::out32(PCI_VALUE_PORT, value);
  47. if constexpr (sizeof(T) == 2)
  48. IO::out16(PCI_VALUE_PORT + (field & 2), value);
  49. if constexpr (sizeof(T) == 1)
  50. IO::out8(PCI_VALUE_PORT + (field & 3), value);
  51. }
  52. word read_type(Address address)
  53. {
  54. return (read_field<byte>(address, PCI_CLASS) << 8u) | read_field<byte>(address, PCI_SUBCLASS);
  55. }
  56. void enumerate_bus(int type, byte bus, Function<void(Address, ID)>&);
  57. void enumerate_functions(int type, byte bus, byte slot, byte function, Function<void(Address, ID)>& callback)
  58. {
  59. Address address(bus, slot, function);
  60. if (type == -1 || type == read_type(address))
  61. callback(address, { read_field<word>(address, PCI_VENDOR_ID), read_field<word>(address, PCI_DEVICE_ID) });
  62. if (read_type(address) == PCI_TYPE_BRIDGE) {
  63. byte secondary_bus = read_field<byte>(address, PCI_SECONDARY_BUS);
  64. kprintf("PCI: Found secondary bus: %u\n", secondary_bus);
  65. ASSERT(secondary_bus != bus);
  66. enumerate_bus(type, secondary_bus, callback);
  67. }
  68. }
  69. void enumerate_slot(int type, byte bus, byte slot, Function<void(Address, ID)>& callback)
  70. {
  71. Address address(bus, slot, 0);
  72. if (read_field<word>(address, PCI_VENDOR_ID) == PCI_NONE)
  73. return;
  74. enumerate_functions(type, bus, slot, 0, callback);
  75. if (!(read_field<byte>(address, PCI_HEADER_TYPE) & 0x80))
  76. return;
  77. for (byte function = 1; function < 8; ++function) {
  78. Address address(bus, slot, function);
  79. if (read_field<word>(address, PCI_VENDOR_ID) != PCI_NONE)
  80. enumerate_functions(type, bus, slot, function, callback);
  81. }
  82. }
  83. void enumerate_bus(int type, byte bus, Function<void(Address, ID)>& callback)
  84. {
  85. for (byte slot = 0; slot < 32; ++slot)
  86. enumerate_slot(type, bus, slot, callback);
  87. }
  88. dword get_BAR0(Address address) { return read_field<dword>(address, PCI_BAR0); }
  89. dword get_BAR1(Address address) { return read_field<dword>(address, PCI_BAR1); }
  90. dword get_BAR2(Address address) { return read_field<dword>(address, PCI_BAR2); }
  91. dword get_BAR3(Address address) { return read_field<dword>(address, PCI_BAR3); }
  92. dword get_BAR4(Address address) { return read_field<dword>(address, PCI_BAR4); }
  93. dword get_BAR5(Address address) { return read_field<dword>(address, PCI_BAR5); }
  94. void enumerate_all(Function<void(Address, ID)> callback)
  95. {
  96. // Single PCI host controller.
  97. if ((read_field<byte>(Address(), PCI_HEADER_TYPE) & 0x80) == 0) {
  98. enumerate_bus(-1, 0, callback);
  99. return;
  100. }
  101. // Multiple PCI host controllers.
  102. for (byte function = 0; function < 8; ++function) {
  103. if (read_field<word>(Address(0, 0, function), PCI_VENDOR_ID) == PCI_NONE)
  104. break;
  105. enumerate_bus(-1, function, callback);
  106. }
  107. }
  108. }