Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 1 | /* |
| 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 Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <arch/acpi.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 22 | #include <cbmem.h> |
| 23 | #include <console/console.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 24 | #include <console/streams.h> |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 25 | #include <cpu/x86/tsc.h> |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 26 | #include <program_loading.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 27 | #include <rmodule.h> |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 28 | #include <ramstage_cache.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 29 | #if IS_ENABLED(CONFIG_CHROMEOS) |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 30 | #include <vendorcode/google/chromeos/vboot_handoff.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 31 | #endif |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 32 | |
Julius Werner | 18ea2d3 | 2014-10-07 16:42:17 -0700 | [diff] [blame^] | 33 | #include <soc/ramstage.h> |
| 34 | #include <soc/efi_wrapper.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 35 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 36 | static inline struct ramstage_cache *next_cache(struct ramstage_cache *c) |
| 37 | { |
| 38 | return (struct ramstage_cache *)&c->program[c->size]; |
| 39 | } |
| 40 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 41 | static void ABI_X86 send_to_console(unsigned char b) |
| 42 | { |
| 43 | console_tx_byte(b); |
| 44 | } |
| 45 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 46 | static 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 | |
| 73 | static 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 Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 88 | if (cache_size < (sizeof(*c) + prog_size(rsl->prog))) { |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 89 | 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 Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 95 | 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 Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 98 | |
| 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 Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 104 | #if IS_ENABLED(CONFIG_CHROMEOS) |
| 105 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 106 | { |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 107 | 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 Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 120 | printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n"); |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 121 | stage = (void *)(uintptr_t)fwc->address; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 122 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 123 | if (rmodule_stage_load(refcode, stage)) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 124 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 129 | #else |
| 130 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
| 131 | { |
| 132 | return -1; |
| 133 | } |
| 134 | #endif |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 135 | |
| 136 | static int load_refcode_from_cbfs(struct rmod_stage_load *refcode) |
| 137 | { |
| 138 | printk(BIOS_DEBUG, "refcode loading from cbfs.\n"); |
| 139 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 140 | if (rmodule_stage_load_from_cbfs(refcode)) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 141 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static efi_wrapper_entry_t load_reference_code(void) |
| 149 | { |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 150 | struct prog prog = { |
| 151 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 152 | }; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 153 | struct rmod_stage_load refcode = { |
| 154 | .cbmem_id = CBMEM_ID_REFCODE, |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 155 | .prog = &prog, |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Kyösti Mälkki | c3ed886 | 2014-06-19 19:50:51 +0300 | [diff] [blame] | 158 | if (acpi_is_wakeup_s3()) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 159 | return load_refcode_from_cache(); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame] | 162 | if (load_refcode_from_vboot(&refcode) || |
| 163 | load_refcode_from_cbfs(&refcode)) |
| 164 | return NULL; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 165 | |
| 166 | /* Cache loaded reference code. */ |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 167 | cache_refcode(&refcode); |
| 168 | |
Aaron Durbin | 460703b | 2015-03-27 21:17:22 -0500 | [diff] [blame] | 169 | return prog_entry(&prog); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 172 | void 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 Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 180 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 181 | entry = load_reference_code(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 182 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 183 | if (entry == NULL) |
| 184 | return; |
| 185 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 186 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 187 | |
| 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 | } |