Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 1 | // 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 Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <acpi/acpi_device.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 16 | #include <dm/acpi.h> |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 18 | #include <dm/root.h> |
| 19 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 20 | #define MAX_ACPI_ITEMS 100 |
| 21 | |
| 22 | /* Type of table that we collected */ |
| 23 | enum gen_type_t { |
| 24 | TYPE_NONE, |
| 25 | TYPE_SSDT, |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 26 | TYPE_DSDT, |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 27 | }; |
| 28 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 29 | /* Type of method to call */ |
| 30 | enum method_t { |
| 31 | METHOD_WRITE_TABLES, |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 32 | METHOD_FILL_SSDT, |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 33 | METHOD_INJECT_DSDT, |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 34 | METHOD_SETUP_NHLT, |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* Prototype for all methods */ |
| 38 | typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx); |
| 39 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 40 | /** |
| 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 | */ |
| 48 | struct 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 */ |
| 56 | static struct acpi_item acpi_item[MAX_ACPI_ITEMS]; |
| 57 | static int item_count; |
| 58 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 59 | int 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 | |
| 67 | int acpi_get_name(const struct udevice *dev, char *out_name) |
| 68 | { |
| 69 | struct acpi_ops *aops; |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 70 | const char *name; |
| 71 | int ret; |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 72 | |
| 73 | aops = device_get_acpi_ops(dev); |
| 74 | if (aops && aops->get_name) |
| 75 | return aops->get_name(dev, out_name); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 76 | 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 Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 82 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 83 | return 0; |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 84 | } |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 85 | |
Simon Glass | 0123233 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 86 | int 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) |
Simon Glass | e74f8f8 | 2021-03-25 10:26:04 +1300 | [diff] [blame] | 94 | return -ENOSPC; |
Simon Glass | 0123233 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 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 Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 105 | /** |
| 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) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 112 | * Return: 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 113 | */ |
| 114 | static 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 Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 142 | void 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 Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 158 | static 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 182 | * Return: 0 if OK, -ve on error |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 183 | */ |
| 184 | static 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 Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 194 | order = ofnode_read_chosen_prop(type == TYPE_DSDT ? |
| 195 | "u-boot,acpi-dsdt-order" : |
| 196 | "u-boot,acpi-ssdt-order", &size); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 197 | if (!order) { |
Simon Glass | 92136c1 | 2020-07-17 08:49:25 -0600 | [diff] [blame] | 198 | log_debug("Failed to find ordering, leaving as is\n"); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 199 | 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 239 | acpi_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 Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 248 | case METHOD_FILL_SSDT: |
| 249 | return aops->fill_ssdt; |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 250 | case METHOD_INJECT_DSDT: |
| 251 | return aops->inject_dsdt; |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 252 | case METHOD_SETUP_NHLT: |
| 253 | return aops->setup_nhlt; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent, |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 261 | enum method_t method, enum gen_type_t type) |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 262 | { |
| 263 | struct udevice *dev; |
| 264 | acpi_method func; |
| 265 | int ret; |
| 266 | |
| 267 | func = acpi_get_method(parent, method); |
| 268 | if (func) { |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 269 | log_debug("- method %d, %s %p\n", method, parent->name, func); |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 270 | ret = device_of_to_plat(parent); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 271 | if (ret) |
| 272 | return log_msg_ret("ofdata", ret); |
Simon Glass | f93b904 | 2021-12-01 09:02:46 -0700 | [diff] [blame^] | 273 | ctx->tab_start = ctx->current; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 274 | ret = func(parent, ctx); |
| 275 | if (ret) |
| 276 | return log_msg_ret("func", ret); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 277 | |
| 278 | /* Add the item to the internal list */ |
| 279 | if (type != TYPE_NONE) { |
Simon Glass | f93b904 | 2021-12-01 09:02:46 -0700 | [diff] [blame^] | 280 | ret = acpi_add_item(ctx, parent, type, ctx->tab_start); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 281 | if (ret) |
| 282 | return log_msg_ret("add", ret); |
| 283 | } |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 284 | } |
| 285 | device_foreach_child(dev, parent) { |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 286 | ret = acpi_recurse_method(ctx, dev, method, type); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 287 | if (ret) |
| 288 | return log_msg_ret("recurse", ret); |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 294 | int acpi_fill_ssdt(struct acpi_ctx *ctx) |
| 295 | { |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 296 | void *start = ctx->current; |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 297 | int ret; |
| 298 | |
| 299 | log_debug("Writing SSDT tables\n"); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 300 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 301 | log_debug("Writing SSDT finished, err=%d\n", ret); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 302 | ret = sort_acpi_item_type(ctx, start, TYPE_SSDT); |
| 303 | if (ret) |
| 304 | return log_msg_ret("build", ret); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 309 | int acpi_inject_dsdt(struct acpi_ctx *ctx) |
| 310 | { |
| 311 | void *start = ctx->current; |
| 312 | int ret; |
| 313 | |
| 314 | log_debug("Writing DSDT tables\n"); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 315 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT, |
| 316 | TYPE_DSDT); |
| 317 | log_debug("Writing DSDT finished, err=%d\n", ret); |
| 318 | ret = sort_acpi_item_type(ctx, start, TYPE_DSDT); |
| 319 | if (ret) |
| 320 | return log_msg_ret("build", ret); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 325 | void acpi_reset_items(void) |
| 326 | { |
| 327 | item_count = 0; |
| 328 | } |
| 329 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 330 | int acpi_write_dev_tables(struct acpi_ctx *ctx) |
| 331 | { |
| 332 | int ret; |
| 333 | |
| 334 | log_debug("Writing device tables\n"); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 335 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES, |
| 336 | TYPE_NONE); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 337 | log_debug("Writing finished, err=%d\n", ret); |
| 338 | |
| 339 | return ret; |
| 340 | } |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 341 | |
| 342 | int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | log_debug("Setup NHLT\n"); |
| 347 | ctx->nhlt = nhlt; |
| 348 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_SETUP_NHLT, TYPE_NONE); |
| 349 | log_debug("Setup finished, err=%d\n", ret); |
| 350 | |
| 351 | return ret; |
| 352 | } |