2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin Köhne <c.koehne@beckhoff.com>
11 #include <machine/vmm.h>
19 #include "acpi_device.h"
23 * List entry to enumerate all resources used by an ACPI device.
25 * @param chain Used to chain multiple elements together.
26 * @param type Type of the ACPI resource.
27 * @param data Data of the ACPI resource.
29 struct acpi_resource_list_entry {
30 SLIST_ENTRY(acpi_resource_list_entry) chain;
32 ACPI_RESOURCE_DATA data;
36 * Holds information about an ACPI device.
38 * @param vm_ctx VM context the ACPI device was created in.
39 * @param softc A pointer to the software context of the ACPI device.
40 * @param emul Device emulation struct. It contains some information like the
41 name of the ACPI device and some device specific functions.
42 * @param crs Current resources used by the ACPI device.
47 const struct acpi_device_emul *emul;
48 SLIST_HEAD(acpi_resource_list, acpi_resource_list_entry) crs;
52 acpi_device_create(struct acpi_device **const new_dev, void *const softc,
53 struct vmctx *const vm_ctx, const struct acpi_device_emul *const emul)
55 assert(new_dev != NULL);
56 assert(vm_ctx != NULL);
59 struct acpi_device *const dev = calloc(1, sizeof(*dev));
67 SLIST_INIT(&dev->crs);
69 const int error = acpi_tables_add_device(dev);
71 acpi_device_destroy(dev);
81 acpi_device_destroy(struct acpi_device *const dev)
87 struct acpi_resource_list_entry *res;
88 while (!SLIST_EMPTY(&dev->crs)) {
89 res = SLIST_FIRST(&dev->crs);
90 SLIST_REMOVE_HEAD(&dev->crs, chain);
98 acpi_device_add_res_fixed_ioport(struct acpi_device *const dev,
99 const UINT16 port, const UINT8 length)
105 struct acpi_resource_list_entry *const res = calloc(1, sizeof(*res));
110 res->type = ACPI_RESOURCE_TYPE_FIXED_IO;
111 res->data.FixedIo.Address = port;
112 res->data.FixedIo.AddressLength = length;
114 SLIST_INSERT_HEAD(&dev->crs, res, chain);
120 acpi_device_add_res_fixed_memory32(struct acpi_device *const dev,
121 const UINT8 write_protected, const UINT32 address, const UINT32 length)
127 struct acpi_resource_list_entry *const res = calloc(1, sizeof(*res));
132 res->type = ACPI_RESOURCE_TYPE_FIXED_MEMORY32;
133 res->data.FixedMemory32.WriteProtect = write_protected;
134 res->data.FixedMemory32.Address = address;
135 res->data.FixedMemory32.AddressLength = length;
137 SLIST_INSERT_HEAD(&dev->crs, res, chain);
143 acpi_device_get_softc(const struct acpi_device *const dev)
151 acpi_device_build_table(const struct acpi_device *const dev)
154 assert(dev->emul != NULL);
156 if (dev->emul->build_table != NULL) {
157 return (dev->emul->build_table(dev));
164 acpi_device_write_dsdt_crs(const struct acpi_device *const dev)
166 const struct acpi_resource_list_entry *res;
167 SLIST_FOREACH(res, &dev->crs, chain) {
169 case ACPI_RESOURCE_TYPE_FIXED_IO:
170 dsdt_fixed_ioport(res->data.FixedIo.Address,
171 res->data.FixedIo.AddressLength);
173 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
174 dsdt_fixed_mem32(res->data.FixedMemory32.Address,
175 res->data.FixedMemory32.AddressLength);
187 acpi_device_write_dsdt(const struct acpi_device *const dev)
192 dsdt_line(" Scope (\\_SB)");
194 dsdt_line(" Device (%s)", dev->emul->name);
196 dsdt_line(" Name (_HID, \"%s\")", dev->emul->hid);
197 dsdt_line(" Name (_STA, 0x0F)");
198 dsdt_line(" Name (_CRS, ResourceTemplate ()");
201 BASL_EXEC(acpi_device_write_dsdt_crs(dev));
204 if (dev->emul->write_dsdt != NULL) {
206 BASL_EXEC(dev->emul->write_dsdt(dev));