1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Core ACPI (Advanced Configuration and Power Interface) support
5 * Copyright 2019 Google LLC
7 * Modified from coreboot file acpigen.h
10 #ifndef __ACPI_ACPIGEN_H
11 #define __ACPI_ACPIGEN_H
13 #include <linux/types.h>
18 /* Top 4 bits of the value used to indicate a three-byte length value */
19 #define ACPI_PKG_LEN_3_BYTES 0x80
21 #define ACPI_METHOD_NARGS_MASK 0x7
22 #define ACPI_METHOD_SERIALIZED_MASK BIT(3)
24 /* ACPI Op/Prefix codes */
39 DUAL_NAME_PREFIX = 0x2e,
40 MULTI_NAME_PREFIX = 0x2f,
61 * acpigen_get_current() - Get the current ACPI code output pointer
63 * @ctx: ACPI context pointer
64 * @return output pointer
66 u8 *acpigen_get_current(struct acpi_ctx *ctx);
69 * acpigen_emit_byte() - Emit a byte to the ACPI code
71 * @ctx: ACPI context pointer
72 * @data: Value to output
74 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
77 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
79 * @ctx: ACPI context pointer
80 * @data: Value to output
82 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
85 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
87 * @ctx: ACPI context pointer
88 * @data: Value to output
90 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
93 * acpigen_emit_stream() - Emit a stream of bytes
95 * @ctx: ACPI context pointer
96 * @data: Data to output
97 * @size: Size of data in bytes
99 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
102 * acpigen_emit_string() - Emit a string
104 * Emit a string with a null terminator
106 * @ctx: ACPI context pointer
107 * @str: String to output, or NULL for an empty string
109 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
112 * acpigen_write_len_f() - Write a 'forward' length placeholder
114 * This adds space for a length value in the ACPI stream and pushes the current
115 * position (before the length) on the stack. After calling this you can write
116 * some data and then call acpigen_pop_len() to update the length value.
120 * acpigen_write_len_f() ------\
121 * acpigen_write...() |
122 * acpigen_write...() |
123 * acpigen_write_len_f() --\ |
124 * acpigen_write...() | |
125 * acpigen_write...() | |
126 * acpigen_pop_len() ------/ |
127 * acpigen_write...() |
128 * acpigen_pop_len() ----------/
130 * See ACPI 6.3 section 20.2.4 Package Length Encoding
132 * This implementation always uses a 3-byte packet length for simplicity. It
133 * could be adjusted to support other lengths.
135 * @ctx: ACPI context pointer
137 void acpigen_write_len_f(struct acpi_ctx *ctx);
140 * acpigen_pop_len() - Update the previously stacked length placeholder
142 * Call this after the data for the block has been written. It updates the
143 * top length value in the stack and pops it off.
145 * @ctx: ACPI context pointer
147 void acpigen_pop_len(struct acpi_ctx *ctx);
150 * acpigen_write_package() - Start writing a package
152 * A package collects together a number of elements in the ACPI code. To write
155 * acpigen_write_package(ctx, 3);
159 * If you don't know the number of elements in advance, acpigen_write_package()
160 * returns a pointer to the value so you can update it later:
162 * char *num_elements = acpigen_write_package(ctx, 0);
164 * *num_elements += 1;
166 * *num_elements += 1;
169 * @ctx: ACPI context pointer
170 * @nr_el: Number of elements (0 if not known)
171 * @returns pointer to the number of elements, which can be updated by the
174 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
177 * acpigen_write_byte() - Write a byte
179 * @ctx: ACPI context pointer
180 * @data: Value to write
182 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
185 * acpigen_write_word() - Write a word
187 * @ctx: ACPI context pointer
188 * @data: Value to write
190 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
193 * acpigen_write_dword() - Write a dword
195 * @ctx: ACPI context pointer
196 * @data: Value to write
198 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
201 * acpigen_write_qword() - Write a qword
203 * @ctx: ACPI context pointer
204 * @data: Value to write
206 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
209 * acpigen_write_zero() - Write zero
211 * @ctx: ACPI context pointer
213 void acpigen_write_zero(struct acpi_ctx *ctx);
216 * acpigen_write_one() - Write one
218 * @ctx: ACPI context pointer
220 void acpigen_write_one(struct acpi_ctx *ctx);
223 * acpigen_write_integer() - Write an integer
225 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
226 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
228 * @ctx: ACPI context pointer
229 * @data: Integer to write
231 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
234 * acpigen_write_string() - Write a string
236 * This writes a STRING_PREFIX followed by a null-terminated string
238 * @ctx: ACPI context pointer
239 * @str: String to write
241 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
244 * acpigen_emit_namestring() - Emit an ACPI name
246 * This writes out an ACPI name or path in the required special format. It does
247 * not add the NAME_OP prefix.
249 * @ctx: ACPI context pointer
250 * @namepath: Name / path to emit
252 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
255 * acpigen_write_name() - Write out an ACPI name
257 * This writes out an ACPI name or path in the required special format with a
260 * @ctx: ACPI context pointer
261 * @namepath: Name / path to emit
263 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
266 * acpigen_write_scope() - Write a scope
268 * @ctx: ACPI context pointer
269 * @scope: Scope to write (e.g. "\\_SB.ABCD")
271 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
274 * acpigen_write_uuid() - Write a UUID
276 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
278 * @ctx: ACPI context pointer
279 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
280 * @return 0 if OK, -EINVAL if the format is incorrect
282 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
285 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
287 * @ctx: ACPI context pointer
288 * @op: Operation code (e.g. SLEEP_OP)
290 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
293 * acpigen_write_method() - Write a method header
295 * @ctx: ACPI context pointer
296 * @name: Method name (4 characters)
297 * @nargs: Number of method arguments (0 if none)
299 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
302 * acpigen_write_method_serialized() - Write a method header
304 * This sets the 'serialized' flag so that the method is thread-safe
306 * @ctx: ACPI context pointer
307 * @name: Method name (4 characters)
308 * @nargs: Number of method arguments (0 if none)
310 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
314 * acpigen_write_sta() - Write a _STA method
316 * @ctx: ACPI context pointer
317 * @status: Status value to return
319 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
322 * acpigen_write_sleep() - Write a sleep operation
324 * @ctx: ACPI context pointer
325 * @sleep_ms: Number of milliseconds to sleep for
327 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
330 * acpigen_write_store() - Write a store operation
332 * @ctx: ACPI context pointer
334 void acpigen_write_store(struct acpi_ctx *ctx);
337 * acpigen_write_debug_string() - Write a debug string
339 * This writes a debug operation with an associated string
341 * @ctx: ACPI context pointer
342 * @str: String to write
344 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
347 * acpigen_write_or() - Write a bitwise OR operation
351 * @ctx: ACPI context pointer
352 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
353 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
354 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
356 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
359 * acpigen_write_and() - Write a bitwise AND operation
363 * @ctx: ACPI context pointer
364 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
365 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
366 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
368 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
371 * acpigen_write_not() - Write a bitwise NOT operation
375 * @ctx: ACPI context pointer
376 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
377 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
379 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
382 * acpigen_write_power_res() - Write a power resource
384 * Name (_PRx, Package(One) { name })
386 * PowerResource (name, level, order)
388 * The caller should fill in the rest of the power resource and then call
389 * acpigen_pop_len() to close it off
391 * @ctx: ACPI context pointer
392 * @name: Name of power resource (e.g. "PRIC")
393 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
394 * @order: Order that this must be enabled/disabled (e.g. 0)
395 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
396 * @dev_states_count: Number of dev states
398 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
399 uint order, const char *const dev_states[],
400 size_t dev_states_count);
403 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
405 * This emits code to either enable to disable a Tx GPIO. It takes account of
408 * The code needs access to the DW0 register for the pad being used. This is
409 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
410 * which can read and write the pad's DW0 register given this address:
411 * @dw0_read: takes a single argument, the DW0 address
412 * returns the DW0 value
413 * @dw0:write: takes two arguments, the DW0 address and the value to write
416 * Example code (-- means comment):
418 * -- Get Pad Configuration DW0 register value
419 * Method (GPC0, 0x1, Serialized)
421 * -- Arg0 - GPIO DW0 address
422 * Store (Arg0, Local0)
423 * OperationRegion (PDW0, SystemMemory, Local0, 4)
424 * Field (PDW0, AnyAcc, NoLock, Preserve) {
430 * -- Set Pad Configuration DW0 register value
431 * Method (SPC0, 0x2, Serialized)
433 * -- Arg0 - GPIO DW0 address
434 * -- Arg1 - Value for DW0 register
435 * Store (Arg0, Local0)
436 * OperationRegion (PDW0, SystemMemory, Local0, 4)
437 * Field (PDW0, AnyAcc, NoLock, Preserve) {
444 * @ctx: ACPI context pointer
445 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
447 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
448 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
449 * @gpio: GPIO to change
450 * @enable: true to enable GPIO, false to disable
451 * Returns 0 on success, -ve on error.
453 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
454 const char *dw0_read, const char *dw0_write,
455 struct acpi_gpio *gpio, bool enable);