aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/acpi.cpp
blob: 998d7a017a4d597b5b54932bb649fffcc09e55df (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "kapi/acpi.hpp"

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

#include "kernel/acpi/manager.hpp"

#include <kstd/units>

#include <algorithm>
#include <atomic>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>

namespace kapi::acpi
{

  namespace
  {
    auto constinit manager = std::optional<kernel::acpi::manager>{};
  }  // namespace

  auto root_system_description_pointer::oem_id() const noexcept -> std::string_view
  {
    return {m_oem_id.data(), m_oem_id.size()};
  }

  auto root_system_description_pointer::revision() const noexcept -> std::uint8_t
  {
    return m_revision;
  }

  auto root_system_description_pointer::signature() const noexcept -> std::string_view
  {
    return {m_signature.data(), m_signature.size()};
  }

  auto root_system_description_pointer::table_address() const noexcept -> memory::physical_address
  {
    auto raw = std::bit_cast<std::uint32_t>(m_rsdt_address);
    return memory::physical_address{static_cast<std::uintptr_t>(raw)};
  }

  auto root_system_description_pointer::validate() const noexcept -> bool
  {
    return validate_checksum({reinterpret_cast<std::byte const *>(this), sizeof(root_system_description_pointer)});
  }

  auto extended_system_description_pointer::length() const noexcept -> kstd::units::bytes
  {
    return kstd::units::bytes{m_length};
  }

  auto extended_system_description_pointer::table_address() const noexcept -> memory::physical_address
  {
    return memory::physical_address{std::bit_cast<std::uintptr_t>(m_xsdt_address)};
  }

  auto extended_system_description_pointer::validate() const noexcept -> bool
  {
    return validate_checksum({reinterpret_cast<std::byte const *>(this), m_length});
  }

  [[nodiscard]] auto system_description_table_header::checksum() const noexcept -> std::uint8_t
  {
    return m_checksum;
  }

  [[nodiscard]] auto system_description_table_header::creator_revision() const noexcept -> std::uint32_t
  {
    return m_creator_revision;
  }

  [[nodiscard]] auto system_description_table_header::creator_id() const noexcept -> std::uint32_t
  {
    return m_creator_id;
  }

  [[nodiscard]] auto system_description_table_header::length() const noexcept -> kstd::units::bytes
  {
    return kstd::units::bytes{m_length};
  }

  [[nodiscard]] auto system_description_table_header::oem_id() const noexcept -> std::string_view
  {
    return {m_oem_id.data(), m_oem_id.size()};
  }

  [[nodiscard]] auto system_description_table_header::oem_revision() const noexcept -> std::uint32_t
  {
    return m_oem_revision;
  }

  [[nodiscard]] auto system_description_table_header::oem_table_id() const noexcept -> std::string_view
  {
    return {m_oem_table_id.data(), m_oem_table_id.size()};
  }

  [[nodiscard]] auto system_description_table_header::revision() const noexcept -> std::uint8_t
  {
    return m_revision;
  }

  [[nodiscard]] auto system_description_table_header::signature() const noexcept -> std::string_view
  {
    return {m_signature.data(), m_signature.size()};
  }

  auto init(root_system_description_pointer const & sdp) -> bool
  {
    auto static constinit initialized = std::atomic_flag{};
    if (initialized.test_and_set())
    {
      system::panic("[OS::ACPI] The ACPI manager has already been initialized!");
    }

    manager.emplace(sdp);
    return manager->load_tables();
  }

  auto validate_checksum(std::span<std::byte const> data) -> bool
  {
    auto sum = std::ranges::fold_left(data, std::uint8_t{}, [](auto acc, auto byte) {
      return static_cast<std::uint8_t>(acc + static_cast<std::uint8_t>(byte));
    });
    return sum == 0;
  }

};  // namespace kapi::acpi