CBMEM CONSOLE: Add CBMEM type for console buffer.
[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 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
25 #include <arch/acpi.h>
26 #endif
27
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
34
35 struct cbmem_entry {
36         u32 magic;
37         u32 id;
38         u64 base;
39         u64 size;
40 } __attribute__((packed));
41
42 #ifndef __PRE_RAM__
43 static struct cbmem_entry *bss_cbmem_toc;
44
45 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
46 {
47         return bss_cbmem_toc;
48 }
49
50 void __attribute__((weak)) set_cbmem_toc(struct cbmem_entry * x)
51 {
52         /* do nothing, this should be called by chipset to save TOC in NVRAM */
53 }
54 #else
55
56 struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
57 {
58         printk(BIOS_WARNING, "WARNING: you need to define get_cbmem_toc() for your chipset\n");
59         return NULL;
60 }
61
62 #endif
63
64 /**
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)
70  *  - ACPI tables
71  *  - suspend/resume backup memory
72  */
73
74 void cbmem_init(u64 baseaddr, u64 size)
75 {
76         struct cbmem_entry *cbmem_toc;
77         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
78
79 #ifndef __PRE_RAM__
80         bss_cbmem_toc = cbmem_toc;
81 #endif
82
83         printk(BIOS_DEBUG, "Initializing CBMEM area to 0x%llx (%lld bytes)\n",
84                baseaddr, size);
85
86         if (size < (64 * 1024)) {
87                 printk(BIOS_DEBUG, "Increase CBMEM size!\n");
88                 for (;;) ;
89         }
90
91         /* we don't need to call this in romstage, usefull only from ramstage */
92 #ifndef __PRE_RAM__
93         set_cbmem_toc((struct cbmem_entry *)(unsigned long)baseaddr);
94 #endif
95         memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
96
97         cbmem_toc[0] = (struct cbmem_entry) {
98                 .magic  = CBMEM_MAGIC,
99                 .id     = CBMEM_ID_FREESPACE,
100                 .base   = baseaddr + CBMEM_TOC_RESERVED,
101                 .size   = size - CBMEM_TOC_RESERVED
102         };
103 }
104
105 int cbmem_reinit(u64 baseaddr)
106 {
107         struct cbmem_entry *cbmem_toc;
108         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
109
110         printk(BIOS_DEBUG, "Re-Initializing CBMEM area to 0x%lx\n",
111                (unsigned long)baseaddr);
112
113 #ifndef __PRE_RAM__
114         bss_cbmem_toc = cbmem_toc;
115 #endif
116
117         return (cbmem_toc[0].magic == CBMEM_MAGIC);
118 }
119
120 void *cbmem_add(u32 id, u64 size)
121 {
122         struct cbmem_entry *cbmem_toc;
123         int i;
124         void *p;
125
126         /*
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.
131          */
132         p = cbmem_find(id);
133         if (p) {
134                 printk(BIOS_NOTICE,
135                        "CBMEM section %x: using existing location at %p.\n",
136                        id, p);
137                 return p;
138         }
139
140         cbmem_toc = get_cbmem_toc();
141
142         if (cbmem_toc == NULL) {
143                 return NULL;
144         }
145
146         if (cbmem_toc[0].magic != CBMEM_MAGIC) {
147                 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
148                 return NULL;
149         }
150
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);
154                 return NULL;
155         }
156
157         /* Align size to 512 byte blocks */
158
159         size = ALIGN(size, 512) < cbmem_toc[0].size ?
160                 ALIGN(size, 512) : cbmem_toc[0].size;
161
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)
165                         break;
166         }
167
168         if (i >= MAX_CBMEM_ENTRIES) {
169                 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
170                 return NULL;
171         }
172
173         printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
174
175         cbmem_toc[i] = (struct cbmem_entry) {
176                 .magic = CBMEM_MAGIC,
177                 .id     = id,
178                 .base   = cbmem_toc[0].base,
179                 .size   = size
180         };
181
182         cbmem_toc[0].base += size;
183         cbmem_toc[0].size -= size;
184
185         return (void *)(u32)cbmem_toc[i].base;
186 }
187
188 void *cbmem_find(u32 id)
189 {
190         struct cbmem_entry *cbmem_toc;
191         int i;
192         cbmem_toc = get_cbmem_toc();
193
194         if (cbmem_toc == NULL)
195                 return NULL;
196
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;
200         }
201
202         return (void *)NULL;
203 }
204
205 #if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
206 /* Returns True if it was not intialized before. */
207 int cbmem_initialize(void)
208 {
209         int rv = 0;
210
211 #ifdef __PRE_RAM__
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;
215 #endif
216
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)
222                         acpi_slp_type = 0;
223 #endif
224                 cbmem_init(high_tables_base, high_tables_size);
225                 rv = 1;
226         }
227 #ifndef __PRE_RAM__
228         cbmem_arch_init();
229 #endif
230         return rv;
231 }
232 #endif
233
234 #ifndef __PRE_RAM__
235 void cbmem_list(void)
236 {
237         struct cbmem_entry *cbmem_toc;
238         int i;
239         cbmem_toc = get_cbmem_toc();
240
241         if (cbmem_toc == NULL)
242                 return;
243
244         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
245
246                 if (cbmem_toc[i].magic != CBMEM_MAGIC)
247                         continue;
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);
261                 }
262                 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
263                 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);
264         }
265 }
266 #endif
267
268