coreboot: common stage cache

Many chipsets were using a stage cache for reference code
or when using a relocatable ramstage. Provide a common
API for the chipsets to use while reducing code duplication.

Change-Id: Ia36efa169fe6bd8a3dbe07bf57a9729c7edbdd46
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: http://review.coreboot.org/8625
Tested-by: build bot (Jenkins)
Reviewed-by: Marc Jones <marc.jones@se-eng.com>
This commit is contained in:
Aaron Durbin
2015-03-06 23:17:33 -06:00
parent cac5050623
commit bd74a4b2d2
20 changed files with 337 additions and 367 deletions

View File

@@ -111,7 +111,14 @@ romstage-y += hexdump.c
romstage-$(CONFIG_REG_SCRIPT) += reg_script.c
ramstage-$(CONFIG_REG_SCRIPT) += reg_script.c
romstage-$(CONFIG_RELOCATABLE_RAMSTAGE) += ramstage_cache.c
ifeq ($(CONFIG_CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM),y)
ramstage-y += ext_stage_cache.c
romstage-y += ext_stage_cache.c
else
ramstage-$(CONFIG_RELOCATABLE_RAMSTAGE) += cbmem_stage_cache.c
romstage-$(CONFIG_RELOCATABLE_RAMSTAGE) += cbmem_stage_cache.c
endif
smm-y += cbfs.c cbfs_core.c memcmp.c
smm-$(CONFIG_COMPILER_GCC) += gcc.c

View File

@@ -0,0 +1,76 @@
/*
* This file is part of the coreboot project.
*
* Copyright 2015 Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc.
*/
#include <arch/early_variables.h>
#include <cbmem.h>
#include <stage_cache.h>
#include <string.h>
/* Provide empty implementations by default. */
void __attribute__((weak)) stage_cache_create_empty(void) {}
void __attribute__((weak)) stage_cache_recover(void) {}
/* Stage cache uses cbmem. */
void stage_cache_add(int stage_id, struct prog *stage)
{
struct stage_cache *meta;
void *c;
meta = cbmem_add(CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta));
if (meta == NULL)
return;
meta->load_addr = (uintptr_t)prog_start(stage);
meta->entry_addr = (uintptr_t)prog_entry(stage);
c = cbmem_add(CBMEM_ID_STAGEx_CACHE + stage_id, prog_size(stage));
if (c == NULL)
return;
memcpy(c, prog_start(stage), prog_size(stage));
}
void stage_cache_load_stage(int stage_id, struct prog *stage)
{
struct stage_cache *meta;
const struct cbmem_entry *e;
void *c;
size_t size;
void *load_addr;
prog_set_entry(stage, NULL, NULL);
meta = cbmem_find(CBMEM_ID_STAGEx_META + stage_id);
if (meta == NULL)
return;
e = cbmem_entry_find(CBMEM_ID_STAGEx_CACHE + stage_id);
if (e == NULL)
return;
c = cbmem_entry_start(e);
size = cbmem_entry_size(e);
load_addr = (void *)(uintptr_t)meta->load_addr;
memcpy(load_addr, c, size);
prog_set_area(stage, load_addr, size);
prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, NULL);
}

129
src/lib/ext_stage_cache.c Normal file
View File

