MMIOAccess.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. namespace Kernel {
  31. namespace PCI {
  32. class MMIOSegment {
  33. public:
  34. MMIOSegment(PhysicalAddress, u8, u8);
  35. u8 get_start_bus();
  36. u8 get_end_bus();
  37. size_t get_size();
  38. PhysicalAddress get_paddr();
  39. private:
  40. PhysicalAddress m_base_addr;
  41. u8 m_start_bus;
  42. u8 m_end_bus;
  43. };
  44. #define PCI_MMIO_CONFIG_SPACE_SIZE 4096
  45. uint32_t MMIOAccess::segment_count() const
  46. {
  47. return m_segments.size();
  48. }
  49. uint8_t MMIOAccess::segment_start_bus(u32 seg) const
  50. {
  51. ASSERT(m_segments.contains(seg));
  52. return m_segments.get(seg).value()->get_start_bus();
  53. }
  54. uint8_t MMIOAccess::segment_end_bus(u32 seg) const
  55. {
  56. ASSERT(m_segments.contains(seg));
  57. return m_segments.get(seg).value()->get_end_bus();
  58. }
  59. void MMIOAccess::initialize(PhysicalAddress mcfg)
  60. {
  61. if (!Access::is_initialized())
  62. new MMIOAccess(mcfg);
  63. }
  64. MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg)
  65. : m_mcfg(p_mcfg)
  66. , m_segments(*new HashMap<u16, MMIOSegment*>())
  67. , m_mapped_address(ChangeableAddress(0xFFFF, 0xFF, 0xFF, 0xFF))
  68. {
  69. klog() << "PCI: Using MMIO Mechanism for PCI Configuartion Space Access";
  70. m_mmio_window_region = MM.allocate_kernel_region(PAGE_ROUND_UP(PCI_MMIO_CONFIG_SPACE_SIZE), "PCI MMIO", Region::Access::Read | Region::Access::Write);
  71. auto checkup_region = MM.allocate_kernel_region(p_mcfg.page_base(), (PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write);
  72. #ifdef PCI_DEBUG
  73. dbg() << "PCI: Checking MCFG Table length to choose the correct mapping size";
  74. #endif
  75. auto* sdt = (ACPI::Structures::SDTHeader*)checkup_region->vaddr().offset(p_mcfg.offset_in_page()).as_ptr();
  76. u32 length = sdt->length;
  77. u8 revision = sdt->revision;
  78. klog() << "PCI: MCFG, length - " << length << ", revision " << revision;
  79. checkup_region->unmap();
  80. 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);
  81. auto& mcfg = *(ACPI::Structures::MCFG*)mcfg_region->vaddr().offset(p_mcfg.offset_in_page()).as_ptr();
  82. #ifdef PCI_DEBUG
  83. dbg() << "PCI: Checking MCFG @ V " << &mcfg << ", P 0x" << String::format("%x", p_mcfg.get());
  84. #endif
  85. for (u32 index = 0; index < ((mcfg.header.length - sizeof(ACPI::Structures::MCFG)) / sizeof(ACPI::Structures::PCI_MMIO_Descriptor)); index++) {
  86. u8 start_bus = mcfg.descriptors[index].start_pci_bus;
  87. u8 end_bus = mcfg.descriptors[index].end_pci_bus;
  88. u32 lower_addr = mcfg.descriptors[index].base_addr;
  89. m_segments.set(index, new MMIOSegment(PhysicalAddress(lower_addr), start_bus, end_bus));
  90. klog() << "PCI: New PCI segment @ " << PhysicalAddress(lower_addr) << ", PCI buses (" << start_bus << "-" << end_bus << ")";
  91. }
  92. mcfg_region->unmap();
  93. klog() << "PCI: MMIO segments - " << m_segments.size();
  94. InterruptDisabler disabler;
  95. #ifdef PCI_DEBUG
  96. dbg() << "PCI: mapped address (" << String::format("%w", m_mapped_address.seg()) << ":" << String::format("%b", m_mapped_address.bus()) << ":" << String::format("%b", m_mapped_address.slot()) << "." << String::format("%b", m_mapped_address.function()) << ")";
  97. #endif
  98. map_device(Address(0, 0, 0, 0));
  99. #ifdef PCI_DEBUG
  100. dbg() << "PCI: Default mapped address (" << String::format("%w", m_mapped_address.seg()) << ":" << String::format("%b", m_mapped_address.bus()) << ":" << String::format("%b", m_mapped_address.slot()) << "." << String::format("%b", m_mapped_address.function()) << ")";
  101. #endif
  102. }
  103. void MMIOAccess::map_device(Address address)
  104. {
  105. if (m_mapped_address == address)
  106. return;
  107. // FIXME: Map and put some lock!
  108. ASSERT_INTERRUPTS_DISABLED();
  109. ASSERT(m_segments.contains(address.seg()));
  110. auto segment = m_segments.get(address.seg());
  111. PhysicalAddress segment_lower_addr = segment.value()->get_paddr();
  112. PhysicalAddress device_physical_mmio_space = segment_lower_addr.offset(
  113. PCI_MMIO_CONFIG_SPACE_SIZE * address.function() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE) * address.slot() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS) * (address.bus() - segment.value()->get_start_bus()));
  114. #ifdef PCI_DEBUG
  115. dbg() << "PCI: Mapping device @ pci (" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ")"
  116. << " V 0x" << String::format("%x", m_mmio_window_region->vaddr().get()) << " P 0x" << String::format("%x", device_physical_mmio_space.get());
  117. #endif
  118. m_mmio_window_region->vmobject().physical_pages()[0] = PhysicalPage::create(device_physical_mmio_space, false, false);
  119. m_mmio_window_region->remap();
  120. m_mapped_address = address;
  121. }
  122. u8 MMIOAccess::read8_field(Address address, u32 field)
  123. {
  124. InterruptDisabler disabler;
  125. ASSERT(field <= 0xfff);
  126. #ifdef PCI_DEBUG
  127. dbg() << "PCI: Reading field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ")";
  128. #endif
  129. map_device(address);
  130. return *((u8*)(m_mmio_window_region->vaddr().get() + (field & 0xfff)));
  131. }
  132. u16 MMIOAccess::read16_field(Address address, u32 field)
  133. {
  134. InterruptDisabler disabler;
  135. ASSERT(field < 0xfff);
  136. #ifdef PCI_DEBUG
  137. dbg() << "PCI: Reading field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ")";
  138. #endif
  139. map_device(address);
  140. return *((u16*)(m_mmio_window_region->vaddr().get() + (field & 0xfff)));
  141. }
  142. u32 MMIOAccess::read32_field(Address address, u32 field)
  143. {
  144. InterruptDisabler disabler;
  145. ASSERT(field <= 0xffc);
  146. #ifdef PCI_DEBUG
  147. dbg() << "PCI: Reading field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ")";
  148. #endif
  149. map_device(address);
  150. return *((u32*)(m_mmio_window_region->vaddr().get() + (field & 0xfff)));
  151. }
  152. void MMIOAccess::write8_field(Address address, u32 field, u8 value)
  153. {
  154. InterruptDisabler disabler;
  155. ASSERT(field <= 0xfff);
  156. #ifdef PCI_DEBUG
  157. dbg() << "PCI: Writing to field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ") value 0x" << String::format("%x", value);
  158. #endif
  159. map_device(address);
  160. *((u8*)(m_mmio_window_region->vaddr().get() + (field & 0xfff))) = value;
  161. }
  162. void MMIOAccess::write16_field(Address address, u32 field, u16 value)
  163. {
  164. InterruptDisabler disabler;
  165. ASSERT(field < 0xfff);
  166. #ifdef PCI_DEBUG
  167. dbg() << "PCI: Writing to field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ") value 0x" << String::format("%x", value);
  168. #endif
  169. map_device(address);
  170. *((u16*)(m_mmio_window_region->vaddr().get() + (field & 0xfff))) = value;
  171. }
  172. void MMIOAccess::write32_field(Address address, u32 field, u32 value)
  173. {
  174. InterruptDisabler disabler;
  175. ASSERT(field <= 0xffc);
  176. #ifdef PCI_DEBUG
  177. dbg() << "PCI: Writing to field " << field << ", Address(" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ") value 0x" << String::format("%x", value);
  178. #endif
  179. map_device(address);
  180. *((u32*)(m_mmio_window_region->vaddr().get() + (field & 0xfff))) = value;
  181. }
  182. void MMIOAccess::enumerate_all(Function<void(Address, ID)>& callback)
  183. {
  184. for (u16 seg = 0; seg < m_segments.size(); seg++) {
  185. #ifdef PCI_DEBUG
  186. dbg() << "PCI: Enumerating Memory mapped IO segment " << seg;
  187. #endif
  188. // Single PCI host controller.
  189. if ((read8_field(Address(seg), PCI_HEADER_TYPE) & 0x80) == 0) {
  190. enumerate_bus(-1, 0, callback);
  191. return;
  192. }
  193. // Multiple PCI host controllers.
  194. for (u8 function = 0; function < 8; ++function) {
  195. if (read16_field(Address(seg, 0, 0, function), PCI_VENDOR_ID) == PCI_NONE)
  196. break;
  197. enumerate_bus(-1, function, callback);
  198. }
  199. }
  200. }
  201. MMIOSegment::MMIOSegment(PhysicalAddress segment_base_addr, u8 start_bus, u8 end_bus)
  202. : m_base_addr(segment_base_addr)
  203. , m_start_bus(start_bus)
  204. , m_end_bus(end_bus)
  205. {
  206. }
  207. u8 MMIOSegment::get_start_bus()
  208. {
  209. return m_start_bus;
  210. }
  211. u8 MMIOSegment::get_end_bus()
  212. {
  213. return m_end_bus;
  214. }
  215. size_t MMIOSegment::get_size()
  216. {
  217. return (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS * (get_end_bus() - get_start_bus()));
  218. }
  219. PhysicalAddress MMIOSegment::get_paddr()
  220. {
  221. return m_base_addr;
  222. }
  223. }
  224. }