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