Initialize CBMEM early.
[coreboot.git] / src / lib / cbmem.c
1 /*
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>
22 #include <cbmem.h>
23 #include <console/console.h>
24
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
31
32 struct cbmem_entry {
33         u32 magic;
34         u32 id;
35         u64 base;
36         u64 size;
37 } __attribute__((packed));
38
39 #ifndef __PRE_RAM__
40 static struct cbmem_entry *bss_cbmem_toc;
41
42 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
43 {
44         return bss_cbmem_toc;
45 }
46
47 void __attribute__((weak)) set_cbmem_toc(struct cbmem_entry * x)
48 {
49         /* do nothing, this should be called by chipset to save TOC in NVRAM */
50 }
51 #else
52
53 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
54 {
55         printk(BIOS_WARNING, "WARNING: you need to define get_cbmem_toc() for your chipset\n");
56         return NULL;
57 }
58
59 #endif
60
61 /**
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)
67  *  - ACPI tables
68  *  - suspend/resume backup memory
69  */
70
71 void cbmem_init(u64 baseaddr, u64 size)
72 {
73         struct cbmem_entry *cbmem_toc;
74         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
75
76 #ifndef __PRE_RAM__
77         bss_cbmem_toc = cbmem_toc;
78 #endif
79
80         printk(BIOS_DEBUG, "Initializing CBMEM area to 0x%llx (%lld bytes)\n",
81                baseaddr, size);
82
83         if (size < (64 * 1024)) {
84                 printk(BIOS_DEBUG, "Increase CBMEM size!\n");
85                 for (;;) ;
86         }
87
88         /* we don't need to call this in romstage, usefull only from ramstage */
89 #ifndef __PRE_RAM__
90         set_cbmem_toc((struct cbmem_entry *)(unsigned long)baseaddr);
91 #endif
92         memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
93
94         cbmem_toc[0] = (struct cbmem_entry) {
95                 .magic  = CBMEM_MAGIC,
96                 .id     = CBMEM_ID_FREESPACE,
97                 .base   = baseaddr + CBMEM_TOC_RESERVED,
98                 .size   = size - CBMEM_TOC_RESERVED
99         };
100 }
101
102 int cbmem_reinit(u64 baseaddr)
103 {
104         struct cbmem_entry *cbmem_toc;
105         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
106
107         printk(BIOS_DEBUG, "Re-Initializing CBMEM area to 0x%lx\n",
108                (unsigned long)baseaddr);
109
110 #ifndef __PRE_RAM__
111         bss_cbmem_toc = cbmem_toc;
112 #endif
113
114         return (cbmem_toc[0].magic == CBMEM_MAGIC);
115 }
116
117 void *cbmem_add(u32 id, u64 size)
118 {
119         struct cbmem_entry *cbmem_toc;
120         int i;
121         cbmem_toc = get_cbmem_toc();
122
123         if (cbmem_toc == NULL) {
124                 return NULL;
125         }
126
127         if (cbmem_toc[0].magic != CBMEM_MAGIC) {
128                 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
129                 return NULL;
130         }
131
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);
135                 return NULL;
136         }
137
138         /* Align size to 512 byte blocks */
139
140         size = ALIGN(size, 512) < cbmem_toc[0].size ?
141                 ALIGN(size, 512) : cbmem_toc[0].size;
142
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)
146                         break;
147         }
148
149         if (i >= MAX_CBMEM_ENTRIES) {
150                 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
151                 return NULL;
152         }
153
154         printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
155
156         cbmem_toc[i] = (struct cbmem_entry) {
157                 .magic = CBMEM_MAGIC,
158                 .id     = id,
159                 .base   = cbmem_toc[0].base,
160                 .size   = size
161         };
162
163         cbmem_toc[0].base += size;
164         cbmem_toc[0].size -= size;
165
166         return (void *)(u32)cbmem_toc[i].base;
167 }
168
169 void *cbmem_find(u32 id)
170 {
171         struct cbmem_entry *cbmem_toc;
172         int i;
173         cbmem_toc = get_cbmem_toc();
174
175         if (cbmem_toc == NULL)
176                 return NULL;
177
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;
181         }
182
183         return (void *)NULL;
184 }
185
186 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
187 extern u8 acpi_slp_type;
188 #endif
189
190 #if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
191 /* Returns True if it was not intialized before. */
192 int cbmem_initialize(void)
193 {
194         int rv = 0;
195
196 #ifdef __PRE_RAM__
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;
200 #endif
201
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)
207                         acpi_slp_type = 0;
208 #endif
209                 cbmem_init(high_tables_base, high_tables_size);
210                 rv = 1;
211         }
212 #ifndef __PRE_RAM__
213         cbmem_arch_init();
214 #endif
215         return rv;
216 }
217 #endif
218
219 #ifndef __PRE_RAM__
220 void cbmem_list(void)
221 {
222         struct cbmem_entry *cbmem_toc;
223         int i;
224         cbmem_toc = get_cbmem_toc();
225
226         if (cbmem_toc == NULL)
227                 return;
228
229         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
230
231                 if (cbmem_toc[i].magic != CBMEM_MAGIC)
232                         continue;
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);
244                 }
245                 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
246                 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);
247         }
248 }
249 #endif
250
251