blob: b13b63ee658ea8283cd0f069dcd8b9d4641cb0d3 [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
77struct lb_mainboard *lb_mainboard(struct lb_header *header)
78{
79 struct lb_record *rec;
80 struct lb_mainboard *mainboard;
81 rec = lb_new_record(header);
82 mainboard = (struct lb_mainboard *)rec;
83 mainboard->tag = LB_TAG_MAINBOARD;
84
85 mainboard->size = (sizeof(*mainboard) +
86 strlen(mainboard_vendor) + 1 +
87 strlen(mainboard_part_number) + 1 +
88 3) & ~3;
89
90 mainboard->vendor_idx = 0;
91 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
92
93 memcpy(mainboard->strings + mainboard->vendor_idx,
94 mainboard_vendor, strlen(mainboard_vendor) + 1);
95 memcpy(mainboard->strings + mainboard->part_number_idx,
96 mainboard_part_number, strlen(mainboard_part_number) + 1);
97
98 return mainboard;
99}
100
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000101struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
102{
103 struct lb_record *rec;
104 struct cmos_checksum *cmos_checksum;
105 rec = lb_new_record(header);
106 cmos_checksum = (struct cmos_checksum *)rec;
107 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
108
109 cmos_checksum->size = (sizeof(*cmos_checksum));
110
111 cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
112 cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
113 cmos_checksum->location = LB_CKS_LOC * 8;
114 cmos_checksum->type = CHECKSUM_PCBIOS;
115
116 return cmos_checksum;
117}
118
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119void lb_strings(struct lb_header *header)
120{
121 static const struct {
122 uint32_t tag;
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000123 const char *string;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000124 } strings[] = {
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000125 { LB_TAG_VERSION, coreboot_version, },
126 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
127 { LB_TAG_BUILD, coreboot_build, },
128 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
129 { LB_TAG_COMPILE_BY, coreboot_compile_by, },
130 { LB_TAG_COMPILE_HOST, coreboot_compile_host, },
131 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
132 { LB_TAG_COMPILER, coreboot_compiler, },
133 { LB_TAG_LINKER, coreboot_linker, },
134 { LB_TAG_ASSEMBLER, coreboot_assembler, },
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135 };
Eric Biederman52685572003-05-19 19:16:21 +0000136 unsigned int i;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
138 struct lb_string *rec;
139 size_t len;
140 rec = (struct lb_string *)lb_new_record(header);
141 len = strlen(strings[i].string);
142 rec->tag = strings[i].tag;
143 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
144 memcpy(rec->string, strings[i].string, len+1);
145 }
146
147}
148
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149void lb_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000150 uint32_t type, uint64_t start, uint64_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000151{
152 int entries;
153 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Eric Biedermanec01aa92004-12-10 20:50:43 +0000154 mem->map[entries].start = pack_lb64(start);
155 mem->map[entries].size = pack_lb64(size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000156 mem->map[entries].type = type;
157 mem->size += sizeof(mem->map[0]);
158}
159
Eric Biederman8ca8d762003-04-22 19:02:15 +0000160static void lb_reserve_table_memory(struct lb_header *head)
161{
162 struct lb_record *last_rec;
163 struct lb_memory *mem;
164 uint64_t start;
165 uint64_t end;
166 int i, entries;
167
168 last_rec = lb_last_record(head);
169 mem = get_lb_mem();
170 start = (unsigned long)head;
171 end = (unsigned long)last_rec;
172 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
173 /* Resize the right two memory areas so this table is in
174 * a reserved area of memory. Everything has been carefully
175 * setup so that is all we need to do.
176 */
177 for(i = 0; i < entries; i++ ) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000178 uint64_t map_start = unpack_lb64(mem->map[i].start);
179 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180 /* Does this area need to be expanded? */
181 if (map_end == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000182 mem->map[i].size = pack_lb64(end - map_start);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183 }
184 /* Does this area need to be contracted? */
185 else if (map_start == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000186 mem->map[i].start = pack_lb64(end);
187 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000188 }
189 }
190}
191
Eric Biederman8ca8d762003-04-22 19:02:15 +0000192unsigned long lb_table_fini(struct lb_header *head)
193{
194 struct lb_record *rec, *first_rec;
195 rec = lb_last_record(head);
196 if (head->table_entries) {
197 head->table_bytes += rec->size;
198 }
199 lb_reserve_table_memory(head);
200 first_rec = lb_first_record(head);
201 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
202 head->header_checksum = 0;
203 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000204 printk_debug("Wrote coreboot table at: %p - %p checksum %lx\n",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000205 head, rec, head->table_checksum);
206 return (unsigned long)rec;
207}
208
Eric Biederman6e53f502004-10-27 08:53:57 +0000209static void lb_cleanup_memory_ranges(struct lb_memory *mem)
210{
211 int entries;
212 int i, j;
213 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
214
215 /* Sort the lb memory ranges */
216 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000217 uint64_t entry_start = unpack_lb64(mem->map[i].start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000218 for(j = i; j < entries; j++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000219 uint64_t temp_start = unpack_lb64(mem->map[j].start);
220 if (temp_start < entry_start) {
Eric Biederman6e53f502004-10-27 08:53:57 +0000221 struct lb_memory_range tmp;
222 tmp = mem->map[i];
223 mem->map[i] = mem->map[j];
224 mem->map[j] = tmp;
225 }
226 }
227 }
228
229 /* Merge adjacent entries */
230 for(i = 0; (i + 1) < entries; i++) {
231 uint64_t start, end, nstart, nend;
232 if (mem->map[i].type != mem->map[i + 1].type) {
233 continue;
234 }
Eric Biedermanec01aa92004-12-10 20:50:43 +0000235 start = unpack_lb64(mem->map[i].start);
236 end = start + unpack_lb64(mem->map[i].size);
237 nstart = unpack_lb64(mem->map[i + 1].start);
238 nend = nstart + unpack_lb64(mem->map[i + 1].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000239 if ((start <= nstart) && (end > nstart)) {
240 if (start > nstart) {
241 start = nstart;
242 }
243 if (end < nend) {
244 end = nend;
245 }
246 /* Record the new region size */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000247 mem->map[i].start = pack_lb64(start);
248 mem->map[i].size = pack_lb64(end - start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000249
250 /* Delete the entry I have merged with */
251 memmove(&mem->map[i + 1], &mem->map[i + 2],
252 ((entries - i - 2) * sizeof(mem->map[0])));
253 mem->size -= sizeof(mem->map[0]);
254 entries -= 1;
255 /* See if I can merge with the next entry as well */
256 i -= 1;
257 }
258 }
259}
260
261static void lb_remove_memory_range(struct lb_memory *mem,
262 uint64_t start, uint64_t size)
263{
264 uint64_t end;
265 int entries;
266 int i;
267
268 end = start + size;
269 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
270
271 /* Remove a reserved area from the memory map */
272 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000273 uint64_t map_start = unpack_lb64(mem->map[i].start);
274 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000275 if ((start <= map_start) && (end >= map_end)) {
276 /* Remove the completely covered range */
277 memmove(&mem->map[i], &mem->map[i + 1],
278 ((entries - i - 1) * sizeof(mem->map[0])));
279 mem->size -= sizeof(mem->map[0]);
280 entries -= 1;
281 /* Since the index will disappear revisit what will appear here */
282 i -= 1;
283 }
284 else if ((start > map_start) && (end < map_end)) {
285 /* Split the memory range */
286 memmove(&mem->map[i + 1], &mem->map[i],
287 ((entries - i) * sizeof(mem->map[0])));
288 mem->size += sizeof(mem->map[0]);
289 entries += 1;
290 /* Update the first map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000291 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000292 /* Update the second map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000293 mem->map[i + 1].start = pack_lb64(end);
294 mem->map[i + 1].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000295 /* Don't bother with this map entry again */
296 i += 1;
297 }
298 else if ((start <= map_start) && (end > map_start)) {
299 /* Shrink the start of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000300 mem->map[i].start = pack_lb64(end);
301 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000302 }
303 else if ((start < map_end) && (start > map_start)) {
304 /* Shrink the end of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000305 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000306 }
307 }
308}
309
310static void lb_add_memory_range(struct lb_memory *mem,
311 uint32_t type, uint64_t start, uint64_t size)
312{
313 lb_remove_memory_range(mem, start, size);
314 lb_memory_range(mem, type, start, size);
315 lb_cleanup_memory_ranges(mem);
316}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000317
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000318/* Routines to extract part so the coreboot table or
319 * information from the coreboot table after we have written it.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000320 * Currently get_lb_mem relies on a global we can change the
321 * implementaiton.
322 */
323static struct lb_memory *mem_ranges = 0;
324struct lb_memory *get_lb_mem(void)
325{
326 return mem_ranges;
327}
328
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000329static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
330{
331 struct lb_memory *mem = gp;
332 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
333}
334
Eric Biederman6e53f502004-10-27 08:53:57 +0000335static struct lb_memory *build_lb_mem(struct lb_header *head)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000336{
Eric Biederman6e53f502004-10-27 08:53:57 +0000337 struct lb_memory *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000338
Eric Biederman6e53f502004-10-27 08:53:57 +0000339 /* Record where the lb memory ranges will live */
340 mem = lb_memory(head);
341 mem_ranges = mem;
342
343 /* Build the raw table of memory */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000344 search_global_resources(
345 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
346 build_lb_mem_range, mem);
Eric Biederman6e53f502004-10-27 08:53:57 +0000347 lb_cleanup_memory_ranges(mem);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000348 return mem;
349}
350
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000351unsigned long write_coreboot_table(
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000352 unsigned long low_table_start, unsigned long low_table_end,
353 unsigned long rom_table_start, unsigned long rom_table_end)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000354{
355 unsigned long table_size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000356 struct lb_header *head;
357 struct lb_memory *mem;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000358
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000359 if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
360 /* We need to put lbtable on to [0xf0000,0x100000) */
361 head = lb_table_init(rom_table_end);
362 rom_table_end = (unsigned long)head;
363 } else {
364 head = lb_table_init(low_table_end);
365 low_table_end = (unsigned long)head;
366 }
Yinghai Lu47cb7c72007-04-06 20:27:40 +0000367
368 printk_debug("Adjust low_table_end from 0x%08x to ", low_table_end);
369 low_table_end += 0xfff; // 4K aligned
370 low_table_end &= ~0xfff;
371 printk_debug("0x%08x \n", low_table_end);
372
373 /* The Linux kernel assumes this region is reserved */
374 printk_debug("Adjust rom_table_end from 0x%08x to ", rom_table_end);
375 rom_table_end += 0xffff; // 64K align
376 rom_table_end &= ~0xffff;
377 printk_debug("0x%08x \n", rom_table_end);
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000378
Stefan Reinauerba430642007-04-06 12:14:51 +0000379#if (HAVE_OPTION_TABLE == 1)
380 {
Eric Biederman6e53f502004-10-27 08:53:57 +0000381 struct lb_record *rec_dest, *rec_src;
382 /* Write the option config table... */
383 rec_dest = lb_new_record(head);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000384 rec_src = (struct lb_record *)(void *)&option_table;
Eric Biederman6e53f502004-10-27 08:53:57 +0000385 memcpy(rec_dest, rec_src, rec_src->size);
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000386 /* Create cmos checksum entry in coreboot table */
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000387 lb_cmos_checksum(head);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000388 }
Stefan Reinauerba430642007-04-06 12:14:51 +0000389#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000390 /* Record where RAM is located */
391 mem = build_lb_mem(head);
392
Eric Biederman6e53f502004-10-27 08:53:57 +0000393 /* Record the mptable and the the lb_table (This will be adjusted later) */
394 lb_add_memory_range(mem, LB_MEM_TABLE,
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000395 low_table_start, low_table_end - low_table_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000396
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000397 /* Record the pirq table, acpi tables, and maybe the mptable */
Roman Kononov57e700f2007-02-01 00:44:27 +0000398 table_size=rom_table_end-rom_table_start;
Eric Biederman6e53f502004-10-27 08:53:57 +0000399 lb_add_memory_range(mem, LB_MEM_TABLE,
Roman Kononov57e700f2007-02-01 00:44:27 +0000400 rom_table_start, table_size<0x10000?0x10000:table_size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000401
402 /* Note:
403 * I assume that there is always memory at immediately after
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000404 * the low_table_end. This means that after I setup the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000405 * I can trivially fixup the reserved memory ranges to hold the correct
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000406 * size of the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000407 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000408
409 /* Record our motheboard */
410 lb_mainboard(head);
411 /* Record our various random string information */
412 lb_strings(head);
413
Eric Biederman8ca8d762003-04-22 19:02:15 +0000414 /* Remember where my valid memory ranges are */
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000415 return lb_table_fini(head);
416
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417}