Access.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteReader.h>
  7. #include <AK/Error.h>
  8. #include <AK/HashTable.h>
  9. #if ARCH(I386) || ARCH(X86_64)
  10. # include <Kernel/Arch/x86/PCI/Controller/HostBridge.h>
  11. #endif
  12. #include <Kernel/Bus/PCI/Access.h>
  13. #include <Kernel/Bus/PCI/Controller/MemoryBackedHostBridge.h>
  14. #include <Kernel/Bus/PCI/Initializer.h>
  15. #include <Kernel/Debug.h>
  16. #include <Kernel/Firmware/ACPI/Definitions.h>
  17. #include <Kernel/Memory/MemoryManager.h>
  18. #include <Kernel/Memory/Region.h>
  19. #include <Kernel/Memory/TypedMapping.h>
  20. #include <Kernel/ProcessExposed.h>
  21. #include <Kernel/Sections.h>
  22. namespace Kernel::PCI {
  23. #define PCI_MMIO_CONFIG_SPACE_SIZE 4096
  24. static Access* s_access;
  25. Access& Access::the()
  26. {
  27. if (s_access == nullptr) {
  28. VERIFY_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
  29. }
  30. return *s_access;
  31. }
  32. bool Access::is_initialized()
  33. {
  34. return (s_access != nullptr);
  35. }
  36. bool Access::is_disabled()
  37. {
  38. return g_pci_access_is_disabled_from_commandline || g_pci_access_io_probe_failed;
  39. }
  40. UNMAP_AFTER_INIT bool Access::find_and_register_pci_host_bridges_from_acpi_mcfg_table(PhysicalAddress mcfg_table)
  41. {
  42. u32 length = 0;
  43. u8 revision = 0;
  44. {
  45. auto mapped_mcfg_table_or_error = Memory::map_typed<ACPI::Structures::SDTHeader>(mcfg_table);
  46. if (mapped_mcfg_table_or_error.is_error()) {
  47. dbgln("Failed to map MCFG table");
  48. return false;
  49. }
  50. auto mapped_mcfg_table = mapped_mcfg_table_or_error.release_value();
  51. length = mapped_mcfg_table->length;
  52. revision = mapped_mcfg_table->revision;
  53. }
  54. if (length == sizeof(ACPI::Structures::SDTHeader))
  55. return false;
  56. dbgln("PCI: MCFG, length: {}, revision: {}", length, revision);
  57. if (Checked<size_t>::addition_would_overflow(length, PAGE_SIZE)) {
  58. dbgln("Overflow when adding extra page to allocation of length {}", length);
  59. return false;
  60. }
  61. length += PAGE_SIZE;
  62. auto region_size_or_error = Memory::page_round_up(length);
  63. if (region_size_or_error.is_error()) {
  64. dbgln("Failed to round up length of {} to pages", length);
  65. return false;
  66. }
  67. auto mcfg_region_or_error = MM.allocate_kernel_region(mcfg_table.page_base(), region_size_or_error.value(), "PCI Parsing MCFG"sv, Memory::Region::Access::ReadWrite);
  68. if (mcfg_region_or_error.is_error())
  69. return false;
  70. auto& mcfg = *(ACPI::Structures::MCFG*)mcfg_region_or_error.value()->vaddr().offset(mcfg_table.offset_in_page()).as_ptr();
  71. dbgln_if(PCI_DEBUG, "PCI: Checking MCFG @ {}, {}", VirtualAddress(&mcfg), mcfg_table);
  72. for (u32 index = 0; index < ((mcfg.header.length - sizeof(ACPI::Structures::MCFG)) / sizeof(ACPI::Structures::PCI_MMIO_Descriptor)); index++) {
  73. u8 start_bus = mcfg.descriptors[index].start_pci_bus;
  74. u8 end_bus = mcfg.descriptors[index].end_pci_bus;
  75. u64 start_addr = mcfg.descriptors[index].base_addr;
  76. Domain pci_domain { index, start_bus, end_bus };
  77. dmesgln("PCI: New PCI domain @ {}, PCI buses ({}-{})", PhysicalAddress { start_addr }, start_bus, end_bus);
  78. auto host_bridge = MemoryBackedHostBridge::must_create(pci_domain, PhysicalAddress { start_addr });
  79. add_host_controller(move(host_bridge));
  80. }
  81. return true;
  82. }
  83. UNMAP_AFTER_INIT bool Access::initialize_for_multiple_pci_domains(PhysicalAddress mcfg_table)
  84. {
  85. VERIFY(!Access::is_initialized());
  86. auto* access = new Access();
  87. if (!access->find_and_register_pci_host_bridges_from_acpi_mcfg_table(mcfg_table))
  88. return false;
  89. access->rescan_hardware();
  90. dbgln_if(PCI_DEBUG, "PCI: access for multiple PCI domain initialised.");
  91. return true;
  92. }
  93. #if ARCH(I386) || ARCH(X86_64)
  94. UNMAP_AFTER_INIT bool Access::initialize_for_one_pci_domain()
  95. {
  96. VERIFY(!Access::is_initialized());
  97. auto* access = new Access();
  98. auto host_bridge = HostBridge::must_create_with_io_access();
  99. access->add_host_controller(move(host_bridge));
  100. access->rescan_hardware();
  101. dbgln_if(PCI_DEBUG, "PCI: access for one PCI domain initialised.");
  102. return true;
  103. }
  104. #endif
  105. ErrorOr<void> Access::add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr<HostController> controller, Function<void(DeviceIdentifier const&)> callback)
  106. {
  107. // Note: We hold the spinlocks for a moment just to ensure we append the
  108. // device identifiers safely. Afterwards, enumeration goes lockless to allow
  109. // IRQs to be fired if necessary.
  110. Vector<DeviceIdentifier> device_identifiers_behind_host_controller;
  111. {
  112. SpinlockLocker locker(m_access_lock);
  113. SpinlockLocker scan_locker(m_scan_lock);
  114. auto domain_number = controller->domain_number();
  115. VERIFY(!m_host_controllers.contains(domain_number));
  116. // Note: We need to register the new controller as soon as possible, and
  117. // definitely before enumerating devices behing that.
  118. m_host_controllers.set(domain_number, move(controller));
  119. ErrorOr<void> expansion_result;
  120. m_host_controllers.get(domain_number).value()->enumerate_attached_devices([&](DeviceIdentifier const& device_identifier) -> IterationDecision {
  121. m_device_identifiers.append(device_identifier);
  122. auto result = device_identifiers_behind_host_controller.try_append(device_identifier);
  123. if (result.is_error()) {
  124. expansion_result = result;
  125. return IterationDecision::Break;
  126. }
  127. return IterationDecision::Continue;
  128. });
  129. if (expansion_result.is_error())
  130. return expansion_result;
  131. }
  132. for (auto const& device_identifier : device_identifiers_behind_host_controller) {
  133. callback(device_identifier);
  134. }
  135. return {};
  136. }
  137. UNMAP_AFTER_INIT void Access::add_host_controller(NonnullOwnPtr<HostController> controller)
  138. {
  139. auto domain_number = controller->domain_number();
  140. m_host_controllers.set(domain_number, move(controller));
  141. }
  142. UNMAP_AFTER_INIT Access::Access()
  143. {
  144. s_access = this;
  145. }
  146. UNMAP_AFTER_INIT void Access::rescan_hardware()
  147. {
  148. SpinlockLocker locker(m_access_lock);
  149. SpinlockLocker scan_locker(m_scan_lock);
  150. VERIFY(m_device_identifiers.is_empty());
  151. for (auto it = m_host_controllers.begin(); it != m_host_controllers.end(); ++it) {
  152. (*it).value->enumerate_attached_devices([this](DeviceIdentifier device_identifier) -> IterationDecision {
  153. m_device_identifiers.append(device_identifier);
  154. return IterationDecision::Continue;
  155. });
  156. }
  157. }
  158. ErrorOr<void> Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& callback) const
  159. {
  160. // Note: We hold the m_access_lock for a brief moment just to ensure we get
  161. // a complete Vector in case someone wants to mutate it.
  162. Vector<DeviceIdentifier> device_identifiers;
  163. {
  164. SpinlockLocker locker(m_access_lock);
  165. VERIFY(!m_device_identifiers.is_empty());
  166. TRY(device_identifiers.try_extend(m_device_identifiers));
  167. }
  168. for (auto const& device_identifier : device_identifiers) {
  169. callback(device_identifier);
  170. }
  171. return {};
  172. }
  173. DeviceIdentifier Access::get_device_identifier(Address address) const
  174. {
  175. for (auto device_identifier : m_device_identifiers) {
  176. if (device_identifier.address().domain() == address.domain()
  177. && device_identifier.address().bus() == address.bus()
  178. && device_identifier.address().device() == address.device()
  179. && device_identifier.address().function() == address.function()) {
  180. return device_identifier;
  181. }
  182. }
  183. VERIFY_NOT_REACHED();
  184. }
  185. void Access::write8_field(Address address, u32 field, u8 value)
  186. {
  187. SpinlockLocker locker(m_access_lock);
  188. VERIFY(m_host_controllers.contains(address.domain()));
  189. auto& controller = *m_host_controllers.get(address.domain()).value();
  190. controller.write8_field(address.bus(), address.device(), address.function(), field, value);
  191. }
  192. void Access::write16_field(Address address, u32 field, u16 value)
  193. {
  194. SpinlockLocker locker(m_access_lock);
  195. VERIFY(m_host_controllers.contains(address.domain()));
  196. auto& controller = *m_host_controllers.get(address.domain()).value();
  197. controller.write16_field(address.bus(), address.device(), address.function(), field, value);
  198. }
  199. void Access::write32_field(Address address, u32 field, u32 value)
  200. {
  201. SpinlockLocker locker(m_access_lock);
  202. VERIFY(m_host_controllers.contains(address.domain()));
  203. auto& controller = *m_host_controllers.get(address.domain()).value();
  204. controller.write32_field(address.bus(), address.device(), address.function(), field, value);
  205. }
  206. u8 Access::read8_field(Address address, RegisterOffset field)
  207. {
  208. return read8_field(address, to_underlying(field));
  209. }
  210. u16 Access::read16_field(Address address, RegisterOffset field)
  211. {
  212. return read16_field(address, to_underlying(field));
  213. }
  214. u8 Access::read8_field(Address address, u32 field)
  215. {
  216. SpinlockLocker locker(m_access_lock);
  217. VERIFY(m_host_controllers.contains(address.domain()));
  218. auto& controller = *m_host_controllers.get(address.domain()).value();
  219. return controller.read8_field(address.bus(), address.device(), address.function(), field);
  220. }
  221. u16 Access::read16_field(Address address, u32 field)
  222. {
  223. SpinlockLocker locker(m_access_lock);
  224. VERIFY(m_host_controllers.contains(address.domain()));
  225. auto& controller = *m_host_controllers.get(address.domain()).value();
  226. return controller.read16_field(address.bus(), address.device(), address.function(), field);
  227. }
  228. u32 Access::read32_field(Address address, u32 field)
  229. {
  230. SpinlockLocker locker(m_access_lock);
  231. VERIFY(m_host_controllers.contains(address.domain()));
  232. auto& controller = *m_host_controllers.get(address.domain()).value();
  233. return controller.read32_field(address.bus(), address.device(), address.function(), field);
  234. }
  235. }