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 | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* Prototype for all methods */ |
| 37 | typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx); |
| 38 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 39 | /** |
| 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 | */ |
| 47 | struct 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 */ |
| 55 | static struct acpi_item acpi_item[MAX_ACPI_ITEMS]; |
| 56 | static int item_count; |
| 57 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 58 | int 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 | |
| 66 | int acpi_get_name(const struct udevice *dev, char *out_name) |
| 67 | { |
| 68 | struct acpi_ops *aops; |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame^] | 69 | const char *name; |
| 70 | int ret; |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 71 | |
| 72 | aops = device_get_acpi_ops(dev); |
| 73 | if (aops && aops->get_name) |
| 74 | return aops->get_name(dev, out_name); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame^] | 75 | 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 Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 81 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame^] | 82 | return 0; |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 83 | } |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 84 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 85 | /** |
| 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 | */ |
| 94 | static 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 Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 122 | static 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 | */ |
| 148 | static 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 Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 158 | order = ofnode_read_chosen_prop(type == TYPE_DSDT ? |
| 159 | "u-boot,acpi-dsdt-order" : |
| 160 | "u-boot,acpi-ssdt-order", &size); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 161 | 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 203 | acpi_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 Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 212 | case METHOD_FILL_SSDT: |
| 213 | return aops->fill_ssdt; |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 214 | case METHOD_INJECT_DSDT: |
| 215 | return aops->inject_dsdt; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent, |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 223 | enum method_t method, enum gen_type_t type) |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 224 | { |
| 225 | struct udevice *dev; |
| 226 | acpi_method func; |
| 227 | int ret; |
| 228 | |
| 229 | func = acpi_get_method(parent, method); |
| 230 | if (func) { |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 231 | void *start = ctx->current; |
| 232 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 233 | 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 Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 241 | |
| 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 248 | } |
| 249 | device_foreach_child(dev, parent) { |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 250 | ret = acpi_recurse_method(ctx, dev, method, type); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 251 | if (ret) |
| 252 | return log_msg_ret("recurse", ret); |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 258 | int acpi_fill_ssdt(struct acpi_ctx *ctx) |
| 259 | { |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 260 | void *start = ctx->current; |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 261 | int ret; |
| 262 | |
| 263 | log_debug("Writing SSDT tables\n"); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 264 | item_count = 0; |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 265 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 266 | log_debug("Writing SSDT finished, err=%d\n", ret); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 267 | ret = sort_acpi_item_type(ctx, start, TYPE_SSDT); |
| 268 | if (ret) |
| 269 | return log_msg_ret("build", ret); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 274 | int 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 291 | int acpi_write_dev_tables(struct acpi_ctx *ctx) |
| 292 | { |
| 293 | int ret; |
| 294 | |
| 295 | log_debug("Writing device tables\n"); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 296 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES, |
| 297 | TYPE_NONE); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 298 | log_debug("Writing finished, err=%d\n", ret); |
| 299 | |
| 300 | return ret; |
| 301 | } |