blob: d06d2c0c7313507cc0e62e8e7e31da9d8d5fe0fc [file] [log] [blame]
Simon Glass61cc9332020-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 Glassf8054dd2020-07-07 13:12:01 -060016struct acpi_gpio;
Simon Glass61cc9332020-07-07 13:11:42 -060017
Simon Glass7e148f22020-07-07 13:11:50 -060018/* Top 4 bits of the value used to indicate a three-byte length value */
19#define ACPI_PKG_LEN_3_BYTES 0x80
20
Simon Glass9c70e7e2020-07-07 13:11:59 -060021#define ACPI_METHOD_NARGS_MASK 0x7
22#define ACPI_METHOD_SERIALIZED_MASK BIT(3)
23
Simon Glass03967ce2020-07-07 13:11:51 -060024/* ACPI Op/Prefix codes */
25enum {
Simon Glass83b2bd52020-07-07 13:11:52 -060026 ZERO_OP = 0x00,
27 ONE_OP = 0x01,
Simon Glass7aed90d2020-07-07 13:11:54 -060028 NAME_OP = 0x08,
Simon Glass83b2bd52020-07-07 13:11:52 -060029 BYTE_PREFIX = 0x0a,
30 WORD_PREFIX = 0x0b,
31 DWORD_PREFIX = 0x0c,
Simon Glass3df33bd2020-07-07 13:11:53 -060032 STRING_PREFIX = 0x0d,
Simon Glass83b2bd52020-07-07 13:11:52 -060033 QWORD_PREFIX = 0x0e,
Simon Glass29df8452020-07-07 13:11:55 -060034 BUFFER_OP = 0x11,
Simon Glass03967ce2020-07-07 13:11:51 -060035 PACKAGE_OP = 0x12,
Simon Glass9c70e7e2020-07-07 13:11:59 -060036 METHOD_OP = 0x14,
37 SLEEP_OP = 0x22,
Simon Glass7aed90d2020-07-07 13:11:54 -060038 DUAL_NAME_PREFIX = 0x2e,
39 MULTI_NAME_PREFIX = 0x2f,
Simon Glass9c70e7e2020-07-07 13:11:59 -060040 DEBUG_OP = 0x31,
41 EXT_OP_PREFIX = 0x5b,
Simon Glass0e5a0a02020-07-07 13:11:56 -060042 ROOT_PREFIX = 0x5c,
Simon Glass9c70e7e2020-07-07 13:11:59 -060043 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,
Simon Glassf9189d52020-07-07 13:12:00 -060055 POWER_RES_OP = 0x84,
Simon Glass9c70e7e2020-07-07 13:11:59 -060056 RETURN_OP = 0xa4,
Simon Glass03967ce2020-07-07 13:11:51 -060057};
58
Simon Glass61cc9332020-07-07 13:11:42 -060059/**
60 * acpigen_get_current() - Get the current ACPI code output pointer
61 *
62 * @ctx: ACPI context pointer
63 * @return output pointer
64 */
65u8 *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 */
73void 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 */
81void 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 */
89void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
90
Simon Glass7fb8da42020-07-07 13:11:45 -060091/**
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 */
98void 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 */
108void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
109
Simon Glass7e148f22020-07-07 13:11:50 -0600110/**
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 */
136void 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 */
146void acpigen_pop_len(struct acpi_ctx *ctx);
147
Simon Glass03967ce2020-07-07 13:11:51 -0600148/**
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 */
173char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
174
Simon Glass83b2bd52020-07-07 13:11:52 -0600175/**
176 * acpigen_write_integer() - Write an integer
177 *
178 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
179 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
180 *
181 * @ctx: ACPI context pointer
182 * @data: Integer to write
183 */
184void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
185
Simon Glass3df33bd2020-07-07 13:11:53 -0600186/**
187 * acpigen_write_string() - Write a string
188 *
189 * This writes a STRING_PREFIX followed by a null-terminated string
190 *
191 * @ctx: ACPI context pointer
192 * @str: String to write
193 */
194void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
Simon Glass7aed90d2020-07-07 13:11:54 -0600195
196/**
197 * acpigen_emit_namestring() - Emit an ACPI name
198 *
199 * This writes out an ACPI name or path in the required special format. It does
200 * not add the NAME_OP prefix.
201 *
202 * @ctx: ACPI context pointer
203 * @namepath: Name / path to emit
204 */
205void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
206
207/**
208 * acpigen_write_name() - Write out an ACPI name
209 *
210 * This writes out an ACPI name or path in the required special format with a
211 * NAME_OP prefix.
212 *
213 * @ctx: ACPI context pointer
214 * @namepath: Name / path to emit
215 */
216void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
Simon Glass29df8452020-07-07 13:11:55 -0600217
218/**
219 * acpigen_write_uuid() - Write a UUID
220 *
221 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
222 *
223 * @ctx: ACPI context pointer
224 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
225 * @return 0 if OK, -EINVAL if the format is incorrect
226 */
227int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
228
Simon Glass9c70e7e2020-07-07 13:11:59 -0600229/**
230 * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
231 *
232 * @ctx: ACPI context pointer
233 * @op: Operation code (e.g. SLEEP_OP)
234 */
235void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
236
237/**
238 * acpigen_write_method() - Write a method header
239 *
240 * @ctx: ACPI context pointer
241 * @name: Method name (4 characters)
242 * @nargs: Number of method arguments (0 if none)
243 */
244void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
245
246/**
247 * acpigen_write_method_serialized() - Write a method header
248 *
249 * This sets the 'serialized' flag so that the method is thread-safe
250 *
251 * @ctx: ACPI context pointer
252 * @name: Method name (4 characters)
253 * @nargs: Number of method arguments (0 if none)
254 */
255void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
256 int nargs);
257
258/**
259 * acpigen_write_sta() - Write a _STA method
260 *
261 * @ctx: ACPI context pointer
262 * @status: Status value to return
263 */
264void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
265
266/**
267 * acpigen_write_sleep() - Write a sleep operation
268 *
269 * @ctx: ACPI context pointer
270 * @sleep_ms: Number of milliseconds to sleep for
271 */
272void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
273
274/**
275 * acpigen_write_store() - Write a store operation
276 *
277 * @ctx: ACPI context pointer
278 */
279void acpigen_write_store(struct acpi_ctx *ctx);
280
281/**
282 * acpigen_write_debug_string() - Write a debug string
283 *
284 * This writes a debug operation with an associated string
285 *
286 * @ctx: ACPI context pointer
287 * @str: String to write
288 */
289void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
290
291/**
292 * acpigen_write_or() - Write a bitwise OR operation
293 *
294 * res = arg1 | arg2
295 *
296 * @ctx: ACPI context pointer
297 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
298 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
299 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
300 */
301void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
302
303/**
304 * acpigen_write_and() - Write a bitwise AND operation
305 *
306 * res = arg1 & arg2
307 *
308 * @ctx: ACPI context pointer
309 * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
310 * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
311 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
312 */
313void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
314
315/**
316 * acpigen_write_not() - Write a bitwise NOT operation
317 *
318 * res = ~arg1
319 *
320 * @ctx: ACPI context pointer
321 * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
322 * @res: ACPI opcode for result (e.g. LOCAL2_OP)
323 */
324void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
325
Simon Glassf9189d52020-07-07 13:12:00 -0600326/**
327 * acpigen_write_power_res() - Write a power resource
328 *
329 * Name (_PRx, Package(One) { name })
330 * ...
331 * PowerResource (name, level, order)
332 *
333 * The caller should fill in the rest of the power resource and then call
334 * acpigen_pop_len() to close it off
335 *
336 * @ctx: ACPI context pointer
337 * @name: Name of power resource (e.g. "PRIC")
338 * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
339 * @order: Order that this must be enabled/disabled (e.g. 0)
340 * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
341 * @dev_states_count: Number of dev states
342 */
343void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
344 uint order, const char *const dev_states[],
345 size_t dev_states_count);
346
Simon Glassf8054dd2020-07-07 13:12:01 -0600347/**
348 * acpigen_set_enable_tx_gpio() - Emit ACPI code to enable/disable a GPIO
349 *
350 * This emits code to either enable to disable a Tx GPIO. It takes account of
351 * the GPIO polarity.
352 *
353 * The code needs access to the DW0 register for the pad being used. This is
354 * provided by gpio->pin0_addr and ACPI methods must be defined for the board
355 * which can read and write the pad's DW0 register given this address:
356 * @dw0_read: takes a single argument, the DW0 address
357 * returns the DW0 value
358 * @dw0:write: takes two arguments, the DW0 address and the value to write
359 * no return value
360 *
361 * Example code (-- means comment):
362 *
363 * -- Get Pad Configuration DW0 register value
364 * Method (GPC0, 0x1, Serialized)
365 * {
366 * -- Arg0 - GPIO DW0 address
367 * Store (Arg0, Local0)
368 * OperationRegion (PDW0, SystemMemory, Local0, 4)
369 * Field (PDW0, AnyAcc, NoLock, Preserve) {
370 * TEMP, 32
371 * }
372 * Return (TEMP)
373 * }
374 *
375 * -- Set Pad Configuration DW0 register value
376 * Method (SPC0, 0x2, Serialized)
377 * {
378 * -- Arg0 - GPIO DW0 address
379 * -- Arg1 - Value for DW0 register
380 * Store (Arg0, Local0)
381 * OperationRegion (PDW0, SystemMemory, Local0, 4)
382 * Field (PDW0, AnyAcc, NoLock, Preserve) {
383 * TEMP,32
384 * }
385 * Store (Arg1, TEMP)
386 * }
387 *
388 *
389 * @ctx: ACPI context pointer
390 * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
391 * PAD_CFG0_TX_STATE
392 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
393 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
394 * @gpio: GPIO to change
395 * @enable: true to enable GPIO, false to disable
396 * Returns 0 on success, -ve on error.
397 */
398int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
399 const char *dw0_read, const char *dw0_write,
400 struct acpi_gpio *gpio, bool enable);
401
Simon Glass61cc9332020-07-07 13:11:42 -0600402#endif