MultiProcessorParser.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/Debug.h>
  31. #include <Kernel/Interrupts/IOAPIC.h>
  32. #include <Kernel/StdLib.h>
  33. #include <Kernel/VM/MemoryManager.h>
  34. #include <Kernel/VM/TypedMapping.h>
  35. namespace Kernel {
  36. UNMAP_AFTER_INIT OwnPtr<MultiProcessorParser> MultiProcessorParser::autodetect()
  37. {
  38. auto floating_pointer = find_floating_pointer();
  39. if (!floating_pointer.has_value())
  40. return {};
  41. return adopt_own(*new MultiProcessorParser(floating_pointer.value()));
  42. }
  43. UNMAP_AFTER_INIT MultiProcessorParser::MultiProcessorParser(PhysicalAddress floating_pointer)
  44. : m_floating_pointer(floating_pointer)
  45. {
  46. klog() << "MultiProcessor: Floating Pointer Structure @ " << m_floating_pointer;
  47. parse_floating_pointer_data();
  48. parse_configuration_table();
  49. }
  50. UNMAP_AFTER_INIT void MultiProcessorParser::parse_floating_pointer_data()
  51. {
  52. auto floating_pointer = map_typed<MultiProcessor::FloatingPointer>(m_floating_pointer);
  53. m_configuration_table = PhysicalAddress(floating_pointer->physical_address_ptr);
  54. dbgln("Features {}, IMCR? {}", floating_pointer->feature_info[0], (floating_pointer->feature_info[0] & (1 << 7)));
  55. }
  56. UNMAP_AFTER_INIT void MultiProcessorParser::parse_configuration_table()
  57. {
  58. auto configuration_table_length = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table)->length;
  59. auto config_table = map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table, configuration_table_length);
  60. size_t entry_count = config_table->entry_count;
  61. auto* entry = config_table->entries;
  62. while (entry_count > 0) {
  63. dbgln_if(MULTIPROCESSOR_DEBUG, "MultiProcessor: Entry Type {} detected.", entry->entry_type);
  64. switch (entry->entry_type) {
  65. case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor):
  66. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::ProcessorEntry);
  67. break;
  68. case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
  69. m_bus_entries.append(*(const MultiProcessor::BusEntry*)entry);
  70. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusEntry);
  71. break;
  72. case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC):
  73. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOAPICEntry);
  74. break;
  75. case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
  76. m_io_interrupt_assignment_entries.append(*(const MultiProcessor::IOInterruptAssignmentEntry*)entry);
  77. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOInterruptAssignmentEntry);
  78. break;
  79. case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment):
  80. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::LocalInterruptAssignmentEntry);
  81. break;
  82. case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping):
  83. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::SystemAddressSpaceMappingEntry);
  84. break;
  85. case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor):
  86. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusHierarchyDescriptorEntry);
  87. break;
  88. case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier):
  89. entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::CompatibilityBusAddressSpaceModifierEntry);
  90. break;
  91. default:
  92. ASSERT_NOT_REACHED();
  93. }
  94. --entry_count;
  95. }
  96. }
  97. UNMAP_AFTER_INIT Optional<PhysicalAddress> MultiProcessorParser::find_floating_pointer()
  98. {
  99. StringView signature("_MP_");
  100. auto mp_floating_pointer = map_ebda().find_chunk_starting_with(signature, 16);
  101. if (mp_floating_pointer.has_value())
  102. return mp_floating_pointer;
  103. return map_bios().find_chunk_starting_with(signature, 16);
  104. }
  105. UNMAP_AFTER_INIT Vector<u8> MultiProcessorParser::get_pci_bus_ids() const
  106. {
  107. Vector<u8> pci_bus_ids;
  108. for (auto& entry : m_bus_entries) {
  109. if (!strncmp("PCI ", entry.bus_type, strlen("PCI ")))
  110. pci_bus_ids.append(entry.bus_id);
  111. }
  112. return pci_bus_ids;
  113. }
  114. UNMAP_AFTER_INIT Vector<PCIInterruptOverrideMetadata> MultiProcessorParser::get_pci_interrupt_redirections()
  115. {
  116. dbgln("MultiProcessor: Get PCI IOAPIC redirections");
  117. Vector<PCIInterruptOverrideMetadata> overrides;
  118. auto pci_bus_ids = get_pci_bus_ids();
  119. for (auto& entry : m_io_interrupt_assignment_entries) {
  120. for (auto id : pci_bus_ids) {
  121. if (id == entry.source_bus_id) {
  122. 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;
  123. overrides.empend(
  124. entry.source_bus_id,
  125. entry.polarity,
  126. entry.trigger_mode,
  127. entry.source_bus_irq,
  128. entry.destination_ioapic_id,
  129. entry.destination_ioapic_intin_pin);
  130. }
  131. }
  132. }
  133. for (auto& override_metadata : overrides) {
  134. 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();
  135. }
  136. return overrides;
  137. }
  138. UNMAP_AFTER_INIT PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin)
  139. : m_bus_id(bus_id)
  140. , m_polarity(polarity)
  141. , m_trigger_mode(trigger_mode)
  142. , m_pci_interrupt_pin(source_irq & 0b11)
  143. , m_pci_device_number((source_irq >> 2) & 0b11111)
  144. , m_ioapic_id(ioapic_id)
  145. , m_ioapic_interrupt_pin(ioapic_int_pin)
  146. {
  147. }
  148. }