blob: 497a5dee47d4484fc5409b1d948209c175ee7243 [file] [log] [blame]
Andrey Petrov465fc132016-02-25 14:16:33 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015 Intel Corp.
5 * (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>
20#include <fsp/api.h>
21#include <fsp/util.h>
22#include <memrange.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050023#include <program_loading.h>
Aaron Durbinb4302502016-07-17 17:04:37 -050024#include <reset.h>
25#include <romstage_handoff.h>
26#include <soc/intel/common/mrc_cache.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080027#include <string.h>
Aaron Durbind04639b2016-07-17 23:23:59 -050028#include <symbols.h>
Andrey Petrov465fc132016-02-25 14:16:33 -080029#include <timestamp.h>
30
31typedef asmlinkage enum fsp_status (*fsp_memory_init_fn)
32 (void *raminit_upd, void **hob_list);
33
Aaron Durbinb4302502016-07-17 17:04:37 -050034static enum fsp_status do_fsp_post_memory_init(void *hob_list_ptr, bool s3wake)
35{
36 struct range_entry fsp_mem;
37 size_t mrc_data_size;
38 const void *mrc_data;
39 struct romstage_handoff *handoff;
40
41 fsp_find_reserved_memory(&fsp_mem, hob_list_ptr);
42
43 /* initialize cbmem by adding FSP reserved memory first thing */
44 if (!s3wake) {
45 cbmem_initialize_empty_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
46 range_entry_size(&fsp_mem));
47 } else if (cbmem_initialize_id_size(CBMEM_ID_FSP_RESERVED_MEMORY,
48 range_entry_size(&fsp_mem))) {
49 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)) {
50 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
51 /* Failed S3 resume, reset to come up cleanly */
52 hard_reset();
53 }
54 }
55
56 /* make sure FSP memory is reserved in cbmem */
57 if (range_entry_base(&fsp_mem) !=
58 (uintptr_t)cbmem_find(CBMEM_ID_FSP_RESERVED_MEMORY))
59 die("Failed to accommodate FSP reserved memory request");
60
61 /* Now that CBMEM is up, save the list so ramstage can use it */
62 fsp_save_hob_list(hob_list_ptr);
63
64 /* Save MRC Data to CBMEM */
65 if (IS_ENABLED(CONFIG_CACHE_MRC_SETTINGS) && !s3wake) {
66 mrc_data = fsp_find_nv_storage_data(&mrc_data_size);
67 if (mrc_data && mrc_cache_stash_data(mrc_data, mrc_data_size) < 0)
68 printk(BIOS_ERR, "Failed to stash MRC data\n");
69 }
70
71 /* Create romstage handof information */
72 handoff = romstage_handoff_find_or_add();
73 if (handoff != NULL)
74 handoff->s3_resume = s3wake;
75 else
76 printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
77
78 return FSP_SUCCESS;
79}
80
81static void fsp_fill_common_arch_params(struct FSPM_ARCH_UPD *arch_upd,
82 bool s3wake)
83{
84 const struct mrc_saved_data *mrc_cache;
85
86 /*
87 * FSPM_UPD passed here is populated with default values provided by
88 * the blob itself. We let FSPM use top of CAR region of the size it
89 * requests.
90 * TODO: add checks to avoid overlap/conflict of CAR usage.
91 */
92 arch_upd->StackBase = _car_region_end - arch_upd->StackSize;
93
94 arch_upd->BootMode = FSP_BOOT_WITH_FULL_CONFIGURATION;
95
96 if (IS_ENABLED(CONFIG_CACHE_MRC_SETTINGS)) {
97 if (!mrc_cache_get_current_with_version(&mrc_cache, 0)) {
98 /* MRC cache found */
99 arch_upd->NvsBufferPtr = (void *)mrc_cache->data;
100 arch_upd->BootMode = s3wake ?
101 FSP_BOOT_ON_S3_RESUME:
102 FSP_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES;
103 printk(BIOS_DEBUG, "MRC cache found, size %x bootmode:%d\n",
104 mrc_cache->size, arch_upd->BootMode);
105 } else
106 printk(BIOS_DEBUG, "MRC cache was not found\n");
107 }
108}
109
110static enum fsp_status do_fsp_memory_init(struct fsp_header *hdr, bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800111{
112 enum fsp_status status;
113 fsp_memory_init_fn fsp_raminit;
114 struct FSPM_UPD fspm_upd, *upd;
Aaron Durbinb4302502016-07-17 17:04:37 -0500115 void *hob_list_ptr;
Andrey Petrov465fc132016-02-25 14:16:33 -0800116
117 post_code(0x34);
118
119 upd = (struct FSPM_UPD *)(hdr->cfg_region_offset + hdr->image_base);
120
121 if (upd->FspUpdHeader.Signature != FSPM_UPD_SIGNATURE) {
122 printk(BIOS_ERR, "Invalid FSPM signature\n");
123 return FSP_INCOMPATIBLE_VERSION;
124 }
125
126 /* Copy the default values from the UPD area */
127 memcpy(&fspm_upd, upd, sizeof(fspm_upd));
128
Aaron Durbin27928682016-07-15 22:32:28 -0500129 /* Reserve enough memory under TOLUD to save CBMEM header */
130 fspm_upd.FspmArchUpd.BootLoaderTolumSize = cbmem_overhead_size();
131
Aaron Durbinb4302502016-07-17 17:04:37 -0500132 /* Fill common settings on behalf of chipset. */
133 fsp_fill_common_arch_params(&fspm_upd.FspmArchUpd, s3wake);
134
Andrey Petrov465fc132016-02-25 14:16:33 -0800135 /* Give SoC and mainboard a chance to update the UPD */
136 platform_fsp_memory_init_params_cb(&fspm_upd);
137
138 /* Call FspMemoryInit */
139 fsp_raminit = (void *)(hdr->image_base + hdr->memory_init_entry_offset);
140 printk(BIOS_DEBUG, "Calling FspMemoryInit: 0x%p\n", fsp_raminit);
141 printk(BIOS_SPEW, "\t%p: raminit_upd\n", &fspm_upd);
Aaron Durbinb4302502016-07-17 17:04:37 -0500142 printk(BIOS_SPEW, "\t%p: hob_list ptr\n", &hob_list_ptr);
Andrey Petrov465fc132016-02-25 14:16:33 -0800143
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700144 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800145 timestamp_add_now(TS_FSP_MEMORY_INIT_START);
Aaron Durbinb4302502016-07-17 17:04:37 -0500146 status = fsp_raminit(&fspm_upd, &hob_list_ptr);
Alexandru Gagniucc4ea8f72016-05-23 12:16:58 -0700147 post_code(POST_FSP_MEMORY_INIT);
Andrey Petrov465fc132016-02-25 14:16:33 -0800148 timestamp_add_now(TS_FSP_MEMORY_INIT_END);
149
150 printk(BIOS_DEBUG, "FspMemoryInit returned 0x%08x\n", status);
151
Aaron Durbinb4302502016-07-17 17:04:37 -0500152 /* TODO: Is this the only thing that can happen? */
153 if (status != FSP_SUCCESS)
154 return status;
155
156 return do_fsp_post_memory_init(hob_list_ptr, s3wake);
Andrey Petrov465fc132016-02-25 14:16:33 -0800157}
158
Aaron Durbind04639b2016-07-17 23:23:59 -0500159/* Load the binary into the memory specified by the info header. */
160static enum cb_err load_fspm_mem(struct fsp_header *hdr,
161 const struct region_device *rdev)
162{
163 struct memranges ranges;
164 struct range_entry freeranges[2];
165 const struct range_entry *r;
166 uintptr_t fspm_begin;
167 uintptr_t fspm_end;
168
169 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
170 return CB_ERR;
171
172 fspm_begin = hdr->image_base;
173 fspm_end = fspm_begin + hdr->image_size;
174
175 /* Build up memory map of romstage address space including CAR. */
176 memranges_init_empty(&ranges, &freeranges[0], ARRAY_SIZE(freeranges));
177 memranges_insert(&ranges, (uintptr_t)_car_region_start,
178 _car_relocatable_data_end - _car_region_start, 0);
179 memranges_insert(&ranges, (uintptr_t)_program, _program_size, 0);
180 memranges_each_entry(r, &ranges) {
181 if (fspm_end <= range_entry_base(r))
182 continue;
183 if (fspm_begin >= range_entry_end(r))
184 continue;
185 printk(BIOS_ERR, "FSPM overlaps currently running program.\n");
186 return CB_ERR;
187 }
188
189 /* Load binary into memory at provided address. */
190 if (rdev_readat(rdev, (void *)fspm_begin, 0, fspm_end - fspm_begin) < 0)
191 return CB_ERR;
192
193 return CB_SUCCESS;
194}
195
196/* Handle the case when FSPM is running XIP. */
197static enum cb_err load_fspm_xip(struct fsp_header *hdr,
198 const struct region_device *rdev)
199{
200 void *base;
201
202 if (fsp_validate_component(hdr, rdev) != CB_SUCCESS)
203 return CB_ERR;
204
205 base = rdev_mmap_full(rdev);
206 if ((uintptr_t)base != hdr->image_base) {
207 printk(BIOS_ERR, "FSPM XIP base does not match: %p vs %p\n",
208 (void *)(uintptr_t)hdr->image_base, base);
209 return CB_ERR;
210 }
211
212 /*
213 * Since the component is XIP it's already in the address space. Thus,
214 * there's no need to rdev_munmap().
215 */
216 return CB_SUCCESS;
217}
218
219enum fsp_status fsp_memory_init(bool s3wake)
Andrey Petrov465fc132016-02-25 14:16:33 -0800220{
221 struct fsp_header hdr;
Aaron Durbind04639b2016-07-17 23:23:59 -0500222 enum cb_err status;
223 struct cbfsf file_desc;
224 struct region_device file_data;
225 const char *name = CONFIG_FSP_M_CBFS;
Andrey Petrov465fc132016-02-25 14:16:33 -0800226
Aaron Durbind04639b2016-07-17 23:23:59 -0500227 if (cbfs_boot_locate(&file_desc, name, NULL)) {
228 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
Andrey Petrov465fc132016-02-25 14:16:33 -0800229 return FSP_NOT_FOUND;
Aaron Durbind04639b2016-07-17 23:23:59 -0500230 }
231
232 cbfs_file_data(&file_data, &file_desc);
233
234 if (IS_ENABLED(CONFIG_NO_XIP_EARLY_STAGES))
235 status = load_fspm_mem(&hdr, &file_data);
236 else
237 status = load_fspm_xip(&hdr, &file_data);
238
239 if (status != CB_SUCCESS) {
240 printk(BIOS_ERR, "Loading FSPM failed.\n");
241 return FSP_NOT_FOUND;
242 }
243
244 /* Signal that FSP component has been loaded. */
245 prog_segment_loaded(hdr.image_base, hdr.image_size, SEG_FINAL);
Andrey Petrov465fc132016-02-25 14:16:33 -0800246
Aaron Durbinb4302502016-07-17 17:04:37 -0500247 return do_fsp_memory_init(&hdr, s3wake);
Andrey Petrov465fc132016-02-25 14:16:33 -0800248}