Skip to contentSkip to conversation

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

PATCH Applied Fu Hao 6 participants 22 messages Jun 15 – Jun 29

Subsystems (6)
x86 Sound PCI perf events +2 more subsystems

v3 of 5 Applied Latest is v5 →

lore
Share & tools
lore

Fu Hao

author
#0 lore raw

v3 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.

5. New SB IOAPIC information support
The southbridge IOAPIC of Hygon's new-generation processors is located at 00:0b.0.

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 (7):
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
ALSA: hda: Add support for Hygon family 18h model 5h HD-Audio
ALSA: hda: Fix single byte writing issue for Hygon family 18h model 5h

 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              |  16 +-
 arch/x86/kernel/cpu/microcode/Makefile   |   1 +
 arch/x86/kernel/cpu/microcode/core.c     |  17 +-
 arch/x86/kernel/cpu/microcode/hygon.c    | 820 +++++++++++++++++++++++
 arch/x86/kernel/cpu/microcode/internal.h |  20 +
 sound/hda/controllers/intel.c            |  11 +
 sound/hda/core/controller.c              |  10 +-
 sound/hda/core/stream.c                  |  40 +-
 12 files changed, 991 insertions(+), 23 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/microcode/hygon.c
Signature · 1 line
2.34.1
author
#1 lore raw

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

The die ID should be read from the NodeId field of CPUID leaf 0x8000001e (ECX) for Hygon processor models 0x4–0x8.

---
 arch/x86/kernel/cpu/hygon.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/cpu/hygon.c +10 -0
index 7f95a74e4..0bf99072f 100644
@@ -191,6 +191,16 @@ 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.
+	 */
+	if (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
#2 lore raw

Re: [PATCH v3 1/7] x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

arch/x86/kernel/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 202

Quoted 9 lines · Fu Hao
arch/x86/kernel/cpu/hygon.c +9 -0
+	/*
+	 * Adjust the die_id and logical_die_id for Hygon models 0x4-0x8.
+	 */
+	if (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);
+	}

My assumption for Hygon stuff is that there's AMD code _somewhere_ for 99% of it. It's just a matter of using it.

Why doesn't this use parse_8000_001e()?

I think that kind of justification needs to be the baseline for Hygon work when it adds code. It needs to explain why the code is being added and why the existing (AMD usually) code doesn't work.

author
#3 lore raw

Re: [PATCH v3 1/7] x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

Quoted 14 lines · Dave Hansen

arch/x86/kernel/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 202

Quoted 9 lines · Fu Hao
arch/x86/kernel/cpu/hygon.c +9 -0
+	/*
+	 * Adjust the die_id and logical_die_id for Hygon models 0x4-0x8.
+	 */
+	if (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);
+	}

My assumption for Hygon stuff is that there's AMD code _somewhere_ for 99% of it. It's just a matter of using it.

Quoted 5 lines

Why doesn't this use parse_8000_001e()?

I think that kind of justification needs to be the baseline for Hygon work when it adds code. It needs to explain why the code is being added and why the existing (AMD usually) code doesn't work.

Hi Dave Hansen,

Thank you for the feedback. Let me clarify the situation regarding Hygon Family 18h Model 4h and newer.

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.

Regarding the reuse of parse_8000_001e():

1. On AMD, this function retrieves amd_node_id, which is an AMD-specific Node ID. We suspect this is kept for CDD compatibility, but its semantics differ from Hygon's requirements.
2. AMD derives its Die ID via leaf 0x80000026 (from APIC ID shifts), whereas Hygon must use the NodeId field from leaf 0x8000001e.

Due to these architectural divergences, the current implementation is inconsistent for Hygon. To ensure correct topology detection and to facilitate subsequent Node management​ and EDAC address translation, we must explicitly parse the NodeId field for Hygon instead of relying on amd_node_id.

Please let me know if this explanation clears up the rationale for the change.

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

Re: [PATCH v3 1/7] x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

In reply to Dave Hansen
Quoted 14 lines · Dave Hansen

arch/x86/kernel/cpu/hygon.c static void init_hygon(struct cpuinfo_x86 *c) · line 202

Quoted 9 lines · Fu Hao
arch/x86/kernel/cpu/hygon.c +9 -0
+	/*
+	 * Adjust the die_id and logical_die_id for Hygon models 0x4-0x8.
+	 */
+	if (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);
+	}

My assumption for Hygon stuff is that there's AMD code _somewhere_ for 99% of it. It's just a matter of using it.

Quoted 5 lines

Why doesn't this use parse_8000_001e()?

I think that kind of justification needs to be the baseline for Hygon work when it adds code. It needs to explain why the code is being added and why the existing (AMD usually) code doesn't work.

Hi Dave Hansen,

