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