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>
48 #include <contrib/dev/acpica/include/amlresrc.h>
51 * Dummy DSDT Table Header
54 static ACPI_TABLE_HEADER dummy_dsdt_table = {
55 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
59 * Region space I/O routines on virtual machine
62 static int aml_debug_prompt = 1;
64 struct ACPIRegionContent {
65 TAILQ_ENTRY(ACPIRegionContent) links;
67 ACPI_PHYSICAL_ADDRESS addr;
71 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
72 static struct ACPIRegionContentList RegionContentList;
74 static int aml_simulation_initialized = 0;
76 ACPI_PHYSICAL_ADDRESS AeLocalGetRootPointer(void);
77 void AeDoObjectOverrides(void);
78 void AeTableOverride(ACPI_TABLE_HEADER *, ACPI_TABLE_HEADER **);
80 static void aml_simulation_init(void);
81 static int aml_simulate_regcontent_add(int regtype,
82 ACPI_PHYSICAL_ADDRESS addr,
84 static int aml_simulate_regcontent_read(int regtype,
85 ACPI_PHYSICAL_ADDRESS addr,
87 static int aml_simulate_regcontent_write(int regtype,
88 ACPI_PHYSICAL_ADDRESS addr,
90 static UINT64 aml_simulate_prompt(char *msg, UINT64 def_val);
91 static void aml_simulation_regload(const char *dumpfile);
92 static void aml_simulation_regdump(const char *dumpfile);
94 /* Stubs to simplify linkage to the ACPICA core subsystem. */
96 AcpiOsGetRootPointer(void)
103 AeDoObjectOverrides(void)
108 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
113 MpSaveGpioInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
114 UINT32 PinCount, UINT16 *PinList, char *DeviceName)
119 MpSaveSerialInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
125 aml_simulation_init(void)
128 aml_simulation_initialized = 1;
129 TAILQ_INIT(&RegionContentList);
130 aml_simulation_regload("region.ini");
134 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
136 struct ACPIRegionContent *rc;
138 rc = malloc(sizeof(struct ACPIRegionContent));
140 return (-1); /* malloc fail */
142 rc->regtype = regtype;
146 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
151 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
153 struct ACPIRegionContent *rc;
155 if (!aml_simulation_initialized) {
156 aml_simulation_init();
158 TAILQ_FOREACH(rc, &RegionContentList, links) {
159 if (rc->regtype == regtype && rc->addr == addr) {
161 return (1); /* found */
166 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
170 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
172 struct ACPIRegionContent *rc;
174 if (!aml_simulation_initialized) {
175 aml_simulation_init();
177 TAILQ_FOREACH(rc, &RegionContentList, links) {
178 if (rc->regtype == regtype && rc->addr == addr) {
180 return (1); /* exists */
184 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
188 aml_simulate_prompt(char *msg, UINT64 def_val)
198 printf("(default: 0x%jx ", (uintmax_t)val);
199 printf(" / %ju) >>", (uintmax_t)val);
202 bzero(buf, sizeof buf);
204 if (read(0, buf, sizeof buf) == 0) {
207 if (buf[0] == '\n') {
208 break; /* use default value */
210 if (buf[0] == '0' && buf[1] == 'x') {
211 val = strtoq(buf, &ep, 16);
213 val = strtoq(buf, &ep, 10);
221 aml_simulation_regload(const char *dumpfile)
223 char buf[256], *np, *ep;
224 struct ACPIRegionContent rc;
227 if (!aml_simulation_initialized) {
231 if ((fp = fopen(dumpfile, "r")) == NULL) {
235 while (fgets(buf, sizeof buf, fp) != NULL) {
237 /* reading region type */
238 rc.regtype = strtoq(np, &ep, 10);
244 /* reading address */
245 rc.addr = strtoq(np, &ep, 16);
252 rc.value = strtoq(np, &ep, 16);
256 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
263 aml_simulation_regdump(const char *dumpfile)
265 struct ACPIRegionContent *rc;
268 if (!aml_simulation_initialized) {
271 if ((fp = fopen(dumpfile, "w")) == NULL) {
272 warn("%s", dumpfile);
275 while (!TAILQ_EMPTY(&RegionContentList)) {
276 rc = TAILQ_FIRST(&RegionContentList);
277 fprintf(fp, "%d 0x%jx 0x%x\n",
278 rc->regtype, (uintmax_t)rc->addr, rc->value);
279 TAILQ_REMOVE(&RegionContentList, rc, links);
284 TAILQ_INIT(&RegionContentList);
288 * Space handlers on virtual machine
292 aml_vm_space_handler(
295 ACPI_PHYSICAL_ADDRESS Address,
304 static const char *space_names[] = {
305 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
306 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
311 for (i = 0; (i * 8) < BitWidth; i++) {
312 state = aml_simulate_regcontent_read(SpaceID,
315 return (AE_NO_MEMORY);
317 value |= val << (i * 8);
321 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
322 space_names[SpaceID], BitWidth,
324 *Value = aml_simulate_prompt(msg, value);
325 if (*Value != value) {
326 return(aml_vm_space_handler(SpaceID,
328 Address, BitWidth, Value, 0));
336 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
337 space_names[SpaceID], BitWidth,
339 value = aml_simulate_prompt(msg, *Value);
342 for (i = 0; (i * 8) < BitWidth; i++) {
344 state = aml_simulate_regcontent_write(SpaceID,
347 return (AE_NO_MEMORY);
356 #define DECLARE_VM_SPACE_HANDLER(name, id); \
358 aml_vm_space_handler_##name ( \
360 ACPI_PHYSICAL_ADDRESS Address, \
364 return (aml_vm_space_handler(id, Function, Address, \
365 BitWidth, Value, aml_debug_prompt)); \
368 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
369 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO);
370 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG);
371 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC);
372 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS);
373 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS);
374 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
377 * Load DSDT data file and invoke debugger
381 load_dsdt(const char *dsdtfile)
383 char filetmp[PATH_MAX];
389 fd = open(dsdtfile, O_RDONLY, 0);
394 if (fstat(fd, &sb) == -1) {
399 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
404 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
409 * make sure DSDT data contains table header or not.
411 if (strncmp((char *)code, "DSDT", 4) == 0) {
412 strncpy(filetmp, dsdtfile, sizeof(filetmp));
414 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
415 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
416 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
417 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
422 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
424 write(fd2, code, sb.st_size);
429 * Install the virtual machine version of address space handlers.
431 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
432 ACPI_ADR_SPACE_SYSTEM_MEMORY,
433 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory,
434 NULL, NULL)) != AE_OK) {
435 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
438 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
439 ACPI_ADR_SPACE_SYSTEM_IO,
440 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io,
441 NULL, NULL)) != AE_OK) {
442 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
445 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
446 ACPI_ADR_SPACE_PCI_CONFIG,
447 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config,
448 NULL, NULL)) != AE_OK) {
449 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
452 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
454 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec,
455 NULL, NULL)) != AE_OK) {
456 fprintf(stderr, "could not initialise EC handler: %d\n", error);
459 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
460 ACPI_ADR_SPACE_SMBUS,
461 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus,
462 NULL, NULL)) != AE_OK) {
463 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
466 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
468 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos,
469 NULL, NULL)) != AE_OK) {
470 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
473 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
474 ACPI_ADR_SPACE_PCI_BAR_TARGET,
475 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target,
476 NULL, NULL)) != AE_OK) {
477 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
481 AcpiDbGetTableFromFile(filetmp, NULL, TRUE);
484 AcpiGbl_DebuggerConfiguration = 0;
485 AcpiDbUserCommands(':', NULL);
487 if (strcmp(dsdtfile, filetmp) != 0) {
495 usage(const char *progname)
498 printf("usage: %s dsdt_file\n", progname);
503 main(int argc, char *argv[])
513 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
516 * Match kernel options for the interpreter. Global variable names
517 * can be found in acglobal.h.
519 AcpiGbl_EnableInterpreterSlack = TRUE;
521 aml_simulation_regload("region.ini");
522 if (load_dsdt(argv[1]) == 0) {
523 aml_simulation_regdump("region.dmp");