aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/acpi/manager.cpp
blob: 180d4585ddfab6ef286f89106c7026911f075417 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "kernel/acpi/manager.hpp"

#include "kapi/acpi.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"

#include <kstd/print>

#include <cstddef>
#include <cstdint>

namespace kernel::acpi
{

  manager::manager(kapi::acpi::root_system_description_pointer const & sdp)
      : m_sdp{&sdp}
  {
    if (m_sdp->signature() != "RSD PTR ")
    {
      kapi::system::panic("[OS:ACPI] Invalid RSDP signature!");
    }

    if (m_sdp->revision() >= 2)
    {
      auto const xsdp = static_cast<kapi::acpi::extended_system_description_pointer const *>(m_sdp);
      if (!xsdp->validate())
      {
        kapi::system::panic("[OS:ACPI] Invalid XSDP signature!");
      }
      auto physical_extended_table_address = xsdp->table_address();
      auto linear_extended_table_address = kapi::memory::hhdm_to_linear(physical_extended_table_address);
      m_rsdt = static_cast<kapi::acpi::system_description_table_header const *>(linear_extended_table_address);
      m_extended = true;
    }
    else
    {
      if (!m_sdp->validate())
      {
        kapi::system::panic("[OS:ACPI] Invalid RSDP checksum!");
      }
      auto physical_root_table_address = m_sdp->table_address();
      auto linear_root_table_address = kapi::memory::hhdm_to_linear(physical_root_table_address);
      m_rsdt = static_cast<kapi::acpi::system_description_table_header const *>(linear_root_table_address);
    }
  }

  auto manager::load_tables() -> bool
  {
    if (!kapi::acpi::validate_checksum({reinterpret_cast<std::byte const *>(m_rsdt), m_rsdt->length().value}))
    {
      kapi::system::panic("[OS:ACPI] Invalid RSDT checksum!");
    }

    auto entry_size = m_extended ? sizeof(std::uint64_t) : sizeof(std::uint32_t);
    auto entry_count = (m_rsdt->length().value - sizeof(kapi::acpi::system_description_table_header)) / entry_size;
    auto entries_base =
        reinterpret_cast<std::byte const *>(m_rsdt) + sizeof(kapi::acpi::system_description_table_header);

    for (std::size_t i = 0; i < entry_count; ++i)
    {
      auto physical_table_address = kapi::memory::physical_address{};

      if (m_extended)
      {
        auto entry = reinterpret_cast<std::uint64_t const *>(entries_base + (i * entry_size));
        physical_table_address = kapi::memory::physical_address{*entry};
      }
      else
      {
        auto entry = reinterpret_cast<std::uint32_t const *>(entries_base + (i * entry_size));
        physical_table_address = kapi::memory::physical_address{*entry};
      }

      auto linear_table_address = kapi::memory::hhdm_to_linear(physical_table_address);
      auto table = static_cast<kapi::acpi::system_description_table_header const *>(linear_table_address);

      if (!kapi::acpi::validate_checksum({reinterpret_cast<std::byte const *>(table), table->length().value}))
      {
        kstd::println(kstd::print_sink::stderr, "[OS:ACPI] Invalid table checksum!");
      }
      else
      {
        kstd::println("[OS:ACPI] Found table: {}@{:#x}/{:#x} OEM: {} Rev: {}", table->signature(), linear_table_address,
                      physical_table_address, table->oem_id(), table->creator_revision());
        m_tables.emplace(table->signature(), table);
      }
    }

    return !m_tables.empty();
  }

}  // namespace kernel::acpi