blob: ae4459a7e0732cb106924cea5c2e8c24fbbeda73 [file] [log] [blame]
Angel Pons6c42d142021-06-14 13:53:44 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <assert.h>
4#include <console/console.h>
5#include <console/streams.h>
6#include <console/usb.h>
7#include <string.h>
8#include <cbmem.h>
9#include <cbfs.h>
10#include <cf9_reset.h>
11#include <ip_checksum.h>
12#include <memory_info.h>
13#include <mrc_cache.h>
14#include <device/device.h>
15#include <device/pci_def.h>
16#include <device/pci_ops.h>
17#include <device/dram/ddr3.h>
18#include <northbridge/intel/haswell/chip.h>
19#include <northbridge/intel/haswell/haswell.h>
20#include <northbridge/intel/haswell/raminit.h>
21#include <smbios.h>
22#include <spd.h>
23#include <security/vboot/vboot_common.h>
24#include <commonlib/region.h>
25#include <southbridge/intel/lynxpoint/me.h>
26#include <southbridge/intel/lynxpoint/pch.h>
27#include <timestamp.h>
28#include <types.h>
29
30#include "pei_data.h"
31
32static void save_mrc_data(struct pei_data *pei_data)
33{
34 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", pei_data->data_to_save,
35 pei_data->data_to_save_size);
36
37 if (pei_data->data_to_save != NULL && pei_data->data_to_save_size > 0)
38 mrc_cache_stash_data(MRC_TRAINING_DATA, 0,
39 pei_data->data_to_save,
40 pei_data->data_to_save_size);
41}
42
43static const char *const ecc_decoder[] = {
44 "inactive",
45 "active on IO",
46 "disabled on IO",
47 "active",
48};
49
50/*
51 * Dump in the log memory controller configuration as read from the memory
52 * controller registers.
53 */
54static void report_memory_config(void)
55{
56 int i;
57
58 const u32 addr_decoder_common = mchbar_read32(MAD_CHNL);
59
60 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
61 (mchbar_read32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
62
63 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
64 (addr_decoder_common >> 0) & 3,
65 (addr_decoder_common >> 2) & 3,
66 (addr_decoder_common >> 4) & 3);
67
68 for (i = 0; i < NUM_CHANNELS; i++) {
69 const u32 ch_conf = mchbar_read32(MAD_DIMM(i));
70
71 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
72 printk(BIOS_DEBUG, " ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
73 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
74 ((ch_conf >> 22) & 1) ? "on" : "off");
75
76 printk(BIOS_DEBUG, " rank interleave %s\n",
77 ((ch_conf >> 21) & 1) ? "on" : "off");
78
79 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
80 ((ch_conf >> 0) & 0xff) * 256,
81 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
82 ((ch_conf >> 17) & 1) ? "dual" : "single",
83 ((ch_conf >> 16) & 1) ? "" : ", selected");
84
85 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
86 ((ch_conf >> 8) & 0xff) * 256,
87 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
88 ((ch_conf >> 18) & 1) ? "dual" : "single",
89 ((ch_conf >> 16) & 1) ? ", selected" : "");
90 }
91}
92
93typedef int ABI_X86 (*pei_wrapper_entry_t)(struct pei_data *pei_data);
94
95static void ABI_X86 send_to_console(unsigned char b)
96{
97 console_tx_byte(b);
98}
99
100/*
101 * Find PEI executable in coreboot filesystem and execute it.
102 */
103static void sdram_initialize(struct pei_data *pei_data)
104{
105 size_t mrc_size;
106 pei_wrapper_entry_t entry;
107 int ret;
108
109 /* Assume boot device is memory mapped. */
110 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
111
112 pei_data->saved_data =
113 mrc_cache_current_mmap_leak(MRC_TRAINING_DATA, 0,
114 &mrc_size);
115 if (pei_data->saved_data) {
116 /* MRC cache found */
117 pei_data->saved_data_size = mrc_size;
118 } else if (pei_data->boot_mode == ACPI_S3) {
119 /* Waking from S3 and no cache. */
120 printk(BIOS_DEBUG,
121 "No MRC cache found in S3 resume path.\n");
lilacious40cb3fe2023-06-21 23:24:14 +0200122 post_code(POSTCODE_RESUME_FAILURE);
Angel Pons6c42d142021-06-14 13:53:44 +0200123 system_reset();
124 } else {
125 printk(BIOS_DEBUG, "No MRC cache found.\n");
126 }
127
128 /*
129 * Do not use saved pei data. Can be set by mainboard romstage
130 * to force a full train of memory on every boot.
131 */
132 if (pei_data->disable_saved_data) {
133 printk(BIOS_DEBUG, "Disabling PEI saved data by request\n");
134 pei_data->saved_data = NULL;
135 pei_data->saved_data_size = 0;
136 }
137
138 /* We don't care about leaking the mapping */
139 entry = cbfs_ro_map("mrc.bin", NULL);
140 if (entry == NULL)
141 die("mrc.bin not found!");
142
143 printk(BIOS_DEBUG, "Starting Memory Reference Code\n");
144
145 ret = entry(pei_data);
146 if (ret < 0)
147 die("pei_data version mismatch\n");
148
149 /* Print the MRC version after executing the UEFI PEI stage. */
150 u32 version = mchbar_read32(MRC_REVISION);
151 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
152 (version >> 24) & 0xff, (version >> 16) & 0xff,
153 (version >> 8) & 0xff, (version >> 0) & 0xff);
154
155 report_memory_config();
156}
157
158static uint8_t nb_get_ecc_type(const uint32_t capid0_a)
159{
160 return capid0_a & CAPID_ECCDIS ? MEMORY_ARRAY_ECC_NONE : MEMORY_ARRAY_ECC_SINGLE_BIT;
161}
162
163static uint16_t nb_slots_per_channel(const uint32_t capid0_a)
164{
165 return !(capid0_a & CAPID_DDPCD) + 1;
166}
167
168static uint16_t nb_number_of_channels(const uint32_t capid0_a)
169{
170 return !(capid0_a & CAPID_PDCD) + 1;
171}
172
173static uint32_t nb_max_chan_capacity_mib(const uint32_t capid0_a)
174{
175 uint32_t ddrsz;
176
177 /* Values from documentation, which assume two DIMMs per channel */
178 switch (CAPID_DDRSZ(capid0_a)) {
179 case 1:
180 ddrsz = 8192;
181 break;
182 case 2:
183 ddrsz = 2048;
184 break;
185 case 3:
186 ddrsz = 512;
187 break;
188 default:
189 ddrsz = 16384;
190 break;
191 }
192
193 /* Account for the maximum number of DIMMs per channel */
194 return (ddrsz / 2) * nb_slots_per_channel(capid0_a);
195}
196
197static void setup_sdram_meminfo(struct pei_data *pei_data)
198{
199 unsigned int dimm_cnt = 0;
200
201 struct memory_info *mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
202 if (!mem_info)
203 die("Failed to add memory info to CBMEM.\n");
204
205 memset(mem_info, 0, sizeof(struct memory_info));
206
207 const u32 ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100;
208
209 for (unsigned int ch = 0; ch < NUM_CHANNELS; ch++) {
210 const u32 ch_conf = mchbar_read32(MAD_DIMM(ch));
211 for (unsigned int slot = 0; slot < NUM_SLOTS; slot++) {
212 const u32 dimm_size = ((ch_conf >> (slot * 8)) & 0xff) * 256;
213 if (dimm_size) {
214 struct dimm_info *dimm = &mem_info->dimm[dimm_cnt];
215 dimm->dimm_size = dimm_size;
216 dimm->ddr_type = MEMORY_TYPE_DDR3;
217 dimm->ddr_frequency = ddr_frequency;
218 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + slot)) & 1);
219 dimm->channel_num = ch;
220 dimm->dimm_num = slot;
221 dimm->bank_locator = ch * 2;
222 memcpy(dimm->serial,
223 &pei_data->spd_data[ch][slot][SPD_DIMM_SERIAL_NUM],
224 SPD_DIMM_SERIAL_LEN);
225 memcpy(dimm->module_part_number,
226 &pei_data->spd_data[ch][slot][SPD_DIMM_PART_NUM],
227 SPD_DIMM_PART_LEN);
228 dimm->mod_id =
229 (pei_data->spd_data[ch][slot][SPD_DIMM_MOD_ID2] << 8) |
230 (pei_data->spd_data[ch][slot][SPD_DIMM_MOD_ID1] & 0xff);
231 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
232 dimm->bus_width = MEMORY_BUS_WIDTH_64;
233 dimm_cnt++;
234 }
235 }
236 }
237 mem_info->dimm_cnt = dimm_cnt;
238
239 const uint32_t capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
240
241 const uint16_t channels = nb_number_of_channels(capid0_a);
242
243 mem_info->ecc_type = nb_get_ecc_type(capid0_a);
244 mem_info->max_capacity_mib = channels * nb_max_chan_capacity_mib(capid0_a);
245 mem_info->number_of_devices = channels * nb_slots_per_channel(capid0_a);
246}
247
248#include <device/smbus_host.h>
249#define SPD_LEN 256
250
251/* Copy SPD data for on-board memory */
252static void copy_spd(struct pei_data *pei_data, struct spd_info *spdi)
253{
254 if (!CONFIG(HAVE_SPD_IN_CBFS))
255 return;
256
257 printk(BIOS_DEBUG, "SPD index %d\n", spdi->spd_index);
258
259 size_t spd_file_len;
260 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
261
262 if (!spd_file)
263 die("SPD data not found.");
264
265 if (spd_file_len < ((spdi->spd_index + 1) * SPD_LEN)) {
266 printk(BIOS_ERR, "SPD index override to 0 - old hardware?\n");
267 spdi->spd_index = 0;
268 }
269
270 if (spd_file_len < SPD_LEN)
271 die("Missing SPD data.");
272
273 /* MRC only uses index 0, but coreboot uses the other indices */
274 memcpy(pei_data->spd_data[0], spd_file + (spdi->spd_index * SPD_LEN), SPD_LEN);
275
276 for (size_t i = 1; i < ARRAY_SIZE(spdi->addresses); i++) {
277 if (spdi->addresses[i] == SPD_MEMORY_DOWN)
278 memcpy(pei_data->spd_data[i], pei_data->spd_data[0], SPD_LEN);
279 }
280}
281
282/*
283 * 0 = leave channel enabled
284 * 1 = disable dimm 0 on channel
285 * 2 = disable dimm 1 on channel
286 * 3 = disable dimm 0+1 on channel
287 */
288static int make_channel_disabled_mask(const struct pei_data *pd, int ch)
289{
290 return (!pd->spd_addresses[ch + ch] << 0) | (!pd->spd_addresses[ch + ch + 1] << 1);
291}
292
293static enum pei_usb2_port_location map_to_pei_usb2_location(const enum usb2_port_location loc)
294{
295 /* TODO: USB_PORT_NGFF_DEVICE_DOWN (not used by any board, though) */
296 static const enum pei_usb2_port_location map[] = {
297 [USB_PORT_SKIP] = PEI_USB_PORT_SKIP,
298 [USB_PORT_BACK_PANEL] = PEI_USB_PORT_BACK_PANEL,
299 [USB_PORT_FRONT_PANEL] = PEI_USB_PORT_FRONT_PANEL,
300 [USB_PORT_DOCK] = PEI_USB_PORT_DOCK,
301 [USB_PORT_MINI_PCIE] = PEI_USB_PORT_MINI_PCIE,
302 [USB_PORT_FLEX] = PEI_USB_PORT_FLEX,
303 [USB_PORT_INTERNAL] = PEI_USB_PORT_INTERNAL,
304 };
305 return loc >= ARRAY_SIZE(map) ? PEI_USB_PORT_SKIP : map[loc];
306}
307
308static uint8_t map_to_pei_oc_pin(const uint8_t oc_pin)
309{
310 return oc_pin >= USB_OC_PIN_SKIP ? PEI_USB_OC_PIN_SKIP : oc_pin;
311}
312
313static bool early_init_native(int s3resume)
314{
315 printk(BIOS_DEBUG, "Starting native platform initialisation\n");
316
317 intel_early_me_init();
318 /** TODO: CPU replacement check must be skipped in warm boots and S3 resumes **/
319 const bool cpu_replaced = !s3resume && intel_early_me_cpu_replacement_check();
320
321 early_pch_init_native(s3resume);
322
323 if (!CONFIG(INTEL_LYNXPOINT_LP))
324 dmi_early_init();
325
326 return cpu_replaced;
327}
328
329void perform_raminit(const int s3resume)
330{
331 const struct northbridge_intel_haswell_config *cfg = config_of_soc();
332
333 struct pei_data pei_data = {
334 .pei_version = PEI_VERSION,
335 .board_type = get_pch_platform_type(),
336 .usbdebug = CONFIG(USBDEBUG),
337 .pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS,
338 .smbusbar = CONFIG_FIXED_SMBUS_IO_BASE,
339 .ehcibar = CONFIG_EHCI_BAR,
340 .xhcibar = 0xd7000000,
341 .gttbar = 0xe0000000,
342 .pmbase = DEFAULT_PMBASE,
343 .gpiobase = DEFAULT_GPIOBASE,
344 .tseg_size = CONFIG_SMM_TSEG_SIZE,
345 .temp_mmio_base = 0xfed08000,
346 .ec_present = cfg->ec_present,
347 .dq_pins_interleaved = cfg->dq_pins_interleaved,
348 .tx_byte = send_to_console,
349 .ddr_refresh_2x = CONFIG(ENABLE_DDR_2X_REFRESH),
350 .rcba = CONFIG_FIXED_RCBA_MMIO_BASE, /* Might be unused */
351 };
352
353 for (size_t i = 0; i < ARRAY_SIZE(mainboard_usb2_ports); i++) {
354 /* If a port is not enabled, skip it */
355 if (!mainboard_usb2_ports[i].enable) {
356 pei_data.usb2_ports[i].oc_pin = PEI_USB_OC_PIN_SKIP;
357 pei_data.usb2_ports[i].location = PEI_USB_PORT_SKIP;
358 continue;
359 }
360 const enum usb2_port_location loc = mainboard_usb2_ports[i].location;
361 const uint8_t oc_pin = mainboard_usb2_ports[i].oc_pin;
362 pei_data.usb2_ports[i].length = mainboard_usb2_ports[i].length;
363 pei_data.usb2_ports[i].enable = mainboard_usb2_ports[i].enable;
364 pei_data.usb2_ports[i].oc_pin = map_to_pei_oc_pin(oc_pin);
365 pei_data.usb2_ports[i].location = map_to_pei_usb2_location(loc);
366 }
367
368 for (size_t i = 0; i < ARRAY_SIZE(mainboard_usb3_ports); i++) {
369 const uint8_t oc_pin = mainboard_usb3_ports[i].oc_pin;
370 pei_data.usb3_ports[i].enable = mainboard_usb3_ports[i].enable;
371 pei_data.usb3_ports[i].oc_pin = map_to_pei_oc_pin(oc_pin);
372 }
373
374 /* Broadwell MRC uses ACPI values for boot_mode */
375 pei_data.boot_mode = s3resume ? ACPI_S3 : ACPI_S0;
376
377 /* Obtain the SPD addresses from mainboard code */
378 struct spd_info spdi = {0};
379 mb_get_spd_map(&spdi);
380
381 /*
382 * Read the SPDs over SMBus in coreboot code so that the data can be used to
383 * populate meminfo. MRC returns some data, but it seems to be incomplete.
384 */
385 for (size_t i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
386 const uint8_t addr = spdi.addresses[i];
387 pei_data.spd_addresses[i] = addr == SPD_MEMORY_DOWN ? 0xff : addr << 1;
388 if (addr == SPD_MEMORY_DOWN)
389 continue;
390
391 if (i2c_eeprom_read(addr, 0, 256, pei_data.spd_data[i / 2][i % 2]) != 256) {
392 printk(BIOS_ERR, "0x%02x failed to read\n", addr);
393 memset(pei_data.spd_data[i / 2][i % 2], 0, 256);
394 }
395 }
396
397 /* Calculate unimplemented DIMM slots for each channel */
398 pei_data.dimm_channel0_disabled = make_channel_disabled_mask(&pei_data, 0);
399 pei_data.dimm_channel1_disabled = make_channel_disabled_mask(&pei_data, 1);
400
401 for (size_t i = 0; i < ARRAY_SIZE(spdi.addresses); i++)
402 pei_data.spd_addresses[i] = 0;
403
404 if (early_init_native(s3resume))
405 pei_data.disable_saved_data = true;
406
407 timestamp_add_now(TS_INITRAM_START);
408
409 copy_spd(&pei_data, &spdi);
410
411 sdram_initialize(&pei_data);
412
413 timestamp_add_now(TS_INITRAM_END);
414
415 if (intel_early_me_uma_size() > 0) {
416 /*
417 * The 'other' success value is to report loss of memory
418 * consistency to ME if warm boot was downgraded to cold.
419 * However, we can't tell if MRC downgraded the bootmode.
420 */
421 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS_OTHER);
422 }
423
424 intel_early_me_status();
425
426 int cbmem_was_initted = !cbmem_recovery(s3resume);
427 if (s3resume && !cbmem_was_initted) {
428 /* Failed S3 resume, reset to come up cleanly */
429 printk(BIOS_CRIT, "Failed to recover CBMEM in S3 resume.\n");
430 system_reset();
431 }
432
433 /* Save data returned from MRC on non-S3 resumes. */
434 if (!s3resume)
435 save_mrc_data(&pei_data);
436
437 setup_sdram_meminfo(&pei_data);
438}