Debian Codegraph @

src/soc/intel/common/basecode/ramtop/ramtop.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <commonlib/bsd/ipchksum.h>
4 #include <console/console.h>
5 #include <cpu/cpu.h>
6 #include <cpu/x86/mtrr.h>
7 #include <fsp/util.h>
8 #include <intelbasecode/ramtop.h>
9 #include <pc80/mc146818rtc.h>
10 #include <stdint.h>
11
12 /* We need a region in CMOS to store the RAMTOP address */
13
14 #define RAMTOP_SIGNATURE 0x504F5452 /* 'RTOP' */
15
16 /*
17 * Address of the ramtop byte in CMOS. Should be reserved
18 * in mainboards' cmos.layout and not covered by checksum.
19 */
20
21 #if CONFIG(USE_OPTION_TABLE)
22 #include "option_table.h"
23
24 #ifndef CMOS_VSTART_ramtop
25 #error "The `ramtop` CMOS entry is missing, please add it to your cmos.layout."
26 #endif
27
28 #if CMOS_VSTART_ramtop % 8 != 0
29 #error "The `ramtop` CMOS entry needs to be byte aligned, check your cmos.layout."
30 #endif // CMOS_VSTART_ramtop % 8 != 0
31
32 #if CMOS_VLEN_ramtop != (10 * 8)
33 #error "The `ramtop` CMOS entry needs to be 10 bytes long, check your cmos.layout."
34 #endif // CMOS_VLEN_ramtop != (10 * 8)
35
36 #else
37 #define CMOS_VSTART_ramtop 800
38 #endif // CONFIG(USE_OPTION_TABLE)
39
40 struct ramtop_table {
41 uint32_t signature;
42 uint32_t addr;
43 size_t size;
44 uint16_t checksum;
45 } __packed;
46
47 /* Read and validate ramtop_table structure from CMOS */
48 static int ramtop_cmos_read(struct ramtop_table *ramtop)
49 {
50 u8 i, *p;
51 u16 csum;
52
53 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
54 *p = cmos_read((CMOS_VSTART_ramtop / 8) + i);
55
56 /* Verify signature */
57 if (ramtop->signature != RAMTOP_SIGNATURE) {
58 printk(BIOS_DEBUG, "ramtop_table invalid signature\n");
59 return -1;
60 }
61
62 /* Verify RAMTOP size */
63 if (ramtop->size == 0) {
64 printk(BIOS_DEBUG, "ramtop_table holds invalid size\n");
65 return -1;
66 }
67
68 /* Verify checksum over signature and counter only */
69 csum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
70
71 if (csum != ramtop->checksum) {
72 printk(BIOS_DEBUG, "ramtop_table checksum mismatch\n");
73 return -1;
74 }
75
76 return 0;
77 }
78
79 /* Write ramtop_table structure to CMOS */
80 static void ramtop_cmos_write(struct ramtop_table *ramtop)
81 {
82 u8 i, *p;
83
84 /* Checksum over signature and counter only */
85 ramtop->checksum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
86
87 for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
88 cmos_write(*p, (CMOS_VSTART_ramtop / 8) + i);
89 }
90
91 /*
92 * RAMTOP range:
93 *
94 * This defines the memory range covered by RAMTOP, which extends from
95 * cbmem_top down to FSP TOLUM. This range includes essential components:
96 *
97 * +---------------------------+ TOLUM / top_of_ram / cbmem_top
98 * | CBMEM Root |
99 * +---------------------------+
100 * | FSP Reserved Memory |
101 * +---------------------------+
102 * | various CBMEM entries |
103 * +---------------------------+ top_of_stack (8 byte aligned)
104 * | stack (CBMEM entry) |
105 * +---------------------------+ FSP TOLUM
106 * | |
107 * +---------------------------+ 0
108 */
109 static size_t calculate_ramtop_size(uint32_t addr)
110 {
111 struct range_entry fsp_mem;
112 uint32_t fsp_reserve_base;
113 fsp_find_reserved_memory(&fsp_mem);
114
115 fsp_reserve_base = range_entry_base(&fsp_mem);
116 size_t ramtop_size = ALIGN_UP(addr - fsp_reserve_base, 4 * MiB);
117
118 return ramtop_size;
119 }
120
121 /* Update the RAMTOP if required based on the input top_of_ram address */
122 void update_ramtop(uint32_t addr)
123 {
124 struct ramtop_table ramtop;
125
126 /* Read and update ramtop (if required) */
127 if (ramtop_cmos_read(&ramtop) < 0) {
128 /* Structure invalid, re-initialize */
129 ramtop.signature = RAMTOP_SIGNATURE;
130 ramtop.addr = 0;
131 ramtop.size = 0;
132 }
133
134 size_t size = calculate_ramtop_size(addr);
135
136 /* Update ramtop if required */
137 if ((ramtop.addr == addr) && (ramtop.size == size))
138 return;
139
140 ramtop.addr = addr;
141 ramtop.size = size;
142
143 /* Write the new top_of_ram address to CMOS */
144 ramtop_cmos_write(&ramtop);
145
146 printk(BIOS_DEBUG, "Updated the RAMTOP address (0x%x) with size (0x%zx) into CMOS\n",
147 ramtop.addr, ramtop.size);
148 }
149
150 uint32_t get_ramtop_addr(void)
151 {
152 struct ramtop_table ramtop;
153
154 if (ramtop_cmos_read(&ramtop) < 0)
155 return 0;
156
157 return ramtop.addr;
158 }
159
160 static uint32_t get_ramtop_size(void)
161 {
162 struct ramtop_table ramtop;
163
164 if (ramtop_cmos_read(&ramtop) < 0)
165 return 0;
166
167 return ramtop.size;
168 }
169
170 /* Early caching of top_of_ram region */
171 void early_ramtop_enable_cache_range(void)
172 {
173 uint32_t ramtop = get_ramtop_addr();
174 if (!ramtop)
175 return;
176
177 int mtrr = get_free_var_mtrr();
178 if (mtrr == -1) {
179 printk(BIOS_WARNING, "ramtop_table update failure due to no free MTRR available!\n");
180 return;
181 }
182
183 size_t ramtop_size = get_ramtop_size();
184 if (!ramtop_size)
185 return;
186
187 /*
188 * INTEL RECOMMENDATION: Early Ramtop Caching Configuration
189 *
190 * Configuring the Early Caching Ramtop range as Write-Back (WB) before
191 * memory initialization is NOT RECOMMENDED. Speculative execution within
192 * this WB range can lead to issues. WB configuration should be applied
193 * to this range ONLY AFTER memory initialization is complete.
194 *
195 * To enable Ramtop caching before memory initialization, use Write-Combining
196 * (WC) instead of Write-Back (WB).
197 */
198 set_var_mtrr(mtrr, ramtop - ramtop_size, ramtop_size, MTRR_TYPE_WRCOMB);
199 }