Debian Codegraph @

src/northbridge/intel/haswell/native_raminit/raminit_native.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <arch/cpu.h>
4 #include <assert.h>
5 #include <cbmem.h>
6 #include <cf9_reset.h>
7 #include <console/console.h>
8 #include <cpu/x86/msr.h>
9 #include <device/pci_ops.h>
10 #include <mrc_cache.h>
11 #include <northbridge/intel/haswell/haswell.h>
12 #include <northbridge/intel/haswell/raminit.h>
13 #include <southbridge/intel/lynxpoint/me.h>
14 #include <southbridge/intel/lynxpoint/pch.h>
15 #include <types.h>
16
17 #include "raminit_native.h"
18
19 static void wait_txt_clear(void)
20 {
21 const struct cpuid_result cpuid = cpuid_ext(1, 0);
22
23 /* Check if TXT is supported */
24 if (!(cpuid.ecx & BIT(6)))
25 return;
26
27 /* Some TXT public bit */
28 if (!(read32p(0xfed30010) & 1))
29 return;
30
31 /* Wait for TXT clear */
32 do {} while (!(read8p(0xfed40000) & (1 << 7)));
33 }
34
35 static enum raminit_boot_mode get_boot_mode(void)
36 {
37 const uint16_t pmcon_2 = pci_read_config16(PCH_LPC_DEV, GEN_PMCON_2);
38 const uint16_t bitmask = GEN_PMCON_2_DISB | GEN_PMCON_2_MEM_SR;
39 return (pmcon_2 & bitmask) == bitmask ? BOOTMODE_WARM : BOOTMODE_COLD;
40 }
41
42 static bool early_init_native(enum raminit_boot_mode bootmode)
43 {
44 printk(BIOS_DEBUG, "Starting native platform initialisation\n");
45
46 intel_early_me_init();
47 bool cpu_replaced = bootmode == BOOTMODE_COLD && intel_early_me_cpu_replacement_check();
48
49 early_pch_init_native(bootmode == BOOTMODE_S3);
50
51 if (!CONFIG(INTEL_LYNXPOINT_LP))
52 dmi_early_init();
53
54 return cpu_replaced;
55 }
56
57 static void save_mrc_data(void)
58 {
59 mrc_cache_stash_data(MRC_TRAINING_DATA, reg_frame_rev(),
60 reg_frame_ptr(), reg_frame_size());
61 }
62
63 static struct mrc_data prepare_mrc_cache(void)
64 {
65 struct mrc_data md = {0};
66 md.buffer = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
67 reg_frame_rev(),
68 &md.buffer_len);
69 return md;
70 }
71
72 static const char *const bm_names[] = {
73 "BOOTMODE_COLD",
74 "BOOTMODE_WARM",
75 "BOOTMODE_S3",
76 "BOOTMODE_FAST",
77 };
78
79 static void clear_disb(void)
80 {
81 pci_and_config16(PCH_LPC_DEV, GEN_PMCON_2, ~GEN_PMCON_2_DISB);
82 }
83
84 static void raminit_reset(void)
85 {
86 clear_disb();
87 system_reset();
88 }
89
90 static enum raminit_boot_mode do_actual_raminit(
91 const bool s3resume,
92 const bool cpu_replaced,
93 const enum raminit_boot_mode orig_bootmode)
94 {
95 struct mrc_data md = prepare_mrc_cache();
96
97 enum raminit_boot_mode bootmode = orig_bootmode;
98
99 bool save_data_valid = md.buffer && md.buffer_len == reg_frame_size();
100
101 if (s3resume) {
102 if (bootmode == BOOTMODE_COLD) {
103 printk(BIOS_EMERG, "Memory may not be in self-refresh for S3 resume\n");
104 printk(BIOS_EMERG, "S3 resume and cold boot are mutually exclusive\n");
105 raminit_reset();
106 }
107 /* Only a true mad hatter would replace a CPU in S3 */
108 if (cpu_replaced) {
109 printk(BIOS_EMERG, "Oh no, CPU was replaced during S3\n");
110 /*
111 * No reason to continue, memory consistency is most likely lost
112 * and ME will probably request a reset through DID response too.
113 */
114 /** TODO: Figure out why past self commented this out **/
115 //raminit_reset();
116 }
117 bootmode = BOOTMODE_S3;
118 if (!save_data_valid) {
119 printk(BIOS_EMERG, "No training data, S3 resume is impossible\n");
120 /* Failed S3 resume, reset to come up cleanly */
121 raminit_reset();
122 }
123 }
124 if (!s3resume && cpu_replaced) {
125 printk(BIOS_NOTICE, "CPU was replaced, forcing a cold boot\n");
126 /*
127 * Looks like the ME will get angry if raminit takes too long.
128 * It will report that the CPU has been replaced on next boot.
129 * Try to continue anyway. This should not happen in most cases.
130 */
131 /** TODO: Figure out why past self commented this out **/
132 //save_data_valid = false;
133 }
134 if (bootmode == BOOTMODE_COLD) {
135 /* If possible, promote to a fast boot */
136 if (save_data_valid)
137 bootmode = BOOTMODE_FAST;
138
139 clear_disb();
140 } else if (bootmode == BOOTMODE_WARM) {
141 /* If a warm reset happened before raminit is done, force a cold boot */
142 if (mchbar_read32(SSKPD) == 0 && mchbar_read32(SSKPD + 4) == 0) {
143 printk(BIOS_NOTICE, "Warm reset occurred early in cold boot\n");
144 save_data_valid = false;
145 }
146 if (!save_data_valid)
147 bootmode = BOOTMODE_COLD;
148 }
149 assert(save_data_valid != (bootmode == BOOTMODE_COLD));
150 if (save_data_valid) {
151 printk(BIOS_INFO, "Using cached memory parameters\n");
152 memcpy(reg_frame_ptr(), md.buffer, reg_frame_size());
153 }
154 printk(RAM_DEBUG, "Initial bootmode: %s\n", bm_names[orig_bootmode]);
155 printk(RAM_DEBUG, "Current bootmode: %s\n", bm_names[bootmode]);
156
157 /*
158 * And now, the actual memory initialization thing.
159 */
160 printk(RAM_DEBUG, "\nStarting native raminit\n");
161 raminit_main(bootmode);
162
163 return bootmode;
164 }
165
166 void perform_raminit(const int s3resume)
167 {
168 /*
169 * See, this function's name is a lie. There are more things to
170 * do that memory initialisation, but they are relatively easy.
171 */
172 const enum raminit_boot_mode orig_bootmode = get_boot_mode();
173
174 const bool cpu_replaced = early_init_native(s3resume ? BOOTMODE_S3 : orig_bootmode);
175
176 wait_txt_clear();
177 wrmsr(0x2e6, (msr_t) {.lo = 0, .hi = 0});
178
179 const enum raminit_boot_mode bootmode =
180 do_actual_raminit(s3resume, cpu_replaced, orig_bootmode);
181
182 /** TODO: report_memory_config **/
183
184 if (intel_early_me_uma_size() > 0) {
185 /*
186 * The 'other' success value is to report loss of memory
187 * consistency to ME if warm boot was downgraded to cold.
188 */
189 uint8_t me_status;
190 if (BOOTMODE_WARM == orig_bootmode && BOOTMODE_COLD == bootmode)
191 me_status = ME_INIT_STATUS_SUCCESS_OTHER;
192 else
193 me_status = ME_INIT_STATUS_SUCCESS;
194
195 intel_early_me_init_done(me_status);
196 }
197
198 intel_early_me_status();
199
200 const bool cbmem_was_initted = !cbmem_recovery(s3resume);
201 if (s3resume && !cbmem_was_initted) {
202 /* Failed S3 resume, reset to come up cleanly */
203 printk(BIOS_CRIT, "Failed to recover CBMEM in S3 resume.\n");
204 system_reset();
205 }
206
207 /* Save training data on non-S3 resumes */
208 if (!s3resume)
209 save_mrc_data();
210
211 /** TODO: setup_sdram_meminfo **/
212 }