Skip to content

Commit f5382de

Browse files
yghannamIngo Molnar
authored andcommitted
x86/mce/AMD: Add system physical address translation for AMD Fam17h
The Unified Memory Controllers (UMCs) on Fam17h log a normalized address in their MCA_ADDR registers. We need to convert that normalized address to a system physical address in order to support a few facilities: 1) To offline poisoned pages in DRAM proactively in the deferred error handler. 2) To print sysaddr and page info for DRAM ECC errors in EDAC. [ Boris: fixes/cleanups ontop: * hi_addr_offset = 0 - no need for that branch. Stick it all under the HiAddrOffsetEn case. It confines hi_addr_offset's declaration too. * Move variables to the innermost scope they're used at so that we save on stack and not blow it up immediately on function entry. * Do not modify *sys_addr prematurely - we want to not exit early and have modified *sys_addr some, which callers get to see. We either convert to a sys_addr or we don't do anything. And we signal that with the retval of the function. * Rename label out -> out_err - because it is the error path. * No need to pr_err of the conversion failed case: imagine a sparsely-populated machine with UMCs which don't have DIMMs. Callers should look at the retval instead and issue a printk only when really necessary. No need for useless info in dmesg. * s/temp_reg/tmp/ and other variable names shortening => shorter code. * Use BIT() everywhere. * Make error messages more informative. * Small build fix for the !CONFIG_X86_MCE_AMD case. * ... and more minor cleanups. ] Signed-off-by: Yazen Ghannam <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: Aravind Gopalakrishnan <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Tony Luck <[email protected]> Cc: linux-edac <[email protected]> Link: http://lkml.kernel.org/r/[email protected] [ Typo fixes. ] Signed-off-by: Ingo Molnar <[email protected]>
1 parent ddfe43c commit f5382de

File tree

3 files changed

+203
-1
lines changed

3 files changed

+203
-1
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ config X86_MCE_INTEL
10251025
config X86_MCE_AMD
10261026
def_bool y
10271027
prompt "AMD MCE features"
1028-
depends on X86_MCE && X86_LOCAL_APIC
1028+
depends on X86_MCE && X86_LOCAL_APIC && AMD_NB
10291029
---help---
10301030
Additional support for AMD specific MCE features such as
10311031
the DRAM Error Threshold.

arch/x86/include/asm/mce.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ static inline void cmci_recheck(void) {}
252252

253253
#ifdef CONFIG_X86_MCE_AMD
254254
void mce_amd_feature_init(struct cpuinfo_x86 *c);
255+
int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr);
255256
#else
256257
static inline void mce_amd_feature_init(struct cpuinfo_x86 *c) { }
258+
static inline int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr) { return -EINVAL; };
257259
#endif
258260

259261
int mce_available(struct cpuinfo_x86 *c);

arch/x86/kernel/cpu/mcheck/mce_amd.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,206 @@ void mce_amd_feature_init(struct cpuinfo_x86 *c)
553553
deferred_error_interrupt_enable(c);
554554
}
555555

