blob: b12229d89e3e623480dc082d3b91bd8cccde72b9 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrey Petrov465fc132016-02-25 14:16:33 -08002
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01003#include <security/vboot/antirollback.h>
Aaron Durbinb4302502016-07-17 17:04:37 -05004#include <arch/symbols.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06005#include <assert.h>
Joel Kitching9a292282020-03-06 13:44:50 +08006#include <bootmode.h>
Aaron Durbind04639b2016-07-17 23:23:59 -05007#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -05008#include <cbmem.h>
Patrick Rudolphf677d172018-10-01 19:17:11 +02009#include <cf9_reset.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080010#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070011#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080012#include <fsp/api.h>
13#include <fsp/util.h>
14#include <memrange.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070015#include <mrc_cache.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050016#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050017#include <romstage_handoff.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080018#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050019#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080020#include <timestamp.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020021#include <security/vboot/vboot_common.h>
Shelley Chen9f8ac642020-10-16 12:20:16 -070022#include <security/tpm/tspi.h>
Furquan Shaikh2db5bac2016-11-07 23:57:48 -080023#include <vb2_api.h>
Elyes HAOUASbd1683d2019-05-15 21:05:37 +020024#include <types.h>
Patrick Rudolph40beb362020-12-01 10:08:38 +010025#include <mode_switch.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080026
Kyösti Mälkkic9871502019-09-03 07:03:39 +030027static uint8_t temp_ram[CONFIG_FSP_TEMP_RAM_SIZE] __aligned(sizeof(uint64_t));
28
Aaron Durbinf0ec8242016-07-18 11:24:36 -050029static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050030{
Aaron Durbinb4302502016-07-17 17:04:37 -050031 size_t mrc_data_size;
32 const void *mrc_data;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050033
Julius Wernercd49cce2019-03-05 16:53:33 -080034 if (!CONFIG(CACHE_MRC_SETTINGS) || s3wake)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050035 return;
36
37 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
38 if (!mrc_data) {
Subrata Banikd5ab1262021-01-21 16:28:10 +053039 printk(BIOS_ERR, "ERROR: FSP_NON_VOLATILE_STORAGE_HOB missing!\n");
Aaron Durbinf0ec8242016-07-18 11:24:36 -050040 return;
41 }
42
43 /*
44 * Save MRC Data to CBMEM. By always saving the data this forces
45 * a retrain after a trip through Chrome OS recovery path. The
46 * code which saves the data to flash doesn't write if the latest
47 * training data matches this one.
48 */
Aaron Durbin31be2c92016-12-03 22:08:20 -060049 if (mrc_cache_stash_data(MRC_TRAINING_DATA, fsp_version, mrc_data,
50 mrc_data_size) < 0)
Subrata Banikd5ab1262021-01-21 16:28:10 +053051 printk(BIOS_ERR, "ERROR: Failed to stash MRC data\n");
Aaron Durbinf0ec8242016-07-18 11:24:36 -050052}
53
Lee Leahy9671faa2016-07-24 18:18:52 -070054static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050055{
56 struct range_entry fsp_mem;
Aaron Durbinb4302502016-07-17 17:04:37 -050057
Michael Niewöhnerbc1dbb32019-10-24 22:58:25 +020058 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050059
60 /* initialize cbmem by adding FSP reserved memory first thing */
61 if (!s3wake) {
62 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
63 range_entry_size(&fsp_mem));
64 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
65 range_entry_size(&fsp_mem))) {
Julius Wernercd49cce2019-03-05 16:53:33 -080066 if (CONFIG(HAVE_ACPI_RESUME)) {
Subrata Banikd5ab1262021-01-21 16:28:10 +053067 printk(BIOS_ERR, "ERROR: Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050068 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolphf677d172018-10-01 19:17:11 +020069 /* FIXME: A "system" reset is likely enough: */
70 full_reset();
Aaron Durbinb4302502016-07-17 17:04:37 -050071 }
72 }
73
74 /* make sure FSP memory is reserved in cbmem */
75 if (range_entry_base(&fsp_mem) !=
76 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
Lee Leahy9671faa2016-07-24 18:18:52 -070077 die("Failed to accommodate FSP reserved memory request!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050078
Aaron Durbinf0ec8242016-07-18 11:24:36 -050079 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -050080
81 /* Create romstage handof information */
Aaron Durbin77e13992016-11-29 17:43:04 -060082 romstage_handoff_init(s3wake);
Aaron Durbinb4302502016-07-17 17:04:37 -050083}
84
Aamir Bohra69cd62c2018-01-08 11:01:34 +053085static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050086{
Aaron Durbin31be2c92016-12-03 22:08:20 -060087 void *data;
Shelley Chenad9cd682020-07-23 16:10:52 -070088 size_t mrc_size;
Aaron Durbinb4302502016-07-17 17:04:37 -050089
Patrick Rudolph31218a42020-11-30 15:50:06 +010090 arch_upd->NvsBufferPtr = 0;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050091
Julius Wernercd49cce2019-03-05 16:53:33 -080092 if (!CONFIG(CACHE_MRC_SETTINGS))
Aaron Durbinf0ec8242016-07-18 11:24:36 -050093 return;
94
Aaron Durbin31be2c92016-12-03 22:08:20 -060095 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -080096 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbin31be2c92016-12-03 22:08:20 -060097
Shelley Chenad9cd682020-07-23 16:10:52 -070098 data = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, fsp_version,
99 &mrc_size);
Aaron Durbin31be2c92016-12-03 22:08:20 -0600100 if (data == NULL)
101 return;
102
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500103 /* MRC cache found */
Patrick Rudolph31218a42020-11-30 15:50:06 +0100104 arch_upd->NvsBufferPtr = (uintptr_t)data;
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530105
Shelley Chenad9cd682020-07-23 16:10:52 -0700106 printk(BIOS_SPEW, "MRC cache found, size %zx\n", mrc_size);
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500107}
108
Aaron Durbin02e504c2016-07-18 11:53:10 -0500109static enum cb_err check_region_overlap(const struct memranges *ranges,
110 const char *description,
111 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500112{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500113 const struct range_entry *r;
114
115 memranges_each_entry(r, ranges) {
116 if (end <= range_entry_base(r))
117 continue;
118 if (begin >= range_entry_end(r))
119 continue;
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700120 printk(BIOS_CRIT, "'%s' overlaps currently running program: "
Aaron Durbin02e504c2016-07-18 11:53:10 -0500121 "[%p, %p)\n", description, (void *)begin, (void *)end);
122 return CB_ERR;
123 }
124
125 return CB_SUCCESS;
126}
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300127
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530128static enum cb_err setup_fsp_stack_frame(FSPM_ARCH_UPD *arch_upd,
129 const struct memranges *memmap)
Aaron Durbin02e504c2016-07-18 11:53:10 -0500130{
131 uintptr_t stack_begin;
132 uintptr_t stack_end;
133
Aaron Durbinb4302502016-07-17 17:04:37 -0500134 /*
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530135 * FSPM_UPD passed here is populated with default values
136 * provided by the blob itself. We let FSPM use top of CAR
137 * region of the size it requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500138 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500139 stack_end = (uintptr_t)_car_region_end;
140 stack_begin = stack_end - arch_upd->StackSize;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500141 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
142 stack_end) != CB_SUCCESS)
143 return CB_ERR;
144
Patrick Rudolph31218a42020-11-30 15:50:06 +0100145 arch_upd->StackBase = stack_begin;
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530146 return CB_SUCCESS;
147}
148
149static enum cb_err fsp_fill_common_arch_params(FSPM_ARCH_UPD *arch_upd,
150 bool s3wake, uint32_t fsp_version,
151 const struct memranges *memmap)
152{
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300153 /*
154 * FSP 2.1 version would use same stack as coreboot instead of
155 * setting up separate stack frame. FSP 2.1 would not relocate stack
156 * top and does not reinitialize stack pointer. The parameters passed
157 * as StackBase and StackSize are actually for temporary RAM and HOBs
158 * and are not related to FSP stack at all.
Felix Held414d7e42020-08-11 22:54:06 +0200159 * Non-CAR FSP 2.0 platforms pass a DRAM location for the FSP stack.
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300160 */
Felix Held414d7e42020-08-11 22:54:06 +0200161 if (CONFIG(FSP_USES_CB_STACK) || !ENV_CACHE_AS_RAM) {
Patrick Rudolph31218a42020-11-30 15:50:06 +0100162 arch_upd->StackBase = (uintptr_t)temp_ram;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300163 arch_upd->StackSize = sizeof(temp_ram);
164 } else if (setup_fsp_stack_frame(arch_upd, memmap)) {
Aamir Bohra6d569e0c2018-08-27 13:36:15 +0530165 return CB_ERR;
Kyösti Mälkkic9871502019-09-03 07:03:39 +0300166 }
Aaron Durbinb4302502016-07-17 17:04:37 -0500167
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530168 fsp_fill_mrc_cache(arch_upd, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -0500169
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530170 /* Configure bootmode */
171 if (s3wake) {
Aamir Bohra69cd62c2018-01-08 11:01:34 +0530172 arch_upd->BootMode = FSP_BOOT_ON_S3_RESUME;
173 } else {
174 if (arch_upd->NvsBufferPtr)
175 arch_upd->BootMode =
176 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
177 else
178 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
179 }
Aaron Durbin02e504c2016-07-18 11:53:10 -0500180
Marshall Dawson22d66ef2019-08-30 14:52:37 -0600181 printk(BIOS_SPEW, "bootmode is set to: %d\n", arch_upd->BootMode);
Aamir Bohra276c06a2017-12-26 17:54:45 +0530182
Aaron Durbin02e504c2016-07-18 11:53:10 -0500183 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500184}
185
Aaron Durbin64031672018-04-21 14:45:32 -0600186__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500187uint8_t fsp_memory_mainboard_version(void)
188{
189 return 0;
190}
191
Aaron Durbin64031672018-04-21 14:45:32 -0600192__weak
Aaron Durbina3cecb22017-04-25 21:58:10 -0500193uint8_t fsp_memory_soc_version(void)
194{
195 return 0;
196}
197
198/*
199 * Allow SoC and/or mainboard to bump the revision of the FSP setting
200 * number. The FSP spec uses the low 8 bits as the build number. Take over
201 * bits 3:0 for the SoC setting and bits 7:4 for the mainboard. That way
202 * a tweak in the settings will bump the version used to track the cached
203 * setting which triggers retraining when the FSP version hasn't changed, but
204 * the SoC or mainboard settings have.
205 */
206static uint32_t fsp_memory_settings_version(const struct fsp_header *hdr)
207{
208 /* Use the full FSP version by default. */
209 uint32_t ver = hdr->fsp_revision;
210
Julius Wernercd49cce2019-03-05 16:53:33 -0800211 if (!CONFIG(FSP_PLATFORM_MEMORY_SETTINGS_VERSIONS))
Aaron Durbina3cecb22017-04-25 21:58:10 -0500212 return ver;
213
214 ver &= ~0xff;
215 ver |= (0xf & fsp_memory_mainboard_version()) << 4;
216 ver |= (0xf & fsp_memory_soc_version()) << 0;
217
218 return ver;
219}
220
Aaron Durbinecbfa992020-05-15 17:01:58 -0600221struct fspm_context {
222 struct fsp_header header;
223 struct memranges memmap;
224};
225
226static void do_fsp_memory_init(const struct fspm_context *context, bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800227{
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700228 uint32_t status;
Andrey Petrov465fc132016-02-25 14:16:33 -0800229 fsp_memory_init_fn fsp_raminit;
Brandon Breitensteinc31ba0e2016-07-27 17:34:45 -0700230 FSPM_UPD fspm_upd, *upd;
231 FSPM_ARCH_UPD *arch_upd;
Aaron Durbina3cecb22017-04-25 21:58:10 -0500232 uint32_t fsp_version;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600233 const struct fsp_header *hdr = &context->header;
234 const struct memranges *memmap = &context->memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800235
Furquan Shaikh585210a2018-10-16 11:54:37 -0700236 post_code(POST_MEM_PREINIT_PREP_START);
Andrey Petrov465fc132016-02-25 14:16:33 -0800237
Aaron Durbina3cecb22017-04-25 21:58:10 -0500238 fsp_version = fsp_memory_settings_version(hdr);
239
Patrick Rudolph31218a42020-11-30 15:50:06 +0100240 upd = (FSPM_UPD *)(uintptr_t)(hdr->cfg_region_offset + hdr->image_base);
Andrey Petrov465fc132016-02-25 14:16:33 -0800241
Felix Helda0955952021-02-02 21:30:25 +0100242 /*
243 * Verify UPD region size. We don't have malloc before ramstage, so we
244 * use a static buffer for the FSP-M UPDs which is sizeof(FSPM_UPD)
245 * bytes long, since that is the value known at compile time. If
246 * hdr->cfg_region_size is bigger than that, not all UPD defaults will
247 * be copied, so it'll contain random data at the end, so we just call
248 * die() in that case. If hdr->cfg_region_size is smaller than that,
249 * there's a mismatch between the FSP and the header, but since it will
250 * copy the full UPD defaults to the buffer, we try to continue and
251 * hope that there was no incompatible change in the UPDs.
252 */
253 if (hdr->cfg_region_size > sizeof(FSPM_UPD))
254 die("FSP-M UPD size is larger than FSPM_UPD struct size.\n");
255 if (hdr->cfg_region_size < sizeof(FSPM_UPD))
256 printk(BIOS_ERR, "FSP-M UPD size is smaller than FSPM_UPD struct size. "
257 "Check if the FSP binary matches the FSP headers.\n");
258
Felix Held88995982021-01-28 22:43:52 +0100259 fsp_verify_upd_header_signature(upd->FspUpdHeader.Signature, FSPM_UPD_SIGNATURE);
Andrey Petrov465fc132016-02-25 14:16:33 -0800260
261 /* Copy the default values from the UPD area */
262 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
263
Aaron Durbin02e504c2016-07-18 11:53:10 -0500264 arch_upd = &fspm_upd.FspmArchUpd;
265
Aaron Durbin27928682016-07-15 22:32:28 -0500266 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500267 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500268
Jonathan Zhangce0e2a02020-09-14 16:27:13 -0700269 /*
270 * If ACPI APEI BERT region size is defined, reserve memory for it.
271 * +------------------------+ range_entry_top(tolum)
272 * | Other reserved regions |
273 * | APEI BERT region |
274 * +------------------------+ cbmem_top()
275 * | CBMEM IMD ROOT |
276 * | CBMEM IMD SMALL |
277 * +------------------------+ range_entry_base(tolum), TOLUM
278 * | CBMEM FSP MEMORY |
279 * | Other CBMEM regions... |
280 */
281 if (CONFIG(ACPI_BERT))
282 arch_upd->BootLoaderTolumSize += CONFIG_ACPI_BERT_SIZE;
283
Aaron Durbinb4302502016-07-17 17:04:37 -0500284 /* Fill common settings on behalf of chipset. */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500285 if (fsp_fill_common_arch_params(arch_upd, s3wake, fsp_version,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500286 memmap) != CB_SUCCESS)
Keith Shortbb41aba2019-05-16 14:07:43 -0600287 die_with_post_code(POST_INVALID_VENDOR_BINARY,
288 "FSPM_ARCH_UPD not found!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -0500289
Andrey Petrov465fc132016-02-25 14:16:33 -0800290 /* Give SoC and mainboard a chance to update the UPD */
Aaron Durbina3cecb22017-04-25 21:58:10 -0500291 platform_fsp_memory_init_params_cb(&fspm_upd, fsp_version);
Andrey Petrov465fc132016-02-25 14:16:33 -0800292
Furquan Shaikhdbce8ba2020-06-05 19:17:00 -0700293 /*
294 * For S3 resume case, if valid mrc cache data is not found or
295 * RECOVERY_MRC_CACHE hash verification fails, the S3 data
296 * pointer would be null and S3 resume fails with fsp-m
297 * returning error. Invoking a reset here saves time.
298 */
299 if (s3wake && !arch_upd->NvsBufferPtr)
300 /* FIXME: A "system" reset is likely enough: */
301 full_reset();
302
Julius Wernercd49cce2019-03-05 16:53:33 -0800303 if (CONFIG(MMA))
Pratik Prajapatiffc934d2016-11-18 14:36:34 -0800304 setup_mma(&fspm_upd.FspmConfig);
305
Furquan Shaikh585210a2018-10-16 11:54:37 -0700306 post_code(POST_MEM_PREINIT_PREP_END);
307
Andrey Petrov465fc132016-02-25 14:16:33 -0800308 /* Call FspMemoryInit */
Patrick Rudolph31218a42020-11-30 15:50:06 +0100309 fsp_raminit = (void *)(uintptr_t)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700310 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800311
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700312 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800313 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Patrick Rudolph31218a42020-11-30 15:50:06 +0100314 if (ENV_X86_64 && CONFIG(PLATFORM_USES_FSP2_X86_32))
Patrick Rudolph40beb362020-12-01 10:08:38 +0100315 status = protected_mode_call_2arg(fsp_raminit,
316 (uintptr_t)&fspm_upd,
317 (uintptr_t)fsp_get_hob_list_ptr());
318 else
319 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
320
Subrata Banik0755ab92017-07-12 15:31:06 +0530321 post_code(POST_FSP_MEMORY_EXIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800322 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
323
Lee Leahy9671faa2016-07-24 18:18:52 -0700324 /* Handle any errors returned by FspMemoryInit */
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500325 fsp_handle_reset(status);
Lee Leahy9671faa2016-07-24 18:18:52 -0700326 if (status != FSP_SUCCESS) {
Lee Leahyb20d4ba2016-07-31 16:49:28 -0700327 printk(BIOS_CRIT, "FspMemoryInit returned 0x%08x\n", status);
Keith Short24302632019-05-16 14:08:31 -0600328 die_with_post_code(POST_RAM_FAILURE,
329 "FspMemoryInit returned an error!\n");
Lee Leahy9671faa2016-07-24 18:18:52 -0700330 }
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500331
Aaron Durbina3cecb22017-04-25 21:58:10 -0500332 do_fsp_post_memory_init(s3wake, fsp_version);
Matthew Garrett78b58a42018-07-28 16:53:16 -0700333
334 /*
335 * fsp_debug_after_memory_init() checks whether the end of the tolum
336 * region is the same as the top of cbmem, so must be called here
337 * after cbmem has been initialised in do_fsp_post_memory_init().
338 */
339 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800340}
341
Julius Werner8205ce62021-03-10 17:25:01 -0800342static void *fspm_allocator(void *arg, size_t size, const union cbfs_mdata *unused)
Aaron Durbind04639b2016-07-17 23:23:59 -0500343{
Julius Werner8205ce62021-03-10 17:25:01 -0800344 const struct fsp_load_descriptor *fspld = arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600345 struct fspm_context *context = fspld->arg;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600346 struct memranges *memmap = &context->memmap;
Aaron Durbind04639b2016-07-17 23:23:59 -0500347
Aaron Durbinecbfa992020-05-15 17:01:58 -0600348 /* Non XIP FSP-M uses FSP-M address */
Julius Werner8205ce62021-03-10 17:25:01 -0800349 uintptr_t fspm_begin = (uintptr_t)CONFIG_FSP_M_ADDR;
350 uintptr_t fspm_end = fspm_begin + size;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600351
352 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) != CB_SUCCESS)
Julius Werner8205ce62021-03-10 17:25:01 -0800353 return NULL;
Aaron Durbinecbfa992020-05-15 17:01:58 -0600354
Julius Werner8205ce62021-03-10 17:25:01 -0800355 return (void *)fspm_begin;
Aaron Durbind04639b2016-07-17 23:23:59 -0500356}
357
Lee Leahy9671faa2016-07-24 18:18:52 -0700358void fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800359{
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600360 struct range_entry prog_ranges[2];
Aaron Durbinecbfa992020-05-15 17:01:58 -0600361 struct fspm_context context;
362 struct fsp_load_descriptor fspld = {
363 .fsp_prog = PROG_INIT(PROG_REFCODE, CONFIG_FSP_M_CBFS),
Aaron Durbinecbfa992020-05-15 17:01:58 -0600364 .arg = &context,
365 };
366 struct fsp_header *hdr = &context.header;
367 struct memranges *memmap = &context.memmap;
Andrey Petrov465fc132016-02-25 14:16:33 -0800368
Julius Werner8205ce62021-03-10 17:25:01 -0800369 /* For FSP-M XIP we leave alloc NULL to get a direct mapping to flash. */
370 if (!CONFIG(FSP_M_XIP))
371 fspld.alloc = fspm_allocator;
372
Kyösti Mälkki7f50afb2019-09-11 17:12:26 +0300373 elog_boot_notify(s3wake);
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700374
Aaron Durbin02e504c2016-07-18 11:53:10 -0500375 /* Build up memory map of romstage address space including CAR. */
Aaron Durbinecbfa992020-05-15 17:01:58 -0600376 memranges_init_empty(memmap, &prog_ranges[0], ARRAY_SIZE(prog_ranges));
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600377 if (ENV_CACHE_AS_RAM)
Aaron Durbinecbfa992020-05-15 17:01:58 -0600378 memranges_insert(memmap, (uintptr_t)_car_region_start,
Marshall Dawsone3aa4242019-10-16 21:53:21 -0600379 _car_unallocated_start - _car_region_start, 0);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600380 memranges_insert(memmap, (uintptr_t)_program, REGION_SIZE(program), 0);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500381
Martin Roth146508d2021-04-30 16:45:08 -0600382 timestamp_add_now(TS_FSP_MEMORY_INIT_LOAD);
Aaron Durbinecbfa992020-05-15 17:01:58 -0600383 if (fsp_load_component(&fspld, hdr) != CB_SUCCESS)
384 die("FSPM not available or failed to load!\n");
Andrey Petrov465fc132016-02-25 14:16:33 -0800385
Julius Werner8205ce62021-03-10 17:25:01 -0800386 if (CONFIG(FSP_M_XIP) && (uintptr_t)prog_start(&fspld.fsp_prog) != hdr->image_base)
387 die("FSPM XIP base does not match: %p vs %p\n",
388 (void *)(uintptr_t)hdr->image_base, prog_start(&fspld.fsp_prog));
389
Kyösti Mälkki216db612019-09-11 09:57:14 +0300390 timestamp_add_now(TS_BEFORE_INITRAM);
391
Aaron Durbinecbfa992020-05-15 17:01:58 -0600392 do_fsp_memory_init(&context, s3wake);
Kyösti Mälkki0889e932019-08-18 07:40:43 +0300393
394 timestamp_add_now(TS_AFTER_INITRAM);
Andrey Petrov465fc132016-02-25 14:16:33 -0800395}