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