PatchworkOS  321f6ec
A non-POSIX operating system.
Loading...
Searching...
No Matches
acpi.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/sysfs.h>
4
5#include <boot/boot_info.h>
6#include <kernel/defs.h>
7
8#include <stdint.h>
9
10/**
11 * @brief Advanced Configuration and Power Interface
12 * @defgroup modules_acpi ACPI
13 * @ingroup modules
14 *
15 * We use version 6.6 of the ACPI specification, but it contains minor mistakes or deprecated features that we use other
16 * versions to straighten out. If the "ACPI specification" is ever sourced, without mentioning its version, assume
17 * version 6.6.
18 *
19 * Take a look at this [osdev post](https://f.osdev.org/viewtopic.php?t=29070) if you want to understand how annoying
20 * the ACPI spec is.
21 *
22 * Checklist for ACPI support from section 1.7.2 of the ACPI specification:
23 * - [x] Use System Address Map Interfaces (this is done by the bootloader).
24 * - [x] Find and consume the ACPI System Description Tables (this is done in `acpi_tables_init()`)
25 * - [x] Interpret ACPI machine language (AML). (this is done in `aml_init()`)
26 * - [X] Enumerate and configure motherboard devices described in the ACPI Namespace. (this is done in
27 `acpi_devices_init()`)
28 * - [ ] Interface with the power management timer. <-- We are here.
29 * - [ ] Interface with the real-time clock wake alarm.
30 * - [ ] Enter ACPI mode (on legacy hardware systems).
31 * - [ ] Implement device power management policy.
32 * - [ ] Implement power resource management.
33 * - [ ] Implement processor power states in the scheduler idle handlers.
34 * - [ ] Control processor and device performance states.
35 * - [ ] Implement the ACPI thermal model.
36 * - [ ] Support the ACPI Event programming model including handling SCI interrupts, managing fixed events, general-
37 purpose events, embedded controller interrupts, and dynamic device support.
38 * - [ ] Support acquisition and release of the Global Lock.
39 * - [ ] Use the reset register to reset the system.
40 * - [ ] Provide APIs to influence power management policy.
41 * - [ ] Implement driver support for ACPI-defined devices.
42 * - [ ] Implement APIs supporting the system indicators.
43 * - [ ] Support all system states S1-S5.
44 *
45 * Included below is a list of deviations from the ACPI specification that we have made. Each deviation is marked with
46 the source of the deviation in parentheses:
47 * - Copying an object to itself is a no-op. (Windows)
48 * - Converting a zero-length String to a Buffer still copies the null-terminator. (ACPICA)
49 *
50 * @see [Easier to read version of the ACPI Specification](https://uefi.org/specs/ACPI/6.6/index.html)
51 * @see [ACPI Specification Version 6.6](https://uefi.org/sites/default/files/resources/ACPI_Spec_6.6.pdf)
52 * @see [ACPI Specification Version 6.3](https://uefi.org/sites/default/files/resources/ACPI_Spec_6_3_A_Oct_6_2020.pdf)
53 * @see [ACPI Specification Version 4.0](https://uefi.org/sites/default/files/resources/ACPI_4.pdf)
54 * @see [ACPICA Reference
55 Version 6.3](https://www.intel.com/content/www/us/en/developer/topic-technology/open/acpica/documentation.html)
56 * @see [LAI Library](https://github.com/managarm/lai)
57 *
58 * @{
59 */
60
61/**
62 * @brief The expected value of the revision field in the RSDP structure.
63 *
64 * @see Section 5.2.5.3 of the ACPI specification for more details.
65 */
66#define RSDP_CURRENT_REVISION 2
67
68/**
69 * @brief The length of the signature field in the SDT header structure.
70 */
71#define SDT_SIGNATURE_LENGTH 4
72
73/**
74 * @brief The length of the OEM ID field in the SDT header structure.
75 */
76#define SDT_OEM_ID_LENGTH 6
77
78/**
79 * @brief The length of the OEM Table ID field in the SDT header structure.
80 */
81#define SDT_OEM_TABLE_ID_LENGTH 8
82
83/**
84 * @brief System Description Table Header
85 * @struct sdt_header_t
86 *
87 * @see Section 5.2.6 of the ACPI specification for more details.
88 */
101
102/**
103 * @brief Length of the signature field in the RSDP structure.
104 */
105#define RSDP_SIGNATURE_LENGTH 8
106
107/**
108 * @brief Root System Description Pointer
109 * @struct rsdp_t
110 *
111 * @see Section 5.2.5.3 of the ACPI specification for more details.
112 */
113typedef struct PACKED
114{
115 char signature[RSDP_SIGNATURE_LENGTH];
116 uint8_t checksum; ///< This is the checksum for the first 20 bytes only.
117 char oemId[SDT_OEM_ID_LENGTH];
122 uint8_t extendedChecksum; ///< This is the checksum for the entire table.
123 uint8_t reserved[3];
124} rsdp_t;
125
126/**
127 * @brief Length of the RSDP structure for ACPI version 1.0.
128 */
129#define RSDP_V1_LENGTH 20
130
131/**
132 * @brief Extended System Description Table
133 * @struct xsdt_t
134 *
135 * @see Section 5.2.8 of the ACPI specification for more details.
136 */
137typedef struct PACKED
138{
140 sdt_header_t* tables[];
141} xsdt_t;
142
143/**
144 * @brief Check if the sum of all bytes in a table is 0.
145 *
146 * Used to validate the checksum of ACPI tables.
147 *
148 * @param table Pointer to the table structure.
149 * @return true if the table is valid, false otherwise.
150 */
151bool acpi_is_checksum_valid(void* table, uint64_t length);
152
153/**
154 * @brief Retrieve the sysfs root directory for ACPI.
155 *
156 * The acpi group and by extension its directory is lazily initialized.
157 *
158 * @return Pointer to the ACPI sysfs root directory.
159 */
161
162/**
163 * @brief Reclaim ACPI memory regions.
164 *
165 * This function will free all memory regions marked as ACPI Reclaimable in the provided boot memory map.
166 *
167 * Should be called after all ACPI initialization is complete.
168 *
169 * @param map Pointer to the boot memory map.
170 */
172
173/** @} */
#define PACKED
GCC packed attribute.
Definition defs.h:32
dentry_t * acpi_get_sysfs_root(void)
Retrieve the sysfs root directory for ACPI.
Definition acpi.c:35
#define SDT_OEM_ID_LENGTH
The length of the OEM ID field in the SDT header structure.
Definition acpi.h:76
#define SDT_OEM_TABLE_ID_LENGTH
The length of the OEM Table ID field in the SDT header structure.
Definition acpi.h:81
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
#define SDT_SIGNATURE_LENGTH
The length of the signature field in the SDT header structure.
Definition acpi.h:71
#define RSDP_SIGNATURE_LENGTH
Length of the signature field in the RSDP structure.
Definition acpi.h:105
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
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Directory entry structure.
Definition dentry.h:84
Root System Description Pointer.
Definition acpi.h:114
uint8_t extendedChecksum
This is the checksum for the entire table.
Definition acpi.h:122
uint32_t rsdtAddress
Definition acpi.h:119
uint8_t revision
Definition acpi.h:118
uint32_t length
Definition acpi.h:120
uint8_t checksum
This is the checksum for the first 20 bytes only.
Definition acpi.h:116
uint64_t xsdtAddress
Definition acpi.h:121
System Description Table Header.
Definition acpi.h:90
uint32_t oemRevision
Definition acpi.h:97
uint32_t creatorRevision
Definition acpi.h:99
uint8_t revision
Definition acpi.h:93
uint32_t length
Definition acpi.h:92
uint8_t checkSum
Definition acpi.h:94
uint32_t creatorID
Definition acpi.h:98
Extended System Description Table.
Definition acpi.h:138
sdt_header_t header
Definition acpi.h:139