Enable TOPOEXT to support hyperthreading on AMD CPU
v3 cover Enable TOPOEXT to support hyperthreading on AMD CPU
This series enables the TOPOEXT feature for AMD CPUs. This is required to support hyperthreading on kvm guests.
This addresses the issues reported in these bugs:
https://bugzilla.redhat.com/show_bug.cgi?id=1481253
https://bugs.launchpad.net/qemu/+bug/1703506
v3:
1.Removed the patch #1. Radim mentioned that original typo problem is in
linux kernel header. qemu is just copying those files.
2.In previous version, I used the cpuid 4 definitions for AMDs cpuid leaf
0x8000001D. CPUID 4 is very intel specific and we dont want to expose those details under AMD. I have renamed some of these definitions as generic. These changes are in patch#1. Radim, let me know if this is what you intended.
3.Added assert to for core_id(Suggested by Radim Krčmář).
4.Changed the if condition under "L3 cache info"(Suggested by Gary Hook). 5.Addressed few more text correction and code cleanup(Suggested by Thomas Lendacky).
v2:
Fixed few more minor issues per Gary Hook's comments. Thank you Gary. Removed the patch#1. We need to handle the instruction cache associativity seperately. It varies based on the cpu family. I will comeback to that later. Added two more typo corrections in patch#1 and patch#5.
v1:
Stanislav Lanci posted few patches earlier.
https://patchwork.kernel.org/patch/10040903/
Rebased his patches with few changes.
1.Spit the patches into two, separating cpuid functions
0x8000001D and 0x8000001E (Patch 2 and 3).
2.Removed the generic non-intel check and made a separate patch
with some changes(Patch 5).
3.Fixed L3_N_SETS_AMD(from 4096 to 8192) based on CPUID_Fn8000001D_ECX_x03.
Added 2 more patches.
Patch 1. Fixes cache associativity.
Patch 4. Adds TOPOEXT feature on AMD EPYC CPU.
Babu Moger (3):
target/i386: Generalize some of the macro definitions target/i386: Enable TOPOEXT feature on AMD EPYC CPU target/i386: Remove generic SMT thread check
Stanislav Lanci (2):
target/i386: Populate AMD Processor Cache Information target/i386: Add support for CPUID_8000_001E for AMD
target/i386/cpu.c | 157 ++++++++++++++++++++++++++++++++++++++++++------------ target/i386/kvm.c | 29 ++++++++-- 2 files changed, 149 insertions(+), 37 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v3 1/5 target/i386: Generalize some of the macro definitions
Generalize some of the macro definitions which are generic cache properties that are common between CPUID 4 and CPUID 0x8000001D in preparation for adding support for 0x8000001D.
--- target/i386/cpu.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-)
target/i386/cpu.c +26 -26
@@ -66,22 +66,22 @@#define CPUID_2_L3_16MB_16WAY_64B 0x4d -/* CPUID Leaf 4 constants: */ +/* Cache specific constants: */ /* EAX: */ -#define CPUID_4_TYPE_DCACHE 1 -#define CPUID_4_TYPE_ICACHE 2 -#define CPUID_4_TYPE_UNIFIED 3 +#define TYPE_DCACHE 1 +#define TYPE_ICACHE 2 +#define TYPE_UNIFIED 3 -#define CPUID_4_LEVEL (l) ((l) << 5) +#define CACHE_LEVEL (l) ((l) << 5) -#define CPUID_4_SELF_INIT_LEVEL (1 << 8) -#define CPUID_4_FULLY_ASSOC (1 << 9) +#define CACHE_SELF_INIT_LEVEL (1 << 8) +#define CACHE_FULLY_ASSOC (1 << 9)/* EDX: */ -#define CPUID_4_NO_INVD_SHARING (1 << 0) -#define CPUID_4_INCLUSIVE (1 << 1) -#define CPUID_4_COMPLEX_IDX (1 << 2) +#define CACHE_NO_INVD_SHARING (1 << 0) +#define CACHE_INCLUSIVE (1 << 1) +#define CACHE_COMPLEX_IDX (1 << 2)#define ASSOC_FULL 0xFF
@@ -3273,29 +3273,29 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *eax =0 ;switch (count) {case 0 :/* L1 dcache info */ - *eax |=CPUID_4_TYPE_DCACHE | \ -CPUID_4_LEVEL (1 ) | \ -CPUID_4_SELF_INIT_LEVEL ; + *eax |=TYPE_DCACHE | \ +CACHE_LEVEL (1 ) | \ +CACHE_SELF_INIT_LEVEL ; *ebx = (L1D_LINE_SIZE -1 ) | \ ((L1D_PARTITIONS -1 ) <<12 ) | \ ((L1D_ASSOCIATIVITY -1 ) <<22 ); *ecx =L1D_SETS -1 ; - *edx =CPUID_4_NO_INVD_SHARING ; + *edx =CACHE_NO_INVD_SHARING ;break ;case 1 :/* L1 icache info */ - *eax |=CPUID_4_TYPE_ICACHE | \ -CPUID_4_LEVEL (1 ) | \ -CPUID_4_SELF_INIT_LEVEL ; + *eax |=TYPE_ICACHE | \ +CACHE_LEVEL (1 ) | \ +CACHE_SELF_INIT_LEVEL ; *ebx = (L1I_LINE_SIZE -1 ) | \ ((L1I_PARTITIONS -1 ) <<12 ) | \ ((L1I_ASSOCIATIVITY -1 ) <<22 ); *ecx =L1I_SETS -1 ; - *edx =CPUID_4_NO_INVD_SHARING ; + *edx =CACHE_NO_INVD_SHARING ;break ;case 2 :/* L2 cache info */ - *eax |=CPUID_4_TYPE_UNIFIED | \ -CPUID_4_LEVEL (2 ) | \ -CPUID_4_SELF_INIT_LEVEL ; + *eax |=TYPE_UNIFIED | \ +CACHE_LEVEL (2 ) | \ +CACHE_SELF_INIT_LEVEL ;if (cs->nr_threads >1 ) { *eax |= (cs->nr_threads -1 ) <<14 ; }
@@ -3303,7 +3303,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, ((L2_PARTITIONS -1 ) <<12 ) | \ ((L2_ASSOCIATIVITY -1 ) <<22 ); *ecx =L2_SETS -1 ; - *edx =CPUID_4_NO_INVD_SHARING ; + *edx =CACHE_NO_INVD_SHARING ;break ;case 3 :/* L3 cache info */ if (!cpu->enable_l3_cache) {
@@ -3313,16 +3313,16 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ;break ; } - *eax |=CPUID_4_TYPE_UNIFIED | \ -CPUID_4_LEVEL (3 ) | \ -CPUID_4_SELF_INIT_LEVEL ; + *eax |=TYPE_UNIFIED | \ +CACHE_LEVEL (3 ) | \ +CACHE_SELF_INIT_LEVEL ; pkg_offset =apicid_pkg_offset (cs->nr_cores, cs->nr_threads); *eax |= ((1 << pkg_offset) -1 ) <<14 ; *ebx = (L3_N_LINE_SIZE -1 ) | \ ((L3_N_PARTITIONS -1 ) <<12 ) | \ ((L3_N_ASSOCIATIVITY -1 ) <<22 ); *ecx =L3_N_SETS -1 ; - *edx =CPUID_4_INCLUSIVE |CPUID_4_COMPLEX_IDX ; + *edx =CACHE_INCLUSIVE |CACHE_COMPLEX_IDX ;break ;default :/* end of info */ *eax =0 ;
Signature · 1 line
1.8.3.1
PATCH v3 2/5 target/i386: Populate AMD Processor Cache Information
In reply to Babu MogerFrom: Stanislav Lanci <[email protected]>
Add information for cpuid 0x8000001D leaf. Populate cache topology information for different cache types(Data Cache, Instruction Cache, L2 and L3) supported by 0x8000001D leaf. Please refer Processor Programming Reference (PPR) for AMD Family 17h Model for more details.
--- target/i386/cpu.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ target/i386/kvm.c | 29 ++++++++++++++++++--- 2 files changed, 102 insertions(+), 3 deletions(-)
target/i386/cpu.c +76 -0
@@ -118,6 +118,7 @@#define L1I_LINE_SIZE 64#define L1I_ASSOCIATIVITY 8#define L1I_SETS 64 +#define L1I_SETS_AMD 256#define L1I_PARTITIONS 1/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 32KiB */ #define L1I_DESCRIPTOR CPUID_2_L1I_32KB_8WAY_64B
@@ -129,7 +130,9 @@/* Level 2 unified cache: */ #define L2_LINE_SIZE 64#define L2_ASSOCIATIVITY 16 +#define L2_ASSOCIATIVITY_AMD 8#define L2_SETS 4096 +#define L2_SETS_AMD 1024#define L2_PARTITIONS 1/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 4MiB */ /*FIXME: CPUID leaf 2 descriptor is inconsistent with CPUID leaf 4 */
@@ -146,6 +149,7 @@#define L3_N_LINE_SIZE 64#define L3_N_ASSOCIATIVITY 16#define L3_N_SETS 16384 +#define L3_N_SETS_AMD 8192#define L3_N_PARTITIONS 1#define L3_N_DESCRIPTOR CPUID_2_L3_16MB_16WAY_64B#define L3_N_LINES_PER_TAG 1
@@ -3590,6 +3594,78 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ; }break ; +case 0x8000001D :/* AMD TOPOEXT cache info */ +if (cpu->cache_info_passthrough) { +host_cpuid (index, count, eax, ebx, ecx, edx); +break ; + }else if (env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) { + *eax =0 ; +switch (count) { +case 0 :/* L1 dcache info */ + *eax |=TYPE_DCACHE | \ +CACHE_LEVEL (1 ) | \ +CACHE_SELF_INIT_LEVEL | \ + ((cs->nr_threads -1 ) <<14 ); + *ebx = (L1D_LINE_SIZE -1 ) | \ + ((L1D_PARTITIONS -1 ) <<12 ) | \ + ((L1D_ASSOCIATIVITY -1 ) <<22 ); + *ecx =L1D_SETS -1 ; + *edx =0 ; +break ; +case 1 :/* L1 icache info */ + *eax |=TYPE_ICACHE | \ +CACHE_LEVEL (1 ) | \ +CACHE_SELF_INIT_LEVEL | \ + ((cs->nr_threads -1 ) <<14 ); + *ebx = (L1I_LINE_SIZE -1 ) | \ + ((L1I_PARTITIONS -1 ) <<12 ) | \ + ((L1I_ASSOCIATIVITY_AMD -1 ) <<22 ); + *ecx =L1I_SETS_AMD -1 ; + *edx =0 ; +break ; +case 2 :/* L2 cache info */ + *eax |=TYPE_UNIFIED | \ +CACHE_LEVEL (2 ) | \ +CACHE_SELF_INIT_LEVEL | \ + ((cs->nr_threads -1 ) <<14 ); + *ebx = (L2_LINE_SIZE -1 ) | \ + ((L2_PARTITIONS -1 ) <<12 ) | \ + ((L2_ASSOCIATIVITY_AMD -1 ) <<22 ); + *ecx =L2_SETS_AMD -1 ; + *edx =CACHE_INCLUSIVE ; +break ; +case 3 :/* L3 cache info */ +if (cpu->enable_l3_cache) { + *eax |=TYPE_UNIFIED | \ +CACHE_LEVEL (3 ) | \ +CACHE_SELF_INIT_LEVEL | \ + ((cs->nr_cores * cs->nr_threads -1 ) <<14 ); + *ebx = (L3_N_LINE_SIZE -1 ) | \ + ((L3_N_PARTITIONS -1 ) <<12 ) | \ + ((L3_N_ASSOCIATIVITY -1 ) <<22 ); + *ecx =L3_N_SETS_AMD -1 ; + *edx =CACHE_NO_INVD_SHARING ; + }else { + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + } +break ; +default :/* end of info */ + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; +break ; + } + }else { + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + } +break ;case 0xC0000000 : *eax = env->cpuid_xlevel2; *ebx =0 ;
target/i386/kvm.c +26 -3
@@ -909,9 +909,32 @@ int kvm_arch_init_vcpu(CPUState *cs) } c = &cpuid_data.entries[cpuid_i++]; - c->function = i; - c->flags =0 ; -cpu_x86_cpuid (env, i,0 , &c->eax, &c->ebx, &c->ecx, &c->edx); +switch (i) { +case 0x8000001d : +/* Query for all AMD cache information leaves */ +for (j =0 ; ; j++) { + c->function = i; + c->flags =KVM_CPUID_FLAG_SIGNIFCANT_INDEX ; + c->index = j; +cpu_x86_cpuid (env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx); + +if (c->eax ==0 ) { +break ; + } +if (cpuid_i ==KVM_MAX_CPUID_ENTRIES ) { +fprintf (stderr,"cpuid_data is full, no space for " +"cpuid(eax:0x%x,ecx:0x%x)\n" , i, j); +abort (); + } + c = &cpuid_data.entries[cpuid_i++]; + } +break ; +default : + c->function = i; + c->flags =0 ; +cpu_x86_cpuid (env, i,0 , &c->eax, &c->ebx, &c->ecx, &c->edx); +break ; + } }/* Call Centaur's CPUID instructions they are supported. */
Signature · 1 line
1.8.3.1
PATCH v3 3/5 target/i386: Add support for CPUID_8000_001E for AMD
In reply to Babu MogerFrom: Stanislav Lanci <[email protected]>
Populate threads/core_id/apic_ids/socket_id when CPUID_EXT3_TOPOEXT feature is supported. This is required to support hyperthreading feature on AMD CPUs. This is supported via CPUID_8000_001E extended functions.
--- target/i386/cpu.c | 9 +++++++++ 1 file changed, 9 insertions(+)
target/i386/cpu.c +9 -0
@@ -3666,6 +3666,15 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ; }break ; +case 0x8000001E : +if (env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) { +assert (cpu->core_id <=255 ); + *eax = cpu->apic_id; + *ebx = (cs->nr_threads -1 ) <<8 | cpu->core_id; + *ecx = cpu->socket_id; + *edx =0 ; + } +break ;case 0xC0000000 : *eax = env->cpuid_xlevel2; *ebx =0 ;
Signature · 1 line
1.8.3.1
PATCH v3 4/5 target/i386: Enable TOPOEXT feature on AMD EPYC CPU
In reply to Babu MogerEnable TOPOEXT feature on EPYC CPU. This is required to support hyperthreading on VM guests. Also extend xlevel to 0x8000001E.
--- target/i386/cpu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
target/i386/cpu.c +3 -2
@@ -1955,7 +1955,8 @@ static X86CPUDefinition builtin_x86_defs[] = { .features[FEAT_8000_0001_ECX] =CPUID_EXT3_OSVW |CPUID_EXT3_3DNOWPREFETCH |CPUID_EXT3_MISALIGNSSE |CPUID_EXT3_SSE4A |CPUID_EXT3_ABM | - CPUID_EXT3_CR8LEG | CPUID_EXT3_SVM | CPUID_EXT3_LAHF_LM, +CPUID_EXT3_CR8LEG |CPUID_EXT3_SVM |CPUID_EXT3_LAHF_LM | + CPUID_EXT3_TOPOEXT, .features[FEAT_7_0_EBX ] =CPUID_7_0_EBX_FSGSBASE |CPUID_7_0_EBX_BMI1 |CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_RDSEED |
@@ -1970,7 +1971,7 @@ static X86CPUDefinition builtin_x86_defs[] = { CPUID_XSAVE_XGETBV1, .features[FEAT_6_EAX ] = CPUID_6_EAX_ARAT, - .xlevel = 0x8000000A, + .xlevel = 0x8000001E, .model_id = "AMD EPYC Processor", }, {
Signature · 1 line
1.8.3.1
PATCH v3 5/5 target/i386: Remove generic SMT thread check
In reply to Babu MogerRemove generic non-intel check while validating hyperthreading support. Certain AMD CPUs can support hyperthreading now.
CPU family with TOPOEXT feature can support hyperthreading now.
--- target/i386/cpu.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
target/i386/cpu.c +9 -6
@@ -4337,17 +4337,20 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)qemu_init_vcpu (cs); -/* Only Intel CPUs support hyperthreading. Even though QEMU fixes this -* issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX -* based on inputs (sockets,cores,threads), it is still better to gives +/* Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU +* fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX +* based on inputs (sockets,cores,threads), it is still better to give * users a warning. * * NOTE: the following code has to follow qemu_init_vcpu(). Otherwise * cs->nr_threads hasn't be populated yet and the checking is incorrect. */ -if (!IS_INTEL_CPU (env) && cs->nr_threads >1 && !ht_warned) { -error_report ("AMD CPU doesn't support hyperthreading. Please configure" -" -smp options properly." ); +if (IS_AMD_CPU (env) && + !(env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_TOPOEXT ) && + cs->nr_threads >1 && !ht_warned) { +error_report ("This family of AMD CPU doesn't support " +"hyperthreading. Please configure -smp " +"options properly." ); ht_warned = true; }
Signature · 1 line
1.8.3.1