Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Core ACPI (Advanced Configuration and Power Interface) support |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #ifndef __DM_ACPI_H__ |
| 10 | #define __DM_ACPI_H__ |
| 11 | |
| 12 | /* Allow operations to be optional for ACPI */ |
| 13 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 14 | #define ACPI_OPS_PTR(_ptr) .acpi_ops = _ptr, |
| 15 | #else |
| 16 | #define ACPI_OPS_PTR(_ptr) |
| 17 | #endif |
| 18 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 19 | /* Length of an ACPI name string, excluding null terminator */ |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 20 | #define ACPI_NAME_LEN 4 |
| 21 | |
| 22 | /* Length of an ACPI name string including nul terminator */ |
| 23 | #define ACPI_NAME_MAX (ACPI_NAME_LEN + 1) |
| 24 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 25 | /* Number of nested objects supported */ |
| 26 | #define ACPIGEN_LENSTACK_SIZE 10 |
| 27 | |
Simon Glass | 4969d21 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 28 | #if !defined(__ACPI__) |
| 29 | |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 30 | #include <linker_lists.h> |
| 31 | |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 32 | struct nhlt; |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 33 | struct udevice; |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 34 | |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 35 | /** enum acpi_dump_option - selects what ACPI information to dump */ |
| 36 | enum acpi_dump_option { |
| 37 | ACPI_DUMP_LIST, /* Just the list of items */ |
| 38 | ACPI_DUMP_CONTENTS, /* Include the binary contents also */ |
| 39 | }; |
| 40 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 41 | /** |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 42 | * struct acpi_ctx - Context used for writing ACPI tables |
| 43 | * |
| 44 | * This contains a few useful pieces of information used when writing |
| 45 | * |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 46 | * @base: Base address of ACPI tables |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 47 | * @current: Current address for writing |
Simon Glass | f93b904 | 2021-12-01 09:02:46 -0700 | [diff] [blame] | 48 | * @tab_start: Address of start of the table being written. This is set up |
| 49 | * before the writer or driver method is called. It must not be changed by the |
| 50 | * method |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 51 | * @rsdp: Pointer to the Root System Description Pointer, typically used when |
| 52 | * adding a new table. The RSDP holds pointers to the RSDT and XSDT. |
| 53 | * @rsdt: Pointer to the Root System Description Table |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 54 | * @xsdt: Pointer to the Extended System Description Table |
Simon Glass | f53f57d | 2021-12-01 09:02:51 -0700 | [diff] [blame] | 55 | * @facs: Pointer to the Firmware ACPI Control Structure |
Simon Glass | 83c3cb5 | 2021-12-01 09:02:52 -0700 | [diff] [blame] | 56 | * @dsdt: Pointer to the Differentiated System Description Table |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 57 | * @nhlt: Intel Non-High-Definition-Audio Link Table (NHLT) pointer, used to |
| 58 | * build up information that audio codecs need to provide in the NHLT ACPI |
| 59 | * table |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 60 | * @len_stack: Stack of 'length' words to fix up later |
| 61 | * @ltop: Points to current top of stack (0 = empty) |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 62 | */ |
| 63 | struct acpi_ctx { |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 64 | void *base; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 65 | void *current; |
Simon Glass | f93b904 | 2021-12-01 09:02:46 -0700 | [diff] [blame] | 66 | void *tab_start; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 67 | struct acpi_rsdp *rsdp; |
| 68 | struct acpi_rsdt *rsdt; |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 69 | struct acpi_xsdt *xsdt; |
Simon Glass | f53f57d | 2021-12-01 09:02:51 -0700 | [diff] [blame] | 70 | struct acpi_facs *facs; |
Simon Glass | 83c3cb5 | 2021-12-01 09:02:52 -0700 | [diff] [blame] | 71 | struct acpi_table_header *dsdt; |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 72 | struct nhlt *nhlt; |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 73 | char *len_stack[ACPIGEN_LENSTACK_SIZE]; |
| 74 | int ltop; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 75 | }; |
| 76 | |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 77 | /** |
| 78 | * enum acpi_writer_flags_t - flags to use for the ACPI writers |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 79 | * |
| 80 | * ACPIWF_ALIGN64 - align to 64 bytes after writing this one (default is 16) |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 81 | */ |
| 82 | enum acpi_writer_flags_t { |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 83 | ACPIWF_ALIGN64 = 1 << 0, |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct acpi_writer; |
| 87 | |
| 88 | /** |
| 89 | * acpi_writer_func() - Function that can write an ACPI table |
| 90 | * |
| 91 | * @ctx: ACPI context to use for writing |
| 92 | * @entry: Linker-list entry for this writer |
| 93 | * @return 0 if OK, -ve on error |
| 94 | */ |
| 95 | typedef int (*acpi_writer_func)(struct acpi_ctx *ctx, |
| 96 | const struct acpi_writer *entry); |
| 97 | |
| 98 | /** |
| 99 | * struct acpi_writer - an ACPI table that can be written |
| 100 | * |
| 101 | * @name: Name of the writer |
| 102 | * @table: Table name that is generated (e.g. "DSDT") |
| 103 | * @h_write: Writer function |
| 104 | */ |
| 105 | struct acpi_writer { |
| 106 | const char *name; |
| 107 | const char *table; |
| 108 | acpi_writer_func h_write; |
| 109 | int flags; |
| 110 | }; |
| 111 | |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 112 | /* Declare a new ACPI-table writer */ |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 113 | #define ACPI_WRITER(_name, _table, _write, _flags) \ |
| 114 | ll_entry_declare(struct acpi_writer, _name, acpi_writer) = { \ |
| 115 | .name = #_name, \ |
| 116 | .table = _table, \ |
| 117 | .h_write = _write, \ |
| 118 | .flags = _flags, \ |
| 119 | } |
| 120 | |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 121 | /* Get a pointer to a given ACPI-table writer */ |
| 122 | #define ACPI_WRITER_GET(_name) \ |
| 123 | ll_entry_get(struct acpi_writer, _name, acpi_writer) |
| 124 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 125 | /** |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 126 | * struct acpi_ops - ACPI operations supported by driver model |
| 127 | */ |
| 128 | struct acpi_ops { |
| 129 | /** |
| 130 | * get_name() - Obtain the ACPI name of a device |
| 131 | * |
| 132 | * @dev: Device to check |
| 133 | * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX |
| 134 | * bytes |
| 135 | * @return 0 if OK, -ENOENT if no name is available, other -ve value on |
| 136 | * other error |
| 137 | */ |
| 138 | int (*get_name)(const struct udevice *dev, char *out_name); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * write_tables() - Write out any tables required by this device |
| 142 | * |
| 143 | * @dev: Device to write |
| 144 | * @ctx: ACPI context to use |
| 145 | * @return 0 if OK, -ve on error |
| 146 | */ |
| 147 | int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * fill_ssdt() - Generate SSDT code for a device |
| 151 | * |
| 152 | * This is called to create the SSDT code. The method should write out |
| 153 | * whatever ACPI code is needed by this device. It will end up in the |
| 154 | * SSDT table. |
| 155 | * |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 156 | * Note that this is called 'fill' because the entire contents of the |
| 157 | * SSDT is build by calling this method on all devices. |
| 158 | * |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 159 | * @dev: Device to write |
| 160 | * @ctx: ACPI context to use |
| 161 | * @return 0 if OK, -ve on error |
| 162 | */ |
| 163 | int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * inject_dsdt() - Generate DSDT code for a device |
| 167 | * |
| 168 | * This is called to create the DSDT code. The method should write out |
| 169 | * whatever ACPI code is needed by this device. It will end up in the |
| 170 | * DSDT table. |
| 171 | * |
| 172 | * Note that this is called 'inject' because the output of calling this |
| 173 | * method on all devices is injected into the DSDT, the bulk of which |
| 174 | * is written in .asl files for the board. |
| 175 | * |
| 176 | * @dev: Device to write |
| 177 | * @ctx: ACPI context to use |
| 178 | * @return 0 if OK, -ve on error |
| 179 | */ |
| 180 | int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * setup_nhlt() - Set up audio information for this device |
| 184 | * |
| 185 | * The method can add information to ctx->nhlt if it likes |
| 186 | * |
| 187 | * @return 0 if OK, -ENODATA if nothing to add, -ve on error |
| 188 | */ |
| 189 | int (*setup_nhlt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | #define device_get_acpi_ops(dev) ((dev)->driver->acpi_ops) |
| 193 | |
| 194 | /** |
| 195 | * acpi_get_name() - Obtain the ACPI name of a device |
| 196 | * |
| 197 | * @dev: Device to check |
| 198 | * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX |
| 199 | * bytes |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 200 | * Return: 0 if OK, -ENOENT if no name is available, other -ve value on |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 201 | * other error |
| 202 | */ |
| 203 | int acpi_get_name(const struct udevice *dev, char *out_name); |
| 204 | |
| 205 | /** |
| 206 | * acpi_copy_name() - Copy an ACPI name to an output buffer |
| 207 | * |
| 208 | * This convenience function can be used to return a literal string as a name |
| 209 | * in functions that implement the get_name() method. |
| 210 | * |
| 211 | * For example: |
| 212 | * |
| 213 | * static int mydev_get_name(const struct udevice *dev, char *out_name) |
| 214 | * { |
| 215 | * return acpi_copy_name(out_name, "WIBB"); |
| 216 | * } |
| 217 | * |
| 218 | * @out_name: Place to put the name |
| 219 | * @name: Name to copy |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 220 | * Return: 0 (always) |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 221 | */ |
| 222 | int acpi_copy_name(char *out_name, const char *name); |
| 223 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 224 | /** |
| 225 | * acpi_write_dev_tables() - Write ACPI tables required by devices |
| 226 | * |
| 227 | * This scans through all devices and tells them to write any tables they want |
| 228 | * to write. |
| 229 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 230 | * Return: 0 if OK, -ve if any device returned an error |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 231 | */ |
| 232 | int acpi_write_dev_tables(struct acpi_ctx *ctx); |
| 233 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 234 | /** |
| 235 | * acpi_fill_ssdt() - Generate ACPI tables for SSDT |
| 236 | * |
| 237 | * This is called to create the SSDT code for all devices. |
| 238 | * |
| 239 | * @ctx: ACPI context to use |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 240 | * Return: 0 if OK, -ve on error |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 241 | */ |
| 242 | int acpi_fill_ssdt(struct acpi_ctx *ctx); |
| 243 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 244 | /** |
| 245 | * acpi_inject_dsdt() - Generate ACPI tables for DSDT |
| 246 | * |
| 247 | * This is called to create the DSDT code for all devices. |
| 248 | * |
| 249 | * @ctx: ACPI context to use |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 250 | * Return: 0 if OK, -ve on error |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 251 | */ |
| 252 | int acpi_inject_dsdt(struct acpi_ctx *ctx); |
| 253 | |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 254 | /** |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 255 | * acpi_setup_nhlt() - Set up audio information |
| 256 | * |
| 257 | * This is called to set up the nhlt information for all devices. |
| 258 | * |
| 259 | * @ctx: ACPI context to use |
| 260 | * @nhlt: Pointer to nhlt information to add to |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 261 | * Return: 0 if OK, -ve on error |
Simon Glass | cd9667b | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 262 | */ |
| 263 | int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt); |
| 264 | |
| 265 | /** |
Simon Glass | 877205e | 2021-12-01 09:03:04 -0700 | [diff] [blame] | 266 | * acpi_add_other_item() - Add a new table to the list of ACPI tables |
| 267 | * |
| 268 | * This adds an entry of type ACPIT_TYPE_OTHER |
| 269 | * |
| 270 | * @ctx: ACPI context |
| 271 | * @writer: Writer entry that generated the data |
| 272 | * @type: Table type it refers to |
| 273 | * @start: The start of the data (the end is obtained from ctx->current) |
| 274 | * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory |
| 275 | */ |
| 276 | int acpi_add_other_item(struct acpi_ctx *ctx, const struct acpi_writer *writer, |
| 277 | void *start); |
| 278 | |
| 279 | /** |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 280 | * acpi_dump_items() - Dump out the collected ACPI items |
| 281 | * |
| 282 | * This lists the ACPI DSDT and SSDT items generated by the various U-Boot |
| 283 | * drivers. |
| 284 | * |
| 285 | * @option: Sets what should be dumpyed |
| 286 | */ |
| 287 | void acpi_dump_items(enum acpi_dump_option option); |
| 288 | |
Simon Glass | 0123233 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 289 | /** |
| 290 | * acpi_get_path() - Get the full ACPI path for a device |
| 291 | * |
| 292 | * This checks for any override in the device tree and calls acpi_device_path() |
| 293 | * if not |
| 294 | * |
| 295 | * @dev: Device to check |
| 296 | * @out_path: Buffer to place the path in (should be ACPI_PATH_MAX long) |
| 297 | * @maxlen: Size of buffer (typically ACPI_PATH_MAX) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 298 | * Return: 0 if OK, -ve on error |
Simon Glass | 0123233 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 299 | */ |
| 300 | int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen); |
| 301 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 302 | /** |
| 303 | * acpi_reset_items() - Reset the list of ACPI items to empty |
| 304 | * |
| 305 | * This list keeps track of DSDT and SSDT items that are generated |
| 306 | * programmatically. The 'acpi items' command shows the list. Use this function |
| 307 | * to empty the list, before writing new items. |
| 308 | */ |
| 309 | void acpi_reset_items(void); |
| 310 | |
Simon Glass | 4ecb97e | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 311 | /** |
| 312 | * acpi_write_one() - Call a single ACPI writer entry |
| 313 | * |
| 314 | * This handles aligning the context afterwards, if the entry flags indicate |
| 315 | * that. |
| 316 | * |
| 317 | * @ctx: ACPI context to use |
| 318 | * @entry: Entry to call |
| 319 | * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve |
| 320 | * value on error |
| 321 | */ |
| 322 | int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry); |
| 323 | |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 324 | /** |
| 325 | * acpi_setup_ctx() - Set up a new ACPI context |
| 326 | * |
| 327 | * This zeros the context and sets up the base and current pointers, ensuring |
| 328 | * that they are aligned. Then it writes the acpi_start and acpi_ctx values in |
| 329 | * global_data |
| 330 | * |
| 331 | * @ctx: ACPI context to set up |
| 332 | * @start: Start address for ACPI table |
| 333 | */ |
| 334 | void acpi_setup_ctx(struct acpi_ctx *ctx, ulong start); |
| 335 | |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 336 | /** |
| 337 | * acpi_write_one() - Call a single ACPI writer entry |
| 338 | * |
| 339 | * This handles aligning the context afterwards, if the entry flags indicate |
| 340 | * that. |
| 341 | * |
| 342 | * @ctx: ACPI context to use |
| 343 | * @entry: Entry to call |
| 344 | * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve |
| 345 | * value on error |
| 346 | */ |
| 347 | int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry); |
| 348 | |
Simon Glass | 4969d21 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 349 | #endif /* __ACPI__ */ |
| 350 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 351 | #endif |