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,
79 * enum psd_coord - Coordination types for P-states
81 * The type of coordination that exists (hardware) or is required (software) as
82 * a result of the underlying hardware dependency
91 * acpigen_get_current() - Get the current ACPI code output pointer
93 * @ctx: ACPI context pointer
94 * @return output pointer
96 u8 *acpigen_get_current(struct acpi_ctx *ctx);
99 * acpigen_emit_byte() - Emit a byte to the ACPI code
101 * @ctx: ACPI context pointer
102 * @data: Value to output
104 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
107 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
109 * @ctx: ACPI context pointer
110 * @data: Value to output
112 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
115 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
117 * @ctx: ACPI context pointer
118 * @data: Value to output
120 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
123 * acpigen_emit_stream() - Emit a stream of bytes
125 * @ctx: ACPI context pointer
126 * @data: Data to output
127 * @size: Size of data in bytes
129 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
132 * acpigen_emit_string() - Emit a string
134 * Emit a string with a null terminator
136 * @ctx: ACPI context pointer
137 * @str: String to output, or NULL for an empty string
139 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
142 * acpigen_write_len_f() - Write a 'forward' length placeholder
144 * This adds space for a length value in the ACPI stream and pushes the current
145 * position (before the length) on the stack. After calling this you can write
146 * some data and then call acpigen_pop_len() to update the length value.
150 * acpigen_write_len_f() ------\
151 * acpigen_write...() |
152 * acpigen_write...() |
153 * acpigen_write_len_f() --\ |
154 * acpigen_write...() | |
155 * acpigen_write...() | |
156 * acpigen_pop_len() ------/ |
157 * acpigen_write...() |
158 * acpigen_pop_len() ----------/
160 * See ACPI 6.3 section 20.2.4 Package Length Encoding
162 * This implementation always uses a 3-byte packet length for simplicity. It
163 * could be adjusted to support other lengths.
165 * @ctx: ACPI context pointer
167 void acpigen_write_len_f(struct acpi_ctx *ctx);
170 * acpigen_pop_len() - Update the previously stacked length placeholder
172 * Call this after the data for the block has been written. It updates the
173 * top length value in the stack and pops it off.
175 * @ctx: ACPI context pointer
177 void acpigen_pop_len(struct acpi_ctx *ctx);
180 * acpigen_write_package() - Start writing a package
182 * A package collects together a number of elements in the ACPI code. To write
185 * acpigen_write_package(ctx, 3);
189 * If you don't know the number of elements in advance, acpigen_write_package()
190 * returns a pointer to the value so you can update it later:
192 * char *num_elements = acpigen_write_package(ctx, 0);
194 * *num_elements += 1;
196 * *num_elements += 1;
199 * @ctx: ACPI context pointer
200 * @nr_el: Number of elements (0 if not known)
201 * @returns pointer to the number of elements, which can be updated by the
204 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
207 * acpigen_write_byte() - Write a byte
209 * @ctx: ACPI context pointer
210 * @data: Value to write
212 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
215 * acpigen_write_word() - Write a word
217 * @ctx: ACPI context pointer
218 * @data: Value to write
220 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
223 * acpigen_write_dword() - Write a dword
225 * @ctx: ACPI context pointer
226 * @data: Value to write
228 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
231 * acpigen_write_qword() - Write a qword
233 * @ctx: ACPI context pointer
234 * @data: Value to write
236 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
239 * acpigen_write_zero() - Write zero
241 * @ctx: ACPI context pointer
243 void acpigen_write_zero(struct acpi_ctx *ctx);
246 * acpigen_write_one() - Write one
248 * @ctx: ACPI context pointer
250 void acpigen_write_one(struct acpi_ctx *ctx);
253 * acpigen_write_integer() - Write an integer
255 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
256 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
258 * @ctx: ACPI context pointer
259 * @data: Integer to write
261 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
264 * acpigen_write_name_zero() - Write a named zero value
266 * @ctx: ACPI context pointer
267 * @name: Name of the value
269 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name);
272 * acpigen_write_name_one() - Write a named one value
274 * @ctx: ACPI context pointer
275 * @name: Name of the value
277 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name);
280 * acpigen_write_name_byte() - Write a named byte value
282 * @ctx: ACPI context pointer
283 * @name: Name of the value
284 * @val: Value to write
286 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val);
289 * acpigen_write_name_word() - Write a named word value
291 * @ctx: ACPI context pointer
292 * @name: Name of the value
293 * @val: Value to write
295 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val);
298 * acpigen_write_name_dword() - Write a named dword value
300 * @ctx: ACPI context pointer
301 * @name: Name of the value
302 * @val: Value to write
304 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val);
307 * acpigen_write_name_qword() - Write a named qword value
309 * @ctx: ACPI context pointer
310 * @name: Name of the value
311 * @val: Value to write
313 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val);
316 * acpigen_write_name_integer() - Write a named integer value
318 * @ctx: ACPI context pointer
319 * @name: Name of the value
320 * @val: Value to write
322 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name,
326 * acpigen_write_name_string() - Write a named string value
328 * @ctx: ACPI context pointer
329 * @name: Name of the value
330 * @string: String to write
332 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
336 * acpigen_write_string() - Write a string
338 * This writes a STRING_PREFIX followed by a null-terminated string
340 * @ctx: ACPI context pointer
341 * @str: String to write
343 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
346 * acpigen_emit_namestring() - Emit an ACPI name
348 * This writes out an ACPI name or path in the required special format. It does
349 * not add the NAME_OP prefix.
351 * @ctx: ACPI context pointer
352 * @namepath: Name / path to emit
354 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
357 * acpigen_write_name() - Write out an ACPI name
359 * This writes out an ACPI name or path in the required special format with a
362 * @ctx: ACPI context pointer
363 * @namepath: Name / path to emit
365 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
368 * acpigen_write_scope() - Write a scope
370 * @ctx: ACPI context pointer
371 * @scope: Scope to write (e.g. "\\_SB.ABCD")
373 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
376 * acpigen_write_uuid() - Write a UUID
378 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
380 * @ctx: ACPI context pointer
381 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
382 * @return 0 if OK, -EINVAL if the format is incorrect
384 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
387 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
389 * @ctx: ACPI context pointer
390 * @op: Operation code (e.g. SLEEP_OP)
392 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
395 * acpigen_write_method() - Write a method header
397 * @ctx: ACPI context pointer
398 * @name: Method name (4 characters)
399 * @nargs: Number of method arguments (0 if none)
401 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
404 * acpigen_write_method_serialized() - Write a method header
406 * This sets the 'serialized' flag so that the method is thread-safe
408 * @ctx: ACPI context pointer
409 * @name: Method name (4 characters)
410 * @nargs: Number of method arguments (0 if none)
412 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
416 * acpigen_write_device() - Write an ACPI device
418 * @ctx: ACPI context pointer
419 * @name: Device name to write
421 void acpigen_write_device(struct acpi_ctx *ctx, const char *name);
424 * acpigen_write_sta() - Write a _STA method
426 * @ctx: ACPI context pointer
427 * @status: Status value to return
429 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
432 * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
434 * @ctx: ACPI context pointer
436 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
439 * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
441 * @ctx: ACPI context pointer
443 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
446 * acpigen_write_register_resource() - Write a register resource
448 * This writes a header, the address information and a footer
450 * @ctx: ACPI context pointer
451 * @addr: Address to write
453 void acpigen_write_register_resource(struct acpi_ctx *ctx,
454 const struct acpi_gen_regaddr *addr);
457 * acpigen_write_sleep() - Write a sleep operation
459 * @ctx: ACPI context pointer
460 * @sleep_ms: Number of milliseconds to sleep for
462 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
465 * acpigen_write_store() - Write a store operation
467 * @ctx: ACPI context pointer
469 void acpigen_write_store(struct acpi_ctx *ctx);
472 * acpigen_write_debug_string() - Write a debug string
474 * This writes a debug operation with an associated string
476 * @ctx: ACPI context pointer
477 * @str: String to write
479 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
482 * acpigen_write_or() - Write a bitwise OR operation
486 * @ctx: ACPI context pointer
487 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
488 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
489 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
491 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
494 * acpigen_write_and() - Write a bitwise AND operation
498 * @ctx: ACPI context pointer
499 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
500 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
501 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
503 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
506 * acpigen_write_not() - Write a bitwise NOT operation
510 * @ctx: ACPI context pointer
511 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
512 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
514 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
517 * acpigen_write_power_res() - Write a power resource
519 * Name (_PRx, Package(One) { name })
521 * PowerResource (name, level, order)
523 * The caller should fill in the rest of the power resource and then call
524 * acpigen_pop_len() to close it off
526 * @ctx: ACPI context pointer
527 * @name: Name of power resource (e.g. "PRIC")
528 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
529 * @order: Order that this must be enabled/disabled (e.g. 0)
530 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
531 * @dev_states_count: Number of dev states
533 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
534 uint order, const char *const dev_states[],
535 size_t dev_states_count);
538 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
540 * This emits code to either enable to disable a Tx GPIO. It takes account of
543 * The code needs access to the DW0 register for the pad being used. This is
544 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
545 * which can read and write the pad's DW0 register given this address:
546 * @dw0_read: takes a single argument, the DW0 address
547 * returns the DW0 value
548 * @dw0:write: takes two arguments, the DW0 address and the value to write
551 * Example code (-- means comment):
553 * -- Get Pad Configuration DW0 register value
554 * Method (GPC0, 0x1, Serialized)
556 * -- Arg0 - GPIO DW0 address
557 * Store (Arg0, Local0)
558 * OperationRegion (PDW0, SystemMemory, Local0, 4)
559 * Field (PDW0, AnyAcc, NoLock, Preserve) {
565 * -- Set Pad Configuration DW0 register value
566 * Method (SPC0, 0x2, Serialized)
568 * -- Arg0 - GPIO DW0 address
569 * -- Arg1 - Value for DW0 register
570 * Store (Arg0, Local0)
571 * OperationRegion (PDW0, SystemMemory, Local0, 4)
572 * Field (PDW0, AnyAcc, NoLock, Preserve) {
579 * @ctx: ACPI context pointer
580 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
582 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
583 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
584 * @gpio: GPIO to change
585 * @enable: true to enable GPIO, false to disable
586 * Returns 0 on success, -ve on error.
588 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
589 const char *dw0_read, const char *dw0_write,
590 struct acpi_gpio *gpio, bool enable);
593 * acpigen_write_prw() - Write a power resource for wake (_PRW)
595 * @ctx: ACPI context pointer
596 * @wake: GPE that wakes up the device
597 * @level: Deepest power system sleeping state that can be entered while still
598 * providing wake functionality
600 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);
603 * acpigen_write_if() - Write an If block
605 * This requires a call to acpigen_pop_len() to complete the block
607 * @ctx: ACPI context pointer
609 void acpigen_write_if(struct acpi_ctx *ctx);
612 * acpigen_write_if_lequal_op_int() - Write comparison between op and integer
614 * Generates ACPI code for checking if operand1 and operand2 are equal
616 * If (Lequal (op, val))
618 * @ctx: ACPI context pointer
619 * @op: Operand to check
620 * @val: Value to check against
622 void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val);
625 * acpigen_write_else() - Write an Ef block
627 * This requires a call to acpigen_pop_len() to complete the block
629 * @ctx: ACPI context pointer
631 void acpigen_write_else(struct acpi_ctx *ctx);
634 * acpigen_write_to_buffer() - Write a ToBuffer operation
636 * E.g.: to generate: ToBuffer (Arg0, Local0)
637 * use acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP)
639 * @ctx: ACPI context pointer
640 * @src: Source argument
641 * @dst: Destination argument
643 void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst);
646 * acpigen_write_to_integer() - Write a ToInteger operation
648 * E.g.: to generate: ToInteger (Arg0, Local0)
649 * use acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP)
651 * @ctx: ACPI context pointer
652 * @src: Source argument
653 * @dst: Destination argument
655 void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst);
658 * acpigen_write_return_byte_buffer() - Write a return of a byte buffer
660 * @ctx: ACPI context pointer
661 * @arr: Array of bytes to return
662 * @size: Number of bytes
664 void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
668 * acpigen_write_return_singleton_buffer() - Write a return of a 1-byte buffer
670 * @ctx: ACPI context pointer
671 * @arg: Byte to return
673 void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg);
676 * acpigen_write_return_byte() - Write a return of a byte
678 * @ctx: ACPI context pointer
679 * @arg: Byte to return
681 void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg);
684 * acpigen_write_dsm_start() - Start a _DSM method
686 * Generate ACPI AML code to start the _DSM method.
688 * The functions need to be called in the correct sequence as below.
690 * Within the <generate-code-here> region, Local0 and Local1 must be are left
691 * untouched, but Local2-Local7 can be used
693 * Arguments passed into _DSM method:
696 * Arg2 = Function index
697 * Arg3 = Function-specific arguments
699 * AML code generated looks like this:
700 * Method (_DSM, 4, Serialized) { -- acpigen_write_dsm_start)
701 * ToBuffer (Arg0, Local0)
702 * If (LEqual (Local0, ToUUID(uuid))) { -- acpigen_write_dsm_uuid_start
703 * ToInteger (Arg2, Local1)
704 * If (LEqual (Local1, 0)) { -- acpigen_write_dsm_uuid_start_cond
705 * <generate-code-here>
706 * } -- acpigen_write_dsm_uuid_end_cond
708 * If (LEqual (Local1, n)) { -- acpigen_write_dsm_uuid_start_cond
709 * <generate-code-here>
710 * } -- acpigen_write_dsm_uuid_end_cond
711 * Return (Buffer (One) { 0x0 })
712 * } -- acpigen_write_dsm_uuid_end
714 * If (LEqual (Local0, ToUUID(uuidn))) {
717 * Return (Buffer (One) { 0x0 }) -- acpigen_write_dsm_end
720 * @ctx: ACPI context pointer
722 void acpigen_write_dsm_start(struct acpi_ctx *ctx);
725 * acpigen_write_dsm_uuid_start() - Start a new UUID block
727 * This starts generation of code to handle a particular UUID:
729 * If (LEqual (Local0, ToUUID(uuid))) {
730 * ToInteger (Arg2, Local1)
732 * @ctx: ACPI context pointer
734 int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid);
737 * acpigen_write_dsm_uuid_start_cond() - Start a new condition block
739 * This starts generation of condition-checking code to handle a particular
742 * If (LEqual (Local1, i))
744 * @ctx: ACPI context pointer
746 void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq);
749 * acpigen_write_dsm_uuid_end_cond() - Start a new condition block
751 * This ends generation of condition-checking code to handle a particular
756 * @ctx: ACPI context pointer
758 void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx);
761 * acpigen_write_dsm_uuid_end() - End a UUID block
763 * This ends generation of code to handle a particular UUID:
765 * Return (Buffer (One) { 0x0 })
767 * @ctx: ACPI context pointer
769 void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx);
772 * acpigen_write_dsm_end() - End a _DSM method
774 * This ends generates of the _DSM block:
776 * Return (Buffer (One) { 0x0 })
778 * @ctx: ACPI context pointer
780 void acpigen_write_dsm_end(struct acpi_ctx *ctx);
783 * acpigen_write_processor() - Write a Processor package
785 * This emits a Processor package header with the required information. The
786 * caller must complete the information and call acpigen_pop_len() at the end
788 * @ctx: ACPI context pointer
789 * @cpuindex: CPU number
790 * @pblock_addr: PBlk system IO address
791 * @pblock_len: PBlk length
793 void acpigen_write_processor(struct acpi_ctx *ctx, uint cpuindex,
794 u32 pblock_addr, uint pblock_len);
797 * acpigen_write_processor_package() - Write a package containing the processors
799 * The package containins the name of each processor in the SoC
801 * @ctx: ACPI context pointer
802 * @name: Package name (.e.g "PPKG")
803 * @first_core: Number of the first core (e.g. 0)
804 * @core_count: Number of cores (e.g. 4)
806 void acpigen_write_processor_package(struct acpi_ctx *ctx, const char *name,
807 uint first_core, uint core_count);
810 * acpigen_write_processor_cnot() - Write a processor notification method
812 * This writes a method that notifies all CPU cores
814 * @ctx: ACPI context pointer
815 * @num_cores: Number of CPU cores
817 void acpigen_write_processor_cnot(struct acpi_ctx *ctx, const uint num_cores);