Simon Glass | 7ca2850 | 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 | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 19 | /* Length of an ACPI name string, excluding null terminator */ |
Simon Glass | 7ca2850 | 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 | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 25 | /* Number of nested objects supported */ |
| 26 | #define ACPIGEN_LENSTACK_SIZE 10 |
| 27 | |
Simon Glass | 89c2798 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 28 | #if !defined(__ACPI__) |
| 29 | |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 30 | #include <linker_lists.h> |
| 31 | |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 32 | struct nhlt; |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 33 | struct udevice; |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 34 | |
Simon Glass | a4f8208 | 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 | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 41 | /** |
Simon Glass | 93f7f82 | 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 | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 46 | * @base: Base address of ACPI tables |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 47 | * @current: Current address for writing |
Simon Glass | fb746fd | 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 | 29b3511 | 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 | b38309b | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 54 | * @xsdt: Pointer to the Extended System Description Table |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 55 | * @nhlt: Intel Non-High-Definition-Audio Link Table (NHLT) pointer, used to |
| 56 | * build up information that audio codecs need to provide in the NHLT ACPI |
| 57 | * table |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 58 | * @len_stack: Stack of 'length' words to fix up later |
| 59 | * @ltop: Points to current top of stack (0 = empty) |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 60 | */ |
| 61 | struct acpi_ctx { |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 62 | void *base; |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 63 | void *current; |
Simon Glass | fb746fd | 2021-12-01 09:02:46 -0700 | [diff] [blame] | 64 | void *tab_start; |
Simon Glass | 29b3511 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 65 | struct acpi_rsdp *rsdp; |
| 66 | struct acpi_rsdt *rsdt; |
Simon Glass | b38309b | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 67 | struct acpi_xsdt *xsdt; |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 68 | struct nhlt *nhlt; |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 69 | char *len_stack[ACPIGEN_LENSTACK_SIZE]; |
| 70 | int ltop; |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 74 | * enum acpi_writer_flags_t - flags to use for the ACPI writers |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame^] | 75 | * |
| 76 | * ACPIWF_ALIGN64 - align to 64 bytes after writing this one (default is 16) |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 77 | */ |
| 78 | enum acpi_writer_flags_t { |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame^] | 79 | ACPIWF_ALIGN64 = 1 << 0, |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct acpi_writer; |
| 83 | |
| 84 | /** |
| 85 | * acpi_writer_func() - Function that can write an ACPI table |
| 86 | * |
| 87 | * @ctx: ACPI context to use for writing |
| 88 | * @entry: Linker-list entry for this writer |
| 89 | * @return 0 if OK, -ve on error |
| 90 | */ |
| 91 | typedef int (*acpi_writer_func)(struct acpi_ctx *ctx, |
| 92 | const struct acpi_writer *entry); |
| 93 | |
| 94 | /** |
| 95 | * struct acpi_writer - an ACPI table that can be written |
| 96 | * |
| 97 | * @name: Name of the writer |
| 98 | * @table: Table name that is generated (e.g. "DSDT") |
| 99 | * @h_write: Writer function |
| 100 | */ |
| 101 | struct acpi_writer { |
| 102 | const char *name; |
| 103 | const char *table; |
| 104 | acpi_writer_func h_write; |
| 105 | int flags; |
| 106 | }; |
| 107 | |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame^] | 108 | /* Declare a new ACPI-table writer */ |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 109 | #define ACPI_WRITER(_name, _table, _write, _flags) \ |
| 110 | ll_entry_declare(struct acpi_writer, _name, acpi_writer) = { \ |
| 111 | .name = #_name, \ |
| 112 | .table = _table, \ |
| 113 | .h_write = _write, \ |
| 114 | .flags = _flags, \ |
| 115 | } |
| 116 | |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame^] | 117 | /* Get a pointer to a given ACPI-table writer */ |
| 118 | #define ACPI_WRITER_GET(_name) \ |
| 119 | ll_entry_get(struct acpi_writer, _name, acpi_writer) |
| 120 | |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 121 | /** |
Simon Glass | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 122 | * struct acpi_ops - ACPI operations supported by driver model |
| 123 | */ |
| 124 | struct acpi_ops { |
| 125 | /** |
| 126 | * get_name() - Obtain the ACPI name of a device |
| 127 | * |
| 128 | * @dev: Device to check |
| 129 | * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX |
| 130 | * bytes |
| 131 | * @return 0 if OK, -ENOENT if no name is available, other -ve value on |
| 132 | * other error |
| 133 | */ |
| 134 | int (*get_name)(const struct udevice *dev, char *out_name); |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * write_tables() - Write out any tables required by this device |
| 138 | * |
| 139 | * @dev: Device to write |
| 140 | * @ctx: ACPI context to use |
| 141 | * @return 0 if OK, -ve on error |
| 142 | */ |
| 143 | int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | b518317 | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * fill_ssdt() - Generate SSDT code for a device |
| 147 | * |
| 148 | * This is called to create the SSDT code. The method should write out |
| 149 | * whatever ACPI code is needed by this device. It will end up in the |
| 150 | * SSDT table. |
| 151 | * |
Simon Glass | 0169458 | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 152 | * Note that this is called 'fill' because the entire contents of the |
| 153 | * SSDT is build by calling this method on all devices. |
| 154 | * |
Simon Glass | b518317 | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 155 | * @dev: Device to write |
| 156 | * @ctx: ACPI context to use |
| 157 | * @return 0 if OK, -ve on error |
| 158 | */ |
| 159 | int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 0169458 | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * inject_dsdt() - Generate DSDT code for a device |
| 163 | * |
| 164 | * This is called to create the DSDT code. The method should write out |
| 165 | * whatever ACPI code is needed by this device. It will end up in the |
| 166 | * DSDT table. |
| 167 | * |
| 168 | * Note that this is called 'inject' because the output of calling this |
| 169 | * method on all devices is injected into the DSDT, the bulk of which |
| 170 | * is written in .asl files for the board. |
| 171 | * |
| 172 | * @dev: Device to write |
| 173 | * @ctx: ACPI context to use |
| 174 | * @return 0 if OK, -ve on error |
| 175 | */ |
| 176 | int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * setup_nhlt() - Set up audio information for this device |
| 180 | * |
| 181 | * The method can add information to ctx->nhlt if it likes |
| 182 | * |
| 183 | * @return 0 if OK, -ENODATA if nothing to add, -ve on error |
| 184 | */ |
| 185 | int (*setup_nhlt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #define device_get_acpi_ops(dev) ((dev)->driver->acpi_ops) |
| 189 | |
| 190 | /** |
| 191 | * acpi_get_name() - Obtain the ACPI name of a device |
| 192 | * |
| 193 | * @dev: Device to check |
| 194 | * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX |
| 195 | * bytes |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 196 | * Return: 0 if OK, -ENOENT if no name is available, other -ve value on |
Simon Glass | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 197 | * other error |
| 198 | */ |
| 199 | int acpi_get_name(const struct udevice *dev, char *out_name); |
| 200 | |
| 201 | /** |
| 202 | * acpi_copy_name() - Copy an ACPI name to an output buffer |
| 203 | * |
| 204 | * This convenience function can be used to return a literal string as a name |
| 205 | * in functions that implement the get_name() method. |
| 206 | * |
| 207 | * For example: |
| 208 | * |
| 209 | * static int mydev_get_name(const struct udevice *dev, char *out_name) |
| 210 | * { |
| 211 | * return acpi_copy_name(out_name, "WIBB"); |
| 212 | * } |
| 213 | * |
| 214 | * @out_name: Place to put the name |
| 215 | * @name: Name to copy |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 216 | * Return: 0 (always) |
Simon Glass | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 217 | */ |
| 218 | int acpi_copy_name(char *out_name, const char *name); |
| 219 | |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 220 | /** |
| 221 | * acpi_write_dev_tables() - Write ACPI tables required by devices |
| 222 | * |
| 223 | * This scans through all devices and tells them to write any tables they want |
| 224 | * to write. |
| 225 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 226 | * Return: 0 if OK, -ve if any device returned an error |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 227 | */ |
| 228 | int acpi_write_dev_tables(struct acpi_ctx *ctx); |
| 229 | |
Simon Glass | b518317 | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 230 | /** |
| 231 | * acpi_fill_ssdt() - Generate ACPI tables for SSDT |
| 232 | * |
| 233 | * This is called to create the SSDT code for all devices. |
| 234 | * |
| 235 | * @ctx: ACPI context to use |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 236 | * Return: 0 if OK, -ve on error |
Simon Glass | b518317 | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 237 | */ |
| 238 | int acpi_fill_ssdt(struct acpi_ctx *ctx); |
| 239 | |
Simon Glass | 0169458 | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 240 | /** |
| 241 | * acpi_inject_dsdt() - Generate ACPI tables for DSDT |
| 242 | * |
| 243 | * This is called to create the DSDT code for all devices. |
| 244 | * |
| 245 | * @ctx: ACPI context to use |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 246 | * Return: 0 if OK, -ve on error |
Simon Glass | 0169458 | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 247 | */ |
| 248 | int acpi_inject_dsdt(struct acpi_ctx *ctx); |
| 249 | |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 250 | /** |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 251 | * acpi_setup_nhlt() - Set up audio information |
| 252 | * |
| 253 | * This is called to set up the nhlt information for all devices. |
| 254 | * |
| 255 | * @ctx: ACPI context to use |
| 256 | * @nhlt: Pointer to nhlt information to add to |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 257 | * Return: 0 if OK, -ve on error |
Simon Glass | b4e8433 | 2020-07-07 21:32:08 -0600 | [diff] [blame] | 258 | */ |
| 259 | int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt); |
| 260 | |
| 261 | /** |
Simon Glass | a4f8208 | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 262 | * acpi_dump_items() - Dump out the collected ACPI items |
| 263 | * |
| 264 | * This lists the ACPI DSDT and SSDT items generated by the various U-Boot |
| 265 | * drivers. |
| 266 | * |
| 267 | * @option: Sets what should be dumpyed |
| 268 | */ |
| 269 | void acpi_dump_items(enum acpi_dump_option option); |
| 270 | |
Simon Glass | f185895 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 271 | /** |
| 272 | * acpi_get_path() - Get the full ACPI path for a device |
| 273 | * |
| 274 | * This checks for any override in the device tree and calls acpi_device_path() |
| 275 | * if not |
| 276 | * |
| 277 | * @dev: Device to check |
| 278 | * @out_path: Buffer to place the path in (should be ACPI_PATH_MAX long) |
| 279 | * @maxlen: Size of buffer (typically ACPI_PATH_MAX) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 280 | * Return: 0 if OK, -ve on error |
Simon Glass | f185895 | 2020-07-07 21:32:07 -0600 | [diff] [blame] | 281 | */ |
| 282 | int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen); |
| 283 | |
Simon Glass | 18434ae | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 284 | /** |
| 285 | * acpi_reset_items() - Reset the list of ACPI items to empty |
| 286 | * |
| 287 | * This list keeps track of DSDT and SSDT items that are generated |
| 288 | * programmatically. The 'acpi items' command shows the list. Use this function |
| 289 | * to empty the list, before writing new items. |
| 290 | */ |
| 291 | void acpi_reset_items(void); |
| 292 | |
Simon Glass | 6afa63a | 2021-12-01 09:02:47 -0700 | [diff] [blame] | 293 | /** |
| 294 | * acpi_write_one() - Call a single ACPI writer entry |
| 295 | * |
| 296 | * This handles aligning the context afterwards, if the entry flags indicate |
| 297 | * that. |
| 298 | * |
| 299 | * @ctx: ACPI context to use |
| 300 | * @entry: Entry to call |
| 301 | * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve |
| 302 | * value on error |
| 303 | */ |
| 304 | int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry); |
| 305 | |
Simon Glass | cc1f8c3 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 306 | /** |
| 307 | * acpi_setup_ctx() - Set up a new ACPI context |
| 308 | * |
| 309 | * This zeros the context and sets up the base and current pointers, ensuring |
| 310 | * that they are aligned. Then it writes the acpi_start and acpi_ctx values in |
| 311 | * global_data |
| 312 | * |
| 313 | * @ctx: ACPI context to set up |
| 314 | * @start: Start address for ACPI table |
| 315 | */ |
| 316 | void acpi_setup_ctx(struct acpi_ctx *ctx, ulong start); |
| 317 | |
Simon Glass | 94ba15a | 2021-12-01 09:02:50 -0700 | [diff] [blame^] | 318 | /** |
| 319 | * acpi_write_one() - Call a single ACPI writer entry |
| 320 | * |
| 321 | * This handles aligning the context afterwards, if the entry flags indicate |
| 322 | * that. |
| 323 | * |
| 324 | * @ctx: ACPI context to use |
| 325 | * @entry: Entry to call |
| 326 | * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve |
| 327 | * value on error |
| 328 | */ |
| 329 | int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry); |
| 330 | |
Simon Glass | 89c2798 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 331 | #endif /* __ACPI__ */ |
| 332 | |
Simon Glass | 7ca2850 | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 333 | #endif |