blob: 7fe93992b5fa8933fe9c7da48650f93b93bcde8a [file] [log] [blame]
Simon Glass7ca28502020-04-09 10:27:38 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Core driver model support for ACPI table generation
4 *
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEOGRY LOGC_ACPI
10
11#include <common.h>
12#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glassfefac0b2020-07-07 13:12:11 -060014#include <malloc.h>
15#include <acpi/acpi_device.h>
Simon Glass7ca28502020-04-09 10:27:38 -060016#include <dm/acpi.h>
Simon Glass93f7f822020-04-26 09:19:46 -060017#include <dm/device-internal.h>
Simon Glass7ca28502020-04-09 10:27:38 -060018#include <dm/root.h>
19
Simon Glass64ba6f42020-07-07 13:12:05 -060020#define MAX_ACPI_ITEMS 100
21
22/* Type of table that we collected */
23enum gen_type_t {
24 TYPE_NONE,
25 TYPE_SSDT,
Simon Glass01694582020-07-07 13:12:08 -060026 TYPE_DSDT,
Simon Glass64ba6f42020-07-07 13:12:05 -060027};
28
Simon Glass93f7f822020-04-26 09:19:46 -060029/* Type of method to call */
30enum method_t {
31 METHOD_WRITE_TABLES,
Simon Glassb5183172020-07-07 13:12:03 -060032 METHOD_FILL_SSDT,
Simon Glass01694582020-07-07 13:12:08 -060033 METHOD_INJECT_DSDT,
Simon Glassb4e84332020-07-07 21:32:08 -060034 METHOD_SETUP_NHLT,
Simon Glass93f7f822020-04-26 09:19:46 -060035};
36
37/* Prototype for all methods */
38typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
39
Simon Glass64ba6f42020-07-07 13:12:05 -060040/**
41 * struct acpi_item - Holds info about ACPI data generated by a driver method
42 *
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
47 */
48struct acpi_item {
49 struct udevice *dev;
50 enum gen_type_t type;
51 char *buf;
52 int size;
53};
54
55/* List of ACPI items collected */
56static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
57static int item_count;
58
Simon Glass7ca28502020-04-09 10:27:38 -060059int acpi_copy_name(char *out_name, const char *name)
60{
61 strncpy(out_name, name, ACPI_NAME_LEN);
62 out_name[ACPI_NAME_LEN] = '\0';
63
64 return 0;
65}
66
67int acpi_get_name(const struct udevice *dev, char *out_name)
68{
69 struct acpi_ops *aops;
Simon Glassfefac0b2020-07-07 13:12:11 -060070 const char *name;
71 int ret;
Simon Glass7ca28502020-04-09 10:27:38 -060072
73 aops = device_get_acpi_ops(dev);
74 if (aops && aops->get_name)
75 return aops->get_name(dev, out_name);
Simon Glassfefac0b2020-07-07 13:12:11 -060076 name = dev_read_string(dev, "acpi,name");
77 if (name)
78 return acpi_copy_name(out_name, name);
79 ret = acpi_device_infer_name(dev, out_name);
80 if (ret)
81 return log_msg_ret("dev", ret);
Simon Glass7ca28502020-04-09 10:27:38 -060082
Simon Glassfefac0b2020-07-07 13:12:11 -060083 return 0;
Simon Glass7ca28502020-04-09 10:27:38 -060084}
Simon Glass93f7f822020-04-26 09:19:46 -060085
Simon Glassf1858952020-07-07 21:32:07 -060086int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen)
87{
88 const char *path;
89 int ret;
90
91 path = dev_read_string(dev, "acpi,path");
92 if (path) {
93 if (strlen(path) >= maxlen)
94 return -E2BIG;
95 strcpy(out_path, path);
96 return 0;
97 }
98 ret = acpi_device_path(dev, out_path, maxlen);
99 if (ret)
100 return log_msg_ret("dev", ret);
101
102 return 0;
103}
104
Simon Glass64ba6f42020-07-07 13:12:05 -0600105/**
106 * acpi_add_item() - Add a new item to the list of data collected
107 *
108 * @ctx: ACPI context
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
113 */
114static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
115 enum gen_type_t type, void *start)
116{
117 struct acpi_item *item;
118 void *end = ctx->current;
119
120 if (item_count == MAX_ACPI_ITEMS) {
121 log_err("Too many items\n");
122 return log_msg_ret("mem", -ENOSPC);
123 }
124
125 item = &acpi_item[item_count];
126 item->dev = dev;
127 item->type = type;
128 item->size = end - start;
129 if (!item->size)
130 return 0;
131 item->buf = malloc(item->size);
132 if (!item->buf)
133 return log_msg_ret("mem", -ENOMEM);
134 memcpy(item->buf, start, item->size);
135 item_count++;
136 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
137 item->size);
138
139 return 0;
140}
141
Simon Glassa4f82082020-07-07 13:12:12 -0600142void acpi_dump_items(enum acpi_dump_option option)
143{
144 int i;
145
146 for (i = 0; i < item_count; i++) {
147 struct acpi_item *item = &acpi_item[i];
148
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);
153 printf("\n");
154 }
155 }
156}
157
Simon Glass0f7b1112020-07-07 13:12:06 -0600158static struct acpi_item *find_acpi_item(const char *devname)
159{
160 int i;
161
162 for (i = 0; i < item_count; i++) {
163 struct acpi_item *item = &acpi_item[i];
164
165 if (!strcmp(devname, item->dev->name))
166 return item;
167 }
168
169 return NULL;
170}
171
172/**
173 * sort_acpi_item_type - Sort the ACPI items into the desired order
174 *
175 * This looks up the ordering in the device tree and then adds each item one by
176 * one into the supplied buffer
177 *
178 * @ctx: ACPI context
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
183 */
184static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
185 enum gen_type_t type)
186{
187 const u32 *order;
188 int size;
189 int count;
190 void *ptr;
191 void *end = ctx->current;
192
193 ptr = start;
Simon Glass01694582020-07-07 13:12:08 -0600194 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
195 "u-boot,acpi-dsdt-order" :
196 "u-boot,acpi-ssdt-order", &size);
Simon Glass0f7b1112020-07-07 13:12:06 -0600197 if (!order) {
Simon Glassd2ee5432020-07-17 08:49:25 -0600198 log_debug("Failed to find ordering, leaving as is\n");
Simon Glass0f7b1112020-07-07 13:12:06 -0600199 return 0;
200 }
201
202 /*
203 * This algorithm rewrites the context buffer without changing its
204 * length. So there is no need to update ctx-current
205 */
206 count = size / sizeof(u32);
207 while (count--) {
208 struct acpi_item *item;
209 const char *name;
210 ofnode node;
211
212 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
213 name = ofnode_get_name(node);
214 item = find_acpi_item(name);
215 if (!item) {
216 log_err("Failed to find item '%s'\n", name);
217 return log_msg_ret("find", -ENOENT);
218 }
219 if (item->type == type) {
220 log_debug(" - add %s\n", item->dev->name);
221 memcpy(ptr, item->buf, item->size);
222 ptr += item->size;
223 }
224 }
225
226 /*
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.
230 */
231 if (ptr != end) {
232 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
233 return -ENXIO;
234 }
235
236 return 0;
237}
238
Simon Glass93f7f822020-04-26 09:19:46 -0600239acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
240{
241 struct acpi_ops *aops;
242
243 aops = device_get_acpi_ops(dev);
244 if (aops) {
245 switch (method) {
246 case METHOD_WRITE_TABLES:
247 return aops->write_tables;
Simon Glassb5183172020-07-07 13:12:03 -0600248 case METHOD_FILL_SSDT:
249 return aops->fill_ssdt;
Simon Glass01694582020-07-07 13:12:08 -0600250 case METHOD_INJECT_DSDT:
251 return aops->inject_dsdt;
Simon Glassb4e84332020-07-07 21:32:08 -0600252 case METHOD_SETUP_NHLT:
253 return aops->setup_nhlt;
Simon Glass93f7f822020-04-26 09:19:46 -0600254 }
255 }
256
257 return NULL;
258}
259
260int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
Simon Glass64ba6f42020-07-07 13:12:05 -0600261 enum method_t method, enum gen_type_t type)
Simon Glass93f7f822020-04-26 09:19:46 -0600262{
263 struct udevice *dev;
264 acpi_method func;
265 int ret;
266
267 func = acpi_get_method(parent, method);
268 if (func) {
Simon Glass64ba6f42020-07-07 13:12:05 -0600269 void *start = ctx->current;
270
Simon Glass93f7f822020-04-26 09:19:46 -0600271 log_debug("\n");
272 log_debug("- %s %p\n", parent->name, func);
273 ret = device_ofdata_to_platdata(parent);
274 if (ret)
275 return log_msg_ret("ofdata", ret);
276 ret = func(parent, ctx);
277 if (ret)
278 return log_msg_ret("func", ret);
Simon Glass64ba6f42020-07-07 13:12:05 -0600279
280 /* Add the item to the internal list */
281 if (type != TYPE_NONE) {
282 ret = acpi_add_item(ctx, parent, type, start);
283 if (ret)
284 return log_msg_ret("add", ret);
285 }
Simon Glass93f7f822020-04-26 09:19:46 -0600286 }
287 device_foreach_child(dev, parent) {
Simon Glass64ba6f42020-07-07 13:12:05 -0600288 ret = acpi_recurse_method(ctx, dev, method, type);
Simon Glass93f7f822020-04-26 09:19:46 -0600289 if (ret)
290 return log_msg_ret("recurse", ret);
291 }
292
293 return 0;
294}
295
Simon Glassb5183172020-07-07 13:12:03 -0600296int acpi_fill_ssdt(struct acpi_ctx *ctx)
297{
Simon Glass0f7b1112020-07-07 13:12:06 -0600298 void *start = ctx->current;
Simon Glassb5183172020-07-07 13:12:03 -0600299 int ret;
300
301 log_debug("Writing SSDT tables\n");
Simon Glass0f7b1112020-07-07 13:12:06 -0600302 item_count = 0;
Simon Glass64ba6f42020-07-07 13:12:05 -0600303 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
Simon Glassb5183172020-07-07 13:12:03 -0600304 log_debug("Writing SSDT finished, err=%d\n", ret);
Simon Glass0f7b1112020-07-07 13:12:06 -0600305 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
306 if (ret)
307 return log_msg_ret("build", ret);
Simon Glassb5183172020-07-07 13:12:03 -0600308
309 return ret;
310}
311
Simon Glass01694582020-07-07 13:12:08 -0600312int acpi_inject_dsdt(struct acpi_ctx *ctx)
313{
314 void *start = ctx->current;
315 int ret;
316
317 log_debug("Writing DSDT tables\n");
318 item_count = 0;
319 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
320 TYPE_DSDT);
321 log_debug("Writing DSDT finished, err=%d\n", ret);
322 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
323 if (ret)
324 return log_msg_ret("build", ret);
325
326 return ret;
327}
328
Simon Glass93f7f822020-04-26 09:19:46 -0600329int acpi_write_dev_tables(struct acpi_ctx *ctx)
330{
331 int ret;
332
333 log_debug("Writing device tables\n");
Simon Glass64ba6f42020-07-07 13:12:05 -0600334 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
335 TYPE_NONE);
Simon Glass93f7f822020-04-26 09:19:46 -0600336 log_debug("Writing finished, err=%d\n", ret);
337
338 return ret;
339}
Simon Glassb4e84332020-07-07 21:32:08 -0600340
341int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt)
342{
343 int ret;
344
345 log_debug("Setup NHLT\n");
346 ctx->nhlt = nhlt;
347 ret = acpi_recurse_method(ctx, dm_root(), METHOD_SETUP_NHLT, TYPE_NONE);
348 log_debug("Setup finished, err=%d\n", ret);
349
350 return ret;
351}