acpi: Add support for conditions and return values
[platform/kernel/u-boot.git] / include / acpi / acpigen.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Core ACPI (Advanced Configuration and Power Interface) support
4  *
5  * Copyright 2019 Google LLC
6  *
7  * Modified from coreboot file acpigen.h
8  */
9
10 #ifndef __ACPI_ACPIGEN_H
11 #define __ACPI_ACPIGEN_H
12
13 #include <linux/types.h>
14
15 struct acpi_ctx;
16 struct acpi_gen_regaddr;
17 struct acpi_gpio;
18
19 /* Top 4 bits of the value used to indicate a three-byte length value */
20 #define ACPI_PKG_LEN_3_BYTES    0x80
21
22 #define ACPI_METHOD_NARGS_MASK          0x7
23 #define ACPI_METHOD_SERIALIZED_MASK     BIT(3)
24
25 #define ACPI_END_TAG                    0x79
26
27 /* ACPI Op/Prefix codes */
28 enum {
29         ZERO_OP                 = 0x00,
30         ONE_OP                  = 0x01,
31         NAME_OP                 = 0x08,
32         BYTE_PREFIX             = 0x0a,
33         WORD_PREFIX             = 0x0b,
34         DWORD_PREFIX            = 0x0c,
35         STRING_PREFIX           = 0x0d,
36         QWORD_PREFIX            = 0x0e,
37         SCOPE_OP                = 0x10,
38         BUFFER_OP               = 0x11,
39         PACKAGE_OP              = 0x12,
40         METHOD_OP               = 0x14,
41         SLEEP_OP                = 0x22,
42         DUAL_NAME_PREFIX        = 0x2e,
43         MULTI_NAME_PREFIX       = 0x2f,
44         DEBUG_OP                = 0x31,
45         EXT_OP_PREFIX           = 0x5b,
46         ROOT_PREFIX             = 0x5c,
47         LOCAL0_OP               = 0x60,
48         LOCAL1_OP               = 0x61,
49         LOCAL2_OP               = 0x62,
50         LOCAL3_OP               = 0x63,
51         LOCAL4_OP               = 0x64,
52         LOCAL5_OP               = 0x65,
53         LOCAL6_OP               = 0x66,
54         LOCAL7_OP               = 0x67,
55         ARG0_OP                 = 0x68,
56         ARG1_OP                 = 0x69,
57         ARG2_OP                 = 0x6a,
58         ARG3_OP                 = 0x6b,
59         ARG4_OP                 = 0x6c,
60         ARG5_OP                 = 0x6d,
61         ARG6_OP                 = 0x6e,
62         STORE_OP                = 0x70,
63         AND_OP                  = 0x7b,
64         OR_OP                   = 0x7d,
65         NOT_OP                  = 0x80,
66         DEVICE_OP               = 0x82,
67         POWER_RES_OP            = 0x84,
68         LEQUAL_OP               = 0x93,
69         TO_BUFFER_OP            = 0x96,
70         TO_INTEGER_OP           = 0x99,
71         IF_OP                   = 0xa0,
72         ELSE_OP                 = 0xa1,
73         RETURN_OP               = 0xa4,
74 };
75
76 /**
77  * acpigen_get_current() - Get the current ACPI code output pointer
78  *
79  * @ctx: ACPI context pointer
80  * @return output pointer
81  */
82 u8 *acpigen_get_current(struct acpi_ctx *ctx);
83
84 /**
85  * acpigen_emit_byte() - Emit a byte to the ACPI code
86  *
87  * @ctx: ACPI context pointer
88  * @data: Value to output
89  */
90 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
91
92 /**
93  * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
94  *
95  * @ctx: ACPI context pointer
96  * @data: Value to output
97  */
98 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
99
100 /**
101  * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
102  *
103  * @ctx: ACPI context pointer
104  * @data: Value to output
105  */
106 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
107
108 /**
109  * acpigen_emit_stream() - Emit a stream of bytes
110  *
111  * @ctx: ACPI context pointer
112  * @data: Data to output
113  * @size: Size of data in bytes
114  */
115 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
116
117 /**
118  * acpigen_emit_string() - Emit a string
119  *
120  * Emit a string with a null terminator
121  *
122  * @ctx: ACPI context pointer
123  * @str: String to output, or NULL for an empty string
124  */
125 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
126
127 /**
128  * acpigen_write_len_f() - Write a 'forward' length placeholder
129  *
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.
133  *
134  * Usage:
135  *
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() ----------/
145  *
146  * See ACPI 6.3 section 20.2.4 Package Length Encoding
147  *
148  * This implementation always uses a 3-byte packet length for simplicity. It
149  * could be adjusted to support other lengths.
150  *
151  * @ctx: ACPI context pointer
152  */
153 void acpigen_write_len_f(struct acpi_ctx *ctx);
154
155 /**
156  * acpigen_pop_len() - Update the previously stacked length placeholder
157  *
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.
160  *
161  * @ctx: ACPI context pointer
162  */
163 void acpigen_pop_len(struct acpi_ctx *ctx);
164
165 /**
166  * acpigen_write_package() - Start writing a package
167  *
168  * A package collects together a number of elements in the ACPI code. To write
169  * a package use:
170  *
171  * acpigen_write_package(ctx, 3);
172  * ...write things
173  * acpigen_pop_len()
174  *
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:
177  *
178  * char *num_elements = acpigen_write_package(ctx, 0);
179  * ...write things
180  * *num_elements += 1;
181  * ...write things
182  * *num_elements += 1;
183  * acpigen_pop_len()
184  *
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
188  *      caller if needed
189  */
190 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
191
192 /**
193  * acpigen_write_byte() - Write a byte
194  *
195  * @ctx: ACPI context pointer
196  * @data: Value to write
197  */
198 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
199
200 /**
201  * acpigen_write_word() - Write a word
202  *
203  * @ctx: ACPI context pointer
204  * @data: Value to write
205  */
206 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
207
208 /**
209  * acpigen_write_dword() - Write a dword
210  *
211  * @ctx: ACPI context pointer
212  * @data: Value to write
213  */
214 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
215
216 /**
217  * acpigen_write_qword() - Write a qword
218  *
219  * @ctx: ACPI context pointer
220  * @data: Value to write
221  */
222 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
223
224 /**
225  * acpigen_write_zero() - Write zero
226  *
227  * @ctx: ACPI context pointer
228  */
229 void acpigen_write_zero(struct acpi_ctx *ctx);
230
231 /**
232  * acpigen_write_one() - Write one
233  *
234  * @ctx: ACPI context pointer
235  */
236 void acpigen_write_one(struct acpi_ctx *ctx);
237
238 /**
239  * acpigen_write_integer() - Write an integer
240  *
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.
243  *
244  * @ctx: ACPI context pointer
245  * @data: Integer to write
246  */
247 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
248
249 /**
250  * acpigen_write_name_zero() - Write a named zero value
251  *
252  * @ctx: ACPI context pointer
253  * @name: Name of the value
254  */
255 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name);
256
257 /**
258  * acpigen_write_name_one() - Write a named one value
259  *
260  * @ctx: ACPI context pointer
261  * @name: Name of the value
262  */
263 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name);
264
265 /**
266  * acpigen_write_name_byte() - Write a named byte value
267  *
268  * @ctx: ACPI context pointer
269  * @name: Name of the value
270  * @val: Value to write
271  */
272 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val);
273
274 /**
275  * acpigen_write_name_word() - Write a named word value
276  *
277  * @ctx: ACPI context pointer
278  * @name: Name of the value
279  * @val: Value to write
280  */
281 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val);
282
283 /**
284  * acpigen_write_name_dword() - Write a named dword value
285  *
286  * @ctx: ACPI context pointer
287  * @name: Name of the value
288  * @val: Value to write
289  */
290 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val);
291
292 /**
293  * acpigen_write_name_qword() - Write a named qword value
294  *
295  * @ctx: ACPI context pointer
296  * @name: Name of the value
297  * @val: Value to write
298  */
299 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val);
300
301 /**
302  * acpigen_write_name_integer() - Write a named integer value
303  *
304  * @ctx: ACPI context pointer
305  * @name: Name of the value
306  * @val: Value to write
307  */
308 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name,
309                                 u64 val);
310
311 /**
312  * acpigen_write_name_string() - Write a named string value
313  *
314  * @ctx: ACPI context pointer
315  * @name: Name of the value
316  * @string: String to write
317  */
318 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
319                                const char *string);
320
321 /**
322  * acpigen_write_string() - Write a string
323  *
324  * This writes a STRING_PREFIX followed by a null-terminated string
325  *
326  * @ctx: ACPI context pointer
327  * @str: String to write
328  */
329 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
330
331 /**
332  * acpigen_emit_namestring() - Emit an ACPI name
333  *
334  * This writes out an ACPI name or path in the required special format. It does
335  * not add the NAME_OP prefix.
336  *
337  * @ctx: ACPI context pointer
338  * @namepath: Name / path to emit
339  */
340 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
341
342 /**
343  * acpigen_write_name() - Write out an ACPI name
344  *
345  * This writes out an ACPI name or path in the required special format with a
346  * NAME_OP prefix.
347  *
348  * @ctx: ACPI context pointer
349  * @namepath: Name / path to emit
350  */
351 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
352
353 /**
354  * acpigen_write_scope() - Write a scope
355  *
356  * @ctx: ACPI context pointer
357  * @scope: Scope to write (e.g. "\\_SB.ABCD")
358  */
359 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope);
360
361 /**
362  * acpigen_write_uuid() - Write a UUID
363  *
364  * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
365  *
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
369  */
370 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
371
372 /**
373  * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
374  *
375  * @ctx: ACPI context pointer
376  * @op: Operation code (e.g. SLEEP_OP)
377  */
378 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
379
380 /**
381  * acpigen_write_method() - Write a method header
382  *
383  * @ctx: ACPI context pointer
384  * @name: Method name (4 characters)
385  * @nargs: Number of method arguments (0 if none)
386  */
387 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
388
389 /**
390  * acpigen_write_method_serialized() - Write a method header
391  *
392  * This sets the 'serialized' flag so that the method is thread-safe
393  *
394  * @ctx: ACPI context pointer
395  * @name: Method name (4 characters)
396  * @nargs: Number of method arguments (0 if none)
397  */
398 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
399                                      int nargs);
400
401 /**
402  * acpigen_write_device() - Write an ACPI device
403  *
404  * @ctx: ACPI context pointer
405  * @name: Device name to write
406  */
407 void acpigen_write_device(struct acpi_ctx *ctx, const char *name);
408
409 /**
410  * acpigen_write_sta() - Write a _STA method
411  *
412  * @ctx: ACPI context pointer
413  * @status: Status value to return
414  */
415 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
416
417 /**
418  * acpigen_write_resourcetemplate_header() - Write a ResourceTemplate header
419  *
420  * @ctx: ACPI context pointer
421  */
422 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx);
423
424 /**
425  * acpigen_write_resourcetemplate_footer() - Write a ResourceTemplate footer
426  *
427  * @ctx: ACPI context pointer
428  */
429 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx);
430
431 /**
432  * acpigen_write_register_resource() - Write a register resource
433  *
434  * This writes a header, the address information and a footer
435  *
436  * @ctx: ACPI context pointer
437  * @addr: Address to write
438  */
439 void acpigen_write_register_resource(struct acpi_ctx *ctx,
440                                      const struct acpi_gen_regaddr *addr);
441
442 /**
443  * acpigen_write_sleep() - Write a sleep operation
444  *
445  * @ctx: ACPI context pointer
446  * @sleep_ms: Number of milliseconds to sleep for
447  */
448 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
449
450 /**
451  * acpigen_write_store() - Write a store operation
452  *
453  * @ctx: ACPI context pointer
454  */
455 void acpigen_write_store(struct acpi_ctx *ctx);
456
457 /**
458  * acpigen_write_debug_string() - Write a debug string
459  *
460  * This writes a debug operation with an associated string
461  *
462  * @ctx: ACPI context pointer
463  * @str: String to write
464  */
465 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
466
467 /**
468  * acpigen_write_or() - Write a bitwise OR operation
469  *
470  * res = arg1 | arg2
471  *
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)
476  */
477 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
478
479 /**
480  * acpigen_write_and() - Write a bitwise AND operation
481  *
482  * res = arg1 & arg2
483  *
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)
488  */
489 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
490
491 /**
492  * acpigen_write_not() - Write a bitwise NOT operation
493  *
494  * res = ~arg1
495  *
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)
499  */
500 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
501
502 /**
503  * acpigen_write_power_res() - Write a power resource
504  *
505  * Name (_PRx, Package(One) { name })
506  * ...
507  * PowerResource (name, level, order)
508  *
509  * The caller should fill in the rest of the power resource and then call
510  * acpigen_pop_len() to close it off
511  *
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
518  */
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);
522
523 /**
524  * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
525  *
526  * This emits code to either enable to disable a Tx GPIO. It takes account of
527  * the GPIO polarity.
528  *
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
535  *               no return value
536  *
537  * Example code (-- means comment):
538  *
539  *      -- Get Pad Configuration DW0 register value
540  *      Method (GPC0, 0x1, Serialized)
541  *      {
542  *              -- Arg0 - GPIO DW0 address
543  *              Store (Arg0, Local0)
544  *              OperationRegion (PDW0, SystemMemory, Local0, 4)
545  *              Field (PDW0, AnyAcc, NoLock, Preserve) {
546  *                      TEMP, 32
547  *              }
548  *              Return (TEMP)
549  *      }
550  *
551  *      -- Set Pad Configuration DW0 register value
552  *      Method (SPC0, 0x2, Serialized)
553  *      {
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) {
559  *                      TEMP,32
560  *              }
561  *              Store (Arg1, TEMP)
562  *      }
563  *
564  *
565  * @ctx: ACPI context pointer
566  * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
567  *      PAD_CFG0_TX_STATE
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.
573  */
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);
577
578 /**
579  * acpigen_write_prw() - Write a power resource for wake (_PRW)
580  *
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
585  */
586 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level);
587
588 /**
589  * acpigen_write_if() - Write an If block
590  *
591  * This requires a call to acpigen_pop_len() to complete the block
592  *
593  * @ctx: ACPI context pointer
594  */
595 void acpigen_write_if(struct acpi_ctx *ctx);
596
597 /**
598  * acpigen_write_if_lequal_op_int() - Write comparison between op and integer
599  *
600  * Generates ACPI code for checking if operand1 and operand2 are equal
601  *
602  * If (Lequal (op, val))
603  *
604  * @ctx: ACPI context pointer
605  * @op: Operand to check
606  * @val: Value to check against
607  */
608 void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val);
609
610 /**
611  * acpigen_write_else() - Write an Ef block
612  *
613  * This requires a call to acpigen_pop_len() to complete the block
614  *
615  * @ctx: ACPI context pointer
616  */
617 void acpigen_write_else(struct acpi_ctx *ctx);
618
619 /**
620  * acpigen_write_to_buffer() - Write a ToBuffer operation
621  *
622  * E.g.: to generate: ToBuffer (Arg0, Local0)
623  * use acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP)
624  *
625  * @ctx: ACPI context pointer
626  * @src: Source argument
627  * @dst: Destination argument
628  */
629 void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst);
630
631 /**
632  * acpigen_write_to_integer() - Write a ToInteger operation
633  *
634  * E.g.: to generate: ToInteger (Arg0, Local0)
635  * use acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP)
636  *
637  * @ctx: ACPI context pointer
638  * @src: Source argument
639  * @dst: Destination argument
640  */
641 void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst);
642
643 /**
644  * acpigen_write_return_byte_buffer() - Write a return of a byte buffer
645  *
646  * @ctx: ACPI context pointer
647  * @arr: Array of bytes to return
648  * @size: Number of bytes
649  */
650 void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
651                                       size_t size);
652
653 /**
654  * acpigen_write_return_singleton_buffer() - Write a return of a 1-byte buffer
655  *
656  * @ctx: ACPI context pointer
657  * @arg: Byte to return
658  */
659 void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg);
660
661 /**
662  * acpigen_write_return_byte() - Write a return of a byte
663  *
664  * @ctx: ACPI context pointer
665  * @arg: Byte to return
666  */
667 void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg);
668
669 #endif