Skip to contentSkip to conversation

Enable TOPOEXT to support hyperthreading on AMD CPU

PATCH Superseded Babu Moger 1 participant 7 messages Jan 26, 2018 – Feb 20, 2018 KVM

v1 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Babu Moger

author
#0 lore raw

cover Enable TOPOEXT to support hyperthreading on AMD CPU

These series enable TOPOEXT feature on AMD CPU. These are required to support hyperthreading on kvm guests.

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: Fix instruction cache associativity for AMD 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 | 104 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 target/i386/kvm.c |  29 +++++++++++++--
 2 files changed, 122 insertions(+), 11 deletions(-)
Signature · 1 line
2.16.0
author
#1 lore raw

PATCH 1/5 target/i386: Fix instruction cache associativity for AMD

Per Processor Programming Reference, CPUID_Fn80000005_EDX should report L1 instruction cache associativity as 4(way) instead of 2(way).

---
 target/i386/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/cpu.c +1 -1
index 3818d72831..cb055f5940 100644
@@ -123,7 +123,7 @@
 /*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
 #define L1I_LINES_PER_TAG      1
 #define L1I_SIZE_KB_AMD       64
-#define L1I_ASSOCIATIVITY_AMD  2
+#define L1I_ASSOCIATIVITY_AMD  4
 
 /* Level 2 unified cache: */
 #define L2_LINE_SIZE          64
Signature · 1 line
2.16.0
author
#2 lore raw

PATCH 2/5 target/i386: Populate AMD Processor Cache Information

In reply to Babu Moger

From: Stanislav Lanci <[email protected]>

Adds information about cache size and topology from cpuid 0x8000001D leaf for different cache types on AMD processors.

---
 target/i386/cpu.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/kvm.c | 29 ++++++++++++++++++---
 2 files changed, 102 insertions(+), 3 deletions(-)
target/i386/cpu.c +76 -0
index cb055f5940..31ee746dac 100644
@@ -117,6 +117,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
@@ -128,7 +129,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 */
@@ -145,6 +148,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
@@ -3143,6 +3147,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 |= CPUID_4_TYPE_DCACHE | \
+                        CPUID_4_LEVEL(1) | \
+                        CPUID_4_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 |= CPUID_4_TYPE_ICACHE | \
+                        CPUID_4_LEVEL(1) | \
+                        CPUID_4_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 |= CPUID_4_TYPE_UNIFIED | \
+                        CPUID_4_LEVEL(2) | \
+                        CPUID_4_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 = CPUID_4_INCLUSIVE;
+                break;
+            case 3: /* L3 cache info */
+                if (!cpu->enable_l3_cache) {
+                    *eax = 0;
+                    *ebx = 0;
+                    *ecx = 0;
+                    *edx = 0;
+                    break;
+                }
+                *eax |= CPUID_4_TYPE_UNIFIED | \
+                        CPUID_4_LEVEL(3) | \
+                        CPUID_4_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 = CPUID_4_NO_INVD_SHARING;
+                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
index 6f69e2fcfd..8022db7631 100644
@@ -898,9 +898,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
2.16.0
author
#3 lore raw

PATCH 3/5 target/i386: Add support for CPUID_8000_001E for AMD

In reply to Babu Moger

From: 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. These are supported via CPUID_8000_001E extended functions.

---
 target/i386/cpu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/i386/cpu.c +8 -0
index 31ee746dac..52591a1486 100644
@@ -3219,6 +3219,14 @@ 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) {
+            *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
2.16.0
author
#4 lore raw

PATCH 4/5 target/i386: Enable TOPOEXT feature on AMD EPYC CPU

In reply to Babu Moger

Enable TOPOEXT feature on EPYC CPU. This is required to support hyperthreading on VM guests. These are supported via CPUID_8000_001E extended functions.

---
 target/i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/i386/cpu.c +3 -2
index 52591a1486..956ed112db 100644
@@ -1557,7 +1557,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 |
@@ -1572,7 +1573,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
2.16.0
author
#5 lore raw

PATCH 5/5 target/i386: Remove generic SMT thread check

In reply to Babu Moger

Remove 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 | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
target/i386/cpu.c +8 -5
index 956ed112db..6c34720839 100644
@@ -3888,17 +3888,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
+    /* 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 gives
      * 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
2.16.0
#6 lore raw
In reply to Babu Moger

Ping..
Looks like these patches got lost in the mix. Any comments on these patches? Thanks
Babu

Quoted 46 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Babu Moger
Sent: Friday, January 26, 2018 3:40 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; Moger, Babu <[email protected]>; qemu- [email protected]; [email protected]
Subject: [PATCH 0/5] Enable TOPOEXT to support hyperthreading on AMD CPU

These series enable TOPOEXT feature on AMD CPU. These are required to support
hyperthreading on kvm guests.

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: Fix instruction cache associativity for AMD 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 | 104
+++++++++++++++++++++++++++++++++++++++++++++++++-----
 target/i386/kvm.c |  29 +++++++++++++--
 2 files changed, 122 insertions(+), 11 deletions(-)

--
2.16.0