blob: 7b32694ad43f2dbd077a9f47686071cfb1495c1b [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>
Simon Glassee61e602020-07-07 13:12:05 -060012#include <malloc.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060015#include <dm/acpi.h>
Simon Glass17968c32020-04-26 09:19:46 -060016#include <dm/device-internal.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060017#include <dm/root.h>
18
Simon Glassee61e602020-07-07 13:12:05 -060019#define MAX_ACPI_ITEMS 100
20
21/* Type of table that we collected */
22enum gen_type_t {
23 TYPE_NONE,
24 TYPE_SSDT,
Simon Glass990cd5b2020-07-07 13:12:08 -060025 TYPE_DSDT,
Simon Glassee61e602020-07-07 13:12:05 -060026};
27
Simon Glass17968c32020-04-26 09:19:46 -060028/* Type of method to call */
29enum method_t {
30 METHOD_WRITE_TABLES,
Simon Glassd43e0ba2020-07-07 13:12:03 -060031 METHOD_FILL_SSDT,
Simon Glass990cd5b2020-07-07 13:12:08 -060032 METHOD_INJECT_DSDT,
Simon Glass17968c32020-04-26 09:19:46 -060033};
34
35/* Prototype for all methods */
36typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
37
Simon Glassee61e602020-07-07 13:12:05 -060038/**
39 * struct acpi_item - Holds info about ACPI data generated by a driver method
40 *
41 * @dev: Device that generated this data
42 * @type: Table type it refers to
43 * @buf: Buffer containing the data
44 * @size: Size of the data in bytes
45 */
46struct acpi_item {
47 struct udevice *dev;
48 enum gen_type_t type;
49 char *buf;
50 int size;
51};
52
53/* List of ACPI items collected */
54static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
55static int item_count;
56
Simon Glass9fb9e9b2020-04-09 10:27:38 -060057int acpi_copy_name(char *out_name, const char *name)
58{
59 strncpy(out_name, name, ACPI_NAME_LEN);
60 out_name[ACPI_NAME_LEN] = '\0';
61
62 return 0;
63}
64
65int acpi_get_name(const struct udevice *dev, char *out_name)
66{
67 struct acpi_ops *aops;
68
69 aops = device_get_acpi_ops(dev);
70 if (aops && aops->get_name)
71 return aops->get_name(dev, out_name);
72
73 return -ENOSYS;
74}
Simon Glass17968c32020-04-26 09:19:46 -060075
Simon Glassee61e602020-07-07 13:12:05 -060076/**
77 * acpi_add_item() - Add a new item to the list of data collected
78 *
79 * @ctx: ACPI context
80 * @dev: Device that generated the data
81 * @type: Table type it refers to
82 * @start: The start of the data (the end is obtained from ctx->current)
83 * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
84 */
85static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
86 enum gen_type_t type, void *start)
87{
88 struct acpi_item *item;
89 void *end = ctx->current;
90
91 if (item_count == MAX_ACPI_ITEMS) {
92 log_err("Too many items\n");
93 return log_msg_ret("mem", -ENOSPC);
94 }
95
96 item = &acpi_item[item_count];
97 item->dev = dev;
98 item->type = type;
99 item->size = end - start;
100 if (!item->size)
101 return 0;
102 item->buf = malloc(item->size);
103 if (!item->buf)
104 return log_msg_ret("mem", -ENOMEM);
105 memcpy(item->buf, start, item->size);
106 item_count++;
107 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
108 item->size);
109
110 return 0;
111}
112
Simon Glass3c601b12020-07-07 13:12:06 -0600113static struct acpi_item *find_acpi_item(const char *devname)
114{
115 int i;
116
117 for (i = 0; i < item_count; i++) {
118 struct acpi_item *item = &acpi_item[i];
119
120 if (!strcmp(devname, item->dev->name))
121 return item;
122 }
123
124 return NULL;
125}
126
127/**
128 * sort_acpi_item_type - Sort the ACPI items into the desired order
129 *
130 * This looks up the ordering in the device tree and then adds each item one by
131 * one into the supplied buffer
132 *
133 * @ctx: ACPI context
134 * @start: Start position to put the sorted items. The items will follow each
135 * other in sorted order
136 * @type: Type of items to sort
137 * @return 0 if OK, -ve on error
138 */
139static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
140 enum gen_type_t type)
141{
142 const u32 *order;
143 int size;
144 int count;
145 void *ptr;
146 void *end = ctx->current;
147
148 ptr = start;
Simon Glass990cd5b2020-07-07 13:12:08 -0600149 order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
150 "u-boot,acpi-dsdt-order" :
151 "u-boot,acpi-ssdt-order", &size);
Simon Glass3c601b12020-07-07 13:12:06 -0600152 if (!order) {
153 log_warning("Failed to find ordering, leaving as is\n");
154 return 0;
155 }
156
157 /*
158 * This algorithm rewrites the context buffer without changing its
159 * length. So there is no need to update ctx-current
160 */
161 count = size / sizeof(u32);
162 while (count--) {
163 struct acpi_item *item;
164 const char *name;
165 ofnode node;
166
167 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
168 name = ofnode_get_name(node);
169 item = find_acpi_item(name);
170 if (!item) {
171 log_err("Failed to find item '%s'\n", name);
172 return log_msg_ret("find", -ENOENT);
173 }
174 if (item->type == type) {
175 log_debug(" - add %s\n", item->dev->name);
176 memcpy(ptr, item->buf, item->size);
177 ptr += item->size;
178 }
179 }
180
181 /*
182 * If the sort order is missing an item then the output will be too
183 * small. Report this error since the item needs to be added to the
184 * ordering for the ACPI tables to be complete.
185 */
186 if (ptr != end) {
187 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
188 return -ENXIO;
189 }
190
191 return 0;
192}
193
Simon Glass17968c32020-04-26 09:19:46 -0600194acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
195{
196 struct acpi_ops *aops;
197
198 aops = device_get_acpi_ops(dev);
199 if (aops) {
200 switch (method) {
201 case METHOD_WRITE_TABLES:
202 return aops->write_tables;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600203 case METHOD_FILL_SSDT:
204 return aops->fill_ssdt;
Simon Glass990cd5b2020-07-07 13:12:08 -0600205 case METHOD_INJECT_DSDT:
206 return aops->inject_dsdt;
Simon Glass17968c32020-04-26 09:19:46 -0600207 }
208 }
209
210 return NULL;
211}
212
213int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
Simon Glassee61e602020-07-07 13:12:05 -0600214 enum method_t method, enum gen_type_t type)
Simon Glass17968c32020-04-26 09:19:46 -0600215{
216 struct udevice *dev;
217 acpi_method func;
218 int ret;
219
220 func = acpi_get_method(parent, method);
221 if (func) {
Simon Glassee61e602020-07-07 13:12:05 -0600222 void *start = ctx->current;
223
Simon Glass17968c32020-04-26 09:19:46 -0600224 log_debug("\n");
225 log_debug("- %s %p\n", parent->name, func);
226 ret = device_ofdata_to_platdata(parent);
227 if (ret)
228 return log_msg_ret("ofdata", ret);
229 ret = func(parent, ctx);
230 if (ret)
231 return log_msg_ret("func", ret);
Simon Glassee61e602020-07-07 13:12:05 -0600232
233 /* Add the item to the internal list */
234 if (type != TYPE_NONE) {
235 ret = acpi_add_item(ctx, parent, type, start);
236 if (ret)
237 return log_msg_ret("add", ret);
238 }
Simon Glass17968c32020-04-26 09:19:46 -0600239 }
240 device_foreach_child(dev, parent) {
Simon Glassee61e602020-07-07 13:12:05 -0600241 ret = acpi_recurse_method(ctx, dev, method, type);
Simon Glass17968c32020-04-26 09:19:46 -0600242 if (ret)
243 return log_msg_ret("recurse", ret);
244 }
245
246 return 0;
247}
248
Simon Glassd43e0ba2020-07-07 13:12:03 -0600249int acpi_fill_ssdt(struct acpi_ctx *ctx)
250{
Simon Glass3c601b12020-07-07 13:12:06 -0600251 void *start = ctx->current;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600252 int ret;
253
254 log_debug("Writing SSDT tables\n");
Simon Glass3c601b12020-07-07 13:12:06 -0600255 item_count = 0;
Simon Glassee61e602020-07-07 13:12:05 -0600256 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600257 log_debug("Writing SSDT finished, err=%d\n", ret);
Simon Glass3c601b12020-07-07 13:12:06 -0600258 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
259 if (ret)
260 return log_msg_ret("build", ret);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600261
262 return ret;
263}
264
Simon Glass990cd5b2020-07-07 13:12:08 -0600265int acpi_inject_dsdt(struct acpi_ctx *ctx)
266{
267 void *start = ctx->current;
268 int ret;
269
270 log_debug("Writing DSDT tables\n");
271 item_count = 0;
272 ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
273 TYPE_DSDT);
274 log_debug("Writing DSDT finished, err=%d\n", ret);
275 ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
276 if (ret)
277 return log_msg_ret("build", ret);
278
279 return ret;
280}
281
Simon Glass17968c32020-04-26 09:19:46 -0600282int acpi_write_dev_tables(struct acpi_ctx *ctx)
283{
284 int ret;
285
286 log_debug("Writing device tables\n");
Simon Glassee61e602020-07-07 13:12:05 -0600287 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
288 TYPE_NONE);
Simon Glass17968c32020-04-26 09:19:46 -0600289 log_debug("Writing finished, err=%d\n", ret);
290
291 return ret;
292}