2 * Copyright (c) 2000-2002 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.2 2007/01/17 17:31:19 y0netan1 Exp $
30 #include <sys/param.h>
31 #include <sys/queue.h>
34 #include <sys/types.h>
50 * Dummy DSDT Table Header
53 ACPI_TABLE_HEADER dummy_dsdt_table = {
54 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
58 * Region space I/O routines on virtual machine
61 int aml_debug_prompt = 1;
63 struct ACPIRegionContent {
64 TAILQ_ENTRY(ACPIRegionContent) links;
66 ACPI_PHYSICAL_ADDRESS addr;
70 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
71 struct ACPIRegionContentList RegionContentList;
73 static int aml_simulation_initialized = 0;
75 static void aml_simulation_init(void);
76 static int aml_simulate_regcontent_add(int regtype,
77 ACPI_PHYSICAL_ADDRESS addr,
79 static int aml_simulate_regcontent_read(int regtype,
80 ACPI_PHYSICAL_ADDRESS addr,
82 static int aml_simulate_regcontent_write(int regtype,
83 ACPI_PHYSICAL_ADDRESS addr,
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);
90 aml_simulation_init(void)
93 aml_simulation_initialized = 1;
94 TAILQ_INIT(&RegionContentList);
95 aml_simulation_regload("region.ini");
99 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
101 struct ACPIRegionContent *rc;
103 rc = malloc(sizeof(struct ACPIRegionContent));
105 return (-1); /* malloc fail */
107 rc->regtype = regtype;
111 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
116 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
118 struct ACPIRegionContent *rc;
120 if (!aml_simulation_initialized) {
121 aml_simulation_init();
123 TAILQ_FOREACH(rc, &RegionContentList, links) {
124 if (rc->regtype == regtype && rc->addr == addr) {
126 return (1); /* found */
131 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
135 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
137 struct ACPIRegionContent *rc;
139 if (!aml_simulation_initialized) {
140 aml_simulation_init();
142 TAILQ_FOREACH(rc, &RegionContentList, links) {
143 if (rc->regtype == regtype && rc->addr == addr) {
145 return (1); /* exists */
149 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
153 aml_simulate_prompt(char *msg, ACPI_INTEGER def_val)
163 printf("(default: 0x%x ", val);
164 printf(" / %u) >>", val);
167 bzero(buf, sizeof buf);
169 if (read(0, buf, sizeof buf) == 0) {
172 if (buf[0] == '\n') {
173 break; /* use default value */
175 if (buf[0] == '0' && buf[1] == 'x') {
176 val = strtoq(buf, &ep, 16);
178 val = strtoq(buf, &ep, 10);
186 aml_simulation_regload(const char *dumpfile)
188 char buf[256], *np, *ep;
189 struct ACPIRegionContent rc;
192 if (!aml_simulation_initialized) {
196 if ((fp = fopen(dumpfile, "r")) == NULL) {
200 while (fgets(buf, sizeof buf, fp) != NULL) {
202 /* reading region type */
203 rc.regtype = strtoq(np, &ep, 10);
209 /* reading address */
210 rc.addr = strtoq(np, &ep, 16);
217 rc.value = strtoq(np, &ep, 16);
221 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
228 aml_simulation_regdump(const char *dumpfile)
230 struct ACPIRegionContent *rc;
233 if (!aml_simulation_initialized) {
236 if ((fp = fopen(dumpfile, "w")) == NULL) {
237 warn("%s", dumpfile);
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);
249 TAILQ_INIT(&RegionContentList);
253 * Space handlers on virtual machine
257 aml_vm_space_handler(
260 ACPI_PHYSICAL_ADDRESS Address,
267 ACPI_INTEGER value, i;
269 static char *space_names[] = {
270 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
271 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
276 for (i = 0; (i * 8) < BitWidth; i++) {
277 state = aml_simulate_regcontent_read(SpaceID,
280 return (AE_NO_MEMORY);
282 value |= val << (i * 8);
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,
292 Address, BitWidth, Value, 0));
300 sprintf(msg, "[write(%s, %2d, 0x%x)]",
301 space_names[SpaceID], BitWidth, Address);
302 value = aml_simulate_prompt(msg, *Value);
305 for (i = 0; (i * 8) < BitWidth; i++) {
307 state = aml_simulate_regcontent_write(SpaceID,
310 return (AE_NO_MEMORY);
319 #define DECLARE_VM_SPACE_HANDLER(name, id); \
321 aml_vm_space_handler_##name ( \
323 ACPI_PHYSICAL_ADDRESS Address, \
325 ACPI_INTEGER *Value, \
326 void *HandlerContext, \
327 void *RegionContext) \
329 return (aml_vm_space_handler(id, Function, Address, \
330 BitWidth, Value, aml_debug_prompt)); \
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);
341 static u_int8_t *mapped_rsdp;
342 #define ACPI_MAX_INIT_TABLES (16)
343 static ACPI_TABLE_DESC Tables[ACPI_MAX_INIT_TABLES];
346 * Load DSDT data file and invoke debugger
350 load_dsdt(const char *dsdtfile)
352 char filetmp[PATH_MAX];
353 u_int8_t *code, *amlptr;
357 ACPI_TABLE_HEADER *Table;
359 fd = open(dsdtfile, O_RDONLY, 0);
364 if (fstat(fd, &sb) == -1) {
368 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
374 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
375 err(1, "AcpiInitializeSubsystem returned %d", error);
380 * make sure DSDT data contains table header or not.
382 if (strncmp((char *)code, "DSDT", 4) == 0) {
383 strncpy(filetmp, dsdtfile, sizeof(filetmp));
385 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
386 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
387 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
388 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
393 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
395 write(fd2, code, sb.st_size);
400 * Install the virtual machine version of address space handlers.
402 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
403 ACPI_ADR_SPACE_SYSTEM_MEMORY,
404 aml_vm_space_handler_system_memory,
405 NULL, NULL)) != AE_OK) {
406 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
409 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
410 ACPI_ADR_SPACE_SYSTEM_IO,
411 aml_vm_space_handler_system_io,
412 NULL, NULL)) != AE_OK) {
413 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
416 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
417 ACPI_ADR_SPACE_PCI_CONFIG,
418 aml_vm_space_handler_pci_config,
419 NULL, NULL)) != AE_OK) {
420 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
423 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
425 aml_vm_space_handler_ec,
426 NULL, NULL)) != AE_OK) {
427 fprintf(stderr, "could not initialise EC handler: %d\n", error);
430 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431 ACPI_ADR_SPACE_SMBUS,
432 aml_vm_space_handler_smbus,
433 NULL, NULL)) != AE_OK) {
434 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
437 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
439 aml_vm_space_handler_cmos,
440 NULL, NULL)) != AE_OK) {
441 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
444 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
445 ACPI_ADR_SPACE_PCI_BAR_TARGET,
446 aml_vm_space_handler_pci_bar_target,
447 NULL, NULL)) != AE_OK) {
448 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
452 AcpiGbl_GlobalLockPresent = TRUE;
454 error = AcpiDbReadTableFromFile (filetmp, &Table);
455 if (ACPI_FAILURE(error)) {
456 fprintf(stderr, "AcpiDbReadTableFromFile failed: %s\n",
457 AcpiFormatException(error));
460 AeBuildLocalTables(Table);
461 if (ACPI_FAILURE(error = AeInstallTables())) {
462 fprintf(stderr, "AeInstallTables failed: %s\n",
463 AcpiFormatException(error));
467 AcpiUtSetIntegerWidth (Table->Revision);
470 AcpiGbl_DebuggerConfiguration = 0;
471 AcpiDbUserCommands(':', NULL);
473 if (strcmp(dsdtfile, filetmp) != 0) {
481 usage(const char *progname)
484 printf("usage: %s dsdt_file\n", progname);
488 BOOLEAN AcpiGbl_IgnoreErrors = FALSE;
491 main(int argc, char *argv[])
501 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
502 AcpiGbl_EnableInterpreterSlack = TRUE;
504 aml_simulation_regload("region.ini");
505 if (load_dsdt(argv[1]) == 0) {
506 aml_simulation_regdump("region.dmp");