MMIOAccess.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Optional.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/PCI/MMIOAccess.h>
  29. #include <Kernel/VM/MemoryManager.h>
  30. //#define PCI_DEBUG
  31. namespace Kernel {
  32. namespace PCI {
  33. class MMIOSegment {
  34. public:
  35. MMIOSegment(PhysicalAddress, u8, u8);
  36. u8 get_start_bus() const;
  37. u8 get_end_bus() const;
  38. size_t get_size() const;
  39. PhysicalAddress get_paddr() const;
  40. private:
  41. PhysicalAddress m_base_addr;
  42. u8 m_start_bus;
  43. u8 m_end_bus;
  44. };
  45. #define PCI_MMIO_CONFIG_SPACE_SIZE 4096
  46. DeviceConfigurationSpaceMapping::DeviceConfigurationSpaceMapping(Address device_address, const MMIOSegment& mmio_segment)
  47. : m_device_address(device_address)
  48. , m_mapped_region(MM.allocate_kernel_region(PAGE_ROUND_UP(PCI_MMIO_CONFIG_SPACE_SIZE), "PCI MMIO Device Access", Region::Access::Read | Region::Access::Write).release_nonnull())
  49. {
  50. PhysicalAddress segment_lower_addr = mmio_segment.get_paddr();
  51. PhysicalAddress device_physical_mmio_space = segment_lower_addr.offset(
  52. PCI_MMIO_CONFIG_SPACE_SIZE * m_device_address.function() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE) * m_device_address.slot() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS) * (m_device_address.bus() - mmio_segment.get_start_bus()));
  53. m_mapped_region->physical_page_slot(0) = PhysicalPage::create(device_physical_mmio_space, false, false);
  54. m_mapped_region->remap();
  55. }
  56. uint32_t MMIOAccess::segment_count() const
  57. {
  58. return m_segments.size();
  59. }
  60. uint8_t MMIOAccess::segment_start_bus(u32 seg) const
  61. {
  62. auto segment = m_segments.get(seg);
  63. ASSERT(segment.has_value());
  64. return segment.value().get_start_bus();
  65. }
  66. uint8_t MMIOAccess::segment_end_bus(u32 seg) const
  67. {
  68. auto segment = m_segments.get(seg);
  69. ASSERT(segment.has_value());
  70. return segment.value().get_end_bus();
  71. }
  72. void MMIOAccess::initialize(PhysicalAddress mcfg)
  73. {
  74. if (!Access::is_initialized()) {
  75. new MMIOAccess(mcfg);
  76. #ifdef PCI_DEBUG
  77. dbg() << "PCI: MMIO access initialised.";
  78. #endif
  79. }
  80. }
  81. MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg)
  82. : m_mcfg(p_mcfg)
  83. {
  84. klog() << "PCI: Using MMIO for PCI configuration space access";
  85. auto checkup_region = MM.allocate_kernel_region(p_mcfg.page_base(), (PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write);
  86. #ifdef PCI_DEBUG
  87. dbg() << "PCI: Checking MCFG Table length to choose the correct mapping size";
  88. #endif
  89. auto* sdt = (ACPI::Structures::SDTHeader*)checkup_region->vaddr().offset(p_mcfg.offset_in_page()).as_ptr();
  90. u32 length = sdt->length;
  91. u8 revision = sdt->revision;
  92. klog() << "PCI: MCFG, length - " << length << ", revision " << revision;
  93. checkup_region->unmap();
  94. auto mcfg_region = MM.allocate_kernel_region(p_mcfg.page_base(), PAGE_ROUND_UP(length) + PAGE_SIZE, "PCI Parsing MCFG", Region::Access::Read | Region::Access::Write);
  95. auto& mcfg = *(ACPI::Structures::MCFG*)mcfg_region->vaddr().offset(p_mcfg.offset_in_page()).as_ptr();
  96. #ifdef PCI_DEBUG
  97. dbg() << "PCI: Checking MCFG @ V " << &mcfg << ", P 0x" << String::format("%x", p_mcfg.get());
  98. #endif
  99. for (u32 index = 0; index < ((mcfg.header.length - sizeof(ACPI::Structures::MCFG)) / sizeof(ACPI::Structures::PCI_MMIO_Descriptor)); index++) {
  100. u8 start_bus = mcfg.descriptors[index].start_pci_bus;
  101. u8 end_bus = mcfg.descriptors[index].end_pci_bus;
  102. u32 lower_addr = mcfg.descriptors[index].base_addr;
  103. m_segments.set(index, { PhysicalAddress(lower_addr), start_bus, end_bus });
  104. klog() << "PCI: New PCI segment @ " << PhysicalAddress(lower_addr) << ", PCI buses (" << start_bus << "-" << end_bus << ")";
  105. }
  106. mcfg_region->unmap();
  107. klog() << "PCI: MMIO segments - " << m_segments.size();
  108. InterruptDisabler disabler;
  109. enumerate_hardware([&](const Address& address, ID id) {
  110. m_mapped_device_regions.append(make<DeviceConfigurationSpaceMapping>(address, m_segments.get(address.seg()).value()));
  111. m_physical_ids.append({ address, id, get_capabilities(address) });
  112. #ifdef PCI_DEBUG
  113. dbg() << "PCI: Mapping device @ pci (" << address << ")"
  114. << " " << m_mapped_device_regions.last().vaddr() << " " << m_mapped_device_regions.last().paddr();
  115. #endif
  116. });
  117. }
  118. Optional<VirtualAddress> MMIOAccess::get_device_configuration_space(Address address)
  119. {
  120. #ifdef PCI_DEBUG
  121. dbg() << "PCI: Getting device configuration space for " << address;
  122. #endif
  123. for (auto& mapping : m_mapped_device_regions) {
  124. auto checked_address = mapping.address();
  125. #ifdef PCI_DEBUG
  126. dbg() << "PCI Device Configuration Space Mapping: Check if " << checked_address << " was requested";
  127. #endif
  128. if (address.seg() == checked_address.seg()
  129. && address.bus() == checked_address.bus()
  130. && address.slot() == checked_address.slot()
  131. && address.function() == checked_address.function()) {
  132. #ifdef PCI_DEBUG
  133. dbg() << "PCI Device Configuration Space Mapping: Found " << checked_address;
  134. #endif
  135. return mapping.vaddr();
  136. }
  137. }
  138. #ifdef PCI_DEBUG
  139. dbg() << "PCI: No device configuration space found for " << address;
  140. #endif
  141. return {};
  142. }
  143. u8 MMIOAccess::read8_field(Address address, u32 field)
  144. {
  145. InterruptDisabler disabler;
  146. ASSERT(field <= 0xfff);
  147. #ifdef PCI_DEBUG
  148. dbg() << "PCI: MMIO Reading 8-bit field 0x" << String::formatted("{:08x}", field) << " for " << address;
  149. #endif
  150. return *((u8*)(get_device_configuration_space(address).value().get() + (field & 0xfff)));
  151. }
  152. u16 MMIOAccess::read16_field(Address address, u32 field)
  153. {
  154. InterruptDisabler disabler;
  155. ASSERT(field < 0xfff);
  156. #ifdef PCI_DEBUG
  157. dbg() << "PCI: MMIO Reading 16-bit field 0x" << String::formatted("{:08x}", field) << " for " << address;
  158. #endif
  159. return *((u16*)(get_device_configuration_space(address).value().get() + (field & 0xfff)));
  160. }
  161. u32 MMIOAccess::read32_field(Address address, u32 field)
  162. {
  163. InterruptDisabler disabler;
  164. ASSERT(field <= 0xffc);
  165. #ifdef PCI_DEBUG
  166. dbg() << "PCI: MMIO Reading 32-bit field 0x" << String::formatted("{:08x}", field) << " for " << address;
  167. #endif
  168. return *((u32*)(get_device_configuration_space(address).value().get() + (field & 0xfff)));
  169. }
  170. void MMIOAccess::write8_field(Address address, u32 field, u8 value)
  171. {
  172. InterruptDisabler disabler;
  173. ASSERT(field <= 0xfff);
  174. #ifdef PCI_DEBUG
  175. dbg() << "PCI: MMIO Writing to 8-bit field 0x" << String::formatted("{:08x}", field) << ", value=0x" << String::formatted("{:02x}", value) << " for " << address;
  176. #endif
  177. *((u8*)(get_device_configuration_space(address).value().get() + (field & 0xfff))) = value;
  178. }
  179. void MMIOAccess::write16_field(Address address, u32 field, u16 value)
  180. {
  181. InterruptDisabler disabler;
  182. ASSERT(field < 0xfff);
  183. #ifdef PCI_DEBUG
  184. dbg() << "PCI: MMIO Writing to 16-bit field 0x" << String::formatted("{:08x}", field) << ", value=0x" << String::formatted("{:04x}", value) << " for " << address;
  185. #endif
  186. *((u16*)(get_device_configuration_space(address).value().get() + (field & 0xfff))) = value;
  187. }
  188. void MMIOAccess::write32_field(Address address, u32 field, u32 value)
  189. {
  190. InterruptDisabler disabler;
  191. ASSERT(field <= 0xffc);
  192. #ifdef PCI_DEBUG
  193. dbg() << "PCI: MMIO Writing to 32-bit field 0x" << String::formatted("{:08x}", field) << ", value=0x" << String::formatted("{:08x}", value) << " for " << address;
  194. #endif
  195. *((u32*)(get_device_configuration_space(address).value().get() + (field & 0xfff))) = value;
  196. }
  197. void MMIOAccess::enumerate_hardware(Function<void(Address, ID)> callback)
  198. {
  199. for (u16 seg = 0; seg < m_segments.size(); seg++) {
  200. #ifdef PCI_DEBUG
  201. dbg() << "PCI: Enumerating Memory mapped IO segment " << seg;
  202. #endif
  203. // Single PCI host controller.
  204. if ((early_read8_field(Address(seg), PCI_HEADER_TYPE) & 0x80) == 0) {
  205. enumerate_bus(-1, 0, callback);
  206. return;
  207. }
  208. // Multiple PCI host controllers.
  209. for (u8 function = 0; function < 8; ++function) {
  210. if (early_read16_field(Address(seg, 0, 0, function), PCI_VENDOR_ID) == PCI_NONE)
  211. break;
  212. enumerate_bus(-1, function, callback);
  213. }
  214. }
  215. }
  216. MMIOSegment::MMIOSegment(PhysicalAddress segment_base_addr, u8 start_bus, u8 end_bus)
  217. : m_base_addr(segment_base_addr)
  218. , m_start_bus(start_bus)
  219. , m_end_bus(end_bus)
  220. {
  221. }
  222. u8 MMIOSegment::get_start_bus() const
  223. {
  224. return m_start_bus;
  225. }
  226. u8 MMIOSegment::get_end_bus() const
  227. {
  228. return m_end_bus;
  229. }
  230. size_t MMIOSegment::get_size() const
  231. {
  232. return (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS * (get_end_bus() - get_start_bus()));
  233. }
  234. PhysicalAddress MMIOSegment::get_paddr() const
  235. {
  236. return m_base_addr;
  237. }
  238. }
  239. }