556+
int umc_normaddr_to_sysaddr(u64 norm_addr, u16 nid, u8 umc, u64 *sys_addr)
557+
{
558+
u64 dram_base_addr, dram_limit_addr, dram_hole_base;
559+
/* We start from the normalized address */
560+
u64 ret_addr = norm_addr;
561+
562+
u32 tmp;
563+
564+
u8 die_id_shift, die_id_mask, socket_id_shift, socket_id_mask;
565+
u8 intlv_num_dies, intlv_num_chan, intlv_num_sockets;
566+
u8 intlv_addr_sel, intlv_addr_bit;
567+
u8 num_intlv_bits, hashed_bit;
568+
u8 lgcy_mmio_hole_en, base = 0;
569+
u8 cs_mask, cs_id = 0;
570+
bool hash_enabled = false;
571+
572+
/* Read D18F0x1B4 (DramOffset), check if base 1 is used. */
573+
if (amd_df_indirect_read(nid, 0, 0x1B4, umc, &tmp))
574+
goto out_err;
575+
576+
/* Remove HiAddrOffset from normalized address, if enabled: */
577+
if (tmp & BIT(0)) {
578+
u64 hi_addr_offset = (tmp & GENMASK_ULL(31, 20)) << 8;
579+
580+
if (norm_addr >= hi_addr_offset) {
581+
ret_addr -= hi_addr_offset;
582+
base = 1;
583+
}
584+
}
585+
586+
/* Read D18F0x110 (DramBaseAddress). */
587+
if (amd_df_indirect_read(nid, 0, 0x110 + (8 * base), umc, &tmp))
588+
goto out_err;
589+
590+
/* Check if address range is valid. */
591+
if (!(tmp & BIT(0))) {
592+
pr_err("%s: Invalid DramBaseAddress range: 0x%x.\n",
593+
__func__, tmp);
594+
goto out_err;
595+
}
596+
597+
lgcy_mmio_hole_en = tmp & BIT(1);
598+
intlv_num_chan = (tmp >> 4) & 0xF;
599+
intlv_addr_sel = (tmp >> 8) & 0x7;
600+
dram_base_addr = (tmp & GENMASK_ULL(31, 12)) << 16;
601+
602+
/* {0, 1, 2, 3} map to address bits {8, 9, 10, 11} respectively */
603+
if (intlv_addr_sel > 3) {
604+
pr_err("%s: Invalid interleave address select %d.\n",
605+
__func__, intlv_addr_sel);
606+
goto out_err;
607+
}
608+
609+
/* Read D18F0x114 (DramLimitAddress). */
610+
if (amd_df_indirect_read(nid, 0, 0x114 + (8 * base), umc, &tmp))
611+
goto out_err;
612+
613+
intlv_num_sockets = (tmp >> 8) & 0x1;
614+
intlv_num_dies = (tmp >> 10) & 0x3;
615+
dram_limit_addr = ((tmp & GENMASK_ULL(31, 12)) << 16) | GENMASK_ULL(27, 0);
616+
617+
intlv_addr_bit = intlv_addr_sel + 8;
618+
619+
/* Re-use intlv_num_chan by setting it equal to log2(#channels) */
620+
switch (intlv_num_chan) {
621+
case 0: intlv_num_chan = 0; break;
622+
case 1: intlv_num_chan = 1; break;
623+
case 3: intlv_num_chan = 2; break;
624+
case 5: intlv_num_chan = 3; break;
625+
case 7: intlv_num_chan = 4; break;
626+
627+
case 8: intlv_num_chan = 1;
628+
hash_enabled = true;
629+
break;
630+
default:
631+
pr_err("%s: Invalid number of interleaved channels %d.\n",
632+
__func__, intlv_num_chan);
633+
goto out_err;
634+
}
635+
636+
num_intlv_bits = intlv_num_chan;
637+
638+
if (intlv_num_dies > 2) {
639+
pr_err("%s: Invalid number of interleaved nodes/dies %d.\n",
640+
__func__, intlv_num_dies);
641+
goto out_err;
642+
}
643+
644+
num_intlv_bits += intlv_num_dies;
645+
646+
/* Add a bit if sockets are interleaved. */
647+
num_intlv_bits += intlv_num_sockets;
648+
649+
/* Assert num_intlv_bits <= 4 */
650+
if (num_intlv_bits > 4) {
651+
pr_err("%s: Invalid interleave bits %d.\n",
652+
__func__, num_intlv_bits);
653+
goto out_err;
654+
}
655+
656+
if (num_intlv_bits > 0) {
657+
u64 temp_addr_x, temp_addr_i, temp_addr_y;
658+
u8 die_id_bit, sock_id_bit, cs_fabric_id;
659+
660+
/*
661+
* Read FabricBlockInstanceInformation3_CS[BlockFabricID].
662+
* This is the fabric id for this coherent slave. Use
663+
* umc/channel# as instance id of the coherent slave
664+
* for FICAA.
665+
*/
666+
if (amd_df_indirect_read(nid, 0, 0x50, umc, &tmp))
667+
goto out_err;
668+
669+
cs_fabric_id = (tmp >> 8) & 0xFF;
670+
die_id_bit = 0;
671+
672+
/* If interleaved over more than 1 channel: */
673+
if (intlv_num_chan) {
674+
die_id_bit = intlv_num_chan;
675+
cs_mask = (1 << die_id_bit) - 1;
676+
cs_id = cs_fabric_id & cs_mask;
677+
}
678+
679+
sock_id_bit = die_id_bit;
680+
681+
/* Read D18F1x208 (SystemFabricIdMask). */
682+
if (intlv_num_dies || intlv_num_sockets)
683+
if (amd_df_indirect_read(nid, 1, 0x208, umc, &tmp))
684+
goto out_err;
685+
686+
/* If interleaved over more than 1 die. */
687+
if (intlv_num_dies) {
688+
sock_id_bit = die_id_bit + intlv_num_dies;
689+
die_id_shift = (tmp >> 24) & 0xF;
690+
die_id_mask = (tmp >> 8) & 0xFF;
691+
692+
cs_id |= ((cs_fabric_id & die_id_mask) >> die_id_shift) << die_id_bit;
693+
}
694+
695+
/* If interleaved over more than 1 socket. */
696+
if (intlv_num_sockets) {
697+
socket_id_shift = (tmp >> 28) & 0xF;
698+
socket_id_mask = (tmp >> 16) & 0xFF;
699+
700+
cs_id |= ((cs_fabric_id & socket_id_mask) >> socket_id_shift) << sock_id_bit;
701+
}
702+
703+
/*
704+
* The pre-interleaved address consists of XXXXXXIIIYYYYY
705+
* where III is the ID for this CS, and XXXXXXYYYYY are the
706+
* address bits from the post-interleaved address.
707+
* "num_intlv_bits" has been calculated to tell us how many "I"
708+
* bits there are. "intlv_addr_bit" tells us how many "Y" bits
709+
* there are (where "I" starts).
710+
*/
711+
temp_addr_y = ret_addr & GENMASK_ULL(intlv_addr_bit-1, 0);
712+
temp_addr_i = (cs_id << intlv_addr_bit);
713+
temp_addr_x = (ret_addr & GENMASK_ULL(63, intlv_addr_bit)) << num_intlv_bits;
714+
ret_addr = temp_addr_x | temp_addr_i | temp_addr_y;
715+
}
716+
717+
/* Add dram base address */
718+
ret_addr += dram_base_addr;
719+
720+
/* If legacy MMIO hole enabled */
721+
if (lgcy_mmio_hole_en) {
722+
if (amd_df_indirect_read(nid, 0, 0x104, umc, &tmp))
723+
goto out_err;
724+
725+
dram_hole_base = tmp & GENMASK(31, 24);
726+
if (ret_addr >= dram_hole_base)
727+
ret_addr += (BIT_ULL(32) - dram_hole_base);
728+
}
729+
730+
if (hash_enabled) {
731+
/* Save some parentheses and grab ls-bit at the end. */
732+
hashed_bit = (ret_addr >> 12) ^
733+
(ret_addr >> 18) ^
734+
(ret_addr >> 21) ^
735+
(ret_addr >> 30) ^
736+
cs_id;
737+
738+
hashed_bit &= BIT(0);
739+
740+
if (hashed_bit != ((ret_addr >> intlv_addr_bit) & BIT(0)))
741+
ret_addr ^= BIT(intlv_addr_bit);
742+
}
743+
744+
/* Is calculated system address is above DRAM limit address? */
745+
if (ret_addr > dram_limit_addr)
746+
goto out_err;
747+
748+
*sys_addr = ret_addr;
749+
return 0;
750+
751+
out_err:
752+
return -EINVAL;
753+
}
754+
EXPORT_SYMBOL_GPL(umc_normaddr_to_sysaddr);
755+
556756
static void
557757
__log_error(unsigned int bank, bool deferred_err, bool threshold_err, u64 misc)
558758
{

0 commit comments

Comments
 (0)