blob: ad0d9649475ec5d561f11c63592e3e68b556ee58 [file] [log] [blame]
Furquan Shaikhf9be2d12020-06-08 12:30:40 -07001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
3#include <acpi/acpi.h>
4#include <assert.h>
5#include <boot_device.h>
6#include <bootstate.h>
7#include <commonlib/region.h>
8#include <console/console.h>
9#include <fmap.h>
10#include <soc/mrc_cache.h>
11#include <spi_flash.h>
12#include <stdint.h>
13#include <string.h>
14
15#define DEFAULT_MRC_CACHE "RW_MRC_CACHE"
16/* PSP requires this value to be 64KiB */
17#define DEFAULT_MRC_CACHE_SIZE 0x10000
18
19#if !CONFIG_PSP_APOB_DRAM_ADDRESS
20#error Incorrect APOB configuration setting(s)
21#endif
22
23#define APOB_SIGNATURE 0x424F5041 /* 'APOB' */
24
25/* APOB_BASE_HEADER from AGESA */
26struct apob_base_header {
27 uint32_t signature; /* APOB signature */
28 uint32_t version; /* Version */
29 uint32_t size; /* APOB Size */
30 uint32_t offset_of_first_entry; /* APOB Header Size */
31};
32
33static bool apob_header_valid(const struct apob_base_header *apob_header_ptr, const char *where)
34{
35 if (apob_header_ptr->signature != APOB_SIGNATURE) {
36 printk(BIOS_WARNING, "Invalid %s APOB signature %x\n",
37 where, apob_header_ptr->signature);
38 return false;
39 }
40
41 if (apob_header_ptr->size == 0 || apob_header_ptr->size > DEFAULT_MRC_CACHE_SIZE) {
42 printk(BIOS_WARNING, "%s APOB data is too large %x > %x\n",
43 where, apob_header_ptr->size, DEFAULT_MRC_CACHE_SIZE);
44 return false;
45 }
46
47 return true;
48}
49
50static void *get_apob_dram_address(void)
51{
52 /*
53 * TODO: Find the APOB destination by parsing the PSP's tables
54 * (once vboot is implemented).
55 */
56 void *apob_src_ram = (void *)(uintptr_t)CONFIG_PSP_APOB_DRAM_ADDRESS;
57
58 if (apob_header_valid(apob_src_ram, "RAM") == false)
59 return NULL;
60
61 return apob_src_ram;
62}
63
64static int get_nv_region(struct region *r)
65{
66 if (fmap_locate_area(DEFAULT_MRC_CACHE, r) < 0) {
67 printk(BIOS_ERR, "Error: No APOB NV region is found in flash\n");
68 return -1;
69 }
70
71 return 0;
72}
73
74static void *get_apob_from_nv_region(struct region *region)
75{
76 struct region_device read_rdev;
77 struct apob_base_header apob_header;
78
79 if (boot_device_ro_subregion(region, &read_rdev) < 0) {
80 printk(BIOS_ERR, "Failed boot_device_ro_subregion\n");
81 return NULL;
82 }
83
84 if (rdev_readat(&read_rdev, &apob_header, 0, sizeof(apob_header)) < 0) {
85 printk(BIOS_ERR, "Couldn't read APOB header!\n");
86 return NULL;
87 }
88
89 if (apob_header_valid(&apob_header, "ROM") == false) {
90 printk(BIOS_ERR, "No APOB NV data!\n");
91 return NULL;
92 }
93
94 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
95 return rdev_mmap_full(&read_rdev);
96}
97
98/* Save APOB buffer to flash */
99void soc_update_mrc_cache(void)
100{
101 struct apob_base_header *apob_rom;
102 struct region_device write_rdev;
103 struct region region;
104 bool update_needed = false;
105 const struct apob_base_header *apob_src_ram;
106
107 /* Nothing to update in case of S3 resume. */
108 if (acpi_is_wakeup_s3())
109 return;
110
111 apob_src_ram = get_apob_dram_address();
112 if (apob_src_ram == NULL)
113 return;
114
115 if (get_nv_region(&region) != 0)
116 return;
117
118 apob_rom = get_apob_from_nv_region(&region);
119 if (apob_rom == NULL) {
120 update_needed = true;
121 } else if (memcmp(apob_src_ram, apob_rom, apob_src_ram->size)) {
122 printk(BIOS_INFO, "APOB RAM copy differs from flash\n");
123 update_needed = true;
124 } else
125 printk(BIOS_DEBUG, "APOB valid copy is already in flash\n");
126
127 if (!update_needed)
128 return;
129
130 printk(BIOS_SPEW, "Copy APOB from RAM 0x%p/0x%x to flash 0x%zx/0x%zx\n",
131 apob_src_ram, apob_src_ram->size,
132 region_offset(&region), region_sz(&region));
133
134 if (boot_device_rw_subregion(&region, &write_rdev) < 0) {
135 printk(BIOS_ERR, "Failed boot_device_rw_subregion\n");
136 return;
137 }
138
139 /* write data to flash region */
140 if (rdev_eraseat(&write_rdev, 0, DEFAULT_MRC_CACHE_SIZE) < 0) {
141 printk(BIOS_ERR, "Error: APOB flash region erase failed\n");
142 return;
143 }
144
145 if (rdev_writeat(&write_rdev, apob_src_ram, 0, apob_src_ram->size) < 0) {
146 printk(BIOS_ERR, "Error: APOB flash region update failed\n");
147 return;
148 }
149
150 printk(BIOS_INFO, "Updated APOB in flash\n");
151}
152
153static void *get_apob_nv_address(void)
154{
155 struct region region;
156
157 if (get_nv_region(&region) != 0)
158 return NULL;
159
160 return get_apob_from_nv_region(&region);
161}
162
163void *soc_fill_mrc_cache(void)
164{
165 /* If this is non-S3 boot, then use the APOB data placed by PSP in DRAM. */
166 if (!acpi_is_wakeup_s3())
167 return get_apob_dram_address();
168
169 /*
170 * In case of S3 resume, PSP does not copy APOB data to DRAM. Thus, coreboot needs to
171 * provide the APOB NV data from RW_MRC_CACHE on SPI flash so that FSP can use it
172 * without having to traverse the BIOS directory table.
173 */
174 return get_apob_nv_address();
175}