Simon Glass | 98528d4 | 2020-07-07 13:11:42 -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 | * |
| 7 | * Modified from coreboot file acpigen.h |
| 8 | */ |
| 9 | |
| 10 | #ifndef __ACPI_ACPIGEN_H |
| 11 | #define __ACPI_ACPIGEN_H |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | |
| 15 | struct acpi_ctx; |
| 16 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 17 | /* Top 4 bits of the value used to indicate a three-byte length value */ |
| 18 | #define ACPI_PKG_LEN_3_BYTES 0x80 |
| 19 | |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 20 | /* ACPI Op/Prefix codes */ |
| 21 | enum { |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 22 | ZERO_OP = 0x00, |
| 23 | ONE_OP = 0x01, |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame^] | 24 | NAME_OP = 0x08, |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 25 | BYTE_PREFIX = 0x0a, |
| 26 | WORD_PREFIX = 0x0b, |
| 27 | DWORD_PREFIX = 0x0c, |
Simon Glass | 45d6d95 | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 28 | STRING_PREFIX = 0x0d, |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 29 | QWORD_PREFIX = 0x0e, |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 30 | PACKAGE_OP = 0x12, |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame^] | 31 | DUAL_NAME_PREFIX = 0x2e, |
| 32 | MULTI_NAME_PREFIX = 0x2f, |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 35 | /** |
| 36 | * acpigen_get_current() - Get the current ACPI code output pointer |
| 37 | * |
| 38 | * @ctx: ACPI context pointer |
| 39 | * @return output pointer |
| 40 | */ |
| 41 | u8 *acpigen_get_current(struct acpi_ctx *ctx); |
| 42 | |
| 43 | /** |
| 44 | * acpigen_emit_byte() - Emit a byte to the ACPI code |
| 45 | * |
| 46 | * @ctx: ACPI context pointer |
| 47 | * @data: Value to output |
| 48 | */ |
| 49 | void acpigen_emit_byte(struct acpi_ctx *ctx, uint data); |
| 50 | |
| 51 | /** |
| 52 | * acpigen_emit_word() - Emit a 16-bit word to the ACPI code |
| 53 | * |
| 54 | * @ctx: ACPI context pointer |
| 55 | * @data: Value to output |
| 56 | */ |
| 57 | void acpigen_emit_word(struct acpi_ctx *ctx, uint data); |
| 58 | |
| 59 | /** |
| 60 | * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code |
| 61 | * |
| 62 | * @ctx: ACPI context pointer |
| 63 | * @data: Value to output |
| 64 | */ |
| 65 | void acpigen_emit_dword(struct acpi_ctx *ctx, uint data); |
| 66 | |
Simon Glass | 071c4a5 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 67 | /** |
| 68 | * acpigen_emit_stream() - Emit a stream of bytes |
| 69 | * |
| 70 | * @ctx: ACPI context pointer |
| 71 | * @data: Data to output |
| 72 | * @size: Size of data in bytes |
| 73 | */ |
| 74 | void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size); |
| 75 | |
| 76 | /** |
| 77 | * acpigen_emit_string() - Emit a string |
| 78 | * |
| 79 | * Emit a string with a null terminator |
| 80 | * |
| 81 | * @ctx: ACPI context pointer |
| 82 | * @str: String to output, or NULL for an empty string |
| 83 | */ |
| 84 | void acpigen_emit_string(struct acpi_ctx *ctx, const char *str); |
| 85 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 86 | /** |
| 87 | * acpigen_write_len_f() - Write a 'forward' length placeholder |
| 88 | * |
| 89 | * This adds space for a length value in the ACPI stream and pushes the current |
| 90 | * position (before the length) on the stack. After calling this you can write |
| 91 | * some data and then call acpigen_pop_len() to update the length value. |
| 92 | * |
| 93 | * Usage: |
| 94 | * |
| 95 | * acpigen_write_len_f() ------\ |
| 96 | * acpigen_write...() | |
| 97 | * acpigen_write...() | |
| 98 | * acpigen_write_len_f() --\ | |
| 99 | * acpigen_write...() | | |
| 100 | * acpigen_write...() | | |
| 101 | * acpigen_pop_len() ------/ | |
| 102 | * acpigen_write...() | |
| 103 | * acpigen_pop_len() ----------/ |
| 104 | * |
| 105 | * See ACPI 6.3 section 20.2.4 Package Length Encoding |
| 106 | * |
| 107 | * This implementation always uses a 3-byte packet length for simplicity. It |
| 108 | * could be adjusted to support other lengths. |
| 109 | * |
| 110 | * @ctx: ACPI context pointer |
| 111 | */ |
| 112 | void acpigen_write_len_f(struct acpi_ctx *ctx); |
| 113 | |
| 114 | /** |
| 115 | * acpigen_pop_len() - Update the previously stacked length placeholder |
| 116 | * |
| 117 | * Call this after the data for the block has been written. It updates the |
| 118 | * top length value in the stack and pops it off. |
| 119 | * |
| 120 | * @ctx: ACPI context pointer |
| 121 | */ |
| 122 | void acpigen_pop_len(struct acpi_ctx *ctx); |
| 123 | |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 124 | /** |
| 125 | * acpigen_write_package() - Start writing a package |
| 126 | * |
| 127 | * A package collects together a number of elements in the ACPI code. To write |
| 128 | * a package use: |
| 129 | * |
| 130 | * acpigen_write_package(ctx, 3); |
| 131 | * ...write things |
| 132 | * acpigen_pop_len() |
| 133 | * |
| 134 | * If you don't know the number of elements in advance, acpigen_write_package() |
| 135 | * returns a pointer to the value so you can update it later: |
| 136 | * |
| 137 | * char *num_elements = acpigen_write_package(ctx, 0); |
| 138 | * ...write things |
| 139 | * *num_elements += 1; |
| 140 | * ...write things |
| 141 | * *num_elements += 1; |
| 142 | * acpigen_pop_len() |
| 143 | * |
| 144 | * @ctx: ACPI context pointer |
| 145 | * @nr_el: Number of elements (0 if not known) |
| 146 | * @returns pointer to the number of elements, which can be updated by the |
| 147 | * caller if needed |
| 148 | */ |
| 149 | char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el); |
| 150 | |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 151 | /** |
| 152 | * acpigen_write_integer() - Write an integer |
| 153 | * |
| 154 | * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on |
| 155 | * the integer size) and an integer value. Note that WORD means 16 bits in ACPI. |
| 156 | * |
| 157 | * @ctx: ACPI context pointer |
| 158 | * @data: Integer to write |
| 159 | */ |
| 160 | void acpigen_write_integer(struct acpi_ctx *ctx, u64 data); |
| 161 | |
Simon Glass | 45d6d95 | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 162 | /** |
| 163 | * acpigen_write_string() - Write a string |
| 164 | * |
| 165 | * This writes a STRING_PREFIX followed by a null-terminated string |
| 166 | * |
| 167 | * @ctx: ACPI context pointer |
| 168 | * @str: String to write |
| 169 | */ |
| 170 | void acpigen_write_string(struct acpi_ctx *ctx, const char *str); |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame^] | 171 | |
| 172 | /** |
| 173 | * acpigen_emit_namestring() - Emit an ACPI name |
| 174 | * |
| 175 | * This writes out an ACPI name or path in the required special format. It does |
| 176 | * not add the NAME_OP prefix. |
| 177 | * |
| 178 | * @ctx: ACPI context pointer |
| 179 | * @namepath: Name / path to emit |
| 180 | */ |
| 181 | void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath); |
| 182 | |
| 183 | /** |
| 184 | * acpigen_write_name() - Write out an ACPI name |
| 185 | * |
| 186 | * This writes out an ACPI name or path in the required special format with a |
| 187 | * NAME_OP prefix. |
| 188 | * |
| 189 | * @ctx: ACPI context pointer |
| 190 | * @namepath: Name / path to emit |
| 191 | */ |
| 192 | void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath); |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 193 | #endif |