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/tools/acpiexec/aecommon.h>
49 * Dummy DSDT Table Header
52 ACPI_TABLE_HEADER dummy_dsdt_table = {
53 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
57 * Region space I/O routines on virtual machine
60 int aml_debug_prompt = 1;
62 struct ACPIRegionContent {
63 TAILQ_ENTRY(ACPIRegionContent) links;
65 ACPI_PHYSICAL_ADDRESS addr;
69 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
70 struct ACPIRegionContentList RegionContentList;
72 static int aml_simulation_initialized = 0;
74 static void aml_simulation_init(void);
75 static int aml_simulate_regcontent_add(int regtype,
76 ACPI_PHYSICAL_ADDRESS addr,
78 static int aml_simulate_regcontent_read(int regtype,
79 ACPI_PHYSICAL_ADDRESS addr,
81 static int aml_simulate_regcontent_write(int regtype,
82 ACPI_PHYSICAL_ADDRESS addr,
84 static UINT64 aml_simulate_prompt(char *msg, UINT64 def_val);
85 static void aml_simulation_regload(const char *dumpfile);
86 static void aml_simulation_regdump(const char *dumpfile);
88 /* Stubs to simplify linkage to the ACPI CA core subsystem. */
90 AeLocalGetRootPointer(void)
97 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
102 aml_simulation_init(void)
105 aml_simulation_initialized = 1;
106 TAILQ_INIT(&RegionContentList);
107 aml_simulation_regload("region.ini");
111 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
113 struct ACPIRegionContent *rc;
115 rc = malloc(sizeof(struct ACPIRegionContent));
117 return (-1); /* malloc fail */
119 rc->regtype = regtype;
123 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
128 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
130 struct ACPIRegionContent *rc;
132 if (!aml_simulation_initialized) {
133 aml_simulation_init();
135 TAILQ_FOREACH(rc, &RegionContentList, links) {
136 if (rc->regtype == regtype && rc->addr == addr) {
138 return (1); /* found */
143 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
147 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
149 struct ACPIRegionContent *rc;
151 if (!aml_simulation_initialized) {
152 aml_simulation_init();
154 TAILQ_FOREACH(rc, &RegionContentList, links) {
155 if (rc->regtype == regtype && rc->addr == addr) {
157 return (1); /* exists */
161 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
165 aml_simulate_prompt(char *msg, UINT64 def_val)
175 printf("(default: 0x%jx ", val);
176 printf(" / %ju) >>", val);
179 bzero(buf, sizeof buf);
181 if (read(0, buf, sizeof buf) == 0) {
184 if (buf[0] == '\n') {
185 break; /* use default value */
187 if (buf[0] == '0' && buf[1] == 'x') {
188 val = strtoq(buf, &ep, 16);
190 val = strtoq(buf, &ep, 10);
198 aml_simulation_regload(const char *dumpfile)
200 char buf[256], *np, *ep;
201 struct ACPIRegionContent rc;
204 if (!aml_simulation_initialized) {
208 if ((fp = fopen(dumpfile, "r")) == NULL) {
212 while (fgets(buf, sizeof buf, fp) != NULL) {
214 /* reading region type */
215 rc.regtype = strtoq(np, &ep, 10);
221 /* reading address */
222 rc.addr = strtoq(np, &ep, 16);
229 rc.value = strtoq(np, &ep, 16);
233 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
240 aml_simulation_regdump(const char *dumpfile)
242 struct ACPIRegionContent *rc;
245 if (!aml_simulation_initialized) {
248 if ((fp = fopen(dumpfile, "w")) == NULL) {
249 warn("%s", dumpfile);
252 while (!TAILQ_EMPTY(&RegionContentList)) {
253 rc = TAILQ_FIRST(&RegionContentList);
254 fprintf(fp, "%d 0x%jx 0x%x\n",
255 rc->regtype, (uintmax_t)rc->addr, rc->value);
256 TAILQ_REMOVE(&RegionContentList, rc, links);
261 TAILQ_INIT(&RegionContentList);
265 * Space handlers on virtual machine
269 aml_vm_space_handler(
272 ACPI_PHYSICAL_ADDRESS Address,
281 static const char *space_names[] = {
282 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
283 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
288 for (i = 0; (i * 8) < BitWidth; i++) {
289 state = aml_simulate_regcontent_read(SpaceID,
292 return (AE_NO_MEMORY);
294 value |= val << (i * 8);
298 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
299 space_names[SpaceID], BitWidth,
301 *Value = aml_simulate_prompt(msg, value);
302 if (*Value != value) {
303 return(aml_vm_space_handler(SpaceID,
305 Address, BitWidth, Value, 0));
313 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
314 space_names[SpaceID], BitWidth,
316 value = aml_simulate_prompt(msg, *Value);
319 for (i = 0; (i * 8) < BitWidth; i++) {
321 state = aml_simulate_regcontent_write(SpaceID,
324 return (AE_NO_MEMORY);
333 #define DECLARE_VM_SPACE_HANDLER(name, id); \
335 aml_vm_space_handler_##name ( \
337 ACPI_PHYSICAL_ADDRESS Address, \
341 return (aml_vm_space_handler(id, Function, Address, \
342 BitWidth, Value, aml_debug_prompt)); \
345 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
346 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO);
347 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG);
348 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC);
349 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS);
350 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS);
351 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
354 * Load DSDT data file and invoke debugger
358 load_dsdt(const char *dsdtfile)
360 char filetmp[PATH_MAX];
366 fd = open(dsdtfile, O_RDONLY, 0);
371 if (fstat(fd, &sb) == -1) {
375 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
380 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
385 * make sure DSDT data contains table header or not.
387 if (strncmp((char *)code, "DSDT", 4) == 0) {
388 strncpy(filetmp, dsdtfile, sizeof(filetmp));
390 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
391 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
392 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
393 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
398 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
400 write(fd2, code, sb.st_size);
405 * Install the virtual machine version of address space handlers.
407 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
408 ACPI_ADR_SPACE_SYSTEM_MEMORY,
409 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory,
410 NULL, NULL)) != AE_OK) {
411 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
414 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
415 ACPI_ADR_SPACE_SYSTEM_IO,
416 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io,
417 NULL, NULL)) != AE_OK) {
418 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
421 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
422 ACPI_ADR_SPACE_PCI_CONFIG,
423 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config,
424 NULL, NULL)) != AE_OK) {
425 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
428 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
430 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec,
431 NULL, NULL)) != AE_OK) {
432 fprintf(stderr, "could not initialise EC handler: %d\n", error);
435 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
436 ACPI_ADR_SPACE_SMBUS,
437 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus,
438 NULL, NULL)) != AE_OK) {
439 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
442 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
444 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos,
445 NULL, NULL)) != AE_OK) {
446 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
449 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
450 ACPI_ADR_SPACE_PCI_BAR_TARGET,
451 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target,
452 NULL, NULL)) != AE_OK) {
453 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
457 AcpiDbGetTableFromFile(filetmp, NULL);
460 AcpiGbl_DebuggerConfiguration = 0;
461 AcpiDbUserCommands(':', NULL);
463 if (strcmp(dsdtfile, filetmp) != 0) {
471 usage(const char *progname)
474 printf("usage: %s dsdt_file\n", progname);
479 main(int argc, char *argv[])
489 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
492 * Match kernel options for the interpreter. Global variable names
493 * can be found in acglobal.h.
495 AcpiGbl_EnableInterpreterSlack = TRUE;
497 aml_simulation_regload("region.ini");
498 if (load_dsdt(argv[1]) == 0) {
499 aml_simulation_regdump("region.dmp");