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,
77 * acpigen_get_current() - Get the current ACPI code output pointer
79 * @ctx: ACPI context pointer
80 * @return output pointer
82 u8 *acpigen_get_current(struct acpi_ctx *ctx);
85 * acpigen_emit_byte() - Emit a byte to the ACPI code
87 * @ctx: ACPI context pointer
88 * @data: Value to output
90 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
93 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
95 * @ctx: ACPI context pointer
96 * @data: Value to output
98 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
101 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
103 * @ctx: ACPI context pointer
104 * @data: Value to output
106 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
109 * acpigen_emit_stream() - Emit a stream of bytes
111 * @ctx: ACPI context pointer
112 * @data: Data to output
113 * @size: Size of data in bytes
115 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
118 * acpigen_emit_string() - Emit a string
120 * Emit a string with a null terminator
122 * @ctx: ACPI context pointer
123 * @str: String to output, or NULL for an empty string
125 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
128 * acpigen_write_len_f() - Write a 'forward' length placeholder
130 * This adds space for a length value in the ACPI stream and pushes the current
131 * position (before the length) on the stack. After calling this you can write
132 * some data and then call acpigen_pop_len() to update the length value.
136 * acpigen_write_len_f() ------\
137 * acpigen_write...() |
138 * acpigen_write...() |
139 * acpigen_write_len_f() --\ |
140 * acpigen_write...() | |
141 * acpigen_write...() | |
142 * acpigen_pop_len() ------/ |
143 * acpigen_write...() |
144 * acpigen_pop_len() ----------/
146 * See ACPI 6.3 section 20.2.4 Package Length Encoding
148 * This implementation always uses a 3-byte packet length for simplicity. It
149 * could be adjusted to support other lengths.
151 * @ctx: ACPI context pointer
153 void acpigen_write_len_f(struct acpi_ctx *ctx);
156 * acpigen_pop_len() - Update the previously stacked length placeholder
158 * Call this after the data for the block has been written. It updates the
159 * top length value in the stack and pops it off.
161 * @ctx: ACPI context pointer
163 void acpigen_pop_len(struct acpi_ctx *ctx);
166 * acpigen_write_package() - Start writing a package
168 * A package collects together a number of elements in the ACPI code. To write
171 * acpigen_write_package(ctx, 3);
175 * If you don't know the number of elements in advance, acpigen_write_package()
176 * returns a pointer to the value so you can update it later:
178 * char *num_elements = acpigen_write_package(ctx, 0);
180 * *num_elements += 1;
182 * *num_elements += 1;
185 * @ctx: ACPI context pointer
186 * @nr_el: Number of elements (0 if not known)
187 * @returns pointer to the number of elements, which can be updated by the
190 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
193 * acpigen_write_byte() - Write a byte
195 * @ctx: ACPI context pointer
196 * @data: Value to write
198 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
201 * acpigen_write_word() - Write a word
203 * @ctx: ACPI context pointer
204 * @data: Value to write
206 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
209 * acpigen_write_dword() - Write a dword
211 * @ctx: ACPI context pointer
212 * @data: Value to write
214 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
217 * acpigen_write_qword() - Write a qword
219 * @ctx: ACPI context pointer
220 * @data: Value to write
222 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
225 * acpigen_write_zero() - Write zero
227 * @ctx: ACPI context pointer
229 void acpigen_write_zero(struct acpi_ctx *ctx);
232 * acpigen_write_one() - Write one
234 * @ctx: ACPI context pointer
236 void acpigen_write_one(struct acpi_ctx *ctx);
239 * acpigen_write_integer() - Write an integer
241 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
242 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
244 * @ctx: ACPI context pointer
245 * @data: Integer to write
247 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
250 * acpigen_write_name_zero() - Write a named zero value
252 * @ctx: ACPI context pointer
253 * @name: Name of the value
255 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name);
258 * acpigen_write_name_one() - Write a named one value
260 * @ctx: ACPI context pointer
261 * @name: Name of the value
263 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name);
266 * acpigen_write_name_byte() - Write a named byte value
268 * @ctx: ACPI context pointer
269 * @name: Name of the value
270 * @val: Value to write
272 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val);
275 * acpigen_write_name_word() - Write a named word value
277 * @ctx: ACPI context pointer
278 * @name: Name of the value
279 * @val: Value to write
281 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val);
284 * acpigen_write_name_dword() - Write a named dword value
286 * @ctx: ACPI context pointer
287 * @name: Name of the value
288 * @val: Value to write
290 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val);
293 * acpigen_write_name_qword() - Write a named qword value
295 * @ctx: ACPI context pointer
296 * @name: Name of the value
297 * @val: Value to write
299 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val);
302 * acpigen_write_name_integer() - Write a named integer value
304 * @ctx: ACPI context pointer
305 * @name: Name of the value
306 * @val: Value to write
308 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name,
312 * acpigen_write_name_string() - Write a named string value
314 * @ctx: ACPI context pointer
315 * @name: Name of the value
316 * @string: String to write
318 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
322 * acpigen_write_string() - Write a string
324 * This writes a STRING_PREFIX followed by a null-terminated string
326 * @ctx: ACPI context pointer
327 * @str: String to write
329 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
332 * acpigen_emit_namestring() - Emit an ACPI name
334 * This writes out an ACPI name or path in the required special format. It does
335 * not add the NAME_OP prefix.
337 * @ctx: ACPI context pointer
338 * @namepath: Name / path to emit
340 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
343 * acpigen_write_name() - Write out an ACPI name
345 * This writes out an ACPI name or path in the required special format with a
348 * @ctx: ACPI context pointer
349 * @namepath: Name / path to emit
351 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
354 * acpigen_write_scope() - Write a scope
356 * @ctx: ACPI context pointer
357 * @scope: Scope to write (e.g. "\\_SB.ABCD")
359 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
362 * acpigen_write_uuid() - Write a UUID
364 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
366 * @ctx: ACPI context pointer
367 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
368 * @return 0 if OK, -EINVAL if the format is incorrect
370 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
373 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
375 * @ctx: ACPI context pointer
376 * @op: Operation code (e.g. SLEEP_OP)
378 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
381 * acpigen_write_method() - Write a method header
383 * @ctx: ACPI context pointer
384 * @name: Method name (4 characters)
385 * @nargs: Number of method arguments (0 if none)
387 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
390 * acpigen_write_method_serialized() - Write a method header
392 * This sets the 'serialized' flag so that the method is thread-safe
394 * @ctx: ACPI context pointer
395 * @name: Method name (4 characters)
396 * @nargs: Number of method arguments (0 if none)
398 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
402 * acpigen_write_device() - Write an ACPI device
404 * @ctx: ACPI context pointer
405 * @name: Device name to write
407 void acpigen_write_device(struct acpi_ctx *ctx, const char *name);
410 * acpigen_write_sta() - Write a _STA method
412 * @ctx: ACPI context pointer
413 * @status: Status value to return
415 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
418 * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
420 * @ctx: ACPI context pointer
422 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
425 * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
427 * @ctx: ACPI context pointer
429 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
432 * acpigen_write_register_resource() - Write a register resource
434 * This writes a header, the address information and a footer
436 * @ctx: ACPI context pointer
437 * @addr: Address to write
439 void acpigen_write_register_resource(struct acpi_ctx *ctx,
440 const struct acpi_gen_regaddr *addr);
443 * acpigen_write_sleep() - Write a sleep operation
445 * @ctx: ACPI context pointer
446 * @sleep_ms: Number of milliseconds to sleep for
448 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
451 * acpigen_write_store() - Write a store operation
453 * @ctx: ACPI context pointer
455 void acpigen_write_store(struct acpi_ctx *ctx);
458 * acpigen_write_debug_string() - Write a debug string
460 * This writes a debug operation with an associated string
462 * @ctx: ACPI context pointer
463 * @str: String to write
465 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
468 * acpigen_write_or() - Write a bitwise OR 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_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
480 * acpigen_write_and() - Write a bitwise AND operation
484 * @ctx: ACPI context pointer
485 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
486 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
487 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
489 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
492 * acpigen_write_not() - Write a bitwise NOT operation
496 * @ctx: ACPI context pointer
497 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
498 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
500 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
503 * acpigen_write_power_res() - Write a power resource
505 * Name (_PRx, Package(One) { name })
507 * PowerResource (name, level, order)
509 * The caller should fill in the rest of the power resource and then call
510 * acpigen_pop_len() to close it off
512 * @ctx: ACPI context pointer
513 * @name: Name of power resource (e.g. "PRIC")
514 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
515 * @order: Order that this must be enabled/disabled (e.g. 0)
516 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
517 * @dev_states_count: Number of dev states
519 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
520 uint order, const char *const dev_states[],
521 size_t dev_states_count);
524 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
526 * This emits code to either enable to disable a Tx GPIO. It takes account of
529 * The code needs access to the DW0 register for the pad being used. This is
530 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
531 * which can read and write the pad's DW0 register given this address:
532 * @dw0_read: takes a single argument, the DW0 address
533 * returns the DW0 value
534 * @dw0:write: takes two arguments, the DW0 address and the value to write
537 * Example code (-- means comment):
539 * -- Get Pad Configuration DW0 register value
540 * Method (GPC0, 0x1, Serialized)
542 * -- Arg0 - GPIO DW0 address
543 * Store (Arg0, Local0)
544 * OperationRegion (PDW0, SystemMemory, Local0, 4)
545 * Field (PDW0, AnyAcc, NoLock, Preserve) {
551 * -- Set Pad Configuration DW0 register value
552 * Method (SPC0, 0x2, Serialized)
554 * -- Arg0 - GPIO DW0 address
555 * -- Arg1 - Value for DW0 register
556 * Store (Arg0, Local0)
557 * OperationRegion (PDW0, SystemMemory, Local0, 4)
558 * Field (PDW0, AnyAcc, NoLock, Preserve) {
565 * @ctx: ACPI context pointer
566 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
568 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
569 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
570 * @gpio: GPIO to change
571 * @enable: true to enable GPIO, false to disable
572 * Returns 0 on success, -ve on error.
574 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
575 const char *dw0_read, const char *dw0_write,
576 struct acpi_gpio *gpio, bool enable);
579 * acpigen_write_prw() - Write a power resource for wake (_PRW)
581 * @ctx: ACPI context pointer
582 * @wake: GPE that wakes up the device
583 * @level: Deepest power system sleeping state that can be entered while still
584 * providing wake functionality
586 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);
589 * acpigen_write_if() - Write an If block
591 * This requires a call to acpigen_pop_len() to complete the block
593 * @ctx: ACPI context pointer
595 void acpigen_write_if(struct acpi_ctx *ctx);
598 * acpigen_write_if_lequal_op_int() - Write comparison between op and integer
600 * Generates ACPI code for checking if operand1 and operand2 are equal
602 * If (Lequal (op, val))
604 * @ctx: ACPI context pointer
605 * @op: Operand to check
606 * @val: Value to check against
608 void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val);
611 * acpigen_write_else() - Write an Ef block
613 * This requires a call to acpigen_pop_len() to complete the block
615 * @ctx: ACPI context pointer
617 void acpigen_write_else(struct acpi_ctx *ctx);
620 * acpigen_write_to_buffer() - Write a ToBuffer operation
622 * E.g.: to generate: ToBuffer (Arg0, Local0)
623 * use acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP)
625 * @ctx: ACPI context pointer
626 * @src: Source argument
627 * @dst: Destination argument
629 void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst);
632 * acpigen_write_to_integer() - Write a ToInteger operation
634 * E.g.: to generate: ToInteger (Arg0, Local0)
635 * use acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP)
637 * @ctx: ACPI context pointer
638 * @src: Source argument
639 * @dst: Destination argument
641 void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst);
644 * acpigen_write_return_byte_buffer() - Write a return of a byte buffer
646 * @ctx: ACPI context pointer
647 * @arr: Array of bytes to return
648 * @size: Number of bytes
650 void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
654 * acpigen_write_return_singleton_buffer() - Write a return of a 1-byte buffer
656 * @ctx: ACPI context pointer
657 * @arg: Byte to return
659 void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg);
662 * acpigen_write_return_byte() - Write a return of a byte
664 * @ctx: ACPI context pointer
665 * @arg: Byte to return
667 void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg);
670 * acpigen_write_dsm_start() - Start a _DSM method
672 * Generate ACPI AML code to start the _DSM method.
674 * The functions need to be called in the correct sequence as below.
676 * Within the <generate-code-here> region, Local0 and Local1 must be are left
677 * untouched, but Local2-Local7 can be used
679 * Arguments passed into _DSM method:
682 * Arg2 = Function index
683 * Arg3 = Function-specific arguments
685 * AML code generated looks like this:
686 * Method (_DSM, 4, Serialized) { -- acpigen_write_dsm_start)
687 * ToBuffer (Arg0, Local0)
688 * If (LEqual (Local0, ToUUID(uuid))) { -- acpigen_write_dsm_uuid_start
689 * ToInteger (Arg2, Local1)
690 * If (LEqual (Local1, 0)) { -- acpigen_write_dsm_uuid_start_cond
691 * <generate-code-here>
692 * } -- acpigen_write_dsm_uuid_end_cond
694 * If (LEqual (Local1, n)) { -- acpigen_write_dsm_uuid_start_cond
695 * <generate-code-here>
696 * } -- acpigen_write_dsm_uuid_end_cond
697 * Return (Buffer (One) { 0x0 })
698 * } -- acpigen_write_dsm_uuid_end
700 * If (LEqual (Local0, ToUUID(uuidn))) {
703 * Return (Buffer (One) { 0x0 }) -- acpigen_write_dsm_end
706 * @ctx: ACPI context pointer
708 void acpigen_write_dsm_start(struct acpi_ctx *ctx);
711 * acpigen_write_dsm_uuid_start() - Start a new UUID block
713 * This starts generation of code to handle a particular UUID:
715 * If (LEqual (Local0, ToUUID(uuid))) {
716 * ToInteger (Arg2, Local1)
718 * @ctx: ACPI context pointer
720 int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid);
723 * acpigen_write_dsm_uuid_start_cond() - Start a new condition block
725 * This starts generation of condition-checking code to handle a particular
728 * If (LEqual (Local1, i))
730 * @ctx: ACPI context pointer
732 void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq);
735 * acpigen_write_dsm_uuid_end_cond() - Start a new condition block
737 * This ends generation of condition-checking code to handle a particular
742 * @ctx: ACPI context pointer
744 void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx);
747 * acpigen_write_dsm_uuid_end() - End a UUID block
749 * This ends generation of code to handle a particular UUID:
751 * Return (Buffer (One) { 0x0 })
753 * @ctx: ACPI context pointer
755 void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx);
758 * acpigen_write_dsm_end() - End a _DSM method
760 * This ends generates of the _DSM block:
762 * Return (Buffer (One) { 0x0 })
764 * @ctx: ACPI context pointer
766 void acpigen_write_dsm_end(struct acpi_ctx *ctx);