blob: 0a7d1921d84fc558d52ea45f2ed4249c59dc99bf [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
3#include <console/console.h>
4#include <console/usb.h>
Elyes HAOUASc0567292019-04-28 17:57:47 +02005#include <cf9_reset.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07006#include <string.h>
Nico Huber47bf4982019-11-17 02:58:00 +01007#include <device/device.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Patrick Rudolph5709e032019-03-25 10:12:14 +01009#include <arch/cpu.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070010#include <cbmem.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070011#include <cbfs.h>
12#include <ip_checksum.h>
13#include <pc80/mc146818rtc.h>
14#include <device/pci_def.h>
Kyösti Mälkkib697c902019-01-30 08:19:49 +020015#include <lib.h>
Arthur Heymans7539b8c2017-12-24 10:42:57 +010016#include <mrc_cache.h>
Elyes HAOUAS62b23c12022-01-26 07:43:51 +010017#include <smbios.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020018#include <stddef.h>
19#include <stdint.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010020#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070021#include "raminit.h"
22#include "pei_data.h"
23#include "sandybridge.h"
Patrick Rudolph5709e032019-03-25 10:12:14 +010024#include "chip.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020025#include <security/vboot/vboot_common.h>
Patrick Georgi27fbbcf2019-04-23 12:33:23 +020026#include <southbridge/intel/bd82x6x/pch.h>
Matt DeVillierff1ef8d2016-12-24 15:36:24 -060027#include <memory_info.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070028
29/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020030#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031
32/*
33 * MRC scrambler seed offsets should be reserved in
34 * mainboard cmos.layout and not covered by checksum.
35 */
Julius Wernercd49cce2019-03-05 16:53:33 -080036#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070037#include "option_table.h"
Angel Pons7c49cb82020-03-16 23:17:32 +010038#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
39#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070040#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
41#else
42#define CMOS_OFFSET_MRC_SEED 152
43#define CMOS_OFFSET_MRC_SEED_S3 156
44#define CMOS_OFFSET_MRC_SEED_CHK 160
45#endif
46
Arthur Heymans7539b8c2017-12-24 10:42:57 +010047#define MRC_CACHE_VERSION 0
48
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070049void save_mrc_data(struct pei_data *pei_data)
50{
51 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070052
53 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +010054 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output,
Arthur Heymans7539b8c2017-12-24 10:42:57 +010055 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070056
57 /* Save the MRC seed values to CMOS */
Kyösti Mälkki28791072020-01-04 12:58:53 +020058 cmos_write32(pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070059 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
60 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
61
Kyösti Mälkki28791072020-01-04 12:58:53 +020062 cmos_write32(pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070063 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
64 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
65
66 /* Save a simple checksum of the seed values */
Angel Pons7c49cb82020-03-16 23:17:32 +010067 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
68 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070069 checksum = add_ip_checksums(sizeof(u32), c1, c2);
70
Angel Pons7c49cb82020-03-16 23:17:32 +010071 cmos_write((checksum >> 0) & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
72 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK + 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070073}
74
75static void prepare_mrc_cache(struct pei_data *pei_data)
76{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070077 u16 c1, c2, checksum, seed_checksum;
Shelley Chenad9cd682020-07-23 16:10:52 -070078 size_t mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070079
Angel Pons7c49cb82020-03-16 23:17:32 +010080 /* Preset just in case there is an error */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070081 pei_data->mrc_input = NULL;
82 pei_data->mrc_input_len = 0;
83
84 /* Read scrambler seeds from CMOS */
85 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
86 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
87 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
88
89 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
90 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
91 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
92
93 /* Compute seed checksum and compare */
Angel Pons7c49cb82020-03-16 23:17:32 +010094 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
95 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070096 checksum = add_ip_checksums(sizeof(u32), c1, c2);
97
Angel Pons7c49cb82020-03-16 23:17:32 +010098 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
99 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700100
101 if (checksum != seed_checksum) {
102 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
103 pei_data->scrambler_seed = 0;
104 pei_data->scrambler_seed_s3 = 0;
105 return;
106 }
107
Shelley Chenad9cd682020-07-23 16:10:52 -0700108 pei_data->mrc_input = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
109 MRC_CACHE_VERSION,
110 &mrc_size);
111 if (pei_data->mrc_input == NULL) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100112 /* Error message printed in find_current_mrc_cache */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700113 return;
114 }
115
Shelley Chenad9cd682020-07-23 16:10:52 -0700116 pei_data->mrc_input_len = mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700117
Shelley Chenad9cd682020-07-23 16:10:52 -0700118 printk(BIOS_DEBUG, "%s: at %p, size %zx\n", __func__,
119 pei_data->mrc_input, mrc_size);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700120}
121
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700122/**
123 * Find PEI executable in coreboot filesystem and execute it.
124 *
125 * @param pei_data: configuration data for UEFI PEI reference code
126 */
127void sdram_initialize(struct pei_data *pei_data)
128{
Angel Pons7c49cb82020-03-16 23:17:32 +0100129 int (*entry)(struct pei_data *pei_data) __attribute__((regparm(1)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700130
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700131 /* Wait for ME to be ready */
132 intel_early_me_init();
133 intel_early_me_uma_size();
134
135 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
136
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700137 /*
Shelley Chen6615c6e2020-10-27 15:58:31 -0700138 * Always pass in mrc_cache data. The driver will determine
139 * whether to use the data or not.
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700140 */
Shelley Chen6615c6e2020-10-27 15:58:31 -0700141 prepare_mrc_cache(pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700142
143 /* If MRC data is not found we cannot continue S3 resume. */
144 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100145 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUASc0567292019-04-28 17:57:47 +0200146 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700147 }
148
149 /* Pass console handler in pei_data */
150 pei_data->tx_byte = do_putchar;
151
152 /* Locate and call UEFI System Agent binary. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800153 entry = cbfs_map("mrc.bin", NULL);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700154 if (entry) {
155 int rv;
156 rv = entry (pei_data);
157 if (rv) {
158 switch (rv) {
159 case -1:
160 printk(BIOS_ERR, "PEI version mismatch.\n");
161 break;
162 case -2:
163 printk(BIOS_ERR, "Invalid memory frequency.\n");
164 break;
165 default:
166 printk(BIOS_ERR, "MRC returned %x.\n", rv);
167 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600168 die_with_post_code(POST_INVALID_VENDOR_BINARY,
169 "Nonzero MRC return value.\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700170 }
171 } else {
172 die("UEFI PEI System Agent not found.\n");
173 }
174
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700175 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800176 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200177 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700178
Angel Pons9f3bc3712020-10-13 23:57:10 +0200179 /* Print the MRC version after executing the UEFI PEI stage */
Angel Pons66780a02021-03-26 13:33:22 +0100180 u32 version = mchbar_read32(MRC_REVISION);
Angel Ponsc1328a62021-06-14 12:43:11 +0200181 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100182 (version >> 24) & 0xff, (version >> 16) & 0xff,
183 (version >> 8) & 0xff, (version >> 0) & 0xff);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700184
Angel Pons7c49cb82020-03-16 23:17:32 +0100185 /*
186 * Send ME init done for SandyBridge here.
187 * This is done inside the SystemAgent binary on IvyBridge.
188 */
189 if (BASE_REV_SNB == (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700190 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
191 else
192 intel_early_me_status();
193
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700194 report_memory_config();
195}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100196
Angel Pons7c49cb82020-03-16 23:17:32 +0100197/*
198 * These are the location and structure of MRC_VAR data in CAR.
199 * The CAR region looks like this:
200 * +------------------+ -> DCACHE_RAM_BASE
201 * | |
202 * | |
203 * | COREBOOT STACK |
204 * | |
205 * | |
206 * +------------------+ -> DCACHE_RAM_BASE + DCACHE_RAM_SIZE
207 * | |
208 * | MRC HEAP |
209 * | size = 0x5000 |
210 * | |
211 * +------------------+
212 * | |
213 * | MRC VAR |
214 * | size = 0x4000 |
215 * | |
216 * +------------------+ -> DACHE_RAM_BASE + DACHE_RAM_SIZE
217 * + DCACHE_RAM_MRC_VAR_SIZE
Arthur Heymans01c83a22019-06-05 13:36:55 +0200218 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100219#define DCACHE_RAM_MRC_VAR_BASE (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE \
220 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 0x4000)
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200221
222struct mrc_var_data {
223 u32 acpi_timer_flag;
224 u32 pool_used;
225 u32 pool_base;
226 u32 tx_byte;
227 u32 reserved[4];
228} __packed;
229
Patrick Rudolph5709e032019-03-25 10:12:14 +0100230static void northbridge_fill_pei_data(struct pei_data *pei_data)
231{
Angel Ponsd9e58dc2021-01-20 01:22:20 +0100232 pei_data->mchbar = CONFIG_FIXED_MCHBAR_MMIO_BASE;
233 pei_data->dmibar = CONFIG_FIXED_DMIBAR_MMIO_BASE;
234 pei_data->epbar = CONFIG_FIXED_EPBAR_MMIO_BASE;
Shelley Chen4e9bb332021-10-20 15:43:45 -0700235 pei_data->pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100236 pei_data->hpet_address = CONFIG_HPET_ADDRESS;
Angel Pons7c49cb82020-03-16 23:17:32 +0100237 pei_data->thermalbase = 0xfed08000;
238 pei_data->system_type = !(get_platform_type() == PLATFORM_MOBILE);
239 pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100240
241 if ((cpu_get_cpuid() & 0xffff0) == 0x306a0) {
242 const struct device *dev = pcidev_on_root(1, 0);
243 pei_data->pcie_init = dev && dev->enabled;
244 } else {
245 pei_data->pcie_init = 0;
246 }
247}
248
249static void southbridge_fill_pei_data(struct pei_data *pei_data)
250{
251 const struct device *dev = pcidev_on_root(0x19, 0);
252
Angel Ponsb21bffa2020-07-03 01:02:28 +0200253 pei_data->smbusbar = CONFIG_FIXED_SMBUS_IO_BASE;
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 pei_data->wdbbar = 0x04000000;
255 pei_data->wdbsize = 0x1000;
Angel Pons92717ff2020-09-14 16:22:22 +0200256 pei_data->rcba = (uintptr_t)DEFAULT_RCBA;
Angel Pons7c49cb82020-03-16 23:17:32 +0100257 pei_data->pmbase = DEFAULT_PMBASE;
258 pei_data->gpiobase = DEFAULT_GPIOBASE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100259 pei_data->gbe_enable = dev && dev->enabled;
260}
261
262static void devicetree_fill_pei_data(struct pei_data *pei_data)
263{
264 const struct northbridge_intel_sandybridge_config *cfg;
265
266 const struct device *dev = pcidev_on_root(0, 0);
267 if (!dev || !dev->chip_info)
268 return;
269
270 cfg = dev->chip_info;
271
272 switch (cfg->max_mem_clock_mhz) {
273 /* MRC only supports fixed numbers of frequencies */
274 default:
275 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
276 /* fallthrough */
277 case 400:
278 pei_data->max_ddr3_freq = 800;
279 break;
280 case 533:
281 pei_data->max_ddr3_freq = 1066;
282 break;
283 case 666:
284 pei_data->max_ddr3_freq = 1333;
285 break;
286 case 800:
287 pei_data->max_ddr3_freq = 1600;
288 break;
289
290 }
291
Angel Pons7c49cb82020-03-16 23:17:32 +0100292 memcpy(pei_data->spd_addresses, cfg->spd_addresses, sizeof(pei_data->spd_addresses));
293 memcpy(pei_data->ts_addresses, cfg->ts_addresses, sizeof(pei_data->ts_addresses));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100294
Angel Pons7c49cb82020-03-16 23:17:32 +0100295 pei_data->ec_present = cfg->ec_present;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100296 pei_data->ddr3lv_support = cfg->ddr3lv_support;
297
298 pei_data->nmode = cfg->nmode;
299 pei_data->ddr_refresh_rate_config = cfg->ddr_refresh_rate_config;
300
301 memcpy(pei_data->usb_port_config, cfg->usb_port_config,
302 sizeof(pei_data->usb_port_config));
303
Angel Pons7c49cb82020-03-16 23:17:32 +0100304 pei_data->usb3.mode = cfg->usb3.mode;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100305 pei_data->usb3.hs_port_switch_mask = cfg->usb3.hs_port_switch_mask;
Angel Pons7c49cb82020-03-16 23:17:32 +0100306 pei_data->usb3.preboot_support = cfg->usb3.preboot_support;
307 pei_data->usb3.xhci_streams = cfg->usb3.xhci_streams;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100308}
309
Nico Huber47bf4982019-11-17 02:58:00 +0100310static void disable_p2p(void)
311{
Angel Pons7c49cb82020-03-16 23:17:32 +0100312 /* Disable PCI-to-PCI bridge early to prevent probing by MRC */
Nico Huber47bf4982019-11-17 02:58:00 +0100313 const struct device *const p2p = pcidev_on_root(0x1e, 0);
314 if (p2p && p2p->enabled)
315 return;
316
317 RCBA32(FD) |= PCH_DISABLE_P2P;
318}
319
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100320void perform_raminit(int s3resume)
321{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100322 struct pei_data pei_data;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200323 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100324
325 /* Prepare USB controller early in S3 resume */
326 if (!mainboard_should_reset_usb(s3resume))
327 enable_usb_bar();
328
Patrick Rudolph5709e032019-03-25 10:12:14 +0100329 memset(&pei_data, 0, sizeof(pei_data));
330 pei_data.pei_version = PEI_VERSION,
331
332 northbridge_fill_pei_data(&pei_data);
333 southbridge_fill_pei_data(&pei_data);
334 devicetree_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100335 mainboard_fill_pei_data(&pei_data);
336
337 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200338
Patrick Rudolph5709e032019-03-25 10:12:14 +0100339 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
340 pei_data.dimm_channel0_disabled =
341 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
342 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
343
344 pei_data.dimm_channel1_disabled =
345 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
346 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
347
Patrick Rudolph59b42552019-05-08 12:44:15 +0200348 /* Fix spd_data. MRC only uses spd_data[0] and ignores the other */
349 for (size_t i = 1; i < ARRAY_SIZE(pei_data.spd_data); i++) {
350 if (pei_data.spd_data[i][0] && !pei_data.spd_data[0][0]) {
351 memcpy(pei_data.spd_data[0], pei_data.spd_data[i],
352 sizeof(pei_data.spd_data[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +0100353
Patrick Rudolph59b42552019-05-08 12:44:15 +0200354 } else if (pei_data.spd_data[i][0] && pei_data.spd_data[0][0]) {
355 if (memcmp(pei_data.spd_data[i], pei_data.spd_data[0],
356 sizeof(pei_data.spd_data[0])) != 0)
357 die("Onboard SPDs must match each other");
358 }
359 }
360
Nico Huber47bf4982019-11-17 02:58:00 +0100361 disable_p2p();
362
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100363 pei_data.boot_mode = s3resume ? 2 : 0;
364 timestamp_add_now(TS_BEFORE_INITRAM);
365 sdram_initialize(&pei_data);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200366 timestamp_add_now(TS_AFTER_INITRAM);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200367
Angel Pons7c49cb82020-03-16 23:17:32 +0100368 /* Sanity check mrc_var location by verifying a known field */
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200369 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200370 if (mrc_var->tx_byte == (uintptr_t)pei_data.tx_byte) {
371 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100372 mrc_var->pool_base, mrc_var->pool_base + mrc_var->pool_used);
373
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200374 } else {
375 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
Felix Held2a29d452021-05-25 19:15:11 +0200376 hexdump(mrc_var, sizeof(*mrc_var));
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200377 }
378
Angel Pons7c49cb82020-03-16 23:17:32 +0100379 const int cbmem_was_initted = !cbmem_recovery(s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100380 if (!s3resume)
381 save_mrc_data(&pei_data);
382
383 if (s3resume && !cbmem_was_initted) {
384 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200385 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100386 }
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600387 setup_sdram_meminfo(&pei_data);
388}
389
390void setup_sdram_meminfo(struct pei_data *pei_data)
391{
392 u32 addr_decoder_common, addr_decode_ch[2];
393 struct memory_info *mem_info;
394 struct dimm_info *dimm;
395 int dimm_size;
396 int i;
397 int dimm_cnt = 0;
398
399 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
400 memset(mem_info, 0, sizeof(struct memory_info));
401
402 addr_decoder_common = mchbar_read32(MAD_CHNL);
403 addr_decode_ch[0] = mchbar_read32(MAD_DIMM_CH0);
404 addr_decode_ch[1] = mchbar_read32(MAD_DIMM_CH1);
405
406 const int refclk = mchbar_read32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
407 const int ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100;
408
409 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
410 u32 ch_conf = addr_decode_ch[i];
411
412 /* DIMM-A */
413 dimm_size = ((ch_conf >> 0) & 0xff) * 256;
414 if (dimm_size) {
415 dimm = &mem_info->dimm[dimm_cnt];
416 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100417 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600418 dimm->ddr_frequency = ddr_frequency;
419 dimm->rank_per_dimm = 1 + ((ch_conf >> 17) & 1);
420 dimm->channel_num = i;
421 dimm->dimm_num = 0;
422 dimm->bank_locator = i * 2;
423 memcpy(dimm->serial, /* bytes 122-125 */
424 &pei_data->spd_data[0][122],
425 sizeof(uint8_t) * 4);
426 memcpy(dimm->module_part_number, /* bytes 128-145 */
427 &pei_data->spd_data[0][128],
428 sizeof(uint8_t) * 18);
429 dimm->mod_id = /* bytes 117/118 */
430 (pei_data->spd_data[0][118] << 8) |
431 (pei_data->spd_data[0][117] & 0xFF);
432 dimm->mod_type = 3; /* SPD_SODIMM */
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100433 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600434 dimm_cnt++;
435 }
436 /* DIMM-B */
437 dimm_size = ((ch_conf >> 8) & 0xff) * 256;
438 if (dimm_size) {
439 dimm = &mem_info->dimm[dimm_cnt];
440 dimm->dimm_size = dimm_size;
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100441 dimm->ddr_type = MEMORY_TYPE_DDR3;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600442 dimm->ddr_frequency = ddr_frequency;
443 dimm->rank_per_dimm = 1 + ((ch_conf >> 18) & 1);
444 dimm->channel_num = i;
445 dimm->dimm_num = 1;
446 dimm->bank_locator = i * 2;
447 memcpy(dimm->serial, /* bytes 122-125 */
448 &pei_data->spd_data[0][122],
449 sizeof(uint8_t) * 4);
450 memcpy(dimm->module_part_number, /* bytes 128-145 */
451 &pei_data->spd_data[0][128],
452 sizeof(uint8_t) * 18);
453 dimm->mod_id = /* bytes 117/118 */
454 (pei_data->spd_data[0][118] << 8) |
455 (pei_data->spd_data[0][117] & 0xFF);
456 dimm->mod_type = 3; /* SPD_SODIMM */
Elyes HAOUAS62b23c12022-01-26 07:43:51 +0100457 dimm->bus_width = MEMORY_BUS_WIDTH_64;
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600458 dimm_cnt++;
459 }
460 }
461 mem_info->dimm_cnt = dimm_cnt;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100462}