acpi: Add a linker list for ACPI tables
[platform/kernel/u-boot.git] / include / dm / acpi.h
1 /* 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
19 /* Length of an ACPI name string, excluding null terminator */
20 #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
25 /* Number of nested objects supported */
26 #define ACPIGEN_LENSTACK_SIZE 10
27
28 #if !defined(__ACPI__)
29
30 #include <linker_lists.h>
31
32 struct nhlt;
33 struct udevice;
34
35 /** enum acpi_dump_option - selects what ACPI information to dump */
36 enum acpi_dump_option {
37         ACPI_DUMP_LIST,         /* Just the list of items */
38         ACPI_DUMP_CONTENTS,     /* Include the binary contents also */
39 };
40
41 /**
42  * struct acpi_ctx - Context used for writing ACPI tables
43  *
44  * This contains a few useful pieces of information used when writing
45  *
46  * @base: Base address of ACPI tables
47  * @current: Current address for writing
48  * @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
51  * @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
54  * @xsdt: Pointer to the Extended System Description Table
55  * @nhlt: Intel Non-High-Definition-Audio Link Table (NHLT) pointer, used to
56  *      build up information that audio codecs need to provide in the NHLT ACPI
57  *      table
58  * @len_stack: Stack of 'length' words to fix up later
59  * @ltop: Points to current top of stack (0 = empty)
60  */
61 struct acpi_ctx {
62         void *base;
63         void *current;
64         void *tab_start;
65         struct acpi_rsdp *rsdp;
66         struct acpi_rsdt *rsdt;
67         struct acpi_xsdt *xsdt;
68         struct nhlt *nhlt;
69         char *len_stack[ACPIGEN_LENSTACK_SIZE];
70         int ltop;
71 };
72
73 /**
74  * enum acpi_writer_flags_t - flags to use for the ACPI writers
75  */
76 enum acpi_writer_flags_t {
77         ACPIWF_ALIGN64_,
78 };
79
80 struct acpi_writer;
81
82 /**
83  * acpi_writer_func() - Function that can write an ACPI table
84  *
85  * @ctx: ACPI context to use for writing
86  * @entry: Linker-list entry for this writer
87  * @return 0 if OK, -ve on error
88  */
89 typedef int (*acpi_writer_func)(struct acpi_ctx *ctx,
90                                 const struct acpi_writer *entry);
91
92 /**
93  * struct acpi_writer - an ACPI table that can be written
94  *
95  * @name: Name of the writer
96  * @table: Table name that is generated (e.g. "DSDT")
97  * @h_write: Writer function
98  */
99 struct acpi_writer {
100         const char *name;
101         const char *table;
102         acpi_writer_func h_write;
103         int flags;
104 };
105
106 /* Declare a new ACPI table writer */
107 #define ACPI_WRITER(_name, _table, _write, _flags)                                      \
108         ll_entry_declare(struct acpi_writer, _name, acpi_writer) = {    \
109                 .name = #_name,                                         \
110                 .table = _table,                                        \
111                 .h_write = _write,                                      \
112                 .flags = _flags,                                        \
113         }
114
115 /**
116  * struct acpi_ops - ACPI operations supported by driver model
117  */
118 struct acpi_ops {
119         /**
120          * get_name() - Obtain the ACPI name of a device
121          *
122          * @dev: Device to check
123          * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
124          *      bytes
125          * @return 0 if OK, -ENOENT if no name is available, other -ve value on
126          *      other error
127          */
128         int (*get_name)(const struct udevice *dev, char *out_name);
129
130         /**
131          * write_tables() - Write out any tables required by this device
132          *
133          * @dev: Device to write
134          * @ctx: ACPI context to use
135          * @return 0 if OK, -ve on error
136          */
137         int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx);
138
139         /**
140          * fill_ssdt() - Generate SSDT code for a device
141          *
142          * This is called to create the SSDT code. The method should write out
143          * whatever ACPI code is needed by this device. It will end up in the
144          * SSDT table.
145          *
146          * Note that this is called 'fill' because the entire contents of the
147          * SSDT is build by calling this method on all devices.
148          *
149          * @dev: Device to write
150          * @ctx: ACPI context to use
151          * @return 0 if OK, -ve on error
152          */
153         int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
154
155         /**
156          * inject_dsdt() - Generate DSDT code for a device
157          *
158          * This is called to create the DSDT code. The method should write out
159          * whatever ACPI code is needed by this device. It will end up in the
160          * DSDT table.
161          *
162          * Note that this is called 'inject' because the output of calling this
163          * method on all devices is injected into the DSDT, the bulk of which
164          * is written in .asl files for the board.
165          *
166          * @dev: Device to write
167          * @ctx: ACPI context to use
168          * @return 0 if OK, -ve on error
169          */
170         int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx);
171
172         /**
173          * setup_nhlt() - Set up audio information for this device
174          *
175          * The method can add information to ctx->nhlt if it likes
176          *
177          * @return 0 if OK, -ENODATA if nothing to add, -ve on error
178          */
179         int (*setup_nhlt)(const struct udevice *dev, struct acpi_ctx *ctx);
180 };
181
182 #define device_get_acpi_ops(dev)        ((dev)->driver->acpi_ops)
183
184 /**
185  * acpi_get_name() - Obtain the ACPI name of a device
186  *
187  * @dev: Device to check
188  * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
189  *      bytes
190  * Return: 0 if OK, -ENOENT if no name is available, other -ve value on
191  *      other error
192  */
193 int acpi_get_name(const struct udevice *dev, char *out_name);
194
195 /**
196  * acpi_copy_name() - Copy an ACPI name to an output buffer
197  *
198  * This convenience function can be used to return a literal string as a name
199  * in functions that implement the get_name() method.
200  *
201  * For example:
202  *
203  *      static int mydev_get_name(const struct udevice *dev, char *out_name)
204  *      {
205  *              return acpi_copy_name(out_name, "WIBB");
206  *      }
207  *
208  * @out_name: Place to put the name
209  * @name: Name to copy
210  * Return: 0 (always)
211  */
212 int acpi_copy_name(char *out_name, const char *name);
213
214 /**
215  * acpi_write_dev_tables() - Write ACPI tables required by devices
216  *
217  * This scans through all devices and tells them to write any tables they want
218  * to write.
219  *
220  * Return: 0 if OK, -ve if any device returned an error
221  */
222 int acpi_write_dev_tables(struct acpi_ctx *ctx);
223
224 /**
225  * acpi_fill_ssdt() - Generate ACPI tables for SSDT
226  *
227  * This is called to create the SSDT code for all devices.
228  *
229  * @ctx: ACPI context to use
230  * Return: 0 if OK, -ve on error
231  */
232 int acpi_fill_ssdt(struct acpi_ctx *ctx);
233
234 /**
235  * acpi_inject_dsdt() - Generate ACPI tables for DSDT
236  *
237  * This is called to create the DSDT code for all devices.
238  *
239  * @ctx: ACPI context to use
240  * Return: 0 if OK, -ve on error
241  */
242 int acpi_inject_dsdt(struct acpi_ctx *ctx);
243
244 /**
245  * acpi_setup_nhlt() - Set up audio information
246  *
247  * This is called to set up the nhlt information for all devices.
248  *
249  * @ctx: ACPI context to use
250  * @nhlt: Pointer to nhlt information to add to
251  * Return: 0 if OK, -ve on error
252  */
253 int acpi_setup_nhlt(struct acpi_ctx *ctx, struct nhlt *nhlt);
254
255 /**
256  * acpi_dump_items() - Dump out the collected ACPI items
257  *
258  * This lists the ACPI DSDT and SSDT items generated by the various U-Boot
259  * drivers.
260  *
261  * @option: Sets what should be dumpyed
262  */
263 void acpi_dump_items(enum acpi_dump_option option);
264
265 /**
266  * acpi_get_path() - Get the full ACPI path for a device
267  *
268  * This checks for any override in the device tree and calls acpi_device_path()
269  * if not
270  *
271  * @dev: Device to check
272  * @out_path: Buffer to place the path in (should be ACPI_PATH_MAX long)
273  * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
274  * Return: 0 if OK, -ve on error
275  */
276 int acpi_get_path(const struct udevice *dev, char *out_path, int maxlen);
277
278 /**
279  * acpi_reset_items() - Reset the list of ACPI items to empty
280  *
281  * This list keeps track of DSDT and SSDT items that are generated
282  * programmatically. The 'acpi items' command shows the list. Use this function
283  * to empty the list, before writing new items.
284  */
285 void acpi_reset_items(void);
286
287 /**
288  * acpi_write_one() - Call a single ACPI writer entry
289  *
290  * This handles aligning the context afterwards, if the entry flags indicate
291  * that.
292  *
293  * @ctx: ACPI context to use
294  * @entry: Entry to call
295  * @return 0 if OK, -ENOENT if this writer produced an empty entry, other -ve
296  * value on error
297  */
298 int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry);
299
300 #endif /* __ACPI__ */
301
302 #endif