Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / arch / i386 / boot / coreboot_table.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003-2004 Eric Biederman
5  * Copyright (C) 2005-2010 coresystems GmbH
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; version 2 of
10  * the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20  * MA 02110-1301 USA
21  */
22
23 #include <console/console.h>
24 #include <ip_checksum.h>
25 #include <boot/tables.h>
26 #include <boot/coreboot_tables.h>
27 #include <arch/coreboot_tables.h>
28 #include <string.h>
29 #include <version.h>
30 #include <device/device.h>
31 #include <stdlib.h>
32 #if (CONFIG_HAVE_OPTION_TABLE == 1)
33 #include <option_table.h>
34 #endif
35
36 static struct lb_header *lb_table_init(unsigned long addr)
37 {
38         struct lb_header *header;
39
40         /* 16 byte align the address */
41         addr += 15;
42         addr &= ~15;
43
44         header = (void *)addr;
45         header->signature[0] = 'L';
46         header->signature[1] = 'B';
47         header->signature[2] = 'I';
48         header->signature[3] = 'O';
49         header->header_bytes = sizeof(*header);
50         header->header_checksum = 0;
51         header->table_bytes = 0;
52         header->table_checksum = 0;
53         header->table_entries = 0;
54         return header;
55 }
56
57 static struct lb_record *lb_first_record(struct lb_header *header)
58 {
59         struct lb_record *rec;
60         rec = (void *)(((char *)header) + sizeof(*header));
61         return rec;
62 }
63
64 static struct lb_record *lb_last_record(struct lb_header *header)
65 {
66         struct lb_record *rec;
67         rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
68         return rec;
69 }
70
71 #if 0
72 static struct lb_record *lb_next_record(struct lb_record *rec)
73 {
74         rec = (void *)(((char *)rec) + rec->size);
75         return rec;
76 }
77 #endif
78
79 static struct lb_record *lb_new_record(struct lb_header *header)
80 {
81         struct lb_record *rec;
82         rec = lb_last_record(header);
83         if (header->table_entries) {
84                 header->table_bytes += rec->size;
85         }
86         rec = lb_last_record(header);
87         header->table_entries++;
88         rec->tag = LB_TAG_UNUSED;
89         rec->size = sizeof(*rec);
90         return rec;
91 }
92
93
94 static struct lb_memory *lb_memory(struct lb_header *header)
95 {
96         struct lb_record *rec;
97         struct lb_memory *mem;
98         rec = lb_new_record(header);
99         mem = (struct lb_memory *)rec;
100         mem->tag = LB_TAG_MEMORY;
101         mem->size = sizeof(*mem);
102         return mem;
103 }
104
105 static struct lb_serial *lb_serial(struct lb_header *header)
106 {
107 #if CONFIG_CONSOLE_SERIAL8250
108         struct lb_record *rec;
109         struct lb_serial *serial;
110         rec = lb_new_record(header);
111         serial = (struct lb_serial *)rec;
112         serial->tag = LB_TAG_SERIAL;
113         serial->size = sizeof(*serial);
114         serial->ioport = CONFIG_TTYS0_BASE;
115         serial->baud = CONFIG_TTYS0_BAUD;
116         return serial;
117 #else
118         return header;
119 #endif
120 }
121
122 static void add_console(struct lb_header *header, u16 consoletype)
123 {
124         struct lb_console *console;
125
126         console = (struct lb_console *)lb_new_record(header);
127         console->tag = LB_TAG_CONSOLE;
128         console->size = sizeof(*console);
129         console->type = consoletype;
130 }
131
132 static void lb_console(struct lb_header *header)
133 {
134 #if CONFIG_CONSOLE_SERIAL8250
135         add_console(header, LB_TAG_CONSOLE_SERIAL8250);
136 #endif
137 #if CONFIG_CONSOLE_VGA
138         add_console(header, LB_TAG_CONSOLE_VGA);
139 #endif
140 #if CONFIG_CONSOLE_BTEXT
141         add_console(header, LB_TAG_CONSOLE_BTEXT);
142 #endif
143 #if CONFIG_CONSOLE_LOGBUF
144         add_console(header, LB_TAG_CONSOLE_LOGBUF);
145 #endif
146 #if CONFIG_CONSOLE_SROM
147         add_console(header, LB_TAG_CONSOLE_SROM);
148 #endif
149 #if CONFIG_USBDEBUG_DIRECT
150         add_console(header, LB_TAG_CONSOLE_EHCI);
151 #endif
152 }
153
154 static void lb_framebuffer(struct lb_header *header)
155 {
156 #if defined(CONFIG_BOOTSPLASH) && CONFIG_BOOTSPLASH && CONFIG_COREBOOT_KEEP_FRAMEBUFFER
157         void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
158
159         struct lb_framebuffer *framebuffer;
160         framebuffer = (struct lb_framebuffer *)lb_new_record(header);
161         framebuffer->tag = LB_TAG_FRAMEBUFFER;
162         framebuffer->size = sizeof(*framebuffer);
163         fill_lb_framebuffer(framebuffer);
164 #endif
165 }
166
167 static struct lb_mainboard *lb_mainboard(struct lb_header *header)
168 {
169         struct lb_record *rec;
170         struct lb_mainboard *mainboard;
171         rec = lb_new_record(header);
172         mainboard = (struct lb_mainboard *)rec;
173         mainboard->tag = LB_TAG_MAINBOARD;
174
175         mainboard->size = (sizeof(*mainboard) +
176                 strlen(mainboard_vendor) + 1 +
177                 strlen(mainboard_part_number) + 1 +
178                 3) & ~3;
179
180         mainboard->vendor_idx = 0;
181         mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
182
183         memcpy(mainboard->strings + mainboard->vendor_idx,
184                 mainboard_vendor,      strlen(mainboard_vendor) + 1);
185         memcpy(mainboard->strings + mainboard->part_number_idx,
186                 mainboard_part_number, strlen(mainboard_part_number) + 1);
187
188         return mainboard;
189 }
190
191 #if (CONFIG_HAVE_OPTION_TABLE == 1)
192 static struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
193 {
194         struct lb_record *rec;
195         struct cmos_checksum *cmos_checksum;
196         rec = lb_new_record(header);
197         cmos_checksum = (struct cmos_checksum *)rec;
198         cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
199
200         cmos_checksum->size = (sizeof(*cmos_checksum));
201
202         cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
203         cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
204         cmos_checksum->location = LB_CKS_LOC * 8;
205         cmos_checksum->type = CHECKSUM_PCBIOS;
206
207         return cmos_checksum;
208 }
209 #endif
210
211 static void lb_strings(struct lb_header *header)
212 {
213         static const struct {
214                 uint32_t tag;
215                 const char *string;
216         } strings[] = {
217                 { LB_TAG_VERSION,        coreboot_version,        },
218                 { LB_TAG_EXTRA_VERSION,  coreboot_extra_version,  },
219                 { LB_TAG_BUILD,          coreboot_build,          },
220                 { LB_TAG_COMPILE_TIME,   coreboot_compile_time,   },
221                 { LB_TAG_COMPILE_BY,     coreboot_compile_by,     },
222                 { LB_TAG_COMPILE_HOST,   coreboot_compile_host,   },
223                 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
224                 { LB_TAG_COMPILER,       coreboot_compiler,       },
225                 { LB_TAG_LINKER,         coreboot_linker,         },
226                 { LB_TAG_ASSEMBLER,      coreboot_assembler,      },
227         };
228         unsigned int i;
229         for(i = 0; i < ARRAY_SIZE(strings); i++) {
230                 struct lb_string *rec;
231                 size_t len;
232                 rec = (struct lb_string *)lb_new_record(header);
233                 len = strlen(strings[i].string);
234                 rec->tag = strings[i].tag;
235                 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
236                 memcpy(rec->string, strings[i].string, len+1);
237         }
238
239 }
240
241 #if CONFIG_WRITE_HIGH_TABLES == 1
242 static struct lb_forward *lb_forward(struct lb_header *header, struct lb_header *next_header)
243 {
244         struct lb_record *rec;
245         struct lb_forward *forward;
246         rec = lb_new_record(header);
247         forward = (struct lb_forward *)rec;
248         forward->tag = LB_TAG_FORWARD;
249         forward->size = sizeof(*forward);
250         forward->forward = (uint64_t)(unsigned long)next_header;
251         return forward;
252 }
253 #endif
254
255 void lb_memory_range(struct lb_memory *mem,
256         uint32_t type, uint64_t start, uint64_t size)
257 {
258         int entries;
259         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
260         mem->map[entries].start = pack_lb64(start);
261         mem->map[entries].size = pack_lb64(size);
262         mem->map[entries].type = type;
263         mem->size += sizeof(mem->map[0]);
264 }
265
266 static void lb_reserve_table_memory(struct lb_header *head)
267 {
268         struct lb_record *last_rec;
269         struct lb_memory *mem;
270         uint64_t start;
271         uint64_t end;
272         int i, entries;
273
274         last_rec = lb_last_record(head);
275         mem = get_lb_mem();
276         start = (unsigned long)head;
277         end = (unsigned long)last_rec;
278         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
279         /* Resize the right two memory areas so this table is in
280          * a reserved area of memory.  Everything has been carefully
281          * setup so that is all we need to do.
282          */
283         for(i = 0; i < entries; i++ ) {
284                 uint64_t map_start = unpack_lb64(mem->map[i].start);
285                 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
286                 /* Does this area need to be expanded? */
287                 if (map_end == start) {
288                         mem->map[i].size = pack_lb64(end - map_start);
289                 }
290                 /* Does this area need to be contracted? */
291                 else if (map_start == start) {
292                         mem->map[i].start = pack_lb64(end);
293                         mem->map[i].size = pack_lb64(map_end - end);
294                 }
295         }
296 }
297
298 static unsigned long lb_table_fini(struct lb_header *head, int fixup)
299 {
300         struct lb_record *rec, *first_rec;
301         rec = lb_last_record(head);
302         if (head->table_entries) {
303                 head->table_bytes += rec->size;
304         }
305
306         if (fixup)
307                 lb_reserve_table_memory(head);
308
309         first_rec = lb_first_record(head);
310         head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
311         head->header_checksum = 0;
312         head->header_checksum = compute_ip_checksum(head, sizeof(*head));
313         printk(BIOS_DEBUG, "Wrote coreboot table at: %p - %p  checksum %x\n",
314                 head, rec, head->table_checksum);
315         return (unsigned long)rec;
316 }
317
318 static void lb_cleanup_memory_ranges(struct lb_memory *mem)
319 {
320         int entries;
321         int i, j;
322         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
323
324         /* Sort the lb memory ranges */
325         for(i = 0; i < entries; i++) {
326                 uint64_t entry_start = unpack_lb64(mem->map[i].start);
327                 for(j = i; j < entries; j++) {
328                         uint64_t temp_start = unpack_lb64(mem->map[j].start);
329                         if (temp_start < entry_start) {
330                                 struct lb_memory_range tmp;
331                                 tmp = mem->map[i];
332                                 mem->map[i] = mem->map[j];
333                                 mem->map[j] = tmp;
334                         }
335                 }
336         }
337
338         /* Merge adjacent entries */
339         for(i = 0; (i + 1) < entries; i++) {
340                 uint64_t start, end, nstart, nend;
341                 if (mem->map[i].type != mem->map[i + 1].type) {
342                         continue;
343                 }
344                 start  = unpack_lb64(mem->map[i].start);
345                 end    = start + unpack_lb64(mem->map[i].size);
346                 nstart = unpack_lb64(mem->map[i + 1].start);
347                 nend   = nstart + unpack_lb64(mem->map[i + 1].size);
348                 if ((start <= nstart) && (end > nstart)) {
349                         if (start > nstart) {
350                                 start = nstart;
351                         }
352                         if (end < nend) {
353                                 end = nend;
354                         }
355                         /* Record the new region size */
356                         mem->map[i].start = pack_lb64(start);
357                         mem->map[i].size  = pack_lb64(end - start);
358
359                         /* Delete the entry I have merged with */
360                         memmove(&mem->map[i + 1], &mem->map[i + 2],
361                                 ((entries - i - 2) * sizeof(mem->map[0])));
362                         mem->size -= sizeof(mem->map[0]);
363                         entries -= 1;
364                         /* See if I can merge with the next entry as well */
365                         i -= 1;
366                 }
367         }
368 }
369
370 static void lb_remove_memory_range(struct lb_memory *mem,
371         uint64_t start, uint64_t size)
372 {
373         uint64_t end;
374         int entries;
375         int i;
376
377         end = start + size;
378         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
379
380         /* Remove a reserved area from the memory map */
381         for(i = 0; i < entries; i++) {
382                 uint64_t map_start = unpack_lb64(mem->map[i].start);
383                 uint64_t map_end   = map_start + unpack_lb64(mem->map[i].size);
384                 if ((start <= map_start) && (end >= map_end)) {
385                         /* Remove the completely covered range */
386                         memmove(&mem->map[i], &mem->map[i + 1],
387                                 ((entries - i - 1) * sizeof(mem->map[0])));
388                         mem->size -= sizeof(mem->map[0]);
389                         entries -= 1;
390                         /* Since the index will disappear revisit what will appear here */
391                         i -= 1;
392                 }
393                 else if ((start > map_start) && (end < map_end)) {
394                         /* Split the memory range */
395                         memmove(&mem->map[i + 1], &mem->map[i],
396                                 ((entries - i) * sizeof(mem->map[0])));
397                         mem->size += sizeof(mem->map[0]);
398                         entries += 1;
399                         /* Update the first map entry */
400                         mem->map[i].size = pack_lb64(start - map_start);
401                         /* Update the second map entry */
402                         mem->map[i + 1].start = pack_lb64(end);
403                         mem->map[i + 1].size  = pack_lb64(map_end - end);
404                         /* Don't bother with this map entry again */
405                         i += 1;
406                 }
407                 else if ((start <= map_start) && (end > map_start)) {
408                         /* Shrink the start of the memory range */
409                         mem->map[i].start = pack_lb64(end);
410                         mem->map[i].size  = pack_lb64(map_end - end);
411                 }
412                 else if ((start < map_end) && (start > map_start)) {
413                         /* Shrink the end of the memory range */
414                         mem->map[i].size = pack_lb64(start - map_start);
415                 }
416         }
417 }
418
419 /* This function is used in mainboard specific code, too */
420 void lb_add_memory_range(struct lb_memory *mem,
421         uint32_t type, uint64_t start, uint64_t size)
422 {
423         lb_remove_memory_range(mem, start, size);
424         lb_memory_range(mem, type, start, size);
425         lb_cleanup_memory_ranges(mem);
426 }
427
428 static void lb_dump_memory_ranges(struct lb_memory *mem)
429 {
430         int entries;
431         int i;
432         entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
433
434         printk(BIOS_DEBUG, "coreboot memory table:\n");
435         for(i = 0; i < entries; i++) {
436                 uint64_t entry_start = unpack_lb64(mem->map[i].start);
437                 uint64_t entry_size = unpack_lb64(mem->map[i].size);
438                 const char *entry_type;
439
440                 switch (mem->map[i].type) {
441                 case LB_MEM_RAM: entry_type="RAM"; break;
442                 case LB_MEM_RESERVED: entry_type="RESERVED"; break;
443                 case LB_MEM_ACPI: entry_type="ACPI"; break;
444                 case LB_MEM_NVS: entry_type="NVS"; break;
445                 case LB_MEM_UNUSABLE: entry_type="UNUSABLE"; break;
446                 case LB_MEM_VENDOR_RSVD: entry_type="VENDOR RESERVED"; break;
447                 case LB_MEM_TABLE: entry_type="CONFIGURATION TABLES"; break;
448                 default: entry_type="UNKNOWN!"; break;
449                 }
450
451                 printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n",
452                         i, entry_start, entry_start+entry_size-1, entry_type);
453
454         }
455 }
456
457
458 /* Routines to extract part so the coreboot table or
459  * information from the coreboot table after we have written it.
460  * Currently get_lb_mem relies on a global we can change the
461  * implementaiton.
462  */
463 static struct lb_memory *mem_ranges = 0;
464 struct lb_memory *get_lb_mem(void)
465 {
466         return mem_ranges;
467 }
468
469 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
470 {
471         struct lb_memory *mem = gp;
472         lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
473 }
474
475 static struct lb_memory *build_lb_mem(struct lb_header *head)
476 {
477         struct lb_memory *mem;
478
479         /* Record where the lb memory ranges will live */
480         mem = lb_memory(head);
481         mem_ranges = mem;
482
483         /* Build the raw table of memory */
484         search_global_resources(
485                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
486                 build_lb_mem_range, mem);
487         lb_cleanup_memory_ranges(mem);
488         return mem;
489 }
490
491 #if CONFIG_WRITE_HIGH_TABLES == 1
492 extern uint64_t high_tables_base, high_tables_size;
493 #endif
494
495 unsigned long write_coreboot_table(
496         unsigned long low_table_start, unsigned long low_table_end,
497         unsigned long rom_table_start, unsigned long rom_table_end)
498 {
499         struct lb_header *head;
500         struct lb_memory *mem;
501
502 #if CONFIG_WRITE_HIGH_TABLES == 1
503         printk(BIOS_DEBUG, "Writing high table forward entry at 0x%08lx\n",
504                         low_table_end);
505         head = lb_table_init(low_table_end);
506         lb_forward(head, (struct lb_header*)rom_table_end);
507
508         low_table_end = (unsigned long) lb_table_fini(head, 0);
509         printk(BIOS_DEBUG, "New low_table_end: 0x%08lx\n", low_table_end);
510         printk(BIOS_DEBUG, "Now going to write high coreboot table at 0x%08lx\n",
511                         rom_table_end);
512
513         head = lb_table_init(rom_table_end);
514         rom_table_end = (unsigned long)head;
515         printk(BIOS_DEBUG, "rom_table_end = 0x%08lx\n", rom_table_end);
516 #else
517         if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
518                 /* We need to put lbtable on  to [0xf0000,0x100000) */
519                 head = lb_table_init(rom_table_end);
520                 rom_table_end = (unsigned long)head;
521         } else {
522                 head = lb_table_init(low_table_end);
523                 low_table_end = (unsigned long)head;
524         }
525 #endif
526
527         printk(BIOS_DEBUG, "Adjust low_table_end from 0x%08lx to ", low_table_end);
528         low_table_end += 0xfff; // 4K aligned
529         low_table_end &= ~0xfff;
530         printk(BIOS_DEBUG, "0x%08lx \n", low_table_end);
531
532         /* The Linux kernel assumes this region is reserved */
533         printk(BIOS_DEBUG, "Adjust rom_table_end from 0x%08lx to ", rom_table_end);
534         rom_table_end += 0xffff; // 64K align
535         rom_table_end &= ~0xffff;
536         printk(BIOS_DEBUG, "0x%08lx \n", rom_table_end);
537
538 #if (CONFIG_HAVE_OPTION_TABLE == 1)
539         {
540                 struct lb_record *rec_dest = lb_new_record(head);
541                 /* Copy the option config table, it's already a lb_record... */
542                 memcpy(rec_dest,  &option_table, option_table.size);
543                 /* Create cmos checksum entry in coreboot table */
544                 lb_cmos_checksum(head);
545         }
546 #endif
547         /* Record where RAM is located */
548         mem = build_lb_mem(head);
549
550         /* Record the mptable and the the lb_table (This will be adjusted later) */
551         lb_add_memory_range(mem, LB_MEM_TABLE,
552                 low_table_start, low_table_end - low_table_start);
553
554         /* Record the pirq table, acpi tables, and maybe the mptable */
555         lb_add_memory_range(mem, LB_MEM_TABLE,
556                 rom_table_start, rom_table_end-rom_table_start);
557
558 #if CONFIG_WRITE_HIGH_TABLES == 1
559         printk(BIOS_DEBUG, "Adding high table area\n");
560         // should this be LB_MEM_ACPI?
561         lb_add_memory_range(mem, LB_MEM_TABLE,
562                 high_tables_base, high_tables_size);
563 #endif
564
565 #if (CONFIG_HAVE_MAINBOARD_RESOURCES == 1)
566         add_mainboard_resources(mem);
567 #endif
568
569         lb_dump_memory_ranges(mem);
570
571         /* Note:
572          * I assume that there is always memory at immediately after
573          * the low_table_end.  This means that after I setup the coreboot table.
574          * I can trivially fixup the reserved memory ranges to hold the correct
575          * size of the coreboot table.
576          */
577
578         /* Record our motherboard */
579         lb_mainboard(head);
580         /* Record the serial port, if present */
581         lb_serial(head);
582         /* Record our console setup */
583         lb_console(head);
584         /* Record our various random string information */
585         lb_strings(head);
586         /* Record our framebuffer */
587         lb_framebuffer(head);
588
589         /* Remember where my valid memory ranges are */
590         return lb_table_fini(head, 1);
591
592 }