blob: bdacd81ba4d82e75d4c9697b60293b9051d520f5 [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>
Felix Held21c46c02021-03-05 00:13:16 +01004#include <amdblocks/apob_cache.h>
Furquan Shaikhf9be2d12020-06-08 12:30:40 -07005#include <assert.h>
6#include <boot_device.h>
Raul E Rangel73e0f182021-07-12 09:21:51 -06007#include <bootstate.h>
Raul E Rangelfca58332021-06-25 11:15:41 -06008#include <commonlib/helpers.h>
Furquan Shaikhf9be2d12020-06-08 12:30:40 -07009#include <commonlib/region.h>
10#include <console/console.h>
11#include <fmap.h>
Furquan Shaikhf9be2d12020-06-08 12:30:40 -070012#include <spi_flash.h>
13#include <stdint.h>
14#include <string.h>
Raul E Rangelfca58332021-06-25 11:15:41 -060015#include <thread.h>
Raul E Rangeld742b5e2021-06-10 14:39:01 -060016#include <timestamp.h>
Furquan Shaikhf9be2d12020-06-08 12:30:40 -070017
18#define DEFAULT_MRC_CACHE "RW_MRC_CACHE"
19/* PSP requires this value to be 64KiB */
20#define DEFAULT_MRC_CACHE_SIZE 0x10000
21
22#if !CONFIG_PSP_APOB_DRAM_ADDRESS
23#error Incorrect APOB configuration setting(s)
24#endif
25
26#define APOB_SIGNATURE 0x424F5041 /* 'APOB' */
27
28/* APOB_BASE_HEADER from AGESA */
29struct apob_base_header {
30 uint32_t signature; /* APOB signature */
31 uint32_t version; /* Version */
32 uint32_t size; /* APOB Size */
33 uint32_t offset_of_first_entry; /* APOB Header Size */
34};
35
36static bool apob_header_valid(const struct apob_base_header *apob_header_ptr, const char *where)
37{
38 if (apob_header_ptr->signature != APOB_SIGNATURE) {
39 printk(BIOS_WARNING, "Invalid %s APOB signature %x\n",
40 where, apob_header_ptr->signature);
41 return false;
42 }
43
44 if (apob_header_ptr->size == 0 || apob_header_ptr->size > DEFAULT_MRC_CACHE_SIZE) {
45 printk(BIOS_WARNING, "%s APOB data is too large %x > %x\n",
46 where, apob_header_ptr->size, DEFAULT_MRC_CACHE_SIZE);
47 return false;
48 }
49
50 return true;
51}
52
53static void *get_apob_dram_address(void)
54{
55 /*
56 * TODO: Find the APOB destination by parsing the PSP's tables
57 * (once vboot is implemented).
58 */
59 void *apob_src_ram = (void *)(uintptr_t)CONFIG_PSP_APOB_DRAM_ADDRESS;
60
61 if (apob_header_valid(apob_src_ram, "RAM") == false)
62 return NULL;
63
64 return apob_src_ram;
65}
66
Raul E Rangelce63dc42021-07-16 10:37:58 -060067static int get_nv_rdev(struct region_device *r)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -070068{
Raul E Rangelce63dc42021-07-16 10:37:58 -060069 if (fmap_locate_area_as_rdev(DEFAULT_MRC_CACHE, r) < 0) {
Julius Wernere9665952022-01-21 17:06:20 -080070 printk(BIOS_ERR, "No APOB NV region is found in flash\n");
Furquan Shaikhf9be2d12020-06-08 12:30:40 -070071 return -1;
72 }
73
74 return 0;
75}
76
Raul E Rangelfca58332021-06-25 11:15:41 -060077static struct apob_thread_context {
78 uint8_t buffer[DEFAULT_MRC_CACHE_SIZE] __attribute__((aligned(64)));
79 struct thread_handle handle;
80 struct region_device apob_rdev;
81} global_apob_thread;
82
83static enum cb_err apob_thread_entry(void *arg)
84{
85 ssize_t size;
86 struct apob_thread_context *thread = arg;
87
88 printk(BIOS_DEBUG, "APOB thread running\n");
89 size = rdev_readat(&thread->apob_rdev, thread->buffer, 0,
90 region_device_sz(&thread->apob_rdev));
91
92 printk(BIOS_DEBUG, "APOB thread done\n");
93
94 if (size == region_device_sz(&thread->apob_rdev))
95 return CB_SUCCESS;
96
97 return CB_ERR;
98}
99
100void start_apob_cache_read(void)
101{
102 struct apob_thread_context *thread = &global_apob_thread;
103
104 if (!CONFIG(COOP_MULTITASKING))
105 return;
106
107 /* We don't perform any comparison on S3 resume */
108 if (acpi_is_wakeup_s3())
109 return;
110
111 if (get_nv_rdev(&thread->apob_rdev) != 0)
112 return;
113
114 assert(ARRAY_SIZE(thread->buffer) == region_device_sz(&thread->apob_rdev));
115
116 printk(BIOS_DEBUG, "Starting APOB preload\n");
117 if (thread_run(&thread->handle, apob_thread_entry, thread))
118 printk(BIOS_ERR, "Failed to start APOB preload thread\n");
119}
120
Raul E Rangelce63dc42021-07-16 10:37:58 -0600121static void *get_apob_from_nv_rdev(struct region_device *read_rdev)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700122{
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700123 struct apob_base_header apob_header;
124
Raul E Rangelce63dc42021-07-16 10:37:58 -0600125 if (rdev_readat(read_rdev, &apob_header, 0, sizeof(apob_header)) < 0) {
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700126 printk(BIOS_ERR, "Couldn't read APOB header!\n");
127 return NULL;
128 }
129
130 if (apob_header_valid(&apob_header, "ROM") == false) {
131 printk(BIOS_ERR, "No APOB NV data!\n");
132 return NULL;
133 }
134
135 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Raul E Rangelce63dc42021-07-16 10:37:58 -0600136 return rdev_mmap_full(read_rdev);
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700137}
138
139/* Save APOB buffer to flash */
Raul E Rangel73e0f182021-07-12 09:21:51 -0600140static void soc_update_apob_cache(void *unused)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700141{
142 struct apob_base_header *apob_rom;
Raul E Rangelce63dc42021-07-16 10:37:58 -0600143 struct region_device read_rdev, write_rdev;
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700144 bool update_needed = false;
145 const struct apob_base_header *apob_src_ram;
146
147 /* Nothing to update in case of S3 resume. */
148 if (acpi_is_wakeup_s3())
149 return;
150
151 apob_src_ram = get_apob_dram_address();
152 if (apob_src_ram == NULL)
153 return;
154
Raul E Rangelce63dc42021-07-16 10:37:58 -0600155 if (get_nv_rdev(&read_rdev) != 0)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700156 return;
157
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600158 timestamp_add_now(TS_AMD_APOB_READ_START);
159
Raul E Rangelfca58332021-06-25 11:15:41 -0600160 if (CONFIG(COOP_MULTITASKING) && thread_join(&global_apob_thread.handle) == CB_SUCCESS)
161 apob_rom = (struct apob_base_header *)global_apob_thread.buffer;
162 else
163 apob_rom = get_apob_from_nv_rdev(&read_rdev);
164
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700165 if (apob_rom == NULL) {
166 update_needed = true;
167 } else if (memcmp(apob_src_ram, apob_rom, apob_src_ram->size)) {
168 printk(BIOS_INFO, "APOB RAM copy differs from flash\n");
169 update_needed = true;
170 } else
171 printk(BIOS_DEBUG, "APOB valid copy is already in flash\n");
172
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600173 if (!update_needed) {
174 timestamp_add_now(TS_AMD_APOB_DONE);
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700175 return;
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600176 }
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700177
Raul E Rangel72240432021-07-12 09:50:49 -0600178 printk(BIOS_SPEW, "Copy APOB from RAM %p/%#x to flash %#zx/%#zx\n",
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700179 apob_src_ram, apob_src_ram->size,
Raul E Rangelce63dc42021-07-16 10:37:58 -0600180 region_device_offset(&read_rdev), region_device_sz(&read_rdev));
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700181
Raul E Rangelce63dc42021-07-16 10:37:58 -0600182 if (fmap_locate_area_as_rdev_rw(DEFAULT_MRC_CACHE, &write_rdev) < 0) {
Julius Wernere9665952022-01-21 17:06:20 -0800183 printk(BIOS_ERR, "No RW APOB NV region is found in flash\n");
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700184 return;
185 }
186
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600187 timestamp_add_now(TS_AMD_APOB_ERASE_START);
188
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700189 /* write data to flash region */
190 if (rdev_eraseat(&write_rdev, 0, DEFAULT_MRC_CACHE_SIZE) < 0) {
Julius Wernere9665952022-01-21 17:06:20 -0800191 printk(BIOS_ERR, "APOB flash region erase failed\n");
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700192 return;
193 }
194
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600195 timestamp_add_now(TS_AMD_APOB_WRITE_START);
196
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700197 if (rdev_writeat(&write_rdev, apob_src_ram, 0, apob_src_ram->size) < 0) {
Julius Wernere9665952022-01-21 17:06:20 -0800198 printk(BIOS_ERR, "APOB flash region update failed\n");
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700199 return;
200 }
201
Raul E Rangeld742b5e2021-06-10 14:39:01 -0600202 timestamp_add_now(TS_AMD_APOB_DONE);
203
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700204 printk(BIOS_INFO, "Updated APOB in flash\n");
205}
206
207static void *get_apob_nv_address(void)
208{
Raul E Rangelce63dc42021-07-16 10:37:58 -0600209 struct region_device rdev;
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700210
Raul E Rangelce63dc42021-07-16 10:37:58 -0600211 if (get_nv_rdev(&rdev) != 0)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700212 return NULL;
213
Raul E Rangelce63dc42021-07-16 10:37:58 -0600214 return get_apob_from_nv_rdev(&rdev);
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700215}
216
Felix Held21c46c02021-03-05 00:13:16 +0100217void *soc_fill_apob_cache(void)
Furquan Shaikhf9be2d12020-06-08 12:30:40 -0700218{
219 /* If this is non-S3 boot, then use the APOB data placed by PSP in DRAM. */
220 if (!acpi_is_wakeup_s3())
221 return get_apob_dram_address();
222
223 /*
224 * In case of S3 resume, PSP does not copy APOB data to DRAM. Thus, coreboot needs to
225 * provide the APOB NV data from RW_MRC_CACHE on SPI flash so that FSP can use it
226 * without having to traverse the BIOS directory table.
227 */
228 return get_apob_nv_address();
229}
Raul E Rangelfca58332021-06-25 11:15:41 -0600230
231/*
232 * BS_POST_DEVICE was chosen because this gives start_apob_cache_read plenty of time to read
233 * the APOB from SPI.
234 */
Raul E Rangel73e0f182021-07-12 09:21:51 -0600235BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, soc_update_apob_cache, NULL);