acpi.c revision 1.4 1 1.4 drochner /* $NetBSD: acpi.c,v 1.4 2008/02/13 18:59:18 drochner Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1998 Doug Rabson
5 1.1 christos * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki (at) FreeBSD.org>
6 1.3 joerg * Copyright (c) 2008 Joerg Sonnenberger <joerg (at) NetBSD.org>
7 1.1 christos * All rights reserved.
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 christos * SUCH DAMAGE.
29 1.1 christos *
30 1.1 christos * Id: acpi.c,v 1.4 2000/08/09 14:47:52 iwasaki Exp
31 1.1 christos * $FreeBSD: src/usr.sbin/acpi/acpidump/acpi.c,v 1.4 2001/10/22 17:25:25 iwasaki Exp $
32 1.1 christos */
33 1.1 christos #include <sys/cdefs.h>
34 1.4 drochner __RCSID("$NetBSD: acpi.c,v 1.4 2008/02/13 18:59:18 drochner Exp $");
35 1.1 christos
36 1.1 christos #include <sys/param.h>
37 1.1 christos #include <sys/stat.h>
38 1.1 christos
39 1.1 christos #include <assert.h>
40 1.1 christos #include <err.h>
41 1.1 christos #include <fcntl.h>
42 1.1 christos #include <stdio.h>
43 1.1 christos #include <unistd.h>
44 1.1 christos #include <string.h>
45 1.1 christos
46 1.1 christos #include <acpi_common.h>
47 1.1 christos #include "acpidump.h"
48 1.1 christos
49 1.1 christos #include "aml/aml_env.h"
50 1.1 christos #include "aml/aml_common.h"
51 1.1 christos #include "aml/aml_parse.h"
52 1.1 christos #include "aml/aml_region.h"
53 1.1 christos
54 1.1 christos #define BEGIN_COMMENT "/*\n"
55 1.1 christos #define END_COMMENT " */\n"
56 1.1 christos
57 1.1 christos struct ACPIsdt dsdt_header = {
58 1.1 christos .signature = "DSDT",
59 1.1 christos .rev = 1,
60 1.1 christos .oemid = "OEMID",
61 1.1 christos .oemtblid = "OEMTBLID",
62 1.1 christos .oemrev = 0x12345678,
63 1.1 christos .creator = "CRTR",
64 1.1 christos .crerev = 0x12345678,
65 1.1 christos };
66 1.1 christos
67 1.1 christos static void
68 1.1 christos acpi_trim_string(char *s, size_t length)
69 1.1 christos {
70 1.1 christos
71 1.1 christos /* Trim trailing spaces and NULLs */
72 1.1 christos while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
73 1.1 christos s[length-- - 1] = '\0';
74 1.1 christos }
75 1.1 christos
76 1.1 christos static void
77 1.1 christos acpi_print_dsdt_definition(void)
78 1.1 christos {
79 1.1 christos char oemid[6 + 1];
80 1.1 christos char oemtblid[8 + 1];
81 1.1 christos
82 1.1 christos acpi_trim_string((char *)dsdt_header.oemid, sizeof(oemid) - 1);
83 1.1 christos acpi_trim_string((char *)dsdt_header.oemtblid, sizeof(oemtblid) - 1);
84 1.1 christos (void)strlcpy(oemid, (const char *)dsdt_header.oemid, sizeof(oemid));
85 1.1 christos (void)strlcpy(oemtblid, (const char *)dsdt_header.oemtblid,
86 1.1 christos sizeof(oemtblid));
87 1.1 christos
88 1.3 joerg printf("DefinitionBlock (\"%s\", \"%s\", 0x%x, \"%s\", \"%s\", 0x%x)",
89 1.3 joerg "acpi_dst.aml", "DSDT", dsdt_header.rev, oemid, oemtblid,
90 1.3 joerg dsdt_header.oemrev);
91 1.1 christos }
92 1.1 christos
93 1.1 christos static void
94 1.1 christos acpi_print_string(const char *s, size_t length)
95 1.1 christos {
96 1.1 christos int c;
97 1.1 christos
98 1.1 christos /* Trim trailing spaces and NULLs */
99 1.1 christos while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
100 1.1 christos length--;
101 1.1 christos
102 1.1 christos while (length--) {
103 1.1 christos c = *s++;
104 1.1 christos putchar(c);
105 1.1 christos }
106 1.1 christos }
107 1.1 christos
108 1.1 christos static void
109 1.1 christos acpi_handle_dsdt(struct ACPIsdt *dsdp)
110 1.1 christos {
111 1.1 christos u_int8_t *dp;
112 1.1 christos u_int8_t *end;
113 1.1 christos
114 1.1 christos acpi_print_dsdt(dsdp);
115 1.1 christos dp = (u_int8_t *)dsdp->body;
116 1.1 christos end = (u_int8_t *)dsdp + dsdp->len;
117 1.1 christos
118 1.1 christos acpi_dump_dsdt(dp, end);
119 1.1 christos }
120 1.1 christos
121 1.1 christos static void
122 1.1 christos acpi_handle_facp(struct FACPbody *facp)
123 1.1 christos {
124 1.1 christos struct ACPIsdt *dsdp;
125 1.1 christos
126 1.1 christos acpi_print_facp(facp);
127 1.1 christos dsdp = (struct ACPIsdt *) acpi_map_sdt(facp->dsdt_ptr);
128 1.1 christos if (acpi_checksum(dsdp, dsdp->len))
129 1.4 drochner errx(1, "DSDT is corrupt");
130 1.1 christos acpi_handle_dsdt(dsdp);
131 1.1 christos aml_dump(dsdp);
132 1.1 christos }
133 1.1 christos
134 1.1 christos static void
135 1.1 christos init_namespace(void)
136 1.1 christos {
137 1.1 christos struct aml_environ env;
138 1.1 christos struct aml_name *newname;
139 1.1 christos
140 1.2 dogcow aml_new_name_group((void *)AML_NAME_GROUP_OS_DEFINED);
141 1.1 christos env.curname = aml_get_rootname();
142 1.1 christos newname = aml_create_name(&env, (const unsigned char *)"\\_OS_");
143 1.1 christos newname->property = aml_alloc_object(aml_t_string, NULL);
144 1.1 christos newname->property->str.needfree = 0;
145 1.1 christos newname->property->str.string = __UNCONST("Microsoft Windows NT");
146 1.3 joerg
147 1.3 joerg newname = aml_create_name(&env, (const unsigned char *)"\\_OSI");
148 1.3 joerg newname->property = aml_alloc_object(aml_t_method, NULL);
149 1.3 joerg newname->property->meth.argnum = 1;
150 1.1 christos }
151 1.1 christos
152 1.1 christos /*
153 1.1 christos * Public interfaces
154 1.1 christos */
155 1.1 christos
156 1.1 christos void
157 1.1 christos acpi_dump_dsdt(u_int8_t *dp, u_int8_t *end)
158 1.1 christos {
159 1.1 christos extern struct aml_environ asl_env;
160 1.1 christos
161 1.1 christos acpi_print_dsdt_definition();
162 1.1 christos
163 1.1 christos /* 1st stage: parse only w/o printing */
164 1.1 christos init_namespace();
165 1.2 dogcow aml_new_name_group(dp);
166 1.1 christos bzero(&asl_env, sizeof(asl_env));
167 1.1 christos
168 1.1 christos asl_env.dp = dp;
169 1.1 christos asl_env.end = end;
170 1.1 christos asl_env.curname = aml_get_rootname();
171 1.1 christos
172 1.1 christos aml_local_stack_push(aml_local_stack_create());
173 1.1 christos aml_parse_objectlist(&asl_env, 0);
174 1.1 christos aml_local_stack_delete(aml_local_stack_pop());
175 1.1 christos
176 1.1 christos assert(asl_env.dp == asl_env.end);
177 1.1 christos asl_env.dp = dp;
178 1.1 christos
179 1.1 christos /* 2nd stage: dump whole object list */
180 1.1 christos printf("\n{\n");
181 1.1 christos asl_dump_objectlist(&dp, end, 0);
182 1.1 christos printf("\n}\n");
183 1.1 christos assert(dp == end);
184 1.1 christos }
185 1.1 christos void
186 1.1 christos acpi_print_sdt(struct ACPIsdt *sdp)
187 1.1 christos {
188 1.1 christos
189 1.1 christos printf(BEGIN_COMMENT);
190 1.1 christos acpi_print_string((const char *)sdp->signature, 4);
191 1.1 christos printf(": Length=%d, Revision=%d, Checksum=%d,\n",
192 1.1 christos sdp->len, sdp->rev, sdp->check);
193 1.1 christos printf("\tOEMID=");
194 1.1 christos acpi_print_string((const char *)sdp->oemid, 6);
195 1.1 christos printf(", OEM Table ID=");
196 1.1 christos acpi_print_string((const char *)sdp->oemtblid, 8);
197 1.1 christos printf(", OEM Revision=0x%x,\n", sdp->oemrev);
198 1.1 christos printf("\tCreator ID=");
199 1.1 christos acpi_print_string((const char *)sdp->creator, 4);
200 1.1 christos printf(", Creator Revision=0x%x\n", sdp->crerev);
201 1.1 christos printf(END_COMMENT);
202 1.1 christos if (!memcmp(sdp->signature, "DSDT", 4)) {
203 1.1 christos memcpy(&dsdt_header, sdp, sizeof(dsdt_header));
204 1.1 christos }
205 1.1 christos }
206 1.1 christos
207 1.1 christos void
208 1.1 christos acpi_print_rsdt(struct ACPIsdt *rsdp)
209 1.1 christos {
210 1.1 christos int i, entries;
211 1.1 christos
212 1.1 christos acpi_print_sdt(rsdp);
213 1.1 christos entries = (rsdp->len - SIZEOF_SDT_HDR) / sizeof(u_int32_t);
214 1.1 christos printf(BEGIN_COMMENT);
215 1.1 christos printf("\tEntries={ ");
216 1.1 christos for (i = 0; i < entries; i++) {
217 1.1 christos if (i > 0)
218 1.1 christos printf(", ");
219 1.1 christos printf("0x%08x", rsdp->body[i]);
220 1.1 christos }
221 1.1 christos printf(" }\n");
222 1.1 christos printf(END_COMMENT);
223 1.1 christos }
224 1.1 christos
225 1.1 christos void
226 1.1 christos acpi_print_facp(struct FACPbody *facp)
227 1.1 christos {
228 1.1 christos char sep;
229 1.1 christos
230 1.1 christos printf(BEGIN_COMMENT);
231 1.1 christos printf("\tDSDT=0x%x\n", facp->dsdt_ptr);
232 1.1 christos printf("\tINT_MODEL=%s\n", facp->int_model ? "APIC" : "PIC");
233 1.1 christos printf("\tSCI_INT=%d\n", facp->sci_int);
234 1.1 christos printf("\tSMI_CMD=0x%x, ", facp->smi_cmd);
235 1.1 christos printf("ACPI_ENABLE=0x%x, ", facp->acpi_enable);
236 1.1 christos printf("ACPI_DISABLE=0x%x, ", facp->acpi_disable);
237 1.1 christos printf("S4BIOS_REQ=0x%x\n", facp->s4biosreq);
238 1.1 christos if (facp->pm1a_evt_blk)
239 1.1 christos printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
240 1.1 christos facp->pm1a_evt_blk,
241 1.1 christos facp->pm1a_evt_blk + facp->pm1_evt_len - 1);
242 1.1 christos if (facp->pm1b_evt_blk)
243 1.1 christos printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
244 1.1 christos facp->pm1b_evt_blk,
245 1.1 christos facp->pm1b_evt_blk + facp->pm1_evt_len - 1);
246 1.1 christos if (facp->pm1a_cnt_blk)
247 1.1 christos printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
248 1.1 christos facp->pm1a_cnt_blk,
249 1.1 christos facp->pm1a_cnt_blk + facp->pm1_cnt_len - 1);
250 1.1 christos if (facp->pm1b_cnt_blk)
251 1.1 christos printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
252 1.1 christos facp->pm1b_cnt_blk,
253 1.1 christos facp->pm1b_cnt_blk + facp->pm1_cnt_len - 1);
254 1.1 christos if (facp->pm2_cnt_blk)
255 1.1 christos printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
256 1.1 christos facp->pm2_cnt_blk,
257 1.1 christos facp->pm2_cnt_blk + facp->pm2_cnt_len - 1);
258 1.1 christos if (facp->pm_tmr_blk)
259 1.1 christos printf("\tPM2_TMR_BLK=0x%x-0x%x\n",
260 1.1 christos facp->pm_tmr_blk,
261 1.1 christos facp->pm_tmr_blk + facp->pm_tmr_len - 1);
262 1.1 christos if (facp->gpe0_blk)
263 1.1 christos printf("\tPM2_GPE0_BLK=0x%x-0x%x\n",
264 1.1 christos facp->gpe0_blk,
265 1.1 christos facp->gpe0_blk + facp->gpe0_len - 1);
266 1.1 christos if (facp->gpe1_blk)
267 1.1 christos printf("\tPM2_GPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
268 1.1 christos facp->gpe1_blk,
269 1.1 christos facp->gpe1_blk + facp->gpe1_len - 1,
270 1.1 christos facp->gpe1_base);
271 1.1 christos printf("\tP_LVL2_LAT=%dms, P_LVL3_LAT=%dms\n",
272 1.1 christos facp->p_lvl2_lat, facp->p_lvl3_lat);
273 1.1 christos printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
274 1.1 christos facp->flush_size, facp->flush_stride);
275 1.1 christos printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
276 1.1 christos facp->duty_off, facp->duty_width);
277 1.1 christos printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
278 1.1 christos facp->day_alrm, facp->mon_alrm, facp->century);
279 1.1 christos printf("\tFlags=");
280 1.1 christos sep = '{';
281 1.1 christos
282 1.1 christos #define PRINTFLAG(xx) do { \
283 1.1 christos if (facp->flags & ACPI_FACP_FLAG_## xx) { \
284 1.1 christos printf("%c%s", sep, #xx); sep = ','; \
285 1.1 christos } \
286 1.1 christos } while (0)
287 1.1 christos
288 1.1 christos PRINTFLAG(WBINVD);
289 1.1 christos PRINTFLAG(WBINVD_FLUSH);
290 1.1 christos PRINTFLAG(PROC_C1);
291 1.1 christos PRINTFLAG(P_LVL2_UP);
292 1.1 christos PRINTFLAG(PWR_BUTTON);
293 1.1 christos PRINTFLAG(SLP_BUTTON);
294 1.1 christos PRINTFLAG(FIX_RTC);
295 1.1 christos PRINTFLAG(RTC_S4);
296 1.1 christos PRINTFLAG(TMR_VAL_EXT);
297 1.1 christos PRINTFLAG(DCK_CAP);
298 1.1 christos
299 1.1 christos #undef PRINTFLAG
300 1.1 christos
301 1.1 christos printf("}\n");
302 1.1 christos printf(END_COMMENT);
303 1.1 christos }
304 1.1 christos
305 1.1 christos void
306 1.1 christos acpi_print_dsdt(struct ACPIsdt *dsdp)
307 1.1 christos {
308 1.1 christos
309 1.1 christos acpi_print_sdt(dsdp);
310 1.1 christos }
311 1.1 christos
312 1.1 christos int
313 1.1 christos acpi_checksum(void *p, size_t length)
314 1.1 christos {
315 1.1 christos u_int8_t *bp;
316 1.1 christos u_int8_t sum;
317 1.1 christos
318 1.1 christos bp = p;
319 1.1 christos sum = 0;
320 1.1 christos while (length--)
321 1.1 christos sum += *bp++;
322 1.1 christos
323 1.1 christos return (sum);
324 1.1 christos }
325 1.1 christos
326 1.1 christos struct ACPIsdt *
327 1.1 christos acpi_map_sdt(vm_offset_t pa)
328 1.1 christos {
329 1.1 christos struct ACPIsdt *sp;
330 1.1 christos
331 1.1 christos sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
332 1.1 christos sp = acpi_map_physical(pa, sp->len);
333 1.1 christos return (sp);
334 1.1 christos }
335 1.1 christos
336 1.1 christos void
337 1.1 christos acpi_print_rsd_ptr(struct ACPIrsdp *rp)
338 1.1 christos {
339 1.1 christos
340 1.1 christos printf(BEGIN_COMMENT);
341 1.1 christos printf("RSD PTR: Checksum=%d, OEMID=", rp->sum);
342 1.1 christos acpi_print_string((const char *)rp->oem, 6);
343 1.1 christos printf(", RsdtAddress=0x%08x\n", rp->addr);
344 1.1 christos printf(END_COMMENT);
345 1.1 christos }
346 1.1 christos
347 1.1 christos void
348 1.1 christos acpi_handle_rsdt(struct ACPIsdt *rsdp)
349 1.1 christos {
350 1.1 christos int i;
351 1.1 christos int entries;
352 1.1 christos struct ACPIsdt *sdp;
353 1.1 christos
354 1.1 christos entries = (rsdp->len - SIZEOF_SDT_HDR) / sizeof(u_int32_t);
355 1.1 christos acpi_print_rsdt(rsdp);
356 1.1 christos for (i = 0; i < entries; i++) {
357 1.1 christos sdp = (struct ACPIsdt *) acpi_map_sdt(rsdp->body[i]);
358 1.4 drochner if (acpi_checksum(sdp, sdp->len)) {
359 1.4 drochner warnx("RSDT entry %d: bad checksum", i);
360 1.4 drochner continue;
361 1.4 drochner }
362 1.1 christos if (!memcmp(sdp->signature, "FACP", 4)) {
363 1.1 christos acpi_handle_facp((struct FACPbody *) sdp->body);
364 1.1 christos } else {
365 1.1 christos acpi_print_sdt(sdp);
366 1.1 christos }
367 1.1 christos }
368 1.1 christos }
369 1.1 christos
370 1.1 christos /*
371 1.1 christos * Dummy functions
372 1.1 christos */
373 1.1 christos
374 1.1 christos void
375 1.1 christos aml_dbgr(struct aml_environ *env1, struct aml_environ *env2)
376 1.1 christos {
377 1.1 christos /* do nothing */
378 1.1 christos }
379 1.1 christos
380 1.1 christos int
381 1.1 christos aml_region_read_simple(struct aml_region_handle *h, vm_offset_t offset,
382 1.1 christos u_int32_t *valuep)
383 1.1 christos {
384 1.1 christos return (0);
385 1.1 christos }
386 1.1 christos
387 1.1 christos int
388 1.1 christos aml_region_write_simple(struct aml_region_handle *h, vm_offset_t offset,
389 1.1 christos u_int32_t value)
390 1.1 christos {
391 1.1 christos return (0);
392 1.1 christos }
393 1.1 christos
394 1.1 christos u_int32_t
395 1.1 christos aml_region_prompt_read(struct aml_region_handle *h, u_int32_t value)
396 1.1 christos {
397 1.1 christos return (0);
398 1.1 christos }
399 1.1 christos
400 1.1 christos u_int32_t
401 1.1 christos aml_region_prompt_write(struct aml_region_handle *h, u_int32_t value)
402 1.1 christos {
403 1.1 christos return (0);
404 1.1 christos }
405 1.1 christos
406 1.1 christos int
407 1.1 christos aml_region_prompt_update_value(u_int32_t orgval, u_int32_t value,
408 1.1 christos struct aml_region_handle *h)
409 1.1 christos {
410 1.1 christos return (0);
411 1.1 christos }
412 1.1 christos
413 1.1 christos u_int32_t
414 1.1 christos aml_region_read(struct aml_environ *env, int regtype, u_int32_t flags,
415 1.1 christos u_int32_t addr, u_int32_t bitoffset, u_int32_t bitlen)
416 1.1 christos {
417 1.1 christos return (0);
418 1.1 christos }
419 1.1 christos
420 1.1 christos int
421 1.1 christos aml_region_write(struct aml_environ *env, int regtype, u_int32_t flags,
422 1.1 christos u_int32_t value, u_int32_t addr, u_int32_t bitoffset, u_int32_t bitlen)
423 1.1 christos {
424 1.1 christos return (0);
425 1.1 christos }
426 1.1 christos
427 1.1 christos int
428 1.1 christos aml_region_write_from_buffer(struct aml_environ *env, int regtype,
429 1.1 christos u_int32_t flags, u_int8_t *buffer, u_int32_t addr, u_int32_t bitoffset,
430 1.1 christos u_int32_t bitlen)
431 1.1 christos {
432 1.1 christos return (0);
433 1.1 christos }
434 1.1 christos
435 1.1 christos int
436 1.1 christos aml_region_bcopy(struct aml_environ *env, int regtype, u_int32_t flags,
437 1.1 christos u_int32_t addr, u_int32_t bitoffset, u_int32_t bitlen,
438 1.1 christos u_int32_t dflags, u_int32_t daddr,
439 1.1 christos u_int32_t dbitoffset, u_int32_t dbitlen)
440 1.1 christos {
441 1.1 christos return (0);
442 1.1 christos }
443 1.1 christos
444 1.1 christos int
445 1.1 christos aml_region_read_into_buffer(struct aml_environ *env, int regtype,
446 1.1 christos u_int32_t flags, u_int32_t addr, u_int32_t bitoffset,
447 1.1 christos u_int32_t bitlen, u_int8_t *buffer)
448 1.1 christos {
449 1.1 christos return (0);
450 1.1 christos }
451