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 | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 27 | |
| 28 | #include <baytrail/ramstage.h> |
| 29 | #include <baytrail/efi_wrapper.h> |
| 30 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame^] | 31 | static inline int is_s3_resume(void) |
| 32 | { |
| 33 | #if CONFIG_HAVE_ACPI_RESUME |
| 34 | return acpi_slp_type == 3; |
| 35 | #else |
| 36 | return 0; |
| 37 | #endif |
| 38 | } |
| 39 | |
| 40 | static inline struct ramstage_cache *next_cache(struct ramstage_cache *c) |
| 41 | { |
| 42 | return (struct ramstage_cache *)&c->program[c->size]; |
| 43 | } |
| 44 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 45 | static void ABI_X86 send_to_console(unsigned char b) |
| 46 | { |
| 47 | console_tx_byte(b); |
| 48 | } |
| 49 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame^] | 50 | static efi_wrapper_entry_t load_refcode_from_cache(void) |
| 51 | { |
| 52 | struct ramstage_cache *c; |
| 53 | long cache_size; |
| 54 | |
| 55 | printk(BIOS_DEBUG, "refcode loading from cache.\n"); |
| 56 | |
| 57 | c = ramstage_cache_location(&cache_size); |
| 58 | |
| 59 | if (!ramstage_cache_is_valid(c)) { |
| 60 | printk(BIOS_DEBUG, "Invalid ramstage cache descriptor.\n"); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | c = next_cache(c); |
| 65 | if (!ramstage_cache_is_valid(c)) { |
| 66 | printk(BIOS_DEBUG, "Invalid refcode cache descriptor.\n"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | printk(BIOS_DEBUG, "Loading cached reference code from 0x%08x(%x)\n", |
| 71 | c->load_address, c->size); |
| 72 | memcpy((void *)c->load_address, &c->program[0], c->size); |
| 73 | |
| 74 | return (efi_wrapper_entry_t)c->entry_point; |
| 75 | } |
| 76 | |
| 77 | static void cache_refcode(const struct rmod_stage_load *rsl) |
| 78 | { |
| 79 | struct ramstage_cache *c; |
| 80 | long cache_size; |
| 81 | |
| 82 | c = ramstage_cache_location(&cache_size); |
| 83 | |
| 84 | if (!ramstage_cache_is_valid(c)) { |
| 85 | printk(BIOS_DEBUG, "No place to cache reference code.\n"); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | /* Determine how much remaining cache available. */ |
| 90 | cache_size -= c->size + sizeof(*c); |
| 91 | |
| 92 | if (cache_size < (sizeof(*c) + cbmem_entry_size(rsl->cbmem_entry))) { |
| 93 | printk(BIOS_DEBUG, "Not enough cache space for ref code.\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | c = next_cache(c); |
| 98 | c->magic = RAMSTAGE_CACHE_MAGIC; |
| 99 | c->entry_point = (uint32_t)rsl->entry; |
| 100 | c->load_address = (uint32_t)cbmem_entry_start(rsl->cbmem_entry); |
| 101 | c->size = cbmem_entry_size(rsl->cbmem_entry);; |
| 102 | |
| 103 | printk(BIOS_DEBUG, "Caching refcode at 0x%p(%x)\n", |
| 104 | &c->program[0], c->size); |
| 105 | memcpy(&c->program[0], (void *)c->load_address, c->size); |
| 106 | } |
| 107 | |
| 108 | static efi_wrapper_entry_t load_refcode_from_cbfs(void) |
| 109 | { |
| 110 | struct rmod_stage_load refcode = { |
| 111 | .cbmem_id = CBMEM_ID_REFCODE, |
| 112 | .name = CONFIG_CBFS_PREFIX "/refcode", |
| 113 | }; |
| 114 | |
| 115 | printk(BIOS_DEBUG, "refcode loading from cbfs.\n"); |
| 116 | |
| 117 | if (rmodule_stage_load_from_cbfs(&refcode) || refcode.entry == NULL) { |
| 118 | printk(BIOS_DEBUG, "Error loading reference code.\n"); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | cache_refcode(&refcode); |
| 123 | |
| 124 | return refcode.entry; |
| 125 | } |
| 126 | |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 127 | void baytrail_run_reference_code(void) |
| 128 | { |
| 129 | int ret; |
| 130 | efi_wrapper_entry_t entry; |
| 131 | struct efi_wrapper_params wrp = { |
| 132 | .version = EFI_WRAPPER_VER, |
| 133 | .console_out = send_to_console, |
| 134 | }; |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 135 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame^] | 136 | if (is_s3_resume()) { |
| 137 | entry = load_refcode_from_cache(); |
| 138 | } else { |
| 139 | entry = load_refcode_from_cbfs(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Aaron Durbin | 63fcb4a | 2013-12-12 10:29:48 -0800 | [diff] [blame^] | 142 | if (entry == NULL) |
| 143 | return; |
| 144 | |
Aaron Durbin | 8fa6283 | 2013-10-28 09:54:22 -0500 | [diff] [blame] | 145 | wrp.tsc_ticks_per_microsecond = tsc_freq_mhz(); |
Aaron Durbin | ae5d83e | 2013-10-24 10:21:43 -0500 | [diff] [blame] | 146 | |
| 147 | /* Call into reference code. */ |
| 148 | ret = entry(&wrp); |
| 149 | |
| 150 | if (ret != 0) { |
| 151 | printk(BIOS_DEBUG, "Reference code returned %d\n", ret); |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |