blob: a9b7fc1d9a077156776b187b64c57f13ba55759a [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,
25};
26
Simon Glass17968c32020-04-26 09:19:46 -060027/* Type of method to call */
28enum method_t {
29 METHOD_WRITE_TABLES,
Simon Glassd43e0ba2020-07-07 13:12:03 -060030 METHOD_FILL_SSDT,
Simon Glass17968c32020-04-26 09:19:46 -060031};
32
33/* Prototype for all methods */
34typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
35
Simon Glassee61e602020-07-07 13:12:05 -060036/**
37 * struct acpi_item - Holds info about ACPI data generated by a driver method
38 *
39 * @dev: Device that generated this data
40 * @type: Table type it refers to
41 * @buf: Buffer containing the data
42 * @size: Size of the data in bytes
43 */
44struct acpi_item {
45 struct udevice *dev;
46 enum gen_type_t type;
47 char *buf;
48 int size;
49};
50
51/* List of ACPI items collected */
52static struct acpi_item acpi_item[MAX_ACPI_ITEMS];
53static int item_count;
54
Simon Glass9fb9e9b2020-04-09 10:27:38 -060055int acpi_copy_name(char *out_name, const char *name)
56{
57 strncpy(out_name, name, ACPI_NAME_LEN);
58 out_name[ACPI_NAME_LEN] = '\0';
59
60 return 0;
61}
62
63int acpi_get_name(const struct udevice *dev, char *out_name)
64{
65 struct acpi_ops *aops;
66
67 aops = device_get_acpi_ops(dev);
68 if (aops && aops->get_name)
69 return aops->get_name(dev, out_name);
70
71 return -ENOSYS;
72}
Simon Glass17968c32020-04-26 09:19:46 -060073
Simon Glassee61e602020-07-07 13:12:05 -060074/**
75 * acpi_add_item() - Add a new item to the list of data collected
76 *
77 * @ctx: ACPI context
78 * @dev: Device that generated the data
79 * @type: Table type it refers to
80 * @start: The start of the data (the end is obtained from ctx->current)
81 * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
82 */
83static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
84 enum gen_type_t type, void *start)
85{
86 struct acpi_item *item;
87 void *end = ctx->current;
88
89 if (item_count == MAX_ACPI_ITEMS) {
90 log_err("Too many items\n");
91 return log_msg_ret("mem", -ENOSPC);
92 }
93
94 item = &acpi_item[item_count];
95 item->dev = dev;
96 item->type = type;
97 item->size = end - start;
98 if (!item->size)
99 return 0;
100 item->buf = malloc(item->size);
101 if (!item->buf)
102 return log_msg_ret("mem", -ENOMEM);
103 memcpy(item->buf, start, item->size);
104 item_count++;
105 log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
106 item->size);
107
108 return 0;
109}
110
Simon Glass3c601b12020-07-07 13:12:06 -0600111static struct acpi_item *find_acpi_item(const char *devname)
112{
113 int i;
114
115 for (i = 0; i < item_count; i++) {
116 struct acpi_item *item = &acpi_item[i];
117
118 if (!strcmp(devname, item->dev->name))
119 return item;
120 }
121
122 return NULL;
123}
124
125/**
126 * sort_acpi_item_type - Sort the ACPI items into the desired order
127 *
128 * This looks up the ordering in the device tree and then adds each item one by
129 * one into the supplied buffer
130 *
131 * @ctx: ACPI context
132 * @start: Start position to put the sorted items. The items will follow each
133 * other in sorted order
134 * @type: Type of items to sort
135 * @return 0 if OK, -ve on error
136 */
137static int sort_acpi_item_type(struct acpi_ctx *ctx, void *start,
138 enum gen_type_t type)
139{
140 const u32 *order;
141 int size;
142 int count;
143 void *ptr;
144 void *end = ctx->current;
145
146 ptr = start;
147 order = ofnode_read_chosen_prop("u-boot,acpi-ssdt-order", &size);
148 if (!order) {
149 log_warning("Failed to find ordering, leaving as is\n");
150 return 0;
151 }
152
153 /*
154 * This algorithm rewrites the context buffer without changing its
155 * length. So there is no need to update ctx-current
156 */
157 count = size / sizeof(u32);
158 while (count--) {
159 struct acpi_item *item;
160 const char *name;
161 ofnode node;
162
163 node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
164 name = ofnode_get_name(node);
165 item = find_acpi_item(name);
166 if (!item) {
167 log_err("Failed to find item '%s'\n", name);
168 return log_msg_ret("find", -ENOENT);
169 }
170 if (item->type == type) {
171 log_debug(" - add %s\n", item->dev->name);
172 memcpy(ptr, item->buf, item->size);
173 ptr += item->size;
174 }
175 }
176
177 /*
178 * If the sort order is missing an item then the output will be too
179 * small. Report this error since the item needs to be added to the
180 * ordering for the ACPI tables to be complete.
181 */
182 if (ptr != end) {
183 log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
184 return -ENXIO;
185 }
186
187 return 0;
188}
189
Simon Glass17968c32020-04-26 09:19:46 -0600190acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
191{
192 struct acpi_ops *aops;
193
194 aops = device_get_acpi_ops(dev);
195 if (aops) {
196 switch (method) {
197 case METHOD_WRITE_TABLES:
198 return aops->write_tables;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600199 case METHOD_FILL_SSDT:
200 return aops->fill_ssdt;
Simon Glass17968c32020-04-26 09:19:46 -0600201 }
202 }
203
204 return NULL;
205}
206
207int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
Simon Glassee61e602020-07-07 13:12:05 -0600208 enum method_t method, enum gen_type_t type)
Simon Glass17968c32020-04-26 09:19:46 -0600209{
210 struct udevice *dev;
211 acpi_method func;
212 int ret;
213
214 func = acpi_get_method(parent, method);
215 if (func) {
Simon Glassee61e602020-07-07 13:12:05 -0600216 void *start = ctx->current;
217
Simon Glass17968c32020-04-26 09:19:46 -0600218 log_debug("\n");
219 log_debug("- %s %p\n", parent->name, func);
220 ret = device_ofdata_to_platdata(parent);
221 if (ret)
222 return log_msg_ret("ofdata", ret);
223 ret = func(parent, ctx);
224 if (ret)
225 return log_msg_ret("func", ret);
Simon Glassee61e602020-07-07 13:12:05 -0600226
227 /* Add the item to the internal list */
228 if (type != TYPE_NONE) {
229 ret = acpi_add_item(ctx, parent, type, start);
230 if (ret)
231 return log_msg_ret("add", ret);
232 }
Simon Glass17968c32020-04-26 09:19:46 -0600233 }
234 device_foreach_child(dev, parent) {
Simon Glassee61e602020-07-07 13:12:05 -0600235 ret = acpi_recurse_method(ctx, dev, method, type);
Simon Glass17968c32020-04-26 09:19:46 -0600236 if (ret)
237 return log_msg_ret("recurse", ret);
238 }
239
240 return 0;
241}
242
Simon Glassd43e0ba2020-07-07 13:12:03 -0600243int acpi_fill_ssdt(struct acpi_ctx *ctx)
244{
Simon Glass3c601b12020-07-07 13:12:06 -0600245 void *start = ctx->current;
Simon Glassd43e0ba2020-07-07 13:12:03 -0600246 int ret;
247
248 log_debug("Writing SSDT tables\n");
Simon Glass3c601b12020-07-07 13:12:06 -0600249 item_count = 0;
Simon Glassee61e602020-07-07 13:12:05 -0600250 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600251 log_debug("Writing SSDT finished, err=%d\n", ret);
Simon Glass3c601b12020-07-07 13:12:06 -0600252 ret = sort_acpi_item_type(ctx, start, TYPE_SSDT);
253 if (ret)
254 return log_msg_ret("build", ret);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600255
256 return ret;
257}
258
Simon Glass17968c32020-04-26 09:19:46 -0600259int acpi_write_dev_tables(struct acpi_ctx *ctx)
260{
261 int ret;
262
263 log_debug("Writing device tables\n");
Simon Glassee61e602020-07-07 13:12:05 -0600264 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES,
265 TYPE_NONE);
Simon Glass17968c32020-04-26 09:19:46 -0600266 log_debug("Writing finished, err=%d\n", ret);
267
268 return ret;
269}