Merge from vendor branch GDB:
[dragonfly.git] / usr.sbin / acpi / acpidb / acpidb.c
1 /*-
2  * Copyright (c) 2000-2002 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/usr.sbin/acpi/acpidb/acpidb.c,v 1.1 2003/08/07 16:51:50 njl Exp $
27  *      $DragonFly: src/usr.sbin/acpi/acpidb/acpidb.c,v 1.1 2004/07/05 00:22:43 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #include <assert.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include <acpi.h>
46 #include <acnamesp.h>
47 #include <acdebug.h>
48
49 /*
50  * Dummy DSDT Table Header
51  */
52
53 ACPI_TABLE_HEADER       dummy_dsdt_table = {
54         "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
55 };
56
57 /*
58  * Region space I/O routines on virtual machine
59  */
60
61 int     aml_debug_prompt = 1;
62
63 struct ACPIRegionContent {
64         TAILQ_ENTRY(ACPIRegionContent) links;
65         int                     regtype;
66         ACPI_PHYSICAL_ADDRESS   addr;
67         UINT8                   value;
68 };
69
70 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
71 struct  ACPIRegionContentList RegionContentList;
72
73 static int               aml_simulation_initialized = 0;
74
75 static void              aml_simulation_init(void);
76 static int               aml_simulate_regcontent_add(int regtype,
77                              ACPI_PHYSICAL_ADDRESS addr,
78                              UINT8 value);
79 static int               aml_simulate_regcontent_read(int regtype,
80                              ACPI_PHYSICAL_ADDRESS addr,
81                              UINT8 *valuep); 
82 static int               aml_simulate_regcontent_write(int regtype,
83                              ACPI_PHYSICAL_ADDRESS addr,
84                              UINT8 *valuep);
85 static ACPI_INTEGER      aml_simulate_prompt(char *msg, ACPI_INTEGER def_val);
86 static void              aml_simulation_regload(const char *dumpfile);
87 static void              aml_simulation_regdump(const char *dumpfile);
88
89 static void
90 aml_simulation_init(void)
91 {
92
93         aml_simulation_initialized = 1;
94         TAILQ_INIT(&RegionContentList);
95         aml_simulation_regload("region.ini");
96 }
97
98 static int
99 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
100 {
101         struct  ACPIRegionContent *rc;
102
103         rc = malloc(sizeof(struct ACPIRegionContent));
104         if (rc == NULL) {
105                 return (-1);    /* malloc fail */
106         }
107         rc->regtype = regtype;
108         rc->addr = addr;
109         rc->value = value;
110
111         TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
112         return (0);
113 }
114
115 static int
116 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
117 {
118         struct  ACPIRegionContent *rc;
119
120         if (!aml_simulation_initialized) {
121                 aml_simulation_init();
122         }
123         TAILQ_FOREACH(rc, &RegionContentList, links) {
124                 if (rc->regtype == regtype && rc->addr == addr) {
125                         *valuep = rc->value;
126                         return (1);     /* found */
127                 }
128         }
129
130         *valuep = 0;
131         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
132 }
133
134 static int
135 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
136 {
137         struct  ACPIRegionContent *rc;
138
139         if (!aml_simulation_initialized) {
140                 aml_simulation_init();
141         }
142         TAILQ_FOREACH(rc, &RegionContentList, links) {
143                 if (rc->regtype == regtype && rc->addr == addr) {
144                         rc->value = *valuep;
145                         return (1);     /* exists */
146                 }
147         }
148
149         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
150 }
151
152 static ACPI_INTEGER
153 aml_simulate_prompt(char *msg, ACPI_INTEGER def_val)
154 {
155         char            buf[16], *ep;
156         ACPI_INTEGER    val;
157
158         val = def_val;
159         printf("DEBUG");
160         if (msg != NULL) {
161                 printf("%s", msg);
162         }
163         printf("(default: 0x%x ", val);
164         printf(" / %u) >>", val);
165         fflush(stdout);
166
167         bzero(buf, sizeof buf);
168         while (1) {
169                 if (read(0, buf, sizeof buf) == 0) {
170                         continue;
171                 }
172                 if (buf[0] == '\n') {
173                         break;  /* use default value */
174                 }
175                 if (buf[0] == '0' && buf[1] == 'x') {
176                         val = strtoq(buf, &ep, 16);
177                 } else {
178                         val = strtoq(buf, &ep, 10);
179                 }
180                 break;
181         }
182         return (val);
183 }
184
185 static void
186 aml_simulation_regload(const char *dumpfile)
187 {
188         char    buf[256], *np, *ep;
189         struct  ACPIRegionContent rc;
190         FILE    *fp;
191
192         if (!aml_simulation_initialized) {
193                 return;
194         }
195
196         if ((fp = fopen(dumpfile, "r")) == NULL) {
197                 return;
198         }
199
200         while (fgets(buf, sizeof buf, fp) != NULL) {
201                 np = buf;
202                 /* reading region type */
203                 rc.regtype = strtoq(np, &ep, 10);
204                 if (np == ep) {
205                         continue;
206                 }
207                 np = ep;
208
209                 /* reading address */
210                 rc.addr = strtoq(np, &ep, 16);
211                 if (np == ep) {
212                         continue;
213                 }
214                 np = ep;
215
216                 /* reading value */
217                 rc.value = strtoq(np, &ep, 16);
218                 if (np == ep) {
219                         continue;
220                 }
221                 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
222         }
223
224         fclose(fp);
225 }
226
227 static void
228 aml_simulation_regdump(const char *dumpfile)
229 {
230         struct  ACPIRegionContent *rc;
231         FILE    *fp;
232
233         if (!aml_simulation_initialized) {
234                 return;
235         }
236         if ((fp = fopen(dumpfile, "w")) == NULL) {
237                 warn("%s", dumpfile);
238                 return;
239         }
240         while (!TAILQ_EMPTY(&RegionContentList)) {
241                 rc = TAILQ_FIRST(&RegionContentList);
242                 fprintf(fp, "%d 0x%x    0x%x\n",
243                     rc->regtype, rc->addr, rc->value);
244                 TAILQ_REMOVE(&RegionContentList, rc, links);
245                 free(rc);
246         }
247
248         fclose(fp);
249         TAILQ_INIT(&RegionContentList);
250 }
251
252 /*
253  * Space handlers on virtual machine
254  */
255
256 static ACPI_STATUS
257 aml_vm_space_handler(
258         UINT32                  SpaceID,
259         UINT32                  Function,
260         ACPI_PHYSICAL_ADDRESS   Address,
261         UINT32                  BitWidth,
262         ACPI_INTEGER            *Value,
263         int                     Prompt)
264 {
265         int             state;
266         UINT8           val;
267         ACPI_INTEGER    value, i;
268         char            msg[256];
269         static char     *space_names[] = {
270                 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
271                 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
272
273         switch (Function) {
274         case ACPI_READ:
275                 value = 0;
276                 for (i = 0; (i * 8) < BitWidth; i++) {
277                         state = aml_simulate_regcontent_read(SpaceID,
278                                                              Address + i, &val);
279                         if (state == -1) {
280                                 return (AE_NO_MEMORY);
281                         }
282                         value |= val << (i * 8);
283                 }
284                 *Value = value;
285                 if (Prompt) {
286                         sprintf(msg, "[read (%s, %2d, 0x%x)]",
287                                 space_names[SpaceID], BitWidth, Address);
288                         *Value = aml_simulate_prompt(msg, value);
289                         if (*Value != value) {
290                                 return(aml_vm_space_handler(SpaceID,
291                                                 ACPI_WRITE,
292                                                 Address, BitWidth, Value, 0));
293                         }
294                 }
295                 break;
296
297         case ACPI_WRITE:
298                 value = *Value;
299                 if (Prompt) {
300                         sprintf(msg, "[write(%s, %2d, 0x%x)]",
301                                 space_names[SpaceID], BitWidth, Address);
302                         value = aml_simulate_prompt(msg, *Value);
303                 }
304                 *Value = value;
305                 for (i = 0; (i * 8) < BitWidth; i++) {
306                         val = value & 0xff;
307                         state = aml_simulate_regcontent_write(SpaceID,
308                                                               Address + i, &val);
309                         if (state == -1) {
310                                 return (AE_NO_MEMORY);
311                         }
312                         value = value >> 8;
313                 }
314         }
315
316         return (AE_OK);
317 }
318
319 #define DECLARE_VM_SPACE_HANDLER(name, id);                     \
320 static ACPI_STATUS                                              \
321 aml_vm_space_handler_##name (                                   \
322         UINT32                  Function,                       \
323         ACPI_PHYSICAL_ADDRESS   Address,                        \
324         UINT32                  BitWidth,                       \
325         ACPI_INTEGER            *Value,                         \
326         void                    *HandlerContext,                \
327         void                    *RegionContext)                 \
328 {                                                               \
329         return (aml_vm_space_handler(id, Function, Address,     \
330                 BitWidth, Value, aml_debug_prompt));            \
331 }
332
333 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
334 DECLARE_VM_SPACE_HANDLER(system_io,     ACPI_ADR_SPACE_SYSTEM_IO);
335 DECLARE_VM_SPACE_HANDLER(pci_config,    ACPI_ADR_SPACE_PCI_CONFIG);
336 DECLARE_VM_SPACE_HANDLER(ec,            ACPI_ADR_SPACE_EC);
337 DECLARE_VM_SPACE_HANDLER(smbus,         ACPI_ADR_SPACE_SMBUS);
338 DECLARE_VM_SPACE_HANDLER(cmos,          ACPI_ADR_SPACE_CMOS);
339 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
340
341 /*
342  * Load DSDT data file and invoke debugger
343  */
344
345 static UINT32   DummyGlobalLock;
346
347 static int
348 load_dsdt(const char *dsdtfile)
349 {
350         char                    filetmp[PATH_MAX];
351         u_int8_t                *code, *amlptr;
352         struct stat              sb;
353         int                      fd, fd2;
354         int                      error;
355         ACPI_TABLE_HEADER       *tableptr;
356
357         fd = open(dsdtfile, O_RDONLY, 0);
358         if (fd == -1) {
359                 perror("open");
360                 return (-1);
361         }
362         if (fstat(fd, &sb) == -1) {
363                 perror("fstat");
364                 return (-1);
365         }
366         code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
367         if (code == NULL) {
368                 perror("mmap");
369                 return (-1);
370         }
371         if ((error = AcpiInitializeSubsystem()) != AE_OK) {
372                 return (-1);
373         }
374
375         /*
376          * make sure DSDT data contains table header or not.
377          */
378         if (strncmp((char *)code, "DSDT", 4) == 0) {
379                 strncpy(filetmp, dsdtfile, sizeof(filetmp));
380         } else {
381                 mode_t  mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
382                 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
383                 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
384                 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
385                 if (fd2 == -1) {
386                         perror("open");
387                         return (-1);
388                 }
389                 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
390
391                 write(fd2, code, sb.st_size);
392                 close(fd2);
393         }
394
395         /*
396          * Install the virtual machine version of address space handlers.
397          */
398         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
399                         ACPI_ADR_SPACE_SYSTEM_MEMORY,
400                         aml_vm_space_handler_system_memory,
401                         NULL, NULL)) != AE_OK) {
402                 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
403                 return (-1);
404         }
405         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
406                         ACPI_ADR_SPACE_SYSTEM_IO,
407                         aml_vm_space_handler_system_io,
408                         NULL, NULL)) != AE_OK) {
409                 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
410                 return (-1);
411         }
412         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
413                         ACPI_ADR_SPACE_PCI_CONFIG,
414                         aml_vm_space_handler_pci_config,
415                         NULL, NULL)) != AE_OK) {
416                 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
417                 return (-1);
418         }
419         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
420                         ACPI_ADR_SPACE_EC,
421                         aml_vm_space_handler_ec,
422                         NULL, NULL)) != AE_OK) {
423                 fprintf(stderr, "could not initialise EC handler: %d\n", error);
424                 return (-1);
425         }
426         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
427                         ACPI_ADR_SPACE_SMBUS,
428                         aml_vm_space_handler_smbus,
429                         NULL, NULL)) != AE_OK) {
430                 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
431                 return (-1);
432         }
433         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
434                         ACPI_ADR_SPACE_CMOS,
435                         aml_vm_space_handler_cmos,
436                         NULL, NULL)) != AE_OK) {
437                 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
438                 return (-1);
439         }
440         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
441                         ACPI_ADR_SPACE_PCI_BAR_TARGET,
442                         aml_vm_space_handler_pci_bar_target,
443                         NULL, NULL)) != AE_OK) {
444                 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
445                 return (-1);
446         }
447
448         AcpiGbl_FACS = malloc(sizeof (ACPI_COMMON_FACS));
449         if (AcpiGbl_FACS == NULL) {
450                 fprintf(stderr, "could not allocate memory for FACS\n");
451                 return (-1);
452         }
453         DummyGlobalLock = 0;
454         AcpiGbl_CommonFACS.GlobalLock = &DummyGlobalLock;
455         AcpiGbl_GlobalLockPresent = TRUE;
456
457         AcpiDbGetTableFromFile(filetmp, NULL);
458         AcpiUtSetIntegerWidth (AcpiGbl_DSDT->Revision);
459
460         AcpiDbInitialize();
461         AcpiGbl_DebuggerConfiguration = 0;
462         AcpiDbUserCommands(':', NULL);
463
464         if (strcmp(dsdtfile, filetmp) != 0) {
465                 unlink(filetmp);
466         }
467
468         return (0);
469 }
470
471 static void
472 usage(const char *progname)
473 {
474
475         printf("usage: %s dsdt_file\n", progname);
476         exit(1);
477 }
478
479 int
480 main(int argc, char *argv[])
481 {
482         char    *progname;
483
484         progname = argv[0];
485
486         if (argc == 1) {
487                 usage(progname);
488         }
489
490         AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
491
492         aml_simulation_regload("region.ini");
493         if (load_dsdt(argv[1]) == 0) {
494                 aml_simulation_regdump("region.dmp");
495         }
496
497         return (0);
498 }