Thank you for the feedback. Let me clarify the situation regarding Hygon Family 18h Model 4h and newer.

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.

Regarding the reuse of parse_8000_001e():

1. On AMD, this function retrieves amd_node_id, which is an AMD-specific Node ID. We suspect this is kept for CDD compatibility, but its semantics differ from Hygon's requirements.
2. AMD derives its Die ID via leaf 0x80000026 (from APIC ID shifts), whereas Hygon must use the NodeId field from leaf 0x8000001e.

Due to these architectural divergences, the current implementation is inconsistent for Hygon. To ensure correct topology detection and to facilitate subsequent Node management​ and EDAC address translation, we must explicitly parse the NodeId field for Hygon instead of relying on amd_node_id.

Please let me know if this explanation clears up the rationale for the change.

Signature · 2 lines
Regards,
Fu Hao
#5 lore raw

Re: [PATCH v3 1/7] x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

Quoted 3 lines · Fu Hao

1. On AMD, this function retrieves amd_node_id, which is an AMD-specific Node ID. We suspect this is kept for CDD compatibility, but its semantics differ from Hygon's requirements.

What are those requirements?

author
#6 lore raw

Re: [PATCH v3 1/7] x86/cpu/hygon: Adjust the die_id and logical_die_id for Hygon models 0x4 through 0x8

Quoted 6 lines · Dave Hansen
Quoted 3 lines · Fu Hao

1. On AMD, this function retrieves amd_node_id, which is an AMD-specific Node ID. We suspect this is kept for CDD compatibility, but its semantics differ from Hygon's requirements.

What are those requirements?

We need to accurately obtain both the physical die ID and the logical die ID. It is important to note that physical die IDs may be non- contiguous​ in multi-socket interconnect products.

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].
This format diverges from AMD's definition of the same leaf. Since the bit allocation differs, we cannot reuse AMD's parsing logic.

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

PATCH v3 2/7 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
#8 lore raw

PATCH v3 3/7 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.

Fixes d7caac991fee ("x86/cpu/amd: Add Spectral Chicken")
---
 arch/x86/kernel/cpu/hygon.c | 6 ------
 1 file changed, 6 deletions(-)
arch/x86/kernel/cpu/hygon.c +0 -6
index 0bf99072f..d5513c0f1 100644
@@ -176,12 +176,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
#9 lore raw

PATCH v3 4/7 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 dd956cfca..e71d9e784 100644
@@ -308,6 +308,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)
 {
@@ -359,6 +367,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[] = {
@@ -388,6 +398,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 */
@@ -413,6 +429,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,
@@ -442,6 +464,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,
@@ -820,6 +847,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;
@@ -877,7 +910,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 ?
@@ -904,6 +938,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 ff5acb8b1..404752601 100644
@@ -89,6 +89,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
#10 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Registered PMUs are not unregistered if cpuhp_setup_state() fails during module initialization, leading to a Use-After-Free. --

commit 98d69c16b51701dce640f5ac92cee0daa35f2cfb
Author: Fu Hao <[email protected]>

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

This patch adds L3 PMU support for the Hygon family 18h model 6h processor by adjusting the slicemask and threadmask in the uncore driver.

Quoted 4 lines
arch/x86/events/amd/uncore.c +0 -0
index 9293ce50574da..21f00edd3caa6 100644

[ ... ]

Quoted 9 lines
arch/x86/events/amd/uncore.c +2 -1
@@ -877,7 +910,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;

[ ... ]

Quoted 5 lines
arch/x86/events/amd/uncore.c +5 -0
+	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) {

arch/x86/events/amd/uncore.c int amd_uncore_l3_ctx_init(struct amd_uncore *uncore, unsigned int cpu) · line 955

Quoted 10 lines
arch/x86/events/amd/uncore.c +7 -0
+			*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;

[Severity: High]
This is a pre-existing issue, but registering the PMU here exposes a teardown issue in the module initialization error path.

If cpuhp_setup_state() fails for the online state in amd_uncore_init(), the error path jumps to fail_start and removes the hotplug states. This triggers the amd_uncore_cpu_dead() teardown callback, which correctly frees the per-CPU context by calling amd_uncore_ctx_free().

However, the error path does not unregister the PMU or free the uncore->pmus array:

arch/x86/events/amd/uncore.c:amd_uncore_init() {
    ...
fail:
	for (i = 0; i < UNCORE_TYPE_MAX; i++) {
		uncore = &uncores[i];
		if (uncore->info) {
			free_percpu(uncore->info);
			uncore->info = NULL;
		}
	}

	return ret;
}

Since the initialization failed, the module will be unloaded, but the perf subsystem retains references to the registered PMUs.

Could this lead to a use-after-free if the perf subsystem later accesses these dangling PMUs (for instance, via perf stat) after the module memory has been freed?

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

PATCH v3 5/7 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    | 820 +++++++++++++++++++++++
 arch/x86/kernel/cpu/microcode/internal.h |  20 +
 5 files changed, 858 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/microcode/hygon.c
arch/x86/Kconfig +1 -1
index e2df1b147..b94f3dbf1 100644
@@ -1330,7 +1330,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 651202e6f..5f82f2436 100644
@@ -195,6 +195,9 @@ void __init load_ucode_bsp(void)
 			return;
 		intel = false;
 		break;
+	case X86_VENDOR_HYGON:
+		intel = false;
+		break;
 
 	default:
 		return;
@@ -203,7 +206,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)
@@ -229,6 +236,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;
 	}
