acpi: Export functions to write sized 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_gpio;
17
18 /* Top 4 bits of the value used to indicate a three-byte length value */
19 #define ACPI_PKG_LEN_3_BYTES    0x80
20
21 #define ACPI_METHOD_NARGS_MASK          0x7
22 #define ACPI_METHOD_SERIALIZED_MASK     BIT(3)
23
24 /* ACPI Op/Prefix codes */
25 enum {
26         ZERO_OP                 = 0x00,
27         ONE_OP                  = 0x01,
28         NAME_OP                 = 0x08,
29         BYTE_PREFIX             = 0x0a,
30         WORD_PREFIX             = 0x0b,
31         DWORD_PREFIX            = 0x0c,
32         STRING_PREFIX           = 0x0d,
33         QWORD_PREFIX            = 0x0e,
34         BUFFER_OP               = 0x11,
35         PACKAGE_OP              = 0x12,
36         METHOD_OP               = 0x14,
37         SLEEP_OP                = 0x22,
38         DUAL_NAME_PREFIX        = 0x2e,
39         MULTI_NAME_PREFIX       = 0x2f,
40         DEBUG_OP                = 0x31,
41         EXT_OP_PREFIX           = 0x5b,
42         ROOT_PREFIX             = 0x5c,
43         LOCAL0_OP               = 0x60,
44         LOCAL1_OP               = 0x61,
45         LOCAL2_OP               = 0x62,
46         LOCAL3_OP               = 0x63,
47         LOCAL4_OP               = 0x64,
48         LOCAL5_OP               = 0x65,
49         LOCAL6_OP               = 0x66,
50         LOCAL7_OP               = 0x67,
51         STORE_OP                = 0x70,
52         AND_OP                  = 0x7b,
53         OR_OP                   = 0x7d,
54         NOT_OP                  = 0x80,
55         POWER_RES_OP            = 0x84,
56         RETURN_OP               = 0xa4,
57 };
58
59 /**
60  * acpigen_get_current() - Get the current ACPI code output pointer
61  *
62  * @ctx: ACPI context pointer
63  * @return output pointer
64  */
65 u8 *acpigen_get_current(struct acpi_ctx *ctx);
66
67 /**
68  * acpigen_emit_byte() - Emit a byte to the ACPI code
69  *
70  * @ctx: ACPI context pointer
71  * @data: Value to output
72  */
73 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
74
75 /**
76  * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
77  *
78  * @ctx: ACPI context pointer
79  * @data: Value to output
80  */
81 void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
82
83 /**
84  * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
85  *
86  * @ctx: ACPI context pointer
87  * @data: Value to output
88  */
89 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
90
91 /**
92  * acpigen_emit_stream() - Emit a stream of bytes
93  *
94  * @ctx: ACPI context pointer
95  * @data: Data to output
96  * @size: Size of data in bytes
97  */
98 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
99
100 /**
101  * acpigen_emit_string() - Emit a string
102  *
103  * Emit a string with a null terminator
104  *
105  * @ctx: ACPI context pointer
106  * @str: String to output, or NULL for an empty string
107  */
108 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
109
110 /**
111  * acpigen_write_len_f() - Write a 'forward' length placeholder
112  *
113  * This adds space for a length value in the ACPI stream and pushes the current
114  * position (before the length) on the stack. After calling this you can write
115  * some data and then call acpigen_pop_len() to update the length value.
116  *
117  * Usage:
118  *
119  *    acpigen_write_len_f() ------\
120  *    acpigen_write...()          |
121  *    acpigen_write...()          |
122  *      acpigen_write_len_f() --\ |
123  *      acpigen_write...()      | |
124  *      acpigen_write...()      | |
125  *      acpigen_pop_len() ------/ |
126  *    acpigen_write...()          |
127  *    acpigen_pop_len() ----------/
128  *
129  * See ACPI 6.3 section 20.2.4 Package Length Encoding
130  *
131  * This implementation always uses a 3-byte packet length for simplicity. It
132  * could be adjusted to support other lengths.
133  *
134  * @ctx: ACPI context pointer
135  */
136 void acpigen_write_len_f(struct acpi_ctx *ctx);
137
138 /**
139  * acpigen_pop_len() - Update the previously stacked length placeholder
140  *
141  * Call this after the data for the block has been written. It updates the
142  * top length value in the stack and pops it off.
143  *
144  * @ctx: ACPI context pointer
145  */
146 void acpigen_pop_len(struct acpi_ctx *ctx);
147
148 /**
149  * acpigen_write_package() - Start writing a package
150  *
151  * A package collects together a number of elements in the ACPI code. To write
152  * a package use:
153  *
154  * acpigen_write_package(ctx, 3);
155  * ...write things
156  * acpigen_pop_len()
157  *
158  * If you don't know the number of elements in advance, acpigen_write_package()
159  * returns a pointer to the value so you can update it later:
160  *
161  * char *num_elements = acpigen_write_package(ctx, 0);
162  * ...write things
163  * *num_elements += 1;
164  * ...write things
165  * *num_elements += 1;
166  * acpigen_pop_len()
167  *
168  * @ctx: ACPI context pointer
169  * @nr_el: Number of elements (0 if not known)
170  * @returns pointer to the number of elements, which can be updated by the
171  *      caller if needed
172  */
173 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
174
175 /**
176  * acpigen_write_byte() - Write a byte
177  *
178  * @ctx: ACPI context pointer
179  * @data: Value to write
180  */
181 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data);
182
183 /**
184  * acpigen_write_word() - Write a word
185  *
186  * @ctx: ACPI context pointer
187  * @data: Value to write
188  */
189 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data);
190
191 /**
192  * acpigen_write_dword() - Write a dword
193  *
194  * @ctx: ACPI context pointer
195  * @data: Value to write
196  */
197 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data);
198
199 /**
200  * acpigen_write_qword() - Write a qword
201  *
202  * @ctx: ACPI context pointer
203  * @data: Value to write
204  */
205 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data);
206
207 /**
208  * acpigen_write_zero() - Write zero
209  *
210  * @ctx: ACPI context pointer
211  */
212 void acpigen_write_zero(struct acpi_ctx *ctx);
213
214 /**
215  * acpigen_write_one() - Write one
216  *
217  * @ctx: ACPI context pointer
218  */
219 void acpigen_write_one(struct acpi_ctx *ctx);
220
221 /**
222  * acpigen_write_integer() - Write an integer
223  *
224  * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
225  * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
226  *
227  * @ctx: ACPI context pointer
228  * @data: Integer to write
229  */
230 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
231
232 /**
233  * acpigen_write_string() - Write a string
234  *
235  * This writes a STRING_PREFIX followed by a null-terminated string
236  *
237  * @ctx: ACPI context pointer
238  * @str: String to write
239  */
240 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
241
242 /**
243  * acpigen_emit_namestring() - Emit an ACPI name
244  *
245  * This writes out an ACPI name or path in the required special format. It does
246  * not add the NAME_OP prefix.
247  *
248  * @ctx: ACPI context pointer
249  * @namepath: Name / path to emit
250  */
251 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
252
253 /**
254  * acpigen_write_name() - Write out an ACPI name
255  *
256  * This writes out an ACPI name or path in the required special format with a
257  * NAME_OP prefix.
258  *
259  * @ctx: ACPI context pointer
260  * @namepath: Name / path to emit
261  */
262 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
263
264 /**
265  * acpigen_write_uuid() - Write a UUID
266  *
267  * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
268  *
269  * @ctx: ACPI context pointer
270  * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
271  * @return 0 if OK, -EINVAL if the format is incorrect
272  */
273 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
274
275 /**
276  * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
277  *
278  * @ctx: ACPI context pointer
279  * @op: Operation code (e.g. SLEEP_OP)
280  */
281 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
282
283 /**
284  * acpigen_write_method() - Write a method header
285  *
286  * @ctx: ACPI context pointer
287  * @name: Method name (4 characters)
288  * @nargs: Number of method arguments (0 if none)
289  */
290 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
291
292 /**
293  * acpigen_write_method_serialized() - Write a method header
294  *
295  * This sets the 'serialized' flag so that the method is thread-safe
296  *
297  * @ctx: ACPI context pointer
298  * @name: Method name (4 characters)
299  * @nargs: Number of method arguments (0 if none)
300  */
301 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
302                                      int nargs);
303
304 /**
305  * acpigen_write_sta() - Write a _STA method
306  *
307  * @ctx: ACPI context pointer
308  * @status: Status value to return
309  */
310 void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
311
312 /**
313  * acpigen_write_sleep() - Write a sleep operation
314  *
315  * @ctx: ACPI context pointer
316  * @sleep_ms: Number of milliseconds to sleep for
317  */
318 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
319
320 /**
321  * acpigen_write_store() - Write a store operation
322  *
323  * @ctx: ACPI context pointer
324  */
325 void acpigen_write_store(struct acpi_ctx *ctx);
326
327 /**
328  * acpigen_write_debug_string() - Write a debug string
329  *
330  * This writes a debug operation with an associated string
331  *
332  * @ctx: ACPI context pointer
333  * @str: String to write
334  */
335 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
336
337 /**
338  * acpigen_write_or() - Write a bitwise OR operation
339  *
340  * res = arg1 | arg2
341  *
342  * @ctx: ACPI context pointer
343  * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
344  * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
345  * @res: ACPI opcode for result (e.g. LOCAL2_OP)
346  */
347 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
348
349 /**
350  * acpigen_write_and() - Write a bitwise AND operation
351  *
352  * res = arg1 & arg2
353  *
354  * @ctx: ACPI context pointer
355  * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
356  * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
357  * @res: ACPI opcode for result (e.g. LOCAL2_OP)
358  */
359 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
360
361 /**
362  * acpigen_write_not() - Write a bitwise NOT operation
363  *
364  * res = ~arg1
365  *
366  * @ctx: ACPI context pointer
367  * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
368  * @res: ACPI opcode for result (e.g. LOCAL2_OP)
369  */
370 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
371
372 /**
373  * acpigen_write_power_res() - Write a power resource
374  *
375  * Name (_PRx, Package(One) { name })
376  * ...
377  * PowerResource (name, level, order)
378  *
379  * The caller should fill in the rest of the power resource and then call
380  * acpigen_pop_len() to close it off
381  *
382  * @ctx: ACPI context pointer
383  * @name: Name of power resource (e.g. "PRIC")
384  * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
385  * @order: Order that this must be enabled/disabled (e.g. 0)
386  * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
387  * @dev_states_count: Number of dev states
388  */
389 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
390                              uint order, const char *const dev_states[],
391                              size_t dev_states_count);
392
393 /**
394  * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
395  *
396  * This emits code to either enable to disable a Tx GPIO. It takes account of
397  * the GPIO polarity.
398  *
399  * The code needs access to the DW0 register for the pad being used. This is
400  * provided by gpio->pin0_addr and ACPI methods must be defined for the board
401  * which can read and write the pad's DW0 register given this address:
402  *    @dw0_read: takes a single argument, the DW0 address
403  *               returns the DW0 value
404  *    @dw0:write: takes two arguments, the DW0 address and the value to write
405  *               no return value
406  *
407  * Example code (-- means comment):
408  *
409  *      -- Get Pad Configuration DW0 register value
410  *      Method (GPC0, 0x1, Serialized)
411  *      {
412  *              -- Arg0 - GPIO DW0 address
413  *              Store (Arg0, Local0)
414  *              OperationRegion (PDW0, SystemMemory, Local0, 4)
415  *              Field (PDW0, AnyAcc, NoLock, Preserve) {
416  *                      TEMP, 32
417  *              }
418  *              Return (TEMP)
419  *      }
420  *
421  *      -- Set Pad Configuration DW0 register value
422  *      Method (SPC0, 0x2, Serialized)
423  *      {
424  *              -- Arg0 - GPIO DW0 address
425  *              -- Arg1 - Value for DW0 register
426  *              Store (Arg0, Local0)
427  *              OperationRegion (PDW0, SystemMemory, Local0, 4)
428  *              Field (PDW0, AnyAcc, NoLock, Preserve) {
429  *                      TEMP,32
430  *              }
431  *              Store (Arg1, TEMP)
432  *      }
433  *
434  *
435  * @ctx: ACPI context pointer
436  * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
437  *      PAD_CFG0_TX_STATE
438  * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
439  * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
440  * @gpio: GPIO to change
441  * @enable: true to enable GPIO, false to disable
442  * Returns 0 on success, -ve on error.
443  */
444 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
445                                const char *dw0_read, const char *dw0_write,
446                                struct acpi_gpio *gpio, bool enable);
447
448 #endif