MultiProcessorParser.cpp 6.7 KB

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