Skip to contentSkip to conversation

Add support for Hygon family 18h models 4h-8h

PATCH In review Fu Hao 3 participants 9 messages Sep 4 – Sep 20

Subsystems (4)
lore
Share & tools
lore

Fu Hao

author
#0 lore raw

v5 cover Add support for Hygon family 18h models 4h-8h

This patch series introduce support for the new-generation Hygon processor models 0x4–0x8, addressing five key areas:

1. CPU Topology Updates
The new processor introduces a revised CPU topology hierarchy compared to previous generations. This patch updates the kernel's topology detection logic to correctly identify core/socket relationships and cache sharing patterns.

2. L3 Performance Monitoring Updates
The L3 perf registers of model 6h differ from those of the previous processor.

3. Microcode loading
Add microcode loading support for Hygon processor.

4. Audio Controller Driver
Add initial support for the integrated HD-Audio controller. This patch has been accepted and merged into mainline.

5. New SB IOAPIC information support
The southbridge IOAPIC of Hygon's new-generation processors is located at 00:0b.0.
This patch has been accepted and merged into mainline.

v4->v5:
- Add HYPERVISOR check for patch x86/cpu/hygon.
- Refine the commit messages for the third patch.
- Use per-CPU MSR read for microcode revision retrieval.

v3->v4:
- Rebase to 7.2-rc2.
- Refine the commit messages and comments for patch x86/cpu/hygon.
- Add bounds checks to the microcode patch.
- The HD-Audio patch has been applied to for-next branch.

v2->v3:
- Refine the commit messages for patch x86/cpu/hygon.
- Update the logic for deriving the LLC ID from the APIC ID on Hygon processors.
- Clean up and refine Hygon microcode support code.
- Refine the code of Hygon HD-Audio device ID.
- The iommu/hygon patch has been accepted and merged into iommu branch.

v1->v2:
- Refine the patch x86/cpu/hygon.
- Do a separate compilation unit on hygon microcode loading support.
- Use "access_sdnctl_in_dword" instead of "hygon_dword_acess" for accessing the HDA registers.

Fu Hao (5):
x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models

    0x4 through 0x8

x86/cpu: Get LLC ID for Hygon processor models 0x6 through 0x8 x86/cpu/hygon: Remove Spectral Chicken for Hygon processors perf/x86/uncore: Add L3 PMU support for Hygon family 18h model 6h x86/microcode/hygon: Add microcode loading support for Hygon
processors

 arch/x86/Kconfig                         |   2 +-
 arch/x86/events/amd/uncore.c             |  48 +-
 arch/x86/include/asm/perf_event.h        |   8 +
 arch/x86/kernel/cpu/cacheinfo.c          |  21 +-
 arch/x86/kernel/cpu/hygon.c              |  20 +-
 arch/x86/kernel/cpu/microcode/Makefile   |   1 +
 arch/x86/kernel/cpu/microcode/core.c     |  17 +-
 arch/x86/kernel/cpu/microcode/hygon.c    | 851 +++++++++++++++++++++++
 arch/x86/kernel/cpu/microcode/internal.h |  20 +
 9 files changed, 976 insertions(+), 12 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/microcode/hygon.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v5 1/5 x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

For Hygon processor models 0x4–0x8, the die ID should be obtained from the NodeId field in ECX of CPUID leaf 0x8000001e.

Starting with these models, Hygon CPUs support CPUID leaf 0xb but do not support leaf 0x80000026. Consequently, the die ID and logical die ID cannot be derived from the APIC ID space. CPUID leaf 0xb only supports core and package levels, it cannot enumerate die IDs within a socket.

However, we found that CPUID leaf 0x8000001e (ECX) provides the necessary information. In Hygon CPU design, the NodeId field [7:0] encodes both Socket and Die IDs in the format:
Bits [7:4]: Socket ID
Bits [3:0]: Die ID

We can calculate the correct IDs(die_id and logical_die_id) by parsing this field.

