Access.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include <Kernel/Arch/x86/IO.h>
  10. #include <Kernel/Bus/PCI/Access.h>
  11. #include <Kernel/Bus/PCI/Controller/HostBridge.h>
  12. #include <Kernel/Bus/PCI/Controller/MemoryBackedHostBridge.h>
  13. #include <Kernel/Debug.h>
  14. #include <Kernel/Firmware/ACPI/Definitions.h>
  15. #include <Kernel/Memory/MemoryManager.h>
  16. #include <Kernel/Memory/Region.h>
  17. #include <Kernel/Memory/TypedMapping.h>
  18. #include <Kernel/Sections.h>
  19. namespace Kernel::PCI {
  20. #define PCI_MMIO_CONFIG_SPACE_SIZE 4096
  21. static Access* s_access;
  22. Access& Access::the()
  23. {
  24. if (s_access == nullptr) {
  25. VERIFY_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
  26. }
  27. return *s_access;
  28. }
  29. bool Access::is_initialized()
  30. {
  31. return (s_access != nullptr);
  32. }
  33. UNMAP_AFTER_INIT bool Access::find_and_register_pci_host_bridges_from_acpi_mcfg_table(PhysicalAddress mcfg_table)
  34. {
  35. u32 length = 0;
  36. u8 revision = 0;
  37. {
  38. auto mapped_mcfg_table = Memory::map_typed<ACPI::Structures::SDTHeader>(mcfg_table);
  39. length = mapped_mcfg_table->length;
  40. revision = mapped_mcfg_table->revision;
  41. }
  42. if (length == sizeof(ACPI::Structures::SDTHeader))
  43. return false;
  44. dbgln("PCI: MCFG, length: {}, revision: {}", length, revision);
  45. if (Checked<size_t>::addition_would_overflow(length, PAGE_SIZE)) {
  46. dbgln("Overflow when adding extra page to allocation of length {}", length);
  47. return false;
  48. }
  49. length += PAGE_SIZE;
  50. auto region_size_or_error = Memory::page_round_up(length);
  51. if (region_size_or_error.is_error()) {
  52. dbgln("Failed to round up length of {} to pages", length);
  53. return false;
  54. }
  55. auto mcfg_region_or_error = MM.allocate_kernel_region(mcfg_table.page_base(), region_size_or_error.value(), "PCI Parsing MCFG", Memory::Region::Access::ReadWrite);
  56. if (mcfg_region_or_error.is_error())
  57. return false;
  58. auto& mcfg = *(ACPI::Structures::MCFG*)mcfg_region_or_error.value()->vaddr().offset(mcfg_table.offset_in_page()).as_ptr();
  59. dbgln_if(PCI_DEBUG, "PCI: Checking MCFG @ {}, {}", VirtualAddress(&mcfg), mcfg_table);
  60. for (u32 index = 0; index < ((mcfg.header.length - sizeof(ACPI::Structures::MCFG)) / sizeof(ACPI::Structures::PCI_MMIO_Descriptor)); index++) {
  61. u8 start_bus = mcfg.descriptors[index].start_pci_bus;
  62. u8 end_bus = mcfg.descriptors[index].end_pci_bus;
  63. u64 start_addr = mcfg.descriptors[index].base_addr;
  64. Domain pci_domain { index, start_bus, end_bus };
  65. dmesgln("PCI: New PCI domain @ {}, PCI buses ({}-{})", PhysicalAddress { start_addr }, start_bus, end_bus);
  66. auto host_bridge = MemoryBackedHostBridge::must_create(pci_domain, PhysicalAddress { start_addr });
  67. add_host_controller(move(host_bridge));
  68. }
  69. return true;
  70. }
  71. UNMAP_AFTER_INIT bool Access::initialize_for_multiple_pci_domains(PhysicalAddress mcfg_table)
  72. {
  73. if (Access::is_initialized())
  74. return false;
  75. auto* access = new Access();
  76. if (!access->find_and_register_pci_host_bridges_from_acpi_mcfg_table(mcfg_table))
  77. return false;
  78. access->rescan_hardware();
  79. dbgln_if(PCI_DEBUG, "PCI: access for multiple PCI domain initialised.");
  80. return true;
  81. }
  82. UNMAP_AFTER_INIT bool Access::initialize_for_one_pci_domain()
  83. {
  84. if (Access::is_initialized()) {
  85. return false;
  86. }
  87. auto* access = new Access();
  88. auto host_bridge = HostBridge::must_create_with_io_access();
  89. access->add_host_controller(move(host_bridge));
  90. access->rescan_hardware();
  91. dbgln_if(PCI_DEBUG, "PCI: access for one PCI domain initialised.");
  92. return true;
  93. }
  94. UNMAP_AFTER_INIT void Access::add_host_controller(NonnullOwnPtr<HostController> controller)
  95. {
  96. auto domain_number = controller->domain_number();
  97. m_host_controllers.set(domain_number, move(controller));
  98. }
  99. UNMAP_AFTER_INIT Access::Access()
  100. {
  101. s_access = this;
  102. }
  103. UNMAP_AFTER_INIT void Access::rescan_hardware()
  104. {
  105. MutexLocker locker(m_access_lock);
  106. SpinlockLocker scan_locker(m_scan_lock);
  107. VERIFY(m_device_identifiers.is_empty());
  108. for (auto it = m_host_controllers.begin(); it != m_host_controllers.end(); ++it) {
  109. (*it).value->enumerate_attached_devices([this](DeviceIdentifier device_identifier) -> void {
  110. m_device_identifiers.append(device_identifier);
  111. });
  112. }
  113. }
  114. void Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& callback) const
  115. {
  116. MutexLocker locker(m_access_lock);
  117. VERIFY(!m_device_identifiers.is_empty());
  118. for (auto const& device_identifier : m_device_identifiers) {
  119. callback(device_identifier);
  120. }
  121. }
  122. DeviceIdentifier Access::get_device_identifier(Address address) const
  123. {
  124. for (auto device_identifier : m_device_identifiers) {
  125. if (device_identifier.address().domain() == address.domain()
  126. && device_identifier.address().bus() == address.bus()
  127. && device_identifier.address().device() == address.device()
  128. && device_identifier.address().function() == address.function()) {
  129. return device_identifier;
  130. }
  131. }
  132. VERIFY_NOT_REACHED();
  133. }
  134. void Access::write8_field(Address address, u32 field, u8 value)
  135. {
  136. MutexLocker lock(m_access_lock);
  137. VERIFY(m_host_controllers.contains(address.domain()));
  138. auto& controller = *m_host_controllers.get(address.domain()).value();
  139. controller.write8_field(address.bus(), address.device(), address.function(), field, value);
  140. }
  141. void Access::write16_field(Address address, u32 field, u16 value)
  142. {
  143. MutexLocker lock(m_access_lock);
  144. VERIFY(m_host_controllers.contains(address.domain()));
  145. auto& controller = *m_host_controllers.get(address.domain()).value();
  146. controller.write16_field(address.bus(), address.device(), address.function(), field, value);
  147. }
  148. void Access::write32_field(Address address, u32 field, u32 value)
  149. {
  150. MutexLocker lock(m_access_lock);
  151. VERIFY(m_host_controllers.contains(address.domain()));
  152. auto& controller = *m_host_controllers.get(address.domain()).value();
  153. controller.write32_field(address.bus(), address.device(), address.function(), field, value);
  154. }
  155. u8 Access::read8_field(Address address, RegisterOffset field)
  156. {
  157. return read8_field(address, to_underlying(field));
  158. }
  159. u16 Access::read16_field(Address address, RegisterOffset field)
  160. {
  161. return read16_field(address, to_underlying(field));
  162. }
  163. u8 Access::read8_field(Address address, u32 field)
  164. {
  165. MutexLocker lock(m_access_lock);
  166. VERIFY(m_host_controllers.contains(address.domain()));
  167. auto& controller = *m_host_controllers.get(address.domain()).value();
  168. return controller.read8_field(address.bus(), address.device(), address.function(), field);
  169. }
  170. u16 Access::read16_field(Address address, u32 field)
  171. {
  172. MutexLocker lock(m_access_lock);
  173. VERIFY(m_host_controllers.contains(address.domain()));
  174. auto& controller = *m_host_controllers.get(address.domain()).value();
  175. return controller.read16_field(address.bus(), address.device(), address.function(), field);
  176. }
  177. u32 Access::read32_field(Address address, u32 field)
  178. {
  179. MutexLocker lock(m_access_lock);
  180. VERIFY(m_host_controllers.contains(address.domain()));
  181. auto& controller = *m_host_controllers.get(address.domain()).value();
  182. return controller.read32_field(address.bus(), address.device(), address.function(), field);
  183. }
  184. }