-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RAS: Introduce AMD Address Translation Library
AMD Zen-based systems report memory errors through Machine Check banks representing Unified Memory Controllers (UMCs). The address value reported for DRAM ECC errors is a "normalized address" that is relative to the UMC. This normalized address must be converted to a system physical address to be usable by the OS. Support for this address translation was introduced to the MCA subsystem with Zen1 systems. The code was later moved to the AMD64 EDAC module, since this was the only user of the code at the time. However, there are uses for this translation outside of EDAC. The system physical address can be used in MCA for preemptive page offlining as done in some MCA notifier functions. Also, this translation is needed as the basis of similar functionality needed for some CXL configurations on AMD systems. Introduce a common address translation library that can be used for multiple subsystems including MCA, EDAC, and CXL. Include support for UMC normalized to system physical address translation for current CPU systems. The Data Fabric Indirect register access offsets and one of the register fields were changed. Default to the current offsets and register field definition. And fallback to the older values if running on a "legacy" system. Provide built-in code to facilitate the loading and unloading of the library module without affecting other modules or built-in code. Signed-off-by: Yazen Ghannam <yazen.ghannam@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20240123041401.79812-2-yazen.ghannam@amd.com
- Loading branch information
Yazen Ghannam
authored and
Borislav Petkov (AMD)
committed
Jan 24, 2024
1 parent
6613476
commit 3f31749
Showing
16 changed files
with
3,336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,6 @@ menuconfig RAS | |
if RAS | ||
|
||
source "arch/x86/ras/Kconfig" | ||
source "drivers/ras/amd/atl/Kconfig" | ||
|
||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
obj-$(CONFIG_RAS) += ras.o | ||
obj-$(CONFIG_DEBUG_FS) += debugfs.o | ||
obj-$(CONFIG_RAS_CEC) += cec.o | ||
|
||
obj-y += amd/atl/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# SPDX-License-Identifier: GPL-2.0-or-later | ||
# | ||
# AMD Address Translation Library Kconfig | ||
# | ||
# Copyright (c) 2023, Advanced Micro Devices, Inc. | ||
# All Rights Reserved. | ||
# | ||
# Author: Yazen Ghannam <Yazen.Ghannam@amd.com> | ||
|
||
config AMD_ATL | ||
tristate "AMD Address Translation Library" | ||
depends on AMD_NB && X86_64 && RAS | ||
default N | ||
help | ||
This library includes support for implementation-specific | ||
address translation procedures needed for various error | ||
handling cases. | ||
|
||
Enable this option if using DRAM ECC on Zen-based systems | ||
and OS-based error handling. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# SPDX-License-Identifier: GPL-2.0-or-later | ||
# | ||
# AMD Address Translation Library Makefile | ||
# | ||
# Copyright (c) 2023, Advanced Micro Devices, Inc. | ||
# All Rights Reserved. | ||
# | ||
# Author: Yazen Ghannam <Yazen.Ghannam@amd.com> | ||
|
||
amd_atl-y := access.o | ||
amd_atl-y += core.o | ||
amd_atl-y += dehash.o | ||
amd_atl-y += denormalize.o | ||
amd_atl-y += map.o | ||
amd_atl-y += system.o | ||
amd_atl-y += umc.o | ||
|
||
obj-$(CONFIG_AMD_ATL) += amd_atl.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* AMD Address Translation Library | ||
* | ||
* access.c : DF Indirect Access functions | ||
* | ||
* Copyright (c) 2023, Advanced Micro Devices, Inc. | ||
* All Rights Reserved. | ||
* | ||
* Author: Yazen Ghannam <Yazen.Ghannam@amd.com> | ||
*/ | ||
|
||
#include "internal.h" | ||
|
||
/* Protect the PCI config register pairs used for DF indirect access. */ | ||
static DEFINE_MUTEX(df_indirect_mutex); | ||
|
||
/* | ||
* Data Fabric Indirect Access uses FICAA/FICAD. | ||
* | ||
* Fabric Indirect Configuration Access Address (FICAA): constructed based | ||
* on the device's Instance Id and the PCI function and register offset of | ||
* the desired register. | ||
* | ||
* Fabric Indirect Configuration Access Data (FICAD): there are FICAD | ||
* low and high registers but so far only the low register is needed. | ||
* | ||
* Use Instance Id 0xFF to indicate a broadcast read. | ||
*/ | ||
#define DF_BROADCAST 0xFF | ||
|
||
#define DF_FICAA_INST_EN BIT(0) | ||
#define DF_FICAA_REG_NUM GENMASK(10, 1) | ||
#define DF_FICAA_FUNC_NUM GENMASK(13, 11) | ||
#define DF_FICAA_INST_ID GENMASK(23, 16) | ||
|
||
#define DF_FICAA_REG_NUM_LEGACY GENMASK(10, 2) | ||
|
||
static int __df_indirect_read(u16 node, u8 func, u16 reg, u8 instance_id, u32 *lo) | ||
{ | ||
u32 ficaa_addr = 0x8C, ficad_addr = 0xB8; | ||
struct pci_dev *F4; | ||
int err = -ENODEV; | ||
u32 ficaa = 0; | ||
|
||
if (node >= amd_nb_num()) | ||
goto out; | ||
|
||
F4 = node_to_amd_nb(node)->link; | ||
if (!F4) | ||
goto out; | ||
|
||
/* Enable instance-specific access. */ | ||
if (instance_id != DF_BROADCAST) { | ||
ficaa |= FIELD_PREP(DF_FICAA_INST_EN, 1); | ||
ficaa |= FIELD_PREP(DF_FICAA_INST_ID, instance_id); | ||
} | ||
|
||
/* | ||
* The two least-significant bits are masked when inputing the | ||
* register offset to FICAA. | ||
*/ | ||
reg >>= 2; | ||
|
||
if (df_cfg.flags.legacy_ficaa) { | ||
ficaa_addr = 0x5C; | ||
ficad_addr = 0x98; | ||
|
||
ficaa |= FIELD_PREP(DF_FICAA_REG_NUM_LEGACY, reg); | ||
} else { | ||
ficaa |= FIELD_PREP(DF_FICAA_REG_NUM, reg); | ||
} | ||
|
||
ficaa |= FIELD_PREP(DF_FICAA_FUNC_NUM, func); | ||
|
||
mutex_lock(&df_indirect_mutex); | ||
|
||
err = pci_write_config_dword(F4, ficaa_addr, ficaa); | ||
if (err) { | ||
pr_warn("Error writing DF Indirect FICAA, FICAA=0x%x\n", ficaa); | ||
goto out_unlock; | ||
} | ||
|
||
err = pci_read_config_dword(F4, ficad_addr, lo); | ||
if (err) | ||
pr_warn("Error reading DF Indirect FICAD LO, FICAA=0x%x.\n", ficaa); | ||
|
||
pr_debug("node=%u inst=0x%x func=0x%x reg=0x%x val=0x%x", | ||
node, instance_id, func, reg << 2, *lo); | ||
|
||
out_unlock: | ||
mutex_unlock(&df_indirect_mutex); | ||
|
||
out: | ||
return err; | ||
} | ||
|
||
int df_indirect_read_instance(u16 node, u8 func, u16 reg, u8 instance_id, u32 *lo) | ||
{ | ||
return __df_indirect_read(node, func, reg, instance_id, lo); | ||
} | ||
|
||
int df_indirect_read_broadcast(u16 node, u8 func, u16 reg, u32 *lo) | ||
{ | ||
return __df_indirect_read(node, func, reg, DF_BROADCAST, lo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* AMD Address Translation Library | ||
* | ||
* core.c : Module init and base translation functions | ||
* | ||
* Copyright (c) 2023, Advanced Micro Devices, Inc. | ||
* All Rights Reserved. | ||
* | ||
* Author: Yazen Ghannam <Yazen.Ghannam@amd.com> | ||
*/ | ||
|
||
#include <linux/module.h> | ||
#include <asm/cpu_device_id.h> | ||
|
||
#include "internal.h" | ||
|
||
struct df_config df_cfg __read_mostly; | ||
|
||
static int addr_over_limit(struct addr_ctx *ctx) | ||
{ | ||
u64 dram_limit_addr; | ||
|
||
if (df_cfg.rev >= DF4) | ||
dram_limit_addr = FIELD_GET(DF4_DRAM_LIMIT_ADDR, ctx->map.limit); | ||
else | ||
dram_limit_addr = FIELD_GET(DF2_DRAM_LIMIT_ADDR, ctx->map.limit); | ||
|
||
dram_limit_addr <<= DF_DRAM_BASE_LIMIT_LSB; | ||
dram_limit_addr |= GENMASK(DF_DRAM_BASE_LIMIT_LSB - 1, 0); | ||
|
||
/* Is calculated system address above DRAM limit address? */ | ||
if (ctx->ret_addr > dram_limit_addr) { | ||
atl_debug(ctx, "Calculated address (0x%016llx) > DRAM limit (0x%016llx)", | ||
ctx->ret_addr, dram_limit_addr); | ||
return -EINVAL; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static bool legacy_hole_en(struct addr_ctx *ctx) | ||
{ | ||
u32 reg = ctx->map.base; | ||
|
||
if (df_cfg.rev >= DF4) | ||
reg = ctx->map.ctl; | ||
|
||
return FIELD_GET(DF_LEGACY_MMIO_HOLE_EN, reg); | ||
} | ||
|
||
static int add_legacy_hole(struct addr_ctx *ctx) | ||
{ | ||
u32 dram_hole_base; | ||
u8 func = 0; | ||
|
||
if (!legacy_hole_en(ctx)) | ||
return 0; | ||
|
||
if (df_cfg.rev >= DF4) | ||
func = 7; | ||
|
||
if (df_indirect_read_broadcast(ctx->node_id, func, 0x104, &dram_hole_base)) | ||
return -EINVAL; | ||
|
||
dram_hole_base &= DF_DRAM_HOLE_BASE_MASK; | ||
|
||
if (ctx->ret_addr >= dram_hole_base) | ||
ctx->ret_addr += (BIT_ULL(32) - dram_hole_base); | ||
|
||
return 0; | ||
} | ||
|
||
static u64 get_base_addr(struct addr_ctx *ctx) | ||
{ | ||
u64 base_addr; | ||
|
||
if (df_cfg.rev >= DF4) | ||
base_addr = FIELD_GET(DF4_BASE_ADDR, ctx->map.base); | ||
else | ||
base_addr = FIELD_GET(DF2_BASE_ADDR, ctx->map.base); | ||
|
||
return base_addr << DF_DRAM_BASE_LIMIT_LSB; | ||
} | ||
|
||
static int add_base_and_hole(struct addr_ctx *ctx) | ||
{ | ||
ctx->ret_addr += get_base_addr(ctx); | ||
|
||
if (add_legacy_hole(ctx)) | ||
return -EINVAL; | ||
|
||
return 0; | ||
} | ||
|
||
static bool late_hole_remove(struct addr_ctx *ctx) | ||
{ | ||
if (df_cfg.rev == DF3p5) | ||
return true; | ||
|
||
if (df_cfg.rev == DF4) | ||
return true; | ||
|
||
if (ctx->map.intlv_mode == DF3_6CHAN) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
unsigned long norm_to_sys_addr(u8 socket_id, u8 die_id, u8 coh_st_inst_id, unsigned long addr) | ||
{ | ||
struct addr_ctx ctx; | ||
|
||
if (df_cfg.rev == UNKNOWN) | ||
return -EINVAL; | ||
|
||
memset(&ctx, 0, sizeof(ctx)); | ||
|
||
/* Start from the normalized address */ | ||
ctx.ret_addr = addr; | ||
ctx.inst_id = coh_st_inst_id; | ||
|
||
ctx.inputs.norm_addr = addr; | ||
ctx.inputs.socket_id = socket_id; | ||
ctx.inputs.die_id = die_id; | ||
ctx.inputs.coh_st_inst_id = coh_st_inst_id; | ||
|
||
if (determine_node_id(&ctx, socket_id, die_id)) | ||
return -EINVAL; | ||
|
||
if (get_address_map(&ctx)) | ||
return -EINVAL; | ||
|
||
if (denormalize_address(&ctx)) | ||
return -EINVAL; | ||
|
||
if (!late_hole_remove(&ctx) && add_base_and_hole(&ctx)) | ||
return -EINVAL; | ||
|
||
if (dehash_address(&ctx)) | ||
return -EINVAL; | ||
|
||
if (late_hole_remove(&ctx) && add_base_and_hole(&ctx)) | ||
return -EINVAL; | ||
|
||
if (addr_over_limit(&ctx)) | ||
return -EINVAL; | ||
|
||
return ctx.ret_addr; | ||
} | ||
|
||
static void check_for_legacy_df_access(void) | ||
{ | ||
/* | ||
* All Zen-based systems before Family 19h use the legacy | ||
* DF Indirect Access (FICAA/FICAD) offsets. | ||
*/ | ||
if (boot_cpu_data.x86 < 0x19) { | ||
df_cfg.flags.legacy_ficaa = true; | ||
return; | ||
} | ||
|
||
/* All systems after Family 19h use the current offsets. */ | ||
if (boot_cpu_data.x86 > 0x19) | ||
return; | ||
|
||
/* Some Family 19h systems use the legacy offsets. */ | ||
switch (boot_cpu_data.x86_model) { | ||
case 0x00 ... 0x0f: | ||
case 0x20 ... 0x5f: | ||
df_cfg.flags.legacy_ficaa = true; | ||
} | ||
} | ||
|
||
/* | ||
* This library provides functionality for AMD-based systems with a Data Fabric. | ||
* The set of systems with a Data Fabric is equivalent to the set of Zen-based systems | ||
* and the set of systems with the Scalable MCA feature at this time. However, these | ||
* are technically independent things. | ||
* | ||
* It's possible to match on the PCI IDs of the Data Fabric devices, but this will be | ||
* an ever expanding list. Instead, match on the SMCA and Zen features to cover all | ||
* relevant systems. | ||
*/ | ||
static const struct x86_cpu_id amd_atl_cpuids[] = { | ||
X86_MATCH_FEATURE(X86_FEATURE_SMCA, NULL), | ||
X86_MATCH_FEATURE(X86_FEATURE_ZEN, NULL), | ||
{ } | ||
}; | ||
MODULE_DEVICE_TABLE(x86cpu, amd_atl_cpuids); | ||
|
||
static int __init amd_atl_init(void) | ||
{ | ||
if (!x86_match_cpu(amd_atl_cpuids)) | ||
return -ENODEV; | ||
|
||
if (!amd_nb_num()) | ||
return -ENODEV; | ||
|
||
check_for_legacy_df_access(); | ||
|
||
if (get_df_system_info()) | ||
return -ENODEV; | ||
|
||
/* Increment this module's recount so that it can't be easily unloaded. */ | ||
__module_get(THIS_MODULE); | ||
amd_atl_register_decoder(convert_umc_mca_addr_to_sys_addr); | ||
|
||
pr_info("AMD Address Translation Library initialized"); | ||
return 0; | ||
} | ||
|
||
/* | ||
* Exit function is only needed for testing and debug. Module unload must be | ||
* forced to override refcount check. | ||
*/ | ||
static void __exit amd_atl_exit(void) | ||
{ | ||
amd_atl_unregister_decoder(); | ||
} | ||
|
||
module_init(amd_atl_init); | ||
module_exit(amd_atl_exit); | ||
|
||
MODULE_LICENSE("GPL"); |
Oops, something went wrong.