HostController.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <Kernel/Bus/PCI/Access.h>
  8. #include <Kernel/Bus/PCI/Controller/HostController.h>
  9. #include <Kernel/Bus/PCI/Definitions.h>
  10. #include <Kernel/Sections.h>
  11. namespace Kernel::PCI {
  12. HostController::HostController(PCI::Domain const& domain)
  13. : m_domain(domain)
  14. , m_enumerated_buses(Bitmap::create(256, false).release_value_but_fixme_should_propagate_errors())
  15. {
  16. }
  17. UNMAP_AFTER_INIT Optional<u8> HostController::get_capabilities_pointer_for_function(BusNumber bus, DeviceNumber device, FunctionNumber function)
  18. {
  19. if (read16_field(bus, device, function, PCI::RegisterOffset::STATUS) & (1 << 4)) {
  20. return read8_field(bus, device, function, PCI::RegisterOffset::CAPABILITIES_POINTER);
  21. }
  22. return {};
  23. }
  24. UNMAP_AFTER_INIT Vector<Capability> HostController::get_capabilities_for_function(BusNumber bus, DeviceNumber device, FunctionNumber function)
  25. {
  26. auto capabilities_pointer = get_capabilities_pointer_for_function(bus, device, function);
  27. if (!capabilities_pointer.has_value()) {
  28. return {};
  29. }
  30. Vector<Capability> capabilities;
  31. auto capability_pointer = capabilities_pointer.value();
  32. while (capability_pointer != 0) {
  33. u16 capability_header = read16_field(bus, device, function, capability_pointer);
  34. u8 capability_id = capability_header & 0xff;
  35. // FIXME: Don't attach a PCI address to a capability object
  36. capabilities.append({ Address(domain_number(), bus.value(), device.value(), function.value()), capability_id, capability_pointer });
  37. capability_pointer = capability_header >> 8;
  38. }
  39. return capabilities;
  40. }
  41. u8 HostController::read8_field(BusNumber bus, DeviceNumber device, FunctionNumber function, PCI::RegisterOffset field)
  42. {
  43. return read8_field(bus, device, function, to_underlying(field));
  44. }
  45. u16 HostController::read16_field(BusNumber bus, DeviceNumber device, FunctionNumber function, PCI::RegisterOffset field)
  46. {
  47. return read16_field(bus, device, function, to_underlying(field));
  48. }
  49. UNMAP_AFTER_INIT void HostController::enumerate_functions(Function<IterationDecision(EnumerableDeviceIdentifier)> const& callback, BusNumber bus, DeviceNumber device, FunctionNumber function, bool recursive_search_into_bridges)
  50. {
  51. dbgln_if(PCI_DEBUG, "PCI: Enumerating function, bus={}, device={}, function={}", bus, device, function);
  52. Address address(domain_number(), bus.value(), device.value(), function.value());
  53. auto pci_class = (read8_field(bus, device, function, PCI::RegisterOffset::CLASS) << 8u) | read8_field(bus, device, function, PCI::RegisterOffset::SUBCLASS);
  54. HardwareID id = { read16_field(bus, device, function, PCI::RegisterOffset::VENDOR_ID), read16_field(bus, device, function, PCI::RegisterOffset::DEVICE_ID) };
  55. ClassCode class_code = read8_field(bus, device, function, PCI::RegisterOffset::CLASS);
  56. SubclassCode subclass_code = read8_field(bus, device, function, PCI::RegisterOffset::SUBCLASS);
  57. ProgrammingInterface prog_if = read8_field(bus, device, function, PCI::RegisterOffset::PROG_IF);
  58. RevisionID revision_id = read8_field(bus, device, function, PCI::RegisterOffset::REVISION_ID);
  59. SubsystemID subsystem_id = read16_field(bus, device, function, PCI::RegisterOffset::SUBSYSTEM_ID);
  60. SubsystemVendorID subsystem_vendor_id = read16_field(bus, device, function, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID);
  61. InterruptLine interrupt_line = read8_field(bus, device, function, PCI::RegisterOffset::INTERRUPT_LINE);
  62. InterruptPin interrupt_pin = read8_field(bus, device, function, PCI::RegisterOffset::INTERRUPT_PIN);
  63. auto capabilities = get_capabilities_for_function(bus, device, function);
  64. callback(EnumerableDeviceIdentifier { address, id, revision_id, class_code, subclass_code, prog_if, subsystem_id, subsystem_vendor_id, interrupt_line, interrupt_pin, capabilities });
  65. if (pci_class == (to_underlying(PCI::ClassID::Bridge) << 8 | to_underlying(PCI::Bridge::SubclassID::PCI_TO_PCI))
  66. && recursive_search_into_bridges
  67. && (!m_enumerated_buses.get(read8_field(bus, device, function, PCI::RegisterOffset::SECONDARY_BUS)))) {
  68. u8 secondary_bus = read8_field(bus, device, function, PCI::RegisterOffset::SECONDARY_BUS);
  69. dbgln_if(PCI_DEBUG, "PCI: Found secondary bus: {}", secondary_bus);
  70. VERIFY(secondary_bus != bus);
  71. m_enumerated_buses.set(secondary_bus, true);
  72. enumerate_bus(callback, secondary_bus, recursive_search_into_bridges);
  73. }
  74. }
  75. UNMAP_AFTER_INIT void HostController::enumerate_device(Function<IterationDecision(EnumerableDeviceIdentifier)> const& callback, BusNumber bus, DeviceNumber device, bool recursive_search_into_bridges)
  76. {
  77. dbgln_if(PCI_DEBUG, "PCI: Enumerating device in bus={}, device={}", bus, device);
  78. if (read16_field(bus, device, 0, PCI::RegisterOffset::VENDOR_ID) == PCI::none_value)
  79. return;
  80. enumerate_functions(callback, bus, device, 0, recursive_search_into_bridges);
  81. if (!(read8_field(bus, device, 0, PCI::RegisterOffset::HEADER_TYPE) & 0x80))
  82. return;
  83. for (u8 function = 1; function < 8; ++function) {
  84. if (read16_field(bus, device, function, PCI::RegisterOffset::VENDOR_ID) != PCI::none_value)
  85. enumerate_functions(callback, bus, device, function, recursive_search_into_bridges);
  86. }
  87. }
  88. UNMAP_AFTER_INIT void HostController::enumerate_bus(Function<IterationDecision(EnumerableDeviceIdentifier)> const& callback, BusNumber bus, bool recursive_search_into_bridges)
  89. {
  90. dbgln_if(PCI_DEBUG, "PCI: Enumerating bus {}", bus);
  91. for (u8 device = 0; device < 32; ++device)
  92. enumerate_device(callback, bus, device, recursive_search_into_bridges);
  93. }
  94. UNMAP_AFTER_INIT void HostController::enumerate_attached_devices(Function<IterationDecision(EnumerableDeviceIdentifier)> callback)
  95. {
  96. VERIFY(Access::the().access_lock().is_locked());
  97. VERIFY(Access::the().scan_lock().is_locked());
  98. // First scan bus 0. Find any device on that bus, and if it's a PCI-to-PCI
  99. // bridge, recursively scan it too.
  100. m_enumerated_buses.set(m_domain.start_bus(), true);
  101. enumerate_bus(callback, m_domain.start_bus(), true);
  102. // Handle Multiple PCI host bridges on bus 0, device 0, functions 1-7 (function 0
  103. // is the main host bridge).
  104. // If we happen to miss some PCI buses because they are not reachable through
  105. // recursive PCI-to-PCI bridges starting from bus 0, we might find them here.
  106. if ((read8_field(0, 0, 0, PCI::RegisterOffset::HEADER_TYPE) & 0x80) != 0) {
  107. for (int bus_as_function_number = 1; bus_as_function_number < 8; ++bus_as_function_number) {
  108. if (read16_field(0, 0, bus_as_function_number, PCI::RegisterOffset::VENDOR_ID) == PCI::none_value)
  109. continue;
  110. if (read16_field(0, 0, bus_as_function_number, PCI::RegisterOffset::CLASS) != 0x6)
  111. continue;
  112. if (Checked<u8>::addition_would_overflow(m_domain.start_bus(), bus_as_function_number))
  113. break;
  114. if (m_enumerated_buses.get(m_domain.start_bus() + bus_as_function_number))
  115. continue;
  116. enumerate_bus(callback, m_domain.start_bus() + bus_as_function_number, false);
  117. m_enumerated_buses.set(m_domain.start_bus() + bus_as_function_number, true);
  118. }
  119. }
  120. }
  121. }