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 * enum psd_coord - Coordination types for P-states
79 * The type of coordination that exists (hardware) or is required (software) as
80 * a result of the underlying hardware dependency
89 * acpigen_get_current() - Get the current ACPI code output pointer
91 * @ctx: ACPI context pointer
92 * @return output pointer
94 u8 *acpigen_get_current(struct acpi_ctx *ctx);
97 * acpigen_emit_byte() - Emit a byte to the ACPI code
99 * @ctx: ACPI context pointer
100 * @data: Value to output
102 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
105 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
107 * @ctx: ACPI context pointer
108 * @data: Value to output
110 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
113 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
115 * @ctx: ACPI context pointer
116 * @data: Value to output
118 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
121 * acpigen_emit_stream() - Emit a stream of bytes
123 * @ctx: ACPI context pointer
124 * @data: Data to output
125 * @size: Size of data in bytes
127 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
130 * acpigen_emit_string() - Emit a string
132 * Emit a string with a null terminator
134 * @ctx: ACPI context pointer
135 * @str: String to output, or NULL for an empty string
137 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
140 * acpigen_write_len_f() - Write a 'forward' length placeholder
142 * This adds space for a length value in the ACPI stream and pushes the current
143 * position (before the length) on the stack. After calling this you can write
144 * some data and then call acpigen_pop_len() to update the length value.
148 * acpigen_write_len_f() ------\
149 * acpigen_write...() |
150 * acpigen_write...() |
151 * acpigen_write_len_f() --\ |
152 * acpigen_write...() | |
153 * acpigen_write...() | |
154 * acpigen_pop_len() ------/ |
155 * acpigen_write...() |
156 * acpigen_pop_len() ----------/
158 * See ACPI 6.3 section 20.2.4 Package Length Encoding
160 * This implementation always uses a 3-byte packet length for simplicity. It
161 * could be adjusted to support other lengths.
163 * @ctx: ACPI context pointer
165 void acpigen_write_len_f(struct acpi_ctx *ctx);
168 * acpigen_pop_len() - Update the previously stacked length placeholder
170 * Call this after the data for the block has been written. It updates the
171 * top length value in the stack and pops it off.
173 * @ctx: ACPI context pointer
175 void acpigen_pop_len(struct acpi_ctx *ctx);
178 * acpigen_write_package() - Start writing a package
180 * A package collects together a number of elements in the ACPI code. To write
183 * acpigen_write_package(ctx, 3);
187 * If you don't know the number of elements in advance, acpigen_write_package()
188 * returns a pointer to the value so you can update it later:
190 * char *num_elements = acpigen_write_package(ctx, 0);
192 * *num_elements += 1;
194 * *num_elements += 1;
197 * @ctx: ACPI context pointer
198 * @nr_el: Number of elements (0 if not known)
199 * @returns pointer to the number of elements, which can be updated by the
202 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
205 * acpigen_write_byte() - Write a byte
207 * @ctx: ACPI context pointer
208 * @data: Value to write
210 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
213 * acpigen_write_word() - Write a word
215 * @ctx: ACPI context pointer
216 * @data: Value to write
218 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
221 * acpigen_write_dword() - Write a dword
223 * @ctx: ACPI context pointer
224 * @data: Value to write
226 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
229 * acpigen_write_qword() - Write a qword
231 * @ctx: ACPI context pointer
232 * @data: Value to write
234 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
237 * acpigen_write_zero() - Write zero
239 * @ctx: ACPI context pointer
241 void acpigen_write_zero(struct acpi_ctx *ctx);
244 * acpigen_write_one() - Write one
246 * @ctx: ACPI context pointer
248 void acpigen_write_one(struct acpi_ctx *ctx);
251 * acpigen_write_integer() - Write an integer
253 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
254 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
256 * @ctx: ACPI context pointer
257 * @data: Integer to write
259 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
262 * acpigen_write_name_zero() - Write a named zero value
264 * @ctx: ACPI context pointer
265 * @name: Name of the value
267 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name);
270 * acpigen_write_name_one() - Write a named one value
272 * @ctx: ACPI context pointer
273 * @name: Name of the value
275 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name);
278 * acpigen_write_name_byte() - Write a named byte value
280 * @ctx: ACPI context pointer
281 * @name: Name of the value
282 * @val: Value to write
284 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val);
287 * acpigen_write_name_word() - Write a named word value
289 * @ctx: ACPI context pointer
290 * @name: Name of the value
291 * @val: Value to write
293 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val);
296 * acpigen_write_name_dword() - Write a named dword value
298 * @ctx: ACPI context pointer
299 * @name: Name of the value
300 * @val: Value to write
302 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val);
305 * acpigen_write_name_qword() - Write a named qword value
307 * @ctx: ACPI context pointer
308 * @name: Name of the value
309 * @val: Value to write
311 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val);
314 * acpigen_write_name_integer() - Write a named integer value
316 * @ctx: ACPI context pointer
317 * @name: Name of the value
318 * @val: Value to write
320 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name,
324 * acpigen_write_name_string() - Write a named string value
326 * @ctx: ACPI context pointer
327 * @name: Name of the value
328 * @string: String to write
330 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
334 * acpigen_write_string() - Write a string
336 * This writes a STRING_PREFIX followed by a null-terminated string
338 * @ctx: ACPI context pointer
339 * @str: String to write
341 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
344 * acpigen_emit_namestring() - Emit an ACPI name
346 * This writes out an ACPI name or path in the required special format. It does
347 * not add the NAME_OP prefix.
349 * @ctx: ACPI context pointer
350 * @namepath: Name / path to emit
352 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
355 * acpigen_write_name() - Write out an ACPI name
357 * This writes out an ACPI name or path in the required special format with a
360 * @ctx: ACPI context pointer
361 * @namepath: Name / path to emit
363 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
366 * acpigen_write_scope() - Write a scope
368 * @ctx: ACPI context pointer
369 * @scope: Scope to write (e.g. "\\_SB.ABCD")
371 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
374 * acpigen_write_uuid() - Write a UUID
376 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
378 * @ctx: ACPI context pointer
379 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
380 * @return 0 if OK, -EINVAL if the format is incorrect
382 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
385 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
387 * @ctx: ACPI context pointer
388 * @op: Operation code (e.g. SLEEP_OP)
390 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
393 * acpigen_write_method() - Write a method header
395 * @ctx: ACPI context pointer
396 * @name: Method name (4 characters)
397 * @nargs: Number of method arguments (0 if none)
399 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
402 * acpigen_write_method_serialized() - Write a method header
404 * This sets the 'serialized' flag so that the method is thread-safe
406 * @ctx: ACPI context pointer
407 * @name: Method name (4 characters)
408 * @nargs: Number of method arguments (0 if none)
410 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
414 * acpigen_write_device() - Write an ACPI device
416 * @ctx: ACPI context pointer
417 * @name: Device name to write
419 void acpigen_write_device(struct acpi_ctx *ctx, const char *name);
422 * acpigen_write_sta() - Write a _STA method
424 * @ctx: ACPI context pointer
425 * @status: Status value to return
427 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
430 * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
432 * @ctx: ACPI context pointer
434 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
437 * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
439 * @ctx: ACPI context pointer
441 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
444 * acpigen_write_register_resource() - Write a register resource
446 * This writes a header, the address information and a footer
448 * @ctx: ACPI context pointer
449 * @addr: Address to write
451 void acpigen_write_register_resource(struct acpi_ctx *ctx,
452 const struct acpi_gen_regaddr *addr);
455 * acpigen_write_sleep() - Write a sleep operation
457 * @ctx: ACPI context pointer
458 * @sleep_ms: Number of milliseconds to sleep for
460 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
463 * acpigen_write_store() - Write a store operation
465 * @ctx: ACPI context pointer
467 void acpigen_write_store(struct acpi_ctx *ctx);
470 * acpigen_write_debug_string() - Write a debug string
472 * This writes a debug operation with an associated string
474 * @ctx: ACPI context pointer
475 * @str: String to write
477 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
480 * acpigen_write_or() - Write a bitwise OR 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_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
492 * acpigen_write_and() - Write a bitwise AND operation
496 * @ctx: ACPI context pointer
497 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
498 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
499 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
501 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
504 * acpigen_write_not() - Write a bitwise NOT operation
508 * @ctx: ACPI context pointer
509 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
510 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
512 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
515 * acpigen_write_power_res() - Write a power resource
517 * Name (_PRx, Package(One) { name })
519 * PowerResource (name, level, order)
521 * The caller should fill in the rest of the power resource and then call
522 * acpigen_pop_len() to close it off
524 * @ctx: ACPI context pointer
525 * @name: Name of power resource (e.g. "PRIC")
526 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
527 * @order: Order that this must be enabled/disabled (e.g. 0)
528 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
529 * @dev_states_count: Number of dev states
531 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
532 uint order, const char *const dev_states[],
533 size_t dev_states_count);
536 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
538 * This emits code to either enable to disable a Tx GPIO. It takes account of
541 * The code needs access to the DW0 register for the pad being used. This is
542 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
543 * which can read and write the pad's DW0 register given this address:
544 * @dw0_read: takes a single argument, the DW0 address
545 * returns the DW0 value
546 * @dw0:write: takes two arguments, the DW0 address and the value to write
549 * Example code (-- means comment):
551 * -- Get Pad Configuration DW0 register value
552 * Method (GPC0, 0x1, Serialized)
554 * -- Arg0 - GPIO DW0 address
555 * Store (Arg0, Local0)
556 * OperationRegion (PDW0, SystemMemory, Local0, 4)
557 * Field (PDW0, AnyAcc, NoLock, Preserve) {
563 * -- Set Pad Configuration DW0 register value
564 * Method (SPC0, 0x2, Serialized)
566 * -- Arg0 - GPIO DW0 address
567 * -- Arg1 - Value for DW0 register
568 * Store (Arg0, Local0)
569 * OperationRegion (PDW0, SystemMemory, Local0, 4)
570 * Field (PDW0, AnyAcc, NoLock, Preserve) {
577 * @ctx: ACPI context pointer
578 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
580 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
581 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
582 * @gpio: GPIO to change
583 * @enable: true to enable GPIO, false to disable
584 * Returns 0 on success, -ve on error.
586 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
587 const char *dw0_read, const char *dw0_write,
588 struct acpi_gpio *gpio, bool enable);
591 * acpigen_write_prw() - Write a power resource for wake (_PRW)
593 * @ctx: ACPI context pointer
594 * @wake: GPE that wakes up the device
595 * @level: Deepest power system sleeping state that can be entered while still
596 * providing wake functionality
598 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);
601 * acpigen_write_if() - Write an If block
603 * This requires a call to acpigen_pop_len() to complete the block
605 * @ctx: ACPI context pointer
607 void acpigen_write_if(struct acpi_ctx *ctx);
610 * acpigen_write_if_lequal_op_int() - Write comparison between op and integer
612 * Generates ACPI code for checking if operand1 and operand2 are equal
614 * If (Lequal (op, val))
616 * @ctx: ACPI context pointer
617 * @op: Operand to check
618 * @val: Value to check against
620 void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val);
623 * acpigen_write_else() - Write an Ef block
625 * This requires a call to acpigen_pop_len() to complete the block
627 * @ctx: ACPI context pointer
629 void acpigen_write_else(struct acpi_ctx *ctx);
632 * acpigen_write_to_buffer() - Write a ToBuffer operation
634 * E.g.: to generate: ToBuffer (Arg0, Local0)
635 * use acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP)
637 * @ctx: ACPI context pointer
638 * @src: Source argument
639 * @dst: Destination argument
641 void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst);
644 * acpigen_write_to_integer() - Write a ToInteger operation
646 * E.g.: to generate: ToInteger (Arg0, Local0)
647 * use acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP)
649 * @ctx: ACPI context pointer
650 * @src: Source argument
651 * @dst: Destination argument
653 void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst);
656 * acpigen_write_return_byte_buffer() - Write a return of a byte buffer
658 * @ctx: ACPI context pointer
659 * @arr: Array of bytes to return
660 * @size: Number of bytes
662 void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
666 * acpigen_write_return_singleton_buffer() - Write a return of a 1-byte buffer
668 * @ctx: ACPI context pointer
669 * @arg: Byte to return
671 void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg);
674 * acpigen_write_return_byte() - Write a return of a byte
676 * @ctx: ACPI context pointer
677 * @arg: Byte to return
679 void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg);
682 * acpigen_write_dsm_start() - Start a _DSM method
684 * Generate ACPI AML code to start the _DSM method.
686 * The functions need to be called in the correct sequence as below.
688 * Within the <generate-code-here> region, Local0 and Local1 must be are left
689 * untouched, but Local2-Local7 can be used
691 * Arguments passed into _DSM method:
694 * Arg2 = Function index
695 * Arg3 = Function-specific arguments
697 * AML code generated looks like this:
698 * Method (_DSM, 4, Serialized) { -- acpigen_write_dsm_start)
699 * ToBuffer (Arg0, Local0)
700 * If (LEqual (Local0, ToUUID(uuid))) { -- acpigen_write_dsm_uuid_start
701 * ToInteger (Arg2, Local1)
702 * If (LEqual (Local1, 0)) { -- acpigen_write_dsm_uuid_start_cond
703 * <generate-code-here>
704 * } -- acpigen_write_dsm_uuid_end_cond
706 * If (LEqual (Local1, n)) { -- acpigen_write_dsm_uuid_start_cond
707 * <generate-code-here>
708 * } -- acpigen_write_dsm_uuid_end_cond
709 * Return (Buffer (One) { 0x0 })
710 * } -- acpigen_write_dsm_uuid_end
712 * If (LEqual (Local0, ToUUID(uuidn))) {
715 * Return (Buffer (One) { 0x0 }) -- acpigen_write_dsm_end
718 * @ctx: ACPI context pointer
720 void acpigen_write_dsm_start(struct acpi_ctx *ctx);
723 * acpigen_write_dsm_uuid_start() - Start a new UUID block
725 * This starts generation of code to handle a particular UUID:
727 * If (LEqual (Local0, ToUUID(uuid))) {
728 * ToInteger (Arg2, Local1)
730 * @ctx: ACPI context pointer
732 int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid);
735 * acpigen_write_dsm_uuid_start_cond() - Start a new condition block
737 * This starts generation of condition-checking code to handle a particular
740 * If (LEqual (Local1, i))
742 * @ctx: ACPI context pointer
744 void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq);
747 * acpigen_write_dsm_uuid_end_cond() - Start a new condition block
749 * This ends generation of condition-checking code to handle a particular
754 * @ctx: ACPI context pointer
756 void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx);
759 * acpigen_write_dsm_uuid_end() - End a UUID block
761 * This ends generation of code to handle a particular UUID:
763 * Return (Buffer (One) { 0x0 })
765 * @ctx: ACPI context pointer
767 void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx);
770 * acpigen_write_dsm_end() - End a _DSM method
772 * This ends generates of the _DSM block:
774 * Return (Buffer (One) { 0x0 })
776 * @ctx: ACPI context pointer
778 void acpigen_write_dsm_end(struct acpi_ctx *ctx);