Serenity Operating System
at master 164 lines 7.3 kB view raw
1/* 2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> 3 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 4 * Copyright (c) 2022, the SerenityOS developers. 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#include <AK/StringView.h> 10#include <Kernel/Arch/x86_64/Interrupts/IOAPIC.h> 11#include <Kernel/Debug.h> 12#include <Kernel/Firmware/BIOS.h> 13#include <Kernel/Firmware/MultiProcessor/Parser.h> 14#include <Kernel/Memory/TypedMapping.h> 15#include <Kernel/Sections.h> 16#include <Kernel/StdLib.h> 17 18namespace Kernel { 19 20UNMAP_AFTER_INIT OwnPtr<MultiProcessorParser> MultiProcessorParser::autodetect() 21{ 22 auto floating_pointer = find_floating_pointer(); 23 if (!floating_pointer.has_value()) 24 return {}; 25 auto parser = adopt_own_if_nonnull(new (nothrow) MultiProcessorParser(floating_pointer.value())); 26 VERIFY(parser != nullptr); 27 return parser; 28} 29 30UNMAP_AFTER_INIT MultiProcessorParser::MultiProcessorParser(PhysicalAddress floating_pointer) 31 : m_floating_pointer(floating_pointer) 32{ 33 dbgln("MultiProcessor: Floating Pointer Structure @ {}", m_floating_pointer); 34 parse_floating_pointer_data(); 35 parse_configuration_table(); 36} 37 38UNMAP_AFTER_INIT void MultiProcessorParser::parse_floating_pointer_data() 39{ 40 auto floating_pointer = Memory::map_typed<MultiProcessor::FloatingPointer>(m_floating_pointer).release_value_but_fixme_should_propagate_errors(); 41 m_configuration_table = PhysicalAddress(floating_pointer->physical_address_ptr); 42 dbgln("Features {}, IMCR? {}", floating_pointer->feature_info[0], (floating_pointer->feature_info[0] & (1 << 7))); 43} 44 45UNMAP_AFTER_INIT void MultiProcessorParser::parse_configuration_table() 46{ 47 auto configuration_table_length = Memory::map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table).release_value_but_fixme_should_propagate_errors()->length; 48 auto config_table = Memory::map_typed<MultiProcessor::ConfigurationTableHeader>(m_configuration_table, configuration_table_length).release_value_but_fixme_should_propagate_errors(); 49 50 size_t entry_count = config_table->entry_count; 51 auto* entry = config_table->entries; 52 while (entry_count > 0) { 53 dbgln_if(MULTIPROCESSOR_DEBUG, "MultiProcessor: Entry Type {} detected.", entry->entry_type); 54 switch (entry->entry_type) { 55 case ((u8)MultiProcessor::ConfigurationTableEntryType::Processor): 56 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::ProcessorEntry); 57 break; 58 case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus): 59 MUST(m_bus_entries.try_append(*(MultiProcessor::BusEntry const*)entry)); 60 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusEntry); 61 break; 62 case ((u8)MultiProcessor::ConfigurationTableEntryType::IOAPIC): 63 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOAPICEntry); 64 break; 65 case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment): 66 MUST(m_io_interrupt_assignment_entries.try_append(*(MultiProcessor::IOInterruptAssignmentEntry const*)entry)); 67 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::IOInterruptAssignmentEntry); 68 break; 69 case ((u8)MultiProcessor::ConfigurationTableEntryType::Local_Interrupt_Assignment): 70 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::LocalInterruptAssignmentEntry); 71 break; 72 case ((u8)MultiProcessor::ConfigurationTableEntryType::SystemAddressSpaceMapping): 73 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::SystemAddressSpaceMappingEntry); 74 break; 75 case ((u8)MultiProcessor::ConfigurationTableEntryType::BusHierarchyDescriptor): 76 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::BusHierarchyDescriptorEntry); 77 break; 78 case ((u8)MultiProcessor::ConfigurationTableEntryType::CompatibilityBusAddressSpaceModifier): 79 entry = (MultiProcessor::EntryHeader*)(FlatPtr)entry + sizeof(MultiProcessor::CompatibilityBusAddressSpaceModifierEntry); 80 break; 81 default: 82 VERIFY_NOT_REACHED(); 83 } 84 --entry_count; 85 } 86} 87 88UNMAP_AFTER_INIT Optional<PhysicalAddress> MultiProcessorParser::find_floating_pointer() 89{ 90 constexpr auto signature = "_MP_"sv; 91 auto ebda_or_error = map_ebda(); 92 if (!ebda_or_error.is_error()) { 93 auto mp_floating_pointer = ebda_or_error.value().find_chunk_starting_with(signature, 16); 94 if (mp_floating_pointer.has_value()) 95 return mp_floating_pointer; 96 } 97 auto bios_or_error = map_bios(); 98 if (bios_or_error.is_error()) 99 return {}; 100 return bios_or_error.value().find_chunk_starting_with(signature, 16); 101} 102 103UNMAP_AFTER_INIT Vector<u8> MultiProcessorParser::get_pci_bus_ids() const 104{ 105 Vector<u8> pci_bus_ids; 106 for (auto& entry : m_bus_entries) { 107 if (!strncmp("PCI ", entry.bus_type, strlen("PCI "))) 108 pci_bus_ids.append(entry.bus_id); 109 } 110 return pci_bus_ids; 111} 112 113UNMAP_AFTER_INIT Vector<PCIInterruptOverrideMetadata> MultiProcessorParser::get_pci_interrupt_redirections() 114{ 115 dbgln("MultiProcessor: Get PCI IOAPIC redirections"); 116 Vector<PCIInterruptOverrideMetadata> overrides; 117 auto pci_bus_ids = get_pci_bus_ids(); 118 for (auto& entry : m_io_interrupt_assignment_entries) { 119 for (auto id : pci_bus_ids) { 120 if (id == entry.source_bus_id) { 121 122 dbgln("Interrupts: Bus {}, polarity {}, trigger mode {}, INT {}, IOAPIC {}, IOAPIC INTIN {}", 123 entry.source_bus_id, 124 entry.polarity, 125 entry.trigger_mode, 126 entry.source_bus_irq, 127 entry.destination_ioapic_id, 128 entry.destination_ioapic_intin_pin); 129 MUST(overrides.try_empend( 130 entry.source_bus_id, 131 entry.polarity, 132 entry.trigger_mode, 133 entry.source_bus_irq, 134 entry.destination_ioapic_id, 135 entry.destination_ioapic_intin_pin)); 136 } 137 } 138 } 139 140 for (auto& override_metadata : overrides) { 141 dbgln("Interrupts: Bus {}, polarity {}, PCI device {}, trigger mode {}, INT {}, IOAPIC {}, IOAPIC INTIN {}", 142 override_metadata.bus(), 143 override_metadata.polarity(), 144 override_metadata.pci_device_number(), 145 override_metadata.trigger_mode(), 146 override_metadata.pci_interrupt_pin(), 147 override_metadata.ioapic_id(), 148 override_metadata.ioapic_interrupt_pin()); 149 } 150 return overrides; 151} 152 153UNMAP_AFTER_INIT PCIInterruptOverrideMetadata::PCIInterruptOverrideMetadata(u8 bus_id, u8 polarity, u8 trigger_mode, u8 source_irq, u32 ioapic_id, u16 ioapic_int_pin) 154 : m_bus_id(bus_id) 155 , m_polarity(polarity) 156 , m_trigger_mode(trigger_mode) 157 , m_pci_interrupt_pin(source_irq & 0b11) 158 , m_pci_device_number((source_irq >> 2) & 0b11111) 159 , m_ioapic_id(ioapic_id) 160 , m_ioapic_interrupt_pin(ioapic_int_pin) 161{ 162} 163 164}