1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
9 #include <acpi/acpi_s3.h>
10 #include <asm/coreboot_tables.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 int high_table_reserve(void)
17 /* adjust stack pointer to reserve space for configuration tables */
18 gd->arch.high_table_limit = gd->start_addr_sp;
19 gd->start_addr_sp -= CONFIG_HIGH_TABLE_SIZE;
20 gd->arch.high_table_ptr = gd->start_addr_sp;
22 /* clear the memory */
23 #ifdef CONFIG_HAVE_ACPI_RESUME
24 if (gd->arch.prev_sleep_state != ACPI_S3)
26 memset((void *)gd->arch.high_table_ptr, 0,
27 CONFIG_HIGH_TABLE_SIZE);
29 gd->start_addr_sp &= ~0xf;
34 void *high_table_malloc(size_t bytes)
39 new_ptr = gd->arch.high_table_ptr + bytes;
40 if (new_ptr >= gd->arch.high_table_limit)
42 ptr = (void *)gd->arch.high_table_ptr;
43 gd->arch.high_table_ptr = new_ptr;
49 * cb_table_init() - initialize a coreboot table header
51 * This fills in the coreboot table header signature and the header bytes.
52 * Other fields are set to zero.
54 * @cbh: coreboot table header address
56 static void cb_table_init(struct cb_header *cbh)
58 memset(cbh, 0, sizeof(struct cb_header));
59 memcpy(cbh->signature, "LBIO", 4);
60 cbh->header_bytes = sizeof(struct cb_header);
64 * cb_table_add_entry() - add a coreboot table entry
66 * This increases the coreboot table entry size with added table entry length
67 * and increases entry count by 1.
69 * @cbh: coreboot table header address
70 * @cbr: to be added table entry address
71 * @return: pointer to next table entry address
73 static u32 cb_table_add_entry(struct cb_header *cbh, struct cb_record *cbr)
75 cbh->table_bytes += cbr->size;
78 return (u32)cbr + cbr->size;
82 * cb_table_finalize() - finalize the coreboot table
84 * This calculates the checksum for all coreboot table entries as well as
85 * the checksum for the coreboot header itself.
87 * @cbh: coreboot table header address
89 static void cb_table_finalize(struct cb_header *cbh)
91 struct cb_record *cbr = (struct cb_record *)(cbh + 1);
93 cbh->table_checksum = compute_ip_checksum(cbr, cbh->table_bytes);
94 cbh->header_checksum = compute_ip_checksum(cbh, cbh->header_bytes);
97 void write_coreboot_table(u32 addr, struct memory_area *cfg_tables)
99 struct cb_header *cbh = (struct cb_header *)addr;
100 struct cb_record *cbr;
101 struct cb_memory *mem;
102 struct cb_memory_range *map;
103 struct e820_entry e820[32];
104 struct cb_framebuffer *fb;
105 struct vesa_mode_info *vesa;
109 cbr = (struct cb_record *)(cbh + 1);
112 * Two type of coreboot table entries are generated by us.
113 * They are 'struct cb_memory' and 'struct cb_framebuffer'.
116 /* populate memory map table */
117 mem = (struct cb_memory *)cbr;
118 mem->tag = CB_TAG_MEMORY;
121 /* first install e820 defined memory maps */
122 num = install_e820_map(ARRAY_SIZE(e820), e820);
123 for (i = 0; i < num; i++) {
124 map->start.lo = e820[i].addr & 0xffffffff;
125 map->start.hi = e820[i].addr >> 32;
126 map->size.lo = e820[i].size & 0xffffffff;
127 map->size.hi = e820[i].size >> 32;
128 map->type = e820[i].type;
132 /* then install all configuration tables */
133 while (cfg_tables->size) {
134 map->start.lo = cfg_tables->start & 0xffffffff;
135 map->start.hi = cfg_tables->start >> 32;
136 map->size.lo = cfg_tables->size & 0xffffffff;
137 map->size.hi = cfg_tables->size >> 32;
138 map->type = CB_MEM_TABLE;
143 mem->size = num * sizeof(struct cb_memory_range) +
144 sizeof(struct cb_record);
145 cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
147 /* populate framebuffer table if we have sane vesa info */
148 vesa = &mode_info.vesa;
149 if (vesa->x_resolution && vesa->y_resolution) {
150 fb = (struct cb_framebuffer *)cbr;
151 fb->tag = CB_TAG_FRAMEBUFFER;
152 fb->size = sizeof(struct cb_framebuffer);
154 fb->x_resolution = vesa->x_resolution;
155 fb->y_resolution = vesa->y_resolution;
156 fb->bits_per_pixel = vesa->bits_per_pixel;
157 fb->bytes_per_line = vesa->bytes_per_scanline;
158 fb->physical_address = vesa->phys_base_ptr;
159 fb->red_mask_size = vesa->red_mask_size;
160 fb->red_mask_pos = vesa->red_mask_pos;
161 fb->green_mask_size = vesa->green_mask_size;
162 fb->green_mask_pos = vesa->green_mask_pos;
163 fb->blue_mask_size = vesa->blue_mask_size;
164 fb->blue_mask_pos = vesa->blue_mask_pos;
165 fb->reserved_mask_size = vesa->reserved_mask_size;
166 fb->reserved_mask_pos = vesa->reserved_mask_pos;
168 cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
171 cb_table_finalize(cbh);