7e3e20123a543e8372b41d794d6a4d64ce695efb
[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 #define get_cbmem_toc() bss_cbmem_toc
48 #else
49 #define get_cbmem_toc() (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE)
50 #endif
51
52 /**
53  * cbmem is a simple mechanism to do some kind of book keeping of the coreboot
54  * high tables memory. This is a small amount of memory which is "stolen" from
55  * the system memory for coreboot purposes. Usually this memory is used for
56  *  - the coreboot table
57  *  - legacy tables (PIRQ, MP table)
58  *  - ACPI tables
59  *  - suspend/resume backup memory
60  */
61
62 void cbmem_init(u64 baseaddr, u64 size)
63 {
64         struct cbmem_entry *cbmem_toc;
65         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
66
67 #ifndef __PRE_RAM__
68         bss_cbmem_toc = cbmem_toc;
69 #endif
70
71         debug("Initializing CBMEM area to 0x%llx (%lld bytes)\n", baseaddr, size);
72
73         if (size < (64 * 1024)) {
74                 debug("Increase CBMEM size!!\n");
75                 for (;;) ;
76         }
77
78         memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
79
80         cbmem_toc[0] = (struct cbmem_entry) {
81                 .magic  = CBMEM_MAGIC,
82                 .id     = CBMEM_ID_FREESPACE,
83                 .base   = baseaddr + CBMEM_TOC_RESERVED,
84                 .size   = size - CBMEM_TOC_RESERVED
85         };
86 }
87
88 int cbmem_reinit(u64 baseaddr)
89 {
90         struct cbmem_entry *cbmem_toc;
91         cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
92
93         debug("Re-Initializing CBMEM area to 0x%lx\n", (unsigned long)baseaddr);
94 #ifndef __PRE_RAM__
95         bss_cbmem_toc = cbmem_toc;
96 #endif
97
98         return (cbmem_toc[0].magic == CBMEM_MAGIC);
99 }
100
101 void *cbmem_add(u32 id, u64 size)
102 {
103         struct cbmem_entry *cbmem_toc;
104         int i;
105         cbmem_toc = get_cbmem_toc();
106
107         if (cbmem_toc == NULL) {
108                 return NULL;
109         }
110
111         if (cbmem_toc[0].magic != CBMEM_MAGIC) {
112                 printk(BIOS_ERR, "ERROR: CBMEM was not initialized yet.\n");
113                 return NULL;
114         }
115
116         /* Will the entry fit at all? */
117         if (size > cbmem_toc[0].size) {
118                 printk(BIOS_ERR, "ERROR: Not enough memory for table %x\n", id);
119                 return NULL;
120         }
121
122         /* Align size to 512 byte blocks */
123
124         size = ALIGN(size, 512) < cbmem_toc[0].size ?
125                 ALIGN(size, 512) : cbmem_toc[0].size;
126
127         /* Now look for the first free/usable TOC entry */
128         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
129                 if (cbmem_toc[i].id == CBMEM_ID_NONE)
130                         break;
131         }
132
133         if (i >= MAX_CBMEM_ENTRIES) {
134                 printk(BIOS_ERR, "ERROR: No more CBMEM entries available.\n");
135                 return NULL;
136         }
137
138         debug("Adding CBMEM entry as no. %d\n", i);
139
140         cbmem_toc[i] = (struct cbmem_entry) {
141                 .magic = CBMEM_MAGIC,
142                 .id     = id,
143                 .base   = cbmem_toc[0].base,
144                 .size   = size
145         };
146
147         cbmem_toc[0].base += size;
148         cbmem_toc[0].size -= size;
149
150         return (void *)(u32)cbmem_toc[i].base;
151 }
152
153 void *cbmem_find(u32 id)
154 {
155         struct cbmem_entry *cbmem_toc;
156         int i;
157         cbmem_toc = get_cbmem_toc();
158
159         if (cbmem_toc == NULL)
160                 return NULL;
161
162         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
163                 if (cbmem_toc[i].id == id)
164                         return (void *)(unsigned long)cbmem_toc[i].base;
165         }
166
167         return (void *)NULL;
168 }
169
170 #ifndef __PRE_RAM__
171 #if CONFIG_HAVE_ACPI_RESUME
172 extern u8 acpi_slp_type;
173 #endif
174 extern uint64_t high_tables_base, high_tables_size;
175
176 void cbmem_initialize(void)
177 {
178 #if CONFIG_HAVE_ACPI_RESUME
179         if (acpi_slp_type == 3) {
180                 if (!cbmem_reinit(high_tables_base)) {
181                         /* Something went wrong, our high memory area got wiped */
182                         acpi_slp_type = 0;
183                         cbmem_init(high_tables_base, high_tables_size);
184                 }
185         } else {
186                 cbmem_init(high_tables_base, high_tables_size);
187         }
188 #else
189         cbmem_init(high_tables_base, high_tables_size);
190 #endif
191         cbmem_arch_init();
192 }
193
194 #ifndef __PRE_RAM__
195 void cbmem_list(void)
196 {
197         struct cbmem_entry *cbmem_toc;
198         int i;
199         cbmem_toc = get_cbmem_toc();
200
201         if (cbmem_toc == NULL)
202                 return;
203
204         for (i = 0; i < MAX_CBMEM_ENTRIES; i++) {
205
206                 if (cbmem_toc[i].magic != CBMEM_MAGIC)
207                         continue;
208                 printk(BIOS_DEBUG, "%2d. ", i);
209                 switch (cbmem_toc[i].id) {
210                 case CBMEM_ID_FREESPACE: printk(BIOS_DEBUG, "FREE SPACE "); break;
211                 case CBMEM_ID_GDT:       printk(BIOS_DEBUG, "GDT        "); break;
212                 case CBMEM_ID_ACPI:      printk(BIOS_DEBUG, "ACPI       "); break;
213                 case CBMEM_ID_CBTABLE:   printk(BIOS_DEBUG, "COREBOOT   "); break;
214                 case CBMEM_ID_PIRQ:      printk(BIOS_DEBUG, "IRQ TABLE  "); break;
215                 case CBMEM_ID_MPTABLE:   printk(BIOS_DEBUG, "SMP TABLE  "); break;
216                 case CBMEM_ID_RESUME:    printk(BIOS_DEBUG, "ACPI RESUME"); break;
217                 default: printk(BIOS_DEBUG, "%08x ", cbmem_toc[i].id);
218                 }
219                 printk(BIOS_DEBUG, "%08llx ", cbmem_toc[i].base);
220                 printk(BIOS_DEBUG, "%08llx\n", cbmem_toc[i].size);
221         }
222 }
223 #endif
224
225 #endif
226