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 static 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 static int aml_debug_prompt = 1;
63 struct ACPIRegionContent {
64 TAILQ_ENTRY(ACPIRegionContent) links;
66 ACPI_PHYSICAL_ADDRESS addr;
70 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
71 static struct ACPIRegionContentList RegionContentList;
73 static int aml_simulation_initialized = 0;
75 ACPI_PHYSICAL_ADDRESS AeLocalGetRootPointer(void);
76 void AeTableOverride(ACPI_TABLE_HEADER *, ACPI_TABLE_HEADER **);
78 static void aml_simulation_init(void);
79 static int aml_simulate_regcontent_add(int regtype,
80 ACPI_PHYSICAL_ADDRESS addr,
82 static int aml_simulate_regcontent_read(int regtype,
83 ACPI_PHYSICAL_ADDRESS addr,
85 static int aml_simulate_regcontent_write(int regtype,
86 ACPI_PHYSICAL_ADDRESS addr,
88 static UINT64 aml_simulate_prompt(char *msg, UINT64 def_val);
89 static void aml_simulation_regload(const char *dumpfile);
90 static void aml_simulation_regdump(const char *dumpfile);
92 /* Stubs to simplify linkage to the ACPI CA core subsystem. */
94 AeLocalGetRootPointer(void)
101 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
106 aml_simulation_init(void)
109 aml_simulation_initialized = 1;
110 TAILQ_INIT(&RegionContentList);
111 aml_simulation_regload("region.ini");
115 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
117 struct ACPIRegionContent *rc;
119 rc = malloc(sizeof(struct ACPIRegionContent));
121 return (-1); /* malloc fail */
123 rc->regtype = regtype;
127 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
132 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
134 struct ACPIRegionContent *rc;
136 if (!aml_simulation_initialized) {
137 aml_simulation_init();
139 TAILQ_FOREACH(rc, &RegionContentList, links) {
140 if (rc->regtype == regtype && rc->addr == addr) {
142 return (1); /* found */
147 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
151 aml_simulate_regcontent_write(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); /* exists */
165 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
169 aml_simulate_prompt(char *msg, UINT64 def_val)
179 printf("(default: 0x%jx ", (uintmax_t)val);
180 printf(" / %ju) >>", (uintmax_t)val);
183 bzero(buf, sizeof buf);
185 if (read(0, buf, sizeof buf) == 0) {
188 if (buf[0] == '\n') {
189 break; /* use default value */
191 if (buf[0] == '0' && buf[1] == 'x') {
192 val = strtoq(buf, &ep, 16);
194 val = strtoq(buf, &ep, 10);
202 aml_simulation_regload(const char *dumpfile)
204 char buf[256], *np, *ep;
205 struct ACPIRegionContent rc;
208 if (!aml_simulation_initialized) {
212 if ((fp = fopen(dumpfile, "r")) == NULL) {
216 while (fgets(buf, sizeof buf, fp) != NULL) {
218 /* reading region type */
219 rc.regtype = strtoq(np, &ep, 10);
225 /* reading address */
226 rc.addr = strtoq(np, &ep, 16);
233 rc.value = strtoq(np, &ep, 16);
237 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
244 aml_simulation_regdump(const char *dumpfile)
246 struct ACPIRegionContent *rc;
249 if (!aml_simulation_initialized) {
252 if ((fp = fopen(dumpfile, "w")) == NULL) {
253 warn("%s", dumpfile);
256 while (!TAILQ_EMPTY(&RegionContentList)) {
257 rc = TAILQ_FIRST(&RegionContentList);
258 fprintf(fp, "%d 0x%jx 0x%x\n",
259 rc->regtype, (uintmax_t)rc->addr, rc->value);
260 TAILQ_REMOVE(&RegionContentList, rc, links);
265 TAILQ_INIT(&RegionContentList);
269 * Space handlers on virtual machine
273 aml_vm_space_handler(
276 ACPI_PHYSICAL_ADDRESS Address,
285 static const char *space_names[] = {
286 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
287 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
292 for (i = 0; (i * 8) < BitWidth; i++) {
293 state = aml_simulate_regcontent_read(SpaceID,
296 return (AE_NO_MEMORY);
298 value |= val << (i * 8);
302 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
303 space_names[SpaceID], BitWidth,
305 *Value = aml_simulate_prompt(msg, value);
306 if (*Value != value) {
307 return(aml_vm_space_handler(SpaceID,
309 Address, BitWidth, Value, 0));
317 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
318 space_names[SpaceID], BitWidth,
320 value = aml_simulate_prompt(msg, *Value);
323 for (i = 0; (i * 8) < BitWidth; i++) {
325 state = aml_simulate_regcontent_write(SpaceID,
328 return (AE_NO_MEMORY);
337 #define DECLARE_VM_SPACE_HANDLER(name, id); \
339 aml_vm_space_handler_##name ( \
341 ACPI_PHYSICAL_ADDRESS Address, \
345 return (aml_vm_space_handler(id, Function, Address, \
346 BitWidth, Value, aml_debug_prompt)); \
349 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
350 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO);
351 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG);
352 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC);
353 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS);
354 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS);
355 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
358 * Load DSDT data file and invoke debugger
362 load_dsdt(const char *dsdtfile)
364 char filetmp[PATH_MAX];
370 fd = open(dsdtfile, O_RDONLY, 0);
375 if (fstat(fd, &sb) == -1) {
380 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
385 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
390 * make sure DSDT data contains table header or not.
392 if (strncmp((char *)code, "DSDT", 4) == 0) {
393 strncpy(filetmp, dsdtfile, sizeof(filetmp));
395 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
396 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
397 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
398 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
403 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
405 write(fd2, code, sb.st_size);
410 * Install the virtual machine version of address space handlers.
412 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
413 ACPI_ADR_SPACE_SYSTEM_MEMORY,
414 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory,
415 NULL, NULL)) != AE_OK) {
416 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
419 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
420 ACPI_ADR_SPACE_SYSTEM_IO,
421 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io,
422 NULL, NULL)) != AE_OK) {
423 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
426 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
427 ACPI_ADR_SPACE_PCI_CONFIG,
428 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config,
429 NULL, NULL)) != AE_OK) {
430 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
433 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
435 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec,
436 NULL, NULL)) != AE_OK) {
437 fprintf(stderr, "could not initialise EC handler: %d\n", error);
440 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
441 ACPI_ADR_SPACE_SMBUS,
442 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus,
443 NULL, NULL)) != AE_OK) {
444 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
447 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
449 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos,
450 NULL, NULL)) != AE_OK) {
451 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
454 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
455 ACPI_ADR_SPACE_PCI_BAR_TARGET,
456 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target,
457 NULL, NULL)) != AE_OK) {
458 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
462 AcpiDbGetTableFromFile(filetmp, NULL);
465 AcpiGbl_DebuggerConfiguration = 0;
466 AcpiDbUserCommands(':', NULL);
468 if (strcmp(dsdtfile, filetmp) != 0) {
476 usage(const char *progname)
479 printf("usage: %s dsdt_file\n", progname);
484 main(int argc, char *argv[])
494 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
497 * Match kernel options for the interpreter. Global variable names
498 * can be found in acglobal.h.
500 AcpiGbl_EnableInterpreterSlack = TRUE;
502 aml_simulation_regload("region.ini");
503 if (load_dsdt(argv[1]) == 0) {
504 aml_simulation_regdump("region.dmp");