2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 coresystems GmbH
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.
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.
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
23 #include <console/console.h>
24 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
25 #include <arch/acpi.h>
28 // The CBMEM TOC reserves 512 bytes to keep
29 // the other entries somewhat aligned.
30 // Increase if MAX_CBMEM_ENTRIES exceeds 21
31 #define CBMEM_TOC_RESERVED 512
32 #define MAX_CBMEM_ENTRIES 16
33 #define CBMEM_MAGIC 0x434f5245
40 } __attribute__((packed));
43 static struct cbmem_entry *bss_cbmem_toc;
45 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
50 void __attribute__((weak)) set_cbmem_toc(struct cbmem_entry * x)
52 /* do nothing, this should be called by chipset to save TOC in NVRAM */
56 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
58 printk(BIOS_WARNING, "WARNING: you need to define get_cbmem_toc() for your chipset\n");
65 * cbmem is a simple mechanism to do some kind of book keeping of the coreboot
66 * high tables memory. This is a small amount of memory which is "stolen" from
67 * the system memory for coreboot purposes. Usually this memory is used for
68 * - the coreboot table
69 * - legacy tables (PIRQ, MP table)
71 * - suspend/resume backup memory
74 void cbmem_init(u64 baseaddr, u64 size)
76 struct cbmem_entry *cbmem_toc;
77 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
80 bss_cbmem_toc = cbmem_toc;
83 printk(BIOS_DEBUG, "Initializing CBMEM area to 0x%llx (%lld bytes)\n",
86 if (size < (64 * 1024)) {
87 printk(BIOS_DEBUG, "Increase CBMEM size!\n");
91 /* we don't need to call this in romstage, usefull only from ramstage */
93 set_cbmem_toc((struct cbmem_entry *)(unsigned long)baseaddr);
95 memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
97 cbmem_toc[0] = (struct cbmem_entry) {
99 .id = CBMEM_ID_FREESPACE,
100 .base = baseaddr + CBMEM_TOC_RESERVED,
101 .size = size - CBMEM_TOC_RESERVED
105 int cbmem_reinit(u64 baseaddr)
107 struct cbmem_entry *cbmem_toc;
108 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
110 printk(BIOS_DEBUG, "Re-Initializing CBMEM area to 0x%lx\n",
111 (unsigned long)baseaddr);
114 bss_cbmem_toc = cbmem_toc;
117 return (cbmem_toc[0].magic == CBMEM_MAGIC);
120 void *cbmem_add(u32 id, u64 size)
122 struct cbmem_entry *cbmem_toc;
127 * This could be a restart, check if the section is there already. It
128 * is remotely possible that the dram contents persisted over the
129 * bootloader upgrade AND the same section now needs more room, but
130 * this is quite a remote possibility and it is ignored here.
135 "CBMEM section %x: using existing location at %p.\n",
140 cbmem_toc = get_cbmem_toc();
142 if (cbmem_toc == NULL) {
146 if (cbmem_toc[0].magic != CBMEM_MAGIC) {
147 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
151 /* Will the entry fit at all? */
152 if (size > cbmem_toc[0].size) {
153 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
157 /* Align size to 512 byte blocks */
159 size = ALIGN(size, 512) < cbmem_toc[0].size ?
160 ALIGN(size, 512) : cbmem_toc[0].size;
162 /* Now look for the first free/usable TOC entry */
163 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
164 if (cbmem_toc[i].id == CBMEM_ID_NONE)
168 if (i >= MAX_CBMEM_ENTRIES) {
169 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
173 printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
175 cbmem_toc[i] = (struct cbmem_entry) {
176 .magic = CBMEM_MAGIC,
178 .base = cbmem_toc[0].base,
182 cbmem_toc[0].base += size;
183 cbmem_toc[0].size -= size;
185 return (void *)(u32)cbmem_toc[i].base;
188 void *cbmem_find(u32 id)
190 struct cbmem_entry *cbmem_toc;
192 cbmem_toc = get_cbmem_toc();
194 if (cbmem_toc == NULL)
197 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
198 if (cbmem_toc[i].id == id)
199 return (void *)(unsigned long)cbmem_toc[i].base;
205 #if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
206 /* Returns True if it was not intialized before. */
207 int cbmem_initialize(void)
212 extern unsigned long get_top_of_ram(void);
213 uint64_t high_tables_base = get_top_of_ram() - HIGH_MEMORY_SIZE;
214 uint64_t high_tables_size = HIGH_MEMORY_SIZE;
217 /* We expect the romstage to always initialize it. */
218 if (!cbmem_reinit(high_tables_base)) {
219 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
220 /* Something went wrong, our high memory area got wiped */
221 if (acpi_slp_type == 3 || acpi_slp_type == 2)
224 cbmem_init(high_tables_base, high_tables_size);
235 void cbmem_list(void)
237 struct cbmem_entry *cbmem_toc;
239 cbmem_toc = get_cbmem_toc();
241 if (cbmem_toc == NULL)
244 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
246 if (cbmem_toc[i].magic != CBMEM_MAGIC)
248 printk(BIOS_DEBUG, "%2d. ", i);
249 switch (cbmem_toc[i].id) {
250 case CBMEM_ID_FREESPACE: printk(BIOS_DEBUG, "FREE SPACE "); break;
251 case CBMEM_ID_GDT: printk(BIOS_DEBUG, "GDT "); break;
252 case CBMEM_ID_ACPI: printk(BIOS_DEBUG, "ACPI "); break;
253 case CBMEM_ID_CBTABLE: printk(BIOS_DEBUG, "COREBOOT "); break;
254 case CBMEM_ID_PIRQ: printk(BIOS_DEBUG, "IRQ TABLE "); break;
255 case CBMEM_ID_MPTABLE: printk(BIOS_DEBUG, "SMP TABLE "); break;
256 case CBMEM_ID_RESUME: printk(BIOS_DEBUG, "ACPI RESUME"); break;
257 case CBMEM_ID_SMBIOS: printk(BIOS_DEBUG, "SMBIOS "); break;
258 case CBMEM_ID_TIMESTAMP: printk(BIOS_DEBUG, "TIME STAMP "); break;
259 case CBMEM_ID_CONSOLE: printk(BIOS_DEBUG, "CONSOLE "); break;
260 default: printk(BIOS_DEBUG, "%08x ", cbmem_toc[i].id);
262 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
263 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);