blob: 186ab59d3a9fbc21e429095d17bbfd3db2e69c9d [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 HAOUAS1d6484a2020-07-10 11:18:11 +020017#include <stddef.h>
18#include <stdint.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010019#include <timestamp.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070020#include "raminit.h"
21#include "pei_data.h"
22#include "sandybridge.h"
Patrick Rudolph5709e032019-03-25 10:12:14 +010023#include "chip.h"
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020024#include <security/vboot/vboot_common.h>
Patrick Georgi27fbbcf2019-04-23 12:33:23 +020025#include <southbridge/intel/bd82x6x/pch.h>
Matt DeVillierff1ef8d2016-12-24 15:36:24 -060026#include <memory_info.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070027
28/* Management Engine is in the southbridge */
Elyes HAOUAS21b71ce62018-06-16 18:43:52 +020029#include <southbridge/intel/bd82x6x/me.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070030
31/*
32 * MRC scrambler seed offsets should be reserved in
33 * mainboard cmos.layout and not covered by checksum.
34 */
Julius Wernercd49cce2019-03-05 16:53:33 -080035#if CONFIG(USE_OPTION_TABLE)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070036#include "option_table.h"
Angel Pons7c49cb82020-03-16 23:17:32 +010037#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
38#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070039#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
40#else
41#define CMOS_OFFSET_MRC_SEED 152
42#define CMOS_OFFSET_MRC_SEED_S3 156
43#define CMOS_OFFSET_MRC_SEED_CHK 160
44#endif
45
Arthur Heymans7539b8c2017-12-24 10:42:57 +010046#define MRC_CACHE_VERSION 0
47
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070048void save_mrc_data(struct pei_data *pei_data)
49{
50 u16 c1, c2, checksum;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070051
52 /* Save the MRC S3 restore data to cbmem */
Angel Pons7c49cb82020-03-16 23:17:32 +010053 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION, pei_data->mrc_output,
Arthur Heymans7539b8c2017-12-24 10:42:57 +010054 pei_data->mrc_output_len);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070055
56 /* Save the MRC seed values to CMOS */
Kyösti Mälkki28791072020-01-04 12:58:53 +020057 cmos_write32(pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070058 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
59 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
60
Kyösti Mälkki28791072020-01-04 12:58:53 +020061 cmos_write32(pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070062 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
63 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
64
65 /* Save a simple checksum of the seed values */
Angel Pons7c49cb82020-03-16 23:17:32 +010066 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
67 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070068 checksum = add_ip_checksums(sizeof(u32), c1, c2);
69
Angel Pons7c49cb82020-03-16 23:17:32 +010070 cmos_write((checksum >> 0) & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
71 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK + 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070072}
73
74static void prepare_mrc_cache(struct pei_data *pei_data)
75{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070076 u16 c1, c2, checksum, seed_checksum;
Shelley Chenad9cd682020-07-23 16:10:52 -070077 size_t mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070078
Angel Pons7c49cb82020-03-16 23:17:32 +010079 /* Preset just in case there is an error */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070080 pei_data->mrc_input = NULL;
81 pei_data->mrc_input_len = 0;
82
83 /* Read scrambler seeds from CMOS */
84 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
85 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
86 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
87
88 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
89 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
90 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
91
92 /* Compute seed checksum and compare */
Angel Pons7c49cb82020-03-16 23:17:32 +010093 c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed, sizeof(u32));
94 c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070095 checksum = add_ip_checksums(sizeof(u32), c1, c2);
96
Angel Pons7c49cb82020-03-16 23:17:32 +010097 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
98 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070099
100 if (checksum != seed_checksum) {
101 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
102 pei_data->scrambler_seed = 0;
103 pei_data->scrambler_seed_s3 = 0;
104 return;
105 }
106
Shelley Chenad9cd682020-07-23 16:10:52 -0700107 pei_data->mrc_input = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
108 MRC_CACHE_VERSION,
109 &mrc_size);
110 if (pei_data->mrc_input == NULL) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100111 /* Error message printed in find_current_mrc_cache */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700112 return;
113 }
114
Shelley Chenad9cd682020-07-23 16:10:52 -0700115 pei_data->mrc_input_len = mrc_size;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700116
Shelley Chenad9cd682020-07-23 16:10:52 -0700117 printk(BIOS_DEBUG, "%s: at %p, size %zx\n", __func__,
118 pei_data->mrc_input, mrc_size);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700119}
120
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700121/**
122 * Find PEI executable in coreboot filesystem and execute it.
123 *
124 * @param pei_data: configuration data for UEFI PEI reference code
125 */
126void sdram_initialize(struct pei_data *pei_data)
127{
Angel Pons7c49cb82020-03-16 23:17:32 +0100128 int (*entry)(struct pei_data *pei_data) __attribute__((regparm(1)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700129
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700130 /* Wait for ME to be ready */
131 intel_early_me_init();
132 intel_early_me_uma_size();
133
134 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
135
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700136 /*
Shelley Chen6615c6e2020-10-27 15:58:31 -0700137 * Always pass in mrc_cache data. The driver will determine
138 * whether to use the data or not.
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700139 */
Shelley Chen6615c6e2020-10-27 15:58:31 -0700140 prepare_mrc_cache(pei_data);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700141
142 /* If MRC data is not found we cannot continue S3 resume. */
143 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Elyes HAOUAS3cd43272020-03-05 22:01:17 +0100144 printk(BIOS_DEBUG, "Giving up in %s: No MRC data\n", __func__);
Elyes HAOUASc0567292019-04-28 17:57:47 +0200145 system_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700146 }
147
148 /* Pass console handler in pei_data */
149 pei_data->tx_byte = do_putchar;
150
151 /* Locate and call UEFI System Agent binary. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800152 entry = cbfs_map("mrc.bin", NULL);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700153 if (entry) {
154 int rv;
155 rv = entry (pei_data);
156 if (rv) {
157 switch (rv) {
158 case -1:
159 printk(BIOS_ERR, "PEI version mismatch.\n");
160 break;
161 case -2:
162 printk(BIOS_ERR, "Invalid memory frequency.\n");
163 break;
164 default:
165 printk(BIOS_ERR, "MRC returned %x.\n", rv);
166 }
Keith Shortbb41aba2019-05-16 14:07:43 -0600167 die_with_post_code(POST_INVALID_VENDOR_BINARY,
168 "Nonzero MRC return value.\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700169 }
170 } else {
171 die("UEFI PEI System Agent not found.\n");
172 }
173
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700174 /* mrc.bin reconfigures USB, so reinit it to have debug */
Julius Wernercd49cce2019-03-05 16:53:33 -0800175 if (CONFIG(USBDEBUG_IN_PRE_RAM))
Kyösti Mälkki63649d22018-12-29 09:40:40 +0200176 usbdebug_hw_init(true);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700177
Angel Pons9f3bc3712020-10-13 23:57:10 +0200178 /* Print the MRC version after executing the UEFI PEI stage */
Angel Pons66780a02021-03-26 13:33:22 +0100179 u32 version = mchbar_read32(MRC_REVISION);
Angel Ponsc1328a62021-06-14 12:43:11 +0200180 printk(BIOS_DEBUG, "MRC Version %u.%u.%u Build %u\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100181 (version >> 24) & 0xff, (version >> 16) & 0xff,
182 (version >> 8) & 0xff, (version >> 0) & 0xff);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700183
Angel Pons7c49cb82020-03-16 23:17:32 +0100184 /*
185 * Send ME init done for SandyBridge here.
186 * This is done inside the SystemAgent binary on IvyBridge.
187 */
188 if (BASE_REV_SNB == (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700189 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
190 else
191 intel_early_me_status();
192
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700193 report_memory_config();
194}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100195
Angel Pons7c49cb82020-03-16 23:17:32 +0100196/*
197 * These are the location and structure of MRC_VAR data in CAR.
198 * The CAR region looks like this:
199 * +------------------+ -> DCACHE_RAM_BASE
200 * | |
201 * | |
202 * | COREBOOT STACK |
203 * | |
204 * | |
205 * +------------------+ -> DCACHE_RAM_BASE + DCACHE_RAM_SIZE
206 * | |
207 * | MRC HEAP |
208 * | size = 0x5000 |
209 * | |
210 * +------------------+
211 * | |
212 * | MRC VAR |
213 * | size = 0x4000 |
214 * | |
215 * +------------------+ -> DACHE_RAM_BASE + DACHE_RAM_SIZE
216 * + DCACHE_RAM_MRC_VAR_SIZE
Arthur Heymans01c83a22019-06-05 13:36:55 +0200217 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100218#define DCACHE_RAM_MRC_VAR_BASE (CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE \
219 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 0x4000)
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200220
221struct mrc_var_data {
222 u32 acpi_timer_flag;
223 u32 pool_used;
224 u32 pool_base;
225 u32 tx_byte;
226 u32 reserved[4];
227} __packed;
228
Patrick Rudolph5709e032019-03-25 10:12:14 +0100229static void northbridge_fill_pei_data(struct pei_data *pei_data)
230{
Angel Ponsd9e58dc2021-01-20 01:22:20 +0100231 pei_data->mchbar = CONFIG_FIXED_MCHBAR_MMIO_BASE;
232 pei_data->dmibar = CONFIG_FIXED_DMIBAR_MMIO_BASE;
233 pei_data->epbar = CONFIG_FIXED_EPBAR_MMIO_BASE;
Shelley Chen4e9bb332021-10-20 15:43:45 -0700234 pei_data->pciexbar = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100235 pei_data->hpet_address = CONFIG_HPET_ADDRESS;
Angel Pons7c49cb82020-03-16 23:17:32 +0100236 pei_data->thermalbase = 0xfed08000;
237 pei_data->system_type = !(get_platform_type() == PLATFORM_MOBILE);
238 pei_data->tseg_size = CONFIG_SMM_TSEG_SIZE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100239
240 if ((cpu_get_cpuid() & 0xffff0) == 0x306a0) {
241 const struct device *dev = pcidev_on_root(1, 0);
242 pei_data->pcie_init = dev && dev->enabled;
243 } else {
244 pei_data->pcie_init = 0;
245 }
246}
247
248static void southbridge_fill_pei_data(struct pei_data *pei_data)
249{
250 const struct device *dev = pcidev_on_root(0x19, 0);
251
Angel Ponsb21bffa2020-07-03 01:02:28 +0200252 pei_data->smbusbar = CONFIG_FIXED_SMBUS_IO_BASE;
Angel Pons7c49cb82020-03-16 23:17:32 +0100253 pei_data->wdbbar = 0x04000000;
254 pei_data->wdbsize = 0x1000;
Angel Pons92717ff2020-09-14 16:22:22 +0200255 pei_data->rcba = (uintptr_t)DEFAULT_RCBA;
Angel Pons7c49cb82020-03-16 23:17:32 +0100256 pei_data->pmbase = DEFAULT_PMBASE;
257 pei_data->gpiobase = DEFAULT_GPIOBASE;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100258 pei_data->gbe_enable = dev && dev->enabled;
259}
260
261static void devicetree_fill_pei_data(struct pei_data *pei_data)
262{
263 const struct northbridge_intel_sandybridge_config *cfg;
264
265 const struct device *dev = pcidev_on_root(0, 0);
266 if (!dev || !dev->chip_info)
267 return;
268
269 cfg = dev->chip_info;
270
271 switch (cfg->max_mem_clock_mhz) {
272 /* MRC only supports fixed numbers of frequencies */
273 default:
274 printk(BIOS_WARNING, "RAMINIT: Limiting DDR3 clock to 800 Mhz\n");
275 /* fallthrough */
276 case 400:
277 pei_data->max_ddr3_freq = 800;
278 break;
279 case 533:
280 pei_data->max_ddr3_freq = 1066;
281 break;
282 case 666:
283 pei_data->max_ddr3_freq = 1333;
284 break;
285 case 800:
286 pei_data->max_ddr3_freq = 1600;
287 break;
288
289 }
290
Angel Pons7c49cb82020-03-16 23:17:32 +0100291 memcpy(pei_data->spd_addresses, cfg->spd_addresses, sizeof(pei_data->spd_addresses));
292 memcpy(pei_data->ts_addresses, cfg->ts_addresses, sizeof(pei_data->ts_addresses));
Patrick Rudolph5709e032019-03-25 10:12:14 +0100293
Angel Pons7c49cb82020-03-16 23:17:32 +0100294 pei_data->ec_present = cfg->ec_present;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100295 pei_data->ddr3lv_support = cfg->ddr3lv_support;
296
297 pei_data->nmode = cfg->nmode;
298 pei_data->ddr_refresh_rate_config = cfg->ddr_refresh_rate_config;
299
300 memcpy(pei_data->usb_port_config, cfg->usb_port_config,
301 sizeof(pei_data->usb_port_config));
302
Angel Pons7c49cb82020-03-16 23:17:32 +0100303 pei_data->usb3.mode = cfg->usb3.mode;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100304 pei_data->usb3.hs_port_switch_mask = cfg->usb3.hs_port_switch_mask;
Angel Pons7c49cb82020-03-16 23:17:32 +0100305 pei_data->usb3.preboot_support = cfg->usb3.preboot_support;
306 pei_data->usb3.xhci_streams = cfg->usb3.xhci_streams;
Patrick Rudolph5709e032019-03-25 10:12:14 +0100307}
308
Nico Huber47bf4982019-11-17 02:58:00 +0100309static void disable_p2p(void)
310{
Angel Pons7c49cb82020-03-16 23:17:32 +0100311 /* Disable PCI-to-PCI bridge early to prevent probing by MRC */
Nico Huber47bf4982019-11-17 02:58:00 +0100312 const struct device *const p2p = pcidev_on_root(0x1e, 0);
313 if (p2p && p2p->enabled)
314 return;
315
316 RCBA32(FD) |= PCH_DISABLE_P2P;
317}
318
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100319void perform_raminit(int s3resume)
320{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100321 struct pei_data pei_data;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200322 struct mrc_var_data *mrc_var;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100323
324 /* Prepare USB controller early in S3 resume */
325 if (!mainboard_should_reset_usb(s3resume))
326 enable_usb_bar();
327
Patrick Rudolph5709e032019-03-25 10:12:14 +0100328 memset(&pei_data, 0, sizeof(pei_data));
329 pei_data.pei_version = PEI_VERSION,
330
331 northbridge_fill_pei_data(&pei_data);
332 southbridge_fill_pei_data(&pei_data);
333 devicetree_fill_pei_data(&pei_data);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100334 mainboard_fill_pei_data(&pei_data);
335
336 post_code(0x3a);
Patrick Rudolph59b42552019-05-08 12:44:15 +0200337
Patrick Rudolph5709e032019-03-25 10:12:14 +0100338 /* Fill after mainboard_fill_pei_data as it might provide spd_data */
339 pei_data.dimm_channel0_disabled =
340 (!pei_data.spd_addresses[0] && !pei_data.spd_data[0][0]) +
341 (!pei_data.spd_addresses[1] && !pei_data.spd_data[1][0]) * 2;
342
343 pei_data.dimm_channel1_disabled =
344 (!pei_data.spd_addresses[2] && !pei_data.spd_data[2][0]) +
345 (!pei_data.spd_addresses[3] && !pei_data.spd_data[3][0]) * 2;
346
Patrick Rudolph59b42552019-05-08 12:44:15 +0200347 /* Fix spd_data. MRC only uses spd_data[0] and ignores the other */
348 for (size_t i = 1; i < ARRAY_SIZE(pei_data.spd_data); i++) {
349 if (pei_data.spd_data[i][0] && !pei_data.spd_data[0][0]) {
350 memcpy(pei_data.spd_data[0], pei_data.spd_data[i],
351 sizeof(pei_data.spd_data[0]));
Angel Pons7c49cb82020-03-16 23:17:32 +0100352
Patrick Rudolph59b42552019-05-08 12:44:15 +0200353 } else if (pei_data.spd_data[i][0] && pei_data.spd_data[0][0]) {
354 if (memcmp(pei_data.spd_data[i], pei_data.spd_data[0],
355 sizeof(pei_data.spd_data[0])) != 0)
356 die("Onboard SPDs must match each other");
357 }
358 }
359
Nico Huber47bf4982019-11-17 02:58:00 +0100360 disable_p2p();
361
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100362 pei_data.boot_mode = s3resume ? 2 : 0;
363 timestamp_add_now(TS_BEFORE_INITRAM);
364 sdram_initialize(&pei_data);
Kyösti Mälkkib33c6fb2021-02-17 20:43:04 +0200365 timestamp_add_now(TS_AFTER_INITRAM);
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200366
Angel Pons7c49cb82020-03-16 23:17:32 +0100367 /* Sanity check mrc_var location by verifying a known field */
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200368 mrc_var = (void *)DCACHE_RAM_MRC_VAR_BASE;
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200369 if (mrc_var->tx_byte == (uintptr_t)pei_data.tx_byte) {
370 printk(BIOS_DEBUG, "MRC_VAR pool occupied [%08x,%08x]\n",
Angel Pons7c49cb82020-03-16 23:17:32 +0100371 mrc_var->pool_base, mrc_var->pool_base + mrc_var->pool_used);
372
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200373 } else {
374 printk(BIOS_ERR, "Could not parse MRC_VAR data\n");
Felix Held2a29d452021-05-25 19:15:11 +0200375 hexdump(mrc_var, sizeof(*mrc_var));
Kyösti Mälkkib697c902019-01-30 08:19:49 +0200376 }
377
Angel Pons7c49cb82020-03-16 23:17:32 +0100378 const int cbmem_was_initted = !cbmem_recovery(s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100379 if (!s3resume)
380 save_mrc_data(&pei_data);
381
382 if (s3resume && !cbmem_was_initted) {
383 /* Failed S3 resume, reset to come up cleanly */
Elyes HAOUASc0567292019-04-28 17:57:47 +0200384 system_reset();
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100385 }
Matt DeVillierff1ef8d2016-12-24 15:36:24 -0600386 setup_sdram_meminfo(&pei_data);
387}
388
389void setup_sdram_meminfo(struct pei_data *pei_data)
390{
391 u32 addr_decoder_common, addr_decode_ch[2];
392 struct memory_info *mem_info;
393 struct dimm_info *dimm;
394 int dimm_size;
395 int i;
396 int dimm_cnt = 0;
397
398 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
399 memset(mem_info, 0, sizeof(struct memory_info));
400
401 addr_decoder_common = mchbar_read32(MAD_CHNL);
402 addr_decode_ch[0] = mchbar_read32(MAD_DIMM_CH0);
403 addr_decode_ch[1] = mchbar_read32(MAD_DIMM_CH1);
404
405 const int refclk = mchbar_read32(MC_BIOS_REQ) & 0x100 ? 100 : 133;
406 const int ddr_frequency = (mchbar_read32(MC_BIOS_DATA) * refclk * 100 * 2 + 50) / 100;
407
408 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
409 u32 ch_conf = addr_decode_ch[i];
410
411 /* DIMM-A */
412 dimm_size = ((ch_conf >> 0) & 0xff) * 256;
413 if (dimm_size) {
414 dimm = &mem_info->dimm[dimm_cnt];
415 dimm->dimm_size = dimm_size;
416 dimm->ddr_type = 0x18; /* DDR3 */
417 dimm->ddr_frequency = ddr_frequency;
418 dimm->rank_per_dimm = 1 + ((ch_conf >> 17) & 1);
419 dimm->channel_num = i;
420 dimm->dimm_num = 0;
421 dimm->bank_locator = i * 2;
422 memcpy(dimm->serial, /* bytes 122-125 */
423 &pei_data->spd_data[0][122],
424 sizeof(uint8_t) * 4);
425 memcpy(dimm->module_part_number, /* bytes 128-145 */
426 &pei_data->spd_data[0][128],
427 sizeof(uint8_t) * 18);
428 dimm->mod_id = /* bytes 117/118 */
429 (pei_data->spd_data[0][118] << 8) |
430 (pei_data->spd_data[0][117] & 0xFF);
431 dimm->mod_type = 3; /* SPD_SODIMM */
432 dimm->bus_width = 0x3; /* 64-bit */
433 dimm_cnt++;
434 }
435 /* DIMM-B */
436 dimm_size = ((ch_conf >> 8) & 0xff) * 256;
437 if (dimm_size) {
438 dimm = &mem_info->dimm[dimm_cnt];
439 dimm->dimm_size = dimm_size;
440 dimm->ddr_type = 0x18; /* DDR3 */
441 dimm->ddr_frequency = ddr_frequency;
442 dimm->rank_per_dimm = 1 + ((ch_conf >> 18) & 1);
443 dimm->channel_num = i;
444 dimm->dimm_num = 1;
445 dimm->bank_locator = i * 2;
446 memcpy(dimm->serial, /* bytes 122-125 */
447 &pei_data->spd_data[0][122],
448 sizeof(uint8_t) * 4);
449 memcpy(dimm->module_part_number, /* bytes 128-145 */
450 &pei_data->spd_data[0][128],
451 sizeof(uint8_t) * 18);
452 dimm->mod_id = /* bytes 117/118 */
453 (pei_data->spd_data[0][118] << 8) |
454 (pei_data->spd_data[0][117] & 0xFF);
455 dimm->mod_type = 3; /* SPD_SODIMM */
456 dimm->bus_width = 0x3; /* 64-bit */
457 dimm_cnt++;
458 }
459 }
460 mem_info->dimm_cnt = dimm_cnt;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +0100461}