summaryrefslogtreecommitdiff
path: root/src/lib/ramstage_cache.c
blob: d61f1c16e6ddaa5e65d260d63d9617c7bf6c81c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * 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,
                           const struct cbmem_entry *ramstage,
                           void *entry_point)
{
	struct ramstage_cache *cache;
	uint32_t total_size;
	uint32_t ramstage_size;
	void *ramstage_base;
	long cache_size = 0;

	ramstage_size = cbmem_entry_size(ramstage);
	ramstage_base = cbmem_entry_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)entry_point;
	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 = (uint32_t)entry_point;
}

void *load_cached_ramstage(struct romstage_handoff *handoff,
                           const struct cbmem_entry *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 NULL;
	}

	printk(BIOS_DEBUG, "Loading ramstage from %p.\n", cache);

	memcpy((void *)cache->load_address, &cache->program[0], cache->size);

	return (void *)cache->entry_point;
}

#else

/* Cache relocated ramstage in CBMEM. */

void cache_loaded_ramstage(struct romstage_handoff *handoff,
                      const struct cbmem_entry *ramstage, void *entry_point)
{
	uint32_t ramstage_size;
	const struct cbmem_entry *entry;

	if (handoff == NULL)
		return;

	ramstage_size = cbmem_entry_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)entry_point;

	memcpy(cbmem_entry_start(entry), cbmem_entry_start(ramstage),
	       ramstage_size);
}

void *load_cached_ramstage(struct romstage_handoff *handoff,
                     const struct cbmem_entry *ramstage)
{
	const struct cbmem_entry *entry_cache;

	if (handoff == NULL)
		return NULL;

	entry_cache = cbmem_entry_find(CBMEM_ID_RAMSTAGE_CACHE);

	if (entry_cache == NULL)
		return NULL;

	/* Load the cached ramstage copy into the to-be-run region. */
	memcpy(cbmem_entry_start(ramstage), cbmem_entry_start(entry_cache),
	       cbmem_entry_size(ramstage));

	return (void *)handoff->ramstage_entry_point;
}

#endif