Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <acpi/acpi_gnvs.h> |
Kyösti Mälkki | 2787237 | 2021-01-21 16:05:26 +0200 | [diff] [blame] | 4 | #include <acpi/acpi_pm.h> |
Felix Held | 4fbae5c | 2021-02-03 17:27:22 +0100 | [diff] [blame] | 5 | #include <amdblocks/acpi.h> |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 6 | #include <bootstate.h> |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 7 | #include <soc/acpi.h> |
| 8 | #include <soc/nvs.h> |
| 9 | #include <soc/southbridge.h> |
| 10 | #include <types.h> |
| 11 | |
| 12 | static int get_index_bit(uint32_t value, uint16_t limit) |
| 13 | { |
| 14 | uint16_t i; |
| 15 | uint32_t t; |
| 16 | |
Felix Held | 404aea8 | 2021-02-02 23:46:25 +0100 | [diff] [blame] | 17 | if (limit > TOTAL_BITS(uint32_t)) |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 18 | return -1; |
| 19 | |
| 20 | /* get a mask of valid bits. Ex limit = 3, set bits 0-2 */ |
Felix Held | 373769f | 2021-02-10 18:57:04 +0100 | [diff] [blame] | 21 | t = (1ULL << limit) - 1; |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 22 | if ((value & t) == 0) |
| 23 | return -1; |
| 24 | t = 1; |
| 25 | for (i = 0; i < limit; i++) { |
| 26 | if (value & t) |
| 27 | break; |
| 28 | t <<= 1; |
| 29 | } |
| 30 | return i; |
| 31 | } |
| 32 | |
Kyösti Mälkki | 08bd207 | 2021-01-21 16:41:16 +0200 | [diff] [blame] | 33 | static void pm_fill_gnvs(struct global_nvs *gnvs, const struct acpi_pm_gpe_state *state) |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 34 | { |
| 35 | int index; |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 36 | |
| 37 | index = get_index_bit(state->pm1_sts & state->pm1_en, PM1_LIMIT); |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 38 | if (index >= 0) |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 39 | gnvs->pm1i = index; |
| 40 | |
| 41 | index = get_index_bit(state->gpe0_sts & state->gpe0_en, GPE0_LIMIT); |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 42 | if (index >= 0) |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 43 | gnvs->gpei = index; |
| 44 | } |
| 45 | |
Kyösti Mälkki | 08bd207 | 2021-01-21 16:41:16 +0200 | [diff] [blame] | 46 | static void acpi_save_wake_source(void *unused) |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 47 | { |
Kyösti Mälkki | 08bd207 | 2021-01-21 16:41:16 +0200 | [diff] [blame] | 48 | const struct chipset_power_state *ps; |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 49 | struct global_nvs *gnvs; |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 50 | |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 51 | if (acpi_reset_gnvs_for_wake(&gnvs) < 0) |
| 52 | return; |
Fabio Aiuto | fdcf698 | 2022-09-11 12:25:13 +0200 | [diff] [blame] | 53 | if (acpi_fetch_pm_state(&ps, PS_CLAIMER_WAKE) < 0) |
Kyösti Mälkki | 08bd207 | 2021-01-21 16:41:16 +0200 | [diff] [blame] | 54 | return; |
| 55 | |
| 56 | pm_fill_gnvs(gnvs, &ps->gpe_state); |
Kyösti Mälkki | f1b0935 | 2020-12-22 06:34:02 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 59 | BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, acpi_save_wake_source, NULL); |