blob: 963ff385f56bf02871bdcbfb747397c2fe0fdaf1 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00002#include <ip_checksum.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +00003#include <boot/coreboot_tables.h>
4#include "coreboot_table.h"
Eric Biederman8ca8d762003-04-22 19:02:15 +00005#include <string.h>
6#include <version.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +00007#include <device/device.h>
8#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009
Eric Biederman8ca8d762003-04-22 19:02:15 +000010struct lb_header *lb_table_init(unsigned long addr)
11{
12 struct lb_header *header;
13
14 /* 16 byte align the address */
15 addr += 15;
16 addr &= ~15;
17
18 header = (void *)addr;
19 header->signature[0] = 'L';
20 header->signature[1] = 'B';
21 header->signature[2] = 'I';
22 header->signature[3] = 'O';
23 header->header_bytes = sizeof(*header);
24 header->header_checksum = 0;
25 header->table_bytes = 0;
26 header->table_checksum = 0;
27 header->table_entries = 0;
28 return header;
29}
30
31struct lb_record *lb_first_record(struct lb_header *header)
32{
33 struct lb_record *rec;
34 rec = (void *)(((char *)header) + sizeof(*header));
35 return rec;
36}
37
38struct lb_record *lb_last_record(struct lb_header *header)
39{
40 struct lb_record *rec;
41 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
42 return rec;
43}
44
45struct lb_record *lb_next_record(struct lb_record *rec)
46{
47 rec = (void *)(((char *)rec) + rec->size);
48 return rec;
49}
50
51struct lb_record *lb_new_record(struct lb_header *header)
52{
53 struct lb_record *rec;
54 rec = lb_last_record(header);
55 if (header->table_entries) {
56 header->table_bytes += rec->size;
57 }
58 rec = lb_last_record(header);
59 header->table_entries++;
60 rec->tag = LB_TAG_UNUSED;
61 rec->size = sizeof(*rec);
62 return rec;
63}
64
65
66struct lb_memory *lb_memory(struct lb_header *header)
67{
68 struct lb_record *rec;
69 struct lb_memory *mem;
70 rec = lb_new_record(header);
71 mem = (struct lb_memory *)rec;
72 mem->tag = LB_TAG_MEMORY;
73 mem->size = sizeof(*mem);
74 return mem;
75}
76
Patrick Georgi8c2a0c12008-01-25 18:28:18 +000077struct lb_serial *lb_serial(struct lb_header *header)
78{
79#if defined(TTYS0_BASE)
80 struct lb_record *rec;
81 struct lb_serial *serial;
82 rec = lb_new_record(header);
83 serial = (struct lb_serial *)rec;
84 serial->tag = LB_TAG_SERIAL;
85 serial->size = sizeof(*serial);
86 serial->ioport = TTYS0_BASE;
87 return serial;
88#else
89 return header;
90#endif
91}
92
Eric Biederman8ca8d762003-04-22 19:02:15 +000093struct lb_mainboard *lb_mainboard(struct lb_header *header)
94{
95 struct lb_record *rec;
96 struct lb_mainboard *mainboard;
97 rec = lb_new_record(header);
98 mainboard = (struct lb_mainboard *)rec;
99 mainboard->tag = LB_TAG_MAINBOARD;
100
101 mainboard->size = (sizeof(*mainboard) +
102 strlen(mainboard_vendor) + 1 +
103 strlen(mainboard_part_number) + 1 +
104 3) & ~3;
105
106 mainboard->vendor_idx = 0;
107 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
108
109 memcpy(mainboard->strings + mainboard->vendor_idx,
110 mainboard_vendor, strlen(mainboard_vendor) + 1);
111 memcpy(mainboard->strings + mainboard->part_number_idx,
112 mainboard_part_number, strlen(mainboard_part_number) + 1);
113
114 return mainboard;
115}
116
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000117struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
118{
119 struct lb_record *rec;
120 struct cmos_checksum *cmos_checksum;
121 rec = lb_new_record(header);
122 cmos_checksum = (struct cmos_checksum *)rec;
123 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
124
125 cmos_checksum->size = (sizeof(*cmos_checksum));
126
127 cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
128 cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
129 cmos_checksum->location = LB_CKS_LOC * 8;
130 cmos_checksum->type = CHECKSUM_PCBIOS;
131
132 return cmos_checksum;
133}
134
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135void lb_strings(struct lb_header *header)
136{
137 static const struct {
138 uint32_t tag;
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000139 const char *string;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000140 } strings[] = {
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000141 { LB_TAG_VERSION, coreboot_version, },
142 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
143 { LB_TAG_BUILD, coreboot_build, },
144 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
145 { LB_TAG_COMPILE_BY, coreboot_compile_by, },
146 { LB_TAG_COMPILE_HOST, coreboot_compile_host, },
147 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
148 { LB_TAG_COMPILER, coreboot_compiler, },
149 { LB_TAG_LINKER, coreboot_linker, },
150 { LB_TAG_ASSEMBLER, coreboot_assembler, },
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151 };
Eric Biederman52685572003-05-19 19:16:21 +0000152 unsigned int i;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
154 struct lb_string *rec;
155 size_t len;
156 rec = (struct lb_string *)lb_new_record(header);
157 len = strlen(strings[i].string);
158 rec->tag = strings[i].tag;
159 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
160 memcpy(rec->string, strings[i].string, len+1);
161 }
162
163}
164
Eric Biederman8ca8d762003-04-22 19:02:15 +0000165void lb_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000166 uint32_t type, uint64_t start, uint64_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000167{
168 int entries;
169 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Eric Biedermanec01aa92004-12-10 20:50:43 +0000170 mem->map[entries].start = pack_lb64(start);
171 mem->map[entries].size = pack_lb64(size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 mem->map[entries].type = type;
173 mem->size += sizeof(mem->map[0]);
174}
175
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176static void lb_reserve_table_memory(struct lb_header *head)
177{
178 struct lb_record *last_rec;
179 struct lb_memory *mem;
180 uint64_t start;
181 uint64_t end;
182 int i, entries;
183
184 last_rec = lb_last_record(head);
185 mem = get_lb_mem();
186 start = (unsigned long)head;
187 end = (unsigned long)last_rec;
188 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
189 /* Resize the right two memory areas so this table is in
190 * a reserved area of memory. Everything has been carefully
191 * setup so that is all we need to do.
192 */
193 for(i = 0; i < entries; i++ ) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000194 uint64_t map_start = unpack_lb64(mem->map[i].start);
195 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000196 /* Does this area need to be expanded? */
197 if (map_end == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000198 mem->map[i].size = pack_lb64(end - map_start);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199 }
200 /* Does this area need to be contracted? */
201 else if (map_start == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000202 mem->map[i].start = pack_lb64(end);
203 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000204 }
205 }
206}
207
Eric Biederman8ca8d762003-04-22 19:02:15 +0000208unsigned long lb_table_fini(struct lb_header *head)
209{
210 struct lb_record *rec, *first_rec;
211 rec = lb_last_record(head);
212 if (head->table_entries) {
213 head->table_bytes += rec->size;
214 }
215 lb_reserve_table_memory(head);
216 first_rec = lb_first_record(head);
217 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
218 head->header_checksum = 0;
219 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000220 printk_debug("Wrote coreboot table at: %p - %p checksum %lx\n",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000221 head, rec, head->table_checksum);
222 return (unsigned long)rec;
223}
224
Eric Biederman6e53f502004-10-27 08:53:57 +0000225static void lb_cleanup_memory_ranges(struct lb_memory *mem)
226{
227 int entries;
228 int i, j;
229 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
230
231 /* Sort the lb memory ranges */
232 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000233 uint64_t entry_start = unpack_lb64(mem->map[i].start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000234 for(j = i; j < entries; j++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000235 uint64_t temp_start = unpack_lb64(mem->map[j].start);
236 if (temp_start < entry_start) {
Eric Biederman6e53f502004-10-27 08:53:57 +0000237 struct lb_memory_range tmp;
238 tmp = mem->map[i];
239 mem->map[i] = mem->map[j];
240 mem->map[j] = tmp;
241 }
242 }
243 }
244
245 /* Merge adjacent entries */
246 for(i = 0; (i + 1) < entries; i++) {
247 uint64_t start, end, nstart, nend;
248 if (mem->map[i].type != mem->map[i + 1].type) {
249 continue;
250 }
Eric Biedermanec01aa92004-12-10 20:50:43 +0000251 start = unpack_lb64(mem->map[i].start);
252 end = start + unpack_lb64(mem->map[i].size);
253 nstart = unpack_lb64(mem->map[i + 1].start);
254 nend = nstart + unpack_lb64(mem->map[i + 1].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000255 if ((start <= nstart) && (end > nstart)) {
256 if (start > nstart) {
257 start = nstart;
258 }
259 if (end < nend) {
260 end = nend;
261 }
262 /* Record the new region size */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000263 mem->map[i].start = pack_lb64(start);
264 mem->map[i].size = pack_lb64(end - start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000265
266 /* Delete the entry I have merged with */
267 memmove(&mem->map[i + 1], &mem->map[i + 2],
268 ((entries - i - 2) * sizeof(mem->map[0])));
269 mem->size -= sizeof(mem->map[0]);
270 entries -= 1;
271 /* See if I can merge with the next entry as well */
272 i -= 1;
273 }
274 }
275}
276
277static void lb_remove_memory_range(struct lb_memory *mem,
278 uint64_t start, uint64_t size)
279{
280 uint64_t end;
281 int entries;
282 int i;
283
284 end = start + size;
285 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
286
287 /* Remove a reserved area from the memory map */
288 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000289 uint64_t map_start = unpack_lb64(mem->map[i].start);
290 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000291 if ((start <= map_start) && (end >= map_end)) {
292 /* Remove the completely covered range */
293 memmove(&mem->map[i], &mem->map[i + 1],
294 ((entries - i - 1) * sizeof(mem->map[0])));
295 mem->size -= sizeof(mem->map[0]);
296 entries -= 1;
297 /* Since the index will disappear revisit what will appear here */
298 i -= 1;
299 }
300 else if ((start > map_start) && (end < map_end)) {
301 /* Split the memory range */
302 memmove(&mem->map[i + 1], &mem->map[i],
303 ((entries - i) * sizeof(mem->map[0])));
304 mem->size += sizeof(mem->map[0]);
305 entries += 1;
306 /* Update the first map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000307 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000308 /* Update the second map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000309 mem->map[i + 1].start = pack_lb64(end);
310 mem->map[i + 1].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000311 /* Don't bother with this map entry again */
312 i += 1;
313 }
314 else if ((start <= map_start) && (end > map_start)) {
315 /* Shrink the start of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000316 mem->map[i].start = pack_lb64(end);
317 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000318 }
319 else if ((start < map_end) && (start > map_start)) {
320 /* Shrink the end of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000321 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000322 }
323 }
324}
325
326static void lb_add_memory_range(struct lb_memory *mem,
327 uint32_t type, uint64_t start, uint64_t size)
328{
329 lb_remove_memory_range(mem, start, size);
330 lb_memory_range(mem, type, start, size);
331 lb_cleanup_memory_ranges(mem);
332}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000334/* Routines to extract part so the coreboot table or
335 * information from the coreboot table after we have written it.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000336 * Currently get_lb_mem relies on a global we can change the
337 * implementaiton.
338 */
339static struct lb_memory *mem_ranges = 0;
340struct lb_memory *get_lb_mem(void)
341{
342 return mem_ranges;
343}
344
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000345static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
346{
347 struct lb_memory *mem = gp;
348 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
349}
350
Eric Biederman6e53f502004-10-27 08:53:57 +0000351static struct lb_memory *build_lb_mem(struct lb_header *head)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000352{
Eric Biederman6e53f502004-10-27 08:53:57 +0000353 struct lb_memory *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000354
Eric Biederman6e53f502004-10-27 08:53:57 +0000355 /* Record where the lb memory ranges will live */
356 mem = lb_memory(head);
357 mem_ranges = mem;
358
359 /* Build the raw table of memory */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000360 search_global_resources(
361 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
362 build_lb_mem_range, mem);
Eric Biederman6e53f502004-10-27 08:53:57 +0000363 lb_cleanup_memory_ranges(mem);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000364 return mem;
365}
366
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000367unsigned long write_coreboot_table(
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000368 unsigned long low_table_start, unsigned long low_table_end,
369 unsigned long rom_table_start, unsigned long rom_table_end)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000370{
371 unsigned long table_size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000372 struct lb_header *head;
373 struct lb_memory *mem;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000374
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000375 if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
376 /* We need to put lbtable on to [0xf0000,0x100000) */
377 head = lb_table_init(rom_table_end);
378 rom_table_end = (unsigned long)head;
379 } else {
380 head = lb_table_init(low_table_end);
381 low_table_end = (unsigned long)head;
382 }
Yinghai Lu47cb7c72007-04-06 20:27:40 +0000383
384 printk_debug("Adjust low_table_end from 0x%08x to ", low_table_end);
385 low_table_end += 0xfff; // 4K aligned
386 low_table_end &= ~0xfff;
387 printk_debug("0x%08x \n", low_table_end);
388
389 /* The Linux kernel assumes this region is reserved */
390 printk_debug("Adjust rom_table_end from 0x%08x to ", rom_table_end);
391 rom_table_end += 0xffff; // 64K align
392 rom_table_end &= ~0xffff;
393 printk_debug("0x%08x \n", rom_table_end);
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000394
Stefan Reinauerba430642007-04-06 12:14:51 +0000395#if (HAVE_OPTION_TABLE == 1)
396 {
Eric Biederman6e53f502004-10-27 08:53:57 +0000397 struct lb_record *rec_dest, *rec_src;
398 /* Write the option config table... */
399 rec_dest = lb_new_record(head);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000400 rec_src = (struct lb_record *)(void *)&option_table;
Eric Biederman6e53f502004-10-27 08:53:57 +0000401 memcpy(rec_dest, rec_src, rec_src->size);
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000402 /* Create cmos checksum entry in coreboot table */
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000403 lb_cmos_checksum(head);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404 }
Stefan Reinauerba430642007-04-06 12:14:51 +0000405#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000406 /* Record where RAM is located */
407 mem = build_lb_mem(head);
408
Eric Biederman6e53f502004-10-27 08:53:57 +0000409 /* Record the mptable and the the lb_table (This will be adjusted later) */
410 lb_add_memory_range(mem, LB_MEM_TABLE,
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000411 low_table_start, low_table_end - low_table_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000412
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000413 /* Record the pirq table, acpi tables, and maybe the mptable */
Roman Kononov57e700f2007-02-01 00:44:27 +0000414 table_size=rom_table_end-rom_table_start;
Eric Biederman6e53f502004-10-27 08:53:57 +0000415 lb_add_memory_range(mem, LB_MEM_TABLE,
Roman Kononov57e700f2007-02-01 00:44:27 +0000416 rom_table_start, table_size<0x10000?0x10000:table_size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000417
418 /* Note:
419 * I assume that there is always memory at immediately after
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000420 * the low_table_end. This means that after I setup the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000421 * I can trivially fixup the reserved memory ranges to hold the correct
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000422 * size of the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000423 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000424
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000425 /* Record our motherboard */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000426 lb_mainboard(head);
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000427 /* Record the serial port, if present */
428 lb_serial(head);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000429 /* Record our various random string information */
430 lb_strings(head);
431
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432 /* Remember where my valid memory ranges are */
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000433 return lb_table_fini(head);
434
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435}