Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 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 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 19 | /* Length of an ACPI name string, excluding null terminator */ |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 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 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 25 | /* Number of nested objects supported */ |
| 26 | #define ACPIGEN_LENSTACK_SIZE 10 |
| 27 | |
Simon Glass | 4969d21 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 28 | #if !defined(__ACPI__) |
| 29 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 30 | /** |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 31 | * struct acpi_ctx - Context used for writing ACPI tables |
| 32 | * |
| 33 | * This contains a few useful pieces of information used when writing |
| 34 | * |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 35 | * @base: Base address of ACPI tables |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 36 | * @current: Current address for writing |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 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 |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 40 | * @xsdt: Pointer to the Extended System Description Table |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 41 | * @len_stack: Stack of 'length' words to fix up later |
| 42 | * @ltop: Points to current top of stack (0 = empty) |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 43 | */ |
| 44 | struct acpi_ctx { |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 45 | void *base; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 46 | void *current; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 47 | struct acpi_rsdp *rsdp; |
| 48 | struct acpi_rsdt *rsdt; |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 49 | struct acpi_xsdt *xsdt; |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 50 | char *len_stack[ACPIGEN_LENSTACK_SIZE]; |
| 51 | int ltop; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /** |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 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); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 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); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 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 | * |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame^] | 85 | * Note that this is called 'fill' because the entire contents of the |
| 86 | * SSDT is build by calling this method on all devices. |
| 87 | * |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 88 | * @dev: Device to write |
| 89 | * @ctx: ACPI context to use |
| 90 | * @return 0 if OK, -ve on error |
| 91 | */ |
| 92 | int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame^] | 93 | |
| 94 | /** |
| 95 | * inject_dsdt() - Generate DSDT code for a device |
| 96 | * |
| 97 | * This is called to create the DSDT code. The method should write out |
| 98 | * whatever ACPI code is needed by this device. It will end up in the |
| 99 | * DSDT table. |
| 100 | * |
| 101 | * Note that this is called 'inject' because the output of calling this |
| 102 | * method on all devices is injected into the DSDT, the bulk of which |
| 103 | * is written in .asl files for the board. |
| 104 | * |
| 105 | * @dev: Device to write |
| 106 | * @ctx: ACPI context to use |
| 107 | * @return 0 if OK, -ve on error |
| 108 | */ |
| 109 | int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx); |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #define device_get_acpi_ops(dev) ((dev)->driver->acpi_ops) |
| 113 | |
| 114 | /** |
| 115 | * acpi_get_name() - Obtain the ACPI name of a device |
| 116 | * |
| 117 | * @dev: Device to check |
| 118 | * @out_name: Place to put the name, must hold at least ACPI_NAME_MAX |
| 119 | * bytes |
| 120 | * @return 0 if OK, -ENOENT if no name is available, other -ve value on |
| 121 | * other error |
| 122 | */ |
| 123 | int acpi_get_name(const struct udevice *dev, char *out_name); |
| 124 | |
| 125 | /** |
| 126 | * acpi_copy_name() - Copy an ACPI name to an output buffer |
| 127 | * |
| 128 | * This convenience function can be used to return a literal string as a name |
| 129 | * in functions that implement the get_name() method. |
| 130 | * |
| 131 | * For example: |
| 132 | * |
| 133 | * static int mydev_get_name(const struct udevice *dev, char *out_name) |
| 134 | * { |
| 135 | * return acpi_copy_name(out_name, "WIBB"); |
| 136 | * } |
| 137 | * |
| 138 | * @out_name: Place to put the name |
| 139 | * @name: Name to copy |
| 140 | * @return 0 (always) |
| 141 | */ |
| 142 | int acpi_copy_name(char *out_name, const char *name); |
| 143 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 144 | /** |
| 145 | * acpi_write_dev_tables() - Write ACPI tables required by devices |
| 146 | * |
| 147 | * This scans through all devices and tells them to write any tables they want |
| 148 | * to write. |
| 149 | * |
| 150 | * @return 0 if OK, -ve if any device returned an error |
| 151 | */ |
| 152 | int acpi_write_dev_tables(struct acpi_ctx *ctx); |
| 153 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 154 | /** |
| 155 | * acpi_fill_ssdt() - Generate ACPI tables for SSDT |
| 156 | * |
| 157 | * This is called to create the SSDT code for all devices. |
| 158 | * |
| 159 | * @ctx: ACPI context to use |
| 160 | * @return 0 if OK, -ve on error |
| 161 | */ |
| 162 | int acpi_fill_ssdt(struct acpi_ctx *ctx); |
| 163 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame^] | 164 | /** |
| 165 | * acpi_inject_dsdt() - Generate ACPI tables for DSDT |
| 166 | * |
| 167 | * This is called to create the DSDT code for all devices. |
| 168 | * |
| 169 | * @ctx: ACPI context to use |
| 170 | * @return 0 if OK, -ve on error |
| 171 | */ |
| 172 | int acpi_inject_dsdt(struct acpi_ctx *ctx); |
| 173 | |
Simon Glass | 4969d21 | 2020-04-08 16:57:37 -0600 | [diff] [blame] | 174 | #endif /* __ACPI__ */ |
| 175 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 176 | #endif |