blob: e55b9e5e0b904de95a567e58e50286a18431f107 [file] [log] [blame]
Andrey Petrov465fc132016-02-25 14:16:33 -08001/*
2 * This file is part of the coreboot project.
3 *
Lee Leahy47bd2d92016-07-24 18:12:16 -07004 * Copyright (C) 2015-2016 Intel Corp.
Andrey Petrov465fc132016-02-25 14:16:33 -08005 * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.)
6 * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <arch/io.h>
15#include <arch/cpu.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050016#include <arch/symbols.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050017#include <cbfs.h>
Aaron Durbin27928682016-07-15 22:32:28 -050018#include <cbmem.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080019#include <console/console.h>
Furquan Shaikh5aea5882016-07-30 18:10:05 -070020#include <elog.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080021#include <fsp/api.h>
22#include <fsp/util.h>
23#include <memrange.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050024#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050025#include <reset.h>
26#include <romstage_handoff.h>
27#include <soc/intel/common/mrc_cache.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080028#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050029#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080030#include <timestamp.h>
Furquan Shaikh0325dc62016-07-25 13:02:36 -070031#include <vboot/vboot_common.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080032
Aaron Durbinf0ec8242016-07-18 11:24:36 -050033static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -050034{
Aaron Durbinb4302502016-07-17 17:04:37 -050035 size_t mrc_data_size;
36 const void *mrc_data;
Aaron Durbinf0ec8242016-07-18 11:24:36 -050037
38 if (!IS_ENABLED(CONFIG_CACHE_MRC_SETTINGS) || s3wake)
39 return;
40
41 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
42 if (!mrc_data) {
43 printk(BIOS_ERR, "Couldn't find memory training data HOB.\n");
44 return;
45 }
46
47 /*
48 * Save MRC Data to CBMEM. By always saving the data this forces
49 * a retrain after a trip through Chrome OS recovery path. The
50 * code which saves the data to flash doesn't write if the latest
51 * training data matches this one.
52 */
53 if (mrc_cache_stash_data_with_version(mrc_data, mrc_data_size,
54 fsp_version) < 0)
55 printk(BIOS_ERR, "Failed to stash MRC data\n");
56}
57
Furquan Shaikhaf8ef2a2016-07-24 08:48:34 -070058/*
59 * On every trip to recovery, newly generated MRC data is stored with this
60 * version since it is not expected to be a legit version. This ensures that on
61 * next normal boot, memory re-training occurs and new MRC data is stored.
62 */
63#define MRC_DEAD_VERSION (0xdeaddead)
64
Lee Leahyac3b0a62016-07-27 07:40:25 -070065static enum fsp_status do_fsp_post_memory_init(bool s3wake,
66 uint32_t fsp_version)
Aaron Durbinf0ec8242016-07-18 11:24:36 -050067{
68 struct range_entry fsp_mem;
Aaron Durbinb4302502016-07-17 17:04:37 -050069 struct romstage_handoff *handoff;
70
Lee Leahyac3b0a62016-07-27 07:40:25 -070071 fsp_find_reserved_memory(&fsp_mem);
Aaron Durbinb4302502016-07-17 17:04:37 -050072
73 /* initialize cbmem by adding FSP reserved memory first thing */
74 if (!s3wake) {
75 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
76 range_entry_size(&fsp_mem));
77 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
78 range_entry_size(&fsp_mem))) {
79 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
Lee Leahyac3b0a62016-07-27 07:40:25 -070080 printk(BIOS_DEBUG,
81 "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050082 /* Failed S3 resume, reset to come up cleanly */
83 hard_reset();
84 }
85 }
86
87 /* make sure FSP memory is reserved in cbmem */
88 if (range_entry_base(&fsp_mem) !=
89 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
90 die("Failed to accommodate FSP reserved memory request");
91
92 /* Now that CBMEM is up, save the list so ramstage can use it */
Furquan Shaikh0325dc62016-07-25 13:02:36 -070093 if (vboot_recovery_mode_enabled())
Furquan Shaikhaf8ef2a2016-07-24 08:48:34 -070094 fsp_version = MRC_DEAD_VERSION;
95
Aaron Durbinf0ec8242016-07-18 11:24:36 -050096 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -050097
98 /* Create romstage handof information */
99 handoff = romstage_handoff_find_or_add();
100 if (handoff != NULL)
101 handoff->s3_resume = s3wake;
102 else
103 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
104
105 return FSP_SUCCESS;
106}
107
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500108static void fsp_fill_mrc_cache(struct FSPM_ARCH_UPD *arch_upd, bool s3wake,
109 uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -0500110{
111 const struct mrc_saved_data *mrc_cache;
112
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500113 arch_upd->NvsBufferPtr = NULL;
114
115 if (!IS_ENABLED(CONFIG_CACHE_MRC_SETTINGS))
116 return;
117
Aaron Durbin98ea6362016-07-18 11:31:53 -0500118 /* Don't use saved training data when recovery mode is enabled. */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700119 if (vboot_recovery_mode_enabled()) {
Aaron Durbin98ea6362016-07-18 11:31:53 -0500120 printk(BIOS_DEBUG, "Recovery mode. Not using MRC cache.\n");
121 return;
122 }
123
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500124 if (mrc_cache_get_current_with_version(&mrc_cache, fsp_version)) {
125 printk(BIOS_DEBUG, "MRC cache was not found\n");
126 return;
127 }
128
129 /* MRC cache found */
130 arch_upd->NvsBufferPtr = (void *)mrc_cache->data;
131 arch_upd->BootMode = s3wake ?
132 FSP_BOOT_ON_S3_RESUME:
133 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
134 printk(BIOS_DEBUG, "MRC cache found, size %x bootmode:%d\n",
135 mrc_cache->size, arch_upd->BootMode);
136}
137
Aaron Durbin02e504c2016-07-18 11:53:10 -0500138static enum cb_err check_region_overlap(const struct memranges *ranges,
139 const char *description,
140 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500141{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500142 const struct range_entry *r;
143
144 memranges_each_entry(r, ranges) {
145 if (end <= range_entry_base(r))
146 continue;
147 if (begin >= range_entry_end(r))
148 continue;
149 printk(BIOS_ERR, "'%s' overlaps currently running program: "
150 "[%p, %p)\n", description, (void *)begin, (void *)end);
151 return CB_ERR;
152 }
153
154 return CB_SUCCESS;
155}
156
157static enum cb_err fsp_fill_common_arch_params(struct FSPM_ARCH_UPD *arch_upd,
158 bool s3wake, uint32_t fsp_version,
159 const struct memranges *memmap)
160{
161 uintptr_t stack_begin;
162 uintptr_t stack_end;
163
Aaron Durbinb4302502016-07-17 17:04:37 -0500164 /*
165 * FSPM_UPD passed here is populated with default values provided by
166 * the blob itself. We let FSPM use top of CAR region of the size it
167 * requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500168 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500169 stack_end = (uintptr_t)_car_region_end;
170 stack_begin = stack_end - arch_upd->StackSize;
171
172 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
173 stack_end) != CB_SUCCESS)
174 return CB_ERR;
175
176 arch_upd->StackBase = (void *)stack_begin;
Aaron Durbinb4302502016-07-17 17:04:37 -0500177
178 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
179
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500180 fsp_fill_mrc_cache(arch_upd, s3wake, fsp_version);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500181
182 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500183}
184
Aaron Durbin02e504c2016-07-18 11:53:10 -0500185static enum fsp_status do_fsp_memory_init(struct fsp_header *hdr, bool s3wake,
186 const struct memranges *memmap)
Andrey Petrov465fc132016-02-25 14:16:33 -0800187{
188 enum fsp_status status;
189 fsp_memory_init_fn fsp_raminit;
190 struct FSPM_UPD fspm_upd, *upd;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500191 struct FSPM_ARCH_UPD *arch_upd;
Andrey Petrov465fc132016-02-25 14:16:33 -0800192
193 post_code(0x34);
194
195 upd = (struct FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
196
197 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE) {
198 printk(BIOS_ERR, "Invalid FSPM signature\n");
199 return FSP_INCOMPATIBLE_VERSION;
200 }
201
202 /* Copy the default values from the UPD area */
203 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
204
Aaron Durbin02e504c2016-07-18 11:53:10 -0500205 arch_upd = &fspm_upd.FspmArchUpd;
206
Aaron Durbin27928682016-07-15 22:32:28 -0500207 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500208 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500209
Aaron Durbinb4302502016-07-17 17:04:37 -0500210 /* Fill common settings on behalf of chipset. */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500211 if (fsp_fill_common_arch_params(arch_upd, s3wake, hdr->fsp_revision,
212 memmap) != CB_SUCCESS)
213 return FSP_NOT_FOUND;
Aaron Durbinb4302502016-07-17 17:04:37 -0500214
Andrey Petrov465fc132016-02-25 14:16:33 -0800215 /* Give SoC and mainboard a chance to update the UPD */
216 platform_fsp_memory_init_params_cb(&fspm_upd);
217
218 /* Call FspMemoryInit */
219 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700220 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800221
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700222 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800223 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700224 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700225 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800226 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
227
Lee Leahyac3b0a62016-07-27 07:40:25 -0700228 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800229
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500230 /* Handle any resets requested by FSPM. */
231 fsp_handle_reset(status);
232
Aaron Durbinb4302502016-07-17 17:04:37 -0500233 if (status != FSP_SUCCESS)
234 return status;
235
Lee Leahyac3b0a62016-07-27 07:40:25 -0700236 return do_fsp_post_memory_init(s3wake, hdr->fsp_revision);
Andrey Petrov465fc132016-02-25 14:16:33 -0800237}
238
Aaron Durbind04639b2016-07-17 23:23:59 -0500239/* Load the binary into the memory specified by the info header. */
240static enum cb_err load_fspm_mem(struct fsp_header *hdr,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500241 const struct region_device *rdev,
242 const struct memranges *memmap)
Aaron Durbind04639b2016-07-17 23:23:59 -0500243{
Aaron Durbind04639b2016-07-17 23:23:59 -0500244 uintptr_t fspm_begin;
245 uintptr_t fspm_end;
246
247 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
248 return CB_ERR;
249
250 fspm_begin = hdr->image_base;
251 fspm_end = fspm_begin + hdr->image_size;
252
Aaron Durbin02e504c2016-07-18 11:53:10 -0500253 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) !=
254 CB_SUCCESS)
Aaron Durbind04639b2016-07-17 23:23:59 -0500255 return CB_ERR;
Aaron Durbind04639b2016-07-17 23:23:59 -0500256
257 /* Load binary into memory at provided address. */
258 if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0)
259 return CB_ERR;
260
261 return CB_SUCCESS;
262}
263
264/* Handle the case when FSPM is running XIP. */
265static enum cb_err load_fspm_xip(struct fsp_header *hdr,
266 const struct region_device *rdev)
267{
268 void *base;
269
270 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
271 return CB_ERR;
272
273 base = rdev_mmap_full(rdev);
274 if ((uintptr_t)base != hdr->image_base) {
275 printk(BIOS_ERR, "FSPM XIP base does not match: %p vs %p\n",
276 (void *)(uintptr_t)hdr->image_base, base);
277 return CB_ERR;
278 }
279
280 /*
281 * Since the component is XIP it's already in the address space. Thus,
282 * there's no need to rdev_munmap().
283 */
284 return CB_SUCCESS;
285}
286
287enum fsp_status fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800288{
289 struct fsp_header hdr;
Aaron Durbind04639b2016-07-17 23:23:59 -0500290 enum cb_err status;
291 struct cbfsf file_desc;
292 struct region_device file_data;
293 const char *name = CONFIG_FSP_M_CBFS;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500294 struct memranges memmap;
295 struct range_entry freeranges[2];
Andrey Petrov465fc132016-02-25 14:16:33 -0800296
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700297 if (IS_ENABLED(CONFIG_ELOG_BOOT_COUNT) && !s3wake)
298 boot_count_increment();
299
Aaron Durbind04639b2016-07-17 23:23:59 -0500300 if (cbfs_boot_locate(&file_desc, name, NULL)) {
301 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
Andrey Petrov465fc132016-02-25 14:16:33 -0800302 return FSP_NOT_FOUND;
Aaron Durbind04639b2016-07-17 23:23:59 -0500303 }
304
305 cbfs_file_data(&file_data, &file_desc);
306
Aaron Durbin02e504c2016-07-18 11:53:10 -0500307 /* Build up memory map of romstage address space including CAR. */
308 memranges_init_empty(&memmap, &freeranges[0], ARRAY_SIZE(freeranges));
309 memranges_insert(&memmap, (uintptr_t)_car_region_start,
310 _car_relocatable_data_end - _car_region_start, 0);
311 memranges_insert(&memmap, (uintptr_t)_program, _program_size, 0);
312
Lee Leahy27cd96a2016-07-21 11:16:39 -0700313 if (!IS_ENABLED(CONFIG_FSP_M_XIP))
Aaron Durbin02e504c2016-07-18 11:53:10 -0500314 status = load_fspm_mem(&hdr, &file_data, &memmap);
Aaron Durbind04639b2016-07-17 23:23:59 -0500315 else
316 status = load_fspm_xip(&hdr, &file_data);
317
318 if (status != CB_SUCCESS) {
319 printk(BIOS_ERR, "Loading FSPM failed.\n");
320 return FSP_NOT_FOUND;
321 }
322
323 /* Signal that FSP component has been loaded. */
324 prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL);
Andrey Petrov465fc132016-02-25 14:16:33 -0800325
Aaron Durbin02e504c2016-07-18 11:53:10 -0500326 return do_fsp_memory_init(&hdr, s3wake, &memmap);
Andrey Petrov465fc132016-02-25 14:16:33 -0800327}