aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/devices/init.cpp
blob: 7f0faa4ad4023ee3638025f638a39549295e873c (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
#include "arch/devices/init.hpp"

#include "kapi/acpi.hpp"
#include "kapi/cpu.hpp"
#include "kapi/devices.hpp"

#include "arch/boot/boot.hpp"
#include "arch/bus/isa.hpp"
#include "arch/devices/legacy_pit.hpp"

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

#include <acpi/acpi.hpp>

#include <cstdint>
#include <utility>

namespace arch::devices
{

  namespace
  {
    constexpr auto pit_frequency_in_hz = std::uint32_t{100u};

    auto get_acpi_root_pointer() -> kstd::observer_ptr<::acpi::rsdp const>
    {
      auto const & mbi = kapi::boot::bootstrap_information.mbi;
      auto system_description_pointer = static_cast<::acpi::rsdp const *>(nullptr);

      if (auto const & xsdp = mbi->maybe_acpi_xsdp())
      {
        auto data = xsdp->pointer().data();

        system_description_pointer = reinterpret_cast<::acpi::xsdp const *>(data);
      }
      else if (auto const & rsdp = mbi->maybe_acpi_rsdp())
      {
        auto data = rsdp->pointer().data();
        system_description_pointer = reinterpret_cast<::acpi::rsdp const *>(data);
      }

      return kstd::make_observer(system_description_pointer);
    }
  }  // namespace

  auto init_acpi_devices() -> void
  {
    auto acpi_root_pointer = get_acpi_root_pointer();
    if (acpi_root_pointer && acpi_root_pointer->validate())
    {
      if (kapi::acpi::init(*acpi_root_pointer))
      {
        kstd::println("[x86_64:DEV] ACPI subsystem initialized.");
      }
    }

    kapi::cpu::discover_topology();
  }

  auto init_legacy_devices() -> void
  {
    kstd::println("[x86_64:DEV] Initializing ISA bus...");

    auto isa_major_number = kapi::devices::allocate_major_number();
    auto isa_bus = kstd::make_unique<arch::bus::isa>(isa_major_number);

    auto pit_major_number = kapi::devices::allocate_major_number();
    auto pit = kstd::make_unique<arch::devices::legacy_pit>(pit_major_number, pit_frequency_in_hz);
    isa_bus->add_child(std::move(pit));

    auto & root_bus = kapi::devices::get_root_bus();
    root_bus.add_child(std::move(isa_bus));
  }

}  // namespace arch::devices