blob: b566f4f18646fc715fd8a4bf06484883c7c1662e [file] [log] [blame]
Simon Glass9fb9e9b2020-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 Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass09642392020-07-07 13:12:11 -060014#include <malloc.h>
15#include <acpi/acpi_device.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060016#include <dm/acpi.h>
Simon Glass17968c32020-04-26 09:19:46 -060017#include <dm/device-internal.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060018#include <dm/root.h>
19
Simon Glassee61e602020-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 Glass990cd5b2020-07-07 13:12:08 -060026 TYPE_DSDT,
Simon Glassee61e602020-07-07 13:12:05 -060027};
28
Simon Glass17968c32020-04-26 09:19:46 -060029/* Type of method to call */
30enum method_t {
31 METHOD_WRITE_TABLES,
Simon Glassd43e0ba2020-07-07 13:12:03 -060032 METHOD_FILL_SSDT,
Simon Glass990cd5b2020-07-07 13:12:08 -060033 METHOD_INJECT_DSDT,
Simon Glass17968c32020-04-26 09:19:46 -060034};
35
36/* Prototype for all methods */
37typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
38
Simon Glassee61e602020-07-07 13:12:05 -060039/**
40 * struct acpi_item - Holds info about ACPI data generated by a driver method
41 *
42 * @dev: Device that generated this data
43 * @type: Table type it refers to
44 * @buf: Buffer containing the data
45 * @size: Size of the data in bytes
46 */
47struct acpi_item {
48 struct udevice *dev;
49 enum gen_type_t type;
50 char *buf;
51 int size;
52};
53
54/* List of ACPI items collected */
55static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
56static int item_count;
57
Simon Glass9fb9e9b2020-04-09 10:27:38 -060058int acpi_copy_name(char *out_name, const char *name)
59{
60 strncpy(out_name, name, ACPI_NAME_LEN);
61 out_name[ACPI_NAME_LEN] = '\0';
62
63 return 0;
64}
65
66int acpi_get_name(const struct udevice *dev, char *out_name)
67{
68 struct acpi_ops *aops;
Simon Glass09642392020-07-07 13:12:11 -060069 const char *name;
70 int ret;
Simon Glass9fb9e9b2020-04-09 10:27:38 -060071
72 aops = device_get_acpi_ops(dev);
73 if (aops && aops->get_name)
74 return aops->get_name(dev, out_name);
Simon Glass09642392020-07-07 13:12:11 -060075 name = dev_read_string(dev, "acpi,name");
76 if (name)
77 return acpi_copy_name(out_name, name);
78 ret = acpi_device_infer_name(dev, out_name);
79 if (ret)
80 return log_msg_ret("dev", ret);
Simon Glass9fb9e9b2020-04-09 10:27:38 -060081
Simon Glass09642392020-07-07 13:12:11 -060082 return 0;
Simon Glass9fb9e9b2020-04-09 10:27:38 -060083}
Simon Glass17968c32020-04-26 09:19:46 -060084
Simon Glassee61e602020-07-07 13:12:05 -060085/**
86 * acpi_add_item() - Add a new item to the list of data collected
87 *
88 * @ctx: ACPI context
89 * @dev: Device that generated the data
90 * @type: Table type it refers to
91 * @start: The start of the data (the end is obtained from ctx->current)
92 * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
93 */
94static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
95 enum gen_type_t type, void *start)
96{
97 struct acpi_item *item;
98 void *end = ctx->current;
99
100 if (item_count == MAX_ACPI_ITEMS) {
101 log_err("Too many items\n");
102 return log_msg_ret("mem", -ENOSPC);
103 }
104
105 item = &acpi_item[item_count];
106 item->dev = dev;
107 item->type = type;
108 item->size = end - start;
109 if (!item->size)
110 return 0;
111 item->buf = malloc(item->size);
112 if (!item->buf)
113 return log_msg_ret("mem", -ENOMEM);
114 memcpy(item->buf, start, item->size);
115 item_count++;
116 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
117 item->size);
118
119 return 0;
120}
121
Simon Glass64bbc0d2020-07-07 13:12:12 -0600122void acpi_dump_items(enum acpi_dump_option option)
123{
124 int i;
125
126 for (i = 0; i < item_count; i++) {
127 struct acpi_item *item = &acpi_item[i];
128
129 printf("dev '%s', type %d, size %x\n", item->dev->name,
130 item->type, item->size);
131 if (option == ACPI_DUMP_CONTENTS) {
132 print_buffer(0, item->buf, 1, item->size, 0);
133 printf("\n");
134 }
135 }
136}
137
Simon Glass3c601b12020-07-07 13:12:06 -0600138static struct acpi_item *find_acpi_item(const char *devname)
139{
140 int i;
141
142 for (i = 0; i < item_count; i++) {
143 struct acpi_item *item = &acpi_item[i];
144
145 if (!strcmp(devname, item->dev->name))
146 return item;
147 }
148
149 return NULL;
150}
151
152/**
153 * sort_acpi_item_type - Sort the ACPI items into the desired order
154 *
155 * This looks up the ordering in the device tree and then adds each item one by
156 * one into the supplied buffer
157 *
158 * @ctx: ACPI context
159 * @start: Start position to put the sorted items. The items will follow each
160 * other in sorted order
161 * @type: Type of items to sort
162 * @return 0 if OK, -ve on error
163 */
164static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
165 enum gen_type_t type)
166{
167 const u32 *order;
168 int size;
169 int count;
170 void *ptr;
171 void *end = ctx->current;
172
173 ptr = start;
Simon Glass990cd5b2020-07-07 13:12:08 -0600174 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
175 "u-boot,acpi-dsdt-order" :
176 "u-boot,acpi-ssdt-order", &size);
Simon Glass3c601b12020-07-07 13:12:06 -0600177 if (!order) {
178 log_warning("Failed to find ordering, leaving as is\n");
179 return 0;
180 }
181
182 /*
183 * This algorithm rewrites the context buffer without changing its
184 * length. So there is no need to update ctx-current
185 */
186 count = size / sizeof(u32);
187 while (count--) {
188 struct acpi_item *item;
189 const char *name;
190 ofnode node;
191
192 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
193 name = ofnode_get_name(node);
194 item = find_acpi_item(name);
195 if (!item) {
196 log_err("Failed to find item '%s'\n", name);
197 return log_msg_ret("find", -ENOENT);
198 }
199 if (item->type == type) {
200 log_debug(" - add %s\n", item->dev->name);
201 memcpy(ptr, item->buf, item->size);
202 ptr += item->size;
203 }
204 }
205
206 /*
207 * If the sort order is missing an item then the output will be too
208 * small. Report this error since the item needs to be added to the
209 * ordering for the ACPI tables to be complete.
210 */
211 if (ptr != end) {
212 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
213 return -ENXIO;
214 }
215
216 return 0;
217}
218
Simon Glass17968c32020-04-26 09:19:46 -0600219acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
220{
221 struct acpi_ops *aops;
222
223 aops = device_get_acpi_ops(dev);
224 if (aops) {
225 switch (method) {
226 case METHOD_WRITE_TABLES:
227 return aops->write_tables;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600228 case METHOD_FILL_SSDT:
229 return aops->fill_ssdt;
Simon Glass990cd5b2020-07-07 13:12:08 -0600230 case METHOD_INJECT_DSDT:
231 return aops->inject_dsdt;
Simon Glass17968c32020-04-26 09:19:46 -0600232 }
233 }
234
235 return NULL;
236}
237
238int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
Simon Glassee61e602020-07-07 13:12:05 -0600239 enum method_t method, enum gen_type_t type)
Simon Glass17968c32020-04-26 09:19:46 -0600240{
241 struct udevice *dev;
242 acpi_method func;
243 int ret;
244
245 func = acpi_get_method(parent, method);
246 if (func) {
Simon Glassee61e602020-07-07 13:12:05 -0600247 void *start = ctx->current;
248
Simon Glass17968c32020-04-26 09:19:46 -0600249 log_debug("\n");
250 log_debug("- %s %p\n", parent->name, func);
251 ret = device_ofdata_to_platdata(parent);
252 if (ret)
253 return log_msg_ret("ofdata", ret);
254 ret = func(parent, ctx);
255 if (ret)
256 return log_msg_ret("func", ret);
Simon Glassee61e602020-07-07 13:12:05 -0600257
258 /* Add the item to the internal list */
259 if (type != TYPE_NONE) {
260 ret = acpi_add_item(ctx, parent, type, start);
261 if (ret)
262 return log_msg_ret("add", ret);
263 }
Simon Glass17968c32020-04-26 09:19:46 -0600264 }
265 device_foreach_child(dev, parent) {
Simon Glassee61e602020-07-07 13:12:05 -0600266 ret = acpi_recurse_method(ctx, dev, method, type);
Simon Glass17968c32020-04-26 09:19:46 -0600267 if (ret)
268 return log_msg_ret("recurse", ret);
269 }
270
271 return 0;
272}
273
Simon Glassd43e0ba2020-07-07 13:12:03 -0600274int acpi_fill_ssdt(struct acpi_ctx *ctx)
275{
Simon Glass3c601b12020-07-07 13:12:06 -0600276 void *start = ctx->current;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600277 int ret;
278
279 log_debug("Writing SSDT tables\n");
Simon Glass3c601b12020-07-07 13:12:06 -0600280 item_count = 0;
Simon Glassee61e602020-07-07 13:12:05 -0600281 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600282 log_debug("Writing SSDT finished, err=%d\n", ret);
Simon Glass3c601b12020-07-07 13:12:06 -0600283 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
284 if (ret)
285 return log_msg_ret("build", ret);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600286
287 return ret;
288}
289
Simon Glass990cd5b2020-07-07 13:12:08 -0600290int acpi_inject_dsdt(struct acpi_ctx *ctx)
291{
292 void *start = ctx->current;
293 int ret;
294
295 log_debug("Writing DSDT tables\n");
296 item_count = 0;
297 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
298 TYPE_DSDT);
299 log_debug("Writing DSDT finished, err=%d\n", ret);
300 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
301 if (ret)
302 return log_msg_ret("build", ret);
303
304 return ret;
305}
306
Simon Glass17968c32020-04-26 09:19:46 -0600307int acpi_write_dev_tables(struct acpi_ctx *ctx)
308{
309 int ret;
310
311 log_debug("Writing device tables\n");
Simon Glassee61e602020-07-07 13:12:05 -0600312 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
313 TYPE_NONE);
Simon Glass17968c32020-04-26 09:19:46 -0600314 log_debug("Writing finished, err=%d\n", ret);
315
316 return ret;
317}