aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/acpi/manager.cpp
blob: 501ce923e3b2d88a8afb189cb236932487960e08 (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
93
94
95
96
97
98
99
100
101
102
#include "kernel/acpi/manager.hpp"

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

#include <kstd/memory>
#include <kstd/print>

#include <acpi/acpi.hpp>

#include <cstddef>
#include <cstdint>
#include <string_view>

namespace kernel::acpi
{

  manager::manager(::acpi::rsdp 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<::acpi::xsdp const *>(m_sdp);
      if (!xsdp->validate())
      {
        kapi::system::panic("[OS:ACPI] Invalid XSDP signature!");
      }
      auto physical_extended_table_address = kapi::memory::physical_address{xsdp->table_address()};
      auto linear_extended_table_address = kapi::memory::hhdm_to_linear(physical_extended_table_address);
      m_rsdt = static_cast<::acpi::sdt 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 = kapi::memory::physical_address{m_sdp->table_address()};
      auto linear_root_table_address = kapi::memory::hhdm_to_linear(physical_root_table_address);
      m_rsdt = static_cast<::acpi::sdt const *>(linear_root_table_address);
    }
  }

  auto manager::load_tables() -> bool
  {
    if (!::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(::acpi::sdt)) / entry_size;
    auto entries_base = reinterpret_cast<std::byte const *>(m_rsdt) + sizeof(::acpi::sdt);

    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<::acpi::sdt const *>(linear_table_address);

      if (!::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 '{}' ACPI table", table->signature());
        m_tables.emplace(table->signature(), table);
      }
    }

    return !m_tables.empty();
  }

  auto manager::get_table(std::string_view signature) -> kstd::observer_ptr<::acpi::sdt const>
  {
    if (m_tables.contains(signature))
    {
      return kstd::make_observer(m_tables.at(signature));
    }
    return nullptr;
  }

}  // namespace kernel::acpi