PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
acpi.c
Go to the documentation of this file.
1#include <modules/acpi/acpi.h>
2
6
7#include <kernel/fs/mount.h>
10#include <kernel/init/init.h>
11#include <kernel/log/log.h>
12#include <kernel/log/panic.h>
13#include <kernel/mem/pmm.h>
15#include <kernel/proc/process.h>
16#include <kernel/sched/sched.h>
17
18#include <boot/boot_info.h>
19#include <string.h>
20
21static bool mountInitialzed = false;
22static mount_t* mount = NULL;
23
24bool acpi_is_checksum_valid(void* table, uint64_t length)
25{
26 uint8_t sum = 0;
27 for (uint64_t i = 0; i < length; i++)
28 {
29 sum += ((uint8_t*)table)[i];
30 }
31
32 return sum == 0;
33}
34
36{
37 if (!mountInitialzed)
38 {
41 if (mount == NULL)
42 {
43 panic(NULL, "failed to initialize ACPI sysfs group");
44 }
45
46 mountInitialzed = true;
47 }
48
49 return REF(mount->source);
50}
51
53{
54 for (uint64_t i = 0; i < map->length; i++)
55 {
56 const EFI_MEMORY_DESCRIPTOR* desc = BOOT_MEMORY_MAP_GET_DESCRIPTOR(map, i);
57
58 if (desc->Type == EfiACPIReclaimMemory)
59 {
60 pmm_free_region((void*)PML_LOWER_TO_HIGHER(desc->PhysicalStart), desc->NumberOfPages);
61 LOG_INFO("reclaim memory [0x%016lx-0x%016lx]\n", desc->PhysicalStart,
62 ((uintptr_t)desc->PhysicalStart) + desc->NumberOfPages * PAGE_SIZE);
63 }
64 }
65}
66
68{
69 switch (event->type)
70 {
72 {
74 if (bootInfo == NULL || bootInfo->rsdp == NULL)
75 {
76 LOG_ERR("no RSDP provided by bootloader\n");
77 return ERR;
78 }
79
81 {
82 LOG_ERR("failed to initialize ACPI tables\n");
83 return ERR;
84 }
85
86 if (aml_init() == ERR)
87 {
88 LOG_ERR("failed to initialize AML subsystem\n");
89 return ERR;
90 }
91
92 if (acpi_devices_init() == ERR)
93 {
94 LOG_ERR("failed to initialize ACPI devices\n");
95 return ERR;
96 }
97
99
100 if (acpi_tables_expose() == ERR)
101 {
102 LOG_ERR("failed to expose ACPI tables via sysfs\n");
103 return ERR;
104 }
105
106 if (aml_namespace_expose() == ERR)
107 {
108 LOG_ERR("failed to expose ACPI devices via sysfs\n");
109 return ERR;
110 }
111 }
112 break;
113 default:
114 break;
115 }
116
117 return 0;
118}
119
120MODULE_INFO("ACPI Module", "Kai Norberg",
121 "ACPI subsystem providing ACPI table handling, AML parsing and device management", OS_VERSION, "MIT", "BOOT_RSDP");
uint64_t _module_procedure(const module_event_t *event)
Definition acpi.c:67
static bool mountInitialzed
Definition acpi.c:21
static mount_t * mount
Definition acpi.c:22
boot_info_t * bootInfo
Definition boot_info.c:14
#define BOOT_MEMORY_MAP_GET_DESCRIPTOR(map, index)
Definition boot_info.h:52
@ MODE_ALL_PERMS
Definition path.h:87
@ MODE_DIRECTORY
Definition path.h:84
boot_info_t * boot_info_get(void)
Gets the boot info structure.
Definition boot_info.c:16
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:266
#define LOG_ERR(format,...)
Definition log.h:108
#define LOG_INFO(format,...)
Definition log.h:106
#define PML_LOWER_TO_HIGHER(addr)
Converts an address from the lower half to the higher half.
void pmm_free_region(void *address, uint64_t count)
Frees a contiguous region of physical pages.
Definition pmm.c:235
#define MODULE_INFO(_name, _author, _description, _version, _licence, _deviceTypes)
Macro to define module information.
Definition module.h:200
@ MODULE_EVENT_DEVICE_ATTACH
Definition module.h:251
#define REF(ptr)
Increment reference count.
Definition ref.h:65
@ MOUNT_PROPAGATE_PARENT
Propagate the mount to parent namespaces.
Definition io.h:490
@ MOUNT_PROPAGATE_CHILDREN
Propagate the mount to child namespaces.
Definition io.h:491
#define PAGE_SIZE
The size of a memory page in bytes.
Definition proc.h:106
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t aml_namespace_expose(void)
Expose the entire namespace heirarchy to sysfs.
Definition namespace.c:121
uint64_t aml_init(void)
Initialize the AML subsystem.
Definition aml.c:107
uint64_t acpi_devices_init(void)
Enumerate, configure and load modules for ACPI devices.
Definition devices.c:480
uint64_t acpi_tables_init(rsdp_t *rsdp)
Initialize ACPI tables and call their init handlers.
Definition tables.c:195
uint64_t acpi_tables_expose(void)
Expose ACPI tables to sysfs.
Definition tables.c:221
dentry_t * acpi_get_sysfs_root(void)
Retrieve the sysfs root directory for ACPI.
Definition acpi.c:35
bool acpi_is_checksum_valid(void *table, uint64_t length)
Check if the sum of all bytes in a table is 0.
Definition acpi.c:24
void acpi_reclaim_memory(const boot_memory_map_t *map)
Reclaim ACPI memory regions.
Definition acpi.c:52
boot_memory_map_t * map
Definition mem.c:19
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
boot_memory_t memory
Definition boot_info.h:106
void * rsdp
Definition boot_info.h:102
uint64_t length
Definition boot_info.h:58
boot_memory_map_t map
Definition boot_info.h:95
Directory entry structure.
Definition dentry.h:84
module_event_type_t type
Definition module.h:268
Mount structure.
Definition mount.h:44
dentry_t * source
The dentry to appear at target once mounted, usually the root dentry of the mounted filesystem.
Definition mount.h:48
mount_t * sysfs_mount_new(const path_t *parent, const char *name, namespace_t *ns, mount_flags_t flags, mode_t mode, const superblock_ops_t *superblockOps)
Mount a new instance of SysFS.
Definition sysfs.c:104