xref: /aosp_15_r20/external/coreboot/src/acpi/gnvs.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpi_gnvs.h>
4 #include <acpi/acpigen.h>
5 #include <bootstate.h>
6 #include <cbmem.h>
7 #include <console/console.h>
8 #include <soc/nvs.h>
9 #include <string.h>
10 #include <types.h>
11 
12 static struct global_nvs *gnvs;
13 static void *dnvs;
14 
acpi_create_gnvs(void * unused)15 static void acpi_create_gnvs(void *unused)
16 {
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));
19 
20 	gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
21 	if (gnvs)
22 		return;
23 
24 	/* Allocate for both GNVS and DNVS OpRegions. */
25 	gnvs = cbmem_add(CBMEM_ID_ACPI_GNVS, gnvs_size + dnvs_size);
26 	if (!gnvs)
27 		return;
28 
29 	memset(gnvs, 0, gnvs_size + dnvs_size);
30 
31 	if (dnvs_size)
32 		dnvs = (char *)gnvs + gnvs_size;
33 }
34 
35 BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_EXIT, acpi_create_gnvs, NULL);
36 
acpi_get_gnvs(void)37 void *acpi_get_gnvs(void)
38 {
39 	if (gnvs)
40 		return gnvs;
41 
42 	gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
43 	if (gnvs)
44 		return gnvs;
45 
46 	printk(BIOS_ERR, "Unable to locate Global NVS\n");
47 	return NULL;
48 }
49 
acpi_get_device_nvs(void)50 void *acpi_get_device_nvs(void)
51 {
52 	return dnvs;
53 }
54 
55 /* Implemented under platform. */
soc_fill_gnvs(struct global_nvs * gnvs_)56 __weak void soc_fill_gnvs(struct global_nvs *gnvs_) { }
mainboard_fill_gnvs(struct global_nvs * gnvs_)57 __weak void mainboard_fill_gnvs(struct global_nvs *gnvs_) { }
size_of_dnvs(void)58 __weak size_t size_of_dnvs(void) { return 0; }
59 
60 /* Called from write_acpi_tables() only on normal boot path. */
acpi_fill_gnvs(void)61 void acpi_fill_gnvs(void)
62 {
63 	const struct opregion gnvs_op = OPREGION("GNVS", SYSTEMMEMORY, (uintptr_t)gnvs,
64 						 sizeof(struct global_nvs));
65 	const struct opregion dnvs_op = OPREGION("DNVS", SYSTEMMEMORY, (uintptr_t)dnvs,
66 						 size_of_dnvs());
67 
68 	if (!gnvs)
69 		return;
70 
71 	soc_fill_gnvs(gnvs);
72 	mainboard_fill_gnvs(gnvs);
73 
74 	acpigen_write_scope("\\");
75 	acpigen_write_opregion(&gnvs_op);
76 	if (dnvs)
77 		acpigen_write_opregion(&dnvs_op);
78 	acpigen_pop_len();
79 }
80 
acpi_reset_gnvs_for_wake(struct global_nvs ** gnvs_)81 int acpi_reset_gnvs_for_wake(struct global_nvs **gnvs_)
82 {
83 	if (!gnvs)
84 		return -1;
85 
86 	/* Set unknown wake source */
87 	gnvs->pm1i = -1;
88 	gnvs->gpei = -1;
89 
90 	*gnvs_ = gnvs;
91 	return 0;
92 }
93