xref: /openbmc/u-boot/arch/x86/lib/coreboot_table.c (revision 2290fe06)
1 /*
2  * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <vbe.h>
9 #include <asm/coreboot_tables.h>
10 #include <asm/e820.h>
11 
12 DECLARE_GLOBAL_DATA_PTR;
13 
14 int high_table_reserve(void)
15 {
16 	/* adjust stack pointer to reserve space for configuration tables */
17 	gd->arch.high_table_limit = gd->start_addr_sp;
18 	gd->start_addr_sp -= CONFIG_HIGH_TABLE_SIZE;
19 	gd->arch.high_table_ptr = gd->start_addr_sp;
20 
21 	/* clear the memory */
22 	memset((void *)gd->arch.high_table_ptr, 0, CONFIG_HIGH_TABLE_SIZE);
23 
24 	gd->start_addr_sp &= ~0xf;
25 
26 	return 0;
27 }
28 
29 void *high_table_malloc(size_t bytes)
30 {
31 	u32 new_ptr;
32 	void *ptr;
33 
34 	new_ptr = gd->arch.high_table_ptr + bytes;
35 	if (new_ptr >= gd->arch.high_table_limit)
36 		return NULL;
37 	ptr = (void *)gd->arch.high_table_ptr;
38 	gd->arch.high_table_ptr = new_ptr;
39 
40 	return ptr;
41 }
42 
43 /**
44  * cb_table_init() - initialize a coreboot table header
45  *
46  * This fills in the coreboot table header signature and the header bytes.
47  * Other fields are set to zero.
48  *
49  * @cbh:	coreboot table header address
50  */
51 static void cb_table_init(struct cb_header *cbh)
52 {
53 	memset(cbh, 0, sizeof(struct cb_header));
54 	memcpy(cbh->signature, "LBIO", 4);
55 	cbh->header_bytes = sizeof(struct cb_header);
56 }
57 
58 /**
59  * cb_table_add_entry() - add a coreboot table entry
60  *
61  * This increases the coreboot table entry size with added table entry length
62  * and increases entry count by 1.
63  *
64  * @cbh:	coreboot table header address
65  * @cbr:	to be added table entry address
66  * @return:	pointer to next table entry address
67  */
68 static u32 cb_table_add_entry(struct cb_header *cbh, struct cb_record *cbr)
69 {
70 	cbh->table_bytes += cbr->size;
71 	cbh->table_entries++;
72 
73 	return (u32)cbr + cbr->size;
74 }
75 
76 /**
77  * cb_table_finalize() - finalize the coreboot table
78  *
79  * This calculates the checksum for all coreboot table entries as well as
80  * the checksum for the coreboot header itself.
81  *
82  * @cbh:	coreboot table header address
83  */
84 static void cb_table_finalize(struct cb_header *cbh)
85 {
86 	struct cb_record *cbr = (struct cb_record *)(cbh + 1);
87 
88 	cbh->table_checksum = compute_ip_checksum(cbr, cbh->table_bytes);
89 	cbh->header_checksum = compute_ip_checksum(cbh, cbh->header_bytes);
90 }
91 
92 void write_coreboot_table(u32 addr, struct memory_area *cfg_tables)
93 {
94 	struct cb_header *cbh = (struct cb_header *)addr;
95 	struct cb_record *cbr;
96 	struct cb_memory *mem;
97 	struct cb_memory_range *map;
98 	struct e820entry e820[32];
99 	struct cb_framebuffer *fb;
100 	struct vesa_mode_info *vesa;
101 	int i, num;
102 
103 	cb_table_init(cbh);
104 	cbr = (struct cb_record *)(cbh + 1);
105 
106 	/*
107 	 * Two type of coreboot table entries are generated by us.
108 	 * They are 'struct cb_memory' and 'struct cb_framebuffer'.
109 	 */
110 
111 	/* populate memory map table */
112 	mem = (struct cb_memory *)cbr;
113 	mem->tag = CB_TAG_MEMORY;
114 	map = mem->map;
115 
116 	/* first install e820 defined memory maps */
117 	num = install_e820_map(ARRAY_SIZE(e820), e820);
118 	for (i = 0; i < num; i++) {
119 		map->start.lo = e820[i].addr & 0xffffffff;
120 		map->start.hi = e820[i].addr >> 32;
121 		map->size.lo = e820[i].size & 0xffffffff;
122 		map->size.hi = e820[i].size >> 32;
123 		map->type = e820[i].type;
124 		map++;
125 	}
126 
127 	/* then install all configuration tables */
128 	while (cfg_tables->size) {
129 		map->start.lo = cfg_tables->start & 0xffffffff;
130 		map->start.hi = cfg_tables->start >> 32;
131 		map->size.lo = cfg_tables->size & 0xffffffff;
132 		map->size.hi = cfg_tables->size >> 32;
133 		map->type = CB_MEM_TABLE;
134 		map++;
135 		num++;
136 		cfg_tables++;
137 	}
138 	mem->size = num * sizeof(struct cb_memory_range) +
139 		    sizeof(struct cb_record);
140 	cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
141 
142 	/* populate framebuffer table if we have sane vesa info */
143 	vesa = &mode_info.vesa;
144 	if (vesa->x_resolution && vesa->y_resolution) {
145 		fb = (struct cb_framebuffer *)cbr;
146 		fb->tag = CB_TAG_FRAMEBUFFER;
147 		fb->size = sizeof(struct cb_framebuffer);
148 
149 		fb->x_resolution = vesa->x_resolution;
150 		fb->y_resolution = vesa->y_resolution;
151 		fb->bits_per_pixel = vesa->bits_per_pixel;
152 		fb->bytes_per_line = vesa->bytes_per_scanline;
153 		fb->physical_address = vesa->phys_base_ptr;
154 		fb->red_mask_size = vesa->red_mask_size;
155 		fb->red_mask_pos = vesa->red_mask_pos;
156 		fb->green_mask_size = vesa->green_mask_size;
157 		fb->green_mask_pos = vesa->green_mask_pos;
158 		fb->blue_mask_size = vesa->blue_mask_size;
159 		fb->blue_mask_pos = vesa->blue_mask_pos;
160 		fb->reserved_mask_size = vesa->reserved_mask_size;
161 		fb->reserved_mask_pos = vesa->reserved_mask_pos;
162 
163 		cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
164 	}
165 
166 	cb_table_finalize(cbh);
167 }
168