xref: /openbmc/u-boot/arch/x86/lib/coreboot_table.c (revision 523849a0)
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 /**
13  * cb_table_init() - initialize a coreboot table header
14  *
15  * This fills in the coreboot table header signature and the header bytes.
16  * Other fields are set to zero.
17  *
18  * @cbh:	coreboot table header address
19  */
20 static void cb_table_init(struct cb_header *cbh)
21 {
22 	memset(cbh, 0, sizeof(struct cb_header));
23 	memcpy(cbh->signature, "LBIO", 4);
24 	cbh->header_bytes = sizeof(struct cb_header);
25 }
26 
27 /**
28  * cb_table_add_entry() - add a coreboot table entry
29  *
30  * This increases the coreboot table entry size with added table entry length
31  * and increases entry count by 1.
32  *
33  * @cbh:	coreboot table header address
34  * @cbr:	to be added table entry address
35  * @return:	pointer to next table entry address
36  */
37 static u32 cb_table_add_entry(struct cb_header *cbh, struct cb_record *cbr)
38 {
39 	cbh->table_bytes += cbr->size;
40 	cbh->table_entries++;
41 
42 	return (u32)cbr + cbr->size;
43 }
44 
45 /**
46  * cb_table_finalize() - finalize the coreboot table
47  *
48  * This calculates the checksum for all coreboot table entries as well as
49  * the checksum for the coreboot header itself.
50  *
51  * @cbh:	coreboot table header address
52  */
53 static void cb_table_finalize(struct cb_header *cbh)
54 {
55 	struct cb_record *cbr = (struct cb_record *)(cbh + 1);
56 
57 	cbh->table_checksum = compute_ip_checksum(cbr, cbh->table_bytes);
58 	cbh->header_checksum = compute_ip_checksum(cbh, cbh->header_bytes);
59 }
60 
61 void write_coreboot_table(u32 addr, struct memory_area *cfg_tables)
62 {
63 	struct cb_header *cbh = (struct cb_header *)addr;
64 	struct cb_record *cbr;
65 	struct cb_memory *mem;
66 	struct cb_memory_range *map;
67 	struct e820entry e820[32];
68 	struct cb_framebuffer *fb;
69 	struct vesa_mode_info *vesa;
70 	int i, num;
71 
72 	cb_table_init(cbh);
73 	cbr = (struct cb_record *)(cbh + 1);
74 
75 	/*
76 	 * Two type of coreboot table entries are generated by us.
77 	 * They are 'struct cb_memory' and 'struct cb_framebuffer'.
78 	 */
79 
80 	/* populate memory map table */
81 	mem = (struct cb_memory *)cbr;
82 	mem->tag = CB_TAG_MEMORY;
83 	map = mem->map;
84 
85 	/* first install e820 defined memory maps */
86 	num = install_e820_map(ARRAY_SIZE(e820), e820);
87 	for (i = 0; i < num; i++) {
88 		map->start.lo = e820[i].addr & 0xffffffff;
89 		map->start.hi = e820[i].addr >> 32;
90 		map->size.lo = e820[i].size & 0xffffffff;
91 		map->size.hi = e820[i].size >> 32;
92 		map->type = e820[i].type;
93 		map++;
94 	}
95 
96 	/* then install all configuration tables */
97 	while (cfg_tables->size) {
98 		map->start.lo = cfg_tables->start & 0xffffffff;
99 		map->start.hi = cfg_tables->start >> 32;
100 		map->size.lo = cfg_tables->size & 0xffffffff;
101 		map->size.hi = cfg_tables->size >> 32;
102 		map->type = CB_MEM_TABLE;
103 		map++;
104 		num++;
105 		cfg_tables++;
106 	}
107 	mem->size = num * sizeof(struct cb_memory_range) +
108 		    sizeof(struct cb_record);
109 	cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
110 
111 	/* populate framebuffer table if we have sane vesa info */
112 	vesa = &mode_info.vesa;
113 	if (vesa->x_resolution && vesa->y_resolution) {
114 		fb = (struct cb_framebuffer *)cbr;
115 		fb->tag = CB_TAG_FRAMEBUFFER;
116 		fb->size = sizeof(struct cb_framebuffer);
117 
118 		fb->x_resolution = vesa->x_resolution;
119 		fb->y_resolution = vesa->y_resolution;
120 		fb->bits_per_pixel = vesa->bits_per_pixel;
121 		fb->bytes_per_line = vesa->bytes_per_scanline;
122 		fb->physical_address = vesa->phys_base_ptr;
123 		fb->red_mask_size = vesa->red_mask_size;
124 		fb->red_mask_pos = vesa->red_mask_pos;
125 		fb->green_mask_size = vesa->green_mask_size;
126 		fb->green_mask_pos = vesa->green_mask_pos;
127 		fb->blue_mask_size = vesa->blue_mask_size;
128 		fb->blue_mask_pos = vesa->blue_mask_pos;
129 		fb->reserved_mask_size = vesa->reserved_mask_size;
130 		fb->reserved_mask_pos = vesa->reserved_mask_pos;
131 
132 		cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
133 	}
134 
135 	cb_table_finalize(cbh);
136 }
137