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