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/acapps.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
49 #include <contrib/dev/acpica/include/amlresrc.h>
52 * Dummy DSDT Table Header
55 static ACPI_TABLE_HEADER dummy_dsdt_table = {
56 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
60 * Region space I/O routines on virtual machine
63 static int aml_debug_prompt = 1;
65 struct ACPIRegionContent {
66 TAILQ_ENTRY(ACPIRegionContent) links;
68 ACPI_PHYSICAL_ADDRESS addr;
72 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
73 static struct ACPIRegionContentList RegionContentList;
75 static int aml_simulation_initialized = 0;
77 ACPI_PHYSICAL_ADDRESS AeLocalGetRootPointer(void);
78 void AeDoObjectOverrides(void);
79 void AeTableOverride(ACPI_TABLE_HEADER *, ACPI_TABLE_HEADER **);
81 static void aml_simulation_init(void);
82 static int aml_simulate_regcontent_add(int regtype,
83 ACPI_PHYSICAL_ADDRESS addr,
85 static int aml_simulate_regcontent_read(int regtype,
86 ACPI_PHYSICAL_ADDRESS addr,
88 static int aml_simulate_regcontent_write(int regtype,
89 ACPI_PHYSICAL_ADDRESS addr,
91 static UINT64 aml_simulate_prompt(char *msg, UINT64 def_val);
92 static void aml_simulation_regload(const char *dumpfile);
93 static void aml_simulation_regdump(const char *dumpfile);
95 /* Stubs to simplify linkage to the ACPICA core subsystem. */
97 AcpiOsGetRootPointer(void)
104 AeDoObjectOverrides(void)
109 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
114 MpSaveGpioInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
115 UINT32 PinCount, UINT16 *PinList, char *DeviceName)
120 MpSaveSerialInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
126 aml_simulation_init(void)
129 aml_simulation_initialized = 1;
130 TAILQ_INIT(&RegionContentList);
131 aml_simulation_regload("region.ini");
135 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
137 struct ACPIRegionContent *rc;
139 rc = malloc(sizeof(struct ACPIRegionContent));
141 return (-1); /* malloc fail */
143 rc->regtype = regtype;
147 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
152 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
154 struct ACPIRegionContent *rc;
156 if (!aml_simulation_initialized) {
157 aml_simulation_init();
159 TAILQ_FOREACH(rc, &RegionContentList, links) {
160 if (rc->regtype == regtype && rc->addr == addr) {
162 return (1); /* found */
167 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
171 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
173 struct ACPIRegionContent *rc;
175 if (!aml_simulation_initialized) {
176 aml_simulation_init();
178 TAILQ_FOREACH(rc, &RegionContentList, links) {
179 if (rc->regtype == regtype && rc->addr == addr) {
181 return (1); /* exists */
185 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
189 aml_simulate_prompt(char *msg, UINT64 def_val)
199 printf("(default: 0x%jx ", (uintmax_t)val);
200 printf(" / %ju) >>", (uintmax_t)val);
203 bzero(buf, sizeof buf);
205 if (read(0, buf, sizeof buf) == 0) {
208 if (buf[0] == '\n') {
209 break; /* use default value */
211 if (buf[0] == '0' && buf[1] == 'x') {
212 val = strtoq(buf, &ep, 16);
214 val = strtoq(buf, &ep, 10);
222 aml_simulation_regload(const char *dumpfile)
224 char buf[256], *np, *ep;
225 struct ACPIRegionContent rc;
228 if (!aml_simulation_initialized) {
232 if ((fp = fopen(dumpfile, "r")) == NULL) {
236 while (fgets(buf, sizeof buf, fp) != NULL) {
238 /* reading region type */
239 rc.regtype = strtoq(np, &ep, 10);
245 /* reading address */
246 rc.addr = strtoq(np, &ep, 16);
253 rc.value = strtoq(np, &ep, 16);
257 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
264 aml_simulation_regdump(const char *dumpfile)
266 struct ACPIRegionContent *rc;
269 if (!aml_simulation_initialized) {
272 if ((fp = fopen(dumpfile, "w")) == NULL) {
273 warn("%s", dumpfile);
276 while (!TAILQ_EMPTY(&RegionContentList)) {
277 rc = TAILQ_FIRST(&RegionContentList);
278 fprintf(fp, "%d 0x%jx 0x%x\n",
279 rc->regtype, (uintmax_t)rc->addr, rc->value);
280 TAILQ_REMOVE(&RegionContentList, rc, links);
285 TAILQ_INIT(&RegionContentList);
289 * Space handlers on virtual machine
293 aml_vm_space_handler(
296 ACPI_PHYSICAL_ADDRESS Address,
305 static const char *space_names[] = {
306 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
307 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
312 for (i = 0; (i * 8) < BitWidth; i++) {
313 state = aml_simulate_regcontent_read(SpaceID,
316 return (AE_NO_MEMORY);
318 value |= val << (i * 8);
322 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
323 space_names[SpaceID], BitWidth,
325 *Value = aml_simulate_prompt(msg, value);
326 if (*Value != value) {
327 return(aml_vm_space_handler(SpaceID,
329 Address, BitWidth, Value, 0));
337 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
338 space_names[SpaceID], BitWidth,
340 value = aml_simulate_prompt(msg, *Value);
343 for (i = 0; (i * 8) < BitWidth; i++) {
345 state = aml_simulate_regcontent_write(SpaceID,
348 return (AE_NO_MEMORY);
357 #define DECLARE_VM_SPACE_HANDLER(name, id); \
359 aml_vm_space_handler_##name ( \
361 ACPI_PHYSICAL_ADDRESS Address, \
365 return (aml_vm_space_handler(id, Function, Address, \
366 BitWidth, Value, aml_debug_prompt)); \
369 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
370 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO);
371 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG);
372 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC);
373 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS);
374 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS);
375 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
378 * Load DSDT data file and invoke debugger
382 load_dsdt(const char *dsdtfile)
384 char filetmp[PATH_MAX];
385 ACPI_NEW_TABLE_DESC *list;
388 int dounlink, error, fd;
390 fd = open(dsdtfile, O_RDONLY, 0);
395 if (fstat(fd, &sb) == -1) {
400 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
406 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
407 munmap(code, (size_t)sb.st_size);
412 * make sure DSDT data contains table header or not.
414 if (strncmp((char *)code, "DSDT", 4) == 0) {
416 strlcpy(filetmp, dsdtfile, sizeof(filetmp));
419 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
420 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
421 if ((size_t)snprintf(filetmp, sizeof(filetmp), "%s.tmp",
422 dsdtfile) > sizeof(filetmp) - 1) {
423 fprintf(stderr, "file name too long\n");
424 munmap(code, (size_t)sb.st_size);
427 fd = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
430 munmap(code, (size_t)sb.st_size);
433 write(fd, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
435 write(fd, code, sb.st_size);
438 munmap(code, (size_t)sb.st_size);
441 * Install the virtual machine version of address space handlers.
443 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
444 ACPI_ADR_SPACE_SYSTEM_MEMORY,
445 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory,
446 NULL, NULL)) != AE_OK) {
447 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
450 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
451 ACPI_ADR_SPACE_SYSTEM_IO,
452 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io,
453 NULL, NULL)) != AE_OK) {
454 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
457 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
458 ACPI_ADR_SPACE_PCI_CONFIG,
459 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config,
460 NULL, NULL)) != AE_OK) {
461 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
464 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
466 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec,
467 NULL, NULL)) != AE_OK) {
468 fprintf(stderr, "could not initialise EC handler: %d\n", error);
471 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
472 ACPI_ADR_SPACE_SMBUS,
473 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus,
474 NULL, NULL)) != AE_OK) {
475 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
478 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
480 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos,
481 NULL, NULL)) != AE_OK) {
482 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
485 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
486 ACPI_ADR_SPACE_PCI_BAR_TARGET,
487 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target,
488 NULL, NULL)) != AE_OK) {
489 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
494 AcGetAllTablesFromFile(filetmp, TRUE, &list);
496 AcpiInitializeDebugger();
497 AcpiGbl_DebuggerConfiguration = 0;
498 AcpiDbUserCommands(':', NULL);
508 usage(const char *progname)
511 printf("usage: %s dsdt_file\n", progname);
516 main(int argc, char *argv[])
526 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
529 * Match kernel options for the interpreter. Global variable names
530 * can be found in acglobal.h.
532 AcpiGbl_EnableInterpreterSlack = TRUE;
534 aml_simulation_regload("region.ini");
535 if (load_dsdt(argv[1]) == 0) {
536 aml_simulation_regdump("region.dmp");