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