---
 arch/x86/kernel/cpu/hygon.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/kernel/cpu/hygon.c +14 -0
index 3e8891a9c..64f9a64c3 100644
@@ -192,6 +192,20 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
 	init_hygon_cacheinfo(c);
 
+	/*
+	 * Adjust the die_id and logical_die_id for Hygon models 0x4-0x8.
+	 * In Hygon CPU design, the NodeId field of CPUID leaf 0x8000001e (ECX)
+	 * is now fixed to represent the socket ID and die ID, allocated as
+	 * 4 bits each: socket ID = NodeId[7:4], die ID = NodeId[3:0].
+	 */
+	if (!boot_cpu_has(X86_FEATURE_HYPERVISOR) &&
+	    c->x86_model >= 0x4 && c->x86_model <= 0x8) {
+		c->topo.die_id = cpuid_ecx(0x8000001e) & 0xff;
+		c->topo.logical_die_id = (c->topo.die_id >> 4) *
+					 topology_amd_nodes_per_pkg() +
+					 (c->topo.die_id & 0xf);
+	}
+
 	if (cpu_has(c, X86_FEATURE_SVM)) {
 		rdmsrq(MSR_VM_CR, vm_cr);
 		if (vm_cr & SVM_VM_CR_SVM_DIS_MASK) {
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v5 2/5 x86/cpu: Get LLC ID for Hygon processor models 0x6 through 0x8

In reply to Fu Hao

For Hygon processors, the LLC ID comes from fixed bit of APIC ID regardless of whether SMT is enabled or not. Add support for Hygon processor models 0x6–0x8.

---
 arch/x86/kernel/cpu/cacheinfo.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +18 -3
index 51a95b078..e563e327b 100644
@@ -342,10 +342,25 @@ void cacheinfo_hygon_init_llc_id(struct cpuinfo_x86 *c)
 		return;
 
 	/*
-	 * Hygons are similar to AMD Family 17h up to 1F models: LLC is
-	 * at the core complex level.  Core complex ID is ApicId[3].
+	 * LLC is at the core complex level. For Hygon processor
+	 * models 0x0–0x5, core complex ID is similar to AMD Family
+	 * 17h up to 1F models, which is ApicId[3]. For Hygon
+	 * processor models 0x6 and 0x7/0x8, core complex ID is
+	 * derived from APIC ID bit 4 and bit 5, respectively.
 	 */
-	c->topo.llc_id = c->topo.apicid >> 3;
+
+	switch (c->x86_model) {
+	case 0x6:
+		c->topo.llc_id = c->topo.apicid >> 4;
+		break;
+	case 0x7:
+	case 0x8:
+		c->topo.llc_id = c->topo.apicid >> 5;
+		break;
+	default:
+		c->topo.llc_id = c->topo.apicid >> 3;
+		break;
+	}
 }
 
 void init_amd_cacheinfo(struct cpuinfo_x86 *c)
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v5 3/5 x86/cpu/hygon: Remove Spectral Chicken for Hygon processors

In reply to Fu Hao

It's no need to set the Spectral chicken bit for all Hygon processors.

Commit d7caac991fee ("x86/cpu/amd: Add Spectral Chicken") added the spectral chicken bit for AMD platforms. There was an inquiry about whether Hygon platforms also require this chicken bit. Hygon has confirmed that it is not needed. Therefore, the related comment is removed in this patch.

---
 arch/x86/kernel/cpu/hygon.c | 6 ------
 1 file changed, 6 deletions(-)
arch/x86/kernel/cpu/hygon.c +0 -6
index 64f9a64c3..eea9e36a8 100644
@@ -177,12 +177,6 @@ static void init_hygon(struct cpuinfo_x86 *c)
 
 	set_cpu_cap(c, X86_FEATURE_REP_GOOD);
 
-	/*
-	 * XXX someone from Hygon needs to confirm this DTRT
-	 *
-	init_spectral_chicken(c);
-	 */
-
 	set_cpu_cap(c, X86_FEATURE_ZEN);
 	set_cpu_cap(c, X86_FEATURE_CPB);
 
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v5 4/5 perf/x86/uncore: Add L3 PMU support for Hygon family 18h model 6h

In reply to Fu Hao

Adjust the L3 PMU slicemask and threadmask for Hygon family 18h model 6h processor.

---
 arch/x86/events/amd/uncore.c      | 48 ++++++++++++++++++++++++++++++-
 arch/x86/include/asm/perf_event.h |  8 ++++++
 2 files changed, 55 insertions(+), 1 deletion(-)
arch/x86/events/amd/uncore.c +47 -1
index dbc00b6dd..fa7a1c70e 100644
@@ -309,6 +309,14 @@ amd_f17h_uncore_is_visible(struct kobject *kobj, struct attribute *attr, int i)
 	       attr->mode : 0;
 }
 
