blob: 98b302850cddda60dab61787d258c8885bfea37e [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
Patrick Rudolph5709e032019-03-25 10:12:14 +0100255static void devicetree_fill_pei_data(struct pei_data *pei_data)
256{
Keith Hui1e9601c2023-07-15 12:08:51 -0400257 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
Patrick Rudolph5709e032019-03-25 10:12:14 +0100258
259 switch (cfg->max_mem_clock_mhz) {
260 /* MRC only supports fixed numbers of frequencies */
261 default:
262 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
Arthur Heymansfff20212021-03-15 14:56:16 +0100263 __fallthrough;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100264 case 400:
265 pei_data->max_ddr3_freq = 800;
266 break;
267 case 533:
268 pei_data->max_ddr3_freq = 1066;
269 break;
270 case 666:
271 pei_data->max_ddr3_freq = 1333;
272 break;
273 case 800:
274 pei_data->max_ddr3_freq = 1600;
275 break;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100276 }
277
Keith Hui1e9601c2023-07-15 12:08:51 -0400278 /*
279 * SPD addresses are listed in devicetree as actual addresses,
280 * and for MRC need to be shifted left so bit 0 is always zero.
281 */
282 if (!CONFIG(HAVE_SPD_IN_CBFS)) {
283 for (unsigned int i = 0; i < ARRAY_SIZE(cfg->spd_addresses); i++) {
284 pei_data->spd_addresses[i] = cfg->spd_addresses[i] << 1;
285 }
286 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100287 memcpy(pei_data->ts_addresses, cfg->ts_addresses, sizeof(pei_data->ts_addresses));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100288}
289
Keith Hui1e9601c2023-07-15 12:08:51 -0400290static void spd_fill_pei_data(struct pei_data *pei_data)
291{
292 struct spd_info spdi = {0};
Jeremy Compostellacaa0c0e2023-11-16 08:48:23 -0800293 unsigned int i, have_memory_down = 0;
Keith Hui1e9601c2023-07-15 12:08:51 -0400294
295 mb_get_spd_map(&spdi);
296
297 for (i = 0; i < ARRAY_SIZE(spdi.addresses); i++) {
298 if (spdi.addresses[i] == SPD_MEMORY_DOWN) {
299 pei_data->spd_addresses[i] = 0;
300 have_memory_down = 1;
301 } else {
302 /* MRC expects left-aligned SMBus addresses. */
303 pei_data->spd_addresses[i] = spdi.addresses[i] << 1;
304 }
305 }
306 /* Copy SPD data from CBFS for on-board memory */
307 if (have_memory_down) {
308 printk(BIOS_DEBUG, "SPD index %d\n", spdi.spd_index);
309
310 size_t spd_file_len;
311 uint8_t *spd_file = cbfs_map("spd.bin", &spd_file_len);
312
313 if (!spd_file)
314 die("SPD data %s!", "not found");
315
316 if (spd_file_len < ((spdi.spd_index + 1) * SPD_SIZE_MAX_DDR3))
317 die("SPD data %s!", "incomplete");
318
319 /* MRC only uses index 0... */
320 memcpy(pei_data->spd_data[0], spd_file + (spdi.spd_index * SPD_SIZE_MAX_DDR3), SPD_SIZE_MAX_DDR3);
321
322 /* but coreboot uses the other indices */
323 for (i = 1; i < ARRAY_SIZE(spdi.addresses); i++) {
324 if (spdi.addresses[i] == SPD_MEMORY_DOWN)
325 memcpy(pei_data->spd_data[i], pei_data->spd_data[0], SPD_SIZE_MAX_DDR3);
326 }
327 }
328}
329
Nico Huber47bf4982019-11-17 02:58:00 +0100330static void disable_p2p(void)
331{
Angel Pons7c49cb82020-03-16 23:17:32 +0100332 /* Disable PCI-to-PCI bridge early to prevent probing by MRC */
Nico Huber47bf4982019-11-17 02:58:00 +0100333 const struct device *const p2p = pcidev_on_root(0x1e, 0);
334 if (p2p && p2p->enabled)
335 return;
336
337 RCBA32(FD) |= PCH_DISABLE_P2P;
338}
339
Arthur Heymans0f89a112022-04-18 17:14:37 +0200340static void setup_sdram_meminfo(struct pei_data *pei_data);
341
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100342void perform_raminit(int s3resume)
343{
Keith Hui317a9822024-05-16 19:02:33 -0400344 const struct northbridge_intel_sandybridge_config *cfg = config_of_soc();
345 struct pei_data pei_data = {
346 .pei_version = PEI_VERSION,
347 .mchbar = CONFIG_FIXED_MCHBAR_MMIO_BASE,
348 .dmibar = CONFIG_FIXED_DMIBAR_MMIO_BASE,
349 .epbar = CONFIG_FIXED_EPBAR_MMIO_BASE,
350 .pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS,
351 .smbusbar = CONFIG_FIXED_SMBUS_IO_BASE,
352 .wdbbar = 0x4000000,
353 .wdbsize = 0x1000,
354 .hpet_address = HPET_BASE_ADDRESS,
355 .rcba = (uintptr_t)DEFAULT_RCBA,
356 .pmbase = DEFAULT_PMBASE,
357 .gpiobase = DEFAULT_GPIOBASE,
358 .thermalbase = 0xfed08000,
359 .tseg_size = CONFIG_SMM_TSEG_SIZE,
360 .system_type = !(get_platform_type() == PLATFORM_MOBILE),
361 .pcie_init = do_pcie_init(),
362 .gbe_enable = is_devfn_enabled(PCI_DEVFN(0x19, 0)),
363 .boot_mode = s3resume ? BOOT_PATH_RESUME : BOOT_PATH_NORMAL,
364 .ec_present = cfg->ec_present,
365 .ddr3lv_support = cfg->ddr3lv_support,
366 .nmode = cfg->nmode,
367 .ddr_refresh_rate_config = cfg->ddr_refresh_rate_config,
368 .usb3.mode = cfg->usb3.mode,
Keith Huiee126342024-02-05 18:49:46 -0500369 /* .usb3.hs_port_switch_mask = native config->xhci_switchable_ports */
Keith Hui317a9822024-05-16 19:02:33 -0400370 .usb3.preboot_support = cfg->usb3.preboot_support,
371 .usb3.xhci_streams = cfg->usb3.xhci_streams,
372 };
373
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200374 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100375
376 /* Prepare USB controller early in S3 resume */
Keith Huic5d6af42023-03-20 02:03:47 -0400377 if (s3resume)
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100378 enable_usb_bar();
379
Patrick Rudolph5709e032019-03-25 10:12:14 +0100380 southbridge_fill_pei_data(&pei_data);
381 devicetree_fill_pei_data(&pei_data);
Keith Hui1e9601c2023-07-15 12:08:51 -0400382 if (CONFIG(HAVE_SPD_IN_CBFS))
383 spd_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100384 mainboard_fill_pei_data(&pei_data);
385
386 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200387
Patrick Rudolph5709e032019-03-25 10:12:14 +0100388 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
389 pei_data.dimm_channel0_disabled =
390 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
391 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
392
393 pei_data.dimm_channel1_disabled =
394 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
395 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
396
Nico Huber47bf4982019-11-17 02:58:00 +0100397 disable_p2p();
398
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100399 timestamp_add_now(TS_INITRAM_START);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100400 sdram_initialize(&pei_data);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100401 timestamp_add_now(TS_INITRAM_END);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200402
Angel Pons7c49cb82020-03-16 23:17:32 +0100403 /* Sanity check mrc_var location by verifying a known field */
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200404 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
Patrick Rudolphb14b96d2023-12-27 10:59:25 +0100405 if (mrc_var->tx_byte == pei_data.tx_byte_ptr) {
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200406 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100407 mrc_var->pool_base, mrc_var->pool_base + mrc_var->pool_used);
408
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200409 } else {
410 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
Felix Held2a29d452021-05-25 19:15:11 +0200411 hexdump(mrc_var, sizeof(*mrc_var));
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200412 }
413
Angel Pons7c49cb82020-03-16 23:17:32 +0100414 const int cbmem_was_initted = !cbmem_recovery(s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100415 if (!s3resume)
416 save_mrc_data(&pei_data);
417
418 if (s3resume && !cbmem_was_initted) {
419 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200420 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100421 }
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600422 setup_sdram_meminfo(&pei_data);
423}
424
Arthur Heymans0f89a112022-04-18 17:14:37 +0200425static void setup_sdram_meminfo(struct pei_data *pei_data)
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600426{
Arthur Heymans55f116a2022-03-24 01:18:02 +0100427 u32 addr_decode_ch[2];
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600428 struct memory_info *mem_info;
429 struct dimm_info *dimm;
430 int dimm_size;
431 int i;
432 int dimm_cnt = 0;
433
434 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
435 memset(mem_info, 0, sizeof(struct memory_info));
436
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600437 addr_decode_ch[0] = mchbar_read32(MAD_DIMM_CH0);
438 addr_decode_ch[1] = mchbar_read32(MAD_DIMM_CH1);
439
440 const int refclk = mchbar_read32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
441 const int ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100;
442
443 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
444 u32 ch_conf = addr_decode_ch[i];
445
446 /* DIMM-A */
447 dimm_size = ((ch_conf >> 0) & 0xff) * 256;
448 if (dimm_size) {
449 dimm = &mem_info->dimm[dimm_cnt];
450 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100451 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600452 dimm->ddr_frequency = ddr_frequency;
453 dimm->rank_per_dimm = 1 + ((ch_conf >> 17) & 1);
454 dimm->channel_num = i;
455 dimm->dimm_num = 0;
456 dimm->bank_locator = i * 2;
457 memcpy(dimm->serial, /* bytes 122-125 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200458 &pei_data->spd_data[0][SPD_DDR3_SERIAL_NUM],
459 sizeof(uint8_t) * SPD_DDR3_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600460 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200461 &pei_data->spd_data[0][SPD_DDR3_PART_NUM],
462 sizeof(uint8_t) * SPD_DDR3_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600463 dimm->mod_id = /* bytes 117/118 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200464 (pei_data->spd_data[0][SPD_DDR3_MOD_ID2] << 8) |
465 (pei_data->spd_data[0][SPD_DDR3_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100466 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100467 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600468 dimm_cnt++;
469 }
470 /* DIMM-B */
471 dimm_size = ((ch_conf >> 8) & 0xff) * 256;
472 if (dimm_size) {
473 dimm = &mem_info->dimm[dimm_cnt];
474 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100475 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600476 dimm->ddr_frequency = ddr_frequency;
477 dimm->rank_per_dimm = 1 + ((ch_conf >> 18) & 1);
478 dimm->channel_num = i;
479 dimm->dimm_num = 1;
480 dimm->bank_locator = i * 2;
481 memcpy(dimm->serial, /* bytes 122-125 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200482 &pei_data->spd_data[0][SPD_DDR3_SERIAL_NUM],
483 sizeof(uint8_t) * SPD_DDR3_SERIAL_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600484 memcpy(dimm->module_part_number, /* bytes 128-145 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200485 &pei_data->spd_data[0][SPD_DDR3_PART_NUM],
486 sizeof(uint8_t) * SPD_DDR3_PART_LEN);
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600487 dimm->mod_id = /* bytes 117/118 */
Elyes Haouas8bcd8212024-05-06 11:48:41 +0200488 (pei_data->spd_data[0][SPD_DDR3_MOD_ID2] << 8) |
489 (pei_data->spd_data[0][SPD_DDR3_MOD_ID1] & 0xFF);
Elyes Haouasf82e68c2022-12-28 12:33:58 +0100490 dimm->mod_type = SPD_DDR3_DIMM_TYPE_SO_DIMM;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100491 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600492 dimm_cnt++;
493 }
494 }
495 mem_info->dimm_cnt = dimm_cnt;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100496}