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
29 #include <sys/param.h>
30 #include <sys/queue.h>
33 #include <sys/stdint.h>
34 #include <sys/types.h>
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acdebug.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 UINT64 aml_simulate_prompt(char *msg, UINT64 def_val);
86 static void aml_simulation_regload(const char *dumpfile);
87 static void aml_simulation_regdump(const char *dumpfile);
89 /* Stubs to simplify linkage to the ACPI CA core subsystem. */
91 AeLocalGetRootPointer(void)
98 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
103 aml_simulation_init(void)
106 aml_simulation_initialized = 1;
107 TAILQ_INIT(&RegionContentList);
108 aml_simulation_regload("region.ini");
112 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
114 struct ACPIRegionContent *rc;
116 rc = malloc(sizeof(struct ACPIRegionContent));
118 return (-1); /* malloc fail */
120 rc->regtype = regtype;
124 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
129 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
131 struct ACPIRegionContent *rc;
133 if (!aml_simulation_initialized) {
134 aml_simulation_init();
136 TAILQ_FOREACH(rc, &RegionContentList, links) {
137 if (rc->regtype == regtype && rc->addr == addr) {
139 return (1); /* found */
144 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
148 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
150 struct ACPIRegionContent *rc;
152 if (!aml_simulation_initialized) {
153 aml_simulation_init();
155 TAILQ_FOREACH(rc, &RegionContentList, links) {
156 if (rc->regtype == regtype && rc->addr == addr) {
158 return (1); /* exists */
162 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
166 aml_simulate_prompt(char *msg, UINT64 def_val)
176 printf("(default: 0x%jx ", val);
177 printf(" / %ju) >>", val);
180 bzero(buf, sizeof buf);
182 if (read(0, buf, sizeof buf) == 0) {
185 if (buf[0] == '\n') {
186 break; /* use default value */
188 if (buf[0] == '0' && buf[1] == 'x') {
189 val = strtoq(buf, &ep, 16);
191 val = strtoq(buf, &ep, 10);
199 aml_simulation_regload(const char *dumpfile)
201 char buf[256], *np, *ep;
202 struct ACPIRegionContent rc;
205 if (!aml_simulation_initialized) {
209 if ((fp = fopen(dumpfile, "r")) == NULL) {
213 while (fgets(buf, sizeof buf, fp) != NULL) {
215 /* reading region type */
216 rc.regtype = strtoq(np, &ep, 10);
222 /* reading address */
223 rc.addr = strtoq(np, &ep, 16);
230 rc.value = strtoq(np, &ep, 16);
234 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
241 aml_simulation_regdump(const char *dumpfile)
243 struct ACPIRegionContent *rc;
246 if (!aml_simulation_initialized) {
249 if ((fp = fopen(dumpfile, "w")) == NULL) {
250 warn("%s", dumpfile);
253 while (!TAILQ_EMPTY(&RegionContentList)) {
254 rc = TAILQ_FIRST(&RegionContentList);
255 fprintf(fp, "%d 0x%jx 0x%x\n",
256 rc->regtype, (uintmax_t)rc->addr, rc->value);
257 TAILQ_REMOVE(&RegionContentList, rc, links);
262 TAILQ_INIT(&RegionContentList);
266 * Space handlers on virtual machine
270 aml_vm_space_handler(
273 ACPI_PHYSICAL_ADDRESS Address,
282 static const char *space_names[] = {
283 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
284 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
289 for (i = 0; (i * 8) < BitWidth; i++) {
290 state = aml_simulate_regcontent_read(SpaceID,
293 return (AE_NO_MEMORY);
295 value |= val << (i * 8);
299 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
300 space_names[SpaceID], BitWidth,
302 *Value = aml_simulate_prompt(msg, value);
303 if (*Value != value) {
304 return(aml_vm_space_handler(SpaceID,
306 Address, BitWidth, Value, 0));
314 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
315 space_names[SpaceID], BitWidth,
317 value = aml_simulate_prompt(msg, *Value);
320 for (i = 0; (i * 8) < BitWidth; i++) {
322 state = aml_simulate_regcontent_write(SpaceID,
325 return (AE_NO_MEMORY);
334 #define DECLARE_VM_SPACE_HANDLER(name, id); \
336 aml_vm_space_handler_##name ( \
338 ACPI_PHYSICAL_ADDRESS Address, \
342 return (aml_vm_space_handler(id, Function, Address, \
343 BitWidth, Value, aml_debug_prompt)); \
346 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
347 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO);
348 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG);
349 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC);
350 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS);
351 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS);
352 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
355 * Load DSDT data file and invoke debugger
359 load_dsdt(const char *dsdtfile)
361 char filetmp[PATH_MAX];
367 fd = open(dsdtfile, O_RDONLY, 0);
372 if (fstat(fd, &sb) == -1) {
377 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
382 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
387 * make sure DSDT data contains table header or not.
389 if (strncmp((char *)code, "DSDT", 4) == 0) {
390 strncpy(filetmp, dsdtfile, sizeof(filetmp));
392 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
393 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
394 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
395 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
400 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
402 write(fd2, code, sb.st_size);
407 * Install the virtual machine version of address space handlers.
409 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
410 ACPI_ADR_SPACE_SYSTEM_MEMORY,
411 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory,
412 NULL, NULL)) != AE_OK) {
413 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
416 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
417 ACPI_ADR_SPACE_SYSTEM_IO,
418 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io,
419 NULL, NULL)) != AE_OK) {
420 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
423 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
424 ACPI_ADR_SPACE_PCI_CONFIG,
425 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config,
426 NULL, NULL)) != AE_OK) {
427 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
430 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
432 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec,
433 NULL, NULL)) != AE_OK) {
434 fprintf(stderr, "could not initialise EC handler: %d\n", error);
437 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438 ACPI_ADR_SPACE_SMBUS,
439 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus,
440 NULL, NULL)) != AE_OK) {
441 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
444 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
446 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos,
447 NULL, NULL)) != AE_OK) {
448 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
451 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
452 ACPI_ADR_SPACE_PCI_BAR_TARGET,
453 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target,
454 NULL, NULL)) != AE_OK) {
455 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
459 AcpiDbGetTableFromFile(filetmp, NULL);
462 AcpiGbl_DebuggerConfiguration = 0;
463 AcpiDbUserCommands(':', NULL);
465 if (strcmp(dsdtfile, filetmp) != 0) {
473 usage(const char *progname)
476 printf("usage: %s dsdt_file\n", progname);
481 main(int argc, char *argv[])
491 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
494 * Match kernel options for the interpreter. Global variable names
495 * can be found in acglobal.h.
497 AcpiGbl_EnableInterpreterSlack = TRUE;
499 aml_simulation_regload("region.ini");
500 if (load_dsdt(argv[1]) == 0) {
501 aml_simulation_regdump("region.dmp");