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> |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 15 | #include <dm/acpi.h> |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 17 | #include <dm/root.h> |
| 18 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 19 | #define MAX_ACPI_ITEMS 100 |
| 20 | |
| 21 | /* Type of table that we collected */ |
| 22 | enum gen_type_t { |
| 23 | TYPE_NONE, |
| 24 | TYPE_SSDT, |
| 25 | }; |
| 26 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 27 | /* Type of method to call */ |
| 28 | enum method_t { |
| 29 | METHOD_WRITE_TABLES, |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 30 | METHOD_FILL_SSDT, |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | /* Prototype for all methods */ |
| 34 | typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx); |
| 35 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 36 | /** |
| 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 | */ |
| 44 | struct 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 */ |
| 52 | static struct acpi_item acpi_item[MAX_ACPI_ITEMS]; |
| 53 | static int item_count; |
| 54 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 55 | int 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 | |
| 63 | int 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 73 | |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 74 | /** |
| 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 | */ |
| 83 | static 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 Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame^] | 111 | static 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 | */ |
| 137 | static 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 190 | acpi_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 Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 199 | case METHOD_FILL_SSDT: |
| 200 | return aops->fill_ssdt; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent, |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 208 | enum method_t method, enum gen_type_t type) |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 209 | { |
| 210 | struct udevice *dev; |
| 211 | acpi_method func; |
| 212 | int ret; |
| 213 | |
| 214 | func = acpi_get_method(parent, method); |
| 215 | if (func) { |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 216 | void *start = ctx->current; |
| 217 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 218 | 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 Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 226 | |
| 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 233 | } |
| 234 | device_foreach_child(dev, parent) { |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 235 | ret = acpi_recurse_method(ctx, dev, method, type); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 236 | if (ret) |
| 237 | return log_msg_ret("recurse", ret); |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 243 | int acpi_fill_ssdt(struct acpi_ctx *ctx) |
| 244 | { |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame^] | 245 | void *start = ctx->current; |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 246 | int ret; |
| 247 | |
| 248 | log_debug("Writing SSDT tables\n"); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame^] | 249 | item_count = 0; |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 250 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 251 | log_debug("Writing SSDT finished, err=%d\n", ret); |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame^] | 252 | ret = sort_acpi_item_type(ctx, start, TYPE_SSDT); |
| 253 | if (ret) |
| 254 | return log_msg_ret("build", ret); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 259 | int acpi_write_dev_tables(struct acpi_ctx *ctx) |
| 260 | { |
| 261 | int ret; |
| 262 | |
| 263 | log_debug("Writing device tables\n"); |
Simon Glass | ee61e60 | 2020-07-07 13:12:05 -0600 | [diff] [blame] | 264 | ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES, |
| 265 | TYPE_NONE); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 266 | log_debug("Writing finished, err=%d\n", ret); |
| 267 | |
| 268 | return ret; |
| 269 | } |