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