+static umode_t
+hygon_f18h_m6h_uncore_is_visible(struct kobject *kobj, struct attribute *attr, int i)
+{
+	return boot_cpu_data.x86 == 0x18 &&
+	       boot_cpu_data.x86_model >= 0x6 && boot_cpu_data.x86_model <= 0xf	?
+	       attr->mode : 0;
+}
+
 static umode_t
 amd_f19h_uncore_is_visible(struct kobject *kobj, struct attribute *attr, int i)
 {
@@ -360,6 +368,8 @@ DEFINE_UNCORE_FORMAT_ATTR(enallslices,	enallslices,	"config:46");		   /* F19h L3
 DEFINE_UNCORE_FORMAT_ATTR(enallcores,	enallcores,	"config:47");		   /* F19h L3 */
 DEFINE_UNCORE_FORMAT_ATTR(sliceid,	sliceid,	"config:48-50");	   /* F19h L3 */
 DEFINE_UNCORE_FORMAT_ATTR(rdwrmask,	rdwrmask,	"config:8-9");		   /* PerfMonV2 UMC */
+DEFINE_UNCORE_FORMAT_ATTR(slicemask4,	slicemask,	"config:28-31");	   /* F18h L3 */
+DEFINE_UNCORE_FORMAT_ATTR(threadmask32,	threadmask,	"config:32-63");	   /* F18h L3 */
 
 /* Common DF and NB attributes */
 static struct attribute *amd_uncore_df_format_attr[] = {
@@ -389,6 +399,12 @@ static struct attribute *amd_f17h_uncore_l3_format_attr[] = {
 	NULL,
 };
 
+/* F18h M06h unique L3 attributes */
+static struct attribute *hygon_f18h_m6h_uncore_l3_format_attr[] = {
+	&format_attr_slicemask4.attr,	/* slicemask */
+	NULL,
+};
+
 /* F19h unique L3 attributes */
 static struct attribute *amd_f19h_uncore_l3_format_attr[] = {
 	&format_attr_coreid.attr,	/* coreid */
@@ -414,6 +430,12 @@ static struct attribute_group amd_f17h_uncore_l3_format_group = {
 	.is_visible = amd_f17h_uncore_is_visible,
 };
 
+static struct attribute_group hygon_f18h_m6h_uncore_l3_format_group = {
+	.name = "format",
+	.attrs = hygon_f18h_m6h_uncore_l3_format_attr,
+	.is_visible = hygon_f18h_m6h_uncore_is_visible,
+};
+
 static struct attribute_group amd_f19h_uncore_l3_format_group = {
 	.name = "format",
 	.attrs = amd_f19h_uncore_l3_format_attr,
@@ -443,6 +465,11 @@ static const struct attribute_group *amd_uncore_l3_attr_update[] = {
 	NULL,
 };
 
+static const struct attribute_group *hygon_uncore_l3_attr_update[] = {
+	&hygon_f18h_m6h_uncore_l3_format_group,
+	NULL,
+};
+
 static const struct attribute_group *amd_uncore_umc_attr_groups[] = {
 	&amd_uncore_attr_group,
 	&amd_uncore_umc_format_group,
@@ -821,6 +848,12 @@ static int amd_uncore_l3_event_init(struct perf_event *event)
 		mask = AMD64_L3_F19H_THREAD_MASK | AMD64_L3_EN_ALL_SLICES |
 		       AMD64_L3_EN_ALL_CORES;
 
+	if (boot_cpu_data.x86 == 0x18 &&
+	    boot_cpu_data.x86_model >= 0x6 &&
+	    boot_cpu_data.x86_model <= 0xf)
+		mask = ((config & HYGON_L3_SLICE_MASK) ? : HYGON_L3_SLICE_MASK) |
+		       ((config & HYGON_L3_THREAD_MASK) ? : HYGON_L3_THREAD_MASK);
+
 	hwc->config |= mask;
 
 	return 0;
@@ -878,7 +911,8 @@ int amd_uncore_l3_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
 	pmu->rdpmc_base = RDPMC_BASE_LLC;
 	pmu->group = amd_uncore_ctx_gid(uncore, cpu);
 
-	if (boot_cpu_data.x86 >= 0x17) {
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
+	    boot_cpu_data.x86 >= 0x17) {
 		*l3_attr++ = &format_attr_event8.attr;
 		*l3_attr++ = &format_attr_umask8.attr;
 		*l3_attr++ = boot_cpu_data.x86 >= 0x19 ?
@@ -905,6 +939,18 @@ int amd_uncore_l3_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
 		.module		= THIS_MODULE,
 	};
 
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON &&
+	    boot_cpu_data.x86 == 0x18) {
+		*l3_attr++ = &format_attr_event8.attr;
+		*l3_attr++ = &format_attr_umask8.attr;
+		if (boot_cpu_data.x86_model >= 0x6 && boot_cpu_data.x86_model <= 0xf) {
+			*l3_attr++ = &format_attr_threadmask32.attr;
+			pmu->pmu.attr_update = hygon_uncore_l3_attr_update;
+		} else {
+			*l3_attr++ = &format_attr_threadmask8.attr;
+		}
+	}
+
 	if (perf_pmu_register(&pmu->pmu, pmu->pmu.name, -1)) {
 		free_percpu(pmu->ctx);
 		pmu->ctx = NULL;
arch/x86/include/asm/perf_event.h +8 -0
index 1eb13673e..f448e93ec 100644
@@ -91,6 +91,14 @@
 #define AMD64_L3_COREID_MASK				\
 	(0x7ULL << AMD64_L3_COREID_SHIFT)
 
+#define HYGON_L3_SLICE_SHIFT				28
+#define HYGON_L3_SLICE_MASK				\
+	(0xFULL << HYGON_L3_SLICE_SHIFT)
+
+#define HYGON_L3_THREAD_SHIFT				32
+#define HYGON_L3_THREAD_MASK				\
+	(0xFFFFFFFFULL << HYGON_L3_THREAD_SHIFT)
+
 #define X86_RAW_EVENT_MASK		\
 	(ARCH_PERFMON_EVENTSEL_EVENT |	\
 	 ARCH_PERFMON_EVENTSEL_UMASK |	\
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v5 4/5] perf/x86/uncore: Add L3 PMU support for Hygon family 18h model 6h

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Adjust the L3 PMU slicemask and threadmask for Hygon family 18h model 6h processor.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

Re: [PATCH v5 4/5] perf/x86/uncore: Add L3 PMU support for Hygon family 18h model 6h

In reply to Fu Hao
Quoted 10 lines · Fu Hao

Adjust the L3 PMU slicemask and threadmask for Hygon family 18h model 6h processor.

---
 arch/x86/events/amd/uncore.c      | 48 ++++++++++++++++++++++++++++++-
 arch/x86/include/asm/perf_event.h |  8 ++++++
 2 files changed, 55 insertions(+), 1 deletion(-)

There was a proposal to have separate uncore PMU drivers for AMD and Hygon. The latest version of that series can be found at
https://lore.kernel.org/lkml/[email protected]/

Can these changes be adapted to that?

author
#7 lore raw

Re: [PATCH v5 4/5] perf/x86/uncore: Add L3 PMU support for Hygon family 18h model 6h

Quoted 12 lines · Sandipan Das
Quoted 10 lines · Fu Hao

Adjust the L3 PMU slicemask and threadmask for Hygon family 18h model 6h processor.

---
  arch/x86/events/amd/uncore.c      | 48 ++++++++++++++++++++++++++++++-
  arch/x86/include/asm/perf_event.h |  8 ++++++
  2 files changed, 55 insertions(+), 1 deletion(-)
Quoted 5 lines

There was a proposal to have separate uncore PMU drivers for AMD and Hygon. The latest version of that series can be found at
https://lore.kernel.org/lkml/[email protected]/

Can these changes be adapted to that?

Hi Sandipan Das,

Thanks for your guidance. I will implement the Hygon L3 PMU support
on top of the Hygon uncore PMU driver (hygon_uncore.c) that is currently under discussion on the list, rather than growing the Hygon bits further inside the AMD uncore code. The goal is to finish the separation between AMD and Hygon for uncore PMU drivers.

Signature · 2 lines
Regards,
Fu Hao
author
#8 lore raw

PATCH v5 5/5 x86/microcode/hygon: Add microcode loading support for Hygon processors

In reply to Fu Hao

Add support for loading Hygon microcode.
There are two loading methods:
1. Loading from the initrd/builtin.
2. Late loading, which depends on the kernel configuration option CONFIG_MICROCODE_LATE_LOADING being enabled, and this option is usually disabled by default, so it's not recommended.

---
 arch/x86/Kconfig                         |   2 +-
 arch/x86/kernel/cpu/microcode/Makefile   |   1 +
 arch/x86/kernel/cpu/microcode/core.c     |  17 +-
 arch/x86/kernel/cpu/microcode/hygon.c    | 851 +++++++++++++++++++++++
 arch/x86/kernel/cpu/microcode/internal.h |  20 +
 5 files changed, 889 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/microcode/hygon.c
arch/x86/Kconfig +1 -1
index bdad90f21..a9245fa47 100644
@@ -1313,7 +1313,7 @@ config X86_REBOOTFIXUPS
 
 config MICROCODE
 	def_bool y
-	depends on CPU_SUP_AMD || CPU_SUP_INTEL
+	depends on CPU_SUP_AMD || CPU_SUP_INTEL || CPU_SUP_HYGON
 	select CRYPTO_LIB_SHA256 if CPU_SUP_AMD
 
 config MICROCODE_INITRD32
arch/x86/kernel/cpu/microcode/Makefile +1 -0
index 193d98b33..671d9a095 100644
@@ -3,3 +3,4 @@ microcode-y				:= core.o
 obj-$(CONFIG_MICROCODE)			+= microcode.o
 microcode-$(CONFIG_CPU_SUP_INTEL)	+= intel.o
 microcode-$(CONFIG_CPU_SUP_AMD)		+= amd.o
+microcode-$(CONFIG_CPU_SUP_HYGON)	+= hygon.o
arch/x86/kernel/cpu/microcode/core.c +16 -1
index 0dd0c7241..ed10266af 100644
@@ -194,6 +194,9 @@ void __init load_ucode_bsp(void)
 			return;
 		intel = false;
 		break;
+	case X86_VENDOR_HYGON:
+		intel = false;
+		break;
 
 	default:
 		return;
@@ -202,7 +205,11 @@ void __init load_ucode_bsp(void)
 	if (intel)
 		load_ucode_intel_bsp(&early_data);
 	else
-		load_ucode_amd_bsp(&early_data, cpuid_1_eax);
+		if (x86_cpuid_vendor() == X86_VENDOR_HYGON)
+			load_ucode_hygon_bsp(&early_data, cpuid_1_eax);
+		else
+			load_ucode_amd_bsp(&early_data, cpuid_1_eax);
+
 }
 
 void load_ucode_ap(void)
@@ -228,6 +235,9 @@ void load_ucode_ap(void)
 		if (x86_family(cpuid_1_eax) >= 0x10)
 			load_ucode_amd_ap(cpuid_1_eax);
 		break;
+	case X86_VENDOR_HYGON:
+		load_ucode_hygon_ap(cpuid_1_eax);
+		break;
 	default:
 		break;
 	}
@@ -287,6 +297,9 @@ static void reload_early_microcode(unsigned int cpu)
 		if (family >= 0x10)
 			reload_ucode_amd(cpu);
 		break;
+	case X86_VENDOR_HYGON:
+		reload_ucode_hygon(cpu);
+		break;
 	default:
 		break;
 	}
@@ -894,6 +907,8 @@ static int __init microcode_init(void)
 		microcode_ops = init_intel_microcode();
 	else if (c->x86_vendor == X86_VENDOR_AMD)
 		microcode_ops = init_amd_microcode();
+	else if (c->x86_vendor == X86_VENDOR_HYGON)
+		microcode_ops = init_hygon_microcode();
 	else
 		pr_err("no support for this CPU vendor\n");
 
arch/x86/kernel/cpu/microcode/hygon.c new file +851 -0

851 lines not on this page · raw message

arch/x86/kernel/cpu/microcode/internal.h +20 -0
index a10b547ed..f8b624255 100644
@@ -60,6 +60,9 @@ struct cpio_data find_microcode_in_initrd(const char *path);
 #define CPUID_AMD1 QCHAR('A', 'u', 't', 'h')
 #define CPUID_AMD2 QCHAR('e', 'n', 't', 'i')
 #define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D')
+#define CPUID_HYGON1 QCHAR('H', 'y', 'g', 'o')
+#define CPUID_HYGON2 QCHAR('n', 'G', 'e', 'n')
+#define CPUID_HYGON3 QCHAR('u', 'i', 'n', 'e')
 
 #define CPUID_IS(a, b, c, ebx, ecx, edx)	\
 		(!(((ebx) ^ (a)) | ((edx) ^ (b)) | ((ecx) ^ (c))))
@@ -86,6 +89,9 @@ static inline int x86_cpuid_vendor(void)
 	if (CPUID_IS(CPUID_AMD1, CPUID_AMD2, CPUID_AMD3, ebx, ecx, edx))
 		return X86_VENDOR_AMD;
 
+	if (CPUID_IS(CPUID_HYGON1, CPUID_HYGON2, CPUID_HYGON3, ebx, ecx, edx))
+		return X86_VENDOR_HYGON;
+
 	return X86_VENDOR_UNKNOWN;
 }
 
@@ -127,6 +133,20 @@ static inline void reload_ucode_intel(void) { }
 static inline struct microcode_ops *init_intel_microcode(void) { return NULL; }
 #endif  /* !CONFIG_CPU_SUP_INTEL */
 
+#ifdef CONFIG_CPU_SUP_HYGON
+void load_ucode_hygon_bsp(struct early_load_data *ed, unsigned int family);
+void load_ucode_hygon_ap(unsigned int family);
+void reload_ucode_hygon(unsigned int cpu);
+struct microcode_ops *init_hygon_microcode(void);
+void exit_hygon_microcode(void);
+#else /* CONFIG_CPU_SUP_HYGON */
+static inline void load_ucode_hygon_bsp(struct early_load_data *ed, unsigned int family) { }
+static inline void load_ucode_hygon_ap(unsigned int family) { }
+static inline void reload_ucode_hygon(unsigned int cpu) { }
+static inline struct microcode_ops *init_hygon_microcode(void) { return NULL; }
+static inline void exit_hygon_microcode(void) { }
+#endif /* !CONFIG_CPU_SUP_HYGON */
+
 #define ucode_dbg(fmt, ...)					\
 ({								\
 	if (IS_ENABLED(CONFIG_MICROCODE_DBG))			\
Signature · 1 line
2.34.1