@@ -0,0 +1,129 @@
/*
* This file is part of the coreboot project.
*
* Copyright 2015 Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc.
*/
#include <arch/early_variables.h>
#include <bootstate.h>
#include <cbmem.h>
#include <console/console.h>
#include <imd.h>
#include <rules.h>
#include <stage_cache.h>
#include <string.h>
static struct imd imd_stage_cache CAR_GLOBAL = { };
static inline struct imd *imd_get(void)
{
return car_get_var_ptr(&imd_stage_cache);
}
void stage_cache_create_empty(void)
{
struct imd *imd;
void *base;
size_t size;
imd = imd_get();
stage_cache_external_region(&base, &size);
imd_handle_init(imd, (void *)(size + (uintptr_t)base));
printk(BIOS_DEBUG, "External stage cache:\n");
imd_create_tiered_empty(imd, 4096, 4096, 1024, 32);
if (imd_limit_size(imd, size))
printk(BIOS_DEBUG, "Could not limit stage cache size.\n");
}
void stage_cache_recover(void)
{
struct imd *imd;
void *base;
size_t size;
imd = imd_get();
stage_cache_external_region(&base, &size);
imd_handle_init(imd, (void *)(size + (uintptr_t)base));
if (imd_recover(imd))
printk(BIOS_DEBUG, "Unable to recover external stage cache.\n");
}
void stage_cache_add(int stage_id, struct prog *stage)
{
struct imd *imd;
const struct imd_entry *e;
struct stage_cache *meta;
void *c;
imd = imd_get();
e = imd_entry_add(imd, CBMEM_ID_STAGEx_META + stage_id, sizeof(*meta));
if (e == NULL)
return;
meta = imd_entry_at(imd, e);
meta->load_addr = (uintptr_t)prog_start(stage);
meta->entry_addr = (uintptr_t)prog_entry(stage);
e = imd_entry_add(imd, CBMEM_ID_STAGEx_CACHE + stage_id,
prog_size(stage));
if (e == NULL)
return;
c = imd_entry_at(imd, e);
memcpy(c, prog_start(stage), prog_size(stage));
}
void stage_cache_load_stage(int stage_id, struct prog *stage)
{
struct imd *imd;
struct stage_cache *meta;
const struct imd_entry *e;
void *c;
size_t size;
imd = imd_get();
e = imd_entry_find(imd, CBMEM_ID_STAGEx_META + stage_id);
if (e == NULL)
return;
meta = imd_entry_at(imd, e);
e = imd_entry_find(imd, CBMEM_ID_STAGEx_CACHE + stage_id);
if (e == NULL)
return;
c = imd_entry_at(imd, e);
size = imd_entry_size(imd, e);
memcpy((void *)(uintptr_t)meta->load_addr, c, size);
prog_set_area(stage, (void *)(uintptr_t)meta->load_addr, size);
prog_set_entry(stage, (void *)(uintptr_t)meta->entry_addr, NULL);
}
#if ENV_RAMSTAGE
static void recover_sc(void *unused)
{
stage_cache_recover();
}
BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, recover_sc, NULL);
#endif

View File

