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
12 #include <display_options.h>
17 #include <acpi/acpi_device.h>
19 #include <dm/device-internal.h>
22 #define MAX_ACPI_ITEMS 100
25 * Type of table that we collected
27 * @TYPE_NONE: Not yet known
28 * @TYPE_SSDT: Items in the Secondary System Description Table
29 * @TYPE_DSDT: Items in the Differentiated System Description Table
30 * @TYPE_OTHER: Other (whole)
39 const char *gen_type_str[] = {
46 /* Type of method to call */
54 /* Prototype for all methods */
55 typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
58 * struct acpi_item - Holds info about ACPI data generated by a driver method
60 * @dev: Device that generated this data
61 * @type: Table type it refers to
62 * @writer: Writer that wrote this table
63 * @base: Pointer to base of table in its original location
64 * @buf: Buffer allocated to contain the data (NULL if not allocated)
65 * @size: Size of the data in bytes
69 const struct acpi_writer *writer;
76 /* List of ACPI items collected */
77 static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
78 static int item_count;
80 int acpi_copy_name(char *out_name, const char *name)
82 strncpy(out_name, name, ACPI_NAME_LEN);
83 out_name[ACPI_NAME_LEN] = '\0';
88 int acpi_get_name(const struct udevice *dev, char *out_name)
90 struct acpi_ops *aops;
94 aops = device_get_acpi_ops(dev);
95 if (aops && aops->get_name)
96 return aops->get_name(dev, out_name);
97 name = dev_read_string(dev, "acpi,name");
99 return acpi_copy_name(out_name, name);
100 ret = acpi_device_infer_name(dev, out_name);
102 return log_msg_ret("dev", ret);
107 int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen)
112 path = dev_read_string(dev, "acpi,path");
114 if (strlen(path) >= maxlen)
116 strcpy(out_path, path);
119 ret = acpi_device_path(dev, out_path, maxlen);
121 return log_msg_ret("dev", ret);
127 * add_item() - Add a new item to the list of data collected
130 * @dev: Device that generated the data, if type != TYPE_OTHER
131 * @writer: Writer entry that generated the data, if type == TYPE_OTHER
132 * @type: Table type it refers to
133 * @start: The start of the data (the end is obtained from ctx->current)
134 * Return: 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
136 static int add_item(struct acpi_ctx *ctx, struct udevice *dev,
137 const struct acpi_writer *writer, enum gen_type_t type,
140 struct acpi_item *item;
141 void *end = ctx->current;
143 if (item_count == MAX_ACPI_ITEMS) {
144 log_err("Too many items\n");
145 return log_msg_ret("mem", -ENOSPC);
148 item = &acpi_item[item_count];
150 item->writer = writer;
152 item->size = end - start;
156 if (type != TYPE_OTHER) {
157 item->buf = malloc(item->size);
159 return log_msg_ret("mem", -ENOMEM);
160 memcpy(item->buf, start, item->size);
163 log_debug("* %s: Added type %d, %p, size %x\n",
164 dev ? dev->name : "other", type, start, item->size);
169 int acpi_add_other_item(struct acpi_ctx *ctx, const struct acpi_writer *writer,
172 return add_item(ctx, NULL, writer, TYPE_OTHER, start);
175 void acpi_dump_items(enum acpi_dump_option option)
179 printf("Seq Type Base Size Device/Writer\n");
180 printf("--- ----- -------- ---- -------------\n");
181 for (i = 0; i < item_count; i++) {
182 struct acpi_item *item = &acpi_item[i];
184 printf("%3x %-5s %8lx %5x %s\n", i,
185 gen_type_str[item->type],
186 (ulong)map_to_sysmem(item->base), item->size,
187 item->dev ? item->dev->name : item->writer->name);
188 if (option == ACPI_DUMP_CONTENTS) {
189 print_buffer(0, item->buf ? item->buf : item->base, 1,
196 static struct acpi_item *find_acpi_item(const char *devname)
200 for (i = 0; i < item_count; i++) {
201 struct acpi_item *item = &acpi_item[i];
203 if (item->dev && !strcmp(devname, item->dev->name))
211 * sort_acpi_item_type - Sort the ACPI items into the desired order
213 * This looks up the ordering in the device tree and then adds each item one by
214 * one into the supplied buffer
217 * @start: Start position to put the sorted items. The items will follow each
218 * other in sorted order
219 * @type: Type of items to sort
220 * Return: 0 if OK, -ve on error
222 static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
223 enum gen_type_t type)
229 void *end = ctx->current;
232 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
233 "u-boot,acpi-dsdt-order" :
234 "u-boot,acpi-ssdt-order", &size);
236 log_debug("Failed to find ordering, leaving as is\n");
241 * This algorithm rewrites the context buffer without changing its
242 * length. So there is no need to update ctx-current
244 count = size / sizeof(u32);
246 struct acpi_item *item;
250 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
251 name = ofnode_get_name(node);
252 item = find_acpi_item(name);
254 log_err("Failed to find item '%s'\n", name);
255 return log_msg_ret("find", -ENOENT);
257 if (item->type == type) {
258 log_debug(" - add %s\n", item->dev->name);
259 memcpy(ptr, item->buf, item->size);
265 * If the sort order is missing an item then the output will be too
266 * small. Report this error since the item needs to be added to the
267 * ordering for the ACPI tables to be complete.
270 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
277 acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
279 struct acpi_ops *aops;
281 aops = device_get_acpi_ops(dev);
284 case METHOD_WRITE_TABLES:
285 return aops->write_tables;
286 case METHOD_FILL_SSDT:
287 return aops->fill_ssdt;
288 case METHOD_INJECT_DSDT:
289 return aops->inject_dsdt;
290 case METHOD_SETUP_NHLT:
291 return aops->setup_nhlt;
298 int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
299 enum method_t method, enum gen_type_t type)
305 func = acpi_get_method(parent, method);
307 log_debug("- method %d, %s %p\n", method, parent->name, func);
308 ret = device_of_to_plat(parent);
310 return log_msg_ret("ofdata", ret);
311 ctx->tab_start = ctx->current;
312 ret = func(parent, ctx);
314 return log_msg_ret("func", ret);
316 /* Add the item to the internal list */
317 if (type != TYPE_NONE) {
318 ret = add_item(ctx, parent, NULL, type, ctx->tab_start);
320 return log_msg_ret("add", ret);
323 device_foreach_child(dev, parent) {
324 ret = acpi_recurse_method(ctx, dev, method, type);
326 return log_msg_ret("recurse", ret);
332 int acpi_fill_ssdt(struct acpi_ctx *ctx)
334 void *start = ctx->current;
337 log_debug("Writing SSDT tables\n");
338 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
339 log_debug("Writing SSDT finished, err=%d\n", ret);
340 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
342 return log_msg_ret("build", ret);
347 int acpi_inject_dsdt(struct acpi_ctx *ctx)
349 void *start = ctx->current;
352 log_debug("Writing DSDT tables\n");
353 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
355 log_debug("Writing DSDT finished, err=%d\n", ret);
356 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
358 return log_msg_ret("build", ret);
363 void acpi_reset_items(void)
368 int acpi_write_dev_tables(struct acpi_ctx *ctx)
372 log_debug("Writing device tables\n");
373 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
375 log_debug("Writing finished, err=%d\n", ret);
380 int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt)
384 log_debug("Setup NHLT\n");
386 ret = acpi_recurse_method(ctx, dm_root(), METHOD_SETUP_NHLT, TYPE_NONE);
387 log_debug("Setup finished, err=%d\n", ret);