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