@@ -288,6 +298,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;
 	}
@@ -895,6 +908,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 +820 -0

820 lines not on this page · raw message

arch/x86/kernel/cpu/microcode/internal.h +20 -0
index 3b93c0676..1dbb48651 100644
@@ -61,6 +61,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))))
@@ -87,6 +90,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;
 }
 
@@ -128,6 +134,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
#12 lore raw

Re: [PATCH v3 5/7] x86/microcode/hygon: Add microcode loading support for Hygon processors

Hi Fu Hao,

I came across a Sashiko AI code review [1] that flagged a potential out-of-bounds read in parse_and_cache_patches() in the new Hygon microcode loader.  I was able to reproduce it in QEMU with KASAN enabled.  The trigger is simple: feed a firmware container that has only an equivalence table (no patch sections), and the subsequent hygon_section_hdr() call reads past the buffer end.

Quoted 1 line · Fu Hao

Add support for loading Hygon microcode.

...

arch/x86/kernel/cpu/microcode/hygon.c line 669

Quoted 13 lines
arch/x86/kernel/cpu/microcode/hygon.c new file +13 -0
+static enum ucode_state parse_and_cache_patches(u8 family, const u8 *data, size_t size)
+{
+    u8 *fw = (u8 *)data;
+    size_t offset;
+
+    offset = load_equiv_cpu_table(data, size);
+    if (!offset)
+        return UCODE_ERROR;
+
+    fw   += offset;
+    size -= offset;
+
+    if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

If the container has only an equivalence table and no patch sections, load_equiv_cpu_table() returns an offset equal to the total buffer size.  Then size -= offset makes size == 0, and fw points past the buffer.  The call to hygon_section_hdr(fw, TYPE) then reads a 4-byte section type field from beyond the buffer boundary.

[Reproduction]

I rebuilt the kernel with CONFIG_CPU_SUP_HYGON=y and CONFIG_KASAN=y, and added a /proc/hygon_poc entry that calls parse_and_cache_patches() with a crafted firmware blob containing only an equivalence table (zero patch sections).  Writing any value to /proc/hygon_poc triggers the path.

PoC source (poc.c):
---8<---------------------------------------------------------------- /*
 * PoC: OOB read in parse_and_cache_patches() in hygon.c
 * Trigger: echo X > /proc/hygon_poc (or run this binary) */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    int fd = open("/proc/hygon_poc", O_WRONLY);
    if (fd < 0) { perror("open"); return 1; }
    write(fd, "X", 1);
    close(fd);
    printf("Done. Check dmesg.\n");
    return 0;
} ---8<----------------------------------------------------------------
Compile: gcc -o poc poc.c -static

[KASAN report — kernel 6.19.0-rc5, CONFIG_KASAN=y]

  ==================================================================

  BUG: KASAN: stack-out-of-bounds in parse_and_cache_patches.isra.0+0x805/0x8e0
  Read of size 4 at addr ffffc9002106fc74 by task poc/9404

  CPU: 1 PID: 9404 Comm: poc Tainted: G  W 6.19.0-rc5-gd3caa0b6e4ac-dirty #2

  Call Trace:
   <TASK>
   dump_stack_lvl+0x116/0x1f0
   print_report+0xcd/0x630
   kasan_report+0xe0/0x110
   parse_and_cache_patches.isra.0+0x805/0x8e0
   hygon_poc_write+0x97/0xd0
   proc_reg_write+0x245/0x340
   vfs_write+0x2a5/0x11e0
   ksys_write+0x12f/0x250
   do_syscall_64+0xcd/0xf80
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  The buggy address belongs to stack of task poc/9404

   and is located at offset 52 in frame:
   hygon_poc_write+0x0/0xd0
   This frame has 1 object: [32, 52) 'test_hdr'

The crash is at parse_and_cache_patches+0x805, which corresponds to the hygon_section_hdr(fw, TYPE) call reading the section type from beyond the buffer.  The stack object test_hdr at [32, 52) has size 20 bytes — the OOB read at offset 52 is exactly at the boundary.

[Suggested fix]

