- Extract RX sensitivity calibration capability from EEPROM
[dragonfly.git] / usr.sbin / acpi / acpidump / acpi.c
blobc35914d1e948974c425c6120694543cc350d6537
1 /*-
2 * Copyright (c) 1998 Doug Rabson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $FreeBSD: src/usr.sbin/acpi/acpidump/acpi.c,v 1.31 2005/02/14 11:21:48 scottl Exp $
28 * $DragonFly: src/usr.sbin/acpi/acpidump/acpi.c,v 1.6 2007/01/10 12:23:23 hsu Exp $
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <assert.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
42 #include "acpidump.h"
44 #define BEGIN_COMMENT "/*\n"
45 #define END_COMMENT " */\n"
47 static void acpi_print_string(char *s, size_t length);
48 static void acpi_print_gas(struct ACPIgas *gas);
49 static int acpi_get_fadt_revision(struct FADTbody *fadt);
50 static void acpi_handle_fadt(struct ACPIsdt *fadt);
51 static void acpi_print_cpu(u_char cpu_id);
52 static void acpi_print_local_apic(u_char cpu_id, u_char apic_id,
53 u_int32_t flags);
54 static void acpi_print_io_apic(u_char apic_id, u_int32_t int_base,
55 u_int64_t apic_addr);
56 static void acpi_print_mps_flags(u_int16_t flags);
57 static void acpi_print_intr(u_int32_t intr, u_int16_t mps_flags);
58 static void acpi_print_apic(struct MADT_APIC *mp);
59 static void acpi_handle_apic(struct ACPIsdt *sdp);
60 static void acpi_handle_hpet(struct ACPIsdt *sdp);
61 static void acpi_print_sdt(struct ACPIsdt *sdp);
62 static void acpi_print_fadt(struct ACPIsdt *sdp);
63 static void acpi_print_facs(struct FACSbody *facs);
64 static void acpi_print_dsdt(struct ACPIsdt *dsdp);
65 static struct ACPIsdt *acpi_map_sdt(vm_offset_t pa);
66 static void acpi_print_rsd_ptr(struct ACPIrsdp *rp);
67 static void acpi_handle_rsdt(struct ACPIsdt *rsdp);
69 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
70 static int addr_size;
72 static void
73 acpi_print_string(char *s, size_t length)
75 int c;
77 /* Trim trailing spaces and NULLs */
78 while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
79 length--;
81 while (length--) {
82 c = *s++;
83 putchar(c);
87 static void
88 acpi_print_gas(struct ACPIgas *gas)
90 switch(gas->address_space_id) {
91 case ACPI_GAS_MEMORY:
92 printf("0x%08lx:%u[%u] (Memory)", (u_long)gas->address,
93 gas->bit_offset, gas->bit_width);
94 break;
95 case ACPI_GAS_IO:
96 printf("0x%02lx:%u[%u] (IO)", (u_long)gas->address,
97 gas->bit_offset, gas->bit_width);
98 break;
99 case ACPI_GAS_PCI:
100 printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->address >> 32),
101 (uint16_t)((gas->address >> 16) & 0xffff),
102 (uint16_t)gas->address);
103 break;
104 /* XXX How to handle these below? */
105 case ACPI_GAS_EMBEDDED:
106 printf("0x%x:%u[%u] (EC)", (uint16_t)gas->address,
107 gas->bit_offset, gas->bit_width);
108 break;
109 case ACPI_GAS_SMBUS:
110 printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->address,
111 gas->bit_offset, gas->bit_width);
112 break;
113 case ACPI_GAS_FIXED:
114 default:
115 printf("0x%08lx (?)", (u_long)gas->address);
116 break;
120 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
121 static int
122 acpi_get_fadt_revision(struct FADTbody *fadt)
124 int fadt_revision;
126 /* Set the FADT revision separately from the RSDP version. */
127 if (addr_size == 8) {
128 fadt_revision = 2;
131 * A few systems (e.g., IBM T23) have an RSDP that claims
132 * revision 2 but the 64 bit addresses are invalid. If
133 * revision 2 and the 32 bit address is non-zero but the
134 * 32 and 64 bit versions don't match, prefer the 32 bit
135 * version for all subsequent tables.
137 if (fadt->facs_ptr != 0 &&
138 (fadt->x_facs_ptr & 0xffffffff) != fadt->facs_ptr)
139 fadt_revision = 1;
140 } else
141 fadt_revision = 1;
142 return (fadt_revision);
145 static void
146 acpi_handle_fadt(struct ACPIsdt *sdp)
148 struct ACPIsdt *dsdp;
149 struct FACSbody *facs;
150 struct FADTbody *fadt;
151 int fadt_revision;
153 fadt = (struct FADTbody *)sdp->body;
154 acpi_print_fadt(sdp);
156 fadt_revision = acpi_get_fadt_revision(fadt);
157 if (fadt_revision == 1)
158 facs = (struct FACSbody *)acpi_map_sdt(fadt->facs_ptr);
159 else
160 facs = (struct FACSbody *)acpi_map_sdt(fadt->x_facs_ptr);
161 if (memcmp(facs->signature, "FACS", 4) != 0 || facs->len < 64)
162 errx(1, "FACS is corrupt");
163 acpi_print_facs(facs);
165 if (fadt_revision == 1)
166 dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
167 else
168 dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
169 if (acpi_checksum(dsdp, dsdp->len))
170 errx(1, "DSDT is corrupt");
171 acpi_print_dsdt(dsdp);
174 static void
175 acpi_print_cpu(u_char cpu_id)
178 printf("\tACPI CPU=");
179 if (cpu_id == 0xff)
180 printf("ALL\n");
181 else
182 printf("%d\n", (u_int)cpu_id);
185 static void
186 acpi_print_local_apic(u_char cpu_id, u_char apic_id, u_int32_t flags)
188 acpi_print_cpu(cpu_id);
189 printf("\tFlags={");
190 if (flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
191 printf("ENABLED");
192 else
193 printf("DISABLED");
194 printf("}\n");
195 printf("\tAPIC ID=%d\n", (u_int)apic_id);
198 static void
199 acpi_print_io_apic(u_char apic_id, u_int32_t int_base, u_int64_t apic_addr)
201 printf("\tAPIC ID=%d\n", (u_int)apic_id);
202 printf("\tINT BASE=%d\n", int_base);
203 printf("\tADDR=0x%016jx\n", apic_addr);
206 static void
207 acpi_print_mps_flags(u_int16_t flags)
210 printf("\tFlags={Polarity=");
211 switch (flags & MPS_INT_FLAG_POLARITY_MASK) {
212 case MPS_INT_FLAG_POLARITY_CONFORM:
213 printf("conforming");
214 break;
215 case MPS_INT_FLAG_POLARITY_HIGH:
216 printf("active-hi");
217 break;
218 case MPS_INT_FLAG_POLARITY_LOW:
219 printf("active-lo");
220 break;
221 default:
222 printf("0x%x", flags & MPS_INT_FLAG_POLARITY_MASK);
223 break;
225 printf(", Trigger=");
226 switch (flags & MPS_INT_FLAG_TRIGGER_MASK) {
227 case MPS_INT_FLAG_TRIGGER_CONFORM:
228 printf("conforming");
229 break;
230 case MPS_INT_FLAG_TRIGGER_EDGE:
231 printf("edge");
232 break;
233 case MPS_INT_FLAG_TRIGGER_LEVEL:
234 printf("level");
235 break;
236 default:
237 printf("0x%x", (flags & MPS_INT_FLAG_TRIGGER_MASK) >> 2);
239 printf("}\n");
242 static void
243 acpi_print_intr(u_int32_t intr, u_int16_t mps_flags)
246 printf("\tINTR=%d\n", (u_int)intr);
247 acpi_print_mps_flags(mps_flags);
250 const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
251 "Local NMI", "Local APIC Override", "IO SAPIC",
252 "Local SAPIC", "Platform Interrupt" };
253 const char *platform_int_types[] = { "PMI", "INIT",
254 "Corrected Platform Error" };
256 static void
257 acpi_print_apic(struct MADT_APIC *mp)
260 printf("\tType=%s\n", apic_types[mp->type]);
261 switch (mp->type) {
262 case ACPI_MADT_APIC_TYPE_LOCAL_APIC:
263 acpi_print_local_apic(mp->body.local_apic.cpu_id,
264 mp->body.local_apic.apic_id, mp->body.local_apic.flags);
265 break;
266 case ACPI_MADT_APIC_TYPE_IO_APIC:
267 acpi_print_io_apic(mp->body.io_apic.apic_id,
268 mp->body.io_apic.int_base,
269 mp->body.io_apic.apic_addr);
270 break;
271 case ACPI_MADT_APIC_TYPE_INT_OVERRIDE:
272 printf("\tBUS=%d\n", (u_int)mp->body.int_override.bus);
273 printf("\tIRQ=%d\n", (u_int)mp->body.int_override.source);
274 acpi_print_intr(mp->body.int_override.intr,
275 mp->body.int_override.mps_flags);
276 break;
277 case ACPI_MADT_APIC_TYPE_NMI:
278 acpi_print_intr(mp->body.nmi.intr, mp->body.nmi.mps_flags);
279 break;
280 case ACPI_MADT_APIC_TYPE_LOCAL_NMI:
281 acpi_print_cpu(mp->body.local_nmi.cpu_id);
282 printf("\tLINT Pin=%d\n", mp->body.local_nmi.lintpin);
283 acpi_print_mps_flags(mp->body.local_nmi.mps_flags);
284 break;
285 case ACPI_MADT_APIC_TYPE_LOCAL_OVERRIDE:
286 printf("\tLocal APIC ADDR=0x%016jx\n",
287 mp->body.local_apic_override.apic_addr);
288 break;
289 case ACPI_MADT_APIC_TYPE_IO_SAPIC:
290 acpi_print_io_apic(mp->body.io_sapic.apic_id,
291 mp->body.io_sapic.int_base,
292 mp->body.io_sapic.apic_addr);
293 break;
294 case ACPI_MADT_APIC_TYPE_LOCAL_SAPIC:
295 acpi_print_local_apic(mp->body.local_sapic.cpu_id,
296 mp->body.local_sapic.apic_id, mp->body.local_sapic.flags);
297 printf("\tAPIC EID=%d\n", (u_int)mp->body.local_sapic.apic_eid);
298 break;
299 case ACPI_MADT_APIC_TYPE_INT_SRC:
300 printf("\tType=%s\n",
301 platform_int_types[mp->body.int_src.type]);
302 printf("\tCPU ID=%d\n", (u_int)mp->body.int_src.cpu_id);
303 printf("\tCPU EID=%d\n", (u_int)mp->body.int_src.cpu_id);
304 printf("\tSAPIC Vector=%d\n",
305 (u_int)mp->body.int_src.sapic_vector);
306 acpi_print_intr(mp->body.int_src.intr,
307 mp->body.int_src.mps_flags);
308 break;
309 default:
310 printf("\tUnknown type %d\n", (u_int)mp->type);
311 break;
315 static void
316 acpi_handle_apic(struct ACPIsdt *sdp)
318 struct MADTbody *madtp;
319 struct MADT_APIC *madt_apicp;
321 printf(BEGIN_COMMENT);
322 acpi_print_sdt(sdp);
323 madtp = (struct MADTbody *) sdp->body;
324 printf("\tLocal APIC ADDR=0x%08x\n", madtp->lapic_addr);
325 printf("\tFlags={");
326 if (madtp->flags & ACPI_APIC_FLAG_PCAT_COMPAT)
327 printf("PC-AT");
328 printf("}\n");
329 madt_apicp = (struct MADT_APIC *)madtp->body;
330 while (((uintptr_t)madt_apicp) - ((uintptr_t)sdp) < sdp->len) {
331 printf("\n");
332 acpi_print_apic(madt_apicp);
333 madt_apicp = (struct MADT_APIC *) ((char *)madt_apicp +
334 madt_apicp->len);
336 printf(END_COMMENT);
339 static void
340 acpi_handle_hpet(struct ACPIsdt *sdp)
342 struct HPETbody *hpetp;
344 printf(BEGIN_COMMENT);
345 acpi_print_sdt(sdp);
346 hpetp = (struct HPETbody *) sdp->body;
347 printf("\tHPET Number=%d\n", hpetp->hpet_number);
348 printf("\tADDR=0x%08x\n", hpetp->base_addr);
349 printf("\tHW Rev=0x%x\n", hpetp->block_hwrev);
350 printf("\tComparitors=%d\n", hpetp->block_comparitors);
351 printf("\tCounter Size=%d\n", hpetp->block_counter_size);
352 printf("\tLegacy IRQ routing capable={");
353 if (hpetp->block_legacy_capable)
354 printf("TRUE}\n");
355 else
356 printf("FALSE}\n");
357 printf("\tPCI Vendor ID=0x%04x\n", hpetp->block_pcivendor);
358 printf("\tMinimal Tick=%d\n", hpetp->clock_tick);
359 printf(END_COMMENT);
362 static void
363 acpi_handle_ecdt(struct ACPIsdt *sdp)
365 struct ECDTbody *ecdt;
367 printf(BEGIN_COMMENT);
368 acpi_print_sdt(sdp);
369 ecdt = (struct ECDTbody *) sdp->body;
370 printf("\tEC_CONTROL=");
371 acpi_print_gas(&ecdt->ec_control);
372 printf("\n\tEC_DATA=");
373 acpi_print_gas(&ecdt->ec_data);
374 printf("\n\tUID=%#x, ", ecdt->uid);
375 printf("GPE_BIT=%#x\n", ecdt->gpe_bit);
376 printf("\tEC_ID=%s\n", ecdt->ec_id);
377 printf(END_COMMENT);
380 static void
381 acpi_handle_mcfg(struct ACPIsdt *sdp)
383 struct MCFGbody *mcfg;
384 u_int i, e;
386 printf(BEGIN_COMMENT);
387 acpi_print_sdt(sdp);
388 mcfg = (struct MCFGbody *) sdp->body;
390 e = (sdp->len - ((caddr_t)&mcfg->s[0] - (caddr_t)sdp)) /
391 sizeof(*mcfg->s);
392 for (i = 0; i < e; i++, mcfg++) {
393 printf("\n");
394 printf("\tBase Address= 0x%016jx\n", mcfg->s[i].baseaddr);
395 printf("\tSegment Group= 0x%04x\n", mcfg->s[i].seg_grp);
396 printf("\tStart Bus= %d\n", mcfg->s[i].start);
397 printf("\tEnd Bus= %d\n", mcfg->s[i].end);
399 printf(END_COMMENT);
402 static void
403 acpi_print_sdt(struct ACPIsdt *sdp)
405 printf(" ");
406 acpi_print_string(sdp->signature, 4);
407 printf(": Length=%d, Revision=%d, Checksum=%d,\n",
408 sdp->len, sdp->rev, sdp->check);
409 printf("\tOEMID=");
410 acpi_print_string(sdp->oemid, 6);
411 printf(", OEM Table ID=");
412 acpi_print_string(sdp->oemtblid, 8);
413 printf(", OEM Revision=0x%x,\n", sdp->oemrev);
414 printf("\tCreator ID=");
415 acpi_print_string(sdp->creator, 4);
416 printf(", Creator Revision=0x%x\n", sdp->crerev);
419 static void
420 acpi_print_rsdt(struct ACPIsdt *rsdp)
422 int i, entries;
423 u_long addr;
425 printf(BEGIN_COMMENT);
426 acpi_print_sdt(rsdp);
427 entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
428 printf("\tEntries={ ");
429 for (i = 0; i < entries; i++) {
430 if (i > 0)
431 printf(", ");
432 switch (addr_size) {
433 case 4:
434 addr = le32dec((char*)rsdp->body + i * addr_size);
435 break;
436 case 8:
437 addr = le64dec((char*)rsdp->body + i * addr_size);
438 break;
439 default:
440 addr = 0;
442 assert(addr != 0);
443 printf("0x%08lx", addr);
445 printf(" }\n");
446 printf(END_COMMENT);
449 static const char *acpi_pm_profiles[] = {
450 "Unspecified", "Desktop", "Mobile", "Workstation",
451 "Enterprise Server", "SOHO Server", "Appliance PC"
454 static void
455 acpi_print_fadt(struct ACPIsdt *sdp)
457 struct FADTbody *fadt;
458 const char *pm;
459 char sep;
461 fadt = (struct FADTbody *)sdp->body;
462 printf(BEGIN_COMMENT);
463 acpi_print_sdt(sdp);
464 printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->facs_ptr,
465 fadt->dsdt_ptr);
466 printf("\tINT_MODEL=%s\n", fadt->int_model ? "APIC" : "PIC");
467 if (fadt->pm_profile >= sizeof(acpi_pm_profiles) / sizeof(char *))
468 pm = "Reserved";
469 else
470 pm = acpi_pm_profiles[fadt->pm_profile];
471 printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->pm_profile);
472 printf("\tSCI_INT=%d\n", fadt->sci_int);
473 printf("\tSMI_CMD=0x%x, ", fadt->smi_cmd);
474 printf("ACPI_ENABLE=0x%x, ", fadt->acpi_enable);
475 printf("ACPI_DISABLE=0x%x, ", fadt->acpi_disable);
476 printf("S4BIOS_REQ=0x%x\n", fadt->s4biosreq);
477 printf("\tPSTATE_CNT=0x%x\n", fadt->pstate_cnt);
478 printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
479 fadt->pm1a_evt_blk,
480 fadt->pm1a_evt_blk + fadt->pm1_evt_len - 1);
481 if (fadt->pm1b_evt_blk != 0)
482 printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
483 fadt->pm1b_evt_blk,
484 fadt->pm1b_evt_blk + fadt->pm1_evt_len - 1);
485 printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
486 fadt->pm1a_cnt_blk,
487 fadt->pm1a_cnt_blk + fadt->pm1_cnt_len - 1);
488 if (fadt->pm1b_cnt_blk != 0)
489 printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
490 fadt->pm1b_cnt_blk,
491 fadt->pm1b_cnt_blk + fadt->pm1_cnt_len - 1);
492 if (fadt->pm2_cnt_blk != 0)
493 printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
494 fadt->pm2_cnt_blk,
495 fadt->pm2_cnt_blk + fadt->pm2_cnt_len - 1);
496 printf("\tPM_TMR_BLK=0x%x-0x%x\n",
497 fadt->pm_tmr_blk,
498 fadt->pm_tmr_blk + fadt->pm_tmr_len - 1);
499 if (fadt->gpe0_blk != 0)
500 printf("\tGPE0_BLK=0x%x-0x%x\n",
501 fadt->gpe0_blk,
502 fadt->gpe0_blk + fadt->gpe0_len - 1);
503 if (fadt->gpe1_blk != 0)
504 printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
505 fadt->gpe1_blk,
506 fadt->gpe1_blk + fadt->gpe1_len - 1,
507 fadt->gpe1_base);
508 if (fadt->cst_cnt != 0)
509 printf("\tCST_CNT=0x%x\n", fadt->cst_cnt);
510 printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
511 fadt->p_lvl2_lat, fadt->p_lvl3_lat);
512 printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
513 fadt->flush_size, fadt->flush_stride);
514 printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
515 fadt->duty_off, fadt->duty_width);
516 printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
517 fadt->day_alrm, fadt->mon_alrm, fadt->century);
519 #define PRINTFLAG(var, flag) do { \
520 if ((var) & FADT_FLAG_## flag) { \
521 printf("%c%s", sep, #flag); sep = ','; \
523 } while (0)
525 printf("\tIAPC_BOOT_ARCH=");
526 sep = '{';
527 PRINTFLAG(fadt->iapc_boot_arch, LEGACY_DEV);
528 PRINTFLAG(fadt->iapc_boot_arch, 8042);
529 if (fadt->iapc_boot_arch != 0)
530 printf("}");
531 printf("\n");
533 printf("\tFlags=");
534 sep = '{';
535 PRINTFLAG(fadt->flags, WBINVD);
536 PRINTFLAG(fadt->flags, WBINVD_FLUSH);
537 PRINTFLAG(fadt->flags, PROC_C1);
538 PRINTFLAG(fadt->flags, P_LVL2_UP);
539 PRINTFLAG(fadt->flags, PWR_BUTTON);
540 PRINTFLAG(fadt->flags, SLP_BUTTON);
541 PRINTFLAG(fadt->flags, FIX_RTC);
542 PRINTFLAG(fadt->flags, RTC_S4);
543 PRINTFLAG(fadt->flags, TMR_VAL_EXT);
544 PRINTFLAG(fadt->flags, DCK_CAP);
545 PRINTFLAG(fadt->flags, RESET_REG);
546 PRINTFLAG(fadt->flags, SEALED_CASE);
547 PRINTFLAG(fadt->flags, HEADLESS);
548 PRINTFLAG(fadt->flags, CPU_SW_SLP);
549 if (fadt->flags != 0)
550 printf("}\n");
552 #undef PRINTFLAG
554 if (fadt->flags & FADT_FLAG_RESET_REG) {
555 printf("\tRESET_REG=");
556 acpi_print_gas(&fadt->reset_reg);
557 printf(", RESET_VALUE=%#x\n", fadt->reset_value);
559 if (acpi_get_fadt_revision(fadt) > 1) {
560 printf("\tX_FACS=0x%08lx, ", (u_long)fadt->x_facs_ptr);
561 printf("X_DSDT=0x%08lx\n", (u_long)fadt->x_dsdt_ptr);
562 printf("\tX_PM1a_EVT_BLK=");
563 acpi_print_gas(&fadt->x_pm1a_evt_blk);
564 if (fadt->x_pm1b_evt_blk.address != 0) {
565 printf("\n\tX_PM1b_EVT_BLK=");
566 acpi_print_gas(&fadt->x_pm1b_evt_blk);
568 printf("\n\tX_PM1a_CNT_BLK=");
569 acpi_print_gas(&fadt->x_pm1a_cnt_blk);
570 if (fadt->x_pm1b_cnt_blk.address != 0) {
571 printf("\n\tX_PM1b_CNT_BLK=");
572 acpi_print_gas(&fadt->x_pm1b_cnt_blk);
574 if (fadt->x_pm1b_cnt_blk.address != 0) {
575 printf("\n\tX_PM2_CNT_BLK=");
576 acpi_print_gas(&fadt->x_pm2_cnt_blk);
578 printf("\n\tX_PM_TMR_BLK=");
579 acpi_print_gas(&fadt->x_pm_tmr_blk);
580 if (fadt->x_gpe0_blk.address != 0) {
581 printf("\n\tX_GPE0_BLK=");
582 acpi_print_gas(&fadt->x_gpe0_blk);
584 if (fadt->x_gpe1_blk.address != 0) {
585 printf("\n\tX_GPE1_BLK=");
586 acpi_print_gas(&fadt->x_gpe1_blk);
588 printf("\n");
591 printf(END_COMMENT);
594 static void
595 acpi_print_facs(struct FACSbody *facs)
597 printf(BEGIN_COMMENT);
598 printf(" FACS:\tLength=%u, ", facs->len);
599 printf("HwSig=0x%08x, ", facs->hw_sig);
600 printf("Firm_Wake_Vec=0x%08x\n", facs->firm_wake_vec);
602 printf("\tGlobal_Lock=");
603 if (facs->global_lock != 0) {
604 if (facs->global_lock & FACS_FLAG_LOCK_PENDING)
605 printf("PENDING,");
606 if (facs->global_lock & FACS_FLAG_LOCK_OWNED)
607 printf("OWNED");
609 printf("\n");
611 printf("\tFlags=");
612 if (facs->flags & FACS_FLAG_S4BIOS_F)
613 printf("S4BIOS");
614 printf("\n");
616 if (facs->x_firm_wake_vec != 0) {
617 printf("\tX_Firm_Wake_Vec=%08lx\n",
618 (u_long)facs->x_firm_wake_vec);
620 printf("\tVersion=%u\n", facs->version);
622 printf(END_COMMENT);
625 static void
626 acpi_print_dsdt(struct ACPIsdt *dsdp)
628 printf(BEGIN_COMMENT);
629 acpi_print_sdt(dsdp);
630 printf(END_COMMENT);
634 acpi_checksum(void *p, size_t length)
636 u_int8_t *bp;
637 u_int8_t sum;
639 bp = p;
640 sum = 0;
641 while (length--)
642 sum += *bp++;
644 return (sum);
647 static struct ACPIsdt *
648 acpi_map_sdt(vm_offset_t pa)
650 struct ACPIsdt *sp;
652 sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
653 sp = acpi_map_physical(pa, sp->len);
654 return (sp);
657 static void
658 acpi_print_rsd_ptr(struct ACPIrsdp *rp)
660 printf(BEGIN_COMMENT);
661 printf(" RSD PTR: OEM=");
662 acpi_print_string(rp->oem, 6);
663 printf(", ACPI_Rev=%s (%d)\n", rp->revision < 2 ? "1.0x" : "2.0x",
664 rp->revision);
665 if (rp->revision < 2) {
666 printf("\tRSDT=0x%08x, cksum=%u\n", rp->rsdt_addr, rp->sum);
667 } else {
668 printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
669 (u_long)rp->xsdt_addr, rp->length, rp->xsum);
671 printf(END_COMMENT);
674 static void
675 acpi_handle_rsdt(struct ACPIsdt *rsdp)
677 struct ACPIsdt *sdp;
678 vm_offset_t addr;
679 int entries, i;
681 acpi_print_rsdt(rsdp);
682 entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
683 for (i = 0; i < entries; i++) {
684 switch (addr_size) {
685 case 4:
686 addr = le32dec((char*)rsdp->body + i * addr_size);
687 break;
688 case 8:
689 addr = le64dec((char*)rsdp->body + i * addr_size);
690 break;
691 default:
692 assert((addr = 0));
695 sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
696 if (acpi_checksum(sdp, sdp->len)) {
697 warnx("RSDT entry %d (sig %.4s) is corrupt", i,
698 sdp->signature);
699 continue;
701 if (!memcmp(sdp->signature, "FACP", 4))
702 acpi_handle_fadt(sdp);
703 else if (!memcmp(sdp->signature, "APIC", 4))
704 acpi_handle_apic(sdp);
705 else if (!memcmp(sdp->signature, "HPET", 4))
706 acpi_handle_hpet(sdp);
707 else if (!memcmp(sdp->signature, "ECDT", 4))
708 acpi_handle_ecdt(sdp);
709 else if (!memcmp(sdp->signature, "MCFG", 4))
710 acpi_handle_mcfg(sdp);
711 else {
712 printf(BEGIN_COMMENT);
713 acpi_print_sdt(sdp);
714 printf(END_COMMENT);
719 struct ACPIsdt *
720 sdt_load_devmem(void)
722 struct ACPIrsdp *rp;
723 struct ACPIsdt *rsdp;
725 rp = acpi_find_rsd_ptr();
726 if (!rp)
727 errx(1, "Can't find ACPI information");
729 if (tflag)
730 acpi_print_rsd_ptr(rp);
731 if (rp->revision < 2) {
732 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
733 if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
734 acpi_checksum(rsdp, rsdp->len) != 0)
735 errx(1, "RSDT is corrupted");
736 addr_size = sizeof(uint32_t);
737 } else {
738 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
739 if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
740 acpi_checksum(rsdp, rsdp->len) != 0)
741 errx(1, "XSDT is corrupted");
742 addr_size = sizeof(uint64_t);
744 return (rsdp);
747 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
748 static int
749 write_dsdt(int fd, struct ACPIsdt *rsdt, struct ACPIsdt *dsdt)
751 struct ACPIsdt sdt;
752 struct ACPIsdt *ssdt;
753 uint8_t sum;
755 /* Create a new checksum to account for the DSDT and any SSDTs. */
756 sdt = *dsdt;
757 if (rsdt != NULL) {
758 sdt.check = 0;
759 sum = acpi_checksum(dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
760 ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
761 while (ssdt != NULL) {
762 sdt.len += ssdt->len - SIZEOF_SDT_HDR;
763 sum += acpi_checksum(ssdt->body,
764 ssdt->len - SIZEOF_SDT_HDR);
765 ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
767 sum += acpi_checksum(&sdt, SIZEOF_SDT_HDR);
768 sdt.check -= sum;
771 /* Write out the DSDT header and body. */
772 write(fd, &sdt, SIZEOF_SDT_HDR);
773 write(fd, dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
775 /* Write out any SSDTs (if present.) */
776 if (rsdt != NULL) {
777 ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
778 while (ssdt != NULL) {
779 write(fd, ssdt->body, ssdt->len - SIZEOF_SDT_HDR);
780 ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
783 return (0);
786 void
787 dsdt_save_file(char *outfile, struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
789 int fd;
790 mode_t mode;
792 assert(outfile != NULL);
793 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
794 fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
795 if (fd == -1) {
796 perror("dsdt_save_file");
797 return;
799 write_dsdt(fd, rsdt, dsdp);
800 close(fd);
803 void
804 aml_disassemble(struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
806 char tmpstr[32], buf[256];
807 FILE *fp;
808 int fd, len;
810 strcpy(tmpstr, "/tmp/acpidump.XXXXXX");
811 fd = mkstemp(tmpstr);
812 if (fd < 0) {
813 perror("iasl tmp file");
814 return;
816 write_dsdt(fd, rsdt, dsdp);
817 close(fd);
819 /* Run iasl -d on the temp file */
820 if (fork() == 0) {
821 close(STDOUT_FILENO);
822 if (vflag == 0)
823 close(STDERR_FILENO);
824 execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, 0);
825 err(1, "exec");
828 wait(NULL);
829 unlink(tmpstr);
831 /* Dump iasl's output to stdout */
832 fp = fopen("acpidump.dsl", "r");
833 unlink("acpidump.dsl");
834 if (fp == NULL) {
835 perror("iasl tmp file (read)");
836 return;
838 while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
839 fwrite(buf, 1, len, stdout);
840 fclose(fp);
843 void
844 sdt_print_all(struct ACPIsdt *rsdp)
846 acpi_handle_rsdt(rsdp);
849 /* Fetch a table matching the given signature via the RSDT. */
850 struct ACPIsdt *
851 sdt_from_rsdt(struct ACPIsdt *rsdt, const char *sig, struct ACPIsdt *last)
853 struct ACPIsdt *sdt;
854 vm_offset_t addr;
855 int entries, i;
857 entries = (rsdt->len - SIZEOF_SDT_HDR) / addr_size;
858 for (i = 0; i < entries; i++) {
859 switch (addr_size) {
860 case 4:
861 addr = le32dec((char*)rsdt->body + i * addr_size);
862 break;
863 case 8:
864 addr = le64dec((char*)rsdt->body + i * addr_size);
865 break;
866 default:
867 assert((addr = 0));
869 sdt = (struct ACPIsdt *)acpi_map_sdt(addr);
870 if (last != NULL) {
871 if (sdt == last)
872 last = NULL;
873 continue;
875 if (memcmp(sdt->signature, sig, strlen(sig)))
876 continue;
877 if (acpi_checksum(sdt, sdt->len))
878 errx(1, "RSDT entry %d is corrupt", i);
879 return (sdt);
882 return (NULL);
885 struct ACPIsdt *
886 dsdt_from_fadt(struct FADTbody *fadt)
888 struct ACPIsdt *sdt;
890 /* Use the DSDT address if it is version 1, otherwise use X_DSDT. */
891 if (acpi_get_fadt_revision(fadt) == 1)
892 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
893 else
894 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
895 if (acpi_checksum(sdt, sdt->len))
896 errx(1, "DSDT is corrupt\n");
897 return (sdt);