blob: 6449b55a8eafdf0b2fb60e6e3e3a62306fe2514f [file] [log] [blame]
Stefan Reinauer3b314022009-10-26 17:04:28 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 coresystems GmbH
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
20#include <types.h>
21#include <string.h>
Aaron Durbin40131cf2013-04-24 16:39:08 -050022#include <bootstate.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000023#include <cbmem.h>
Kyösti Mälkkie1ea8022013-09-04 14:11:08 +030024#include <boot/coreboot_tables.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000025#include <console/console.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070026#include <arch/early_variables.h>
Stefan Reinauere246b312011-09-23 10:24:49 -070027#if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
28#include <arch/acpi.h>
29#endif
Stefan Reinauer3b314022009-10-26 17:04:28 +000030
Stefan Reinauer3b314022009-10-26 17:04:28 +000031// The CBMEM TOC reserves 512 bytes to keep
32// the other entries somewhat aligned.
33// Increase if MAX_CBMEM_ENTRIES exceeds 21
34#define CBMEM_TOC_RESERVED 512
35#define MAX_CBMEM_ENTRIES 16
36#define CBMEM_MAGIC 0x434f5245
37
Stefan Reinauer3b314022009-10-26 17:04:28 +000038struct cbmem_entry {
39 u32 magic;
40 u32 id;
41 u64 base;
42 u64 size;
43} __attribute__((packed));
44
Rudolf Marekc4369532010-12-13 19:59:13 +000045#ifndef __PRE_RAM__
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +030046static uint64_t cbmem_base = 0;
47static uint64_t cbmem_size = 0;
Kyösti Mälkkibc90e152013-09-04 13:26:11 +030048#endif
Rudolf Marekbcaea142010-11-22 22:00:52 +000049
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030050static void cbmem_trace_location(uint64_t base, uint64_t size, const char *s)
51{
52 if (base && size && s) {
53 printk(BIOS_DEBUG, "CBMEM region %llx-%llx (%s)\n",
54 base, base + size - 1, s);
55 }
56}
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030057
58static void cbmem_locate_table(uint64_t *base, uint64_t *size)
59{
60#ifdef __PRE_RAM__
61 get_cbmem_table(base, size);
Rudolf Marek475916d2010-12-13 20:02:23 +000062#else
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +030063 if (!(cbmem_base && cbmem_size)) {
64 get_cbmem_table(&cbmem_base, &cbmem_size);
65 cbmem_trace_location(cbmem_base, cbmem_size, __FUNCTION__);
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030066 }
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +030067 *base = cbmem_base;
68 *size = cbmem_size;
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030069#endif
70}
Rudolf Marek475916d2010-12-13 20:02:23 +000071
Kyösti Mälkkif9f74af2013-09-06 10:46:22 +030072struct cbmem_entry *get_cbmem_toc(void)
Rudolf Marek475916d2010-12-13 20:02:23 +000073{
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030074 uint64_t base, size;
75 cbmem_locate_table(&base, &size);
76 return (struct cbmem_entry *)(unsigned long)base;
Rudolf Marek475916d2010-12-13 20:02:23 +000077}
Rudolf Marekbcaea142010-11-22 22:00:52 +000078
Kyösti Mälkki1ae305e2013-09-04 13:05:01 +030079#if !defined(__PRE_RAM__)
80void cbmem_late_set_table(uint64_t base, uint64_t size)
81{
Kyösti Mälkkic04afd62013-09-04 13:31:39 +030082 cbmem_trace_location(base, size, __FUNCTION__);
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +030083 cbmem_base = base;
84 cbmem_size = size;
Kyösti Mälkki1ae305e2013-09-04 13:05:01 +030085}
Stefan Reinauer3b314022009-10-26 17:04:28 +000086#endif
87
88/**
89 * cbmem is a simple mechanism to do some kind of book keeping of the coreboot
90 * high tables memory. This is a small amount of memory which is "stolen" from
91 * the system memory for coreboot purposes. Usually this memory is used for
92 * - the coreboot table
93 * - legacy tables (PIRQ, MP table)
94 * - ACPI tables
95 * - suspend/resume backup memory
96 */
97
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +030098#if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
Kyösti Mälkki625f1032013-09-04 14:36:31 +030099static void cbmem_init(void)
Stefan Reinauer3b314022009-10-26 17:04:28 +0000100{
Kyösti Mälkki625f1032013-09-04 14:36:31 +0300101 uint64_t baseaddr, size;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000102 struct cbmem_entry *cbmem_toc;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000103
Kyösti Mälkki625f1032013-09-04 14:36:31 +0300104 cbmem_locate_table(&baseaddr, &size);
105 cbmem_trace_location(baseaddr, size, __FUNCTION__);
106
107 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000108
109 if (size < (64 * 1024)) {
Uwe Hermanna953f372010-11-10 00:14:32 +0000110 printk(BIOS_DEBUG, "Increase CBMEM size!\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +0000111 for (;;) ;
112 }
113
114 memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
115
116 cbmem_toc[0] = (struct cbmem_entry) {
117 .magic = CBMEM_MAGIC,
118 .id = CBMEM_ID_FREESPACE,
119 .base = baseaddr + CBMEM_TOC_RESERVED,
120 .size = size - CBMEM_TOC_RESERVED
121 };
122}
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +0300123#endif
Stefan Reinauer3b314022009-10-26 17:04:28 +0000124
Kyösti Mälkkid50cdf12013-06-23 17:01:29 +0300125int cbmem_reinit(void)
Stefan Reinauer3b314022009-10-26 17:04:28 +0000126{
Kyösti Mälkkid50cdf12013-06-23 17:01:29 +0300127 uint64_t baseaddr, size;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000128 struct cbmem_entry *cbmem_toc;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000129
Kyösti Mälkkid50cdf12013-06-23 17:01:29 +0300130 cbmem_locate_table(&baseaddr, &size);
131 cbmem_trace_location(baseaddr, size, __FUNCTION__);
132
133 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
Uwe Hermanna953f372010-11-10 00:14:32 +0000134
Stefan Reinauer3b314022009-10-26 17:04:28 +0000135 return (cbmem_toc[0].magic == CBMEM_MAGIC);
136}
137
138void *cbmem_add(u32 id, u64 size)
139{
140 struct cbmem_entry *cbmem_toc;
141 int i;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700142 void *p;
143
144 /*
145 * This could be a restart, check if the section is there already. It
146 * is remotely possible that the dram contents persisted over the
147 * bootloader upgrade AND the same section now needs more room, but
148 * this is quite a remote possibility and it is ignored here.
149 */
150 p = cbmem_find(id);
151 if (p) {
152 printk(BIOS_NOTICE,
153 "CBMEM section %x: using existing location at %p.\n",
154 id, p);
155 return p;
156 }
157
Myles Watson84b685a2010-03-31 14:57:55 +0000158 cbmem_toc = get_cbmem_toc();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000159
Stefan Reinauer3b314022009-10-26 17:04:28 +0000160 if (cbmem_toc == NULL) {
161 return NULL;
162 }
163
164 if (cbmem_toc[0].magic != CBMEM_MAGIC) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000165 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +0000166 return NULL;
167 }
168
169 /* Will the entry fit at all? */
170 if (size > cbmem_toc[0].size) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000171 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000172 return NULL;
173 }
174
175 /* Align size to 512 byte blocks */
176
Stefan Reinauer14e22772010-04-27 06:56:47 +0000177 size = ALIGN(size, 512) < cbmem_toc[0].size ?
Stefan Reinauer3b314022009-10-26 17:04:28 +0000178 ALIGN(size, 512) : cbmem_toc[0].size;
179
180 /* Now look for the first free/usable TOC entry */
181 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
182 if (cbmem_toc[i].id == CBMEM_ID_NONE)
183 break;
184 }
185
186 if (i >= MAX_CBMEM_ENTRIES) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000187 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +0000188 return NULL;
189 }
190
Uwe Hermanna953f372010-11-10 00:14:32 +0000191 printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000192
193 cbmem_toc[i] = (struct cbmem_entry) {
194 .magic = CBMEM_MAGIC,
195 .id = id,
196 .base = cbmem_toc[0].base,
197 .size = size
198 };
199
200 cbmem_toc[0].base += size;
201 cbmem_toc[0].size -= size;
202
Ronald G. Minnichef402092013-11-11 10:36:28 -0800203 return (void *)(uintptr_t)cbmem_toc[i].base;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000204}
205
206void *cbmem_find(u32 id)
207{
208 struct cbmem_entry *cbmem_toc;
209 int i;
Myles Watson84b685a2010-03-31 14:57:55 +0000210 cbmem_toc = get_cbmem_toc();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000211
Stefan Reinauer3b314022009-10-26 17:04:28 +0000212 if (cbmem_toc == NULL)
213 return NULL;
214
215 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
216 if (cbmem_toc[i].id == id)
217 return (void *)(unsigned long)cbmem_toc[i].base;
218 }
219
220 return (void *)NULL;
221}
222
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +0300223#if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
Martin Rothcbf2bd72013-07-09 21:51:14 -0600224/* Returns True if it was not initialized before. */
Vadim Bendeburye1860602011-09-20 17:07:14 -0700225int cbmem_initialize(void)
Stefan Reinauer3b314022009-10-26 17:04:28 +0000226{
Vadim Bendeburye1860602011-09-20 17:07:14 -0700227 int rv = 0;
228
Vadim Bendeburye1860602011-09-20 17:07:14 -0700229 /* We expect the romstage to always initialize it. */
Kyösti Mälkkid50cdf12013-06-23 17:01:29 +0300230 if (!cbmem_reinit()) {
Vadim Bendeburye1860602011-09-20 17:07:14 -0700231#if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
232 /* Something went wrong, our high memory area got wiped */
233 if (acpi_slp_type == 3 || acpi_slp_type == 2)
234 acpi_slp_type = 0;
235#endif
Kyösti Mälkki625f1032013-09-04 14:36:31 +0300236 cbmem_init();
Vadim Bendeburye1860602011-09-20 17:07:14 -0700237 rv = 1;
238 }
239#ifndef __PRE_RAM__
Stefan Reinauer3b314022009-10-26 17:04:28 +0000240 cbmem_arch_init();
Vadim Bendeburye1860602011-09-20 17:07:14 -0700241#endif
Aaron Durbin716738a2013-05-10 00:33:32 -0500242 /* Migrate cache-as-ram variables. */
243 car_migrate_variables();
244
Vadim Bendeburye1860602011-09-20 17:07:14 -0700245 return rv;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000246}
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +0300247#endif
Stefan Reinauer3b314022009-10-26 17:04:28 +0000248
Myles Watson1d6d45e2009-11-06 17:02:51 +0000249#ifndef __PRE_RAM__
Aaron Durbin40131cf2013-04-24 16:39:08 -0500250static void init_cbmem_post_device(void *unused)
Aaron Durbindf3a1092013-03-13 12:41:44 -0500251{
252 cbmem_initialize();
253#if CONFIG_CONSOLE_CBMEM
254 cbmemc_reinit();
255#endif
256}
257
Aaron Durbin40131cf2013-04-24 16:39:08 -0500258BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = {
259 BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY,
260 init_cbmem_post_device, NULL),
261};
262
Kyösti Mälkkie1ea8022013-09-04 14:11:08 +0300263int cbmem_base_check(void)
264{
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +0300265 if (!cbmem_base) {
Kyösti Mälkkie1ea8022013-09-04 14:11:08 +0300266 printk(BIOS_ERR, "ERROR: CBMEM Base is not set.\n");
267 // Are there any boards without?
268 // Stepan thinks we should die() here!
269 }
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +0300270 printk(BIOS_DEBUG, "CBMEM Base is %llx.\n", cbmem_base);
271 return !!cbmem_base;
Kyösti Mälkkie1ea8022013-09-04 14:11:08 +0300272}
273
274void cbmem_add_lb_mem(struct lb_memory *mem)
275{
Kyösti Mälkki0fbbff42013-06-23 20:03:50 +0300276 lb_add_memory_range(mem, LB_MEM_TABLE, cbmem_base, cbmem_size);
Kyösti Mälkkie1ea8022013-09-04 14:11:08 +0300277}
278
Stefan Reinauer3b314022009-10-26 17:04:28 +0000279void cbmem_list(void)
280{
281 struct cbmem_entry *cbmem_toc;
282 int i;
Myles Watson84b685a2010-03-31 14:57:55 +0000283 cbmem_toc = get_cbmem_toc();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000284
Stefan Reinauer3b314022009-10-26 17:04:28 +0000285 if (cbmem_toc == NULL)
Maciej Pijankaea921852009-10-27 14:29:29 +0000286 return;
Stefan Reinauer3b314022009-10-26 17:04:28 +0000287
288 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
289
290 if (cbmem_toc[i].magic != CBMEM_MAGIC)
291 continue;
Aaron Durbindf3a1092013-03-13 12:41:44 -0500292 cbmem_print_entry(i, cbmem_toc[i].id, cbmem_toc[i].base,
293 cbmem_toc[i].size);
Stefan Reinauer3b314022009-10-26 17:04:28 +0000294 }
295}
296#endif
297
Stefan Reinauer3b314022009-10-26 17:04:28 +0000298