blob: c4baeb6b8c100777b20fad3e515e47971fd1a316 [file] [log] [blame]
Simon Glass7ca28502020-04-09 10:27:38 -06001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Core ACPI (Advanced Configuration and Power Interface) support
4 *
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#ifndef __DM_ACPI_H__
10#define __DM_ACPI_H__
11
12/* Allow operations to be optional for ACPI */
13#if CONFIG_IS_ENABLED(ACPIGEN)
14#define ACPI_OPS_PTR(_ptr) .acpi_ops = _ptr,
15#else
16#define ACPI_OPS_PTR(_ptr)
17#endif
18
Simon Glass7e148f22020-07-07 13:11:50 -060019/* Length of an ACPI name string, excluding null terminator */
Simon Glass7ca28502020-04-09 10:27:38 -060020#define ACPI_NAME_LEN 4
21
22/* Length of an ACPI name string including nul terminator */
23#define ACPI_NAME_MAX (ACPI_NAME_LEN + 1)
24
Simon Glass7e148f22020-07-07 13:11:50 -060025/* Number of nested objects supported */
26#define ACPIGEN_LENSTACK_SIZE 10
27
Simon Glass89c27982020-04-08 16:57:37 -060028#if !defined(__ACPI__)
29
Simon Glass6afa63a2021-12-01 09:02:47 -070030#include <linker_lists.h>
31
Simon Glassb4e84332020-07-07 21:32:08 -060032struct nhlt;
Simon Glass401d1c42020-10-30 21:38:53 -060033struct udevice;
Simon Glassb4e84332020-07-07 21:32:08 -060034
Simon Glassa4f82082020-07-07 13:12:12 -060035/** enum acpi_dump_option - selects what ACPI information to dump */
36enum acpi_dump_option {
37 ACPI_DUMP_LIST, /* Just the list of items */
38 ACPI_DUMP_CONTENTS, /* Include the binary contents also */
39};
40
Simon Glass7ca28502020-04-09 10:27:38 -060041/**
Simon Glass93f7f822020-04-26 09:19:46 -060042 * struct acpi_ctx - Context used for writing ACPI tables
43 *
44 * This contains a few useful pieces of information used when writing
45 *
Simon Glass61cc9332020-07-07 13:11:42 -060046 * @base: Base address of ACPI tables
Simon Glass93f7f822020-04-26 09:19:46 -060047 * @current: Current address for writing
Simon Glassfb746fd2021-12-01 09:02:46 -070048 * @tab_start: Address of start of the table being written. This is set up
49 * before the writer or driver method is called. It must not be changed by the
50 * method
Simon Glass29b35112020-04-26 09:19:50 -060051 * @rsdp: Pointer to the Root System Description Pointer, typically used when
52 * adding a new table. The RSDP holds pointers to the RSDT and XSDT.
53 * @rsdt: Pointer to the Root System Description Table
Simon Glassb38309b2020-04-26 09:19:52 -060054 * @xsdt: Pointer to the Extended System Description Table
Simon Glassa53d38f2021-12-01 09:02:51 -070055 * @facs: Pointer to the Firmware ACPI Control Structure
Simon Glassb4e84332020-07-07 21:32:08 -060056 * @nhlt: Intel Non-High-Definition-Audio Link Table (NHLT) pointer, used to
57 * build up information that audio codecs need to provide in the NHLT ACPI
58 * table
Simon Glass7e148f22020-07-07 13:11:50 -060059 * @len_stack: Stack of 'length' words to fix up later
60 * @ltop: Points to current top of stack (0 = empty)
Simon Glass93f7f822020-04-26 09:19:46 -060061 */
62struct acpi_ctx {
Simon Glass61cc9332020-07-07 13:11:42 -060063 void *base;
Simon Glass93f7f822020-04-26 09:19:46 -060064 void *current;
Simon Glassfb746fd2021-12-01 09:02:46 -070065 void *tab_start;
Simon Glass29b35112020-04-26 09:19:50 -060066 struct acpi_rsdp *rsdp;
67 struct acpi_rsdt *rsdt;
Simon Glassb38309b2020-04-26 09:19:52 -060068 struct acpi_xsdt *xsdt;
Simon Glassa53d38f2021-12-01 09:02:51 -070069 struct acpi_facs *facs;
Simon Glassb4e84332020-07-07 21:32:08 -060070 struct nhlt *nhlt;
Simon Glass7e148f22020-07-07 13:11:50 -060071 char *len_stack[ACPIGEN_LENSTACK_SIZE];
72 int ltop;
Simon Glass93f7f822020-04-26 09:19:46 -060073};
74
75/**
Simon Glass6afa63a2021-12-01 09:02:47 -070076 * enum acpi_writer_flags_t - flags to use for the ACPI writers
Simon Glass94ba15a2021-12-01 09:02:50 -070077 *
78 * ACPIWF_ALIGN64 - align to 64 bytes after writing this one (default is 16)
Simon Glass6afa63a2021-12-01 09:02:47 -070079 */
80enum acpi_writer_flags_t {
Simon Glass94ba15a2021-12-01 09:02:50 -070081 ACPIWF_ALIGN64 = 1 << 0,
Simon Glass6afa63a2021-12-01 09:02:47 -070082};
83
84struct acpi_writer;
85
86/**
87 * acpi_writer_func() - Function that can write an ACPI table
88 *
89 * @ctx: ACPI context to use for writing
90 * @entry: Linker-list entry for this writer
91 * @return 0 if OK, -ve on error
92 */
93typedef int (*acpi_writer_func)(struct acpi_ctx *ctx,
94 const struct acpi_writer *entry);
95
96/**
97 * struct acpi_writer - an ACPI table that can be written
98 *
99 * @name: Name of the writer
100 * @table: Table name that is generated (e.g. "DSDT")
101 * @h_write: Writer function
102 */
103struct acpi_writer {
104 const char *name;
105 const char *table;
106 acpi_writer_func h_write;
107 int flags;
108};
109
Simon Glass94ba15a2021-12-01 09:02:50 -0700110/* Declare a new ACPI-table writer */
Simon Glass6afa63a2021-12-01 09:02:47 -0700111#define ACPI_WRITER(_name, _table, _write, _flags) \
112 ll_entry_declare(struct acpi_writer, _name, acpi_writer) = { \
113 .name = #_name, \
114 .table = _table, \
115 .h_write = _write, \
116 .flags = _flags, \
117 }
118
Simon Glass94ba15a2021-12-01 09:02:50 -0700119/* Get a pointer to a given ACPI-table writer */
120#define ACPI_WRITER_GET(_name) \
121 ll_entry_get(struct acpi_writer, _name, acpi_writer)
122
Simon Glass6afa63a2021-12-01 09:02:47 -0700123/**
Simon Glass7ca28502020-04-09 10:27:38 -0600124 * struct acpi_ops - ACPI operations supported by driver model
125 */
126struct acpi_ops {
127 /**
128 * get_name() - Obtain the ACPI name of a device
129 *
130 * @dev: Device to check
131 * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
132 * bytes
133 * @return 0 if OK, -ENOENT if no name is available, other -ve value on
134 * other error
135 */
136 int (*get_name)(const struct udevice *dev, char *out_name);
Simon Glass93f7f822020-04-26 09:19:46 -0600137
138 /**
139 * write_tables() - Write out any tables required by this device
140 *
141 * @dev: Device to write
142 * @ctx: ACPI context to use
143 * @return 0 if OK, -ve on error
144 */
145 int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx);
Simon Glassb5183172020-07-07 13:12:03 -0600146
147 /**
148 * fill_ssdt() - Generate SSDT code for a device
149 *
150 * This is called to create the SSDT code. The method should write out
151 * whatever ACPI code is needed by this device. It will end up in the
152 * SSDT table.
153 *
Simon Glass01694582020-07-07 13:12:08 -0600154 * Note that this is called 'fill' because the entire contents of the
155 * SSDT is build by calling this method on all devices.
156 *
Simon Glassb5183172020-07-07 13:12:03 -0600157 * @dev: Device to write
158 * @ctx: ACPI context to use
159 * @return 0 if OK, -ve on error
160 */
161 int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
Simon Glass01694582020-07-07 13:12:08 -0600162
163 /**
164 * inject_dsdt() - Generate DSDT code for a device
165 *
166 * This is called to create the DSDT code. The method should write out
167 * whatever ACPI code is needed by this device. It will end up in the
168 * DSDT table.
169 *
170 * Note that this is called 'inject' because the output of calling this
171 * method on all devices is injected into the DSDT, the bulk of which
172 * is written in .asl files for the board.
173 *
174 * @dev: Device to write
175 * @ctx: ACPI context to use
176 * @return 0 if OK, -ve on error
177 */
178 int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx);
Simon Glassb4e84332020-07-07 21:32:08 -0600179
180 /**
181 * setup_nhlt() - Set up audio information for this device
182 *
183 * The method can add information to ctx->nhlt if it likes
184 *
185 * @return 0 if OK, -ENODATA if nothing to add, -ve on error
186 */
187 int (*setup_nhlt)(const struct udevice *dev, struct acpi_ctx *ctx);
Simon Glass7ca28502020-04-09 10:27:38 -0600188};
189
190#define device_get_acpi_ops(dev) ((dev)->driver->acpi_ops)
191
192/**
193 * acpi_get_name() - Obtain the ACPI name of a device
194 *
195 * @dev: Device to check
196 * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
197 * bytes
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100198 * Return: 0 if OK, -ENOENT if no name is available, other -ve value on
Simon Glass7ca28502020-04-09 10:27:38 -0600199 * other error
200 */
201int acpi_get_name(const struct udevice *dev, char *out_name);
202
203/**
204 * acpi_copy_name() - Copy an ACPI name to an output buffer
205 *
206 * This convenience function can be used to return a literal string as a name
207 * in functions that implement the get_name() method.
208 *
209 * For example:
210 *
211 * static int mydev_get_name(const struct udevice *dev, char *out_name)
212 * {
213 * return acpi_copy_name(out_name, "WIBB");
214 * }
215 *
216 * @out_name: Place to put the name
217 * @name: Name to copy
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100218 * Return: 0 (always)
Simon Glass7ca28502020-04-09 10:27:38 -0600219 */
220int acpi_copy_name(char *out_name, const char *name);
221
Simon Glass93f7f822020-04-26 09:19:46 -0600222/**
223 * acpi_write_dev_tables() - Write ACPI tables required by devices
224 *
225 * This scans through all devices and tells them to write any tables they want
226 * to write.
227 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100228 * Return: 0 if OK, -ve if any device returned an error
Simon Glass93f7f822020-04-26 09:19:46 -0600229 */
230int acpi_write_dev_tables(struct acpi_ctx *ctx);
231
Simon Glassb5183172020-07-07 13:12:03 -0600232/**
233 * acpi_fill_ssdt() - Generate ACPI tables for SSDT
234 *
235 * This is called to create the SSDT code for all devices.
236 *
237 * @ctx: ACPI context to use
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100238 * Return: 0 if OK, -ve on error
Simon Glassb5183172020-07-07 13:12:03 -0600239 */
240int acpi_fill_ssdt(struct acpi_ctx *ctx);
241
Simon Glass01694582020-07-07 13:12:08 -0600242/**
243 * acpi_inject_dsdt() - Generate ACPI tables for DSDT
244 *
245 * This is called to create the DSDT code for all devices.
246 *
247 * @ctx: ACPI context to use
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100248 * Return: 0 if OK, -ve on error
Simon Glass01694582020-07-07 13:12:08 -0600249 */
250int acpi_inject_dsdt(struct acpi_ctx *ctx);
251
Simon Glassa4f82082020-07-07 13:12:12 -0600252/**
Simon Glassb4e84332020-07-07 21:32:08 -0600253 * acpi_setup_nhlt() - Set up audio information
254 *
255 * This is called to set up the nhlt information for all devices.
256 *
257 * @ctx: ACPI context to use
258 * @nhlt: Pointer to nhlt information to add to
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100259 * Return: 0 if OK, -ve on error
Simon Glassb4e84332020-07-07 21:32:08 -0600260 */
261int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt);
262
263/**
Simon Glassa4f82082020-07-07 13:12:12 -0600264 * acpi_dump_items() - Dump out the collected ACPI items
265 *
266 * This lists the ACPI DSDT and SSDT items generated by the various U-Boot
267 * drivers.
268 *
269 * @option: Sets what should be dumpyed
270 */
271void acpi_dump_items(enum acpi_dump_option option);
272
Simon Glassf1858952020-07-07 21:32:07 -0600273/**
274 * acpi_get_path() - Get the full ACPI path for a device
275 *
276 * This checks for any override in the device tree and calls acpi_device_path()
277 * if not
278 *
279 * @dev: Device to check
280 * @out_path: Buffer to place the path in (should be ACPI_PATH_MAX long)
281 * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100282 * Return: 0 if OK, -ve on error
Simon Glassf1858952020-07-07 21:32:07 -0600283 */
284int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen);
285
Simon Glass18434ae2020-11-04 09:57:33 -0700286/**
287 * acpi_reset_items() - Reset the list of ACPI items to empty
288 *
289 * This list keeps track of DSDT and SSDT items that are generated
290 * programmatically. The 'acpi items' command shows the list. Use this function
291 * to empty the list, before writing new items.
292 */
293void acpi_reset_items(void);
294
Simon Glass6afa63a2021-12-01 09:02:47 -0700295/**
296 * acpi_write_one() - Call a single ACPI writer entry
297 *
298 * This handles aligning the context afterwards, if the entry flags indicate
299 * that.
300 *
301 * @ctx: ACPI context to use
302 * @entry: Entry to call
303 * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve
304 * value on error
305 */
306int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry);
307
Simon Glasscc1f8c32021-12-01 09:02:48 -0700308/**
309 * acpi_setup_ctx() - Set up a new ACPI context
310 *
311 * This zeros the context and sets up the base and current pointers, ensuring
312 * that they are aligned. Then it writes the acpi_start and acpi_ctx values in
313 * global_data
314 *
315 * @ctx: ACPI context to set up
316 * @start: Start address for ACPI table
317 */
318void acpi_setup_ctx(struct acpi_ctx *ctx, ulong start);
319
Simon Glass94ba15a2021-12-01 09:02:50 -0700320/**
321 * acpi_write_one() - Call a single ACPI writer entry
322 *
323 * This handles aligning the context afterwards, if the entry flags indicate
324 * that.
325 *
326 * @ctx: ACPI context to use
327 * @entry: Entry to call
328 * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve
329 * value on error
330 */
331int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry);
332
Simon Glass89c27982020-04-08 16:57:37 -0600333#endif /* __ACPI__ */
334
Simon Glass7ca28502020-04-09 10:27:38 -0600335#endif