blob: 45771e0777a3bc9128b2ee897fdd917817ab1a78 [file] [log] [blame]
Aaron Durbinae5d83e2013-10-24 10:21:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080020#include <string.h>
21#include <arch/acpi.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050022#include <cbmem.h>
23#include <console/console.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050024#include <console/streams.h>
Aaron Durbin8fa62832013-10-28 09:54:22 -050025#include <cpu/x86/tsc.h>
Aaron Durbin460703b2015-03-27 21:17:22 -050026#include <program_loading.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050027#include <rmodule.h>
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080028#include <ramstage_cache.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050029#if IS_ENABLED(CONFIG_CHROMEOS)
Aaron Durbin7f177592013-12-13 13:05:24 -080030#include <vendorcode/google/chromeos/vboot_handoff.h>
Aaron Durbin43b7db72015-03-27 20:53:30 -050031#endif
Aaron Durbinae5d83e2013-10-24 10:21:43 -050032
33#include <baytrail/ramstage.h>
34#include <baytrail/efi_wrapper.h>
35
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080036static inline struct ramstage_cache *next_cache(struct ramstage_cache *c)
37{
38 return (struct ramstage_cache *)&c->program[c->size];
39}
40
Aaron Durbinae5d83e2013-10-24 10:21:43 -050041static void ABI_X86 send_to_console(unsigned char b)
42{
43 console_tx_byte(b);
44}
45
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080046static efi_wrapper_entry_t load_refcode_from_cache(void)
47{
48 struct ramstage_cache *c;
49 long cache_size;
50
51 printk(BIOS_DEBUG, "refcode loading from cache.\n");
52
53 c = ramstage_cache_location(&cache_size);
54
55 if (!ramstage_cache_is_valid(c)) {
56 printk(BIOS_DEBUG, "Invalid ramstage cache descriptor.\n");
57 return NULL;
58 }
59
60 c = next_cache(c);
61 if (!ramstage_cache_is_valid(c)) {
62 printk(BIOS_DEBUG, "Invalid refcode cache descriptor.\n");
63 return NULL;
64 }
65
66 printk(BIOS_DEBUG, "Loading cached reference code from 0x%08x(%x)\n",
67 c->load_address, c->size);
68 memcpy((void *)c->load_address, &c->program[0], c->size);
69
70 return (efi_wrapper_entry_t)c->entry_point;
71}
72
73static void cache_refcode(const struct rmod_stage_load *rsl)
74{
75 struct ramstage_cache *c;
76 long cache_size;
77
78 c = ramstage_cache_location(&cache_size);
79
80 if (!ramstage_cache_is_valid(c)) {
81 printk(BIOS_DEBUG, "No place to cache reference code.\n");
82 return;
83 }
84
85 /* Determine how much remaining cache available. */
86 cache_size -= c->size + sizeof(*c);
87
Aaron Durbin460703b2015-03-27 21:17:22 -050088 if (cache_size < (sizeof(*c) + prog_size(rsl->prog))) {
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080089 printk(BIOS_DEBUG, "Not enough cache space for ref code.\n");
90 return;
91 }
92
93 c = next_cache(c);
94 c->magic = RAMSTAGE_CACHE_MAGIC;
Aaron Durbin460703b2015-03-27 21:17:22 -050095 c->entry_point = (uint32_t)(uintptr_t)prog_entry(rsl->prog);
96 c->load_address = (uint32_t)(uintptr_t)prog_start(rsl->prog);
97 c->size = prog_size(rsl->prog);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080098
99 printk(BIOS_DEBUG, "Caching refcode at 0x%p(%x)\n",
100 &c->program[0], c->size);
101 memcpy(&c->program[0], (void *)c->load_address, c->size);
102}
103
Aaron Durbin43b7db72015-03-27 20:53:30 -0500104#if IS_ENABLED(CONFIG_CHROMEOS)
105static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800106{
Aaron Durbin43b7db72015-03-27 20:53:30 -0500107 struct vboot_handoff *vboot_handoff;
108 const struct firmware_component *fwc;
109 struct cbfs_stage *stage;
110
111 vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
112 fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
113
114 if (vboot_handoff == NULL ||
115 vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
116 CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
117 fwc->size == 0 || fwc->address == 0)
118 return -1;
119
Aaron Durbin7f177592013-12-13 13:05:24 -0800120 printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
Aaron Durbin43b7db72015-03-27 20:53:30 -0500121 stage = (void *)(uintptr_t)fwc->address;
Aaron Durbin7f177592013-12-13 13:05:24 -0800122
Aaron Durbin460703b2015-03-27 21:17:22 -0500123 if (rmodule_stage_load(refcode, stage)) {
Aaron Durbin7f177592013-12-13 13:05:24 -0800124 printk(BIOS_DEBUG, "Error loading reference code.\n");
125 return -1;
126 }
127 return 0;
128}
Aaron Durbin43b7db72015-03-27 20:53:30 -0500129#else
130static int load_refcode_from_vboot(struct rmod_stage_load *refcode)
131{
132 return -1;
133}
134#endif
Aaron Durbin7f177592013-12-13 13:05:24 -0800135
136static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
137{
138 printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
139
Aaron Durbin460703b2015-03-27 21:17:22 -0500140 if (rmodule_stage_load_from_cbfs(refcode)) {
Aaron Durbin7f177592013-12-13 13:05:24 -0800141 printk(BIOS_DEBUG, "Error loading reference code.\n");
142 return -1;
143 }
144
145 return 0;
146}
147
148static efi_wrapper_entry_t load_reference_code(void)
149{
Aaron Durbin460703b2015-03-27 21:17:22 -0500150 struct prog prog = {
151 .name = CONFIG_CBFS_PREFIX "/refcode",
152 };
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800153 struct rmod_stage_load refcode = {
154 .cbmem_id = CBMEM_ID_REFCODE,
Aaron Durbin460703b2015-03-27 21:17:22 -0500155 .prog = &prog,
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800156 };
157
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300158 if (acpi_is_wakeup_s3()) {
Aaron Durbin7f177592013-12-13 13:05:24 -0800159 return load_refcode_from_cache();
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800160 }
161
Aaron Durbin43b7db72015-03-27 20:53:30 -0500162 if (load_refcode_from_vboot(&refcode) ||
163 load_refcode_from_cbfs(&refcode))
164 return NULL;
Aaron Durbin7f177592013-12-13 13:05:24 -0800165
166 /* Cache loaded reference code. */
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800167 cache_refcode(&refcode);
168
Aaron Durbin460703b2015-03-27 21:17:22 -0500169 return prog_entry(&prog);
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800170}
171
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500172void baytrail_run_reference_code(void)
173{
174 int ret;
175 efi_wrapper_entry_t entry;
176 struct efi_wrapper_params wrp = {
177 .version = EFI_WRAPPER_VER,
178 .console_out = send_to_console,
179 };
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500180
Aaron Durbin7f177592013-12-13 13:05:24 -0800181 entry = load_reference_code();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500182
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800183 if (entry == NULL)
184 return;
185
Aaron Durbin8fa62832013-10-28 09:54:22 -0500186 wrp.tsc_ticks_per_microsecond = tsc_freq_mhz();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500187
188 /* Call into reference code. */
189 ret = entry(&wrp);
190
191 if (ret != 0) {
192 printk(BIOS_DEBUG, "Reference code returned %d\n", ret);
193 return;
194 }
195}