blob: 84eb5a65a90e21401452a5b4d1847c228d99d1e8 [file] [log] [blame]
Stefan Reinauerb883d4c2009-08-27 11:23:06 +00001/*
2 * This file is part of the coreboot project.
Stefan Reinauer14e22772010-04-27 06:56:47 +00003 *
Stefan Reinauerb883d4c2009-08-27 11:23:06 +00004 * Copyright (C) 2003-2004 Eric Biederman
Stefan Reinauerb5828d72010-03-29 17:14:28 +00005 * Copyright (C) 2005-2010 coresystems GmbH
Stefan Reinauerb883d4c2009-08-27 11:23:06 +00006 *
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
Eric Biederman8ca8d762003-04-22 19:02:15 +000023#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <ip_checksum.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +000025#include <boot/coreboot_tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000026#include <string.h>
27#include <version.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000028#include <device/device.h>
29#include <stdlib.h>
Duncan Laurie66ecdc52011-08-15 16:35:10 -070030#include <cbfs.h>
Vadim Bendebury2e438672011-09-23 09:56:11 -070031#include <cbmem.h>
Stefan Reinauerc75bfde2011-08-15 11:26:35 -070032#if CONFIG_USE_OPTION_TABLE
Stefan Reinauer8e726b72010-03-29 23:01:35 +000033#include <option_table.h>
Stefan Reinauerb5828d72010-03-29 17:14:28 +000034#endif
Stefan Reinauer0e672b52012-04-27 00:34:54 +020035#if CONFIG_CHROMEOS
Stefan Reinauer3e4e3032013-03-20 14:08:04 -070036#if CONFIG_GENERATE_ACPI_TABLES
Stefan Reinauer0e672b52012-04-27 00:34:54 +020037#include <arch/acpi.h>
Stefan Reinauer3e4e3032013-03-20 14:08:04 -070038#endif
Aaron Durbindd32a312013-03-07 23:15:06 -060039#include <vendorcode/google/chromeos/chromeos.h>
Stefan Reinauer0e672b52012-04-27 00:34:54 +020040#include <vendorcode/google/chromeos/gnvs.h>
41#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000042
Stefan Reinauerb883d4c2009-08-27 11:23:06 +000043static struct lb_header *lb_table_init(unsigned long addr)
Eric Biederman8ca8d762003-04-22 19:02:15 +000044{
45 struct lb_header *header;
46
47 /* 16 byte align the address */
48 addr += 15;
49 addr &= ~15;
50
51 header = (void *)addr;
52 header->signature[0] = 'L';
53 header->signature[1] = 'B';
54 header->signature[2] = 'I';
55 header->signature[3] = 'O';
56 header->header_bytes = sizeof(*header);
57 header->header_checksum = 0;
58 header->table_bytes = 0;
59 header->table_checksum = 0;
60 header->table_entries = 0;
61 return header;
62}
63
Stefan Reinauerb883d4c2009-08-27 11:23:06 +000064static struct lb_record *lb_first_record(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +000065{
66 struct lb_record *rec;
67 rec = (void *)(((char *)header) + sizeof(*header));
68 return rec;
69}
70
Stefan Reinauerb883d4c2009-08-27 11:23:06 +000071static struct lb_record *lb_last_record(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +000072{
73 struct lb_record *rec;
74 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
75 return rec;
76}
77
Stefan Reinauerb883d4c2009-08-27 11:23:06 +000078static struct lb_record *lb_new_record(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +000079{
80 struct lb_record *rec;
81 rec = lb_last_record(header);
82 if (header->table_entries) {
83 header->table_bytes += rec->size;
84 }
85 rec = lb_last_record(header);
86 header->table_entries++;
87 rec->tag = LB_TAG_UNUSED;
88 rec->size = sizeof(*rec);
89 return rec;
90}
91
Stefan Reinauerb883d4c2009-08-27 11:23:06 +000092static struct lb_memory *lb_memory(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +000093{
94 struct lb_record *rec;
95 struct lb_memory *mem;
96 rec = lb_new_record(header);
97 mem = (struct lb_memory *)rec;
98 mem->tag = LB_TAG_MEMORY;
99 mem->size = sizeof(*mem);
100 return mem;
101}
102
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000103static struct lb_serial *lb_serial(struct lb_header *header)
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000104{
Myles Watsonec0ee642009-10-19 16:21:30 +0000105#if CONFIG_CONSOLE_SERIAL8250
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000106 struct lb_record *rec;
107 struct lb_serial *serial;
108 rec = lb_new_record(header);
109 serial = (struct lb_serial *)rec;
110 serial->tag = LB_TAG_SERIAL;
111 serial->size = sizeof(*serial);
Stefan Reinauerd1bc3312011-06-22 16:39:19 -0700112 serial->type = LB_SERIAL_TYPE_IO_MAPPED;
113 serial->baseaddr = CONFIG_TTYS0_BASE;
114 serial->baud = CONFIG_TTYS0_BAUD;
115 return serial;
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700116#elif CONFIG_CONSOLE_SERIAL8250MEM || CONFIG_CONSOLE_SERIAL_UART
Gabe Black32829ca2011-10-05 01:57:03 -0700117 if (uartmem_getbaseaddr()) {
118 struct lb_record *rec;
119 struct lb_serial *serial;
120 rec = lb_new_record(header);
121 serial = (struct lb_serial *)rec;
122 serial->tag = LB_TAG_SERIAL;
123 serial->size = sizeof(*serial);
124 serial->type = LB_SERIAL_TYPE_MEMORY_MAPPED;
125 serial->baseaddr = uartmem_getbaseaddr();
126 serial->baud = CONFIG_TTYS0_BAUD;
127 return serial;
128 } else {
129 return NULL;
130 }
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000131#else
Stefan Reinauer40e42a82011-04-14 21:05:41 +0000132 return NULL;
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000133#endif
134}
135
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700136#if CONFIG_CONSOLE_SERIAL || CONFIG_CONSOLE_LOGBUF || CONFIG_USBDEBUG
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000137static void add_console(struct lb_header *header, u16 consoletype)
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000138{
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000139 struct lb_console *console;
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000140
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000141 console = (struct lb_console *)lb_new_record(header);
142 console->tag = LB_TAG_CONSOLE;
143 console->size = sizeof(*console);
144 console->type = consoletype;
145}
Stefan Reinauer513eb5a2011-06-01 14:04:50 -0700146#endif
147
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000148static void lb_console(struct lb_header *header)
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000149{
Myles Watsonec0ee642009-10-19 16:21:30 +0000150#if CONFIG_CONSOLE_SERIAL8250
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000151 add_console(header, LB_TAG_CONSOLE_SERIAL8250);
152#endif
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700153#if CONFIG_CONSOLE_SERIAL8250MEM || CONFIG_CONSOLE_SERIAL_UART
Stefan Reinauer4885daa2011-04-26 23:47:04 +0000154 add_console(header, LB_TAG_CONSOLE_SERIAL8250MEM);
155#endif
Myles Watsonec0ee642009-10-19 16:21:30 +0000156#if CONFIG_CONSOLE_LOGBUF
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000157 add_console(header, LB_TAG_CONSOLE_LOGBUF);
158#endif
Stefan Reinauer7e00a442010-05-25 17:09:05 +0000159#if CONFIG_USBDEBUG
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000160 add_console(header, LB_TAG_CONSOLE_EHCI);
161#endif
162}
163
Stefan Reinauerd650e992010-02-22 04:33:13 +0000164static void lb_framebuffer(struct lb_header *header)
165{
Ronald G. Minnich69efaa02013-02-26 10:07:40 -0800166#if CONFIG_FRAMEBUFFER_KEEP_VESA_MODE || CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT
Stefan Reinauerd650e992010-02-22 04:33:13 +0000167 void fill_lb_framebuffer(struct lb_framebuffer *framebuffer);
Gabe Blackfb8632a2012-09-30 04:47:48 -0700168 int vbe_mode_info_valid(void);
Stefan Reinauerd650e992010-02-22 04:33:13 +0000169
Gabe Blackfb8632a2012-09-30 04:47:48 -0700170 // If there isn't any mode info to put in the table, don't ask for it
171 // to be filled with junk.
172 if (!vbe_mode_info_valid())
173 return;
Stefan Reinauerd650e992010-02-22 04:33:13 +0000174 struct lb_framebuffer *framebuffer;
175 framebuffer = (struct lb_framebuffer *)lb_new_record(header);
176 framebuffer->tag = LB_TAG_FRAMEBUFFER;
177 framebuffer->size = sizeof(*framebuffer);
178 fill_lb_framebuffer(framebuffer);
179#endif
180}
181
Stefan Reinauer31324c62012-04-05 21:22:02 +0200182#if CONFIG_CHROMEOS
183static void lb_gpios(struct lb_header *header)
184{
185 struct lb_gpios *gpios;
186 gpios = (struct lb_gpios *)lb_new_record(header);
187 gpios->tag = LB_TAG_GPIO;
188 gpios->size = sizeof(*gpios);
189 gpios->count = 0;
190 fill_lb_gpios(gpios);
191}
192
193static void lb_vdat(struct lb_header *header)
194{
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700195#if CONFIG_GENERATE_ACPI_TABLES
Stefan Reinauer31324c62012-04-05 21:22:02 +0200196 struct lb_vdat* vdat;
197
198 vdat = (struct lb_vdat *)lb_new_record(header);
199 vdat->tag = LB_TAG_VDAT;
200 vdat->size = sizeof(*vdat);
201 acpi_get_vdat_info(&vdat->vdat_addr, &vdat->vdat_size);
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700202#endif
Stefan Reinauer31324c62012-04-05 21:22:02 +0200203}
204
205static void lb_vbnv(struct lb_header *header)
206{
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700207#if CONFIG_PC80_SYSTEM
Stefan Reinauer31324c62012-04-05 21:22:02 +0200208 struct lb_vbnv* vbnv;
209
210 vbnv = (struct lb_vbnv *)lb_new_record(header);
211 vbnv->tag = LB_TAG_VBNV;
212 vbnv->size = sizeof(*vbnv);
213 vbnv->vbnv_start = CONFIG_VBNV_OFFSET + 14;
214 vbnv->vbnv_size = CONFIG_VBNV_SIZE;
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700215#endif
Stefan Reinauer31324c62012-04-05 21:22:02 +0200216}
Aaron Durbindd32a312013-03-07 23:15:06 -0600217
218#if CONFIG_VBOOT_VERIFY_FIRMWARE
219static void lb_vboot_handoff(struct lb_header *header)
220{
221 void *addr;
222 uint32_t size;
223 struct lb_vboot_handoff* vbho;
224
225 if (vboot_get_handoff_info(&addr, &size))
226 return;
227
228 vbho = (struct lb_vboot_handoff *)lb_new_record(header);
229 vbho->tag = LB_TAB_VBOOT_HANDOFF;
230 vbho->size = sizeof(*vbho);
231 vbho->vboot_handoff_addr = addr;
232 vbho->vboot_handoff_size = size;
233}
234#else
235static inline void lb_vboot_handoff(struct lb_header *header) {}
236#endif /* CONFIG_VBOOT_VERIFY_FIRMWARE */
237#endif /* CONFIG_CHROMEOS */
Stefan Reinauer31324c62012-04-05 21:22:02 +0200238
Vadim Bendebury22c04682011-10-03 14:58:57 -0700239static void add_cbmem_pointers(struct lb_header *header)
Vadim Bendebury2e438672011-09-23 09:56:11 -0700240{
Vadim Bendebury22c04682011-10-03 14:58:57 -0700241 /*
242 * These CBMEM sections' addresses are included in the coreboot table
243 * with the appropriate tags.
244 */
245 const struct section_id {
246 int cbmem_id;
247 int table_tag;
248 } section_ids[] = {
249 {CBMEM_ID_TIMESTAMP, LB_TAG_TIMESTAMPS},
250 {CBMEM_ID_CONSOLE, LB_TAG_CBMEM_CONSOLE}
251 };
252 int i;
Vadim Bendebury2e438672011-09-23 09:56:11 -0700253
Vadim Bendebury22c04682011-10-03 14:58:57 -0700254 for (i = 0; i < ARRAY_SIZE(section_ids); i++) {
255 const struct section_id *sid = section_ids + i;
256 struct lb_cbmem_ref *cbmem_ref;
257 void *cbmem_addr = cbmem_find(sid->cbmem_id);
Vadim Bendebury2e438672011-09-23 09:56:11 -0700258
Vadim Bendebury22c04682011-10-03 14:58:57 -0700259 if (!cbmem_addr)
260 continue; /* This section is not present */
Vadim Bendebury2e438672011-09-23 09:56:11 -0700261
Vadim Bendebury22c04682011-10-03 14:58:57 -0700262 cbmem_ref = (struct lb_cbmem_ref *)lb_new_record(header);
263 if (!cbmem_ref) {
264 printk(BIOS_ERR, "No more room in coreboot table!\n");
265 break;
266 }
267 cbmem_ref->tag = sid->table_tag;
268 cbmem_ref->size = sizeof(*cbmem_ref);
Stefan Reinauerb8ad2242013-01-11 10:41:42 -0800269 cbmem_ref->cbmem_addr = (unsigned long)cbmem_addr;
Vadim Bendebury22c04682011-10-03 14:58:57 -0700270 }
Vadim Bendebury2e438672011-09-23 09:56:11 -0700271}
Vadim Bendebury2e438672011-09-23 09:56:11 -0700272
Stefan Reinauere3778572010-01-30 09:47:18 +0000273static struct lb_mainboard *lb_mainboard(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000274{
275 struct lb_record *rec;
276 struct lb_mainboard *mainboard;
277 rec = lb_new_record(header);
278 mainboard = (struct lb_mainboard *)rec;
279 mainboard->tag = LB_TAG_MAINBOARD;
280
281 mainboard->size = (sizeof(*mainboard) +
Stefan Reinauer14e22772010-04-27 06:56:47 +0000282 strlen(mainboard_vendor) + 1 +
Eric Biederman8ca8d762003-04-22 19:02:15 +0000283 strlen(mainboard_part_number) + 1 +
284 3) & ~3;
285
286 mainboard->vendor_idx = 0;
287 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
288
289 memcpy(mainboard->strings + mainboard->vendor_idx,
290 mainboard_vendor, strlen(mainboard_vendor) + 1);
291 memcpy(mainboard->strings + mainboard->part_number_idx,
292 mainboard_part_number, strlen(mainboard_part_number) + 1);
293
294 return mainboard;
295}
296
Stefan Reinauerc75bfde2011-08-15 11:26:35 -0700297#if CONFIG_USE_OPTION_TABLE
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000298static struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000299{
300 struct lb_record *rec;
301 struct cmos_checksum *cmos_checksum;
302 rec = lb_new_record(header);
303 cmos_checksum = (struct cmos_checksum *)rec;
304 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
305
306 cmos_checksum->size = (sizeof(*cmos_checksum));
307
Stefan Reinauerb5828d72010-03-29 17:14:28 +0000308 cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
309 cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
310 cmos_checksum->location = LB_CKS_LOC * 8;
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000311 cmos_checksum->type = CHECKSUM_PCBIOS;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000312
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000313 return cmos_checksum;
314}
Stefan Reinauer2549d522010-03-17 03:44:45 +0000315#endif
Stefan Reinauer4bd0de02005-12-03 22:39:23 +0000316
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000317static void lb_strings(struct lb_header *header)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000318{
319 static const struct {
320 uint32_t tag;
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000321 const char *string;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000322 } strings[] = {
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000323 { LB_TAG_VERSION, coreboot_version, },
324 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
325 { LB_TAG_BUILD, coreboot_build, },
326 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
327 { LB_TAG_COMPILE_BY, coreboot_compile_by, },
328 { LB_TAG_COMPILE_HOST, coreboot_compile_host, },
329 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
330 { LB_TAG_COMPILER, coreboot_compiler, },
331 { LB_TAG_LINKER, coreboot_linker, },
332 { LB_TAG_ASSEMBLER, coreboot_assembler, },
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333 };
Eric Biederman52685572003-05-19 19:16:21 +0000334 unsigned int i;
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000335 for(i = 0; i < ARRAY_SIZE(strings); i++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000336 struct lb_string *rec;
337 size_t len;
338 rec = (struct lb_string *)lb_new_record(header);
339 len = strlen(strings[i].string);
340 rec->tag = strings[i].tag;
341 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
342 memcpy(rec->string, strings[i].string, len+1);
343 }
344
345}
346
Stefan Reinauerb883d4c2009-08-27 11:23:06 +0000347static struct lb_forward *lb_forward(struct lb_header *header, struct lb_header *next_header)
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000348{
349 struct lb_record *rec;
350 struct lb_forward *forward;
351 rec = lb_new_record(header);
352 forward = (struct lb_forward *)rec;
353 forward->tag = LB_TAG_FORWARD;
354 forward->size = sizeof(*forward);
Stefan Reinauerdf77f342009-04-06 14:00:53 +0000355 forward->forward = (uint64_t)(unsigned long)next_header;
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000356 return forward;
357}
358
Kyösti Mälkki4c29d7f2012-08-02 09:49:11 +0300359static void lb_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000360 uint32_t type, uint64_t start, uint64_t size)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000361{
362 int entries;
363 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Eric Biedermanec01aa92004-12-10 20:50:43 +0000364 mem->map[entries].start = pack_lb64(start);
365 mem->map[entries].size = pack_lb64(size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000366 mem->map[entries].type = type;
367 mem->size += sizeof(mem->map[0]);
368}
369
Eric Biederman8ca8d762003-04-22 19:02:15 +0000370static void lb_reserve_table_memory(struct lb_header *head)
371{
Aaron Durbindf3a1092013-03-13 12:41:44 -0500372/* Dynamic cbmem has already reserved the memory where the coreboot tables
373 * reside. Therefore, there is nothing to fix up. */
374#if !CONFIG_DYNAMIC_CBMEM
Eric Biederman8ca8d762003-04-22 19:02:15 +0000375 struct lb_record *last_rec;
376 struct lb_memory *mem;
377 uint64_t start;
378 uint64_t end;
379 int i, entries;
380
381 last_rec = lb_last_record(head);
382 mem = get_lb_mem();
383 start = (unsigned long)head;
384 end = (unsigned long)last_rec;
385 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
386 /* Resize the right two memory areas so this table is in
387 * a reserved area of memory. Everything has been carefully
388 * setup so that is all we need to do.
389 */
390 for(i = 0; i < entries; i++ ) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000391 uint64_t map_start = unpack_lb64(mem->map[i].start);
392 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000393 /* Does this area need to be expanded? */
394 if (map_end == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000395 mem->map[i].size = pack_lb64(end - map_start);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000396 }
397 /* Does this area need to be contracted? */
398 else if (map_start == start) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000399 mem->map[i].start = pack_lb64(end);
400 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000401 }
402 }
Aaron Durbindf3a1092013-03-13 12:41:44 -0500403#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404}
405
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000406static unsigned long lb_table_fini(struct lb_header *head, int fixup)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000407{
408 struct lb_record *rec, *first_rec;
409 rec = lb_last_record(head);
410 if (head->table_entries) {
411 head->table_bytes += rec->size;
412 }
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000413
414 if (fixup)
415 lb_reserve_table_memory(head);
416
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417 first_rec = lb_first_record(head);
418 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
419 head->header_checksum = 0;
420 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
Vadim Bendebury05239892011-08-16 11:44:35 -0700421 printk(BIOS_DEBUG,
422 "Wrote coreboot table at: %p, 0x%x bytes, checksum %x\n",
423 head, head->table_bytes, head->table_checksum);
424 return (unsigned long)rec + rec->size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000425}
426
Eric Biederman6e53f502004-10-27 08:53:57 +0000427static void lb_cleanup_memory_ranges(struct lb_memory *mem)
428{
429 int entries;
430 int i, j;
431 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000432
Eric Biederman6e53f502004-10-27 08:53:57 +0000433 /* Sort the lb memory ranges */
434 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000435 uint64_t entry_start = unpack_lb64(mem->map[i].start);
Aaron Durbinf7fa2182012-12-18 17:01:57 -0600436 for(j = i + 1; j < entries; j++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000437 uint64_t temp_start = unpack_lb64(mem->map[j].start);
438 if (temp_start < entry_start) {
Eric Biederman6e53f502004-10-27 08:53:57 +0000439 struct lb_memory_range tmp;
440 tmp = mem->map[i];
441 mem->map[i] = mem->map[j];
442 mem->map[j] = tmp;
443 }
444 }
445 }
446
447 /* Merge adjacent entries */
448 for(i = 0; (i + 1) < entries; i++) {
449 uint64_t start, end, nstart, nend;
450 if (mem->map[i].type != mem->map[i + 1].type) {
451 continue;
452 }
Eric Biedermanec01aa92004-12-10 20:50:43 +0000453 start = unpack_lb64(mem->map[i].start);
454 end = start + unpack_lb64(mem->map[i].size);
455 nstart = unpack_lb64(mem->map[i + 1].start);
456 nend = nstart + unpack_lb64(mem->map[i + 1].size);
Aaron Durbinf7fa2182012-12-18 17:01:57 -0600457 if ((start <= nstart) && (end >= nstart)) {
Eric Biederman6e53f502004-10-27 08:53:57 +0000458 if (start > nstart) {
459 start = nstart;
460 }
461 if (end < nend) {
462 end = nend;
463 }
464 /* Record the new region size */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000465 mem->map[i].start = pack_lb64(start);
466 mem->map[i].size = pack_lb64(end - start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000467
468 /* Delete the entry I have merged with */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000469 memmove(&mem->map[i + 1], &mem->map[i + 2],
Eric Biederman6e53f502004-10-27 08:53:57 +0000470 ((entries - i - 2) * sizeof(mem->map[0])));
471 mem->size -= sizeof(mem->map[0]);
472 entries -= 1;
473 /* See if I can merge with the next entry as well */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000474 i -= 1;
Eric Biederman6e53f502004-10-27 08:53:57 +0000475 }
476 }
477}
478
Stefan Reinauer14e22772010-04-27 06:56:47 +0000479static void lb_remove_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000480 uint64_t start, uint64_t size)
481{
482 uint64_t end;
483 int entries;
484 int i;
485
486 end = start + size;
487 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
488
489 /* Remove a reserved area from the memory map */
490 for(i = 0; i < entries; i++) {
Eric Biedermanec01aa92004-12-10 20:50:43 +0000491 uint64_t map_start = unpack_lb64(mem->map[i].start);
492 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
Eric Biederman6e53f502004-10-27 08:53:57 +0000493 if ((start <= map_start) && (end >= map_end)) {
494 /* Remove the completely covered range */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000495 memmove(&mem->map[i], &mem->map[i + 1],
Eric Biederman6e53f502004-10-27 08:53:57 +0000496 ((entries - i - 1) * sizeof(mem->map[0])));
497 mem->size -= sizeof(mem->map[0]);
498 entries -= 1;
499 /* Since the index will disappear revisit what will appear here */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000500 i -= 1;
Eric Biederman6e53f502004-10-27 08:53:57 +0000501 }
502 else if ((start > map_start) && (end < map_end)) {
503 /* Split the memory range */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000504 memmove(&mem->map[i + 1], &mem->map[i],
Eric Biederman6e53f502004-10-27 08:53:57 +0000505 ((entries - i) * sizeof(mem->map[0])));
506 mem->size += sizeof(mem->map[0]);
507 entries += 1;
508 /* Update the first map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000509 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000510 /* Update the second map entry */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000511 mem->map[i + 1].start = pack_lb64(end);
512 mem->map[i + 1].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000513 /* Don't bother with this map entry again */
514 i += 1;
515 }
516 else if ((start <= map_start) && (end > map_start)) {
517 /* Shrink the start of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000518 mem->map[i].start = pack_lb64(end);
519 mem->map[i].size = pack_lb64(map_end - end);
Eric Biederman6e53f502004-10-27 08:53:57 +0000520 }
521 else if ((start < map_end) && (start > map_start)) {
522 /* Shrink the end of the memory range */
Eric Biedermanec01aa92004-12-10 20:50:43 +0000523 mem->map[i].size = pack_lb64(start - map_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000524 }
525 }
526}
527
Aaron Durbindf3a1092013-03-13 12:41:44 -0500528void lb_add_memory_range(struct lb_memory *mem,
Eric Biederman6e53f502004-10-27 08:53:57 +0000529 uint32_t type, uint64_t start, uint64_t size)
530{
531 lb_remove_memory_range(mem, start, size);
532 lb_memory_range(mem, type, start, size);
533 lb_cleanup_memory_ranges(mem);
534}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000535
Stefan Reinauere3778572010-01-30 09:47:18 +0000536static void lb_dump_memory_ranges(struct lb_memory *mem)
537{
538 int entries;
539 int i;
540 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000541
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000542 printk(BIOS_DEBUG, "coreboot memory table:\n");
Stefan Reinauere3778572010-01-30 09:47:18 +0000543 for(i = 0; i < entries; i++) {
544 uint64_t entry_start = unpack_lb64(mem->map[i].start);
545 uint64_t entry_size = unpack_lb64(mem->map[i].size);
546 const char *entry_type;
547
548 switch (mem->map[i].type) {
549 case LB_MEM_RAM: entry_type="RAM"; break;
550 case LB_MEM_RESERVED: entry_type="RESERVED"; break;
551 case LB_MEM_ACPI: entry_type="ACPI"; break;
552 case LB_MEM_NVS: entry_type="NVS"; break;
553 case LB_MEM_UNUSABLE: entry_type="UNUSABLE"; break;
554 case LB_MEM_VENDOR_RSVD: entry_type="VENDOR RESERVED"; break;
555 case LB_MEM_TABLE: entry_type="CONFIGURATION TABLES"; break;
556 default: entry_type="UNKNOWN!"; break;
557 }
558
Stefan Reinauer14e22772010-04-27 06:56:47 +0000559 printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n",
Stefan Reinauere3778572010-01-30 09:47:18 +0000560 i, entry_start, entry_start+entry_size-1, entry_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000561
Stefan Reinauere3778572010-01-30 09:47:18 +0000562 }
563}
564
Stefan Reinauer14e22772010-04-27 06:56:47 +0000565/* Routines to extract part so the coreboot table or
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000566 * information from the coreboot table after we have written it.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000567 * Currently get_lb_mem relies on a global we can change the
568 * implementaiton.
569 */
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700570static struct lb_memory *mem_ranges = NULL;
571
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572struct lb_memory *get_lb_mem(void)
573{
574 return mem_ranges;
575}
576
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000577static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
578{
579 struct lb_memory *mem = gp;
580 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
581}
582
Eric Biederman6e53f502004-10-27 08:53:57 +0000583static struct lb_memory *build_lb_mem(struct lb_header *head)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000584{
Eric Biederman6e53f502004-10-27 08:53:57 +0000585 struct lb_memory *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000586
Eric Biederman6e53f502004-10-27 08:53:57 +0000587 /* Record where the lb memory ranges will live */
588 mem = lb_memory(head);
589 mem_ranges = mem;
590
591 /* Build the raw table of memory */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000592 search_global_resources(
593 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
594 build_lb_mem_range, mem);
Eric Biederman6e53f502004-10-27 08:53:57 +0000595 lb_cleanup_memory_ranges(mem);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000596 return mem;
597}
598
Myles Watsone0a000c2010-09-09 14:51:17 +0000599static void lb_add_rsvd_range(void *gp, struct device *dev, struct resource *res)
600{
601 struct lb_memory *mem = gp;
602 lb_add_memory_range(mem, LB_MEM_RESERVED, res->base, res->size);
603}
604
605static void add_lb_reserved(struct lb_memory *mem)
606{
607 /* Add reserved ranges */
608 search_global_resources(
609 IORESOURCE_MEM | IORESOURCE_RESERVE, IORESOURCE_MEM | IORESOURCE_RESERVE,
610 lb_add_rsvd_range, mem);
611}
612
Stefan Reinauer14e22772010-04-27 06:56:47 +0000613unsigned long write_coreboot_table(
614 unsigned long low_table_start, unsigned long low_table_end,
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000615 unsigned long rom_table_start, unsigned long rom_table_end)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000616{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000617 struct lb_header *head;
618 struct lb_memory *mem;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000619
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700620 if (low_table_start || low_table_end) {
621 printk(BIOS_DEBUG, "Writing table forward entry at 0x%08lx\n",
622 low_table_end);
623 head = lb_table_init(low_table_end);
624 lb_forward(head, (struct lb_header*)rom_table_end);
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000625
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700626 low_table_end = (unsigned long) lb_table_fini(head, 0);
627 printk(BIOS_DEBUG, "Table forward entry ends at 0x%08lx.\n",
628 low_table_end);
629 low_table_end = ALIGN(low_table_end, 4096);
630 printk(BIOS_DEBUG, "... aligned to 0x%08lx\n", low_table_end);
631 }
632
633 printk(BIOS_DEBUG, "Writing coreboot table at 0x%08lx\n",
634 rom_table_end);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000635
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000636 head = lb_table_init(rom_table_end);
637 rom_table_end = (unsigned long)head;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000638 printk(BIOS_DEBUG, "rom_table_end = 0x%08lx\n", rom_table_end);
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700639 rom_table_end = ALIGN(rom_table_end, (64 * 1024));
640 printk(BIOS_DEBUG, "... aligned to 0x%08lx\n", rom_table_end);
Stefan Reinauer4f1cb232006-07-19 15:32:49 +0000641
Stefan Reinauerc75bfde2011-08-15 11:26:35 -0700642#if CONFIG_USE_OPTION_TABLE
Stefan Reinauerba430642007-04-06 12:14:51 +0000643 {
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800644 struct cmos_option_table *option_table = cbfs_get_file_content(
645 CBFS_DEFAULT_MEDIA, "cmos_layout.bin",
646 CBFS_COMPONENT_CMOS_LAYOUT);
Patrick Georgi24479372011-01-18 13:56:36 +0000647 if (option_table) {
648 struct lb_record *rec_dest = lb_new_record(head);
649 /* Copy the option config table, it's already a lb_record... */
Josef Kellermann679d38b2011-01-24 21:07:57 +0000650 memcpy(rec_dest, option_table, option_table->size);
Patrick Georgi24479372011-01-18 13:56:36 +0000651 /* Create cmos checksum entry in coreboot table */
652 lb_cmos_checksum(head);
Patrick Georgicef3b892011-01-18 14:28:45 +0000653 } else {
654 printk(BIOS_ERR, "cmos_layout.bin could not be found!\n");
Patrick Georgi24479372011-01-18 13:56:36 +0000655 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000656 }
Stefan Reinauerba430642007-04-06 12:14:51 +0000657#endif
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700658
659 /* The Linux kernel assumes this region is reserved */
Eric Biederman6e53f502004-10-27 08:53:57 +0000660 /* Record where RAM is located */
661 mem = build_lb_mem(head);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000662
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700663 if (low_table_start || low_table_end) {
664 /* Record the mptable and the the lb_table.
665 * (This will be adjusted later) */
666 lb_add_memory_range(mem, LB_MEM_TABLE,
667 low_table_start, low_table_end - low_table_start);
668 }
Eric Biederman6e53f502004-10-27 08:53:57 +0000669
Aaron Durbindf3a1092013-03-13 12:41:44 -0500670 /* Record the pirq table, acpi tables, and maybe the mptable. However,
671 * these only need to be added when the rom_table is sitting below
672 * 1MiB. If it isn't that means high tables are being written.
673 * The code below handles high tables correctly. */
674 if (rom_table_end <= (1 << 20))
675 lb_add_memory_range(mem, LB_MEM_TABLE,
Stefan Reinauer3e4e3032013-03-20 14:08:04 -0700676 rom_table_start, rom_table_end - rom_table_start);
Eric Biederman6e53f502004-10-27 08:53:57 +0000677
Aaron Durbindf3a1092013-03-13 12:41:44 -0500678#if CONFIG_DYNAMIC_CBMEM
679 cbmem_add_lb_mem(mem);
680#else /* CONFIG_DYNAMIC_CBMEM */
Patrick Georgibccaafc2009-04-28 12:57:25 +0000681 lb_add_memory_range(mem, LB_MEM_TABLE,
682 high_tables_base, high_tables_size);
Aaron Durbindf3a1092013-03-13 12:41:44 -0500683#endif /* CONFIG_DYNAMIC_CBMEM */
Patrick Georgibccaafc2009-04-28 12:57:25 +0000684
Myles Watsone0a000c2010-09-09 14:51:17 +0000685 /* Add reserved regions */
686 add_lb_reserved(mem);
687
Stefan Reinauere3778572010-01-30 09:47:18 +0000688 lb_dump_memory_ranges(mem);
689
Eric Biederman6e53f502004-10-27 08:53:57 +0000690 /* Note:
691 * I assume that there is always memory at immediately after
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000692 * the low_table_end. This means that after I setup the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000693 * I can trivially fixup the reserved memory ranges to hold the correct
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000694 * size of the coreboot table.
Eric Biederman6e53f502004-10-27 08:53:57 +0000695 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000696
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000697 /* Record our motherboard */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000698 lb_mainboard(head);
Patrick Georgi8c2a0c12008-01-25 18:28:18 +0000699 /* Record the serial port, if present */
700 lb_serial(head);
Patrick Georgi3bbf2ff2008-01-27 14:12:54 +0000701 /* Record our console setup */
702 lb_console(head);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000703 /* Record our various random string information */
704 lb_strings(head);
Stefan Reinauerd650e992010-02-22 04:33:13 +0000705 /* Record our framebuffer */
706 lb_framebuffer(head);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000707
Stefan Reinauer31324c62012-04-05 21:22:02 +0200708#if CONFIG_CHROMEOS
709 /* Record our GPIO settings (ChromeOS specific) */
710 lb_gpios(head);
711
712 /* pass along the VDAT buffer adress */
713 lb_vdat(head);
714
715 /* pass along VBNV offsets in CMOS */
716 lb_vbnv(head);
Aaron Durbindd32a312013-03-07 23:15:06 -0600717
718 /* pass along the vboot_handoff address. */
719 lb_vboot_handoff(head);
Stefan Reinauer31324c62012-04-05 21:22:02 +0200720#endif
Vadim Bendebury22c04682011-10-03 14:58:57 -0700721 add_cbmem_pointers(head);
722
Eric Biederman8ca8d762003-04-22 19:02:15 +0000723 /* Remember where my valid memory ranges are */
Stefan Reinauerefab4ba2009-03-17 14:38:48 +0000724 return lb_table_fini(head, 1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000725}