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