Access.cpp 9.8 KB

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