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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#include <acpi.h>
#include <lib.h>
#include <stdint.h>
#include <stddef.h>
#include "bindings.h"
#include "memory.h"
#include "serial.h"
/* global state, idk a better way rn */
struct acpi_state state;
struct acpi_header {
char signature[4];
uint32_t length;
uint8_t revision;
uint8_t checksum;
char oem_id[6];
char oem_table_id[8];
uint32_t oem_revision;
uint32_t creator_id;
uint32_t creator_revision;
};
// root system descriptor pointer
// ACPI 1.0
struct rsdp {
char signature[8];
uint8_t checksum;
char oemid[6];
uint8_t revision;
uint32_t rsdt_addr;
};
// eXtended system descriptor pointer
// ACPI 2.0
struct xsdp {
char signature[8];
uint8_t checksum;
char oemid[6];
uint8_t revision;
uint32_t rsdt_addr;
uint32_t length;
uint64_t xsdt_addr;
uint8_t extendeid_checksum;
uint8_t reserved[3];
};
// root system descriptor table
// ACPI 1.0
struct rsdt {
struct acpi_header h;
uint32_t sdt_pointers[];
};
// eXtended system descriptor table
// ACPI 2.0
struct xsdt {
struct acpi_header h;
uint64_t sdt_pointers[];
};
// generic address structure
struct gas {
uint8_t address_space;
uint8_t bit_width;
uint8_t bit_offset;
uint8_t access_size;
uint64_t address;
};
// fixed acpi description table
struct fadt {
struct acpi_header h;
uint32_t firmware_ctrl;
uint32_t dsdt;
// field used in ACPI 1.0; no longer in use, for compatibility only
uint8_t reserved;
uint8_t preferred_power_management_profile;
uint16_t sci_interrupt;
uint32_t smi_command_port;
uint8_t acpi_enable;
uint8_t acpi_disable;
uint8_t s4bios_req;
uint8_t pstate_control;
uint32_t pm1_a_event_block;
uint32_t pm1_b_event_block;
uint32_t pm1_a_control_block;
uint32_t pm1_b_control_block;
uint32_t pm2_control_block;
uint32_t pm_timer_block;
uint32_t gpe0_block;
uint32_t gpe1_block;
uint8_t pm1_event_length;
uint8_t pm1_control_length;
uint8_t pm2_control_length;
uint8_t pm_timer_length;
uint8_t gpe0_length;
uint8_t gpe1_length;
uint8_t gpe1_base;
uint8_t cstate_control;
uint16_t worst_c2_latency;
uint16_t worst_c3_latency;
uint16_t flush_size;
uint16_t flush_stride;
uint8_t duty_offset;
uint8_t duty_width;
uint8_t day_alarm;
uint8_t month_alarm;
uint8_t century;
// reserved in ACPI 1.0; used since ACPI 2.0+
uint16_t boot_architecture_flags;
uint8_t reserved_2;
uint32_t flags;
// 12 byte structure; see below for details
struct gas reset_reg;
uint8_t reset_value;
uint8_t reserved_3[3];
// 64bit pointers - Available on ACPI 2.0+
uint64_t x_firmware_control;
uint64_t x_dsdt;
struct gas x_pm1_a_event_block;
struct gas x_pm1_b_event_block;
struct gas x_pm1_a_control_block;
struct gas x_pm1_b_control_block;
struct gas x_pm2_control_block;
struct gas x_pm_timer_block;
struct gas x_gpe0_block;
struct gas x_gpe1_block;
};
struct acpi_state {
union {
struct xsdt *xsdt;
struct rsdt *rsdt;
} dst;
uint8_t version;
struct fadt fadt;
uint16_t SLP_TYPa;
uint16_t SLP_TYPb;
uint16_t SLP_EN;
uint16_t SCI_EN;
};
static bool checksum(uint8_t *data, size_t len) {
unsigned char sum = 0;
for (size_t i = 0; i < len; i++)
sum += data[i];
return sum == 0;
}
static int read_s5_addr(struct acpi_state *state) {
uintptr_t ptr = state->fadt.dsdt;
char *s5_addr = (void*) (ptr + 36);
int dsdt_len = *((int*) (ptr+1)) - 36;
while (0 < dsdt_len--) {
if ( memcmp(s5_addr, "_S5_", 4) == 0)
break;
s5_addr++;
}
if (dsdt_len > 0) {
// check for valid AML structure
if ( ( *(s5_addr-1) == 0x08 || ( *(s5_addr-2) == 0x08 && *(s5_addr-1) == '\\') ) && *(s5_addr+4) == 0x12 ) {
s5_addr += 5;
s5_addr += ((*s5_addr &0xC0)>>6) +2; // calculate PkgLength size
if (*s5_addr == 0x0A)
s5_addr++; // skip byteprefix
state->SLP_TYPa = *(s5_addr)<<10;
s5_addr++;
if (*s5_addr == 0x0A)
s5_addr++; // skip byteprefix
state->SLP_TYPb = *(s5_addr)<<10;
state->SLP_EN = 1<<13;
state->SCI_EN = 1;
} else {
return -1;
}
} else {
return -1;
}
return -1;
}
static void *acpi_find_table_rsdt(struct rsdt *rsdt, const char *identifier, int ident_len) {
int entries = (rsdt->h.length - sizeof(rsdt->h)) / 4;
for (int i = 0; i < entries; i++) {
struct acpi_header *h = (struct acpi_header *) (uintptr_t) rsdt->sdt_pointers[i];
char buf[6];
memcpy(buf, h->signature, 4);
buf[4] = '\n';
buf[5] = '\0';
serial_out_str(buf);
if (!strncmp(h->signature, identifier, ident_len))
return (void *)h;
}
// TABLE NOT FOUND
return NULL;
}
static void *acpi_find_table_xsdt(struct xsdt *xsdt, const char *identifier, int ident_len) {
int entries = (xsdt->h.length - sizeof(xsdt->h)) / 8;
for (int i = 0; i < entries; i++) {
struct acpi_header *h = (struct acpi_header *) (uintptr_t) xsdt->sdt_pointers[i];
if (!strncmp(h->signature, identifier, ident_len))
return (void *)h;
}
// TABLE NOT FOUND
return NULL;
}
int acpi_init_rsdt(struct rsdt *rsdt) {
rsdt = mmap(rsdt, sizeof(struct rsdt));
state.dst.rsdt = rsdt;
state.version = 0;
if (!checksum((uint8_t *) &rsdt->h, rsdt->h.length))
return -1;
struct fadt *fadt = acpi_find_table_rsdt(rsdt, "FACP", 4);
if (!fadt)
return -1;
if (!checksum((uint8_t *) &fadt->h, fadt->h.length))
return -1;
state.fadt = *fadt;
return -1;
}
int acpi_init_xsdt(struct xsdt *xsdt) {
xsdt = mmap(xsdt, sizeof(struct xsdt));
return -1;
state.dst.xsdt = xsdt;
state.version = 2;
if (!checksum((uint8_t *) &xsdt->h, xsdt->h.length))
return -1;
struct fadt *fadt = acpi_find_table_xsdt(xsdt, "FACP", 4);
if (!fadt)
return -1;
if (!checksum((uint8_t *) &fadt->h, fadt->h.length))
return -1;
state.fadt = *fadt;
return 0;
}
int acpi_init(void *rootsdp) {
struct rsdp *rsdp = (struct rsdp *) rootsdp;
if (!checksum((uint8_t *)rsdp, sizeof(struct rsdp)))
return -1;
int res;
if (rsdp->revision == 0) {
res = acpi_init_rsdt(
(struct rsdt *) (uintptr_t) rsdp->rsdt_addr
);
} else if (rsdp->revision == 2) {
struct xsdp *xsdp = (struct xsdp *) rsdp;
res = acpi_init_xsdt(
(struct xsdt *) (uintptr_t) xsdp->xsdt_addr
);
} else {
return -1;
}
if (res)
return res;
int ret = read_s5_addr(&state);
if (!ret)
return ret;
outb(state.fadt.smi_command_port,state.fadt.acpi_enable);
return 0;
}
int acpi_shutdown(void) {
outw((unsigned int) state.fadt.pm1_a_control_block, state.SLP_TYPb | state.SLP_EN);
return -1;
}
|