MMIOAccess.cpp 10 KB

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