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>
16 struct acpi_gen_regaddr;
19 /* Top 4 bits of the value used to indicate a three-byte length value */
20 #define ACPI_PKG_LEN_3_BYTES 0x80
22 #define ACPI_METHOD_NARGS_MASK 0x7
23 #define ACPI_METHOD_SERIALIZED_MASK BIT(3)
25 #define ACPI_END_TAG 0x79
27 /* ACPI Op/Prefix codes */
42 DUAL_NAME_PREFIX = 0x2e,
43 MULTI_NAME_PREFIX = 0x2f,
65 * acpigen_get_current() - Get the current ACPI code output pointer
67 * @ctx: ACPI context pointer
68 * @return output pointer
70 u8 *acpigen_get_current(struct acpi_ctx *ctx);
73 * acpigen_emit_byte() - Emit a byte to the ACPI code
75 * @ctx: ACPI context pointer
76 * @data: Value to output
78 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
81 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
83 * @ctx: ACPI context pointer
84 * @data: Value to output
86 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
89 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
91 * @ctx: ACPI context pointer
92 * @data: Value to output
94 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
97 * acpigen_emit_stream() - Emit a stream of bytes
99 * @ctx: ACPI context pointer
100 * @data: Data to output
101 * @size: Size of data in bytes
103 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
106 * acpigen_emit_string() - Emit a string
108 * Emit a string with a null terminator
110 * @ctx: ACPI context pointer
111 * @str: String to output, or NULL for an empty string
113 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
116 * acpigen_write_len_f() - Write a 'forward' length placeholder
118 * This adds space for a length value in the ACPI stream and pushes the current
119 * position (before the length) on the stack. After calling this you can write
120 * some data and then call acpigen_pop_len() to update the length value.
124 * acpigen_write_len_f() ------\
125 * acpigen_write...() |
126 * acpigen_write...() |
127 * acpigen_write_len_f() --\ |
128 * acpigen_write...() | |
129 * acpigen_write...() | |
130 * acpigen_pop_len() ------/ |
131 * acpigen_write...() |
132 * acpigen_pop_len() ----------/
134 * See ACPI 6.3 section 20.2.4 Package Length Encoding
136 * This implementation always uses a 3-byte packet length for simplicity. It
137 * could be adjusted to support other lengths.
139 * @ctx: ACPI context pointer
141 void acpigen_write_len_f(struct acpi_ctx *ctx);
144 * acpigen_pop_len() - Update the previously stacked length placeholder
146 * Call this after the data for the block has been written. It updates the
147 * top length value in the stack and pops it off.
149 * @ctx: ACPI context pointer
151 void acpigen_pop_len(struct acpi_ctx *ctx);
154 * acpigen_write_package() - Start writing a package
156 * A package collects together a number of elements in the ACPI code. To write
159 * acpigen_write_package(ctx, 3);
163 * If you don't know the number of elements in advance, acpigen_write_package()
164 * returns a pointer to the value so you can update it later:
166 * char *num_elements = acpigen_write_package(ctx, 0);
168 * *num_elements += 1;
170 * *num_elements += 1;
173 * @ctx: ACPI context pointer
174 * @nr_el: Number of elements (0 if not known)
175 * @returns pointer to the number of elements, which can be updated by the
178 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
181 * acpigen_write_byte() - Write a byte
183 * @ctx: ACPI context pointer
184 * @data: Value to write
186 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
189 * acpigen_write_word() - Write a word
191 * @ctx: ACPI context pointer
192 * @data: Value to write
194 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
197 * acpigen_write_dword() - Write a dword
199 * @ctx: ACPI context pointer
200 * @data: Value to write
202 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
205 * acpigen_write_qword() - Write a qword
207 * @ctx: ACPI context pointer
208 * @data: Value to write
210 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
213 * acpigen_write_zero() - Write zero
215 * @ctx: ACPI context pointer
217 void acpigen_write_zero(struct acpi_ctx *ctx);
220 * acpigen_write_one() - Write one
222 * @ctx: ACPI context pointer
224 void acpigen_write_one(struct acpi_ctx *ctx);
227 * acpigen_write_integer() - Write an integer
229 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
230 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
232 * @ctx: ACPI context pointer
233 * @data: Integer to write
235 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
238 * acpigen_write_name_zero() - Write a named zero value
240 * @ctx: ACPI context pointer
241 * @name: Name of the value
243 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name);
246 * acpigen_write_name_one() - Write a named one value
248 * @ctx: ACPI context pointer
249 * @name: Name of the value
251 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name);
254 * acpigen_write_name_byte() - Write a named byte value
256 * @ctx: ACPI context pointer
257 * @name: Name of the value
258 * @val: Value to write
260 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val);
263 * acpigen_write_name_word() - Write a named word value
265 * @ctx: ACPI context pointer
266 * @name: Name of the value
267 * @val: Value to write
269 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val);
272 * acpigen_write_name_dword() - Write a named dword value
274 * @ctx: ACPI context pointer
275 * @name: Name of the value
276 * @val: Value to write
278 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val);
281 * acpigen_write_name_qword() - Write a named qword value
283 * @ctx: ACPI context pointer
284 * @name: Name of the value
285 * @val: Value to write
287 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val);
290 * acpigen_write_name_integer() - Write a named integer value
292 * @ctx: ACPI context pointer
293 * @name: Name of the value
294 * @val: Value to write
296 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name,
300 * acpigen_write_name_string() - Write a named string value
302 * @ctx: ACPI context pointer
303 * @name: Name of the value
304 * @string: String to write
306 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
310 * acpigen_write_string() - Write a string
312 * This writes a STRING_PREFIX followed by a null-terminated string
314 * @ctx: ACPI context pointer
315 * @str: String to write
317 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
320 * acpigen_emit_namestring() - Emit an ACPI name
322 * This writes out an ACPI name or path in the required special format. It does
323 * not add the NAME_OP prefix.
325 * @ctx: ACPI context pointer
326 * @namepath: Name / path to emit
328 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
331 * acpigen_write_name() - Write out an ACPI name
333 * This writes out an ACPI name or path in the required special format with a
336 * @ctx: ACPI context pointer
337 * @namepath: Name / path to emit
339 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
342 * acpigen_write_scope() - Write a scope
344 * @ctx: ACPI context pointer
345 * @scope: Scope to write (e.g. "\\_SB.ABCD")
347 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
350 * acpigen_write_uuid() - Write a UUID
352 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
354 * @ctx: ACPI context pointer
355 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
356 * @return 0 if OK, -EINVAL if the format is incorrect
358 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
361 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
363 * @ctx: ACPI context pointer
364 * @op: Operation code (e.g. SLEEP_OP)
366 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
369 * acpigen_write_method() - Write a method header
371 * @ctx: ACPI context pointer
372 * @name: Method name (4 characters)
373 * @nargs: Number of method arguments (0 if none)
375 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
378 * acpigen_write_method_serialized() - Write a method header
380 * This sets the 'serialized' flag so that the method is thread-safe
382 * @ctx: ACPI context pointer
383 * @name: Method name (4 characters)
384 * @nargs: Number of method arguments (0 if none)
386 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
390 * acpigen_write_device() - Write an ACPI device
392 * @ctx: ACPI context pointer
393 * @name: Device name to write
395 void acpigen_write_device(struct acpi_ctx *ctx, const char *name);
398 * acpigen_write_sta() - Write a _STA method
400 * @ctx: ACPI context pointer
401 * @status: Status value to return
403 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
406 * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
408 * @ctx: ACPI context pointer
410 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
413 * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
415 * @ctx: ACPI context pointer
417 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
420 * acpigen_write_register_resource() - Write a register resource
422 * This writes a header, the address information and a footer
424 * @ctx: ACPI context pointer
425 * @addr: Address to write
427 void acpigen_write_register_resource(struct acpi_ctx *ctx,
428 const struct acpi_gen_regaddr *addr);
431 * acpigen_write_sleep() - Write a sleep operation
433 * @ctx: ACPI context pointer
434 * @sleep_ms: Number of milliseconds to sleep for
436 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
439 * acpigen_write_store() - Write a store operation
441 * @ctx: ACPI context pointer
443 void acpigen_write_store(struct acpi_ctx *ctx);
446 * acpigen_write_debug_string() - Write a debug string
448 * This writes a debug operation with an associated string
450 * @ctx: ACPI context pointer
451 * @str: String to write
453 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
456 * acpigen_write_or() - Write a bitwise OR operation
460 * @ctx: ACPI context pointer
461 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
462 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
463 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
465 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
468 * acpigen_write_and() - Write a bitwise AND operation
472 * @ctx: ACPI context pointer
473 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
474 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
475 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
477 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
480 * acpigen_write_not() - Write a bitwise NOT operation
484 * @ctx: ACPI context pointer
485 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
486 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
488 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
491 * acpigen_write_power_res() - Write a power resource
493 * Name (_PRx, Package(One) { name })
495 * PowerResource (name, level, order)
497 * The caller should fill in the rest of the power resource and then call
498 * acpigen_pop_len() to close it off
500 * @ctx: ACPI context pointer
501 * @name: Name of power resource (e.g. "PRIC")
502 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
503 * @order: Order that this must be enabled/disabled (e.g. 0)
504 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
505 * @dev_states_count: Number of dev states
507 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
508 uint order, const char *const dev_states[],
509 size_t dev_states_count);
512 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
514 * This emits code to either enable to disable a Tx GPIO. It takes account of
517 * The code needs access to the DW0 register for the pad being used. This is
518 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
519 * which can read and write the pad's DW0 register given this address:
520 * @dw0_read: takes a single argument, the DW0 address
521 * returns the DW0 value
522 * @dw0:write: takes two arguments, the DW0 address and the value to write
525 * Example code (-- means comment):
527 * -- Get Pad Configuration DW0 register value
528 * Method (GPC0, 0x1, Serialized)
530 * -- Arg0 - GPIO DW0 address
531 * Store (Arg0, Local0)
532 * OperationRegion (PDW0, SystemMemory, Local0, 4)
533 * Field (PDW0, AnyAcc, NoLock, Preserve) {
539 * -- Set Pad Configuration DW0 register value
540 * Method (SPC0, 0x2, Serialized)
542 * -- Arg0 - GPIO DW0 address
543 * -- Arg1 - Value for DW0 register
544 * Store (Arg0, Local0)
545 * OperationRegion (PDW0, SystemMemory, Local0, 4)
546 * Field (PDW0, AnyAcc, NoLock, Preserve) {
553 * @ctx: ACPI context pointer
554 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
556 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
557 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
558 * @gpio: GPIO to change
559 * @enable: true to enable GPIO, false to disable
560 * Returns 0 on success, -ve on error.
562 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
563 const char *dw0_read, const char *dw0_write,
564 struct acpi_gpio *gpio, bool enable);
567 * acpigen_write_prw() - Write a power resource for wake (_PRW)
569 * @ctx: ACPI context pointer
570 * @wake: GPE that wakes up the device
571 * @level: Deepest power system sleeping state that can be entered while still
572 * providing wake functionality
574 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);