memtest86plus/system/imc/intel_hsw.c
Sam Demeulemeester 7aeac7271f
Add Memory Controller Registers polling to get current DRAM Timings/Frequency (#306)
Read the memory controller configuration (instead of just relying on SPD data) to get the actual live settings.

Currently supported platforms:
* Intel SNB to RPL (Core 2nd Gen to Core 13th Gen) - Desktop only (no Server nor Mobile)
* AMD SMR to RPL (Zen to Zen4) - Desktop only (no Server, Mobile nor APU).


Individual commits below for archival:

* First functions skeleton for reading IMC/ECC Registers

* Change directory name from 'chipsets' to 'mch' (Memory Controller Hub)

* Add Intel HSW and fix new files encoding

* First Intel HSW IMC implementation

* Add an option to disable MCH registers polling

* Remove old include from Makefiles

* Better Makefile and padding fixes

* Statically init 'imc' struct to generate string relocation record

* Small typos & code fixes

* Add IMC support for Intel Core 6/7/8/9th Gen (SKL/KBL/CFL/CML) This is a bit more complex than Haswell and below because MMIO switched to 64-bit with Skylake (lot of) betatesting needed

* Add IMC read support for Intel SNB/IVB (2nd/3rd gen Core)

* Fix hard-lock on Intel SNB/IVB due to wrong access type on MCHBAR pointer

* Move AMD SMN Registers & offsets to a specific header file

* Add IMC Read support for AMD Zen/Zen2 CPUs

* Change 'IMC' to 'MCH' in Makefiles to match actual mch/ directory

* Add IMC Reading support for Intel ADL&RPL CPUs (Core Gen12&13)

* Add support for Intel Rocket Lake (Core 11th Gen) and AMD Vermeer

* Add IMC reading for AMD Zen4 'Raphael' AM5 CPUs

* Various Cleanup #1 
Change terminology from Intel-based 'MCH' (Memory Controller Hub) to more universal 'IMC' (Integrated Memory Controller) Integrate imc_type var into imc struct. Remove previously created AMD SNM header file

* Various Cleanup 2

* Change DDR5 display format for IMC specs
DDR5 Freq can be > 10000 and timings up to 63-127-127-127, which overwflow the available space.
This commit remove the raw frequency on DDR5 (which may be incorrect due to Gear mechanism) and leave a bit of space to display the Gear engaged in the future
2023-05-12 15:33:28 +02:00

93 lines
2.4 KiB
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2004-2023 Sam Demeulemeester
//
// ------------------------
//
// Platform-specific code for Intel Haswell CPUs (HSW)
//
#include "cpuinfo.h"
#include "memctrl.h"
#include "msr.h"
#include "pci.h"
#include "imc.h"
#define HSW_MMR_BASE_REG 0x48
#define HSW_REG_MAIN_CHAN0 0x5004
#define HSW_REG_MAIN_CHAN1 0x5008
#define HSW_REG_MCH_CFG 0x5E04
#define HSW_REG_TIMING_CAS 0x4014
#define HSW_REG_TIMING_RCD 0x4000
void get_imc_config_intel_hsw(void)
{
uint32_t mmio_reg, mch_cfg, offset;
uint32_t reg0, reg1;
float cpu_ratio, dram_ratio;
uintptr_t *ptr;
imc.type = "DDR3";
imc.tCL_dec = 0;
// Get Memory Mapped Register Base Address (Enable MMIO if needed)
mmio_reg = pci_config_read32(0, 0, 0, HSW_MMR_BASE_REG);
if (!(mmio_reg & 0x1)) {
pci_config_write32( 0, 0, 0, HSW_MMR_BASE_REG, mmio_reg | 1);
mmio_reg = pci_config_read32(0, 0, 0, HSW_MMR_BASE_REG);
if (!(mmio_reg & 0x1)) return;
}
mmio_reg &= 0xFFFFC000;
// Get DRAM Ratio
ptr = (uintptr_t*)((uintptr_t)mmio_reg + HSW_REG_MCH_CFG);
mch_cfg = *ptr & 0xFFFF;
if ((mch_cfg >> 8) & 1) {
dram_ratio = (float)(*ptr & 0x1F) * (100.0f / 100.0f);
} else {
dram_ratio = (float)(*ptr & 0x1F) * (133.34f / 100.0f);
}
// Get CPU Ratio
rdmsr(MSR_IA32_PLATFORM_INFO, reg0, reg1);
cpu_ratio = (float)((reg0 >> 8) & 0xFF);
if (!cpu_ratio) return;
// Compute DRAM Frequency
imc.freq = ((clks_per_msec / 1000) / cpu_ratio) * dram_ratio * 2;
if (imc.freq < 350 || imc.freq > 5000) {
imc.freq = 0;
return;
}
// Get Main Memory Controller Register for both channels
ptr = (uintptr_t*)((uintptr_t)mmio_reg + HSW_REG_MAIN_CHAN0);
reg0 = *ptr & 0xFFFF;
ptr = (uintptr_t*)((uintptr_t)mmio_reg + HSW_REG_MAIN_CHAN1);
reg1 = *ptr & 0xFFFF;
// Populate IMC width
imc.width = (reg0 && reg1) ? 128 : 64;
// Define offset (ie: which channel is really used)
offset = reg0 ? 0x0000 : 0x4000;
// CAS Latency (tCAS)
ptr = (uintptr_t*)((uintptr_t)mmio_reg + offset + HSW_REG_TIMING_CAS);
imc.tCL = *ptr & 0x1F;
// RAS-To-CAS (tRCD)
ptr = (uintptr_t*)((uintptr_t)mmio_reg + offset + HSW_REG_TIMING_RCD);
imc.tRCD = *ptr & 0x1F;
// RAS Precharge (tRP)
imc.tRP = (*ptr >> 5) & 0x1F;
// RAS Active to precharge (tRAS)
imc.tRAS = (*ptr >> 10) & 0x3F;
}