Add a bounds check before reading the section header:

  offset = load_equiv_cpu_table(data, size);
  if (!offset)
      return UCODE_ERROR;

  fw   += offset;
  size -= offset;

+ if (size < HYGON_SECTION_HDR_SIZE) {
+     pr_err("no patch sections in container\n"); +     return UCODE_ERROR;
+ }
+
  if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

This mirrors the pattern used in the AMD loader (parse_container() in amd.c), which checks size < 0 before accessing section headers.

The review also notes that kzalloc_obj in try_verify_and_cache_patch() does not appear to be a defined kernel macro, and hypervisor_present may not be declared — these are build-time issues separate from the runtime OOB above.

[1] https://sashiko.dev/#/patchset/cover.1781523812.git.fuhao%40open-hieco.net
    (Sashiko AI code review — "Out-of-Bounds Access", Severity: High)

Thanks,
XIAOWU

maintainer
#13 lore raw

Re: [PATCH v3 5/7] x86/microcode/hygon: Add microcode loading support for Hygon processors

Quoted 3 lines · XIAO WU

I rebuilt the kernel with CONFIG_CPU_SUP_HYGON=y and CONFIG_KASAN=y, and added a /proc/hygon_poc entry that calls parse_and_cache_patches() with a crafted firmware blob containing only an equivalence table

You do realize that being able to supply a crafted blob means, you already have root or you've managed to craft a supply chain attack of sorts...

Quoted 1 line

Add a bounds check before reading the section header:

But yes, checks are cheap and should be done whenever possible.

Quoted 4 lines

The review also notes that kzalloc_obj in try_verify_and_cache_patch() does not appear to be a defined kernel macro, and hypervisor_present may not be declared — these are build-time issues separate from the runtime OOB above.

Needless to say, all review feedback needs addressing/fixing.

So thanks.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#14 lore raw

Re: [PATCH v3 5/7] x86/microcode/hygon: Add microcode loading support for Hygon processors

Quoted 12 lines · Borislav Petkov
Quoted 3 lines · XIAO WU

I rebuilt the kernel with CONFIG_CPU_SUP_HYGON=y and CONFIG_KASAN=y, and added a /proc/hygon_poc entry that calls parse_and_cache_patches() with a crafted firmware blob containing only an equivalence table

You do realize that being able to supply a crafted blob means, you already have root or you've managed to craft a supply chain attack of sorts...

Quoted 1 line

Add a bounds check before reading the section header:

But yes, checks are cheap and should be done whenever possible.

Quoted 9 lines
Quoted 4 lines

The review also notes that kzalloc_obj in try_verify_and_cache_patch() does not appear to be a defined kernel macro, and hypervisor_present may not be declared — these are build-time issues separate from the runtime OOB above.

Needless to say, all review feedback needs addressing/fixing.

So thanks.

Hi Boris,
Thanks for your guidance. I will carefully review the feedback from
Sashiko AI code review, address the issues in v4 patch, and add more test cases to validate the changes.

Signature · 2 lines
Regards,
Fu Hao
#15 lore raw

Re: [PATCH v3 5/7] x86/microcode/hygon: Add microcode loading support for Hygon processors

In reply to Fu Hao

Hi Fu Hao,

I came across a Sashiko AI code review [1] that flagged a potential out-of-bounds read in parse_and_cache_patches() in the new Hygon microcode loader.  I was able to reproduce it in QEMU with KASAN enabled.  The trigger is simple: feed a firmware container that has only an equivalence table (no patch sections), and the subsequent hygon_section_hdr() call reads past the buffer end.

Quoted 1 line · Fu Hao

Add support for loading Hygon microcode.

...

arch/x86/kernel/cpu/microcode/hygon.c line 669

Quoted 13 lines
arch/x86/kernel/cpu/microcode/hygon.c new file +13 -0
+static enum ucode_state parse_and_cache_patches(u8 family, const u8 *data, size_t size)
+{
+    u8 *fw = (u8 *)data;
+    size_t offset;
+
+    offset = load_equiv_cpu_table(data, size);
+    if (!offset)
+        return UCODE_ERROR;
+
+    fw   += offset;
+    size -= offset;
+
+    if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

If the container has only an equivalence table and no patch sections, load_equiv_cpu_table() returns an offset equal to the total buffer size.  Then size -= offset makes size == 0, and fw points past the buffer.  The call to hygon_section_hdr(fw, TYPE) then reads a 4-byte section type field from beyond the buffer boundary.

[Reproduction]

I rebuilt the kernel with CONFIG_CPU_SUP_HYGON=y and CONFIG_KASAN=y, and added a /proc/hygon_poc entry that calls parse_and_cache_patches() with a crafted firmware blob containing only an equivalence table (zero patch sections).  Writing any value to /proc/hygon_poc triggers the path.

PoC source (poc.c):
---8<---------------------------------------------------------------- /*
 * PoC: OOB read in parse_and_cache_patches() in hygon.c
 * Trigger: echo X > /proc/hygon_poc (or run this binary) */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    int fd = open("/proc/hygon_poc", O_WRONLY);
    if (fd < 0) { perror("open"); return 1; }
    write(fd, "X", 1);
    close(fd);
    printf("Done. Check dmesg.\n");
    return 0;
} ---8<----------------------------------------------------------------
Compile: gcc -o poc poc.c -static

