Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
Elyes Haouas | 8ed5835 | 2022-10-22 22:17:28 +0200 | [diff] [blame] | 3 | #include <acpi/acpi.h> |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 4 | #include <acpi/acpigen.h> |
Tim Wawrzynczak | 3573038 | 2021-09-14 11:54:47 -0600 | [diff] [blame] | 5 | #include <assert.h> |
Elyes Haouas | 8ed5835 | 2022-10-22 22:17:28 +0200 | [diff] [blame] | 6 | #include <commonlib/bsd/helpers.h> |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 7 | #include <console/console.h> |
| 8 | #include <intelblocks/acpi.h> |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 9 | #include <intelblocks/pmc_ipc.h> |
| 10 | #include <stdlib.h> |
Elyes Haouas | 8ed5835 | 2022-10-22 22:17:28 +0200 | [diff] [blame] | 11 | #include <string.h> |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 12 | #include <types.h> |
| 13 | |
| 14 | #define LPI_S0_HELPER_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66" |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 15 | #define PEP_S0IX_UUID "57a6512e-3979-4e9d-9708-ff13b2508972" |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 16 | #define SYSTEM_POWER_MANAGEMENT_HID "INT33A1" |
| 17 | #define SYSTEM_POWER_MANAGEMENT_CID "PNP0D80" |
| 18 | #define EC_S0IX_HOOK "\\_SB.PCI0.LPCB.EC0.S0IX" |
Michael Niewöhner | a972e23 | 2022-05-18 22:27:13 +0200 | [diff] [blame] | 19 | #define EC_DISPLAY_HOOK "\\_SB.PCI0.LPCB.EC0.EDSX" |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 20 | #define MAINBOARD_HOOK "\\_SB.MS0X" |
Michał Żygowski | 2b87b50 | 2022-02-22 12:36:32 +0100 | [diff] [blame] | 21 | #define MAINBOARD_DISPLAY_HOOK "\\_SB.MDSX" |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 22 | #define ENABLE_PM_BITS_HOOK "\\_SB.PCI0.EGPM" |
| 23 | #define RESTORE_PM_BITS_HOOK "\\_SB.PCI0.RGPM" |
Michał Kopeć | 86221c6 | 2022-05-02 10:24:41 +0200 | [diff] [blame] | 24 | #define THUNDERBOLT_DEVICE "\\_SB.PCI0.TXHC" |
| 25 | #define THUNDERBOLT_IOM_DPOF "\\_SB.PCI0.DPOF" |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 26 | #define LPI_STATES_ALL 0xff |
| 27 | #define MIN_DEVICE_STATE ACPI_DEVICE_SLEEP_D0 |
| 28 | #define PEPD_SCOPE "\\_SB.PCI0" |
| 29 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 30 | struct reg_info { |
| 31 | uint8_t *addr; |
| 32 | size_t buffer_size; |
| 33 | }; |
| 34 | |
| 35 | static void read_pmc_lpm_requirements(const struct soc_pmc_lpm *lpm, |
| 36 | struct reg_info *info) |
| 37 | { |
Tim Wawrzynczak | 3573038 | 2021-09-14 11:54:47 -0600 | [diff] [blame] | 38 | assert(info); |
| 39 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 40 | if (!CONFIG(SOC_INTEL_COMMON_BLOCK_ACPI_PEP_LPM_REQ) || !lpm) { |
| 41 | memset(info, 0, sizeof(*info)); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | const size_t register_count = lpm->num_substates * lpm->num_req_regs; |
| 46 | uint32_t *reg = calloc(register_count, sizeof(uint32_t)); |
| 47 | |
| 48 | /* Read the various LPM state requirement registers from the PMC */ |
| 49 | for (size_t i = 0; i < lpm->num_substates; i++) { |
| 50 | if (!(lpm->lpm_enable_mask & BIT(i))) |
| 51 | continue; |
| 52 | |
| 53 | for (size_t j = 0; j < lpm->num_req_regs; j++) { |
| 54 | const uint32_t offset = lpm->lpm_ipc_offset + |
| 55 | i * lpm->req_reg_stride + |
| 56 | j * sizeof(uint32_t); |
Ethan Tsao | eaf71b0 | 2021-10-13 12:37:05 -0700 | [diff] [blame] | 57 | const uint32_t cmd_reg = pmc_make_ipc_cmd(PMC_IPC_CMD_RD_PMC_REG, |
| 58 | PMC_IPC_CMD_SUBCMD_RD_PMC_REG, 0); |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 59 | struct pmc_ipc_buffer req = {.buf[0] = offset}; |
| 60 | struct pmc_ipc_buffer res = {}; |
| 61 | |
| 62 | enum cb_err result = pmc_send_ipc_cmd(cmd_reg, &req, &res); |
| 63 | if (result != CB_SUCCESS) { |
| 64 | printk(BIOS_ERR, "Failed to retrieve LPM substate registers" |
Paul Menzel | 5ca0015 | 2021-11-09 21:56:09 +0100 | [diff] [blame] | 65 | "from LPM, substate %zu, reg %zu\n", i, j); |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | uint32_t *ptr = reg + i * lpm->num_req_regs + j; |
| 69 | *ptr = res.buf[0]; |
| 70 | } |
| 71 | } |
| 72 | |
Tim Wawrzynczak | 3573038 | 2021-09-14 11:54:47 -0600 | [diff] [blame] | 73 | info->addr = (uint8_t *)reg; |
| 74 | info->buffer_size = register_count * sizeof(uint32_t); |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 77 | /* |
Tim Wawrzynczak | 7a94ad7 | 2022-02-11 09:46:15 -0700 | [diff] [blame] | 78 | * Windows expects a non-empty package for this subfunction, otherwise it |
| 79 | * results in a bluescreen (`INTERNAL_POWER_ERROR`); returning an empty package |
| 80 | * does not work. To workaround this, return a package describing a single |
| 81 | * device, one that is known to exist, i.e. ACPI_CPU_STRING. expects at least |
| 82 | * one device and crashes without it with a bluescreen. |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 83 | */ |
Tarun Tuli | c66ea98 | 2022-05-03 20:35:47 +0000 | [diff] [blame] | 84 | __weak void soc_lpi_get_constraints(void *unused) |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 85 | { |
Tim Wawrzynczak | 7a94ad7 | 2022-02-11 09:46:15 -0700 | [diff] [blame] | 86 | char path[16]; |
Tarun Tuli | c66ea98 | 2022-05-03 20:35:47 +0000 | [diff] [blame] | 87 | printk(BIOS_INFO, "Returning default LPI constraint package\n"); |
Tim Wawrzynczak | 7a94ad7 | 2022-02-11 09:46:15 -0700 | [diff] [blame] | 88 | |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 89 | /* |
| 90 | * Return (Package() { |
Tim Wawrzynczak | 7a94ad7 | 2022-02-11 09:46:15 -0700 | [diff] [blame] | 91 | * Package() { "\_SB.CP00", 0, |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 92 | * Package() { 0, |
| 93 | * Package() { 0xff, 0 }}}}) |
| 94 | */ |
| 95 | acpigen_emit_byte(RETURN_OP); |
| 96 | acpigen_write_package(1); |
| 97 | { |
| 98 | acpigen_write_package(3); |
| 99 | { |
Tim Wawrzynczak | 7a94ad7 | 2022-02-11 09:46:15 -0700 | [diff] [blame] | 100 | snprintf(path, sizeof(path), CONFIG_ACPI_CPU_STRING, 0); |
| 101 | acpigen_emit_namestring(path); |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 102 | acpigen_write_integer(0); /* device disabled */ |
| 103 | acpigen_write_package(2); |
| 104 | { |
| 105 | acpigen_write_integer(0); /* revision */ |
| 106 | acpigen_write_package(2); |
| 107 | { |
| 108 | acpigen_write_integer(LPI_STATES_ALL); |
| 109 | acpigen_write_integer(MIN_DEVICE_STATE); |
| 110 | } |
| 111 | acpigen_write_package_end(); |
| 112 | } |
| 113 | acpigen_write_package_end(); |
| 114 | } |
| 115 | acpigen_write_package_end(); |
| 116 | } |
| 117 | acpigen_write_package_end(); |
| 118 | } |
| 119 | |
| 120 | static void lpi_s0ix_entry(void *unused) |
| 121 | { |
| 122 | /* Inform the EC */ |
| 123 | acpigen_write_if_cond_ref_of(EC_S0IX_HOOK); |
| 124 | acpigen_emit_namestring(EC_S0IX_HOOK); |
| 125 | acpigen_write_integer(1); |
| 126 | acpigen_write_if_end(); |
| 127 | |
| 128 | /* Provide a board level S0ix hook */ |
| 129 | acpigen_write_if_cond_ref_of(MAINBOARD_HOOK); |
| 130 | acpigen_emit_namestring(MAINBOARD_HOOK); |
| 131 | acpigen_write_integer(1); |
| 132 | acpigen_write_if_end(); |
| 133 | |
| 134 | /* Save the current PM bits then enable GPIO PM with |
| 135 | MISCCFG_GPIO_PM_CONFIG_BITS */ |
| 136 | acpigen_write_if_cond_ref_of(ENABLE_PM_BITS_HOOK); |
| 137 | acpigen_emit_namestring(ENABLE_PM_BITS_HOOK); |
| 138 | acpigen_write_if_end(); |
Michał Kopeć | 86221c6 | 2022-05-02 10:24:41 +0200 | [diff] [blame] | 139 | |
| 140 | /* Handle Thunderbolt displays */ |
| 141 | acpigen_write_if_cond_ref_of(THUNDERBOLT_DEVICE); |
| 142 | acpigen_write_store_int_to_namestr(1, THUNDERBOLT_IOM_DPOF); |
| 143 | acpigen_write_if_end(); |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void lpi_s0ix_exit(void *unused) |
| 147 | { |
| 148 | /* Inform the EC */ |
| 149 | acpigen_write_if_cond_ref_of(EC_S0IX_HOOK); |
| 150 | acpigen_emit_namestring(EC_S0IX_HOOK); |
| 151 | acpigen_write_integer(0); |
| 152 | acpigen_write_if_end(); |
| 153 | |
| 154 | /* Provide a board level S0ix hook */ |
| 155 | acpigen_write_if_cond_ref_of(MAINBOARD_HOOK); |
| 156 | acpigen_emit_namestring(MAINBOARD_HOOK); |
| 157 | acpigen_write_integer(0); |
| 158 | acpigen_write_if_end(); |
| 159 | |
| 160 | /* Restore GPIO all Community PM */ |
| 161 | acpigen_write_if_cond_ref_of(RESTORE_PM_BITS_HOOK); |
| 162 | acpigen_emit_namestring(RESTORE_PM_BITS_HOOK); |
| 163 | acpigen_write_if_end(); |
Michał Kopeć | 86221c6 | 2022-05-02 10:24:41 +0200 | [diff] [blame] | 164 | |
| 165 | /* Handle Thunderbolt displays */ |
| 166 | acpigen_write_if_cond_ref_of(THUNDERBOLT_DEVICE); |
| 167 | acpigen_write_store_int_to_namestr(0, THUNDERBOLT_IOM_DPOF); |
| 168 | acpigen_write_if_end(); |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 169 | } |
| 170 | |
Michał Żygowski | 2b87b50 | 2022-02-22 12:36:32 +0100 | [diff] [blame] | 171 | static void lpi_display_on(void *unused) |
| 172 | { |
Michael Niewöhner | a972e23 | 2022-05-18 22:27:13 +0200 | [diff] [blame] | 173 | /* Inform the EC */ |
| 174 | acpigen_write_if_cond_ref_of(EC_DISPLAY_HOOK); |
| 175 | acpigen_emit_namestring(EC_DISPLAY_HOOK); |
| 176 | acpigen_write_integer(1); |
| 177 | acpigen_write_if_end(); |
| 178 | |
Michał Żygowski | 2b87b50 | 2022-02-22 12:36:32 +0100 | [diff] [blame] | 179 | /* Provide a board level S0ix hook */ |
| 180 | acpigen_write_if_cond_ref_of(MAINBOARD_DISPLAY_HOOK); |
| 181 | acpigen_emit_namestring(MAINBOARD_DISPLAY_HOOK); |
| 182 | acpigen_write_integer(1); |
| 183 | acpigen_write_if_end(); |
| 184 | } |
| 185 | |
| 186 | static void lpi_display_off(void *unused) |
| 187 | { |
Michael Niewöhner | a972e23 | 2022-05-18 22:27:13 +0200 | [diff] [blame] | 188 | /* Inform the EC */ |
| 189 | acpigen_write_if_cond_ref_of(EC_DISPLAY_HOOK); |
| 190 | acpigen_emit_namestring(EC_DISPLAY_HOOK); |
| 191 | acpigen_write_integer(0); |
| 192 | acpigen_write_if_end(); |
| 193 | |
Michał Żygowski | 2b87b50 | 2022-02-22 12:36:32 +0100 | [diff] [blame] | 194 | /* Provide a board level S0ix hook */ |
| 195 | acpigen_write_if_cond_ref_of(MAINBOARD_DISPLAY_HOOK); |
| 196 | acpigen_emit_namestring(MAINBOARD_DISPLAY_HOOK); |
| 197 | acpigen_write_integer(0); |
| 198 | acpigen_write_if_end(); |
| 199 | } |
| 200 | |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 201 | static void (*lpi_s0_helpers[])(void *) = { |
| 202 | NULL, /* enumerate functions (autogenerated) */ |
Tarun Tuli | c66ea98 | 2022-05-03 20:35:47 +0000 | [diff] [blame] | 203 | soc_lpi_get_constraints,/* get device constraints */ |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 204 | NULL, /* get crash dump device */ |
Michał Żygowski | 2b87b50 | 2022-02-22 12:36:32 +0100 | [diff] [blame] | 205 | lpi_display_off, /* display off notify */ |
| 206 | lpi_display_on, /* display on notify */ |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 207 | lpi_s0ix_entry, /* s0ix entry */ |
| 208 | lpi_s0ix_exit, /* s0ix exit */ |
| 209 | }; |
| 210 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 211 | static void pep_s0ix_return_lpm_requirements(void *arg) |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 212 | { |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 213 | if (!CONFIG(SOC_INTEL_COMMON_BLOCK_ACPI_PEP_LPM_REQ)) { |
| 214 | acpigen_write_return_singleton_buffer(0x0); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | struct reg_info *info = (struct reg_info *)arg; |
| 219 | acpigen_write_return_byte_buffer(info->addr, info->buffer_size); |
| 220 | } |
| 221 | |
| 222 | static void (*pep_s0ix[])(void *) = { |
| 223 | NULL, /* enumerate functions (autogenerated) */ |
| 224 | pep_s0ix_return_lpm_requirements, /* Return LPM requirements */ |
| 225 | }; |
| 226 | |
| 227 | void generate_acpi_power_engine_with_lpm(const struct soc_pmc_lpm *lpm) |
| 228 | { |
| 229 | struct reg_info info; |
| 230 | size_t uuid_count = 1; |
| 231 | struct dsm_uuid ids[] = { |
| 232 | DSM_UUID(LPI_S0_HELPER_UUID, lpi_s0_helpers, ARRAY_SIZE(lpi_s0_helpers), NULL), |
| 233 | DSM_UUID(PEP_S0IX_UUID, pep_s0ix, ARRAY_SIZE(pep_s0ix), &info), |
| 234 | }; |
| 235 | |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 236 | acpigen_write_scope(PEPD_SCOPE); |
| 237 | acpigen_write_device("PEPD"); |
| 238 | |
| 239 | acpigen_write_name_string("_HID", SYSTEM_POWER_MANAGEMENT_HID); |
| 240 | acpigen_write_name("_CID"); |
| 241 | acpigen_emit_eisaid(SYSTEM_POWER_MANAGEMENT_CID); |
| 242 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 243 | read_pmc_lpm_requirements(lpm, &info); |
| 244 | if (info.buffer_size) |
| 245 | uuid_count++; |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 246 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 247 | acpigen_write_dsm_uuid_arr(ids, uuid_count); |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 248 | acpigen_write_device_end(); |
| 249 | acpigen_write_scope_end(); |
| 250 | |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 251 | free(info.addr); |
Tim Wawrzynczak | 6424648 | 2021-06-25 22:44:45 -0600 | [diff] [blame] | 252 | printk(BIOS_INFO, PEPD_SCOPE ".PEPD: Intel Power Engine Plug-in\n"); |
| 253 | } |
Tim Wawrzynczak | 2eb100d | 2021-07-01 08:20:17 -0600 | [diff] [blame] | 254 | |
| 255 | void generate_acpi_power_engine(void) |
| 256 | { |
| 257 | generate_acpi_power_engine_with_lpm(NULL); |
| 258 | } |