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>
26 #define debug(x...) printk_debug(x)
31 // 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
43 } __attribute__((packed));
46 struct cbmem_entry *bss_cbmem_toc;
50 * cbmem is a simple mechanism to do some kind of book keeping of the coreboot
51 * high tables memory. This is a small amount of memory which is "stolen" from
52 * the system memory for coreboot purposes. Usually this memory is used for
53 * - the coreboot table
54 * - legacy tables (PIRQ, MP table)
56 * - suspend/resume backup memory
59 void cbmem_init(u64 baseaddr, u64 size)
61 struct cbmem_entry *cbmem_toc;
62 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
65 bss_cbmem_toc = cbmem_toc;
68 debug("Initializing CBMEM area to 0x%llx (%lld bytes)\n", baseaddr, size);
70 if (size < (64 * 1024)) {
71 debug("Increase CBMEM size!!\n");
75 memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
77 cbmem_toc[0] = (struct cbmem_entry) {
79 .id = CBMEM_ID_FREESPACE,
80 .base = baseaddr + CBMEM_TOC_RESERVED,
81 .size = size - CBMEM_TOC_RESERVED
85 int cbmem_reinit(u64 baseaddr)
87 struct cbmem_entry *cbmem_toc;
88 cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
90 debug("Re-Initializing CBMEM area to 0x%lx\n", (unsigned long)baseaddr);
92 bss_cbmem_toc = cbmem_toc;
95 return (cbmem_toc[0].magic == CBMEM_MAGIC);
98 void *cbmem_add(u32 id, u64 size)
100 struct cbmem_entry *cbmem_toc;
103 cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
105 cbmem_toc = bss_cbmem_toc;
108 if (cbmem_toc == NULL) {
112 if (cbmem_toc[0].magic != CBMEM_MAGIC) {
113 printk_err("ERROR: CBMEM was not initialized yet.\n");
117 /* Will the entry fit at all? */
118 if (size > cbmem_toc[0].size) {
119 printk_err("ERROR: Not enough memory for table %x\n", id);
123 /* Align size to 512 byte blocks */
125 size = ALIGN(size, 512) < cbmem_toc[0].size ?
126 ALIGN(size, 512) : cbmem_toc[0].size;
128 /* Now look for the first free/usable TOC entry */
129 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
130 if (cbmem_toc[i].id == CBMEM_ID_NONE)
134 if (i >= MAX_CBMEM_ENTRIES) {
135 printk_err("ERROR: No more CBMEM entries available.\n");
139 debug("Adding CBMEM entry as no. %d\n", i);
141 cbmem_toc[i] = (struct cbmem_entry) {
142 .magic = CBMEM_MAGIC,
144 .base = cbmem_toc[0].base,
148 cbmem_toc[0].base += size;
149 cbmem_toc[0].size -= size;
151 return (void *)(u32)cbmem_toc[i].base;
154 void *cbmem_find(u32 id)
156 struct cbmem_entry *cbmem_toc;
159 cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
161 cbmem_toc = bss_cbmem_toc;
164 if (cbmem_toc == NULL)
167 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
168 if (cbmem_toc[i].id == id)
169 return (void *)(unsigned long)cbmem_toc[i].base;
176 #if CONFIG_HAVE_ACPI_RESUME
177 extern u8 acpi_slp_type;
179 extern uint64_t high_tables_base, high_tables_size;
181 void cbmem_initialize(void)
183 #if CONFIG_HAVE_ACPI_RESUME
184 if (acpi_slp_type == 3) {
185 if (!cbmem_reinit(high_tables_base)) {
186 /* Something went wrong, our high memory area got wiped */
188 cbmem_init(high_tables_base, high_tables_size);
191 cbmem_init(high_tables_base, high_tables_size);
194 cbmem_init(high_tables_base, high_tables_size);
200 void cbmem_list(void)
202 struct cbmem_entry *cbmem_toc;
205 cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
207 cbmem_toc = bss_cbmem_toc;
210 if (cbmem_toc == NULL)
213 for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
215 if (cbmem_toc[i].magic != CBMEM_MAGIC)
217 printk_debug("%2d. ", i);
218 switch (cbmem_toc[i].id) {
219 case CBMEM_ID_FREESPACE: printk_debug("FREE SPACE "); break;
220 case CBMEM_ID_GDT: printk_debug("GDT "); break;
221 case CBMEM_ID_ACPI: printk_debug("ACPI "); break;
222 case CBMEM_ID_CBTABLE: printk_debug("COREBOOT "); break;
223 case CBMEM_ID_PIRQ: printk_debug("IRQ TABLE "); break;
224 case CBMEM_ID_MPTABLE: printk_debug("SMP TABLE "); break;
225 case CBMEM_ID_RESUME: printk_debug("ACPI RESUME"); break;
226 default: printk_debug("%08x ", cbmem_toc[i].id);
228 printk_debug("%08llx ", cbmem_toc[i].base);
229 printk_debug("%08llx\n", cbmem_toc[i].size);