[KASAN report — kernel 6.19.0-rc5, CONFIG_KASAN=y]

  ==================================================================

  BUG: KASAN: stack-out-of-bounds in parse_and_cache_patches.isra.0+0x805/0x8e0
  Read of size 4 at addr ffffc9002106fc74 by task poc/9404

  CPU: 1 PID: 9404 Comm: poc Tainted: G  W 6.19.0-rc5-gd3caa0b6e4ac-dirty #2

  Call Trace:
   <TASK>
   dump_stack_lvl+0x116/0x1f0
   print_report+0xcd/0x630
   kasan_report+0xe0/0x110
   parse_and_cache_patches.isra.0+0x805/0x8e0
   hygon_poc_write+0x97/0xd0
   proc_reg_write+0x245/0x340
   vfs_write+0x2a5/0x11e0
   ksys_write+0x12f/0x250
   do_syscall_64+0xcd/0xf80
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  The buggy address belongs to stack of task poc/9404

   and is located at offset 52 in frame:
   hygon_poc_write+0x0/0xd0
   This frame has 1 object: [32, 52) 'test_hdr'

The crash is at parse_and_cache_patches+0x805, which corresponds to the hygon_section_hdr(fw, TYPE) call reading the section type from beyond the buffer.  The stack object test_hdr at [32, 52) has size 20 bytes — the OOB read at offset 52 is exactly at the boundary.

[Suggested fix]

Add a bounds check before reading the section header:

  offset = load_equiv_cpu_table(data, size);
  if (!offset)
      return UCODE_ERROR;

  fw   += offset;
  size -= offset;

+ if (size < HYGON_SECTION_HDR_SIZE) {
+     pr_err("no patch sections in container\n"); +     return UCODE_ERROR;
+ }
+
  if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

This mirrors the pattern used in the AMD loader (parse_container() in amd.c), which checks size < 0 before accessing section headers.

The review also notes that kzalloc_obj in try_verify_and_cache_patch() does not appear to be a defined kernel macro, and hypervisor_present may not be declared — these are build-time issues separate from the runtime OOB above.

[1] https://sashiko.dev/#/patchset/cover.1781523812.git.fuhao%40open-hieco.net
    (Sashiko AI code review — "Out-of-Bounds Access", Severity: High)

Thanks,
XIAOWU

author
#16 lore raw

Re: [PATCH v3 5/7] x86/microcode/hygon: Add microcode loading support for Hygon processors

Quoted 124 lines · XIAO WU

Hi Fu Hao,

I came across a Sashiko AI code review [1] that flagged a potential out-of-bounds read in parse_and_cache_patches() in the new Hygon microcode loader.  I was able to reproduce it in QEMU with KASAN enabled.  The trigger is simple: feed a firmware container that has only an equivalence table (no patch sections), and the subsequent hygon_section_hdr() call reads past the buffer end.

Quoted 1 line · Fu Hao

Add support for loading Hygon microcode.

...

Quoted 1 line

+static enum ucode_state parse_and_cache_patches(u8 family, const u8

*data, size_t size)

arch/x86/kernel/cpu/microcode/hygon.c line 669

Quoted 12 lines
arch/x86/kernel/cpu/microcode/hygon.c new file +12 -0
+{
+    u8 *fw = (u8 *)data;
+    size_t offset;
+
+    offset = load_equiv_cpu_table(data, size);
+    if (!offset)
+        return UCODE_ERROR;
+
+    fw   += offset;
+    size -= offset;
+
+    if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

If the container has only an equivalence table and no patch sections, load_equiv_cpu_table() returns an offset equal to the total buffer size.  Then size -= offset makes size == 0, and fw points past the buffer.  The call to hygon_section_hdr(fw, TYPE) then reads a 4-byte section type field from beyond the buffer boundary.

[Reproduction]

I rebuilt the kernel with CONFIG_CPU_SUP_HYGON=y and CONFIG_KASAN=y, and added a /proc/hygon_poc entry that calls parse_and_cache_patches() with a crafted firmware blob containing only an equivalence table (zero patch sections).  Writing any value to /proc/hygon_poc triggers the path.

PoC source (poc.c):
---8<---------------------------------------------------------------- /*
 * PoC: OOB read in parse_and_cache_patches() in hygon.c
 * Trigger: echo X > /proc/hygon_poc (or run this binary) */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    int fd = open("/proc/hygon_poc", O_WRONLY);
    if (fd < 0) { perror("open"); return 1; }
    write(fd, "X", 1);
    close(fd);
    printf("Done. Check dmesg.\n");
    return 0;
} ---8<----------------------------------------------------------------
Compile: gcc -o poc poc.c -static

