acpidump - be a bit more lenient and don't assert
[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.31 2005/02/14 11:21:48 scottl Exp $
28  *      $DragonFly: src/usr.sbin/acpi/acpidump/acpi.c,v 1.7 2008/07/19 13:50:51 swildner 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 <paths.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "acpidump.h"
45
46 #define BEGIN_COMMENT   "/*\n"
47 #define END_COMMENT     " */\n"
48
49 static void     acpi_print_string(char *s, size_t length);
50 static void     acpi_print_gas(struct ACPIgas *gas);
51 static int      acpi_get_fadt_revision(struct FADTbody *fadt);
52 static void     acpi_handle_fadt(struct ACPIsdt *fadt);
53 static void     acpi_print_cpu(u_char cpu_id);
54 static void     acpi_print_local_apic(u_char cpu_id, u_char apic_id,
55                                       u_int32_t flags);
56 static void     acpi_print_io_apic(u_char apic_id, u_int32_t int_base,
57                                    u_int64_t apic_addr);
58 static void     acpi_print_mps_flags(u_int16_t flags);
59 static void     acpi_print_intr(u_int32_t intr, u_int16_t mps_flags);
60 static void     acpi_print_madt(struct MADT_APIC *mp);
61 static void     acpi_handle_madt(struct ACPIsdt *sdp);
62 static void     acpi_handle_hpet(struct ACPIsdt *sdp);
63 static void     acpi_print_sdt(struct ACPIsdt *sdp);
64 static void     acpi_print_fadt(struct ACPIsdt *sdp);
65 static void     acpi_print_facs(struct FACSbody *facs);
66 static void     acpi_print_dsdt(struct ACPIsdt *dsdp);
67 static struct ACPIsdt *acpi_map_sdt(vm_offset_t pa);
68 static void     acpi_print_rsd_ptr(struct ACPIrsdp *rp);
69 static void     acpi_handle_rsdt(struct ACPIsdt *rsdp);
70
71 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
72 static int addr_size;
73
74 static void
75 acpi_print_string(char *s, size_t length)
76 {
77         int     c;
78
79         /* Trim trailing spaces and NULLs */
80         while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
81                 length--;
82
83         while (length--) {
84                 c = *s++;
85                 putchar(c);
86         }
87 }
88
89 static void
90 acpi_print_gas(struct ACPIgas *gas)
91 {
92         switch(gas->address_space_id) {
93         case ACPI_GAS_MEMORY:
94                 printf("0x%08lx:%u[%u] (Memory)", (u_long)gas->address,
95                        gas->bit_offset, gas->bit_width);
96                 break;
97         case ACPI_GAS_IO:
98                 printf("0x%02lx:%u[%u] (IO)", (u_long)gas->address,
99                        gas->bit_offset, gas->bit_width);
100                 break;
101         case ACPI_GAS_PCI:
102                 printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->address >> 32),
103                        (uint16_t)((gas->address >> 16) & 0xffff),
104                        (uint16_t)gas->address);
105                 break;
106         /* XXX How to handle these below? */
107         case ACPI_GAS_EMBEDDED:
108                 printf("0x%x:%u[%u] (EC)", (uint16_t)gas->address,
109                        gas->bit_offset, gas->bit_width);
110                 break;
111         case ACPI_GAS_SMBUS:
112                 printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->address,
113                        gas->bit_offset, gas->bit_width);
114                 break;
115     case ACPI_GAS_CMOS:
116     case ACPI_GAS_PCIBAR:
117     case ACPI_GAS_DATATABLE:
118         case ACPI_GAS_FIXED:
119         default:
120                 printf("0x%08lx (?)", (u_long)gas->address);
121                 break;
122         }
123 }
124
125 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
126 static int
127 acpi_get_fadt_revision(struct FADTbody *fadt)
128 {
129         int fadt_revision;
130
131         /* Set the FADT revision separately from the RSDP version. */
132         if (addr_size == 8) {
133                 fadt_revision = 2;
134
135                 /*
136                  * A few systems (e.g., IBM T23) have an RSDP that claims
137                  * revision 2 but the 64 bit addresses are invalid.  If
138                  * revision 2 and the 32 bit address is non-zero but the
139                  * 32 and 64 bit versions don't match, prefer the 32 bit
140                  * version for all subsequent tables.
141                  */
142                 if (fadt->facs_ptr != 0 &&
143                     (fadt->x_facs_ptr & 0xffffffff) != fadt->facs_ptr)
144                         fadt_revision = 1;
145         } else
146                 fadt_revision = 1;
147         return (fadt_revision);
148 }
149
150 static void
151 acpi_handle_fadt(struct ACPIsdt *sdp)
152 {
153         struct ACPIsdt  *dsdp;
154         struct FACSbody *facs;
155         struct FADTbody *fadt;
156         int             fadt_revision;
157
158         fadt = (struct FADTbody *)sdp->body;
159         acpi_print_fadt(sdp);
160
161         fadt_revision = acpi_get_fadt_revision(fadt);
162         if (fadt_revision == 1)
163                 facs = (struct FACSbody *)acpi_map_sdt(fadt->facs_ptr);
164         else
165                 facs = (struct FACSbody *)acpi_map_sdt(fadt->x_facs_ptr);
166         if (memcmp(facs->signature, "FACS", 4) != 0 || facs->len < 64)
167                 errx(1, "FACS is corrupt");
168         acpi_print_facs(facs);
169
170         if (fadt_revision == 1)
171                 dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
172         else
173                 dsdp = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
174         if (acpi_checksum(dsdp, dsdp->len))
175                 errx(1, "DSDT is corrupt");
176         acpi_print_dsdt(dsdp);
177 }
178
179 static void
180 acpi_print_cpu(u_char cpu_id)
181 {
182
183         printf("\tACPI CPU=");
184         if (cpu_id == 0xff)
185                 printf("ALL\n");
186         else
187                 printf("%d\n", (u_int)cpu_id);
188 }
189
190 static void
191 acpi_print_local_apic(u_char cpu_id, u_char apic_id, u_int32_t flags)
192 {
193         acpi_print_cpu(cpu_id);
194         printf("\tFlags={");
195         if (flags & ACPI_MADT_APIC_LOCAL_FLAG_ENABLED)
196                 printf("ENABLED");
197         else
198                 printf("DISABLED");
199         printf("}\n");
200         printf("\tAPIC ID=%d\n", (u_int)apic_id);
201 }
202
203 static void
204 acpi_print_io_apic(u_char apic_id, u_int32_t int_base, u_int64_t apic_addr)
205 {
206         printf("\tAPIC ID=%d\n", (u_int)apic_id);
207         printf("\tINT BASE=%d\n", int_base);
208         printf("\tADDR=0x%016jx\n", apic_addr);
209 }
210
211 static void
212 acpi_print_mps_flags(u_int16_t flags)
213 {
214
215         printf("\tFlags={Polarity=");
216         switch (flags & MPS_INT_FLAG_POLARITY_MASK) {
217         case MPS_INT_FLAG_POLARITY_CONFORM:
218                 printf("conforming");
219                 break;
220         case MPS_INT_FLAG_POLARITY_HIGH:
221                 printf("active-hi");
222                 break;
223         case MPS_INT_FLAG_POLARITY_LOW:
224                 printf("active-lo");
225                 break;
226         default:
227                 printf("0x%x", flags & MPS_INT_FLAG_POLARITY_MASK);
228                 break;
229         }
230         printf(", Trigger=");
231         switch (flags & MPS_INT_FLAG_TRIGGER_MASK) {
232         case MPS_INT_FLAG_TRIGGER_CONFORM:
233                 printf("conforming");
234                 break;
235         case MPS_INT_FLAG_TRIGGER_EDGE:
236                 printf("edge");
237                 break;
238         case MPS_INT_FLAG_TRIGGER_LEVEL:
239                 printf("level");
240                 break;
241         default:
242                 printf("0x%x", (flags & MPS_INT_FLAG_TRIGGER_MASK) >> 2);
243         }
244         printf("}\n");
245 }
246
247 static void
248 acpi_print_intr(u_int32_t intr, u_int16_t mps_flags)
249 {
250
251         printf("\tINTR=%d\n", (u_int)intr);
252         acpi_print_mps_flags(mps_flags);
253 }
254
255 const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
256                              "Local NMI", "Local APIC Override", "IO SAPIC",
257                              "Local SAPIC", "Platform Interrupt" };
258 const char *platform_int_types[] = { "PMI", "INIT",
259                                      "Corrected Platform Error" };
260
261 static void
262 acpi_print_madt(struct MADT_APIC *mp)
263 {
264
265         printf("\tType=%s\n", apic_types[mp->type]);
266         switch (mp->type) {
267         case ACPI_MADT_APIC_TYPE_LOCAL_APIC:
268                 acpi_print_local_apic(mp->body.local_apic.cpu_id,
269                     mp->body.local_apic.apic_id, mp->body.local_apic.flags);
270                 break;
271         case ACPI_MADT_APIC_TYPE_IO_APIC:
272                 acpi_print_io_apic(mp->body.io_apic.apic_id,
273                     mp->body.io_apic.int_base,
274                     mp->body.io_apic.apic_addr);
275                 break;
276         case ACPI_MADT_APIC_TYPE_INT_OVERRIDE:
277                 printf("\tBUS=%d\n", (u_int)mp->body.int_override.bus);
278                 printf("\tIRQ=%d\n", (u_int)mp->body.int_override.source);
279                 acpi_print_intr(mp->body.int_override.intr,
280                     mp->body.int_override.mps_flags);
281                 break;
282         case ACPI_MADT_APIC_TYPE_NMI:
283                 acpi_print_intr(mp->body.nmi.intr, mp->body.nmi.mps_flags);
284                 break;
285         case ACPI_MADT_APIC_TYPE_LOCAL_NMI:
286                 acpi_print_cpu(mp->body.local_nmi.cpu_id);
287                 printf("\tLINT Pin=%d\n", mp->body.local_nmi.lintpin);
288                 acpi_print_mps_flags(mp->body.local_nmi.mps_flags);
289                 break;
290         case ACPI_MADT_APIC_TYPE_LOCAL_OVERRIDE:
291                 printf("\tLocal APIC ADDR=0x%016jx\n",
292                     mp->body.local_apic_override.apic_addr);
293                 break;
294         case ACPI_MADT_APIC_TYPE_IO_SAPIC:
295                 acpi_print_io_apic(mp->body.io_sapic.apic_id,
296                     mp->body.io_sapic.int_base,
297                     mp->body.io_sapic.apic_addr);
298                 break;
299         case ACPI_MADT_APIC_TYPE_LOCAL_SAPIC:
300                 acpi_print_local_apic(mp->body.local_sapic.cpu_id,
301                     mp->body.local_sapic.apic_id, mp->body.local_sapic.flags);
302                 printf("\tAPIC EID=%d\n", (u_int)mp->body.local_sapic.apic_eid);
303                 break;
304         case ACPI_MADT_APIC_TYPE_INT_SRC:
305                 printf("\tType=%s\n",
306                     platform_int_types[mp->body.int_src.type]);
307                 printf("\tCPU ID=%d\n", (u_int)mp->body.int_src.cpu_id);
308                 printf("\tCPU EID=%d\n", (u_int)mp->body.int_src.cpu_id);
309                 printf("\tSAPIC Vector=%d\n",
310                     (u_int)mp->body.int_src.sapic_vector);
311                 acpi_print_intr(mp->body.int_src.intr,
312                     mp->body.int_src.mps_flags);
313                 break;
314         default:
315                 printf("\tUnknown type %d\n", (u_int)mp->type);
316                 break;
317         }
318 }
319
320 static void
321 acpi_handle_madt(struct ACPIsdt *sdp)
322 {
323         struct MADTbody *madtp;
324         struct MADT_APIC *madt_apicp;
325
326         printf(BEGIN_COMMENT);
327         acpi_print_sdt(sdp);
328         madtp = (struct MADTbody *) sdp->body;
329         printf("\tLocal APIC ADDR=0x%08x\n", madtp->lapic_addr);
330         printf("\tFlags={");
331         if (madtp->flags & ACPI_APIC_FLAG_PCAT_COMPAT)
332                 printf("PC-AT");
333         printf("}\n");
334         madt_apicp = (struct MADT_APIC *)madtp->body;
335         while (((uintptr_t)madt_apicp) - ((uintptr_t)sdp) < sdp->len) {
336                 printf("\n");
337                 acpi_print_madt(madt_apicp);
338                 madt_apicp = (struct MADT_APIC *) ((char *)madt_apicp +
339                     madt_apicp->len);
340         }
341         printf(END_COMMENT);
342 }
343
344 static void
345 acpi_handle_hpet(struct ACPIsdt *sdp)
346 {
347         struct HPETbody *hpetp;
348
349         printf(BEGIN_COMMENT);
350         acpi_print_sdt(sdp);
351         hpetp = (struct HPETbody *) sdp->body;
352         printf("\tHPET Number=%d\n", hpetp->hpet_number);
353         printf("\tADDR=0x%08x\n", hpetp->base_addr);
354         printf("\tHW Rev=0x%x\n", hpetp->block_hwrev);
355         printf("\tComparitors=%d\n", hpetp->block_comparitors);
356         printf("\tCounter Size=%d\n", hpetp->block_counter_size);
357         printf("\tLegacy IRQ routing capable={");
358         if (hpetp->block_legacy_capable)
359                 printf("TRUE}\n");
360         else
361                 printf("FALSE}\n");
362         printf("\tPCI Vendor ID=0x%04x\n", hpetp->block_pcivendor);
363         printf("\tMinimal Tick=%d\n", hpetp->clock_tick);
364         printf(END_COMMENT);
365 }
366
367 static void
368 acpi_handle_ecdt(struct ACPIsdt *sdp)
369 {
370         struct ECDTbody *ecdt;
371
372         printf(BEGIN_COMMENT);
373         acpi_print_sdt(sdp);
374         ecdt = (struct ECDTbody *) sdp->body;
375         printf("\tEC_CONTROL=");
376         acpi_print_gas(&ecdt->ec_control);
377         printf("\n\tEC_DATA=");
378         acpi_print_gas(&ecdt->ec_data);
379         printf("\n\tUID=%#x, ", ecdt->uid);
380         printf("GPE_BIT=%#x\n", ecdt->gpe_bit);
381         printf("\tEC_ID=%s\n", ecdt->ec_id);
382         printf(END_COMMENT);
383 }
384
385 static void
386 acpi_handle_mcfg(struct ACPIsdt *sdp)
387 {
388         struct MCFGbody *mcfg;
389         u_int i, e;
390
391         printf(BEGIN_COMMENT);
392         acpi_print_sdt(sdp);
393         mcfg = (struct MCFGbody *) sdp->body;
394
395         e = (sdp->len - ((caddr_t)&mcfg->s[0] - (caddr_t)sdp)) /
396             sizeof(*mcfg->s);
397         for (i = 0; i < e; i++, mcfg++) {
398                 printf("\n");
399                 printf("\tBase Address= 0x%016jx\n", mcfg->s[i].baseaddr);
400                 printf("\tSegment Group= 0x%04x\n", mcfg->s[i].seg_grp);
401                 printf("\tStart Bus= %d\n", mcfg->s[i].start);
402                 printf("\tEnd Bus= %d\n", mcfg->s[i].end);
403         }
404         printf(END_COMMENT);
405 }
406
407 static void
408 acpi_print_srat_memory(struct SRAT_mem_affinity *mp)
409 {
410
411     printf("\tFlags={");
412     printf(mp->flags & ACPI_SRAT_FLAG_MEM_ENABLED ? "ENABLED" : "DISABLED");
413     if (mp->flags & ACPI_SRAT_FLAG_MEM_HOT_PLUGGABLE)
414         printf(",HOT_PLUGGABLE");
415     if (mp->flags & ACPI_SRAT_FLAG_MEM_NON_VOLATILE)
416         printf(",NON_VOLATILE");
417     printf("}\n");
418     printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->base_address);
419     printf("\tLength=0x%016jx\n", (uintmax_t)mp->length);
420     printf("\tProximity Domain=%d\n", mp->proximity_domain);
421 }
422
423 static void
424 acpi_print_srat_cpu(uint32_t apic_id, uint32_t pd, uint32_t flags)
425 {
426     printf("\tFlags={");
427     printf(flags & ACPI_SRAT_FLAG_CPU_ENABLED ? "ENABLED": "DISABLED");
428     printf("}\n");
429     printf("\tAPIC ID=%d\n", apic_id);
430     printf("\tProximity Domain=%d\n", pd);
431 }
432
433 static void
434 acpi_print_srat_cpu_affinity(struct SRAT_cpu_affinity *cpu)
435 {
436     uint32_t pd =
437         cpu->proximity_domain_hi[2] << 24 | cpu->proximity_domain_hi[1] << 16 |
438         cpu->proximity_domain_hi[0] << 0 | cpu->proximity_domain_lo;
439     acpi_print_srat_cpu(cpu->apic_id, pd, cpu->flags);
440 }
441
442 static void
443 acpi_print_srat(struct SRAT_APIC *sp)
444 {
445     const char *srat_types[] = { "CPU", "Memory", "X2APIC" };
446     const unsigned char srat_types_n =
447         sizeof(srat_types) / sizeof(srat_types[0]);
448
449     printf("\tType(%d)=%s\n", sp->type,
450            sp->type < srat_types_n ? srat_types[sp->type] : "(unknown)");
451
452     switch (sp->type) {
453     case ACPI_SRAT_APIC_TYPE_CPU_AFFINITY:
454         acpi_print_srat_cpu_affinity((struct SRAT_cpu_affinity *) &sp->body);
455         break;
456     case ACPI_SRAT_APIC_TYPE_MEMORY_AFFINITY:
457         acpi_print_srat_memory((struct SRAT_mem_affinity *) &sp->body);
458         break;
459     case ACPI_SRAT_APIC_TYPE_X2APIC_CPU_AFFINITY: {
460         struct SRAT_x2apic_cpu_affinity *cpu =
461             (struct SRAT_x2apic_cpu_affinity *) &sp->body;
462         acpi_print_srat_cpu(cpu->apic_id, cpu->proximity_domain, cpu->flags);
463     } break;
464     }
465 }
466
467 static void
468 acpi_handle_srat(struct ACPIsdt *sdp)
469 {
470     struct SRATbody *srat = (struct SRATbody *) sdp->body;
471     struct SRAT_APIC *apic = (struct SRAT_APIC *)srat->body;
472
473     printf(BEGIN_COMMENT);
474     acpi_print_sdt(sdp);
475     printf("\tRevision=0x%04x\n", srat->revision);
476
477     for (; ((uintptr_t) apic) - ((uintptr_t) sdp) < sdp->len;
478          apic = (struct SRAT_APIC *) ((char *) apic + apic->len)) {
479         printf("\n");
480         acpi_print_srat(apic);
481     }
482     printf(END_COMMENT);
483 }
484
485 static void
486 acpi_print_sdt(struct ACPIsdt *sdp)
487 {
488         printf("  ");
489         acpi_print_string(sdp->signature, 4);
490         printf(": Length=%d, Revision=%d, Checksum=%d,\n",
491                sdp->len, sdp->rev, sdp->check);
492         printf("\tOEMID=");
493         acpi_print_string(sdp->oemid, 6);
494         printf(", OEM Table ID=");
495         acpi_print_string(sdp->oemtblid, 8);
496         printf(", OEM Revision=0x%x,\n", sdp->oemrev);
497         printf("\tCreator ID=");
498         acpi_print_string(sdp->creator, 4);
499         printf(", Creator Revision=0x%x\n", sdp->crerev);
500 }
501
502 static void
503 acpi_print_rsdt(struct ACPIsdt *rsdp)
504 {
505         int     i, entries;
506         u_long  addr;
507
508         printf(BEGIN_COMMENT);
509         acpi_print_sdt(rsdp);
510         entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
511         printf("\tEntries={ ");
512         for (i = 0; i < entries; i++) {
513                 if (i > 0)
514                         printf(", ");
515                 switch (addr_size) {
516                 case 4:
517                         addr = le32dec((char*)rsdp->body + i * addr_size);
518                         break;
519                 case 8:
520                         addr = le64dec((char*)rsdp->body + i * addr_size);
521                         break;
522                 default:
523                         addr = 0;
524                 }
525                 printf("0x%08lx", addr);
526         }
527         printf(" }\n");
528         printf(END_COMMENT);
529 }
530
531 static const char *acpi_pm_profiles[] = {
532         "Unspecified", "Desktop", "Mobile", "Workstation",
533         "Enterprise Server", "SOHO Server", "Appliance PC"
534 };
535
536 static void
537 acpi_print_fadt(struct ACPIsdt *sdp)
538 {
539         struct FADTbody *fadt;
540         const char *pm;
541         char        sep;
542
543         fadt = (struct FADTbody *)sdp->body;
544         printf(BEGIN_COMMENT);
545         acpi_print_sdt(sdp);
546         printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->facs_ptr,
547                fadt->dsdt_ptr);
548         printf("\tINT_MODEL=%s\n", fadt->int_model ? "APIC" : "PIC");
549         if (fadt->pm_profile >= sizeof(acpi_pm_profiles) / sizeof(char *))
550                 pm = "Reserved";
551         else
552                 pm = acpi_pm_profiles[fadt->pm_profile];
553         printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->pm_profile);
554         printf("\tSCI_INT=%d\n", fadt->sci_int);
555         printf("\tSMI_CMD=0x%x, ", fadt->smi_cmd);
556         printf("ACPI_ENABLE=0x%x, ", fadt->acpi_enable);
557         printf("ACPI_DISABLE=0x%x, ", fadt->acpi_disable);
558         printf("S4BIOS_REQ=0x%x\n", fadt->s4biosreq);
559         printf("\tPSTATE_CNT=0x%x\n", fadt->pstate_cnt);
560         printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
561                fadt->pm1a_evt_blk,
562                fadt->pm1a_evt_blk + fadt->pm1_evt_len - 1);
563         if (fadt->pm1b_evt_blk != 0)
564                 printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
565                        fadt->pm1b_evt_blk,
566                        fadt->pm1b_evt_blk + fadt->pm1_evt_len - 1);
567         printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
568                fadt->pm1a_cnt_blk,
569                fadt->pm1a_cnt_blk + fadt->pm1_cnt_len - 1);
570         if (fadt->pm1b_cnt_blk != 0)
571                 printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
572                        fadt->pm1b_cnt_blk,
573                        fadt->pm1b_cnt_blk + fadt->pm1_cnt_len - 1);
574         if (fadt->pm2_cnt_blk != 0)
575                 printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
576                        fadt->pm2_cnt_blk,
577                        fadt->pm2_cnt_blk + fadt->pm2_cnt_len - 1);
578         printf("\tPM_TMR_BLK=0x%x-0x%x\n",
579                fadt->pm_tmr_blk,
580                fadt->pm_tmr_blk + fadt->pm_tmr_len - 1);
581         if (fadt->gpe0_blk != 0)
582                 printf("\tGPE0_BLK=0x%x-0x%x\n",
583                        fadt->gpe0_blk,
584                        fadt->gpe0_blk + fadt->gpe0_len - 1);
585         if (fadt->gpe1_blk != 0)
586                 printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
587                        fadt->gpe1_blk,
588                        fadt->gpe1_blk + fadt->gpe1_len - 1,
589                        fadt->gpe1_base);
590         if (fadt->cst_cnt != 0)
591                 printf("\tCST_CNT=0x%x\n", fadt->cst_cnt);
592         printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
593                fadt->p_lvl2_lat, fadt->p_lvl3_lat);
594         printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
595                fadt->flush_size, fadt->flush_stride);
596         printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
597                fadt->duty_off, fadt->duty_width);
598         printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
599                fadt->day_alrm, fadt->mon_alrm, fadt->century);
600
601 #define PRINTFLAG(var, flag) do {                       \
602         if ((var) & FADT_FLAG_## flag) {                \
603                 printf("%c%s", sep, #flag); sep = ',';  \
604         }                                               \
605 } while (0)
606
607         printf("\tIAPC_BOOT_ARCH=");
608         sep = '{';
609         PRINTFLAG(fadt->iapc_boot_arch, LEGACY_DEV);
610         PRINTFLAG(fadt->iapc_boot_arch, 8042);
611         if (fadt->iapc_boot_arch != 0)
612                 printf("}");
613         printf("\n");
614
615         printf("\tFlags=");
616         sep = '{';
617         PRINTFLAG(fadt->flags, WBINVD);
618         PRINTFLAG(fadt->flags, WBINVD_FLUSH);
619         PRINTFLAG(fadt->flags, PROC_C1);
620         PRINTFLAG(fadt->flags, P_LVL2_UP);
621         PRINTFLAG(fadt->flags, PWR_BUTTON);
622         PRINTFLAG(fadt->flags, SLP_BUTTON);
623         PRINTFLAG(fadt->flags, FIX_RTC);
624         PRINTFLAG(fadt->flags, RTC_S4);
625         PRINTFLAG(fadt->flags, TMR_VAL_EXT);
626         PRINTFLAG(fadt->flags, DCK_CAP);
627         PRINTFLAG(fadt->flags, RESET_REG);
628         PRINTFLAG(fadt->flags, SEALED_CASE);
629         PRINTFLAG(fadt->flags, HEADLESS);
630         PRINTFLAG(fadt->flags, CPU_SW_SLP);
631     PRINTFLAG(fadt->flags, PCI_EXPRESS_WAKE);
632     PRINTFLAG(fadt->flags, PLATFORM_CLOCK);
633     PRINTFLAG(fadt->flags, S4_RTC_VALID);
634     PRINTFLAG(fadt->flags, REMOTE_POWER_ON);
635     PRINTFLAG(fadt->flags, APIC_CLUSTER);
636     PRINTFLAG(fadt->flags, APIC_PHYSICAL);
637         if (fadt->flags != 0)
638                 printf("}\n");
639
640 #undef PRINTFLAG
641
642         if (fadt->flags & FADT_FLAG_RESET_REG) {
643                 printf("\tRESET_REG=");
644                 acpi_print_gas(&fadt->reset_reg);
645                 printf(", RESET_VALUE=%#x\n", fadt->reset_value);
646         }
647         if (acpi_get_fadt_revision(fadt) > 1) {
648                 printf("\tX_FACS=0x%08lx, ", (u_long)fadt->x_facs_ptr);
649                 printf("X_DSDT=0x%08lx\n", (u_long)fadt->x_dsdt_ptr);
650                 printf("\tX_PM1a_EVT_BLK=");
651                 acpi_print_gas(&fadt->x_pm1a_evt_blk);
652                 if (fadt->x_pm1b_evt_blk.address != 0) {
653                         printf("\n\tX_PM1b_EVT_BLK=");
654                         acpi_print_gas(&fadt->x_pm1b_evt_blk);
655                 }
656                 printf("\n\tX_PM1a_CNT_BLK=");
657                 acpi_print_gas(&fadt->x_pm1a_cnt_blk);
658                 if (fadt->x_pm1b_cnt_blk.address != 0) {
659                         printf("\n\tX_PM1b_CNT_BLK=");
660                         acpi_print_gas(&fadt->x_pm1b_cnt_blk);
661                 }
662                 if (fadt->x_pm1b_cnt_blk.address != 0) {
663                         printf("\n\tX_PM2_CNT_BLK=");
664                         acpi_print_gas(&fadt->x_pm2_cnt_blk);
665                 }
666                 printf("\n\tX_PM_TMR_BLK=");
667                 acpi_print_gas(&fadt->x_pm_tmr_blk);
668                 if (fadt->x_gpe0_blk.address != 0) {
669                         printf("\n\tX_GPE0_BLK=");
670                         acpi_print_gas(&fadt->x_gpe0_blk);
671                 }
672                 if (fadt->x_gpe1_blk.address != 0) {
673                         printf("\n\tX_GPE1_BLK=");
674                         acpi_print_gas(&fadt->x_gpe1_blk);
675                 }
676                 printf("\n");
677         }
678
679         printf(END_COMMENT);
680 }
681
682 static void
683 acpi_print_facs(struct FACSbody *facs)
684 {
685         printf(BEGIN_COMMENT);
686         printf("  FACS:\tLength=%u, ", facs->len);
687         printf("HwSig=0x%08x, ", facs->hw_sig);
688         printf("Firm_Wake_Vec=0x%08x\n", facs->firm_wake_vec);
689
690         printf("\tGlobal_Lock=");
691         if (facs->global_lock != 0) {
692                 if (facs->global_lock & FACS_FLAG_LOCK_PENDING)
693                         printf("PENDING,");
694                 if (facs->global_lock & FACS_FLAG_LOCK_OWNED)
695                         printf("OWNED");
696         }
697         printf("\n");
698
699         printf("\tFlags=");
700         if (facs->flags & FACS_FLAG_S4BIOS_F)
701                 printf("S4BIOS");
702         printf("\n");
703
704         if (facs->x_firm_wake_vec != 0) {
705                 printf("\tX_Firm_Wake_Vec=%08lx\n",
706                        (u_long)facs->x_firm_wake_vec);
707         }
708         printf("\tVersion=%u\n", facs->version);
709
710         printf(END_COMMENT);
711 }
712
713 static void
714 acpi_print_dsdt(struct ACPIsdt *dsdp)
715 {
716         printf(BEGIN_COMMENT);
717         acpi_print_sdt(dsdp);
718         printf(END_COMMENT);
719 }
720
721 int
722 acpi_checksum(void *p, size_t length)
723 {
724         u_int8_t        *bp;
725         u_int8_t        sum;
726
727         bp = p;
728         sum = 0;
729         while (length--)
730                 sum += *bp++;
731
732         return (sum);
733 }
734
735 static struct ACPIsdt *
736 acpi_map_sdt(vm_offset_t pa)
737 {
738         struct  ACPIsdt *sp;
739
740         sp = acpi_map_physical(pa, sizeof(struct ACPIsdt));
741         sp = acpi_map_physical(pa, sp->len);
742         return (sp);
743 }
744
745 static void
746 acpi_print_rsd_ptr(struct ACPIrsdp *rp)
747 {
748         printf(BEGIN_COMMENT);
749         printf("  RSD PTR: OEM=");
750         acpi_print_string(rp->oem, 6);
751         printf(", ACPI_Rev=%s (%d)\n", rp->revision < 2 ? "1.0x" : "2.0x",
752                rp->revision);
753         if (rp->revision < 2) {
754                 printf("\tRSDT=0x%08x, cksum=%u\n", rp->rsdt_addr, rp->sum);
755         } else {
756                 printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
757                     (u_long)rp->xsdt_addr, rp->length, rp->xsum);
758         }
759         printf(END_COMMENT);
760 }
761
762 static void
763 acpi_handle_rsdt(struct ACPIsdt *rsdp)
764 {
765         struct ACPIsdt *sdp;
766         vm_offset_t addr;
767         int entries, i;
768
769         acpi_print_rsdt(rsdp);
770         entries = (rsdp->len - SIZEOF_SDT_HDR) / addr_size;
771         for (i = 0; i < entries; i++) {
772                 switch (addr_size) {
773                 case 4:
774                         addr = le32dec((char*)rsdp->body + i * addr_size);
775                         break;
776                 case 8:
777                         addr = le64dec((char*)rsdp->body + i * addr_size);
778                         break;
779                 default:
780                         printf("invalid addr_size: %d", addr_size);
781                         continue;
782                 }
783
784                 sdp = (struct ACPIsdt *)acpi_map_sdt(addr);
785                 if (acpi_checksum(sdp, sdp->len)) {
786                         warnx("RSDT entry %d (sig %.4s) is corrupt", i,
787                             sdp->signature);
788                         continue;
789                 }
790                 if (!memcmp(sdp->signature, "FACP", 4))
791                         acpi_handle_fadt(sdp);
792                 else if (!memcmp(sdp->signature, "APIC", 4))
793                         acpi_handle_madt(sdp);
794                 else if (!memcmp(sdp->signature, "HPET", 4))
795                         acpi_handle_hpet(sdp);
796                 else if (!memcmp(sdp->signature, "ECDT", 4))
797                         acpi_handle_ecdt(sdp);
798                 else if (!memcmp(sdp->signature, "MCFG", 4))
799                         acpi_handle_mcfg(sdp);
800         else if (!memcmp(sdp->signature, "SRAT", 4))
801             acpi_handle_srat(sdp);
802                 else {
803                         printf(BEGIN_COMMENT);
804                         acpi_print_sdt(sdp);
805                         printf(END_COMMENT);
806                 }
807         }
808 }
809
810 struct ACPIsdt *
811 sdt_load_devmem(void)
812 {
813         struct  ACPIrsdp *rp;
814         struct  ACPIsdt *rsdp;
815
816         rp = acpi_find_rsd_ptr();
817         if (!rp)
818                 errx(1, "Can't find ACPI information");
819
820         if (tflag)
821                 acpi_print_rsd_ptr(rp);
822         if (rp->revision < 2) {
823                 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->rsdt_addr);
824                 if (memcmp(rsdp->signature, "RSDT", 4) != 0 ||
825                     acpi_checksum(rsdp, rsdp->len) != 0)
826                         errx(1, "RSDT is corrupted");
827                 addr_size = sizeof(uint32_t);
828         } else {
829                 rsdp = (struct ACPIsdt *)acpi_map_sdt(rp->xsdt_addr);
830                 if (memcmp(rsdp->signature, "XSDT", 4) != 0 ||
831                     acpi_checksum(rsdp, rsdp->len) != 0)
832                         errx(1, "XSDT is corrupted");
833                 addr_size = sizeof(uint64_t);
834         }
835         return (rsdp);
836 }
837
838 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
839 static int
840 write_dsdt(int fd, struct ACPIsdt *rsdt, struct ACPIsdt *dsdt)
841 {
842         struct ACPIsdt sdt;
843         struct ACPIsdt *ssdt;
844         uint8_t sum;
845
846         /* Create a new checksum to account for the DSDT and any SSDTs. */
847         sdt = *dsdt;
848         if (rsdt != NULL) {
849                 sdt.check = 0;
850                 sum = acpi_checksum(dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
851                 ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
852                 while (ssdt != NULL) {
853                         sdt.len += ssdt->len - SIZEOF_SDT_HDR;
854                         sum += acpi_checksum(ssdt->body,
855                             ssdt->len - SIZEOF_SDT_HDR);
856                         ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
857                 }
858                 sum += acpi_checksum(&sdt, SIZEOF_SDT_HDR);
859                 sdt.check -= sum;
860         }
861
862         /* Write out the DSDT header and body. */
863         write(fd, &sdt, SIZEOF_SDT_HDR);
864         write(fd, dsdt->body, dsdt->len - SIZEOF_SDT_HDR);
865
866         /* Write out any SSDTs (if present.) */
867         if (rsdt != NULL) {
868                 ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
869                 while (ssdt != NULL) {
870                         write(fd, ssdt->body, ssdt->len - SIZEOF_SDT_HDR);
871                         ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
872                 }
873         }
874         return (0);
875 }
876
877 void
878 dsdt_save_file(char *outfile, struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
879 {
880         int     fd;
881         mode_t  mode;
882
883         assert(outfile != NULL);
884         mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
885         fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
886         if (fd == -1) {
887                 perror("dsdt_save_file");
888                 return;
889         }
890         write_dsdt(fd, rsdt, dsdp);
891         close(fd);
892 }
893
894 void
895 aml_disassemble(struct ACPIsdt *rsdt, struct ACPIsdt *dsdp)
896 {
897         char buf[PATH_MAX], tmpstr[PATH_MAX];
898         const char *tmpdir;
899         char *tmpext;
900         FILE *fp;
901         size_t len;
902         int fd;
903
904         tmpdir = getenv("TMPDIR");
905         if (tmpdir == NULL)
906                 tmpdir = _PATH_TMP;
907         strncpy(tmpstr, tmpdir, sizeof(tmpstr));
908         strncat(tmpstr, "/acpidump.", sizeof(tmpstr) - strlen(tmpdir));
909         if (realpath(tmpstr, buf) == NULL) {
910                 perror("realpath tmp file");
911                 return;
912         }
913         strncpy(tmpstr, buf, sizeof(tmpstr));
914         len = strlen(buf);
915         tmpext = tmpstr + len;
916         strncpy(tmpext, "XXXXXX", sizeof(tmpstr) - len);
917         fd = mkstemp(tmpstr);
918         if (fd < 0) {
919                 perror("iasl tmp file");
920                 return;
921         }
922         write_dsdt(fd, rsdt, dsdp);
923         close(fd);
924
925         /* Run iasl -d on the temp file */
926         if (fork() == 0) {
927                 close(STDOUT_FILENO);
928                 if (vflag == 0)
929                         close(STDERR_FILENO);
930                 execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
931                 err(1, "exec");
932         }
933
934         wait(NULL);
935         unlink(tmpstr);
936
937         /* Dump iasl's output to stdout */
938         strncpy(tmpext, "dsl", sizeof(tmpstr) - len);
939         fp = fopen(tmpstr, "r");
940         unlink(tmpstr);
941         if (fp == NULL) {
942                 perror("iasl tmp file (read)");
943                 return;
944         }
945         while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
946                 fwrite(buf, 1, len, stdout);
947         fclose(fp);
948 }
949
950 void
951 sdt_print_all(struct ACPIsdt *rsdp)
952 {
953         acpi_handle_rsdt(rsdp);
954 }
955
956 /* Fetch a table matching the given signature via the RSDT. */
957 struct ACPIsdt *
958 sdt_from_rsdt(struct ACPIsdt *rsdt, const char *sig, struct ACPIsdt *last)
959 {
960         struct ACPIsdt *sdt;
961         vm_offset_t addr;
962         int entries, i;
963
964         entries = (rsdt->len - SIZEOF_SDT_HDR) / addr_size;
965         for (i = 0; i < entries; i++) {
966                 switch (addr_size) {
967                 case 4:
968                         addr = le32dec((char*)rsdt->body + i * addr_size);
969                         break;
970                 case 8:
971                         addr = le64dec((char*)rsdt->body + i * addr_size);
972                         break;
973                 default:
974                         assert((addr = 0));
975                 }
976                 sdt = (struct ACPIsdt *)acpi_map_sdt(addr);
977                 if (last != NULL) {
978                         if (sdt == last)
979                                 last = NULL;
980                         continue;
981                 }
982                 if (memcmp(sdt->signature, sig, strlen(sig)))
983                         continue;
984                 if (acpi_checksum(sdt, sdt->len))
985                         errx(1, "RSDT entry %d is corrupt", i);
986                 return (sdt);
987         }
988
989         return (NULL);
990 }
991
992 struct ACPIsdt *
993 dsdt_from_fadt(struct FADTbody *fadt)
994 {
995         struct  ACPIsdt *sdt;
996
997         /* Use the DSDT address if it is version 1, otherwise use X_DSDT. */
998         if (acpi_get_fadt_revision(fadt) == 1)
999                 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->dsdt_ptr);
1000         else
1001                 sdt = (struct ACPIsdt *)acpi_map_sdt(fadt->x_dsdt_ptr);
1002         if (acpi_checksum(sdt, sdt->len))
1003                 errx(1, "DSDT is corrupt\n");
1004         return (sdt);
1005 }