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 */
38 DUAL_NAME_PREFIX = 0x2e,
39 MULTI_NAME_PREFIX = 0x2f,
60 * acpigen_get_current() - Get the current ACPI code output pointer
62 * @ctx: ACPI context pointer
63 * @return output pointer
65 u8 *acpigen_get_current(struct acpi_ctx *ctx);
68 * acpigen_emit_byte() - Emit a byte to the ACPI code
70 * @ctx: ACPI context pointer
71 * @data: Value to output
73 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
76 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
78 * @ctx: ACPI context pointer
79 * @data: Value to output
81 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
84 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
86 * @ctx: ACPI context pointer
87 * @data: Value to output
89 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
92 * acpigen_emit_stream() - Emit a stream of bytes
94 * @ctx: ACPI context pointer
95 * @data: Data to output
96 * @size: Size of data in bytes
98 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
101 * acpigen_emit_string() - Emit a string
103 * Emit a string with a null terminator
105 * @ctx: ACPI context pointer
106 * @str: String to output, or NULL for an empty string
108 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
111 * acpigen_write_len_f() - Write a 'forward' length placeholder
113 * This adds space for a length value in the ACPI stream and pushes the current
114 * position (before the length) on the stack. After calling this you can write
115 * some data and then call acpigen_pop_len() to update the length value.
119 * acpigen_write_len_f() ------\
120 * acpigen_write...() |
121 * acpigen_write...() |
122 * acpigen_write_len_f() --\ |
123 * acpigen_write...() | |
124 * acpigen_write...() | |
125 * acpigen_pop_len() ------/ |
126 * acpigen_write...() |
127 * acpigen_pop_len() ----------/
129 * See ACPI 6.3 section 20.2.4 Package Length Encoding
131 * This implementation always uses a 3-byte packet length for simplicity. It
132 * could be adjusted to support other lengths.
134 * @ctx: ACPI context pointer
136 void acpigen_write_len_f(struct acpi_ctx *ctx);
139 * acpigen_pop_len() - Update the previously stacked length placeholder
141 * Call this after the data for the block has been written. It updates the
142 * top length value in the stack and pops it off.
144 * @ctx: ACPI context pointer
146 void acpigen_pop_len(struct acpi_ctx *ctx);
149 * acpigen_write_package() - Start writing a package
151 * A package collects together a number of elements in the ACPI code. To write
154 * acpigen_write_package(ctx, 3);
158 * If you don't know the number of elements in advance, acpigen_write_package()
159 * returns a pointer to the value so you can update it later:
161 * char *num_elements = acpigen_write_package(ctx, 0);
163 * *num_elements += 1;
165 * *num_elements += 1;
168 * @ctx: ACPI context pointer
169 * @nr_el: Number of elements (0 if not known)
170 * @returns pointer to the number of elements, which can be updated by the
173 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
176 * acpigen_write_byte() - Write a byte
178 * @ctx: ACPI context pointer
179 * @data: Value to write
181 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
184 * acpigen_write_word() - Write a word
186 * @ctx: ACPI context pointer
187 * @data: Value to write
189 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
192 * acpigen_write_dword() - Write a dword
194 * @ctx: ACPI context pointer
195 * @data: Value to write
197 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
200 * acpigen_write_qword() - Write a qword
202 * @ctx: ACPI context pointer
203 * @data: Value to write
205 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
208 * acpigen_write_zero() - Write zero
210 * @ctx: ACPI context pointer
212 void acpigen_write_zero(struct acpi_ctx *ctx);
215 * acpigen_write_one() - Write one
217 * @ctx: ACPI context pointer
219 void acpigen_write_one(struct acpi_ctx *ctx);
222 * acpigen_write_integer() - Write an integer
224 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
225 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
227 * @ctx: ACPI context pointer
228 * @data: Integer to write
230 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
233 * acpigen_write_string() - Write a string
235 * This writes a STRING_PREFIX followed by a null-terminated string
237 * @ctx: ACPI context pointer
238 * @str: String to write
240 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
243 * acpigen_emit_namestring() - Emit an ACPI name
245 * This writes out an ACPI name or path in the required special format. It does
246 * not add the NAME_OP prefix.
248 * @ctx: ACPI context pointer
249 * @namepath: Name / path to emit
251 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
254 * acpigen_write_name() - Write out an ACPI name
256 * This writes out an ACPI name or path in the required special format with a
259 * @ctx: ACPI context pointer
260 * @namepath: Name / path to emit
262 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
265 * acpigen_write_uuid() - Write a UUID
267 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
269 * @ctx: ACPI context pointer
270 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
271 * @return 0 if OK, -EINVAL if the format is incorrect
273 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
276 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
278 * @ctx: ACPI context pointer
279 * @op: Operation code (e.g. SLEEP_OP)
281 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
284 * acpigen_write_method() - Write a method header
286 * @ctx: ACPI context pointer
287 * @name: Method name (4 characters)
288 * @nargs: Number of method arguments (0 if none)
290 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
293 * acpigen_write_method_serialized() - Write a method header
295 * This sets the 'serialized' flag so that the method is thread-safe
297 * @ctx: ACPI context pointer
298 * @name: Method name (4 characters)
299 * @nargs: Number of method arguments (0 if none)
301 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
305 * acpigen_write_sta() - Write a _STA method
307 * @ctx: ACPI context pointer
308 * @status: Status value to return
310 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
313 * acpigen_write_sleep() - Write a sleep operation
315 * @ctx: ACPI context pointer
316 * @sleep_ms: Number of milliseconds to sleep for
318 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
321 * acpigen_write_store() - Write a store operation
323 * @ctx: ACPI context pointer
325 void acpigen_write_store(struct acpi_ctx *ctx);
328 * acpigen_write_debug_string() - Write a debug string
330 * This writes a debug operation with an associated string
332 * @ctx: ACPI context pointer
333 * @str: String to write
335 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
338 * acpigen_write_or() - Write a bitwise OR operation
342 * @ctx: ACPI context pointer
343 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
344 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
345 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
347 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
350 * acpigen_write_and() - Write a bitwise AND operation
354 * @ctx: ACPI context pointer
355 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
356 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
357 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
359 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
362 * acpigen_write_not() - Write a bitwise NOT operation
366 * @ctx: ACPI context pointer
367 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
368 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
370 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
373 * acpigen_write_power_res() - Write a power resource
375 * Name (_PRx, Package(One) { name })
377 * PowerResource (name, level, order)
379 * The caller should fill in the rest of the power resource and then call
380 * acpigen_pop_len() to close it off
382 * @ctx: ACPI context pointer
383 * @name: Name of power resource (e.g. "PRIC")
384 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
385 * @order: Order that this must be enabled/disabled (e.g. 0)
386 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
387 * @dev_states_count: Number of dev states
389 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
390 uint order, const char *const dev_states[],
391 size_t dev_states_count);
394 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
396 * This emits code to either enable to disable a Tx GPIO. It takes account of
399 * The code needs access to the DW0 register for the pad being used. This is
400 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
401 * which can read and write the pad's DW0 register given this address:
402 * @dw0_read: takes a single argument, the DW0 address
403 * returns the DW0 value
404 * @dw0:write: takes two arguments, the DW0 address and the value to write
407 * Example code (-- means comment):
409 * -- Get Pad Configuration DW0 register value
410 * Method (GPC0, 0x1, Serialized)
412 * -- Arg0 - GPIO DW0 address
413 * Store (Arg0, Local0)
414 * OperationRegion (PDW0, SystemMemory, Local0, 4)
415 * Field (PDW0, AnyAcc, NoLock, Preserve) {
421 * -- Set Pad Configuration DW0 register value
422 * Method (SPC0, 0x2, Serialized)
424 * -- Arg0 - GPIO DW0 address
425 * -- Arg1 - Value for DW0 register
426 * Store (Arg0, Local0)
427 * OperationRegion (PDW0, SystemMemory, Local0, 4)
428 * Field (PDW0, AnyAcc, NoLock, Preserve) {
435 * @ctx: ACPI context pointer
436 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
438 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
439 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
440 * @gpio: GPIO to change
441 * @enable: true to enable GPIO, false to disable
442 * Returns 0 on success, -ve on error.
444 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
445 const char *dw0_read, const char *dw0_write,
446 struct acpi_gpio *gpio, bool enable);