blob: 12e3ed47083e46a5794a6205bdd208cfa714b5aa [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 Durbin8fa62832013-10-28 09:54:22 -050024#include <cpu/x86/tsc.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050025#include <rmodule.h>
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080026#include <ramstage_cache.h>
Aaron Durbin7f177592013-12-13 13:05:24 -080027#include <vendorcode/google/chromeos/vboot_handoff.h>
Aaron Durbinae5d83e2013-10-24 10:21:43 -050028
29#include <baytrail/ramstage.h>
30#include <baytrail/efi_wrapper.h>
31
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080032static inline struct ramstage_cache *next_cache(struct ramstage_cache *c)
33{
34 return (struct ramstage_cache *)&c->program[c->size];
35}
36
Aaron Durbinae5d83e2013-10-24 10:21:43 -050037static void ABI_X86 send_to_console(unsigned char b)
38{
39 console_tx_byte(b);
40}
41
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080042static efi_wrapper_entry_t load_refcode_from_cache(void)
43{
44 struct ramstage_cache *c;
45 long cache_size;
46
47 printk(BIOS_DEBUG, "refcode loading from cache.\n");
48
49 c = ramstage_cache_location(&cache_size);
50
51 if (!ramstage_cache_is_valid(c)) {
52 printk(BIOS_DEBUG, "Invalid ramstage cache descriptor.\n");
53 return NULL;
54 }
55
56 c = next_cache(c);
57 if (!ramstage_cache_is_valid(c)) {
58 printk(BIOS_DEBUG, "Invalid refcode cache descriptor.\n");
59 return NULL;
60 }
61
62 printk(BIOS_DEBUG, "Loading cached reference code from 0x%08x(%x)\n",
63 c->load_address, c->size);
64 memcpy((void *)c->load_address, &c->program[0], c->size);
65
66 return (efi_wrapper_entry_t)c->entry_point;
67}
68
69static void cache_refcode(const struct rmod_stage_load *rsl)
70{
71 struct ramstage_cache *c;
72 long cache_size;
73
74 c = ramstage_cache_location(&cache_size);
75
76 if (!ramstage_cache_is_valid(c)) {
77 printk(BIOS_DEBUG, "No place to cache reference code.\n");
78 return;
79 }
80
81 /* Determine how much remaining cache available. */
82 cache_size -= c->size + sizeof(*c);
83
84 if (cache_size < (sizeof(*c) + cbmem_entry_size(rsl->cbmem_entry))) {
85 printk(BIOS_DEBUG, "Not enough cache space for ref code.\n");
86 return;
87 }
88
89 c = next_cache(c);
90 c->magic = RAMSTAGE_CACHE_MAGIC;
91 c->entry_point = (uint32_t)rsl->entry;
92 c->load_address = (uint32_t)cbmem_entry_start(rsl->cbmem_entry);
93 c->size = cbmem_entry_size(rsl->cbmem_entry);;
94
95 printk(BIOS_DEBUG, "Caching refcode at 0x%p(%x)\n",
96 &c->program[0], c->size);
97 memcpy(&c->program[0], (void *)c->load_address, c->size);
98}
99
Aaron Durbin7f177592013-12-13 13:05:24 -0800100static int load_refcode_from_vboot(struct rmod_stage_load *refcode,
101 struct cbfs_stage *stage)
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800102{
Aaron Durbin7f177592013-12-13 13:05:24 -0800103 printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
104
105 if (rmodule_stage_load(refcode, stage) || refcode->entry == NULL) {
106 printk(BIOS_DEBUG, "Error loading reference code.\n");
107 return -1;
108 }
109 return 0;
110}
111
112static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
113{
114 printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
115
116 if (rmodule_stage_load_from_cbfs(refcode) || refcode->entry == NULL) {
117 printk(BIOS_DEBUG, "Error loading reference code.\n");
118 return -1;
119 }
120
121 return 0;
122}
123
124static efi_wrapper_entry_t load_reference_code(void)
125{
126 struct vboot_handoff *vboot_handoff;
127 const struct firmware_component *fwc;
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800128 struct rmod_stage_load refcode = {
129 .cbmem_id = CBMEM_ID_REFCODE,
130 .name = CONFIG_CBFS_PREFIX "/refcode",
131 };
Aaron Durbin7f177592013-12-13 13:05:24 -0800132 int ret;
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800133
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300134 if (acpi_is_wakeup_s3()) {
Aaron Durbin7f177592013-12-13 13:05:24 -0800135 return load_refcode_from_cache();
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800136 }
137
Aaron Durbin7f177592013-12-13 13:05:24 -0800138 vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
139 fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
140
141 if (vboot_handoff == NULL ||
142 vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
143 CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
144 fwc->size == 0 || fwc->address == 0) {
145 ret = load_refcode_from_cbfs(&refcode);
146 } else {
147 ret = load_refcode_from_vboot(&refcode, (void *)fwc->address);
148
149 if (ret < 0)
150 ret = load_refcode_from_cbfs(&refcode);
151 }
152
153 if (ret < 0)
154 return NULL;
155
156 /* Cache loaded reference code. */
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800157 cache_refcode(&refcode);
158
159 return refcode.entry;
160}
161
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500162void baytrail_run_reference_code(void)
163{
164 int ret;
165 efi_wrapper_entry_t entry;
166 struct efi_wrapper_params wrp = {
167 .version = EFI_WRAPPER_VER,
168 .console_out = send_to_console,
169 };
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500170
Aaron Durbin7f177592013-12-13 13:05:24 -0800171 entry = load_reference_code();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500172
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800173 if (entry == NULL)
174 return;
175
Aaron Durbin8fa62832013-10-28 09:54:22 -0500176 wrp.tsc_ticks_per_microsecond = tsc_freq_mhz();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500177
178 /* Call into reference code. */
179 ret = entry(&wrp);
180
181 if (ret != 0) {
182 printk(BIOS_DEBUG, "Reference code returned %d\n", ret);
183 return;
184 }
185}
186