blob: 019e0a5e29232176bb7a61a358450ba99302ebe8 [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 int is_s3_resume(void)
33{
34#if CONFIG_HAVE_ACPI_RESUME
35 return acpi_slp_type == 3;
36#else
37 return 0;
38#endif
39}
40
41static inline struct ramstage_cache *next_cache(struct ramstage_cache *c)
42{
43 return (struct ramstage_cache *)&c->program[c->size];
44}
45
Aaron Durbinae5d83e2013-10-24 10:21:43 -050046static void ABI_X86 send_to_console(unsigned char b)
47{
48 console_tx_byte(b);
49}
50
Aaron Durbin63fcb4a2013-12-12 10:29:48 -080051static efi_wrapper_entry_t load_refcode_from_cache(void)
52{
53 struct ramstage_cache *c;
54 long cache_size;
55
56 printk(BIOS_DEBUG, "refcode loading from cache.\n");
57
58 c = ramstage_cache_location(&cache_size);
59
60 if (!ramstage_cache_is_valid(c)) {
61 printk(BIOS_DEBUG, "Invalid ramstage cache descriptor.\n");
62 return NULL;
63 }
64
65 c = next_cache(c);
66 if (!ramstage_cache_is_valid(c)) {
67 printk(BIOS_DEBUG, "Invalid refcode cache descriptor.\n");
68 return NULL;
69 }
70
71 printk(BIOS_DEBUG, "Loading cached reference code from 0x%08x(%x)\n",
72 c->load_address, c->size);
73 memcpy((void *)c->load_address, &c->program[0], c->size);
74
75 return (efi_wrapper_entry_t)c->entry_point;
76}
77
78static void cache_refcode(const struct rmod_stage_load *rsl)
79{
80 struct ramstage_cache *c;
81 long cache_size;
82
83 c = ramstage_cache_location(&cache_size);
84
85 if (!ramstage_cache_is_valid(c)) {
86 printk(BIOS_DEBUG, "No place to cache reference code.\n");
87 return;
88 }
89
90 /* Determine how much remaining cache available. */
91 cache_size -= c->size + sizeof(*c);
92
93 if (cache_size < (sizeof(*c) + cbmem_entry_size(rsl->cbmem_entry))) {
94 printk(BIOS_DEBUG, "Not enough cache space for ref code.\n");
95 return;
96 }
97
98 c = next_cache(c);
99 c->magic = RAMSTAGE_CACHE_MAGIC;
100 c->entry_point = (uint32_t)rsl->entry;
101 c->load_address = (uint32_t)cbmem_entry_start(rsl->cbmem_entry);
102 c->size = cbmem_entry_size(rsl->cbmem_entry);;
103
104 printk(BIOS_DEBUG, "Caching refcode at 0x%p(%x)\n",
105 &c->program[0], c->size);
106 memcpy(&c->program[0], (void *)c->load_address, c->size);
107}
108
Aaron Durbin7f177592013-12-13 13:05:24 -0800109static int load_refcode_from_vboot(struct rmod_stage_load *refcode,
110 struct cbfs_stage *stage)
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800111{
Aaron Durbin7f177592013-12-13 13:05:24 -0800112 printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n");
113
114 if (rmodule_stage_load(refcode, stage) || refcode->entry == NULL) {
115 printk(BIOS_DEBUG, "Error loading reference code.\n");
116 return -1;
117 }
118 return 0;
119}
120
121static int load_refcode_from_cbfs(struct rmod_stage_load *refcode)
122{
123 printk(BIOS_DEBUG, "refcode loading from cbfs.\n");
124
125 if (rmodule_stage_load_from_cbfs(refcode) || refcode->entry == NULL) {
126 printk(BIOS_DEBUG, "Error loading reference code.\n");
127 return -1;
128 }
129
130 return 0;
131}
132
133static efi_wrapper_entry_t load_reference_code(void)
134{
135 struct vboot_handoff *vboot_handoff;
136 const struct firmware_component *fwc;
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800137 struct rmod_stage_load refcode = {
138 .cbmem_id = CBMEM_ID_REFCODE,
139 .name = CONFIG_CBFS_PREFIX "/refcode",
140 };
Aaron Durbin7f177592013-12-13 13:05:24 -0800141 int ret;
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800142
Aaron Durbin7f177592013-12-13 13:05:24 -0800143 if (is_s3_resume()) {
144 return load_refcode_from_cache();
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800145 }
146
Aaron Durbin7f177592013-12-13 13:05:24 -0800147 vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF);
148 fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX];
149
150 if (vboot_handoff == NULL ||
151 vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY ||
152 CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS ||
153 fwc->size == 0 || fwc->address == 0) {
154 ret = load_refcode_from_cbfs(&refcode);
155 } else {
156 ret = load_refcode_from_vboot(&refcode, (void *)fwc->address);
157
158 if (ret < 0)
159 ret = load_refcode_from_cbfs(&refcode);
160 }
161
162 if (ret < 0)
163 return NULL;
164
165 /* Cache loaded reference code. */
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800166 cache_refcode(&refcode);
167
168 return refcode.entry;
169}
170
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500171void baytrail_run_reference_code(void)
172{
173 int ret;
174 efi_wrapper_entry_t entry;
175 struct efi_wrapper_params wrp = {
176 .version = EFI_WRAPPER_VER,
177 .console_out = send_to_console,
178 };
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500179
Aaron Durbin7f177592013-12-13 13:05:24 -0800180 entry = load_reference_code();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500181
Aaron Durbin63fcb4a2013-12-12 10:29:48 -0800182 if (entry == NULL)
183 return;
184
Aaron Durbin8fa62832013-10-28 09:54:22 -0500185 wrp.tsc_ticks_per_microsecond = tsc_freq_mhz();
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500186
187 /* Call into reference code. */
188 ret = entry(&wrp);
189
190 if (ret != 0) {
191 printk(BIOS_DEBUG, "Reference code returned %d\n", ret);
192 return;
193 }
194}
195