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 | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 26 | #include <rmodule.h> |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 27 | #include <ramstage_cache.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 28 | #if IS_ENABLED(CONFIG_CHROMEOS) |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 29 | #include <vendorcode/google/chromeos/vboot_handoff.h> |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 30 | #endif |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 31 | |
| 32 | #include <baytrail/ramstage.h> |
| 33 | #include <baytrail/efi_wrapper.h> |
| 34 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 35 | static inline struct ramstage_cache *next_cache(struct ramstage_cache *c) |
| 36 | { |
| 37 | return (struct ramstage_cache *)&c->program[c->size]; |
| 38 | } |
| 39 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 40 | static void ABI_X86 send_to_console(unsigned char b) |
| 41 | { |
| 42 | console_tx_byte(b); |
| 43 | } |
| 44 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 45 | static efi_wrapper_entry_t load_refcode_from_cache(void) |
| 46 | { |
| 47 | struct ramstage_cache *c; |
| 48 | long cache_size; |
| 49 | |
| 50 | printk(BIOS_DEBUG, "refcode loading from cache.\n"); |
| 51 | |
| 52 | c = ramstage_cache_location(&cache_size); |
| 53 | |
| 54 | if (!ramstage_cache_is_valid(c)) { |
| 55 | printk(BIOS_DEBUG, "Invalid ramstage cache descriptor.\n"); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | c = next_cache(c); |
| 60 | if (!ramstage_cache_is_valid(c)) { |
| 61 | printk(BIOS_DEBUG, "Invalid refcode cache descriptor.\n"); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | printk(BIOS_DEBUG, "Loading cached reference code from 0x%08x(%x)\n", |
| 66 | c->load_address, c->size); |
| 67 | memcpy((void *)c->load_address, &c->program[0], c->size); |
| 68 | |
| 69 | return (efi_wrapper_entry_t)c->entry_point; |
| 70 | } |
| 71 | |
| 72 | static void cache_refcode(const struct rmod_stage_load *rsl) |
| 73 | { |
| 74 | struct ramstage_cache *c; |
| 75 | long cache_size; |
| 76 | |
| 77 | c = ramstage_cache_location(&cache_size); |
| 78 | |
| 79 | if (!ramstage_cache_is_valid(c)) { |
| 80 | printk(BIOS_DEBUG, "No place to cache reference code.\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | /* Determine how much remaining cache available. */ |
| 85 | cache_size -= c->size + sizeof(*c); |
| 86 | |
| 87 | if (cache_size < (sizeof(*c) + cbmem_entry_size(rsl->cbmem_entry))) { |
| 88 | printk(BIOS_DEBUG, "Not enough cache space for ref code.\n"); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | c = next_cache(c); |
| 93 | c->magic = RAMSTAGE_CACHE_MAGIC; |
| 94 | c->entry_point = (uint32_t)rsl->entry; |
| 95 | c->load_address = (uint32_t)cbmem_entry_start(rsl->cbmem_entry); |
Edward O'Callaghan | 48b6b97 | 2014-11-09 12:06:19 +1100 | [diff] [blame] | 96 | c->size = cbmem_entry_size(rsl->cbmem_entry); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 97 | |
| 98 | printk(BIOS_DEBUG, "Caching refcode at 0x%p(%x)\n", |
| 99 | &c->program[0], c->size); |
| 100 | memcpy(&c->program[0], (void *)c->load_address, c->size); |
| 101 | } |
| 102 | |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 103 | #if IS_ENABLED(CONFIG_CHROMEOS) |
| 104 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 105 | { |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 106 | struct vboot_handoff *vboot_handoff; |
| 107 | const struct firmware_component *fwc; |
| 108 | struct cbfs_stage *stage; |
| 109 | |
| 110 | vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF); |
| 111 | fwc = &vboot_handoff->components[CONFIG_VBOOT_REFCODE_INDEX]; |
| 112 | |
| 113 | if (vboot_handoff == NULL || |
| 114 | vboot_handoff->selected_firmware == VB_SELECT_FIRMWARE_READONLY || |
| 115 | CONFIG_VBOOT_REFCODE_INDEX >= MAX_PARSED_FW_COMPONENTS || |
| 116 | fwc->size == 0 || fwc->address == 0) |
| 117 | return -1; |
| 118 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 119 | printk(BIOS_DEBUG, "refcode loading from vboot rw area.\n"); |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 120 | stage = (void *)(uintptr_t)fwc->address; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 121 | |
| 122 | if (rmodule_stage_load(refcode, stage) || refcode->entry == NULL) { |
| 123 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 124 | return -1; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 128 | #else |
| 129 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode) |
| 130 | { |
| 131 | return -1; |
| 132 | } |
| 133 | #endif |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 134 | |
| 135 | static int load_refcode_from_cbfs(struct rmod_stage_load *refcode) |
| 136 | { |
| 137 | printk(BIOS_DEBUG, "refcode loading from cbfs.\n"); |
| 138 | |
| 139 | if (rmodule_stage_load_from_cbfs(refcode) || refcode->entry == NULL) { |
| 140 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static efi_wrapper_entry_t load_reference_code(void) |
| 148 | { |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 149 | struct rmod_stage_load refcode = { |
| 150 | .cbmem_id = CBMEM_ID_REFCODE, |
| 151 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 152 | }; |
| 153 | |
Kyösti Mälkki | c3ed886 | 2014-06-19 19:50:51 +0300 | [diff] [blame] | 154 | if (acpi_is_wakeup_s3()) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 155 | return load_refcode_from_cache(); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Aaron Durbin | 43b7db7 | 2015-03-27 20:53:30 -0500 | [diff] [blame^] | 158 | if (load_refcode_from_vboot(&refcode) || |
| 159 | load_refcode_from_cbfs(&refcode)) |
| 160 | return NULL; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 161 | |
| 162 | /* Cache loaded reference code. */ |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 163 | cache_refcode(&refcode); |
| 164 | |
| 165 | return refcode.entry; |
| 166 | } |
| 167 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 168 | void baytrail_run_reference_code(void) |
| 169 | { |
| 170 | int ret; |
| 171 | efi_wrapper_entry_t entry; |
| 172 | struct efi_wrapper_params wrp = { |
| 173 | .version = EFI_WRAPPER_VER, |
| 174 | .console_out = send_to_console, |
| 175 | }; |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 176 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 177 | entry = load_reference_code(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 178 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 179 | if (entry == NULL) |
| 180 | return; |
| 181 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 182 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 183 | |
| 184 | /* Call into reference code. */ |
| 185 | ret = entry(&wrp); |
| 186 | |
| 187 | if (ret != 0) { |
| 188 | printk(BIOS_DEBUG, "Reference code returned %d\n", ret); |
| 189 | return; |
| 190 | } |
| 191 | } |