Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <acpi/acpi_gnvs.h> |
| 4 | #include <acpi/acpigen.h> |
| 5 | #include <cbmem.h> |
| 6 | #include <console/console.h> |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 7 | #include <soc/nvs.h> |
| 8 | #include <stdint.h> |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <types.h> |
| 11 | |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 12 | static struct global_nvs *gnvs; |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 13 | static void *dnvs; |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 14 | |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 15 | void acpi_create_gnvs(void) |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 16 | { |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 17 | const size_t gnvs_size = ALIGN_UP(sizeof(struct global_nvs), sizeof(uint64_t)); |
| 18 | const size_t dnvs_size = ALIGN_UP(size_of_dnvs(), sizeof(uint64_t)); |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 19 | |
Kyösti Mälkki | 62d6f1f | 2021-01-27 10:13:33 +0200 | [diff] [blame] | 20 | gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS); |
| 21 | if (gnvs) |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 22 | return; |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 23 | |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 24 | /* Allocate for both GNVS and DNVS OpRegions. */ |
| 25 | gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, gnvs_size + dnvs_size); |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 26 | if (!gnvs) |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 27 | return; |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 28 | |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 29 | memset(gnvs, 0, gnvs_size + dnvs_size); |
| 30 | |
| 31 | if (dnvs_size) |
| 32 | dnvs = (char *)gnvs + gnvs_size; |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 33 | |
| 34 | if (CONFIG(CONSOLE_CBMEM)) |
Kyösti Mälkki | 37eb24b | 2021-01-11 18:40:37 +0200 | [diff] [blame] | 35 | gnvs->cbmc = (uintptr_t)cbmem_find(CBMEM_ID_CONSOLE); |
Kyösti Mälkki | e37459e | 2020-06-17 01:04:44 +0300 | [diff] [blame] | 36 | } |
| 37 | |
Kyösti Mälkki | 62d6f1f | 2021-01-27 10:13:33 +0200 | [diff] [blame] | 38 | void *acpi_get_gnvs(void) |
| 39 | { |
| 40 | if (gnvs) |
| 41 | return gnvs; |
| 42 | |
| 43 | gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS); |
| 44 | if (gnvs) |
| 45 | return gnvs; |
| 46 | |
| 47 | printk(BIOS_ERR, "Unable to locate Global NVS\n"); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Kyösti Mälkki | 4abc731 | 2021-01-12 17:46:30 +0200 | [diff] [blame] | 51 | void *acpi_get_device_nvs(void) |
| 52 | { |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 53 | return dnvs; |
Kyösti Mälkki | 4abc731 | 2021-01-12 17:46:30 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Kyösti Mälkki | 62d6f1f | 2021-01-27 10:13:33 +0200 | [diff] [blame] | 56 | /* Implemented under platform. */ |
| 57 | __weak void soc_fill_gnvs(struct global_nvs *gnvs_) { } |
| 58 | __weak void mainboard_fill_gnvs(struct global_nvs *gnvs_) { } |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 59 | __weak size_t size_of_dnvs(void) { return 0; } |
Kyösti Mälkki | 62d6f1f | 2021-01-27 10:13:33 +0200 | [diff] [blame] | 60 | |
| 61 | /* Called from write_acpi_tables() only on normal boot path. */ |
Kyösti Mälkki | 2ab4a96 | 2020-06-30 11:41:47 +0300 | [diff] [blame] | 62 | void acpi_fill_gnvs(void) |
| 63 | { |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 64 | const struct opregion gnvs_op = OPREGION("GNVS", SYSTEMMEMORY, (uintptr_t)gnvs, |
| 65 | sizeof(struct global_nvs)); |
| 66 | const struct opregion dnvs_op = OPREGION("DNVS", SYSTEMMEMORY, (uintptr_t)dnvs, |
| 67 | size_of_dnvs()); |
Kyösti Mälkki | 2ad598f | 2021-03-16 13:02:34 +0200 | [diff] [blame] | 68 | |
Kyösti Mälkki | 2ab4a96 | 2020-06-30 11:41:47 +0300 | [diff] [blame] | 69 | if (!gnvs) |
| 70 | return; |
| 71 | |
| 72 | soc_fill_gnvs(gnvs); |
| 73 | mainboard_fill_gnvs(gnvs); |
Kyösti Mälkki | 91946c5 | 2020-06-30 11:42:52 +0300 | [diff] [blame] | 74 | |
| 75 | acpigen_write_scope("\\"); |
Kyösti Mälkki | 2ad598f | 2021-03-16 13:02:34 +0200 | [diff] [blame] | 76 | acpigen_write_opregion(&gnvs_op); |
Kyösti Mälkki | 4bd9187 | 2021-03-16 19:02:26 +0200 | [diff] [blame^] | 77 | if (dnvs) |
Kyösti Mälkki | 2ad598f | 2021-03-16 13:02:34 +0200 | [diff] [blame] | 78 | acpigen_write_opregion(&dnvs_op); |
Kyösti Mälkki | 91946c5 | 2020-06-30 11:42:52 +0300 | [diff] [blame] | 79 | acpigen_pop_len(); |
Kyösti Mälkki | 2ab4a96 | 2020-06-30 11:41:47 +0300 | [diff] [blame] | 80 | } |
Kyösti Mälkki | 4de1a31 | 2021-01-15 05:58:42 +0200 | [diff] [blame] | 81 | |
| 82 | int acpi_reset_gnvs_for_wake(struct global_nvs **gnvs_) |
| 83 | { |
| 84 | if (!gnvs) |
| 85 | return -1; |
| 86 | |
| 87 | /* Set unknown wake source */ |
| 88 | gnvs->pm1i = -1; |
| 89 | gnvs->gpei = -1; |
| 90 | |
| 91 | *gnvs_ = gnvs; |
| 92 | return 0; |
| 93 | } |