Add timestamp collecting to coreboot.
[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         void *p;
122
123         /*
124          * This could be a restart, check if the section is there already. It
125          * is remotely possible that the dram contents persisted over the
126          * bootloader upgrade AND the same section now needs more room, but
127          * this is quite a remote possibility and it is ignored here.
128          */
129         p = cbmem_find(id);
130         if (p) {
131                 printk(BIOS_NOTICE,
132                        "CBMEM section %x: using existing location at %p.\n",
133                        id, p);
134                 return p;
135         }
136
137         cbmem_toc = get_cbmem_toc();
138
139         if (cbmem_toc == NULL) {
140                 return NULL;
141         }
142
143         if (cbmem_toc[0].magic != CBMEM_MAGIC) {
144                 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
145                 return NULL;
146         }
147
148         /* Will the entry fit at all? */
149         if (size > cbmem_toc[0].size) {
150                 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
151                 return NULL;
152         }
153
154         /* Align size to 512 byte blocks */
155
156         size = ALIGN(size, 512) < cbmem_toc[0].size ?
157                 ALIGN(size, 512) : cbmem_toc[0].size;
158
159         /* Now look for the first free/usable TOC entry */
160         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
161                 if (cbmem_toc[i].id == CBMEM_ID_NONE)
162                         break;
163         }
164
165         if (i >= MAX_CBMEM_ENTRIES) {
166                 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
167                 return NULL;
168         }
169
170         printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
171
172         cbmem_toc[i] = (struct cbmem_entry) {
173                 .magic = CBMEM_MAGIC,
174                 .id     = id,
175                 .base   = cbmem_toc[0].base,
176                 .size   = size
177         };
178
179         cbmem_toc[0].base += size;
180         cbmem_toc[0].size -= size;
181
182         return (void *)(u32)cbmem_toc[i].base;
183 }
184
185 void *cbmem_find(u32 id)
186 {
187         struct cbmem_entry *cbmem_toc;
188         int i;
189         cbmem_toc = get_cbmem_toc();
190
191         if (cbmem_toc == NULL)
192                 return NULL;
193
194         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
195                 if (cbmem_toc[i].id == id)
196                         return (void *)(unsigned long)cbmem_toc[i].base;
197         }
198
199         return (void *)NULL;
200 }
201
202 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
203 extern u8 acpi_slp_type;
204 #endif
205
206 #if CONFIG_EARLY_CBMEM_INIT || !defined(__PRE_RAM__)
207 /* Returns True if it was not intialized before. */
208 int cbmem_initialize(void)
209 {
210         int rv = 0;
211
212 #ifdef __PRE_RAM__
213         extern unsigned long get_top_of_ram(void);
214         uint64_t high_tables_base = get_top_of_ram() - HIGH_MEMORY_SIZE;
215         uint64_t high_tables_size = HIGH_MEMORY_SIZE;
216 #endif
217
218         /* We expect the romstage to always initialize it. */
219         if (!cbmem_reinit(high_tables_base)) {
220 #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
221                 /* Something went wrong, our high memory area got wiped */
222                 if (acpi_slp_type == 3 || acpi_slp_type == 2)
223                         acpi_slp_type = 0;
224 #endif
225                 cbmem_init(high_tables_base, high_tables_size);
226                 rv = 1;
227         }
228 #ifndef __PRE_RAM__
229         cbmem_arch_init();
230 #endif
231         return rv;
232 }
233 #endif
234
235 #ifndef __PRE_RAM__
236 void cbmem_list(void)
237 {
238         struct cbmem_entry *cbmem_toc;
239         int i;
240         cbmem_toc = get_cbmem_toc();
241
242         if (cbmem_toc == NULL)
243                 return;
244
245         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
246
247                 if (cbmem_toc[i].magic != CBMEM_MAGIC)
248                         continue;
249                 printk(BIOS_DEBUG, "%2d. ", i);
250                 switch (cbmem_toc[i].id) {
251                 case CBMEM_ID_FREESPACE: printk(BIOS_DEBUG, "FREE SPACE "); break;
252                 case CBMEM_ID_GDT:       printk(BIOS_DEBUG, "GDT        "); break;
253                 case CBMEM_ID_ACPI:      printk(BIOS_DEBUG, "ACPI       "); break;
254                 case CBMEM_ID_CBTABLE:   printk(BIOS_DEBUG, "COREBOOT   "); break;
255                 case CBMEM_ID_PIRQ:      printk(BIOS_DEBUG, "IRQ TABLE  "); break;
256                 case CBMEM_ID_MPTABLE:   printk(BIOS_DEBUG, "SMP TABLE  "); break;
257                 case CBMEM_ID_RESUME:    printk(BIOS_DEBUG, "ACPI RESUME"); break;
258                 case CBMEM_ID_SMBIOS:    printk(BIOS_DEBUG, "SMBIOS     "); break;
259                 case CBMEM_ID_TIMESTAMP: printk(BIOS_DEBUG, "TIME STAMP "); 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