blob: 076fb4f1b4754de219028ddb530d83d605234cd0 [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 Glass3c601b12020-07-07 13:12:06 -0600122static struct acpi_item *find_acpi_item(const char *devname)
123{
124 int i;
125
126 for (i = 0; i < item_count; i++) {
127 struct acpi_item *item = &acpi_item[i];
128
129 if (!strcmp(devname, item->dev->name))
130 return item;
131 }
132
133 return NULL;
134}
135
136/**
137 * sort_acpi_item_type - Sort the ACPI items into the desired order
138 *
139 * This looks up the ordering in the device tree and then adds each item one by
140 * one into the supplied buffer
141 *
142 * @ctx: ACPI context
143 * @start: Start position to put the sorted items. The items will follow each
144 * other in sorted order
145 * @type: Type of items to sort
146 * @return 0 if OK, -ve on error
147 */
148static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
149 enum gen_type_t type)
150{
151 const u32 *order;
152 int size;
153 int count;
154 void *ptr;
155 void *end = ctx->current;
156
157 ptr = start;
Simon Glass990cd5b2020-07-07 13:12:08 -0600158 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
159 "u-boot,acpi-dsdt-order" :
160 "u-boot,acpi-ssdt-order", &size);
Simon Glass3c601b12020-07-07 13:12:06 -0600161 if (!order) {
162 log_warning("Failed to find ordering, leaving as is\n");
163 return 0;
164 }
165
166 /*
167 * This algorithm rewrites the context buffer without changing its
168 * length. So there is no need to update ctx-current
169 */
170 count = size / sizeof(u32);
171 while (count--) {
172 struct acpi_item *item;
173 const char *name;
174 ofnode node;
175
176 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
177 name = ofnode_get_name(node);
178 item = find_acpi_item(name);
179 if (!item) {
180 log_err("Failed to find item '%s'\n", name);
181 return log_msg_ret("find", -ENOENT);
182 }
183 if (item->type == type) {
184 log_debug(" - add %s\n", item->dev->name);
185 memcpy(ptr, item->buf, item->size);
186 ptr += item->size;
187 }
188 }
189
190 /*
191 * If the sort order is missing an item then the output will be too
192 * small. Report this error since the item needs to be added to the
193 * ordering for the ACPI tables to be complete.
194 */
195 if (ptr != end) {
196 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
197 return -ENXIO;
198 }
199
200 return 0;
201}
202
Simon Glass17968c32020-04-26 09:19:46 -0600203acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
204{
205 struct acpi_ops *aops;
206
207 aops = device_get_acpi_ops(dev);
208 if (aops) {
209 switch (method) {
210 case METHOD_WRITE_TABLES:
211 return aops->write_tables;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600212 case METHOD_FILL_SSDT:
213 return aops->fill_ssdt;
Simon Glass990cd5b2020-07-07 13:12:08 -0600214 case METHOD_INJECT_DSDT:
215 return aops->inject_dsdt;
Simon Glass17968c32020-04-26 09:19:46 -0600216 }
217 }
218
219 return NULL;
220}
221
222int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
Simon Glassee61e602020-07-07 13:12:05 -0600223 enum method_t method, enum gen_type_t type)
Simon Glass17968c32020-04-26 09:19:46 -0600224{
225 struct udevice *dev;
226 acpi_method func;
227 int ret;
228
229 func = acpi_get_method(parent, method);
230 if (func) {
Simon Glassee61e602020-07-07 13:12:05 -0600231 void *start = ctx->current;
232
Simon Glass17968c32020-04-26 09:19:46 -0600233 log_debug("\n");
234 log_debug("- %s %p\n", parent->name, func);
235 ret = device_ofdata_to_platdata(parent);
236 if (ret)
237 return log_msg_ret("ofdata", ret);
238 ret = func(parent, ctx);
239 if (ret)
240 return log_msg_ret("func", ret);
Simon Glassee61e602020-07-07 13:12:05 -0600241
242 /* Add the item to the internal list */
243 if (type != TYPE_NONE) {
244 ret = acpi_add_item(ctx, parent, type, start);
245 if (ret)
246 return log_msg_ret("add", ret);
247 }
Simon Glass17968c32020-04-26 09:19:46 -0600248 }
249 device_foreach_child(dev, parent) {
Simon Glassee61e602020-07-07 13:12:05 -0600250 ret = acpi_recurse_method(ctx, dev, method, type);
Simon Glass17968c32020-04-26 09:19:46 -0600251 if (ret)
252 return log_msg_ret("recurse", ret);
253 }
254
255 return 0;
256}
257
Simon Glassd43e0ba2020-07-07 13:12:03 -0600258int acpi_fill_ssdt(struct acpi_ctx *ctx)
259{
Simon Glass3c601b12020-07-07 13:12:06 -0600260 void *start = ctx->current;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600261 int ret;
262
263 log_debug("Writing SSDT tables\n");
Simon Glass3c601b12020-07-07 13:12:06 -0600264 item_count = 0;
Simon Glassee61e602020-07-07 13:12:05 -0600265 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600266 log_debug("Writing SSDT finished, err=%d\n", ret);
Simon Glass3c601b12020-07-07 13:12:06 -0600267 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
268 if (ret)
269 return log_msg_ret("build", ret);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600270
271 return ret;
272}
273
Simon Glass990cd5b2020-07-07 13:12:08 -0600274int acpi_inject_dsdt(struct acpi_ctx *ctx)
275{
276 void *start = ctx->current;
277 int ret;
278
279 log_debug("Writing DSDT tables\n");
280 item_count = 0;
281 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
282 TYPE_DSDT);
283 log_debug("Writing DSDT finished, err=%d\n", ret);
284 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
285 if (ret)
286 return log_msg_ret("build", ret);
287
288 return ret;
289}
290
Simon Glass17968c32020-04-26 09:19:46 -0600291int acpi_write_dev_tables(struct acpi_ctx *ctx)
292{
293 int ret;
294
295 log_debug("Writing device tables\n");
Simon Glassee61e602020-07-07 13:12:05 -0600296 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
297 TYPE_NONE);
Simon Glass17968c32020-04-26 09:19:46 -0600298 log_debug("Writing finished, err=%d\n", ret);
299
300 return ret;
301}