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 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 | |
| 41 | static inline struct ramstage_cache *next_cache(struct ramstage_cache *c) |
| 42 | { |
| 43 | return (struct ramstage_cache *)&c->program[c->size]; |
| 44 | } |
| 45 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 46 | static void ABI_X86 send_to_console(unsigned char b) |
| 47 | { |
| 48 | console_tx_byte(b); |
| 49 | } |
| 50 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 51 | static 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 | |
| 78 | static 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 Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 109 | static int load_refcode_from_vboot(struct rmod_stage_load *refcode, |
| 110 | struct cbfs_stage *stage) |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 111 | { |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 112 | 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 | |
| 121 | static 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 | |
| 133 | static efi_wrapper_entry_t load_reference_code(void) |
| 134 | { |
| 135 | struct vboot_handoff *vboot_handoff; |
| 136 | const struct firmware_component *fwc; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 137 | struct rmod_stage_load refcode = { |
| 138 | .cbmem_id = CBMEM_ID_REFCODE, |
| 139 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 140 | }; |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 141 | int ret; |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 142 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 143 | if (is_s3_resume()) { |
| 144 | return load_refcode_from_cache(); |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 147 | 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 Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 166 | cache_refcode(&refcode); |
| 167 | |
| 168 | return refcode.entry; |
| 169 | } |
| 170 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 171 | void 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 Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 179 | |
Aaron Durbin | 7f17759 | 2013-12-13 13:05:24 -0800 | [diff] [blame^] | 180 | entry = load_reference_code(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 181 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame] | 182 | if (entry == NULL) |
| 183 | return; |
| 184 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 185 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 186 | |
| 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 | |