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