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