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>
25 // The CBMEM TOC reserves 512 bytes to keep
26 // the other entries somewhat aligned.
27 // Increase if MAX_CBMEM_ENTRIES exceeds 21
28 #define CBMEM_TOC_RESERVED 512
29 #define MAX_CBMEM_ENTRIES 16
30 #define CBMEM_MAGIC 0x434f5245
37 } __attribute__((packed));
40 static struct cbmem_entry *bss_cbmem_toc;
42 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
47 void __attribute__((weak)) set_cbmem_toc(struct cbmem_entry * x)
49 /* do nothing, this should be called by chipset to save TOC in NVRAM */
53 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
55 printk(BIOS_WARNING, "WARNING: you need to define get_cbmem_toc() for your chipset\n");
62 * cbmem is a simple mechanism to do some kind of book keeping of the coreboot
63 * high tables memory. This is a small amount of memory which is "stolen" from
64 * the system memory for coreboot purposes. Usually this memory is used for
65 * - the coreboot table
66 * - legacy tables (PIRQ, MP table)
68 * - suspend/resume backup memory
71 void cbmem_init(u64 baseaddr, u64 size)
73 struct cbmem_entry *cbmem_toc;
74 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
77 bss_cbmem_toc = cbmem_toc;
80 printk(BIOS_DEBUG, "Initializing CBMEM area to 0x%llx (%lld bytes)\n",
83 if (size < (64 * 1024)) {
84 printk(BIOS_DEBUG, "Increase CBMEM size!\n");
88 /* we don't need to call this in romstage, usefull only from ramstage */
90 set_cbmem_toc((struct cbmem_entry *)(unsigned long)baseaddr);
92 memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
94 cbmem_toc[0] = (struct cbmem_entry) {
96 .id = CBMEM_ID_FREESPACE,
97 .base = baseaddr + CBMEM_TOC_RESERVED,
98 .size = size - CBMEM_TOC_RESERVED
102 int cbmem_reinit(u64 baseaddr)
104 struct cbmem_entry *cbmem_toc;
105 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
107 printk(BIOS_DEBUG, "Re-Initializing CBMEM area to 0x%lx\n",
108 (unsigned long)baseaddr);
111 bss_cbmem_toc = cbmem_toc;
114 return (cbmem_toc[0].magic == CBMEM_MAGIC);
117 void *cbmem_add(u32 id, u64 size)
119 struct cbmem_entry *cbmem_toc;
121 cbmem_toc = get_cbmem_toc();
123 if (cbmem_toc == NULL) {
127 if (cbmem_toc[0].magic != CBMEM_MAGIC) {
128 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
132 /* Will the entry fit at all? */
133 if (size > cbmem_toc[0].size) {
134 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
138 /* Align size to 512 byte blocks */
140 size = ALIGN(size, 512) < cbmem_toc[0].size ?
141 ALIGN(size, 512) : cbmem_toc[0].size;
143 /* Now look for the first free/usable TOC entry */
144 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
145 if (cbmem_toc[i].id == CBMEM_ID_NONE)
149 if (i >= MAX_CBMEM_ENTRIES) {
150 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
154 printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
156 cbmem_toc[i] = (struct cbmem_entry) {
157 .magic = CBMEM_MAGIC,
159 .base = cbmem_toc[0].base,
163 cbmem_toc[0].base += size;
164 cbmem_toc[0].size -= size;
166 return (void *)(u32)cbmem_toc[i].base;
169 void *cbmem_find(u32 id)
171 struct cbmem_entry *cbmem_toc;
173 cbmem_toc = get_cbmem_toc();
175 if (cbmem_toc == NULL)
178 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
179 if (cbmem_toc[i].id == id)
180 return (void *)(unsigned long)cbmem_toc[i].base;
186 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
187 extern u8 acpi_slp_type;
190 #if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
191 /* Returns True if it was not intialized before. */
192 int cbmem_initialize(void)
197 extern unsigned long get_top_of_ram(void);
198 uint64_t high_tables_base = get_top_of_ram() - HIGH_MEMORY_SIZE;
199 uint64_t high_tables_size = HIGH_MEMORY_SIZE;
202 /* We expect the romstage to always initialize it. */
203 if (!cbmem_reinit(high_tables_base)) {
204 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
205 /* Something went wrong, our high memory area got wiped */
206 if (acpi_slp_type == 3 || acpi_slp_type == 2)
209 cbmem_init(high_tables_base, high_tables_size);
220 void cbmem_list(void)
222 struct cbmem_entry *cbmem_toc;
224 cbmem_toc = get_cbmem_toc();
226 if (cbmem_toc == NULL)
229 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
231 if (cbmem_toc[i].magic != CBMEM_MAGIC)
233 printk(BIOS_DEBUG, "%2d. ", i);
234 switch (cbmem_toc[i].id) {
235 case CBMEM_ID_FREESPACE: printk(BIOS_DEBUG, "FREE SPACE "); break;
236 case CBMEM_ID_GDT: printk(BIOS_DEBUG, "GDT "); break;
237 case CBMEM_ID_ACPI: printk(BIOS_DEBUG, "ACPI "); break;
238 case CBMEM_ID_CBTABLE: printk(BIOS_DEBUG, "COREBOOT "); break;
239 case CBMEM_ID_PIRQ: printk(BIOS_DEBUG, "IRQ TABLE "); break;
240 case CBMEM_ID_MPTABLE: printk(BIOS_DEBUG, "SMP TABLE "); break;
241 case CBMEM_ID_RESUME: printk(BIOS_DEBUG, "ACPI RESUME"); break;
242 case CBMEM_ID_SMBIOS: printk(BIOS_DEBUG, "SMBIOS "); break;
243 default: printk(BIOS_DEBUG, "%08x ", cbmem_toc[i].id);
245 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
246 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);