blob: b9279f3c7c5db0aed14f30bf673e293392571eec [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>
Julius Wernerde371092024-01-30 16:51:05 -080014#include <commonlib/bsd/ipchksum.h>
Keith Hui317a9822024-05-16 19:02:33 -040015#include <cpu/intel/model_206ax/model_206ax.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070016#include <pc80/mc146818rtc.h>
17#include <device/pci_def.h>
Kyösti Mälkkib697c902019-01-30 08:19:49 +020018#include <lib.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010019#include <mrc_cache.h>
Elyes HAOUASa233eb42022-01-26 07:51:28 +010020#include <spd.h>
Elyes HAOUAS62b23c12022-01-26 07:43:51 +010021#include <smbios.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020022#include <stddef.h>
23#include <stdint.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010024#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070025#include "raminit.h"
26#include "pei_data.h"
27#include "sandybridge.h"
Patrick Rudolph5709e032019-03-25 10:12:14 +010028#include "chip.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020029#include <security/vboot/vboot_common.h>
Patrick Georgi27fbbcf2019-04-23 12:33:23 +020030#include <southbridge/intel/bd82x6x/pch.h>
Matt DeVillierff1ef8d2016-12-24 15:36:24 -060031#include <memory_info.h>
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010032#include <mode_switch.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070033
34/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020035#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070036
37/*
38 * MRC scrambler seed offsets should be reserved in
39 * mainboard cmos.layout and not covered by checksum.
40 */
Julius Wernercd49cce2019-03-05 16:53:33 -080041#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070042#include "option_table.h"
Angel Pons7c49cb82020-03-16 23:17:32 +010043#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
44#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070045#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
46#else
47#define CMOS_OFFSET_MRC_SEED 152
48#define CMOS_OFFSET_MRC_SEED_S3 156
49#define CMOS_OFFSET_MRC_SEED_CHK 160
50#endif
51
Arthur Heymans7539b8c2017-12-24 10:42:57 +010052#define MRC_CACHE_VERSION 0
53
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010054/* Assembly functions: */
55void mrc_wrapper(void *func_ptr, uint32_t arg1);
Patrick Rudolph1d718de2023-12-28 19:39:11 +010056void __prot2lm_do_putchar(uint8_t byte);
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010057
Arthur Heymans0f89a112022-04-18 17:14:37 +020058static void save_mrc_data(struct pei_data *pei_data)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070059{
60 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070061
62 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010063 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
64 (void *)(uintptr_t)pei_data->mrc_output_ptr,
65 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070066
67 /* Save the MRC seed values to CMOS */
Kyösti Mälkki28791072020-01-04 12:58:53 +020068 cmos_write32(pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070069 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
70 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
71
Kyösti Mälkki28791072020-01-04 12:58:53 +020072 cmos_write32(pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070073 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
74 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
75
76 /* Save a simple checksum of the seed values */
Julius Wernerde371092024-01-30 16:51:05 -080077 c1 = ipchksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
78 c2 = ipchksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
79 checksum = ipchksum_add(sizeof(u32), c1, c2);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070080
Angel Pons7c49cb82020-03-16 23:17:32 +010081 cmos_write((checksum >> 0) & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
82 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK + 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070083}
84
85static void prepare_mrc_cache(struct pei_data *pei_data)
86{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070087 u16 c1, c2, checksum, seed_checksum;
Shelley Chenad9cd682020-07-23 16:10:52 -070088 size_t mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070089
Angel Pons7c49cb82020-03-16 23:17:32 +010090 /* Preset just in case there is an error */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +010091 pei_data->mrc_input_ptr = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070092 pei_data->mrc_input_len = 0;
93
94 /* Read scrambler seeds from CMOS */
95 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
96 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
97 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
98
99 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
100 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
101 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
102
103 /* Compute seed checksum and compare */
Julius Wernerde371092024-01-30 16:51:05 -0800104 c1 = ipchksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
105 c2 = ipchksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
106 checksum = ipchksum_add(sizeof(u32), c1, c2);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700107
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
109 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700110
111 if (checksum != seed_checksum) {
112 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
113 pei_data->scrambler_seed = 0;
114 pei_data->scrambler_seed_s3 = 0;
115 return;
116 }
117
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100118 pei_data->mrc_input_ptr = (uintptr_t)mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
Shelley Chenad9cd682020-07-23 16:10:52 -0700119 MRC_CACHE_VERSION,
120 &mrc_size);
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100121 if (!pei_data->mrc_input_ptr) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100122 /* Error message printed in find_current_mrc_cache */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700123 return;
124 }
125
Shelley Chenad9cd682020-07-23 16:10:52 -0700126 pei_data->mrc_input_len = mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700127
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100128 printk(BIOS_DEBUG, "%s: at 0x%x, size %zx\n", __func__,
129 pei_data->mrc_input_ptr, mrc_size);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700130}
131
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700132/**
133 * Find PEI executable in coreboot filesystem and execute it.
134 *
135 * @param pei_data: configuration data for UEFI PEI reference code
136 */
Arthur Heymans0f89a112022-04-18 17:14:37 +0200137static void sdram_initialize(struct pei_data *pei_data)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700138{
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100139 int (*entry)(struct pei_data *pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700140
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700141 /* Wait for ME to be ready */
142 intel_early_me_init();
143 intel_early_me_uma_size();
144
145 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
146
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700147 /*
Shelley Chen6615c6e2020-10-27 15:58:31 -0700148 * Always pass in mrc_cache data. The driver will determine
149 * whether to use the data or not.
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700150 */
Shelley Chen6615c6e2020-10-27 15:58:31 -0700151 prepare_mrc_cache(pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700152
153 /* If MRC data is not found we cannot continue S3 resume. */
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100154 if (pei_data->boot_mode == 2 && !pei_data->mrc_input_ptr) {
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100155 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUASc0567292019-04-28 17:57:47 +0200156 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700157 }
158
Patrick Rudolph1d718de2023-12-28 19:39:11 +0100159 /*
160 * Pass console handler in pei_data. On x86_64 provide a wrapper around
161 * do_putchar that switches to long mode before calling do_putchar.
162 */
163 if (ENV_X86_64)
164 pei_data->tx_byte_ptr = (uintptr_t)__prot2lm_do_putchar;
165 else
166 pei_data->tx_byte_ptr = (uintptr_t)do_putchar;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700167
168 /* Locate and call UEFI System Agent binary. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800169 entry = cbfs_map("mrc.bin", NULL);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700170 if (entry) {
171 int rv;
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100172 rv = protected_mode_call_2arg(mrc_wrapper, (uintptr_t)entry, (uintptr_t)pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700173 if (rv) {
174 switch (rv) {
175 case -1:
176 printk(BIOS_ERR, "PEI version mismatch.\n");
177 break;
178 case -2:
179 printk(BIOS_ERR, "Invalid memory frequency.\n");
180 break;
181 default:
182 printk(BIOS_ERR, "MRC returned %x.\n", rv);
183 }
lilacious40cb3fe2023-06-21 23:24:14 +0200184 die_with_post_code(POSTCODE_INVALID_VENDOR_BINARY,
Keith Shortbb41aba2019-05-16 14:07:43 -0600185 "Nonzero MRC return value.\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700186 }
187 } else {
188 die("UEFI PEI System Agent not found.\n");
189 }
190
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700191 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800192 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200193 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700194
Angel Pons9f3bc3712020-10-13 23:57:10 +0200195 /* Print the MRC version after executing the UEFI PEI stage */
Angel Pons66780a02021-03-26 13:33:22 +0100196 u32 version = mchbar_read32(MRC_REVISION);
Angel Ponsc1328a62021-06-14 12:43:11 +0200197 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100198 (version >> 24) & 0xff, (version >> 16) & 0xff,
199 (version >> 8) & 0xff, (version >> 0) & 0xff);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700200
Angel Pons7c49cb82020-03-16 23:17:32 +0100201 /*
202 * Send ME init done for SandyBridge here.
203 * This is done inside the SystemAgent binary on IvyBridge.
204 */
205 if (BASE_REV_SNB == (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700206 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
207 else
208 intel_early_me_status();
209
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700210 report_memory_config();
211}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100212
Angel Pons7c49cb82020-03-16 23:17:32 +0100213/*
214 * These are the location and structure of MRC_VAR data in CAR.
215 * The CAR region looks like this:
216 * +------------------+ -> DCACHE_RAM_BASE
217 * | |
218 * | |
219 * | COREBOOT STACK |
220 * | |
221 * | |
222 * +------------------+ -> DCACHE_RAM_BASE + DCACHE_RAM_SIZE
223 * | |
224 * | MRC HEAP |
225 * | size = 0x5000 |
226 * | |
227 * +------------------+
228 * | |
229 * | MRC VAR |
230 * | size = 0x4000 |
231 * | |
232 * +------------------+ -> DACHE_RAM_BASE + DACHE_RAM_SIZE
233 * + DCACHE_RAM_MRC_VAR_SIZE
Arthur Heymans01c83a22019-06-05 13:36:55 +0200234 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100235#define DCACHE_RAM_MRC_VAR_BASE (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE \
236 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 0x4000)
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200237
238struct mrc_var_data {
239 u32 acpi_timer_flag;
240 u32 pool_used;
241 u32 pool_base;
242 u32 tx_byte;
243 u32 reserved[4];
244} __packed;
245
Keith Hui317a9822024-05-16 19:02:33 -0400246static bool do_pcie_init(void)
Patrick Rudolph5709e032019-03-25 10:12:14 +0100247{
Keith Hui317a9822024-05-16 19:02:33 -0400248 if (IS_IVY_CPU(cpu_get_cpuid())) {
249 return is_devfn_enabled(PCI_DEVFN(1, 0));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100250 } else {
Keith Hui317a9822024-05-16 19:02:33 -0400251 return false;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100252 }
253}
254
255static void southbridge_fill_pei_data(struct pei_data *pei_data)
256{
Keith Hui317a9822024-05-16 19:02:33 -0400257 /* This will move to southbridge later. */
Patrick Rudolph5709e032019-03-25 10:12:14 +0100258}
259
260static void devicetree_fill_pei_data(struct pei_data *pei_data)
261{
Keith Hui1e9601c2023-07-15 12:08:51 -0400262 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
Patrick Rudolph5709e032019-03-25 10:12:14 +0100263
264 switch (cfg->max_mem_clock_mhz) {
265 /* MRC only supports fixed numbers of frequencies */
266 default:
267 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
Arthur Heymansfff20212021-03-15 14:56:16 +0100268 __fallthrough;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100269 case 400:
270 pei_data->max_ddr3_freq = 800;
271 break;
272 case 533:
273 pei_data->max_ddr3_freq = 1066;
274 break;
275 case 666:
276 pei_data->max_ddr3_freq = 1333;
277 break;
278 case 800:
279 pei_data->max_ddr3_freq = 1600;
280 break;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100281 }
282
Keith Hui1e9601c2023-07-15 12:08:51 -0400283 /*
284 * SPD addresses are listed in devicetree as actual addresses,
285 * and for MRC need to be shifted left so bit 0 is always zero.
286 */
287 if (!CONFIG(HAVE_SPD_IN_CBFS)) {
288 for (unsigned int i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
289 pei_data->spd_addresses[i] = cfg->spd_addresses[i] << 1;
290 }
291 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100292 memcpy(pei_data->ts_addresses, cfg->ts_addresses, sizeof(pei_data->ts_addresses));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100293
Patrick Rudolph5709e032019-03-25 10:12:14 +0100294 memcpy(pei_data->usb_port_config, cfg->usb_port_config,
295 sizeof(pei_data->usb_port_config));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100296}
297
Keith Hui1e9601c2023-07-15 12:08:51 -0400298static void spd_fill_pei_data(struct pei_data *pei_data)
299{
300 struct spd_info spdi = {0};
Jeremy Compostellacaa0c0e2023-11-16 08:48:23 -0800301 unsigned int i, have_memory_down = 0;
Keith Hui1e9601c2023-07-15 12:08:51 -0400302
303 mb_get_spd_map(&spdi);
304
305 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
306 if (spdi.addresses[i] == SPD_MEMORY_DOWN) {
307 pei_data->spd_addresses[i] = 0;
308 have_memory_down = 1;
309 } else {
310 /* MRC expects left-aligned SMBus addresses. */
311 pei_data->spd_addresses[i] = spdi.addresses[i] << 1;
312 }
313 }
314 /* Copy SPD data from CBFS for on-board memory */
315 if (have_memory_down) {
316 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
317
318 size_t spd_file_len;
319 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
320
321 if (!spd_file)
322 die("SPD data %s!", "not found");
323
324 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
325 die("SPD data %s!", "incomplete");
326
327 /* MRC only uses index 0... */
328 memcpy(pei_data->spd_data[0], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
329
330 /* but coreboot uses the other indices */
331 for (i = 1; i < ARRAY_SIZE(spdi.addresses); i++) {
332 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
333 memcpy(pei_data->spd_data[i], pei_data->spd_data[0], SPD_SIZE_MAX_DDR3);
334 }
335 }
336}
337
Nico Huber47bf4982019-11-17 02:58:00 +0100338static void disable_p2p(void)
339{
Angel Pons7c49cb82020-03-16 23:17:32 +0100340 /* Disable PCI-to-PCI bridge early to prevent probing by MRC */
Nico Huber47bf4982019-11-17 02:58:00 +0100341 const struct device *const p2p = pcidev_on_root(0x1e, 0);
342 if (p2p && p2p->enabled)
343 return;
344
345 RCBA32(FD) |= PCH_DISABLE_P2P;
346}
347
Arthur Heymans0f89a112022-04-18 17:14:37 +0200348static void setup_sdram_meminfo(struct pei_data *pei_data);
349
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100350void perform_raminit(int s3resume)
351{
Keith Hui317a9822024-05-16 19:02:33 -0400352 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
353 struct pei_data pei_data = {
354 .pei_version = PEI_VERSION,
355 .mchbar = CONFIG_FIXED_MCHBAR_MMIO_BASE,
356 .dmibar = CONFIG_FIXED_DMIBAR_MMIO_BASE,
357 .epbar = CONFIG_FIXED_EPBAR_MMIO_BASE,
358 .pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS,
359 .smbusbar = CONFIG_FIXED_SMBUS_IO_BASE,
360 .wdbbar = 0x4000000,
361 .wdbsize = 0x1000,
362 .hpet_address = HPET_BASE_ADDRESS,
363 .rcba = (uintptr_t)DEFAULT_RCBA,
364 .pmbase = DEFAULT_PMBASE,
365 .gpiobase = DEFAULT_GPIOBASE,
366 .thermalbase = 0xfed08000,
367 .tseg_size = CONFIG_SMM_TSEG_SIZE,
368 .system_type = !(get_platform_type() == PLATFORM_MOBILE),
369 .pcie_init = do_pcie_init(),
370 .gbe_enable = is_devfn_enabled(PCI_DEVFN(0x19, 0)),
371 .boot_mode = s3resume ? BOOT_PATH_RESUME : BOOT_PATH_NORMAL,
372 .ec_present = cfg->ec_present,
373 .ddr3lv_support = cfg->ddr3lv_support,
374 .nmode = cfg->nmode,
375 .ddr_refresh_rate_config = cfg->ddr_refresh_rate_config,
376 .usb3.mode = cfg->usb3.mode,
377 .usb3.hs_port_switch_mask = cfg->usb3.hs_port_switch_mask,
378 .usb3.preboot_support = cfg->usb3.preboot_support,
379 .usb3.xhci_streams = cfg->usb3.xhci_streams,
380 };
381
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200382 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100383
384 /* Prepare USB controller early in S3 resume */
Keith Huic5d6af42023-03-20 02:03:47 -0400385 if (s3resume)
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100386 enable_usb_bar();
387
Patrick Rudolph5709e032019-03-25 10:12:14 +0100388 southbridge_fill_pei_data(&pei_data);
389 devicetree_fill_pei_data(&pei_data);
Keith Hui1e9601c2023-07-15 12:08:51 -0400390 if (CONFIG(HAVE_SPD_IN_CBFS))
391 spd_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100392 mainboard_fill_pei_data(&pei_data);
393
394 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200395
Patrick Rudolph5709e032019-03-25 10:12:14 +0100396 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
397 pei_data.dimm_channel0_disabled =
398 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
399 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
400
401 pei_data.dimm_channel1_disabled =
402 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
403 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
404
Nico Huber47bf4982019-11-17 02:58:00 +0100405 disable_p2p();
406
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100407 timestamp_add_now(TS_INITRAM_START);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100408 sdram_initialize(&pei_data);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100409 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200410
Angel Pons7c49cb82020-03-16 23:17:32 +0100411 /* Sanity check mrc_var location by verifying a known field */
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200412 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100413 if (mrc_var->tx_byte == pei_data.tx_byte_ptr) {
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200414 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100415 mrc_var->pool_base, mrc_var->pool_base + mrc_var->pool_used);
416
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200417 } else {
418 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
Felix Held2a29d452021-05-25 19:15:11 +0200419 hexdump(mrc_var, sizeof(*mrc_var));
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200420 }
421
Angel Pons7c49cb82020-03-16 23:17:32 +0100422 const int cbmem_was_initted = !cbmem_recovery(s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100423 if (!s3resume)
424 save_mrc_data(&pei_data);
425
426 if (s3resume && !cbmem_was_initted) {
427 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200428 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100429 }
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600430 setup_sdram_meminfo(&pei_data);
431}
432
Arthur Heymans0f89a112022-04-18 17:14:37 +0200433static void setup_sdram_meminfo(struct pei_data *pei_data)
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600434{
Arthur Heymans55f116a2022-03-24 01:18:02 +0100435 u32 addr_decode_ch[2];
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600436 struct memory_info *mem_info;
437 struct dimm_info *dimm;
438 int dimm_size;
439 int i;
440 int dimm_cnt = 0;
441
442 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
443 memset(mem_info, 0, sizeof(struct memory_info));
444
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600445 addr_decode_ch[0] = mchbar_read32(MAD_DIMM_CH0);
446 addr_decode_ch[1] = mchbar_read32(MAD_DIMM_CH1);
447
448 const int refclk = mchbar_read32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
449 const int ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100;
450
451 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
452 u32 ch_conf = addr_decode_ch[i];
453
454 /* DIMM-A */
455 dimm_size = ((ch_conf >> 0) & 0xff) * 256;
456 if (dimm_size) {
457 dimm = &mem_info->dimm[dimm_cnt];
458 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100459 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600460 dimm->ddr_frequency = ddr_frequency;
461 dimm->rank_per_dimm = 1 + ((ch_conf >> 17) & 1);
462 dimm->channel_num = i;
463 dimm->dimm_num = 0;
464 dimm->bank_locator = i * 2;
465 memcpy(dimm->serial, /* bytes 122-125 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200466 &pei_data->spd_data[0][SPD_DDR3_SERIAL_NUM],
467 sizeof(uint8_t) * SPD_DDR3_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600468 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200469 &pei_data->spd_data[0][SPD_DDR3_PART_NUM],
470 sizeof(uint8_t) * SPD_DDR3_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600471 dimm->mod_id = /* bytes 117/118 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200472 (pei_data->spd_data[0][SPD_DDR3_MOD_ID2] << 8) |
473 (pei_data->spd_data[0][SPD_DDR3_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100474 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100475 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600476 dimm_cnt++;
477 }
478 /* DIMM-B */
479 dimm_size = ((ch_conf >> 8) & 0xff) * 256;
480 if (dimm_size) {
481 dimm = &mem_info->dimm[dimm_cnt];
482 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100483 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600484 dimm->ddr_frequency = ddr_frequency;
485 dimm->rank_per_dimm = 1 + ((ch_conf >> 18) & 1);
486 dimm->channel_num = i;
487 dimm->dimm_num = 1;
488 dimm->bank_locator = i * 2;
489 memcpy(dimm->serial, /* bytes 122-125 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200490 &pei_data->spd_data[0][SPD_DDR3_SERIAL_NUM],
491 sizeof(uint8_t) * SPD_DDR3_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600492 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200493 &pei_data->spd_data[0][SPD_DDR3_PART_NUM],
494 sizeof(uint8_t) * SPD_DDR3_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600495 dimm->mod_id = /* bytes 117/118 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200496 (pei_data->spd_data[0][SPD_DDR3_MOD_ID2] << 8) |
497 (pei_data->spd_data[0][SPD_DDR3_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100498 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100499 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600500 dimm_cnt++;
501 }
502 }
503 mem_info->dimm_cnt = dimm_cnt;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100504}