blob: fb273294ed59e7fe29a136f3e3e1aea2228a10b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include "kapi/platform.hpp"
#include "kapi/acpi.hpp"
#include "kapi/devices.hpp"
#include "arch/devices/local_apic.hpp"
#include <kstd/memory>
#include <kstd/print>
#include <ranges>
#include <utility>
namespace kapi::platform
{
namespace
{
constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled //
| acpi::processor_local_apic::flags::online_capable;
}
auto discover_cpu_topology(kapi::devices::bus & bus) -> bool
{
auto static const core_major = kapi::devices::allocate_major_number();
auto static const interrupt_controller_major = kapi::devices::allocate_major_number();
auto madt = kapi::acpi::get_table<acpi::madt_table_signature>();
if (!madt)
{
kstd::println("[x86_64:PLT] Failed to find ACPI APIC table");
return false;
}
auto bsp_found = false;
auto core_count = 0uz;
auto local_apic_address = madt->local_interrupt_controller_address();
auto lapic_entries = *madt | std::views::filter([](auto const & entry) {
return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic;
}) | std::views::transform([](auto const & entry) {
return static_cast<acpi::processor_local_apic const &>(entry);
}) | std::views::filter([](auto const & entry) {
return static_cast<bool>(entry.active_flags() & candidate_flags);
});
for (auto const & apic : lapic_entries)
{
auto is_bsp = !bsp_found;
bsp_found = true;
auto instance = kstd::make_unique<arch::devices::local_apic>(interrupt_controller_major, core_count,
apic.apic_id(), local_apic_address, is_bsp);
if (kapi::platform::cpu_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance)))
{
++core_count;
}
}
kstd::println("[x86_64:PLT] Found {} CPU cores", core_count);
return core_count > 0;
}
} // namespace kapi::platform
|