blob: d59aa8687fdf5612624b60bdfba7a8a3457960a6 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002
Felix Held972d9f22022-02-23 16:32:20 +01003#include <arch/hpet.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004#include <console/console.h>
5#include <console/usb.h>
Elyes HAOUASc0567292019-04-28 17:57:47 +02006#include <cf9_reset.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07007#include <string.h>
Nico Huber47bf4982019-11-17 02:58:00 +01008#include <device/device.h>
Elyes HAOUAS921b99e2022-01-26 08:01:08 +01009#include <device/dram/ddr3.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Patrick Rudolph5709e032019-03-25 10:12:14 +010011#include <arch/cpu.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070012#include <cbmem.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070013#include <cbfs.h>
14#include <ip_checksum.h>
15#include <pc80/mc146818rtc.h>
16#include <device/pci_def.h>
Kyösti Mälkkib697c902019-01-30 08:19:49 +020017#include <lib.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010018#include <mrc_cache.h>
Elyes HAOUASa233eb42022-01-26 07:51:28 +010019#include <spd.h>
Elyes HAOUAS62b23c12022-01-26 07:43:51 +010020#include <smbios.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020021#include <stddef.h>
22#include <stdint.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010023#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070024#include "raminit.h"
25#include "pei_data.h"
26#include "sandybridge.h"
Patrick Rudolph5709e032019-03-25 10:12:14 +010027#include "chip.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020028#include <security/vboot/vboot_common.h>
Patrick Georgi27fbbcf2019-04-23 12:33:23 +020029#include <southbridge/intel/bd82x6x/pch.h>
Matt DeVillierff1ef8d2016-12-24 15:36:24 -060030#include <memory_info.h>
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010031#include <mode_switch.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070032
33/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020034#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070035
36/*
37 * MRC scrambler seed offsets should be reserved in
38 * mainboard cmos.layout and not covered by checksum.
39 */
Julius Wernercd49cce2019-03-05 16:53:33 -080040#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041#include "option_table.h"
Angel Pons7c49cb82020-03-16 23:17:32 +010042#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
43#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070044#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
45#else
46#define CMOS_OFFSET_MRC_SEED 152
47#define CMOS_OFFSET_MRC_SEED_S3 156
48#define CMOS_OFFSET_MRC_SEED_CHK 160
49#endif
50
Arthur Heymans7539b8c2017-12-24 10:42:57 +010051#define MRC_CACHE_VERSION 0
52
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010053/* Assembly functions: */
54void mrc_wrapper(void *func_ptr, uint32_t arg1);
55
Arthur Heymans0f89a112022-04-18 17:14:37 +020056static void save_mrc_data(struct pei_data *pei_data)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070057{
58 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070059
60 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010061 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
62 (void *)(uintptr_t)pei_data->mrc_output_ptr,
63 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070064
65 /* Save the MRC seed values to CMOS */
Kyösti Mälkki28791072020-01-04 12:58:53 +020066 cmos_write32(pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070067 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
68 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
69
Kyösti Mälkki28791072020-01-04 12:58:53 +020070 cmos_write32(pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070071 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
72 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
73
74 /* Save a simple checksum of the seed values */
Angel Pons7c49cb82020-03-16 23:17:32 +010075 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
76 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070077 checksum = add_ip_checksums(sizeof(u32), c1, c2);
78
Angel Pons7c49cb82020-03-16 23:17:32 +010079 cmos_write((checksum >> 0) & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
80 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK + 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070081}
82
83static void prepare_mrc_cache(struct pei_data *pei_data)
84{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070085 u16 c1, c2, checksum, seed_checksum;
Shelley Chenad9cd682020-07-23 16:10:52 -070086 size_t mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070087
Angel Pons7c49cb82020-03-16 23:17:32 +010088 /* Preset just in case there is an error */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010089 pei_data->mrc_input_ptr = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070090 pei_data->mrc_input_len = 0;
91
92 /* Read scrambler seeds from CMOS */
93 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
94 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
95 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
96
97 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
98 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
99 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
100
101 /* Compute seed checksum and compare */
Angel Pons7c49cb82020-03-16 23:17:32 +0100102 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
103 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700104 checksum = add_ip_checksums(sizeof(u32), c1, c2);
105
Angel Pons7c49cb82020-03-16 23:17:32 +0100106 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
107 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700108
109 if (checksum != seed_checksum) {
110 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
111 pei_data->scrambler_seed = 0;
112 pei_data->scrambler_seed_s3 = 0;
113 return;
114 }
115
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100116 pei_data->mrc_input_ptr = (uintptr_t)mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
Shelley Chenad9cd682020-07-23 16:10:52 -0700117 MRC_CACHE_VERSION,
118 &mrc_size);
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100119 if (!pei_data->mrc_input_ptr) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100120 /* Error message printed in find_current_mrc_cache */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700121 return;
122 }
123
Shelley Chenad9cd682020-07-23 16:10:52 -0700124 pei_data->mrc_input_len = mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700125
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100126 printk(BIOS_DEBUG, "%s: at 0x%x, size %zx\n", __func__,
127 pei_data->mrc_input_ptr, mrc_size);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700128}
129
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700130/**
131 * Find PEI executable in coreboot filesystem and execute it.
132 *
133 * @param pei_data: configuration data for UEFI PEI reference code
134 */
Arthur Heymans0f89a112022-04-18 17:14:37 +0200135static void sdram_initialize(struct pei_data *pei_data)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700136{
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100137 int (*entry)(struct pei_data *pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700138
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700139 /* Wait for ME to be ready */
140 intel_early_me_init();
141 intel_early_me_uma_size();
142
143 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
144
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700145 /*
Shelley Chen6615c6e2020-10-27 15:58:31 -0700146 * Always pass in mrc_cache data. The driver will determine
147 * whether to use the data or not.
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700148 */
Shelley Chen6615c6e2020-10-27 15:58:31 -0700149 prepare_mrc_cache(pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700150
151 /* If MRC data is not found we cannot continue S3 resume. */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100152 if (pei_data->boot_mode == 2 && !pei_data->mrc_input_ptr) {
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100153 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUASc0567292019-04-28 17:57:47 +0200154 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700155 }
156
157 /* Pass console handler in pei_data */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100158 pei_data->tx_byte_ptr = (uintptr_t)do_putchar;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700159
160 /* Locate and call UEFI System Agent binary. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800161 entry = cbfs_map("mrc.bin", NULL);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700162 if (entry) {
163 int rv;
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100164 rv = protected_mode_call_2arg(mrc_wrapper, (uintptr_t)entry, (uintptr_t)pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700165 if (rv) {
166 switch (rv) {
167 case -1:
168 printk(BIOS_ERR, "PEI version mismatch.\n");
169 break;
170 case -2:
171 printk(BIOS_ERR, "Invalid memory frequency.\n");
172 break;
173 default:
174 printk(BIOS_ERR, "MRC returned %x.\n", rv);
175 }
lilacious40cb3fe2023-06-21 23:24:14 +0200176 die_with_post_code(POSTCODE_INVALID_VENDOR_BINARY,
Keith Shortbb41aba2019-05-16 14:07:43 -0600177 "Nonzero MRC return value.\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700178 }
179 } else {
180 die("UEFI PEI System Agent not found.\n");
181 }
182
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700183 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800184 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200185 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700186
Angel Pons9f3bc3712020-10-13 23:57:10 +0200187 /* Print the MRC version after executing the UEFI PEI stage */
Angel Pons66780a02021-03-26 13:33:22 +0100188 u32 version = mchbar_read32(MRC_REVISION);
Angel Ponsc1328a62021-06-14 12:43:11 +0200189 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100190 (version >> 24) & 0xff, (version >> 16) & 0xff,
191 (version >> 8) & 0xff, (version >> 0) & 0xff);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700192
Angel Pons7c49cb82020-03-16 23:17:32 +0100193 /*
194 * Send ME init done for SandyBridge here.
195 * This is done inside the SystemAgent binary on IvyBridge.
196 */
197 if (BASE_REV_SNB == (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700198 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
199 else
200 intel_early_me_status();
201
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700202 report_memory_config();
203}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100204
Angel Pons7c49cb82020-03-16 23:17:32 +0100205/*
206 * These are the location and structure of MRC_VAR data in CAR.
207 * The CAR region looks like this:
208 * +------------------+ -> DCACHE_RAM_BASE
209 * | |
210 * | |
211 * | COREBOOT STACK |
212 * | |
213 * | |
214 * +------------------+ -> DCACHE_RAM_BASE + DCACHE_RAM_SIZE
215 * | |
216 * | MRC HEAP |
217 * | size = 0x5000 |
218 * | |
219 * +------------------+
220 * | |
221 * | MRC VAR |
222 * | size = 0x4000 |
223 * | |
224 * +------------------+ -> DACHE_RAM_BASE + DACHE_RAM_SIZE
225 * + DCACHE_RAM_MRC_VAR_SIZE
Arthur Heymans01c83a22019-06-05 13:36:55 +0200226 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100227#define DCACHE_RAM_MRC_VAR_BASE (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE \
228 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 0x4000)
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200229
230struct mrc_var_data {
231 u32 acpi_timer_flag;
232 u32 pool_used;
233 u32 pool_base;
234 u32 tx_byte;
235 u32 reserved[4];
236} __packed;
237
Patrick Rudolph5709e032019-03-25 10:12:14 +0100238static void northbridge_fill_pei_data(struct pei_data *pei_data)
239{
Angel Ponsd9e58dc2021-01-20 01:22:20 +0100240 pei_data->mchbar = CONFIG_FIXED_MCHBAR_MMIO_BASE;
241 pei_data->dmibar = CONFIG_FIXED_DMIBAR_MMIO_BASE;
242 pei_data->epbar = CONFIG_FIXED_EPBAR_MMIO_BASE;
Shelley Chen4e9bb332021-10-20 15:43:45 -0700243 pei_data->pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
Felix Held972d9f22022-02-23 16:32:20 +0100244 pei_data->hpet_address = HPET_BASE_ADDRESS;
Angel Pons7c49cb82020-03-16 23:17:32 +0100245 pei_data->thermalbase = 0xfed08000;
246 pei_data->system_type = !(get_platform_type() == PLATFORM_MOBILE);
247 pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100248
249 if ((cpu_get_cpuid() & 0xffff0) == 0x306a0) {
250 const struct device *dev = pcidev_on_root(1, 0);
251 pei_data->pcie_init = dev && dev->enabled;
252 } else {
253 pei_data->pcie_init = 0;
254 }
255}
256
257static void southbridge_fill_pei_data(struct pei_data *pei_data)
258{
259 const struct device *dev = pcidev_on_root(0x19, 0);
260
Angel Ponsb21bffa2020-07-03 01:02:28 +0200261 pei_data->smbusbar = CONFIG_FIXED_SMBUS_IO_BASE;
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 pei_data->wdbbar = 0x04000000;
263 pei_data->wdbsize = 0x1000;
Angel Pons92717ff2020-09-14 16:22:22 +0200264 pei_data->rcba = (uintptr_t)DEFAULT_RCBA;
Angel Pons7c49cb82020-03-16 23:17:32 +0100265 pei_data->pmbase = DEFAULT_PMBASE;
266 pei_data->gpiobase = DEFAULT_GPIOBASE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100267 pei_data->gbe_enable = dev && dev->enabled;
268}
269
270static void devicetree_fill_pei_data(struct pei_data *pei_data)
271{
Keith Hui1e9601c2023-07-15 12:08:51 -0400272 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
Patrick Rudolph5709e032019-03-25 10:12:14 +0100273
274 switch (cfg->max_mem_clock_mhz) {
275 /* MRC only supports fixed numbers of frequencies */
276 default:
277 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
Arthur Heymansfff20212021-03-15 14:56:16 +0100278 __fallthrough;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100279 case 400:
280 pei_data->max_ddr3_freq = 800;
281 break;
282 case 533:
283 pei_data->max_ddr3_freq = 1066;
284 break;
285 case 666:
286 pei_data->max_ddr3_freq = 1333;
287 break;
288 case 800:
289 pei_data->max_ddr3_freq = 1600;
290 break;
291
292 }
293
Keith Hui1e9601c2023-07-15 12:08:51 -0400294 /*
295 * SPD addresses are listed in devicetree as actual addresses,
296 * and for MRC need to be shifted left so bit 0 is always zero.
297 */
298 if (!CONFIG(HAVE_SPD_IN_CBFS)) {
299 for (unsigned int i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
300 pei_data->spd_addresses[i] = cfg->spd_addresses[i] << 1;
301 }
302 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100303 memcpy(pei_data->ts_addresses, cfg->ts_addresses, sizeof(pei_data->ts_addresses));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100304
Angel Pons7c49cb82020-03-16 23:17:32 +0100305 pei_data->ec_present = cfg->ec_present;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100306 pei_data->ddr3lv_support = cfg->ddr3lv_support;
307
308 pei_data->nmode = cfg->nmode;
309 pei_data->ddr_refresh_rate_config = cfg->ddr_refresh_rate_config;
310
311 memcpy(pei_data->usb_port_config, cfg->usb_port_config,
312 sizeof(pei_data->usb_port_config));
313
Angel Pons7c49cb82020-03-16 23:17:32 +0100314 pei_data->usb3.mode = cfg->usb3.mode;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100315 pei_data->usb3.hs_port_switch_mask = cfg->usb3.hs_port_switch_mask;
Angel Pons7c49cb82020-03-16 23:17:32 +0100316 pei_data->usb3.preboot_support = cfg->usb3.preboot_support;
317 pei_data->usb3.xhci_streams = cfg->usb3.xhci_streams;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100318}
319
Keith Hui1e9601c2023-07-15 12:08:51 -0400320static void spd_fill_pei_data(struct pei_data *pei_data)
321{
322 struct spd_info spdi = {0};
Jeremy Compostellacaa0c0e2023-11-16 08:48:23 -0800323 unsigned int i, have_memory_down = 0;
Keith Hui1e9601c2023-07-15 12:08:51 -0400324
325 mb_get_spd_map(&spdi);
326
327 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
328 if (spdi.addresses[i] == SPD_MEMORY_DOWN) {
329 pei_data->spd_addresses[i] = 0;
330 have_memory_down = 1;
331 } else {
332 /* MRC expects left-aligned SMBus addresses. */
333 pei_data->spd_addresses[i] = spdi.addresses[i] << 1;
334 }
335 }
336 /* Copy SPD data from CBFS for on-board memory */
337 if (have_memory_down) {
338 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
339
340 size_t spd_file_len;
341 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
342
343 if (!spd_file)
344 die("SPD data %s!", "not found");
345
346 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
347 die("SPD data %s!", "incomplete");
348
349 /* MRC only uses index 0... */
350 memcpy(pei_data->spd_data[0], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
351
352 /* but coreboot uses the other indices */
353 for (i = 1; i < ARRAY_SIZE(spdi.addresses); i++) {
354 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
355 memcpy(pei_data->spd_data[i], pei_data->spd_data[0], SPD_SIZE_MAX_DDR3);
356 }
357 }
358}
359
Nico Huber47bf4982019-11-17 02:58:00 +0100360static void disable_p2p(void)
361{
Angel Pons7c49cb82020-03-16 23:17:32 +0100362 /* Disable PCI-to-PCI bridge early to prevent probing by MRC */
Nico Huber47bf4982019-11-17 02:58:00 +0100363 const struct device *const p2p = pcidev_on_root(0x1e, 0);
364 if (p2p && p2p->enabled)
365 return;
366
367 RCBA32(FD) |= PCH_DISABLE_P2P;
368}
369
Arthur Heymans0f89a112022-04-18 17:14:37 +0200370static void setup_sdram_meminfo(struct pei_data *pei_data);
371
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100372void perform_raminit(int s3resume)
373{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100374 struct pei_data pei_data;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200375 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100376
377 /* Prepare USB controller early in S3 resume */
Keith Huic5d6af42023-03-20 02:03:47 -0400378 if (s3resume)
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100379 enable_usb_bar();
380
Patrick Rudolph5709e032019-03-25 10:12:14 +0100381 memset(&pei_data, 0, sizeof(pei_data));
Elyes Haouas24f4e972022-07-13 18:47:27 +0200382 pei_data.pei_version = PEI_VERSION;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100383
384 northbridge_fill_pei_data(&pei_data);
385 southbridge_fill_pei_data(&pei_data);
386 devicetree_fill_pei_data(&pei_data);
Keith Hui1e9601c2023-07-15 12:08:51 -0400387 if (CONFIG(HAVE_SPD_IN_CBFS))
388 spd_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100389 mainboard_fill_pei_data(&pei_data);
390
391 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200392
Patrick Rudolph5709e032019-03-25 10:12:14 +0100393 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
394 pei_data.dimm_channel0_disabled =
395 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
396 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
397
398 pei_data.dimm_channel1_disabled =
399 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
400 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
401
Nico Huber47bf4982019-11-17 02:58:00 +0100402 disable_p2p();
403
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100404 pei_data.boot_mode = s3resume ? 2 : 0;
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100405 timestamp_add_now(TS_INITRAM_START);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100406 sdram_initialize(&pei_data);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100407 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 /* Sanity check mrc_var location by verifying a known field */
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200410 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100411 if (mrc_var->tx_byte == pei_data.tx_byte_ptr) {
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200412 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100413 mrc_var->pool_base, mrc_var->pool_base + mrc_var->pool_used);
414
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200415 } else {
416 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
Felix Held2a29d452021-05-25 19:15:11 +0200417 hexdump(mrc_var, sizeof(*mrc_var));
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200418 }
419
Angel Pons7c49cb82020-03-16 23:17:32 +0100420 const int cbmem_was_initted = !cbmem_recovery(s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100421 if (!s3resume)
422 save_mrc_data(&pei_data);
423
424 if (s3resume && !cbmem_was_initted) {
425 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200426 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100427 }
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600428 setup_sdram_meminfo(&pei_data);
429}
430
Arthur Heymans0f89a112022-04-18 17:14:37 +0200431static void setup_sdram_meminfo(struct pei_data *pei_data)
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600432{
Arthur Heymans55f116a2022-03-24 01:18:02 +0100433 u32 addr_decode_ch[2];
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600434 struct memory_info *mem_info;
435 struct dimm_info *dimm;
436 int dimm_size;
437 int i;
438 int dimm_cnt = 0;
439
440 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
441 memset(mem_info, 0, sizeof(struct memory_info));
442
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600443 addr_decode_ch[0] = mchbar_read32(MAD_DIMM_CH0);
444 addr_decode_ch[1] = mchbar_read32(MAD_DIMM_CH1);
445
446 const int refclk = mchbar_read32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
447 const int ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100;
448
449 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
450 u32 ch_conf = addr_decode_ch[i];
451
452 /* DIMM-A */
453 dimm_size = ((ch_conf >> 0) & 0xff) * 256;
454 if (dimm_size) {
455 dimm = &mem_info->dimm[dimm_cnt];
456 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100457 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600458 dimm->ddr_frequency = ddr_frequency;
459 dimm->rank_per_dimm = 1 + ((ch_conf >> 17) & 1);
460 dimm->channel_num = i;
461 dimm->dimm_num = 0;
462 dimm->bank_locator = i * 2;
463 memcpy(dimm->serial, /* bytes 122-125 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100464 &pei_data->spd_data[0][SPD_DIMM_SERIAL_NUM],
465 sizeof(uint8_t) * SPD_DIMM_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600466 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100467 &pei_data->spd_data[0][SPD_DIMM_PART_NUM],
468 sizeof(uint8_t) * SPD_DIMM_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600469 dimm->mod_id = /* bytes 117/118 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100470 (pei_data->spd_data[0][SPD_DIMM_MOD_ID2] << 8) |
471 (pei_data->spd_data[0][SPD_DIMM_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100472 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100473 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600474 dimm_cnt++;
475 }
476 /* DIMM-B */
477 dimm_size = ((ch_conf >> 8) & 0xff) * 256;
478 if (dimm_size) {
479 dimm = &mem_info->dimm[dimm_cnt];
480 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100481 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600482 dimm->ddr_frequency = ddr_frequency;
483 dimm->rank_per_dimm = 1 + ((ch_conf >> 18) & 1);
484 dimm->channel_num = i;
485 dimm->dimm_num = 1;
486 dimm->bank_locator = i * 2;
487 memcpy(dimm->serial, /* bytes 122-125 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100488 &pei_data->spd_data[0][SPD_DIMM_SERIAL_NUM],
489 sizeof(uint8_t) * SPD_DIMM_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600490 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100491 &pei_data->spd_data[0][SPD_DIMM_PART_NUM],
492 sizeof(uint8_t) * SPD_DIMM_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600493 dimm->mod_id = /* bytes 117/118 */
Elyes HAOUAS921b99e2022-01-26 08:01:08 +0100494 (pei_data->spd_data[0][SPD_DIMM_MOD_ID2] << 8) |
495 (pei_data->spd_data[0][SPD_DIMM_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100496 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100497 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600498 dimm_cnt++;
499 }
500 }
501 mem_info->dimm_cnt = dimm_cnt;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100502}