1 // SPDX-License-Identifier: GPL-2.0+
3 * Core driver model support for ACPI table generation
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEOGRY LOGC_ACPI
16 #include <acpi/acpi_device.h>
18 #include <dm/device-internal.h>
21 #define MAX_ACPI_ITEMS 100
24 * Type of table that we collected
26 * @TYPE_NONE: Not yet known
27 * @TYPE_SSDT: Items in the Secondary System Description Table
28 * @TYPE_DSDT: Items in the Differentiated System Description Table
29 * @TYPE_OTHER: Other (whole)
38 const char *gen_type_str[] = {
45 /* Type of method to call */
53 /* Prototype for all methods */
54 typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
57 * struct acpi_item - Holds info about ACPI data generated by a driver method
59 * @dev: Device that generated this data
60 * @type: Table type it refers to
61 * @writer: Writer that wrote this table
62 * @base: Pointer to base of table in its original location
63 * @buf: Buffer allocated to contain the data (NULL if not allocated)
64 * @size: Size of the data in bytes
68 const struct acpi_writer *writer;
75 /* List of ACPI items collected */
76 static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
77 static int item_count;
79 int acpi_copy_name(char *out_name, const char *name)
81 strncpy(out_name, name, ACPI_NAME_LEN);
82 out_name[ACPI_NAME_LEN] = '\0';
87 int acpi_get_name(const struct udevice *dev, char *out_name)
89 struct acpi_ops *aops;
93 aops = device_get_acpi_ops(dev);
94 if (aops && aops->get_name)
95 return aops->get_name(dev, out_name);
96 name = dev_read_string(dev, "acpi,name");
98 return acpi_copy_name(out_name, name);
99 ret = acpi_device_infer_name(dev, out_name);
101 return log_msg_ret("dev", ret);
106 int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen)
111 path = dev_read_string(dev, "acpi,path");
113 if (strlen(path) >= maxlen)
115 strcpy(out_path, path);
118 ret = acpi_device_path(dev, out_path, maxlen);
120 return log_msg_ret("dev", ret);
126 * add_item() - Add a new item to the list of data collected
129 * @dev: Device that generated the data, if type != TYPE_OTHER
130 * @writer: Writer entry that generated the data, if type == TYPE_OTHER
131 * @type: Table type it refers to
132 * @start: The start of the data (the end is obtained from ctx->current)
133 * Return: 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
135 static int add_item(struct acpi_ctx *ctx, struct udevice *dev,
136 const struct acpi_writer *writer, enum gen_type_t type,
139 struct acpi_item *item;
140 void *end = ctx->current;
142 if (item_count == MAX_ACPI_ITEMS) {
143 log_err("Too many items\n");
144 return log_msg_ret("mem", -ENOSPC);
147 item = &acpi_item[item_count];
149 item->writer = writer;
151 item->size = end - start;
155 if (type != TYPE_OTHER) {
156 item->buf = malloc(item->size);
158 return log_msg_ret("mem", -ENOMEM);
159 memcpy(item->buf, start, item->size);
162 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
168 int acpi_add_other_item(struct acpi_ctx *ctx, const struct acpi_writer *writer,
171 return add_item(ctx, NULL, writer, TYPE_OTHER, start);
174 void acpi_dump_items(enum acpi_dump_option option)
178 printf("Seq Type Base Size Device/Writer\n");
179 printf("--- ----- -------- ---- -------------\n");
180 for (i = 0; i < item_count; i++) {
181 struct acpi_item *item = &acpi_item[i];
183 printf("%3x %-5s %8lx %5x %s\n", i,
184 gen_type_str[item->type],
185 (ulong)map_to_sysmem(item->base), item->size,
186 item->dev ? item->dev->name : item->writer->name);
187 if (option == ACPI_DUMP_CONTENTS) {
188 print_buffer(0, item->buf ? item->buf : item->base, 1,
195 static struct acpi_item *find_acpi_item(const char *devname)
199 for (i = 0; i < item_count; i++) {
200 struct acpi_item *item = &acpi_item[i];
202 if (item->dev && !strcmp(devname, item->dev->name))
210 * sort_acpi_item_type - Sort the ACPI items into the desired order
212 * This looks up the ordering in the device tree and then adds each item one by
213 * one into the supplied buffer
216 * @start: Start position to put the sorted items. The items will follow each
217 * other in sorted order
218 * @type: Type of items to sort
219 * Return: 0 if OK, -ve on error
221 static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
222 enum gen_type_t type)
228 void *end = ctx->current;
231 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
232 "u-boot,acpi-dsdt-order" :
233 "u-boot,acpi-ssdt-order", &size);
235 log_debug("Failed to find ordering, leaving as is\n");
240 * This algorithm rewrites the context buffer without changing its
241 * length. So there is no need to update ctx-current
243 count = size / sizeof(u32);
245 struct acpi_item *item;
249 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
250 name = ofnode_get_name(node);
251 item = find_acpi_item(name);
253 log_err("Failed to find item '%s'\n", name);
254 return log_msg_ret("find", -ENOENT);
256 if (item->type == type) {
257 log_debug(" - add %s\n", item->dev->name);
258 memcpy(ptr, item->buf, item->size);
264 * If the sort order is missing an item then the output will be too
265 * small. Report this error since the item needs to be added to the
266 * ordering for the ACPI tables to be complete.
269 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
276 acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
278 struct acpi_ops *aops;
280 aops = device_get_acpi_ops(dev);
283 case METHOD_WRITE_TABLES:
284 return aops->write_tables;
285 case METHOD_FILL_SSDT:
286 return aops->fill_ssdt;
287 case METHOD_INJECT_DSDT:
288 return aops->inject_dsdt;
289 case METHOD_SETUP_NHLT:
290 return aops->setup_nhlt;
297 int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
298 enum method_t method, enum gen_type_t type)
304 func = acpi_get_method(parent, method);
306 log_debug("- method %d, %s %p\n", method, parent->name, func);
307 ret = device_of_to_plat(parent);
309 return log_msg_ret("ofdata", ret);
310 ctx->tab_start = ctx->current;
311 ret = func(parent, ctx);
313 return log_msg_ret("func", ret);
315 /* Add the item to the internal list */
316 if (type != TYPE_NONE) {
317 ret = add_item(ctx, parent, NULL, type, ctx->tab_start);
319 return log_msg_ret("add", ret);
322 device_foreach_child(dev, parent) {
323 ret = acpi_recurse_method(ctx, dev, method, type);
325 return log_msg_ret("recurse", ret);
331 int acpi_fill_ssdt(struct acpi_ctx *ctx)
333 void *start = ctx->current;
336 log_debug("Writing SSDT tables\n");
337 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
338 log_debug("Writing SSDT finished, err=%d\n", ret);
339 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
341 return log_msg_ret("build", ret);
346 int acpi_inject_dsdt(struct acpi_ctx *ctx)
348 void *start = ctx->current;
351 log_debug("Writing DSDT tables\n");
352 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
354 log_debug("Writing DSDT finished, err=%d\n", ret);
355 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
357 return log_msg_ret("build", ret);
362 void acpi_reset_items(void)
367 int acpi_write_dev_tables(struct acpi_ctx *ctx)
371 log_debug("Writing device tables\n");
372 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
374 log_debug("Writing finished, err=%d\n", ret);
379 int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt)
383 log_debug("Setup NHLT\n");
385 ret = acpi_recurse_method(ctx, dm_root(), METHOD_SETUP_NHLT, TYPE_NONE);
386 log_debug("Setup finished, err=%d\n", ret);