[KASAN report — kernel 6.19.0-rc5, CONFIG_KASAN=y]

  ==================================================================

   BUG: KASAN: stack-out-of-bounds in 
parse_and_cache_patches.isra.0+0x805/0x8e0
   Read of size 4 at addr ffffc9002106fc74 by task poc/9404

   CPU: 1 PID: 9404 Comm: poc Tainted: G  W 6.19.0-rc5-gd3caa0b6e4ac- 

dirty #2

   Call Trace:
    <TASK>
    dump_stack_lvl+0x116/0x1f0
    print_report+0xcd/0x630
    kasan_report+0xe0/0x110
    parse_and_cache_patches.isra.0+0x805/0x8e0
    hygon_poc_write+0x97/0xd0
    proc_reg_write+0x245/0x340
    vfs_write+0x2a5/0x11e0
    ksys_write+0x12f/0x250
    do_syscall_64+0xcd/0xf80
    entry_SYSCALL_64_after_hwframe+0x77/0x7f

   The buggy address belongs to stack of task poc/9404
    and is located at offset 52 in frame:
    hygon_poc_write+0x0/0xd0
    This frame has 1 object: [32, 52) 'test_hdr'

The crash is at parse_and_cache_patches+0x805, which corresponds to the hygon_section_hdr(fw, TYPE) call reading the section type from beyond the buffer.  The stack object test_hdr at [32, 52) has size 20 bytes — the OOB read at offset 52 is exactly at the boundary.

[Suggested fix]

Add a bounds check before reading the section header:

  offset = load_equiv_cpu_table(data, size);
  if (!offset)
      return UCODE_ERROR;

  fw   += offset;
  size -= offset;

+ if (size < HYGON_SECTION_HDR_SIZE) {
+     pr_err("no patch sections in container\n"); +     return UCODE_ERROR;
+ }
+
  if (hygon_section_hdr(fw, TYPE) != UCODE_UCODE_TYPE) {

This mirrors the pattern used in the AMD loader (parse_container() in amd.c), which checks size < 0 before accessing section headers.

The review also notes that kzalloc_obj in try_verify_and_cache_patch() does not appear to be a defined kernel macro, and hypervisor_present may not be declared — these are build-time issues separate from the runtime OOB above.

Quoted 5 lines

[1] https://sashiko.dev/#/patchset/cover.1781523812.git.fuhao%40open- hieco.net (Sashiko AI code review — "Out-of-Bounds Access", Severity: High)

Thanks,
XIAOWU

Hi Xiao Wu,

Thank you for your review and suggestions. You are right, this could
indeed cause issues. I will fix it in v4 and add more comprehensive tests to improve coverage.

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

PATCH v3 6/7 ALSA: hda: Add support for Hygon family 18h model 5h HD-Audio

In reply to Fu Hao

Add the new PCI ID 0x1d94 0x14a9 for Hygon family 18h model 5h HDA controller.

---
 sound/hda/controllers/intel.c | 7 +++++++
 1 file changed, 7 insertions(+)
sound/hda/controllers/intel.c +7 -0
index 3f434994c..0c3f72170 100644
@@ -100,6 +100,8 @@ enum {
 #define ATIHDMI_NUM_CAPTURE	0
 #define ATIHDMI_NUM_PLAYBACK	8
 
+/* Hygon HD Audio controller */
+#define PCI_DEVICE_ID_HYGON_18H_M05H_HDA	0x14a9
 
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
@@ -241,6 +243,7 @@ enum {
 	AZX_DRIVER_ZHAOXIN,
 	AZX_DRIVER_ZHAOXINHDMI,
 	AZX_DRIVER_LOONGSON,
+	AZX_DRIVER_HYGON,
 	AZX_DRIVER_GENERIC,
 	AZX_NUM_DRIVERS, /* keep this as last entry */
 };
@@ -357,6 +360,7 @@ static const char * const driver_short_names[] = {
 	[AZX_DRIVER_ZHAOXIN] = "HDA Zhaoxin",
 	[AZX_DRIVER_ZHAOXINHDMI] = "HDA Zhaoxin HDMI",
 	[AZX_DRIVER_LOONGSON] = "HDA Loongson",
+	[AZX_DRIVER_HYGON] = "HDA Hygon",
 	[AZX_DRIVER_GENERIC] = "HD-Audio Generic",
 };
 
@@ -2818,6 +2822,9 @@ static const struct pci_device_id azx_ids[] = {
 	  .driver_data = AZX_DRIVER_LOONGSON | AZX_DCAPS_NO_TCSEL },
 	{ PCI_VDEVICE(LOONGSON, PCI_DEVICE_ID_LOONGSON_HDMI),
 	  .driver_data = AZX_DRIVER_LOONGSON | AZX_DCAPS_NO_TCSEL },
+	/* Hygon HDAudio */
+	{ PCI_VDEVICE(HYGON, PCI_DEVICE_ID_HYGON_18H_M05H_HDA),
+	  .driver_data = AZX_DRIVER_HYGON | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_NO_MSI },
 	{ 0, }
 };
 MODULE_DEVICE_TABLE(pci, azx_ids);
Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH v3 6/7] ALSA: hda: Add support for Hygon family 18h model 5h HD-Audio

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The new Hygon HD-Audio controller support misses critical AMD/Zen architectural quirks…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The new Hygon HD-Audio controller support misses critical AMD/Zen architectural quirks, specifically 40-bit DMA mask clamping and the AZX_DCAPS_NO_TCSEL capability flag. --

