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