xref: /openbmc/u-boot/arch/x86/lib/tables.c (revision ad3098f7)
1 /*
2  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <malloc.h>
9 #include <asm/sfi.h>
10 #include <asm/mpspec.h>
11 #include <asm/smbios.h>
12 #include <asm/tables.h>
13 #include <asm/acpi_table.h>
14 #include <asm/coreboot_tables.h>
15 
16 /**
17  * Function prototype to write a specific configuration table
18  *
19  * @addr:	start address to write the table
20  * @return:	end address of the table
21  */
22 typedef u32 (*table_write)(u32 addr);
23 
24 static table_write table_write_funcs[] = {
25 #ifdef CONFIG_GENERATE_PIRQ_TABLE
26 	write_pirq_routing_table,
27 #endif
28 #ifdef CONFIG_GENERATE_SFI_TABLE
29 	write_sfi_table,
30 #endif
31 #ifdef CONFIG_GENERATE_MP_TABLE
32 	write_mp_table,
33 #endif
34 #ifdef CONFIG_GENERATE_ACPI_TABLE
35 	write_acpi_tables,
36 #endif
37 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
38 	write_smbios_table,
39 #endif
40 };
41 
42 u8 table_compute_checksum(void *v, int len)
43 {
44 	u8 *bytes = v;
45 	u8 checksum = 0;
46 	int i;
47 
48 	for (i = 0; i < len; i++)
49 		checksum -= bytes[i];
50 
51 	return checksum;
52 }
53 
54 void table_fill_string(char *dest, const char *src, size_t n, char pad)
55 {
56 	int start, len;
57 	int i;
58 
59 	strncpy(dest, src, n);
60 
61 	/* Fill the remaining bytes with pad */
62 	len = strlen(src);
63 	start = len < n ? len : n;
64 	for (i = start; i < n; i++)
65 		dest[i] = pad;
66 }
67 
68 void write_tables(void)
69 {
70 	u32 rom_table_start = ROM_TABLE_ADDR;
71 	u32 rom_table_end;
72 #ifdef CONFIG_SEABIOS
73 	u32 high_table, table_size;
74 	struct memory_area cfg_tables[ARRAY_SIZE(table_write_funcs) + 1];
75 #endif
76 	int i;
77 
78 	for (i = 0; i < ARRAY_SIZE(table_write_funcs); i++) {
79 		rom_table_end = table_write_funcs[i](rom_table_start);
80 		rom_table_end = ALIGN(rom_table_end, ROM_TABLE_ALIGN);
81 
82 #ifdef CONFIG_SEABIOS
83 		table_size = rom_table_end - rom_table_start;
84 		high_table = (u32)memalign(ROM_TABLE_ALIGN, table_size);
85 		if (high_table) {
86 			memset((void *)high_table, 0, table_size);
87 			table_write_funcs[i](high_table);
88 
89 			cfg_tables[i].start = high_table;
90 			cfg_tables[i].size = table_size;
91 		} else {
92 			printf("%d: no memory for configuration tables\n", i);
93 		}
94 #endif
95 
96 		rom_table_start = rom_table_end;
97 	}
98 
99 #ifdef CONFIG_SEABIOS
100 	/* make sure the last item is zero */
101 	cfg_tables[i].size = 0;
102 	write_coreboot_table(CB_TABLE_ADDR, cfg_tables);
103 #endif
104 }
105