1 // SPDX-License-Identifier: GPL-2.0+
3 * Core driver model support for ACPI table generation
5 * Copyright 2019 Google LLC
9 #define LOG_CATEOGRY LOGC_ACPI
15 #include <acpi/acpi_device.h>
17 #include <dm/device-internal.h>
20 #define MAX_ACPI_ITEMS 100
22 /* Type of table that we collected */
29 /* Type of method to call */
37 /* Prototype for all methods */
38 typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
41 * struct acpi_item - Holds info about ACPI data generated by a driver method
43 * @dev: Device that generated this data
44 * @type: Table type it refers to
45 * @buf: Buffer containing the data
46 * @size: Size of the data in bytes
55 /* List of ACPI items collected */
56 static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
57 static int item_count;
59 int acpi_copy_name(char *out_name, const char *name)
61 strncpy(out_name, name, ACPI_NAME_LEN);
62 out_name[ACPI_NAME_LEN] = '\0';
67 int acpi_get_name(const struct udevice *dev, char *out_name)
69 struct acpi_ops *aops;
73 aops = device_get_acpi_ops(dev);
74 if (aops && aops->get_name)
75 return aops->get_name(dev, out_name);
76 name = dev_read_string(dev, "acpi,name");
78 return acpi_copy_name(out_name, name);
79 ret = acpi_device_infer_name(dev, out_name);
81 return log_msg_ret("dev", ret);
86 int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen)
91 path = dev_read_string(dev, "acpi,path");
93 if (strlen(path) >= maxlen)
95 strcpy(out_path, path);
98 ret = acpi_device_path(dev, out_path, maxlen);
100 return log_msg_ret("dev", ret);
106 * acpi_add_item() - Add a new item to the list of data collected
109 * @dev: Device that generated the data
110 * @type: Table type it refers to
111 * @start: The start of the data (the end is obtained from ctx->current)
112 * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
114 static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
115 enum gen_type_t type, void *start)
117 struct acpi_item *item;
118 void *end = ctx->current;
120 if (item_count == MAX_ACPI_ITEMS) {
121 log_err("Too many items\n");
122 return log_msg_ret("mem", -ENOSPC);
125 item = &acpi_item[item_count];
128 item->size = end - start;
131 item->buf = malloc(item->size);
133 return log_msg_ret("mem", -ENOMEM);
134 memcpy(item->buf, start, item->size);
136 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
142 void acpi_dump_items(enum acpi_dump_option option)
146 for (i = 0; i < item_count; i++) {
147 struct acpi_item *item = &acpi_item[i];
149 printf("dev '%s', type %d, size %x\n", item->dev->name,
150 item->type, item->size);
151 if (option == ACPI_DUMP_CONTENTS) {
152 print_buffer(0, item->buf, 1, item->size, 0);
158 static struct acpi_item *find_acpi_item(const char *devname)
162 for (i = 0; i < item_count; i++) {
163 struct acpi_item *item = &acpi_item[i];
165 if (!strcmp(devname, item->dev->name))
173 * sort_acpi_item_type - Sort the ACPI items into the desired order
175 * This looks up the ordering in the device tree and then adds each item one by
176 * one into the supplied buffer
179 * @start: Start position to put the sorted items. The items will follow each
180 * other in sorted order
181 * @type: Type of items to sort
182 * @return 0 if OK, -ve on error
184 static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
185 enum gen_type_t type)
191 void *end = ctx->current;
194 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
195 "u-boot,acpi-dsdt-order" :
196 "u-boot,acpi-ssdt-order", &size);
198 log_debug("Failed to find ordering, leaving as is\n");
203 * This algorithm rewrites the context buffer without changing its
204 * length. So there is no need to update ctx-current
206 count = size / sizeof(u32);
208 struct acpi_item *item;
212 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
213 name = ofnode_get_name(node);
214 item = find_acpi_item(name);
216 log_err("Failed to find item '%s'\n", name);
217 return log_msg_ret("find", -ENOENT);
219 if (item->type == type) {
220 log_debug(" - add %s\n", item->dev->name);
221 memcpy(ptr, item->buf, item->size);
227 * If the sort order is missing an item then the output will be too
228 * small. Report this error since the item needs to be added to the
229 * ordering for the ACPI tables to be complete.
232 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
239 acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
241 struct acpi_ops *aops;
243 aops = device_get_acpi_ops(dev);
246 case METHOD_WRITE_TABLES:
247 return aops->write_tables;
248 case METHOD_FILL_SSDT:
249 return aops->fill_ssdt;
250 case METHOD_INJECT_DSDT:
251 return aops->inject_dsdt;
252 case METHOD_SETUP_NHLT:
253 return aops->setup_nhlt;
260 int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
261 enum method_t method, enum gen_type_t type)
267 func = acpi_get_method(parent, method);
269 void *start = ctx->current;
271 log_debug("- method %d, %s %p\n", method, parent->name, func);
272 ret = device_of_to_plat(parent);
274 return log_msg_ret("ofdata", ret);
275 ret = func(parent, ctx);
277 return log_msg_ret("func", ret);
279 /* Add the item to the internal list */
280 if (type != TYPE_NONE) {
281 ret = acpi_add_item(ctx, parent, type, start);
283 return log_msg_ret("add", ret);
286 device_foreach_child(dev, parent) {
287 ret = acpi_recurse_method(ctx, dev, method, type);
289 return log_msg_ret("recurse", ret);
295 int acpi_fill_ssdt(struct acpi_ctx *ctx)
297 void *start = ctx->current;
300 log_debug("Writing SSDT tables\n");
301 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
302 log_debug("Writing SSDT finished, err=%d\n", ret);
303 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
305 return log_msg_ret("build", ret);
310 int acpi_inject_dsdt(struct acpi_ctx *ctx)
312 void *start = ctx->current;
315 log_debug("Writing DSDT tables\n");
316 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
318 log_debug("Writing DSDT finished, err=%d\n", ret);
319 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
321 return log_msg_ret("build", ret);
326 void acpi_reset_items(void)
331 int acpi_write_dev_tables(struct acpi_ctx *ctx)
335 log_debug("Writing device tables\n");
336 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
338 log_debug("Writing finished, err=%d\n", ret);
343 int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt)
347 log_debug("Setup NHLT\n");
349 ret = acpi_recurse_method(ctx, dm_root(), METHOD_SETUP_NHLT, TYPE_NONE);
350 log_debug("Setup finished, err=%d\n", ret);