Merge from vendor branch NCURSES:
[dragonfly.git] / usr.sbin / acpi / acpidump / acpi.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4  * All rights reserved.
5  *
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.
14  *
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.
26  *
27  *      $FreeBSD: src/usr.sbin/acpi/acpidump/acpi.c,v 1.25 2004/06/30 03:23:51 njl Exp $
28  *      $DragonFly: src/usr.sbin/acpi/acpidump/acpi.c,v 1.2 2004/08/23 16:03:44 joerg Exp $
29  */
30
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>
41
42 #include "acpidump.h"
43
44 #define BEGIN_COMMENT   "/*\n"
45 #define END_COMMENT     " */\n"
46
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);
68
69 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
70 static int addr_size;
71
72 static void
73 acpi_print_string(char *s, size_t length)
74 {
75         int     c;
76
77         /* Trim trailing spaces and NULLs */
78         while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
79                 length--;
80
81         while (length--) {
82                 c = *s++;
83                 putchar(c);
84         }
85 }
86
87 static void
88 acpi_print_gas(struct ACPIgas *gas)
89 {
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;
117         }
118 }
119
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)
123 {
124         int fadt_revision;
125
126         /* Set the FADT revision separately from the RSDP version. */
127         if (addr_size == 8) {
128                 fadt_revision = 2;
129
130                 /*
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.
136                  */
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);
143 }
144
145 static void
146 acpi_handle_fadt(struct ACPIsdt *sdp)
147 {
148         struct ACPIsdt  *dsdp;
149         struct FACSbody *facs;
150         struct FADTbody *fadt;
151         int             fadt_revision;
152
153         fadt = (struct FADTbody *)sdp->body;
154         acpi_print_fadt(sdp);
155
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);
164
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);
172 }
173
174 static void
175 acpi_print_cpu(u_char cpu_id)
176 {
177
178         printf("\tACPI CPU=");
179         if (cpu_id == 0xff)
180                 printf("ALL\n");
181         else
182                 printf("%d\n", (u_int)cpu_id);
183 }
184
185 static void
186 acpi_print_local_apic(u_char cpu_id, u_char apic_id, u_int32_t flags)
187 {
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);
196 }
197
198 static void
199 acpi_print_io_apic(u_char apic_id, u_int32_t int_base, u_int64_t apic_addr)
200 {
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);
204 }
205
206 static void
207 acpi_print_mps_flags(u_int16_t flags)
208 {
209
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;
224         }
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);
238         }
239         printf("}\n");
240 }
241
242 static void
243 acpi_print_intr(u_int32_t intr, u_int16_t mps_flags)
244 {
245
246         printf("\tINTR=%d\n", (u_int)intr);
247         acpi_print_mps_flags(mps_flags);
248 }
249
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" };
255
256 static void
257 acpi_print_apic(struct MADT_APIC *mp)
258 {
259
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;
312         }
313 }
314
315 static void
316 acpi_handle_apic(struct ACPIsdt *sdp)
317 {
318         struct MADTbody *madtp;
319         struct MADT_APIC *madt_apicp;
320
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);
335         }
336         printf(END_COMMENT);
337 }
338
339 static void
340 acpi_handle_hpet(struct ACPIsdt *sdp)
341 {
342         struct HPETbody *hpetp;
343
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);
360 }
361
362 static void
363 acpi_handle_ecdt(struct ACPIsdt *sdp)
364 {
365         struct ECDTbody *ecdt;
366
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);
378 }
379
380 static void
381 acpi_print_sdt(struct ACPIsdt *sdp)
382 {
383         printf("  ");
384         acpi_print_string(sdp->signature, 4);
385         printf(": Length=%d, Revision=%d, Checksum=%d,\n",
386                sdp->len, sdp->rev, sdp->check);
387         printf("\tOEMID=");
388         acpi_print_string(sdp->oemid, 6);
389         printf(", OEM Table ID=");
390         acpi_print_string(sdp->oemtblid, 8);
391         printf(", OEM Revision=0x%x,\n", sdp->oemrev);
392         printf("\tCreator ID=");
393         acpi_print_string(sdp->creator, 4);
394         printf(", Creator Revision=0x%x\n", sdp->crerev);
395 }
396
397 static void
398 acpi_print_rsdt(struct ACPIsdt *rsdp)
399 {
400         int     i, entries;
401         u_long  addr;
402
403         printf(BEGIN_COMMENT);
404         acpi_print_sdt(rsdp);
405         entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
406         printf("\tEntries={ ");
407         for (i = 0; i < entries; i++) {
408                 if (i > 0)
409                         printf(", ");
410                 switch (addr_size) {
411                 case 4:
412                         addr = le32dec((char*)rsdp->body + i * addr_size);
413                         break;
414                 case 8:
415                         addr = le64dec((char*)rsdp->body + i * addr_size);
416                         break;
417                 default:
418                         addr = 0;
419                 }
420                 assert(addr != 0);
421                 printf("0x%08lx", addr);
422         }
423         printf(" }\n");
424         printf(END_COMMENT);
425 }
426
427 static const char *acpi_pm_profiles[] = {
428         "Unspecified", "Desktop", "Mobile", "Workstation",
429         "Enterprise Server", "SOHO Server", "Appliance PC"
430 };
431
432 static void
433 acpi_print_fadt(struct ACPIsdt *sdp)
434 {
435         struct FADTbody *fadt;
436         const char *pm;
437         char        sep;
438
439         fadt = (struct FADTbody *)sdp->body;
440         printf(BEGIN_COMMENT);
441         acpi_print_sdt(sdp);
442         printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->facs_ptr,
443                fadt->dsdt_ptr);
444         printf("\tINT_MODEL=%s\n", fadt->int_model ? "APIC" : "PIC");
445         if (fadt->pm_profile >= sizeof(acpi_pm_profiles) / sizeof(char *))
446                 pm = "Reserved";
447         else
448                 pm = acpi_pm_profiles[fadt->pm_profile];
449         printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->pm_profile);
450         printf("\tSCI_INT=%d\n", fadt->sci_int);
451         printf("\tSMI_CMD=0x%x, ", fadt->smi_cmd);
452         printf("ACPI_ENABLE=0x%x, ", fadt->acpi_enable);
453         printf("ACPI_DISABLE=0x%x, ", fadt->acpi_disable);
454         printf("S4BIOS_REQ=0x%x\n", fadt->s4biosreq);
455         printf("\tPSTATE_CNT=0x%x\n", fadt->pstate_cnt);
456         printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
457                fadt->pm1a_evt_blk,
458                fadt->pm1a_evt_blk + fadt->pm1_evt_len - 1);
459         if (fadt->pm1b_evt_blk != 0)
460                 printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
461                        fadt->pm1b_evt_blk,
462                        fadt->pm1b_evt_blk + fadt->pm1_evt_len - 1);
463         printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
464                fadt->pm1a_cnt_blk,
465                fadt->pm1a_cnt_blk + fadt->pm1_cnt_len - 1);
466         if (fadt->pm1b_cnt_blk != 0)
467                 printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
468                        fadt->pm1b_cnt_blk,
469                        fadt->pm1b_cnt_blk + fadt->pm1_cnt_len - 1);
470         if (fadt->pm2_cnt_blk != 0)
471                 printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
472                        fadt->pm2_cnt_blk,
473                        fadt->pm2_cnt_blk + fadt->pm2_cnt_len - 1);
474         printf("\tPM_TMR_BLK=0x%x-0x%x\n",
475                fadt->pm_tmr_blk,
476                fadt->pm_tmr_blk + fadt->pm_tmr_len - 1);
477         if (fadt->gpe0_blk != 0)
478                 printf("\tGPE0_BLK=0x%x-0x%x\n",
479                        fadt->gpe0_blk,
480                        fadt->gpe0_blk + fadt->gpe0_len - 1);
481         if (fadt->gpe1_blk != 0)
482                 printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
483                        fadt->gpe1_blk,
484                        fadt->gpe1_blk + fadt->gpe1_len - 1,
485                        fadt->gpe1_base);
486         if (fadt->cst_cnt != 0)
487                 printf("\tCST_CNT=0x%x\n", fadt->cst_cnt);
488         printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
489                fadt->p_lvl2_lat, fadt->p_lvl3_lat);
490         printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
491                fadt->flush_size, fadt->flush_stride);
492         printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
493                fadt->duty_off, fadt->duty_width);
494         printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
495                fadt->day_alrm, fadt->mon_alrm, fadt->century);
496
497 #define PRINTFLAG(var, flag) do {                       \
498         if ((var) & FADT_FLAG_## flag) {                \
499                 printf("%c%s", sep, #flag); sep = ',';  \
500         }                                               \
501 } while (0)
502
503         printf("\tIAPC_BOOT_ARCH=");
504         sep = '{';
505         PRINTFLAG(fadt->iapc_boot_arch, LEGACY_DEV);
506         PRINTFLAG(fadt->iapc_boot_arch, 8042);
507         if (fadt->iapc_boot_arch != 0)
508                 printf("}");
509         printf("\n");
510
511         printf("\tFlags=");
512         sep = '{';
513         PRINTFLAG(fadt->flags, WBINVD);
514         PRINTFLAG(fadt->flags, WBINVD_FLUSH);
515         PRINTFLAG(fadt->flags, PROC_C1);
516         PRINTFLAG(fadt->flags, P_LVL2_UP);
517         PRINTFLAG(fadt->flags, PWR_BUTTON);
518         PRINTFLAG(fadt->flags, SLP_BUTTON);
519         PRINTFLAG(fadt->flags, FIX_RTC);
520         PRINTFLAG(fadt->flags, RTC_S4);
521         PRINTFLAG(fadt->flags, TMR_VAL_EXT);
522         PRINTFLAG(fadt->flags, DCK_CAP);
523         PRINTFLAG(fadt->flags, RESET_REG);
524         PRINTFLAG(fadt->flags, SEALED_CASE);
525         PRINTFLAG(fadt->flags, HEADLESS);
526         PRINTFLAG(fadt->flags, CPU_SW_SLP);
527         if (fadt->flags != 0)
528                 printf("}\n");
529
530 #undef PRINTFLAG
531
532         if (fadt->flags & FADT_FLAG_RESET_REG) {
533                 printf("\tRESET_REG=");
534                 acpi_print_gas(&fadt->reset_reg);
535                 printf(", RESET_VALUE=%#x\n", fadt->reset_value);
536         }
537         if (acpi_get_fadt_revision(fadt) > 1) {
538                 printf("\tX_FACS=0x%08lx, ", (u_long)fadt->x_facs_ptr);
539                 printf("X_DSDT=0x%08lx\n", (u_long)fadt->x_dsdt_ptr);
540                 printf("\tX_PM1a_EVT_BLK=");
541                 acpi_print_gas(&fadt->x_pm1a_evt_blk);
542                 if (fadt->x_pm1b_evt_blk.address != 0) {
543                         printf("\n\tX_PM1b_EVT_BLK=");
544                         acpi_print_gas(&fadt->x_pm1b_evt_blk);
545                 }
546                 printf("\n\tX_PM1a_CNT_BLK=");
547                 acpi_print_gas(&fadt->x_pm1a_cnt_blk);
548                 if (fadt->x_pm1b_cnt_blk.address != 0) {
549                         printf("\n\tX_PM1b_CNT_BLK=");
550                         acpi_print_gas(&fadt->x_pm1b_cnt_blk);
551                 }
552                 if (fadt->x_pm1b_cnt_blk.address != 0) {
553                         printf("\n\tX_PM2_CNT_BLK=");
554                         acpi_print_gas(&fadt->x_pm2_cnt_blk);
555                 }
556                 printf("\n\tX_PM_TMR_BLK=");
557                 acpi_print_gas(&fadt->x_pm_tmr_blk);
558                 if (fadt->x_gpe0_blk.address != 0) {
559                         printf("\n\tX_GPE0_BLK=");
560                         acpi_print_gas(&fadt->x_gpe0_blk);
561                 }
562                 if (fadt->x_gpe1_blk.address != 0) {
563                         printf("\n\tX_GPE1_BLK=");
564                         acpi_print_gas(&fadt->x_gpe1_blk);
565                 }
566                 printf("\n");
567         }
568
569         printf(END_COMMENT);
570 }
571
572 static void
573 acpi_print_facs(struct FACSbody *facs)
574 {
575         printf(BEGIN_COMMENT);
576         printf("  FACS:\tLength=%u, ", facs->len);
577         printf("HwSig=0x%08x, ", facs->hw_sig);
578         printf("Firm_Wake_Vec=0x%08x\n", facs->firm_wake_vec);
579
580         printf("\tGlobal_Lock=");
581         if (facs->global_lock != 0) {
582                 if (facs->global_lock & FACS_FLAG_LOCK_PENDING)
583                         printf("PENDING,");
584                 if (facs->global_lock & FACS_FLAG_LOCK_OWNED)
585                         printf("OWNED");
586         }
587         printf("\n");
588
589         printf("\tFlags=");
590         if (facs->flags & FACS_FLAG_S4BIOS_F)
591                 printf("S4BIOS");
592         printf("\n");
593
594         if (facs->x_firm_wake_vec != 0) {
595                 printf("\tX_Firm_Wake_Vec=%08lx\n",
596                        (u_long)facs->x_firm_wake_vec);
597         }
598         printf("\tVersion=%u\n", facs->version);
599
600         printf(END_COMMENT);
601 }
602
603 static void
604 acpi_print_dsdt(struct ACPIsdt *dsdp)
605 {
606         printf(BEGIN_COMMENT);
607         acpi_print_sdt(dsdp);
608         printf(END_COMMENT);
609 }
610
611 int
612 acpi_checksum(void *p, size_t length)
613 {
614         u_int8_t        *bp;
615         u_int8_t        sum;
616
617         bp = p;
618         sum = 0;
619         while (length--)
620                 sum += *bp++;
621
622         return (sum);
623 }
624
625 static struct ACPIsdt *
626 acpi_map_sdt(vm_offset_t pa)
627 {
628         struct  ACPIsdt *sp;
629
630         sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
631         sp = acpi_map_physical(pa, sp->len);
632         return (sp);
633 }
634
635 static void
636 acpi_print_rsd_ptr(struct ACPIrsdp *rp)
637 {
638         printf(BEGIN_COMMENT);
639         printf("  RSD PTR: OEM=");
640         acpi_print_string(rp->oem, 6);
641         printf(", ACPI_Rev=%s (%d)\n", rp->revision < 2 ? "1.0x" : "2.0x",
642                rp->revision);
643         if (rp->revision < 2) {
644                 printf("\tRSDT=0x%08x, cksum=%u\n", rp->rsdt_addr, rp->sum);
645         } else {
646                 printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
647                     (u_long)rp->xsdt_addr, rp->length, rp->xsum);
648         }
649         printf(END_COMMENT);
650 }
651
652 static void
653 acpi_handle_rsdt(struct ACPIsdt *rsdp)
654 {
655         struct ACPIsdt *sdp;
656         vm_offset_t addr;
657         int entries, i;
658
659         acpi_print_rsdt(rsdp);
660         entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
661         for (i = 0; i < entries; i++) {
662                 switch (addr_size) {
663                 case 4:
664                         addr = le32dec((char*)rsdp->body + i * addr_size);
665                         break;
666                 case 8:
667                         addr = le64dec((char*)rsdp->body + i * addr_size);
668                         break;
669                 default:
670                         assert((addr = 0));
671                 }
672
673                 sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
674                 if (acpi_checksum(sdp, sdp->len))
675                         errx(1, "RSDT entry %d is corrupt", i);
676                 if (!memcmp(sdp->signature, "FACP", 4))
677                         acpi_handle_fadt(sdp);
678                 else if (!memcmp(sdp->signature, "APIC", 4))
679                         acpi_handle_apic(sdp);
680                 else if (!memcmp(sdp->signature, "HPET", 4))
681                         acpi_handle_hpet(sdp);
682                 else if (!memcmp(sdp->signature, "ECDT", 4))
683                         acpi_handle_ecdt(sdp);
684                 else {
685                         printf(BEGIN_COMMENT);
686                         acpi_print_sdt(sdp);
687                         printf(END_COMMENT);
688                 }
689         }
690 }
691
692 struct ACPIsdt *
693 sdt_load_devmem(void)
694 {
695         struct  ACPIrsdp *rp;
696         struct  ACPIsdt *rsdp;
697
698         rp = acpi_find_rsd_ptr();
699         if (!rp)
700                 errx(1, "Can't find ACPI information");
701
702         if (tflag)
703                 acpi_print_rsd_ptr(rp);
704         if (rp->revision < 2) {
705                 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
706                 if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
707                     acpi_checksum(rsdp, rsdp->len) != 0)
708                         errx(1, "RSDT is corrupted");
709                 addr_size = sizeof(uint32_t);
710         } else {
711                 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
712                 if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
713                     acpi_checksum(rsdp, rsdp->len) != 0)
714                         errx(1, "XSDT is corrupted");
715                 addr_size = sizeof(uint64_t);
716         }
717         return (rsdp);
718 }
719
720 void
721 dsdt_save_file(char *outfile, struct ACPIsdt *dsdp)
722 {
723         int     fd;
724         mode_t  mode;
725
726         assert(outfile != NULL);
727         mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
728         fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
729         if (fd == -1) {
730                 perror("dsdt_save_file");
731                 return;
732         }
733         write(fd, dsdp, SIZEOF_SDT_HDR);
734         write(fd, dsdp->body, dsdp->len - SIZEOF_SDT_HDR);
735         close(fd);
736 }
737
738 void
739 aml_disassemble(struct ACPIsdt *dsdp)
740 {
741         char tmpstr[32], buf[256];
742         FILE *fp;
743         int fd, len;
744
745         strcpy(tmpstr, "/tmp/acpidump.XXXXXX");
746         fd = mkstemp(tmpstr);
747         if (fd < 0) {
748                 perror("iasl tmp file");
749                 return;
750         }
751
752         /* Dump DSDT to the temp file */
753         write(fd, dsdp, SIZEOF_SDT_HDR);
754         write(fd, dsdp->body, dsdp->len - SIZEOF_SDT_HDR);
755         close(fd);
756
757         /* Run iasl -d on the temp file */
758         if (fork() == 0) {
759                 close(STDOUT_FILENO);
760                 if (vflag == 0)
761                         close(STDERR_FILENO);
762                 execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, 0);
763                 err(1, "exec");
764         }
765
766         wait(NULL);
767         unlink(tmpstr);
768
769         /* Dump iasl's output to stdout */
770         fp = fopen("acpidump.dsl", "r");
771         unlink("acpidump.dsl");
772         if (fp == NULL) {
773                 perror("iasl tmp file (read)");
774                 return;
775         }
776         while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
777                 fwrite(buf, 1, len, stdout);
778         fclose(fp);
779 }
780
781 void
782 sdt_print_all(struct ACPIsdt *rsdp)
783 {
784         acpi_handle_rsdt(rsdp);
785 }
786
787 /* Fetch a table matching the given signature via the RSDT */
788 struct ACPIsdt *
789 sdt_from_rsdt(struct ACPIsdt *rsdt, const char *sig)
790 {
791         struct ACPIsdt *sdt;
792         vm_offset_t addr;
793         int entries, i;
794
795         entries = (rsdt->len - SIZEOF_SDT_HDR) / addr_size;
796         for (i = 0; i < entries; i++) {
797                 switch (addr_size) {
798                 case 4:
799                         addr = le32dec((char*)rsdt->body + i * addr_size);
800                         break;
801                 case 8:
802                         addr = le64dec((char*)rsdt->body + i * addr_size);
803                         break;
804                 default:
805                         assert((addr = 0));
806                 }
807                 sdt = (struct ACPIsdt *)acpi_map_sdt(addr);
808                 if (memcmp(sdt->signature, sig, strlen(sig)))
809                         continue;
810                 if (acpi_checksum(sdt, sdt->len))
811                         errx(1, "RSDT entry %d is corrupt", i);
812                 return (sdt);
813         }
814
815         return (NULL);
816 }
817
818 struct ACPIsdt *
819 dsdt_from_fadt(struct FADTbody *fadt)
820 {
821         struct  ACPIsdt *sdt;
822
823         /* Use the DSDT address if it is version 1, otherwise use X_DSDT. */
824         if (acpi_get_fadt_revision(fadt) == 1)
825                 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
826         else
827                 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
828         if (acpi_checksum(sdt, sdt->len))
829                 errx(1, "DSDT is corrupt\n");
830         return (sdt);
831 }