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 | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 24 | #include <cpu/x86/tsc.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 25 | #include <rmodule.h> |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 26 | #include <ramstage_cache.h> |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 27 | #include <vendorcode/google/chromeos/vboot_handoff.h> |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 28 | |
| 29 | #include <baytrail/ramstage.h> |
| 30 | #include <baytrail/efi_wrapper.h> |
| 31 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 32 | static inline struct ramstage_cache *next_cache(struct ramstage_cache *c) |
| 33 | { |
| 34 | return (struct ramstage_cache *)&c->program[c->size]; |
| 35 | } |
| 36 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 37 | static void ABI_X86 send_to_console(unsigned char b) |
| 38 | { |
| 39 | console_tx_byte(b); |
| 40 | } |
| 41 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 42 | static 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 | |
| 69 | static 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 Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 100 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode, |
| 101 | struct cbfs_stage *stage) |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 102 | { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 103 | 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 | |
| 112 | static 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 | |
| 124 | static efi_wrapper_entry_t load_reference_code(void) |
| 125 | { |
| 126 | struct vboot_handoff *vboot_handoff; |
| 127 | const struct firmware_component *fwc; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 128 | struct rmod_stage_load refcode = { |
| 129 | .cbmem_id = CBMEM_ID_REFCODE, |
| 130 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 131 | }; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 132 | int ret; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 133 | |
Kyösti Mälkki | c3ed886 | 2014-06-19 19:50:51 +0300 | [diff] [blame^] | 134 | if (acpi_is_wakeup_s3()) { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 135 | return load_refcode_from_cache(); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 138 | 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 Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 157 | cache_refcode(&refcode); |
| 158 | |
| 159 | return refcode.entry; |
| 160 | } |
| 161 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 162 | void 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 Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 170 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame] | 171 | entry = load_reference_code(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 172 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 173 | if (entry == NULL) |
| 174 | return; |
| 175 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 176 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 177 | |
| 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 | |