MultiProcessorParser.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/StringView.h>
  28. #include <Kernel/ACPI/MultiProcessorParser.h>
  29. #include <Kernel/Arch/PC/BIOS.h>
  30. #include <Kernel/Interrupts/IOAPIC.h>
  31. #include <Kernel/StdLib.h>
  32. #include <Kernel/VM/MemoryManager.h>
  33. #include <Kernel/VM/TypedMapping.h>
  34. //#define MULTIPROCESSOR_DEBUG
  35. namespace Kernel {
  36. static MultiProcessorParser* s_parser;
  37. bool MultiProcessorParser::is_initialized()
  38. {
  39. return s_parser != nullptr;
  40. }
  41. void MultiProcessorParser::initialize()
  42. {
  43. ASSERT(!is_initialized());
  44. s_parser = new MultiProcessorParser;
  45. }
  46. MultiProcessorParser::MultiProcessorParser()
  47. : m_floating_pointer(search_floating_pointer())
  48. {
  49. if (!m_floating_pointer.is_null()) {
  50. klog() << "MultiProcessor: Floating Pointer Structure @ " << PhysicalAddress(m_floating_pointer);
  51. parse_floating_pointer_data();
  52. parse_configuration_table();
  53. } else {
  54. klog() << "MultiProcessor: Can't Locate Floating Pointer Structure, disabled.";
  55. }
  56. }
  57. void MultiProcessorParser::parse_floating_pointer_data()
  58. {
  59. auto floating_pointer = map_typed<MultiProcessor::FloatingPointer>(m_floating_pointer);
  60. m_configuration_table = PhysicalAddress(floating_pointer->physical_address_ptr);
  61. dbg() << "Features " << floating_pointer->feature_info[0] << ", IMCR? " << (floating_pointer->feature_info[0] & (1 << 7));
  62. }
  63. void MultiProcessorParser::parse_configuration_table()
  64. {
  65. auto configuration_table_length = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table)->length;
  66. auto config_table = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table, configuration_table_length);
  67. size_t entry_count = config_table->entry_count;
  68. auto* entry = config_table->entries;
  69. while (entry_count > 0) {
  70. #ifdef MULTIPROCESSOR_DEBUG
  71. dbg() << "MultiProcessor: Entry Type " << entry->entry_type << " detected.";
  72. #endif
  73. switch (entry->entry_type) {
  74. case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
  75. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
  76. break;
  77. case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
  78. m_bus_entries.append(*(const MultiProcessor::BusEntry*)entry);
  79. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
  80. break;
  81. case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
  82. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
  83. break;
  84. case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
  85. m_io_interrupt_assignment_entries.append(*(const MultiProcessor::IOInterruptAssignmentEntry*)entry);
  86. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
  87. break;
  88. case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
  89. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Local_Interrupt_Assignment;
  90. break;
  91. case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
  92. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::SystemAddressSpaceMapping;
  93. break;
  94. case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
  95. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::BusHierarchyDescriptor;
  96. break;
  97. case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
  98. entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::CompatibilityBusAddressSpaceModifier;
  99. break;
  100. default:
  101. ASSERT_NOT_REACHED();
  102. }
  103. --entry_count;
  104. }
  105. }
  106. PhysicalAddress MultiProcessorParser::search_floating_pointer()
  107. {
  108. auto mp_floating_pointer = search_floating_pointer_in_ebda();
  109. if (!mp_floating_pointer.is_null())
  110. return mp_floating_pointer;
  111. return search_floating_pointer_in_bios_area();
  112. }
  113. PhysicalAddress MultiProcessorParser::search_floating_pointer_in_ebda()
  114. {
  115. klog() << "MultiProcessor: Probing EBDA";
  116. auto ebda = map_ebda();
  117. for (auto* floating_pointer_str = ebda.base(); floating_pointer_str < ebda.end(); floating_pointer_str += 16) {
  118. if (!strncmp("_MP_", (const char*)floating_pointer_str, strlen("_MP_")))
  119. return ebda.paddr_of(floating_pointer_str);
  120. }
  121. return {};
  122. }
  123. PhysicalAddress MultiProcessorParser::search_floating_pointer_in_bios_area()
  124. {
  125. auto bios = map_bios();
  126. for (auto* floating_pointer_str = bios.base(); floating_pointer_str < bios.end(); floating_pointer_str += 16) {
  127. if (!strncmp("_MP_", (const char*)floating_pointer_str, strlen("_MP_")))
  128. return bios.paddr_of(floating_pointer_str);
  129. }
  130. return {};
  131. }
  132. Vector<u8> MultiProcessorParser::get_pci_bus_ids() const
  133. {
  134. Vector<u8> pci_bus_ids;
  135. for (auto& entry : m_bus_entries) {
  136. if (!strncmp("PCI ", entry.bus_type, strlen("PCI ")))
  137. pci_bus_ids.append(entry.bus_id);
  138. }
  139. return pci_bus_ids;
  140. }
  141. MultiProcessorParser& MultiProcessorParser::the()
  142. {
  143. ASSERT(is_initialized());
  144. return *s_parser;
  145. }
  146. Vector<PCIInterruptOverrideMetadata> MultiProcessorParser::get_pci_interrupt_redirections()
  147. {
  148. dbg() << "MultiProcessor: Get PCI IOAPIC redirections";
  149. Vector<PCIInterruptOverrideMetadata> overrides;
  150. auto pci_bus_ids = get_pci_bus_ids();
  151. for (auto& entry : m_io_interrupt_assignment_entries) {
  152. for (auto id : pci_bus_ids) {
  153. if (id == entry.source_bus_id) {
  154. klog() << "Interrupts: Bus " << entry.source_bus_id << ", Polarity " << entry.polarity << ", Trigger Mode " << entry.trigger_mode << ", INT " << entry.source_bus_irq << ", IOAPIC " << entry.destination_ioapic_id << ", IOAPIC INTIN " << entry.destination_ioapic_intin_pin;
  155. overrides.empend(
  156. entry.source_bus_id,
  157. entry.polarity,
  158. entry.trigger_mode,
  159. entry.source_bus_irq,
  160. entry.destination_ioapic_id,
  161. entry.destination_ioapic_intin_pin);
  162. }
  163. }
  164. }
  165. for (auto& override_metadata : overrides) {
  166. klog() << "Interrupts: Bus " << override_metadata.bus() << ", Polarity " << override_metadata.polarity() << ", PCI Device " << override_metadata.pci_device_number() << ", Trigger Mode " << override_metadata.trigger_mode() << ", INT " << override_metadata.pci_interrupt_pin() << ", IOAPIC " << override_metadata.ioapic_id() << ", IOAPIC INTIN " << override_metadata.ioapic_interrupt_pin();
  167. }
  168. return overrides;
  169. }
  170. PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
  171. : m_bus_id(bus_id)
  172. , m_polarity(polarity)
  173. , m_trigger_mode(trigger_mode)
  174. , m_pci_interrupt_pin(source_irq & 0b11)
  175. , m_pci_device_number((source_irq >> 2) & 0b11111)
  176. , m_ioapic_id(ioapic_id)
  177. , m_ioapic_interrupt_pin(ioapic_int_pin)
  178. {
  179. }
  180. }