blob: 1e14df274675b37264ba6c779b5069ee90f6ec63 [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 Leahy52d0c682016-08-01 15:47:42 -070071 if (fsp_find_reserved_memory(&fsp_mem))
72 die("Failed to find FSP_RESERVED_MEMORY_RESOURCE_HOB!\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050073
74 /* initialize cbmem by adding FSP reserved memory first thing */
75 if (!s3wake) {
76 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
77 range_entry_size(&fsp_mem));
78 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
79 range_entry_size(&fsp_mem))) {
80 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
Lee Leahyac3b0a62016-07-27 07:40:25 -070081 printk(BIOS_DEBUG,
82 "Failed to recover CBMEM in S3 resume.\n");
Aaron Durbinb4302502016-07-17 17:04:37 -050083 /* Failed S3 resume, reset to come up cleanly */
84 hard_reset();
85 }
86 }
87
88 /* make sure FSP memory is reserved in cbmem */
89 if (range_entry_base(&fsp_mem) !=
90 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
91 die("Failed to accommodate FSP reserved memory request");
92
93 /* Now that CBMEM is up, save the list so ramstage can use it */
Furquan Shaikh0325dc62016-07-25 13:02:36 -070094 if (vboot_recovery_mode_enabled())
Furquan Shaikhaf8ef2a2016-07-24 08:48:34 -070095 fsp_version = MRC_DEAD_VERSION;
96
Aaron Durbinf0ec8242016-07-18 11:24:36 -050097 save_memory_training_data(s3wake, fsp_version);
Aaron Durbinb4302502016-07-17 17:04:37 -050098
99 /* Create romstage handof information */
100 handoff = romstage_handoff_find_or_add();
101 if (handoff != NULL)
102 handoff->s3_resume = s3wake;
103 else
104 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
105
106 return FSP_SUCCESS;
107}
108
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500109static void fsp_fill_mrc_cache(struct FSPM_ARCH_UPD *arch_upd, bool s3wake,
110 uint32_t fsp_version)
Aaron Durbinb4302502016-07-17 17:04:37 -0500111{
112 const struct mrc_saved_data *mrc_cache;
113
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500114 arch_upd->NvsBufferPtr = NULL;
115
116 if (!IS_ENABLED(CONFIG_CACHE_MRC_SETTINGS))
117 return;
118
Aaron Durbin98ea6362016-07-18 11:31:53 -0500119 /* Don't use saved training data when recovery mode is enabled. */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700120 if (vboot_recovery_mode_enabled()) {
Aaron Durbin98ea6362016-07-18 11:31:53 -0500121 printk(BIOS_DEBUG, "Recovery mode. Not using MRC cache.\n");
122 return;
123 }
124
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500125 if (mrc_cache_get_current_with_version(&mrc_cache, fsp_version)) {
126 printk(BIOS_DEBUG, "MRC cache was not found\n");
127 return;
128 }
129
130 /* MRC cache found */
131 arch_upd->NvsBufferPtr = (void *)mrc_cache->data;
132 arch_upd->BootMode = s3wake ?
133 FSP_BOOT_ON_S3_RESUME:
134 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
135 printk(BIOS_DEBUG, "MRC cache found, size %x bootmode:%d\n",
136 mrc_cache->size, arch_upd->BootMode);
137}
138
Aaron Durbin02e504c2016-07-18 11:53:10 -0500139static enum cb_err check_region_overlap(const struct memranges *ranges,
140 const char *description,
141 uintptr_t begin, uintptr_t end)
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500142{
Aaron Durbin02e504c2016-07-18 11:53:10 -0500143 const struct range_entry *r;
144
145 memranges_each_entry(r, ranges) {
146 if (end <= range_entry_base(r))
147 continue;
148 if (begin >= range_entry_end(r))
149 continue;
150 printk(BIOS_ERR, "'%s' overlaps currently running program: "
151 "[%p, %p)\n", description, (void *)begin, (void *)end);
152 return CB_ERR;
153 }
154
155 return CB_SUCCESS;
156}
157
158static enum cb_err fsp_fill_common_arch_params(struct FSPM_ARCH_UPD *arch_upd,
159 bool s3wake, uint32_t fsp_version,
160 const struct memranges *memmap)
161{
162 uintptr_t stack_begin;
163 uintptr_t stack_end;
164
Aaron Durbinb4302502016-07-17 17:04:37 -0500165 /*
166 * FSPM_UPD passed here is populated with default values provided by
167 * the blob itself. We let FSPM use top of CAR region of the size it
168 * requests.
Aaron Durbinb4302502016-07-17 17:04:37 -0500169 */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500170 stack_end = (uintptr_t)_car_region_end;
171 stack_begin = stack_end - arch_upd->StackSize;
172
173 if (check_region_overlap(memmap, "FSPM stack", stack_begin,
174 stack_end) != CB_SUCCESS)
175 return CB_ERR;
176
177 arch_upd->StackBase = (void *)stack_begin;
Aaron Durbinb4302502016-07-17 17:04:37 -0500178
179 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
180
Aaron Durbinf0ec8242016-07-18 11:24:36 -0500181 fsp_fill_mrc_cache(arch_upd, s3wake, fsp_version);
Aaron Durbin02e504c2016-07-18 11:53:10 -0500182
183 return CB_SUCCESS;
Aaron Durbinb4302502016-07-17 17:04:37 -0500184}
185
Aaron Durbin02e504c2016-07-18 11:53:10 -0500186static enum fsp_status do_fsp_memory_init(struct fsp_header *hdr, bool s3wake,
187 const struct memranges *memmap)
Andrey Petrov465fc132016-02-25 14:16:33 -0800188{
189 enum fsp_status status;
190 fsp_memory_init_fn fsp_raminit;
191 struct FSPM_UPD fspm_upd, *upd;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500192 struct FSPM_ARCH_UPD *arch_upd;
Andrey Petrov465fc132016-02-25 14:16:33 -0800193
194 post_code(0x34);
195
196 upd = (struct FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
197
198 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE) {
199 printk(BIOS_ERR, "Invalid FSPM signature\n");
200 return FSP_INCOMPATIBLE_VERSION;
201 }
202
203 /* Copy the default values from the UPD area */
204 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
205
Aaron Durbin02e504c2016-07-18 11:53:10 -0500206 arch_upd = &fspm_upd.FspmArchUpd;
207
Aaron Durbin27928682016-07-15 22:32:28 -0500208 /* Reserve enough memory under TOLUD to save CBMEM header */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500209 arch_upd->BootLoaderTolumSize = cbmem_overhead_size();
Aaron Durbin27928682016-07-15 22:32:28 -0500210
Aaron Durbinb4302502016-07-17 17:04:37 -0500211 /* Fill common settings on behalf of chipset. */
Aaron Durbin02e504c2016-07-18 11:53:10 -0500212 if (fsp_fill_common_arch_params(arch_upd, s3wake, hdr->fsp_revision,
213 memmap) != CB_SUCCESS)
214 return FSP_NOT_FOUND;
Aaron Durbinb4302502016-07-17 17:04:37 -0500215
Andrey Petrov465fc132016-02-25 14:16:33 -0800216 /* Give SoC and mainboard a chance to update the UPD */
217 platform_fsp_memory_init_params_cb(&fspm_upd);
218
219 /* Call FspMemoryInit */
220 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700221 fsp_debug_before_memory_init(fsp_raminit, upd, &fspm_upd);
Andrey Petrov465fc132016-02-25 14:16:33 -0800222
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700223 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800224 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Lee Leahyac3b0a62016-07-27 07:40:25 -0700225 status = fsp_raminit(&fspm_upd, fsp_get_hob_list_ptr());
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700226 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800227 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
228
Lee Leahyac3b0a62016-07-27 07:40:25 -0700229 fsp_debug_after_memory_init(status);
Andrey Petrov465fc132016-02-25 14:16:33 -0800230
Aaron Durbinf41f2aa2016-07-18 12:03:58 -0500231 /* Handle any resets requested by FSPM. */
232 fsp_handle_reset(status);
233
Aaron Durbinb4302502016-07-17 17:04:37 -0500234 if (status != FSP_SUCCESS)
235 return status;
236
Lee Leahyac3b0a62016-07-27 07:40:25 -0700237 return do_fsp_post_memory_init(s3wake, hdr->fsp_revision);
Andrey Petrov465fc132016-02-25 14:16:33 -0800238}
239
Aaron Durbind04639b2016-07-17 23:23:59 -0500240/* Load the binary into the memory specified by the info header. */
241static enum cb_err load_fspm_mem(struct fsp_header *hdr,
Aaron Durbin02e504c2016-07-18 11:53:10 -0500242 const struct region_device *rdev,
243 const struct memranges *memmap)
Aaron Durbind04639b2016-07-17 23:23:59 -0500244{
Aaron Durbind04639b2016-07-17 23:23:59 -0500245 uintptr_t fspm_begin;
246 uintptr_t fspm_end;
247
248 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
249 return CB_ERR;
250
251 fspm_begin = hdr->image_base;
252 fspm_end = fspm_begin + hdr->image_size;
253
Aaron Durbin02e504c2016-07-18 11:53:10 -0500254 if (check_region_overlap(memmap, "FSPM", fspm_begin, fspm_end) !=
255 CB_SUCCESS)
Aaron Durbind04639b2016-07-17 23:23:59 -0500256 return CB_ERR;
Aaron Durbind04639b2016-07-17 23:23:59 -0500257
258 /* Load binary into memory at provided address. */
259 if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0)
260 return CB_ERR;
261
262 return CB_SUCCESS;
263}
264
265/* Handle the case when FSPM is running XIP. */
266static enum cb_err load_fspm_xip(struct fsp_header *hdr,
267 const struct region_device *rdev)
268{
269 void *base;
270
271 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
272 return CB_ERR;
273
274 base = rdev_mmap_full(rdev);
275 if ((uintptr_t)base != hdr->image_base) {
276 printk(BIOS_ERR, "FSPM XIP base does not match: %p vs %p\n",
277 (void *)(uintptr_t)hdr->image_base, base);
278 return CB_ERR;
279 }
280
281 /*
282 * Since the component is XIP it's already in the address space. Thus,
283 * there's no need to rdev_munmap().
284 */
285 return CB_SUCCESS;
286}
287
288enum fsp_status fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800289{
290 struct fsp_header hdr;
Aaron Durbind04639b2016-07-17 23:23:59 -0500291 enum cb_err status;
292 struct cbfsf file_desc;
293 struct region_device file_data;
294 const char *name = CONFIG_FSP_M_CBFS;
Aaron Durbin02e504c2016-07-18 11:53:10 -0500295 struct memranges memmap;
296 struct range_entry freeranges[2];
Andrey Petrov465fc132016-02-25 14:16:33 -0800297
Furquan Shaikh5aea5882016-07-30 18:10:05 -0700298 if (IS_ENABLED(CONFIG_ELOG_BOOT_COUNT) && !s3wake)
299 boot_count_increment();
300
Aaron Durbind04639b2016-07-17 23:23:59 -0500301 if (cbfs_boot_locate(&file_desc, name, NULL)) {
302 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
Andrey Petrov465fc132016-02-25 14:16:33 -0800303 return FSP_NOT_FOUND;
Aaron Durbind04639b2016-07-17 23:23:59 -0500304 }
305
306 cbfs_file_data(&file_data, &file_desc);
307
Aaron Durbin02e504c2016-07-18 11:53:10 -0500308 /* Build up memory map of romstage address space including CAR. */
309 memranges_init_empty(&memmap, &freeranges[0], ARRAY_SIZE(freeranges));
310 memranges_insert(&memmap, (uintptr_t)_car_region_start,
311 _car_relocatable_data_end - _car_region_start, 0);
312 memranges_insert(&memmap, (uintptr_t)_program, _program_size, 0);
313
Lee Leahy27cd96a2016-07-21 11:16:39 -0700314 if (!IS_ENABLED(CONFIG_FSP_M_XIP))
Aaron Durbin02e504c2016-07-18 11:53:10 -0500315 status = load_fspm_mem(&hdr, &file_data, &memmap);
Aaron Durbind04639b2016-07-17 23:23:59 -0500316 else
317 status = load_fspm_xip(&hdr, &file_data);
318
319 if (status != CB_SUCCESS) {
320 printk(BIOS_ERR, "Loading FSPM failed.\n");
321 return FSP_NOT_FOUND;
322 }
323
324 /* Signal that FSP component has been loaded. */
325 prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL);
Andrey Petrov465fc132016-02-25 14:16:33 -0800326
Aaron Durbin02e504c2016-07-18 11:53:10 -0500327 return do_fsp_memory_init(&hdr, s3wake, &memmap);
Andrey Petrov465fc132016-02-25 14:16:33 -0800328}