printk_foo -> printk(BIOS_FOO, ...)
[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 #if 1
26 #define debug(x...) printk(BIOS_DEBUG, x)
27 #else
28 #define debug(x...)
29 #endif
30
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
37
38 struct cbmem_entry {
39         u32 magic;
40         u32 id;
41         u64 base;
42         u64 size;
43 } __attribute__((packed));
44
45 #ifndef __PRE_RAM__
46 struct cbmem_entry *bss_cbmem_toc;
47 #endif
48
49 /**
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)
55  *  - ACPI tables
56  *  - suspend/resume backup memory
57  */
58
59 void cbmem_init(u64 baseaddr, u64 size)
60 {
61         struct cbmem_entry *cbmem_toc;
62         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
63
64 #ifndef __PRE_RAM__
65         bss_cbmem_toc = cbmem_toc;
66 #endif
67         
68         debug("Initializing CBMEM area to 0x%llx (%lld bytes)\n", baseaddr, size);
69
70         if (size < (64 * 1024)) {
71                 debug("Increase CBMEM size!!\n");
72                 for (;;) ;
73         }
74
75         memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
76
77         cbmem_toc[0] = (struct cbmem_entry) {
78                 .magic  = CBMEM_MAGIC,
79                 .id     = CBMEM_ID_FREESPACE,
80                 .base   = baseaddr + CBMEM_TOC_RESERVED,
81                 .size   = size - CBMEM_TOC_RESERVED
82         };
83 }
84
85 int cbmem_reinit(u64 baseaddr)
86 {
87         struct cbmem_entry *cbmem_toc;
88         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
89
90         debug("Re-Initializing CBMEM area to 0x%lx\n", (unsigned long)baseaddr);
91 #ifndef __PRE_RAM__
92         bss_cbmem_toc = cbmem_toc;
93 #endif
94
95         return (cbmem_toc[0].magic == CBMEM_MAGIC);
96 }
97
98 void *cbmem_add(u32 id, u64 size)
99 {
100         struct cbmem_entry *cbmem_toc;
101         int i;
102 #ifdef __PRE_RAM__
103          cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
104 #else
105          cbmem_toc = bss_cbmem_toc;
106 #endif
107         
108         if (cbmem_toc == NULL) {
109                 return NULL;
110         }
111
112         if (cbmem_toc[0].magic != CBMEM_MAGIC) {
113                 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
114                 return NULL;
115         }
116
117         /* Will the entry fit at all? */
118         if (size > cbmem_toc[0].size) {
119                 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
120                 return NULL;
121         }
122
123         /* Align size to 512 byte blocks */
124
125         size = ALIGN(size, 512) < cbmem_toc[0].size ? 
126                 ALIGN(size, 512) : cbmem_toc[0].size;
127
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)
131                         break;
132         }
133
134         if (i >= MAX_CBMEM_ENTRIES) {
135                 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
136                 return NULL;
137         }
138
139         debug("Adding CBMEM entry as no. %d\n", i);
140
141         cbmem_toc[i] = (struct cbmem_entry) {
142                 .magic = CBMEM_MAGIC,
143                 .id     = id,
144                 .base   = cbmem_toc[0].base,
145                 .size   = size
146         };
147
148         cbmem_toc[0].base += size;
149         cbmem_toc[0].size -= size;
150
151         return (void *)(u32)cbmem_toc[i].base;
152 }
153
154 void *cbmem_find(u32 id)
155 {
156         struct cbmem_entry *cbmem_toc;
157         int i;
158 #ifdef __PRE_RAM__
159          cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
160 #else
161          cbmem_toc = bss_cbmem_toc;
162 #endif
163         
164         if (cbmem_toc == NULL)
165                 return NULL;
166
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;
170         }
171
172         return (void *)NULL;
173 }
174
175 #ifndef __PRE_RAM__
176 #if CONFIG_HAVE_ACPI_RESUME
177 extern u8 acpi_slp_type;
178 #endif
179 extern uint64_t high_tables_base, high_tables_size;
180
181 void cbmem_initialize(void)
182 {
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 */
187                         acpi_slp_type = 0;
188                         cbmem_init(high_tables_base, high_tables_size);
189                 }
190         } else {
191                 cbmem_init(high_tables_base, high_tables_size);
192         }
193 #else
194         cbmem_init(high_tables_base, high_tables_size);
195 #endif
196         cbmem_arch_init();
197 }
198
199 #ifndef __PRE_RAM__
200 void cbmem_list(void)
201 {
202         struct cbmem_entry *cbmem_toc;
203         int i;
204 #ifdef __PRE_RAM__
205          cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
206 #else
207          cbmem_toc = bss_cbmem_toc;
208 #endif
209         
210         if (cbmem_toc == NULL)
211                 return;
212
213         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
214
215                 if (cbmem_toc[i].magic != CBMEM_MAGIC)
216                         continue;
217                 printk(BIOS_DEBUG, "%2d. ", i);
218                 switch (cbmem_toc[i].id) {
219                 case CBMEM_ID_FREESPACE: printk(BIOS_DEBUG, "FREE SPACE "); break;
220                 case CBMEM_ID_GDT:       printk(BIOS_DEBUG, "GDT        "); break;
221                 case CBMEM_ID_ACPI:      printk(BIOS_DEBUG, "ACPI       "); break;
222                 case CBMEM_ID_CBTABLE:   printk(BIOS_DEBUG, "COREBOOT   "); break;
223                 case CBMEM_ID_PIRQ:      printk(BIOS_DEBUG, "IRQ TABLE  "); break;
224                 case CBMEM_ID_MPTABLE:   printk(BIOS_DEBUG, "SMP TABLE  "); break;
225                 case CBMEM_ID_RESUME:    printk(BIOS_DEBUG, "ACPI RESUME"); break;
226                 default: printk(BIOS_DEBUG, "%08x ", cbmem_toc[i].id);
227                 }
228                 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
229                 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);
230         }
231 }
232 #endif
233
234 #endif
235