acpi: Add support for SSDT generation
[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 /**
31  * struct acpi_ctx - Context used for writing ACPI tables
32  *
33  * This contains a few useful pieces of information used when writing
34  *
35  * @base: Base address of ACPI tables
36  * @current: Current address for writing
37  * @rsdp: Pointer to the Root System Description Pointer, typically used when
38  *      adding a new table. The RSDP holds pointers to the RSDT and XSDT.
39  * @rsdt: Pointer to the Root System Description Table
40  * @xsdt: Pointer to the Extended System Description Table
41  * @len_stack: Stack of 'length' words to fix up later
42  * @ltop: Points to current top of stack (0 = empty)
43  */
44 struct acpi_ctx {
45         void *base;
46         void *current;
47         struct acpi_rsdp *rsdp;
48         struct acpi_rsdt *rsdt;
49         struct acpi_xsdt *xsdt;
50         char *len_stack[ACPIGEN_LENSTACK_SIZE];
51         int ltop;
52 };
53
54 /**
55  * struct acpi_ops - ACPI operations supported by driver model
56  */
57 struct acpi_ops {
58         /**
59          * get_name() - Obtain the ACPI name of a device
60          *
61          * @dev: Device to check
62          * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
63          *      bytes
64          * @return 0 if OK, -ENOENT if no name is available, other -ve value on
65          *      other error
66          */
67         int (*get_name)(const struct udevice *dev, char *out_name);
68
69         /**
70          * write_tables() - Write out any tables required by this device
71          *
72          * @dev: Device to write
73          * @ctx: ACPI context to use
74          * @return 0 if OK, -ve on error
75          */
76         int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx);
77
78         /**
79          * fill_ssdt() - Generate SSDT code for a device
80          *
81          * This is called to create the SSDT code. The method should write out
82          * whatever ACPI code is needed by this device. It will end up in the
83          * SSDT table.
84          *
85          * @dev: Device to write
86          * @ctx: ACPI context to use
87          * @return 0 if OK, -ve on error
88          */
89         int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
90 };
91
92 #define device_get_acpi_ops(dev)        ((dev)->driver->acpi_ops)
93
94 /**
95  * acpi_get_name() - Obtain the ACPI name of a device
96  *
97  * @dev: Device to check
98  * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX
99  *      bytes
100  * @return 0 if OK, -ENOENT if no name is available, other -ve value on
101  *      other error
102  */
103 int acpi_get_name(const struct udevice *dev, char *out_name);
104
105 /**
106  * acpi_copy_name() - Copy an ACPI name to an output buffer
107  *
108  * This convenience function can be used to return a literal string as a name
109  * in functions that implement the get_name() method.
110  *
111  * For example:
112  *
113  *      static int mydev_get_name(const struct udevice *dev, char *out_name)
114  *      {
115  *              return acpi_copy_name(out_name, "WIBB");
116  *      }
117  *
118  * @out_name: Place to put the name
119  * @name: Name to copy
120  * @return 0 (always)
121  */
122 int acpi_copy_name(char *out_name, const char *name);
123
124 /**
125  * acpi_write_dev_tables() - Write ACPI tables required by devices
126  *
127  * This scans through all devices and tells them to write any tables they want
128  * to write.
129  *
130  * @return 0 if OK, -ve if any device returned an error
131  */
132 int acpi_write_dev_tables(struct acpi_ctx *ctx);
133
134 /**
135  * acpi_fill_ssdt() - Generate ACPI tables for SSDT
136  *
137  * This is called to create the SSDT code for all devices.
138  *
139  * @ctx: ACPI context to use
140  * @return 0 if OK, -ve on error
141  */
142 int acpi_fill_ssdt(struct acpi_ctx *ctx);
143
144 #endif /* __ACPI__ */
145
146 #endif