blob: b10226f8f8d8db1f01e772c849c8c9854d9c6169 [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;
16
Simon Glass7e148f22020-07-07 13:11:50 -060017/* Top 4 bits of the value used to indicate a three-byte length value */
18#define ACPI_PKG_LEN_3_BYTES 0x80
19
Simon Glass03967ce2020-07-07 13:11:51 -060020/* ACPI Op/Prefix codes */
21enum {
Simon Glass83b2bd52020-07-07 13:11:52 -060022 ZERO_OP = 0x00,
23 ONE_OP = 0x01,
Simon Glass7aed90d2020-07-07 13:11:54 -060024 NAME_OP = 0x08,
Simon Glass83b2bd52020-07-07 13:11:52 -060025 BYTE_PREFIX = 0x0a,
26 WORD_PREFIX = 0x0b,
27 DWORD_PREFIX = 0x0c,
Simon Glass3df33bd2020-07-07 13:11:53 -060028 STRING_PREFIX = 0x0d,
Simon Glass83b2bd52020-07-07 13:11:52 -060029 QWORD_PREFIX = 0x0e,
Simon Glass29df8452020-07-07 13:11:55 -060030 BUFFER_OP = 0x11,
Simon Glass03967ce2020-07-07 13:11:51 -060031 PACKAGE_OP = 0x12,
Simon Glass7aed90d2020-07-07 13:11:54 -060032 DUAL_NAME_PREFIX = 0x2e,
33 MULTI_NAME_PREFIX = 0x2f,
Simon Glass0e5a0a02020-07-07 13:11:56 -060034 ROOT_PREFIX = 0x5c,
Simon Glass03967ce2020-07-07 13:11:51 -060035};
36
Simon Glass61cc9332020-07-07 13:11:42 -060037/**
38 * acpigen_get_current() - Get the current ACPI code output pointer
39 *
40 * @ctx: ACPI context pointer
41 * @return output pointer
42 */
43u8 *acpigen_get_current(struct acpi_ctx *ctx);
44
45/**
46 * acpigen_emit_byte() - Emit a byte to the ACPI code
47 *
48 * @ctx: ACPI context pointer
49 * @data: Value to output
50 */
51void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
52
53/**
54 * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
55 *
56 * @ctx: ACPI context pointer
57 * @data: Value to output
58 */
59void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
60
61/**
62 * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
63 *
64 * @ctx: ACPI context pointer
65 * @data: Value to output
66 */
67void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
68
Simon Glass7fb8da42020-07-07 13:11:45 -060069/**
70 * acpigen_emit_stream() - Emit a stream of bytes
71 *
72 * @ctx: ACPI context pointer
73 * @data: Data to output
74 * @size: Size of data in bytes
75 */
76void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
77
78/**
79 * acpigen_emit_string() - Emit a string
80 *
81 * Emit a string with a null terminator
82 *
83 * @ctx: ACPI context pointer
84 * @str: String to output, or NULL for an empty string
85 */
86void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
87
Simon Glass7e148f22020-07-07 13:11:50 -060088/**
89 * acpigen_write_len_f() - Write a 'forward' length placeholder
90 *
91 * This adds space for a length value in the ACPI stream and pushes the current
92 * position (before the length) on the stack. After calling this you can write
93 * some data and then call acpigen_pop_len() to update the length value.
94 *
95 * Usage:
96 *
97 * acpigen_write_len_f() ------\
98 * acpigen_write...() |
99 * acpigen_write...() |
100 * acpigen_write_len_f() --\ |
101 * acpigen_write...() | |
102 * acpigen_write...() | |
103 * acpigen_pop_len() ------/ |
104 * acpigen_write...() |
105 * acpigen_pop_len() ----------/
106 *
107 * See ACPI 6.3 section 20.2.4 Package Length Encoding
108 *
109 * This implementation always uses a 3-byte packet length for simplicity. It
110 * could be adjusted to support other lengths.
111 *
112 * @ctx: ACPI context pointer
113 */
114void acpigen_write_len_f(struct acpi_ctx *ctx);
115
116/**
117 * acpigen_pop_len() - Update the previously stacked length placeholder
118 *
119 * Call this after the data for the block has been written. It updates the
120 * top length value in the stack and pops it off.
121 *
122 * @ctx: ACPI context pointer
123 */
124void acpigen_pop_len(struct acpi_ctx *ctx);
125
Simon Glass03967ce2020-07-07 13:11:51 -0600126/**
127 * acpigen_write_package() - Start writing a package
128 *
129 * A package collects together a number of elements in the ACPI code. To write
130 * a package use:
131 *
132 * acpigen_write_package(ctx, 3);
133 * ...write things
134 * acpigen_pop_len()
135 *
136 * If you don't know the number of elements in advance, acpigen_write_package()
137 * returns a pointer to the value so you can update it later:
138 *
139 * char *num_elements = acpigen_write_package(ctx, 0);
140 * ...write things
141 * *num_elements += 1;
142 * ...write things
143 * *num_elements += 1;
144 * acpigen_pop_len()
145 *
146 * @ctx: ACPI context pointer
147 * @nr_el: Number of elements (0 if not known)
148 * @returns pointer to the number of elements, which can be updated by the
149 * caller if needed
150 */
151char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
152
Simon Glass83b2bd52020-07-07 13:11:52 -0600153/**
154 * acpigen_write_integer() - Write an integer
155 *
156 * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
157 * the integer size) and an integer value. Note that WORD means 16 bits in ACPI.
158 *
159 * @ctx: ACPI context pointer
160 * @data: Integer to write
161 */
162void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
163
Simon Glass3df33bd2020-07-07 13:11:53 -0600164/**
165 * acpigen_write_string() - Write a string
166 *
167 * This writes a STRING_PREFIX followed by a null-terminated string
168 *
169 * @ctx: ACPI context pointer
170 * @str: String to write
171 */
172void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
Simon Glass7aed90d2020-07-07 13:11:54 -0600173
174/**
175 * acpigen_emit_namestring() - Emit an ACPI name
176 *
177 * This writes out an ACPI name or path in the required special format. It does
178 * not add the NAME_OP prefix.
179 *
180 * @ctx: ACPI context pointer
181 * @namepath: Name / path to emit
182 */
183void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
184
185/**
186 * acpigen_write_name() - Write out an ACPI name
187 *
188 * This writes out an ACPI name or path in the required special format with a
189 * NAME_OP prefix.
190 *
191 * @ctx: ACPI context pointer
192 * @namepath: Name / path to emit
193 */
194void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
Simon Glass29df8452020-07-07 13:11:55 -0600195
196/**
197 * acpigen_write_uuid() - Write a UUID
198 *
199 * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
200 *
201 * @ctx: ACPI context pointer
202 * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
203 * @return 0 if OK, -EINVAL if the format is incorrect
204 */
205int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
206
Simon Glass61cc9332020-07-07 13:11:42 -0600207#endif