commit b159eff305dbdd80947b5dde567df2ff7e67de70
Author: Fu Hao <[email protected]>

ALSA: hda: Add support for Hygon family 18h model 5h HD-Audio

This commit adds the PCI ID 0x1d94 0x14a9 to support the Hygon family 18h model 5h HDA controller.

Quoted 4 lines
sound/hda/controllers/intel.c +0 -0
index 1e8e3d61291a1..e706d38d7fd6c 100644

[ ... ]

sound/hda/controllers/intel.c static const struct pci_device_id azx_ids[] = { · line 2824

Quoted 7 lines
sound/hda/controllers/intel.c +3 -0
@@ -2815,6 +2819,9 @@ static const struct pci_device_id azx_ids[] = {
 	  .driver_data = AZX_DRIVER_LOONGSON | AZX_DCAPS_NO_TCSEL },
 	{ PCI_VDEVICE(LOONGSON, PCI_DEVICE_ID_LOONGSON_HDMI),
 	  .driver_data = AZX_DRIVER_LOONGSON | AZX_DCAPS_NO_TCSEL },
+	/* Hygon HDAudio */
+	{ PCI_VDEVICE(HYGON, PCI_DEVICE_ID_HYGON_18H_M05H_HDA),
+	  .driver_data = AZX_DRIVER_HYGON | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_NO_MSI },

[Severity: High]
Should this capabilities bitmask include the AZX_DCAPS_NO_TCSEL flag?

Hygon processors are based on the AMD Zen architecture. Without this flag, azx_init_pci() will inadvertently write to the Intel-specific Traffic Class Select register at PCI offset 0x44:

sound/hda/controllers/intel.c:azx_init_pci() {
    ...
    if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
        dev_dbg(chip->card->dev, "Clearing TCSEL\n");
        update_pci_byte(chip->pci, AZX_PCIREG_TCSEL, 0x07, 0);
    }
    ...
}

On AMD/Hygon hardware, offset 0x44 is not TCSEL and modifying it could cause unintended behavior or system instability.

[Severity: High]
Does this new Hygon device also need the 40-bit DMA mask clamping that is applied to AMD devices?

In azx_first_init(), AMD devices have their DMA bits explicitly restricted:

sound/hda/controllers/intel.c:azx_first_init() {
    ...
    if (chip->pci->vendor == PCI_VENDOR_ID_AMD)
        dma_bits = 40;
    ...
}

Since Hygon shares hardware constraints with AMD Zen, leaving the DMA bits at 64 could lead to physical address truncation or IOMMU DMAR faults on systems with sufficient memory.

Quoted 2 lines
sound/hda/controllers/intel.c +0 -0
 	{ 0, }
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Applied
#19 lore raw

Re: [PATCH v3 6/7] ALSA: hda: Add support for Hygon family 18h model 5h HD-Audio

In reply to Fu Hao
Quoted 5 lines · Fu Hao

Add the new PCI ID 0x1d94 0x14a9 for Hygon family 18h model 5h HDA controller.

Applied to for-next branch now.

thanks,

Takashi

author
#20 lore raw

PATCH v3 7/7 ALSA: hda: Fix single byte writing issue for Hygon family 18h model 5h

In reply to Fu Hao

On Hygon family 18h model 5h controller, some registers such as GCTL, SD_CTL and SD_CTL_3B should be accessed in dword, or the writing will fail.

---
 sound/hda/controllers/intel.c |  4 ++++
 sound/hda/core/controller.c   | 10 +++++++--
 sound/hda/core/stream.c       | 40 +++++++++++++++++++++++++++--------
 3 files changed, 43 insertions(+), 11 deletions(-)