@@ -23,6 +23,7 @@
#include <cbfs.h>
#include <program_loading.h>
#include <romstage_handoff.h>
#include <stage_cache.h>
#include <timestamp.h>
extern const struct prog_loader_ops cbfs_ramstage_loader;
@@ -35,16 +36,20 @@ static const struct prog_loader_ops *loaders[] = {
&cbfs_ramstage_loader,
};
static void
load_ramstage(const struct prog_loader_ops *ops,
struct romstage_handoff *handoff, struct prog *ramstage)
void __attribute__((weak)) stage_cache_add(int stage_id, struct prog *stage) {}
void __attribute__((weak)) stage_cache_load_stage(int stage_id,
struct prog *stage) {}
void __attribute__((weak)) ramstage_cache_invalid(void) {}
static void load_ramstage(const struct prog_loader_ops *ops,
struct prog *ramstage)
{
timestamp_add_now(TS_START_COPYRAM);
if (ops->prepare(ramstage))
return;
cache_loaded_ramstage(handoff, ramstage);
stage_cache_add(STAGE_RAMSTAGE, ramstage);
timestamp_add_now(TS_END_COPYRAM);
@@ -56,18 +61,18 @@ static void run_ramstage_from_resume(struct romstage_handoff *handoff,
{
if (handoff != NULL && handoff->s3_resume) {
/* Load the cached ramstage to runtime location. */
load_cached_ramstage(handoff, ramstage);
stage_cache_load_stage(STAGE_RAMSTAGE, ramstage);
if (prog_entry(ramstage) != NULL) {
printk(BIOS_DEBUG, "Jumping to image.\n");
prog_run(ramstage);
}
ramstage_cache_invalid();
}
}
void run_ramstage(void)
{
struct romstage_handoff *handoff;
const struct prog_loader_ops *ops;
int i;
struct prog ramstage = {
@@ -75,14 +80,12 @@ void run_ramstage(void)
.type = PROG_RAMSTAGE,
};
handoff = romstage_handoff_find_or_add();
run_ramstage_from_resume(handoff, &ramstage);
run_ramstage_from_resume(romstage_handoff_find_or_add(), &ramstage);
for (i = 0; i < ARRAY_SIZE(loaders); i++) {
ops = loaders[i];
printk(BIOS_DEBUG, "Trying %s ramstage loader.\n", ops->name);
load_ramstage(ops, handoff, &ramstage);
load_ramstage(ops, &ramstage);
}
die("Ramstage was not loaded!\n");

View File

@@ -1,149 +0,0 @@
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2013 Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stddef.h>
#include <string.h>
#include <console/console.h>
#include <program_loading.h>
#include <ramstage_cache.h>
#include <romstage_handoff.h>
#if CONFIG_CACHE_RELOCATED_RAMSTAGE_OUTSIDE_CBMEM
void cache_loaded_ramstage(struct romstage_handoff *handoff,
struct prog *ramstage)
{
struct ramstage_cache *cache;
uint32_t total_size;
uint32_t ramstage_size;
void *ramstage_base;
long cache_size = 0;
ramstage_size = prog_size(ramstage);
ramstage_base = prog_start(ramstage);
cache = ramstage_cache_location(&cache_size);
if (cache == NULL) {
printk(BIOS_DEBUG, "No ramstage cache location provided.\n");
return;
}
total_size = sizeof(*cache) + ramstage_size;
if (total_size > cache_size) {
printk(BIOS_DEBUG, "cache size too small: 0x%08x > 0x%08lx\n",
total_size, cache_size);
/* Nuke whatever may be there now just in case. */
cache->magic = ~RAMSTAGE_CACHE_MAGIC;
return;
}
cache->magic = RAMSTAGE_CACHE_MAGIC;
cache->entry_point = (uint32_t)prog_entry(ramstage);
cache->load_address = (uint32_t)ramstage_base;
cache->size = ramstage_size;
printk(BIOS_DEBUG, "Saving ramstage to %p.\n", cache);
/* Copy over the program. */
memcpy(&cache->program[0], ramstage_base, ramstage_size);
if (handoff == NULL)
return;
handoff->ramstage_entry_point = cache->entry_point;
}
void load_cached_ramstage(struct romstage_handoff *handoff,
struct prog *ramstage)
{
struct ramstage_cache *cache;
long size = 0;
cache = ramstage_cache_location(&size);
if (!ramstage_cache_is_valid(cache)) {
printk(BIOS_DEBUG, "Invalid ramstage cache found.\n");
ramstage_cache_invalid(cache);
return;
}
printk(BIOS_DEBUG, "Loading ramstage from %p.\n", cache);
prog_set_area(ramstage, (void *)cache->load_address, cache->size);
prog_set_entry(ramstage, (void *)cache->entry_point, NULL);
memcpy((void *)cache->load_address, &cache->program[0], cache->size);
}
#else
/* Cache relocated ramstage in CBMEM. */
void cache_loaded_ramstage(struct romstage_handoff *handoff,
struct prog *ramstage)
{
uint32_t ramstage_size;
const struct cbmem_entry *entry;
if (handoff == NULL)
return;
ramstage_size = prog_size(ramstage);
/* cbmem_entry_add() does a find() before add(). */
entry = cbmem_entry_add(CBMEM_ID_RAMSTAGE_CACHE, ramstage_size);
if (entry == NULL)
return;
/* Keep track of the entry point in the handoff structure. */
handoff->ramstage_entry_point = (uint32_t)prog_entry(ramstage);
memcpy(cbmem_entry_start(entry), prog_start(ramstage), ramstage_size);
}
void load_cached_ramstage(struct romstage_handoff *handoff,
struct prog *ramstage)
{
const struct cbmem_entry *entry_cache;
const struct cbmem_entry *entry_dest;
if (handoff == NULL)
return;
entry_cache = cbmem_entry_find(CBMEM_ID_RAMSTAGE_CACHE);
if (entry_cache == NULL)
return;
entry_dest = cbmem_entry_find(CBMEM_ID_RAMSTAGE);
if (entry_dest == NULL)
return;
prog_set_area(ramstage, cbmem_entry_start(entry_dest),
cbmem_entry_size(entry_dest));
prog_set_entry(ramstage, (void *)handoff->ramstage_entry_point, NULL);
/* Load the cached ramstage copy into the to-be-run region. */
memcpy(prog_start(ramstage), cbmem_entry_start(entry_cache),
prog_size(ramstage));
}
#endif