sound/hda/controllers/intel.c +4 -0
index 0c3f72170..8afa6b1d6 100644
@@ -1887,6 +1887,10 @@ static int azx_first_init(struct azx *chip)
 	if (chip->driver_type == AZX_DRIVER_ZHAOXINHDMI)
 		bus->polling_mode = 1;
 
+	if (chip->driver_type == AZX_DRIVER_HYGON &&
+	    chip->pci->device == PCI_DEVICE_ID_HYGON_18H_M05H_HDA)
+		bus->access_sdnctl_in_dword = 1;
+
 	bus->remap_addr = pcim_iomap_region(pci, 0, "ICH HD audio");
 	if (IS_ERR(bus->remap_addr))
 		return PTR_ERR(bus->remap_addr);
sound/hda/core/controller.c +8 -2
index 69e11d62b..6312ad7af 100644
@@ -511,7 +511,10 @@ void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus)
 {
 	unsigned long timeout;
 
-	snd_hdac_chip_updateb(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
+	if (bus->access_sdnctl_in_dword)
+		snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
+	else
+		snd_hdac_chip_updateb(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
 
 	timeout = jiffies + msecs_to_jiffies(100);
 	while (!snd_hdac_chip_readb(bus, GCTL) && time_before(jiffies, timeout))
@@ -576,7 +579,10 @@ static void azx_int_disable(struct hdac_bus *bus)
 
 	/* disable interrupts in stream descriptor */
 	list_for_each_entry(azx_dev, &bus->stream_list, list)
-		snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
+		if (bus->access_sdnctl_in_dword)
+			snd_hdac_stream_updatel(azx_dev, SD_CTL, SD_INT_MASK, 0);
+		else
+			snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
 
 	/* disable SIE for all streams & disable controller CIE and GIE */
 	snd_hdac_chip_writel(bus, INTCTL, 0);
sound/hda/core/stream.c +31 -9
index b471a038b..0091c4ef6 100644
@@ -146,8 +146,12 @@ void snd_hdac_stream_start(struct hdac_stream *azx_dev)
 			stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
 		else
 			stripe_ctl = 0;
-		snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
-					stripe_ctl);
+		if (bus->access_sdnctl_in_dword)
+			snd_hdac_stream_updatel(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
+						stripe_ctl);
+		else
+			snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
+						stripe_ctl);
 	}
 	/* set DMA start and interrupt mask */
 	if (bus->access_sdnctl_in_dword)
@@ -166,11 +170,22 @@ EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
  */
 static void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
 {
-	snd_hdac_stream_updateb(azx_dev, SD_CTL,
-				SD_CTL_DMA_START | SD_INT_MASK, 0);
-	snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
-	if (azx_dev->stripe)
-		snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
+	struct hdac_bus *bus = azx_dev->bus;
+
+	if (bus->access_sdnctl_in_dword) {
+		snd_hdac_stream_updatel(azx_dev, SD_CTL,
+					SD_CTL_DMA_START | SD_INT_MASK, 0);
+		snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
+		if (azx_dev->stripe)
+			snd_hdac_stream_updatel(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
+	} else {
+		snd_hdac_stream_updateb(azx_dev, SD_CTL,
+					SD_CTL_DMA_START | SD_INT_MASK, 0);
+		snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
+		if (azx_dev->stripe)
+			snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
+	}
+
 	azx_dev->running = false;
 }
 
@@ -225,12 +240,16 @@ void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
 {
 	unsigned char val;
 	int dma_run_state;
+	struct hdac_bus *bus = azx_dev->bus;
 
 	snd_hdac_stream_clear(azx_dev);
 
 	dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START;
 
-	snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
+	if (bus->access_sdnctl_in_dword)
+		snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
+	else
+		snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
 
 	/* wait for hardware to report that the stream entered reset */
 	snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300);
@@ -238,7 +257,10 @@ void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
 	if (azx_dev->bus->dma_stop_delay && dma_run_state)
 		udelay(azx_dev->bus->dma_stop_delay);
 
-	snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
+	if (bus->access_sdnctl_in_dword)
+		snd_hdac_stream_updatel(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
+	else
+		snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
 
 	/* wait for hardware to report that the stream is out of reset */
 	snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300);
Signature · 1 line
2.34.1
Applied
#21 lore raw

Re: [PATCH v3 7/7] ALSA: hda: Fix single byte writing issue for Hygon family 18h model 5h

Quoted 6 lines · Fu Hao

On Hygon family 18h model 5h controller, some registers such as GCTL, SD_CTL and SD_CTL_3B should be accessed in dword, or the writing will fail.

Applied to for-next branch now.

thanks,

Takashi