Skip to contentSkip to conversation

target/ppc/spapr: Add SPAPR_CAP_LARGE_DECREMENTER

PATCH Superseded Suraj Jitindar Singh 2 participants 15 messages Feb 26, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 target/ppc/spapr: Add SPAPR_CAP_LARGE_DECREMENTER

Add spapr_cap SPAPR_CAP_LARGE_DECREMENTER to be used to control the availability and size of the large decrementer made available to the guest.

---
 hw/ppc/spapr.c         |  2 ++
 hw/ppc/spapr_caps.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  5 ++++-
 3 files changed, 51 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +2 -0
index b6a571b6f1..acf62a2b9f 100644
@@ -2077,6 +2077,7 @@ static const VMStateDescription vmstate_spapr = {
         &vmstate_spapr_irq_map,
         &vmstate_spapr_cap_nested_kvm_hv,
         &vmstate_spapr_dtb,
+        &vmstate_spapr_cap_large_decr,
         NULL
     }
 };
@@ -4288,6 +4289,7 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
     smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /* 64kiB */
     smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] = SPAPR_CAP_OFF;
+    smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;
     spapr_caps_add_properties(smc, &error_abort);
     smc->irq = &spapr_irq_xics;
     smc->dr_phb_enabled = true;
hw/ppc/spapr_caps.c +45 -0

69 lines not on this page · raw message

include/hw/ppc/spapr.h +4 -1
index 59073a7579..8efc5e0779 100644
@@ -74,8 +74,10 @@ typedef enum {
 #define SPAPR_CAP_HPT_MAXPAGESIZE       0x06
 /* Nested KVM-HV */
 #define SPAPR_CAP_NESTED_KVM_HV         0x07
+/* Large Decrementer */
+#define SPAPR_CAP_LARGE_DECREMENTER     0x08
 /* Num Caps */
-#define SPAPR_CAP_NUM                   (SPAPR_CAP_NESTED_KVM_HV + 1)
+#define SPAPR_CAP_NUM                   (SPAPR_CAP_LARGE_DECREMENTER + 1)
 
 /*
  * Capability Values
@@ -828,6 +830,7 @@ extern const VMStateDescription vmstate_spapr_cap_cfpc;
 extern const VMStateDescription vmstate_spapr_cap_sbbc;
 extern const VMStateDescription vmstate_spapr_cap_ibs;
 extern const VMStateDescription vmstate_spapr_cap_nested_kvm_hv;
+extern const VMStateDescription vmstate_spapr_cap_large_decr;
 
 static inline uint8_t spapr_get_cap(sPAPRMachineState *spapr, int cap)
 {
Signature · 1 line
2.13.6
#1 lore raw

PATCH 2/4 target/ppc: Implement large decrementer support for TCG

Prior to POWER9 the decrementer was a 32-bit register which decremented with each tick of the timebase. From POWER9 onwards the decrementer can be set to operate in a mode called large decrementer where it acts as a n-bit decrementing register which is visible as a 64-bit register, that is the value of the decrementer is sign extended to 64 bits (where n is implementation dependant).

The mode in which the decrementer operates is controlled by the LPCR_LD bit in the logical paritition control register (LPCR).

Quoted 1 line

From POWER9 onwards the HDEC (hypervisor decrementer) was enlarged to

h-bits, also sign extended to 64 bits (where h is implementation dependant). Note this isn't configurable and is always enabled.

For TCG we allow the user to configure a custom large decrementer size, so long as it's at least 32 and less than the size of the HDEC (the restrictions imposed by the ISA).

---
 hw/ppc/ppc.c                    | 78 ++++++++++++++++++++++++++++-------------
 hw/ppc/spapr.c                  |  8 +++++
 hw/ppc/spapr_caps.c             | 38 +++++++++++++++++++-
 target/ppc/cpu-qom.h            |  1 +
 target/ppc/cpu.h                | 11 +++---
 target/ppc/mmu-hash64.c         |  2 +-
 target/ppc/translate.c          |  2 +-
 target/ppc/translate_init.inc.c |  1 +
 8 files changed, 109 insertions(+), 32 deletions(-)
hw/ppc/ppc.c +53 -25
index d1e3d4cd20..853afeed6a 100644
@@ -744,10 +744,10 @@ bool ppc_decr_clear_on_delivery(CPUPPCState *env)
     return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
 }
 
-static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
+static inline uint64_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
 {
     ppc_tb_t *tb_env = env->tb_env;
-    uint32_t decr;
+    uint64_t decr;
     int64_t diff;
 
     diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -758,27 +758,42 @@ static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
     }  else {
         decr = -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
     }
-    LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
+    LOG_TB("%s: %016" PRIx64 "\n", __func__, decr);
 
     return decr;
 }
 
-uint32_t cpu_ppc_load_decr (CPUPPCState *env)
+target_ulong cpu_ppc_load_decr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
     if (kvm_enabled()) {
         return env->spr[SPR_DECR];
     }
 
-    return _cpu_ppc_load_decr(env, tb_env->decr_next);
+    decr = _cpu_ppc_load_decr(env, tb_env->decr_next);
+
+    /*
+     * If large decrementer is enabled then the decrementer is signed extened
+     * to 64 bits, otherwise it is a 32 bit value.
+     */
+    if (env->spr[SPR_LPCR] & LPCR_LD)
+        return decr;
+    return (uint32_t) decr;
 }
 
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
-    return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+    decr =  _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+
+    /* If POWER9 or later then hdecr is sign extended to 64 bits otherwise 32 */
+    if (env->mmu_model & POWERPC_MMU_3_00)
+        return decr;
+    return (uint32_t) decr;
 }
 
 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
@@ -832,13 +847,21 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
                                  QEMUTimer *timer,
                                  void (*raise_excp)(void *),
                                  void (*lower_excp)(PowerPCCPU *),
-                                 uint32_t decr, uint32_t value)
+                                 target_ulong decr, target_ulong value,
+                                 int nr_bits)
 {
     CPUPPCState *env = &cpu->env;
     ppc_tb_t *tb_env = env->tb_env;
     uint64_t now, next;
+    bool negative;
+
+    /* Truncate value to decr_width and sign extend for simplicity */
+    value &= ((1ULL << nr_bits) - 1);
+    negative = !!(value & (1ULL << (nr_bits - 1)));
+    if (negative)
+        value |= (0xFFFFFFFFULL << nr_bits);
 
-    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
+    LOG_TB("%s: " TARGET_FMT_lx " => " TARGET_FMT_lx "\n", __func__,
                 decr, value);
 
     if (kvm_enabled()) {
@@ -860,15 +883,15 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
      * an edge interrupt, so raise it here too.
      */
     if ((value < 3) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
-          && !(decr & 0x80000000))) {
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && negative) ||
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && negative
+          && !(decr & (1ULL << (nr_bits - 1))))) {
         (*raise_excp)(cpu);
         return;
     }
 
     /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
-    if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
+    if (!negative && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
         (*lower_excp)(cpu);
     }
 
@@ -881,21 +904,24 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
     timer_mod(timer, next);
 }
 
-static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
-                                       uint32_t value)
+static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, target_ulong decr,
+                                       target_ulong value, int nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
                          tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
-                         value);
+                         value, nr_bits);
 }
 
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    int nr_bits = 32;
+    if ((env->spr[SPR_LPCR] & LPCR_LD) && (env->large_decr_bits > 32))
+        nr_bits = env->large_decr_bits;
 
-    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
+    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value, nr_bits);
 }
 
 static void cpu_ppc_decr_cb(void *opaque)
@@ -905,23 +931,25 @@ static void cpu_ppc_decr_cb(void *opaque)
     cpu_ppc_decr_excp(cpu);
 }
 
-static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
-                                        uint32_t value)
+static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, target_ulong hdecr,
+                                        target_ulong value, int nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     if (tb_env->hdecr_timer != NULL) {
         __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
                              tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
-                             hdecr, value);
+                             hdecr, value, nr_bits);
     }
 }
 
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+    int nr_bits = (pcc->hdecr_bits > 32) ? pcc->hdecr_bits : 32;
 
-    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
+    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value, nr_bits);
 }
 
 static void cpu_ppc_hdecr_cb(void *opaque)
@@ -951,8 +979,8 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
      * if a decrementer exception is pending when it enables msr_ee at startup,
      * it's not ready to handle it...
      */
-    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
-    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
+    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
+    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
hw/ppc/spapr.c +8 -0
index acf62a2b9f..966bc74e68 100644
@@ -557,6 +557,14 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
                           pcc->radix_page_info->count *
                           sizeof(radix_AP_encodings[0]))));
     }
+
+    /*
+     * We set this property to let the guest know that it can use the large
+     * decrementer and its width in bits.
+     */
+    if (env->large_decr_bits)
+        _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
+                              env->large_decr_bits)));
 }
 
 static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
hw/ppc/spapr_caps.c +37 -1
index 1545a02729..44542fdbb2 100644
@@ -421,8 +421,43 @@ static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
 static void cap_large_decr_apply(sPAPRMachineState *spapr,
                                  uint8_t val, Error **errp)
 {
-    if (val)
+    PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+
+    if (!val)
+        return; /* Disabled by default */
+
+    if (tcg_enabled()) {
+        if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
+                              spapr->max_compat_pvr)) {
+            error_setg(errp,
+                "Large decrementer only supported on POWER9, try -cpu POWER9");
+            return;
+        }
+        if ((val < 32) || (val > pcc->hdecr_bits)) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-large-decr=%d",
+                pcc->hdecr_bits);
+            return;
+        }
+    } else {
         error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    }
+}
+
+static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
+                                     PowerPCCPU *cpu,
+                                     uint8_t val, Error **errp)
+{
+    CPUPPCState *env = &cpu->env;
+    target_ulong lpcr = env->spr[SPR_LPCR];
+
+    if (val)
+        lpcr |= LPCR_LD;
+    else
+        lpcr &= ~LPCR_LD;
+    ppc_store_lpcr(cpu, lpcr);
+    env->large_decr_bits = val;
 }
 
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
@@ -511,6 +546,7 @@ sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
         .set = spapr_cap_set_uint8,
         .type = "int",
         .apply = cap_large_decr_apply,
+        .cpu_apply = cap_large_decr_cpu_apply,
     },
 };
 
target/ppc/cpu-qom.h +1 -0
index ae51fe754e..cced705e30 100644
@@ -190,6 +190,7 @@ typedef struct PowerPCCPUClass {
 #endif
     const PPCHash64Options *hash64_opts;
     struct ppc_radix_page_info *radix_page_info;
+    uint32_t hdecr_bits;
     void (*init_proc)(CPUPPCState *env);
     int  (*check_pow)(CPUPPCState *env);
     int (*handle_mmu_fault)(PowerPCCPU *cpu, vaddr eaddr, int rwx, int mmu_idx);
target/ppc/cpu.h +7 -4
index 26604ddf98..8da333e9da 100644
@@ -1171,6 +1171,9 @@ struct CPUPPCState {
     uint32_t tm_vscr;
     uint64_t tm_dscr;
     uint64_t tm_tar;
+
+    /* Large Decrementer */
+    int large_decr_bits;
 };
 
 #define SET_FIT_PERIOD(a_, b_, c_, d_)          \
@@ -1321,10 +1324,10 @@ uint32_t cpu_ppc_load_atbu (CPUPPCState *env);
 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value);
 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value);
 bool ppc_decr_clear_on_delivery(CPUPPCState *env);
-uint32_t cpu_ppc_load_decr (CPUPPCState *env);
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value);
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env);
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value);
+target_ulong cpu_ppc_load_decr (CPUPPCState *env);
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value);
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env);
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value);
 uint64_t cpu_ppc_load_purr (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env);
target/ppc/mmu-hash64.c +1 -1
index c431303eff..a2b1ec5040 100644
@@ -1109,7 +1109,7 @@ void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
     case POWERPC_MMU_3_00: /* P9 */
         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD |
                       (LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE | LPCR_AIL |
-                      LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR |
+                      LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR | LPCR_LD |
                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE | LPCR_EEE |
                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE | LPCR_TC |
                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE | LPCR_HDICE);
target/ppc/translate.c +1 -1
index 819221f246..b156be4d98 100644
@@ -7417,7 +7417,7 @@ void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
 #if !defined(NO_TIMER_DUMP)
     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
 #if !defined(CONFIG_USER_ONLY)
-                " DECR %08" PRIu32
+                " DECR " TARGET_FMT_lu
 #endif
                 "\n",
                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
target/ppc/translate_init.inc.c +1 -0
index 58542c0fe0..4e0bf1f47a 100644
@@ -8926,6 +8926,7 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data)
     /* segment page size remain the same */
     pcc->hash64_opts = &ppc_hash64_opts_POWER7;
     pcc->radix_page_info = &POWER9_radix_page_info;
+    pcc->hdecr_bits = 56;
 #endif
     pcc->excp_model = POWERPC_EXCP_POWER9;
     pcc->bus_model = PPC_FLAGS_INPUT_POWER9;
Signature · 1 line
2.13.6
#2 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 2/4] target/ppc: Implement large decrementer support for TCG

Quoted 36 lines · Suraj Jitindar Singh

Prior to POWER9 the decrementer was a 32-bit register which decremented with each tick of the timebase. From POWER9 onwards the decrementer can be set to operate in a mode called large decrementer where it acts as a n-bit decrementing register which is visible as a 64-bit register, that is the value of the decrementer is sign extended to 64 bits (where n is implementation dependant).

The mode in which the decrementer operates is controlled by the LPCR_LD bit in the logical paritition control register (LPCR).

Quoted 1 line

From POWER9 onwards the HDEC (hypervisor decrementer) was enlarged to

h-bits, also sign extended to 64 bits (where h is implementation dependant). Note this isn't configurable and is always enabled.

For TCG we allow the user to configure a custom large decrementer size, so long as it's at least 32 and less than the size of the HDEC (the restrictions imposed by the ISA).

---
 hw/ppc/ppc.c                    | 78 ++++++++++++++++++++++++++++-------------
 hw/ppc/spapr.c                  |  8 +++++
 hw/ppc/spapr_caps.c             | 38 +++++++++++++++++++-
 target/ppc/cpu-qom.h            |  1 +
 target/ppc/cpu.h                | 11 +++---
 target/ppc/mmu-hash64.c         |  2 +-
 target/ppc/translate.c          |  2 +-
 target/ppc/translate_init.inc.c |  1 +
 8 files changed, 109 insertions(+), 32 deletions(-)
hw/ppc/ppc.c +0 -0
index d1e3d4cd20..853afeed6a 100644

hw/ppc/ppc.c bool ppc_decr_clear_on_delivery(CPUPPCState *env) · line 747

Quoted 5 lines
hw/ppc/ppc.c +1 -1
@@ -744,10 +744,10 @@ bool ppc_decr_clear_on_delivery(CPUPPCState *env)
     return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
 }
 
-static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
+static inline uint64_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)

Hrm. Given how we use this - and how muldiv64 works, wouldn't it make more sense to have it return int64_t (i.e. signed).

Quoted 40 lines
hw/ppc/ppc.c +13 -5
 {
     ppc_tb_t *tb_env = env->tb_env;
-    uint32_t decr;
+    uint64_t decr;
     int64_t diff;
 
     diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -758,27 +758,42 @@ static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
     }  else {
         decr = -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
     }
-    LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
+    LOG_TB("%s: %016" PRIx64 "\n", __func__, decr);
 
     return decr;
 }
 
-uint32_t cpu_ppc_load_decr (CPUPPCState *env)
+target_ulong cpu_ppc_load_decr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
     if (kvm_enabled()) {
         return env->spr[SPR_DECR];
     }
 
-    return _cpu_ppc_load_decr(env, tb_env->decr_next);
+    decr = _cpu_ppc_load_decr(env, tb_env->decr_next);
+
+    /*
+     * If large decrementer is enabled then the decrementer is signed extened
+     * to 64 bits, otherwise it is a 32 bit value.
+     */
+    if (env->spr[SPR_LPCR] & LPCR_LD)
+        return decr;
+    return (uint32_t) decr;
 }
 
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)

hw/ppc/ppc.c static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next) · line 794

Quoted 10 lines
hw/ppc/ppc.c +6 -1
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
-    return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+    decr =  _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+
+    /* If POWER9 or later then hdecr is sign extended to 64 bits otherwise 32 */
+    if (env->mmu_model & POWERPC_MMU_3_00)

You already have a pcc->hdecr_bits. Wouldn't it make more sense to check against that than the cpu model directly?

Quoted 11 lines
hw/ppc/ppc.c +3 -1
+        return decr;
+    return (uint32_t) decr;
 }
 
 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
@@ -832,13 +847,21 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
                                  QEMUTimer *timer,
                                  void (*raise_excp)(void *),
                                  void (*lower_excp)(PowerPCCPU *),
-                                 uint32_t decr, uint32_t value)
+                                 target_ulong decr, target_ulong value,

hw/ppc/ppc.c static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp, · line 860

Quoted 10 lines
hw/ppc/ppc.c +6 -0
+                                 int nr_bits)
 {
     CPUPPCState *env = &cpu->env;
     ppc_tb_t *tb_env = env->tb_env;
     uint64_t now, next;
+    bool negative;
+
+    /* Truncate value to decr_width and sign extend for simplicity */
+    value &= ((1ULL << nr_bits) - 1);
+    negative = !!(value & (1ULL << (nr_bits - 1)));

Could you simplify this by just using

	negative = (int64_t)decr < 0;

before you mask? Or would that be wrong in some edge case or other?

Quoted 42 lines
hw/ppc/ppc.c +9 -8
+    if (negative)
+        value |= (0xFFFFFFFFULL << nr_bits);
 
-    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
+    LOG_TB("%s: " TARGET_FMT_lx " => " TARGET_FMT_lx "\n", __func__,
                 decr, value);
 
     if (kvm_enabled()) {
@@ -860,15 +883,15 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
      * an edge interrupt, so raise it here too.
      */
     if ((value < 3) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
-          && !(decr & 0x80000000))) {
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && negative) ||
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && negative
+          && !(decr & (1ULL << (nr_bits - 1))))) {
         (*raise_excp)(cpu);
         return;
     }
 
     /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
-    if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
+    if (!negative && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
         (*lower_excp)(cpu);
     }
 
@@ -881,21 +904,24 @@ static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
     timer_mod(timer, next);
 }
 
-static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
-                                       uint32_t value)
+static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, target_ulong decr,
+                                       target_ulong value, int nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
                          tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
-                         value);

hw/ppc/ppc.c static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp, · line 922

Quoted 10 lines
hw/ppc/ppc.c +5 -1
+                         value, nr_bits);
 }
 
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    int nr_bits = 32;
+    if ((env->spr[SPR_LPCR] & LPCR_LD) && (env->large_decr_bits > 32))
+        nr_bits = env->large_decr_bits;

This would be simpler if you initialized large_decr_bits to 32 on cpus that don't have large decr, wouldn't it?

Quoted 21 lines
hw/ppc/ppc.c +3 -4
-    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
+    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value, nr_bits);
 }
 
 static void cpu_ppc_decr_cb(void *opaque)
@@ -905,23 +931,25 @@ static void cpu_ppc_decr_cb(void *opaque)
     cpu_ppc_decr_excp(cpu);
 }
 
-static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
-                                        uint32_t value)
+static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, target_ulong hdecr,
+                                        target_ulong value, int nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     if (tb_env->hdecr_timer != NULL) {
         __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
                              tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
-                             hdecr, value);

hw/ppc/ppc.c static void cpu_ppc_decr_cb(void *opaque) · line 950

Quoted 10 lines
hw/ppc/ppc.c +4 -1
+                             hdecr, value, nr_bits);
     }
 }
 
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+    int nr_bits = (pcc->hdecr_bits > 32) ? pcc->hdecr_bits : 32;

Similarly with hdecr_bits.

Quoted 43 lines
hw/ppc/ppc.c +3 -3
-    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
+    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value, nr_bits);
 }
 
 static void cpu_ppc_hdecr_cb(void *opaque)
@@ -951,8 +979,8 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
      * if a decrementer exception is pending when it enables msr_ee at startup,
      * it's not ready to handle it...
      */
-    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
-    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
+    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
+    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
hw/ppc/spapr.c +8 -0
index acf62a2b9f..966bc74e68 100644
@@ -557,6 +557,14 @@ static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
                           pcc->radix_page_info->count *
                           sizeof(radix_AP_encodings[0]))));
     }
+
+    /*
+     * We set this property to let the guest know that it can use the large
+     * decrementer and its width in bits.
+     */
+    if (env->large_decr_bits)
+        _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
+                              env->large_decr_bits)));
 }
 
 static void spapr_populate_cpus_dt_node(void *fdt, sPAPRMachineState *spapr)
hw/ppc/spapr_caps.c +0 -0
index 1545a02729..44542fdbb2 100644
@@ -421,8 +421,43 @@ static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
 static void cap_large_decr_apply(sPAPRMachineState *spapr,
                                  uint8_t val, Error **errp)
 {

hw/ppc/spapr_caps.c static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr, · line 432

Quoted 10 lines
hw/ppc/spapr_caps.c +9 -1
-    if (val)
+    PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+
+    if (!val)
+        return; /* Disabled by default */
+
+    if (tcg_enabled()) {
+        if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
+                              spapr->max_compat_pvr)) {

IIUC this is strictly redundant with the check against hdecr_bits, yes, but will result in a more helpful error message. Is that right?

Quoted 118 lines
hw/ppc/spapr_caps.c +28 -0
+            error_setg(errp,
+                "Large decrementer only supported on POWER9, try -cpu POWER9");
+            return;
+        }
+        if ((val < 32) || (val > pcc->hdecr_bits)) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-large-decr=%d",
+                pcc->hdecr_bits);
+            return;
+        }
+    } else {
         error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    }
+}
+
+static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
+                                     PowerPCCPU *cpu,
+                                     uint8_t val, Error **errp)
+{
+    CPUPPCState *env = &cpu->env;
+    target_ulong lpcr = env->spr[SPR_LPCR];
+
+    if (val)
+        lpcr |= LPCR_LD;
+    else
+        lpcr &= ~LPCR_LD;
+    ppc_store_lpcr(cpu, lpcr);
+    env->large_decr_bits = val;
 }
 
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
@@ -511,6 +546,7 @@ sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
         .set = spapr_cap_set_uint8,
         .type = "int",
         .apply = cap_large_decr_apply,
+        .cpu_apply = cap_large_decr_cpu_apply,
     },
 };
 
target/ppc/cpu-qom.h +1 -0
index ae51fe754e..cced705e30 100644
@@ -190,6 +190,7 @@ typedef struct PowerPCCPUClass {
 #endif
     const PPCHash64Options *hash64_opts;
     struct ppc_radix_page_info *radix_page_info;
+    uint32_t hdecr_bits;
     void (*init_proc)(CPUPPCState *env);
     int  (*check_pow)(CPUPPCState *env);
     int (*handle_mmu_fault)(PowerPCCPU *cpu, vaddr eaddr, int rwx, int mmu_idx);
target/ppc/cpu.h +7 -4
index 26604ddf98..8da333e9da 100644
@@ -1171,6 +1171,9 @@ struct CPUPPCState {
     uint32_t tm_vscr;
     uint64_t tm_dscr;
     uint64_t tm_tar;
+
+    /* Large Decrementer */
+    int large_decr_bits;
 };
 
 #define SET_FIT_PERIOD(a_, b_, c_, d_)          \
@@ -1321,10 +1324,10 @@ uint32_t cpu_ppc_load_atbu (CPUPPCState *env);
 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value);
 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value);
 bool ppc_decr_clear_on_delivery(CPUPPCState *env);
-uint32_t cpu_ppc_load_decr (CPUPPCState *env);
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value);
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env);
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value);
+target_ulong cpu_ppc_load_decr (CPUPPCState *env);
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value);
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env);
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value);
 uint64_t cpu_ppc_load_purr (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env);
target/ppc/mmu-hash64.c +1 -1
index c431303eff..a2b1ec5040 100644
@@ -1109,7 +1109,7 @@ void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
     case POWERPC_MMU_3_00: /* P9 */
         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD |
                       (LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE | LPCR_AIL |
-                      LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR |
+                      LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR | LPCR_LD |
                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE | LPCR_EEE |
                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE | LPCR_TC |
                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE | LPCR_HDICE);
target/ppc/translate.c +1 -1
index 819221f246..b156be4d98 100644
@@ -7417,7 +7417,7 @@ void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
 #if !defined(NO_TIMER_DUMP)
     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
 #if !defined(CONFIG_USER_ONLY)
-                " DECR %08" PRIu32
+                " DECR " TARGET_FMT_lu
 #endif
                 "\n",
                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
target/ppc/translate_init.inc.c +1 -0
index 58542c0fe0..4e0bf1f47a 100644
@@ -8926,6 +8926,7 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data)
     /* segment page size remain the same */
     pcc->hash64_opts = &ppc_hash64_opts_POWER7;
     pcc->radix_page_info = &POWER9_radix_page_info;
+    pcc->hdecr_bits = 56;
 #endif
     pcc->excp_model = POWERPC_EXCP_POWER9;
     pcc->bus_model = PPC_FLAGS_INPUT_POWER9;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 2/4] target/ppc: Implement large decrementer support for TCG

Quoted 52 lines · David Gibson
Quoted 51 lines · Suraj Jitindar Singh

Prior to POWER9 the decrementer was a 32-bit register which decremented
with each tick of the timebase. From POWER9 onwards the decrementer can
be set to operate in a mode called large decrementer where it acts as a
n-bit decrementing register which is visible as a 64-bit register, that
is the value of the decrementer is sign extended to 64 bits (where n is
implementation dependant).

The mode in which the decrementer operates is controlled by the LPCR_LD
bit in the logical paritition control register (LPCR).

Quoted 2 lines

From POWER9 onwards the HDEC (hypervisor decrementer) was enlarged to

h-bits, also sign extended to 64 bits (where h is implementation dependant). Note this isn't configurable and is always enabled.

For TCG we allow the user to configure a custom large decrementer size,
so long as it's at least 32 and less than the size of the HDEC (the restrictions imposed by the ISA).

---
 hw/ppc/ppc.c                    | 78 ++++++++++++++++++++++++++++-
------------
 hw/ppc/spapr.c                  |  8 +++++
 hw/ppc/spapr_caps.c             | 38 +++++++++++++++++++-
 target/ppc/cpu-qom.h            |  1 +
 target/ppc/cpu.h                | 11 +++---
 target/ppc/mmu-hash64.c         |  2 +-
 target/ppc/translate.c          |  2 +-
 target/ppc/translate_init.inc.c |  1 +
 8 files changed, 109 insertions(+), 32 deletions(-)
hw/ppc/ppc.c +0 -0
index d1e3d4cd20..853afeed6a 100644
@@ -744,10 +744,10 @@ bool ppc_decr_clear_on_delivery(CPUPPCState
 *env)
     return ((tb_env->flags & flags) ==
 PPC_DECR_UNDERFLOW_TRIGGERED);
 }
 
Quoted 8 lines

hw/ppc/ppc.c bool ppc_decr_clear_on_delivery(CPUPPCState · line 751

Quoted 4 lines
hw/ppc/ppc.c +1 -1
-static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env,
 uint64_t next)
+static inline uint64_t _cpu_ppc_load_decr(CPUPPCState *env,
 uint64_t next)

Hrm. Given how we use this - and how muldiv64 works, wouldn't it make
more sense to have it return int64_t (i.e. signed).

I mean, functionally it's the same. So sure

Quoted 49 lines
Quoted 49 lines
hw/ppc/ppc.c +8 -4
 {
     ppc_tb_t *tb_env = env->tb_env;
-    uint32_t decr;
+    uint64_t decr;
     int64_t diff;
 
     diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -758,27 +758,42 @@ static inline uint32_t
 _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
     }  else {
         decr = -muldiv64(-diff, tb_env->decr_freq,
 NANOSECONDS_PER_SECOND);
     }
-    LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
+    LOG_TB("%s: %016" PRIx64 "\n", __func__, decr);
 
     return decr;
 }
 
-uint32_t cpu_ppc_load_decr (CPUPPCState *env)
+target_ulong cpu_ppc_load_decr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
     if (kvm_enabled()) {
         return env->spr[SPR_DECR];
     }
 
-    return _cpu_ppc_load_decr(env, tb_env->decr_next);
+    decr = _cpu_ppc_load_decr(env, tb_env->decr_next);
+
+    /*
+     * If large decrementer is enabled then the decrementer is

signed extened

hw/ppc/ppc.c +7 -1
+     * to 64 bits, otherwise it is a 32 bit value.
+     */
+    if (env->spr[SPR_LPCR] & LPCR_LD)
+        return decr;
+    return (uint32_t) decr;
 }
 
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
Quoted 9 lines
Quoted 6 lines
hw/ppc/ppc.c +3 -1
-    return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+    decr =  _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+
+    /* If POWER9 or later then hdecr is sign extended to 64 bits

otherwise 32 */
+ if (env->mmu_model & POWERPC_MMU_3_00)

You already have a pcc->hdecr_bits. Wouldn't it make more sense to check against that than the cpu model directly?

The end result is the same since P9 is the only one with hdecr_bits defined. So I'll change it if you prefer.

Quoted 27 lines
Quoted 15 lines
hw/ppc/ppc.c +4 -1
+        return decr;
+    return (uint32_t) decr;
 }
 
 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
@@ -832,13 +847,21 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
                                  QEMUTimer *timer,
                                  void (*raise_excp)(void *),
                                  void (*lower_excp)(PowerPCCPU *),
-                                 uint32_t decr, uint32_t value)
+                                 target_ulong decr, target_ulong
 value,
+                                 int nr_bits)

hw/ppc/ppc.c static void __cpu_ppc_store_decr(PowerPCCPU · line 863

Quoted 10 lines
hw/ppc/ppc.c +5 -0
 {
     CPUPPCState *env = &cpu->env;
     ppc_tb_t *tb_env = env->tb_env;
     uint64_t now, next;
+    bool negative;
+
+    /* Truncate value to decr_width and sign extend for simplicity
 */
+    value &= ((1ULL << nr_bits) - 1);
+    negative = !!(value & (1ULL << (nr_bits - 1)));

Could you simplify this by just using

Quoted 2 lines
	negative = (int64_t)decr < 0;

before you mask? Or would that be wrong in some edge case or other?

Value might not be a 64 bit number. It could be 56 bits for example where the 56th bit being set would indicate that it's negative. So we need to explicitly check as far as I can tell, unless I've missed something.

Casting it to a (int64_t) would only make it negative if the 64th bit was set correct?

But on a 56 bit decrementer you could load -1 as:
0x00FFFFFFFFFFFFFF
Which when cast to a (int64_t) wouldn't be negative.

Quoted 60 lines
Quoted 60 lines
hw/ppc/ppc.c +3 -1
+    if (negative)
+        value |= (0xFFFFFFFFULL << nr_bits);
 
-    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
+    LOG_TB("%s: " TARGET_FMT_lx " => " TARGET_FMT_lx "\n",

__func__,

                 decr, value);
 
     if (kvm_enabled()) {
hw/ppc/ppc.c +0 -1
@@ -860,15 +883,15 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
      * an edge interrupt, so raise it here too.
      */
     if ((value < 3) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value &

0x80000000)) ||
- ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value
& 0x80000000)

hw/ppc/ppc.c +1 -1
-          && !(decr & 0x80000000))) {
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && negative)
||
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) &&
negative
+          && !(decr & (1ULL << (nr_bits - 1))))) {
         (*raise_excp)(cpu);
         return;
     }
 
     /* On MSB level based systems a 0 for the MSB stops interrupt

delivery */
- if (!(value & 0x80000000) && (tb_env->flags &
PPC_DECR_UNDERFLOW_LEVEL)) {

hw/ppc/ppc.c +4 -3
+    if (!negative && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
         (*lower_excp)(cpu);
     }
@@ -881,21 +904,24 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
     timer_mod(timer, next);
 }
 
-static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t
 decr,
-                                       uint32_t value)
+static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu,
 target_ulong decr,
+                                       target_ulong value, int
 nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env-
 >decr_timer,
                          tb_env->decr_timer->cb,
 &cpu_ppc_decr_lower, decr,
-                         value);
+                         value, nr_bits);
 }
 
Quoted 11 lines
Quoted 8 lines
hw/ppc/ppc.c +3 -1
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    int nr_bits = 32;
+    if ((env->spr[SPR_LPCR] & LPCR_LD) && (env->large_decr_bits >

32))
+ nr_bits = env->large_decr_bits;

This would be simpler if you initialized large_decr_bits to 32 on cpus that don't have large decr, wouldn't it?

Correct, will do.

Quoted 32 lines
Quoted 32 lines
hw/ppc/ppc.c +1 -1
-    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
+    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value,

nr_bits);
}

static void cpu_ppc_decr_cb(void *opaque)

hw/ppc/ppc.c +3 -3
@@ -905,23 +931,25 @@ static void cpu_ppc_decr_cb(void *opaque)
     cpu_ppc_decr_excp(cpu);
 }
 
-static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t
 hdecr,
-                                        uint32_t value)
+static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu,
 target_ulong hdecr,
+                                        target_ulong value, int
 nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     if (tb_env->hdecr_timer != NULL) {
         __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env-
 >hdecr_timer,
                              tb_env->hdecr_timer->cb,
 &cpu_ppc_hdecr_lower,
-                             hdecr, value);
+                             hdecr, value, nr_bits);
     }
 }
 
Quoted 8 lines

hw/ppc/ppc.c static void cpu_ppc_decr_cb(void *opaque) · line 950

Quoted 6 lines
hw/ppc/ppc.c +3 -1
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+    int nr_bits = (pcc->hdecr_bits > 32) ? pcc->hdecr_bits : 32;

Similarly with hdecr_bits.

Yep

Quoted 57 lines
Quoted 57 lines
hw/ppc/ppc.c +1 -1
-    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
+    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value,

nr_bits);
}

static void cpu_ppc_hdecr_cb(void *opaque)

hw/ppc/ppc.c +2 -2
@@ -951,8 +979,8 @@ static void cpu_ppc_set_tb_clk (void *opaque,
 uint32_t freq)
      * if a decrementer exception is pending when it enables
 msr_ee at startup,
      * it's not ready to handle it...
      */
-    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
-    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
+    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
+    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);

}

hw/ppc/spapr.c +3 -0
index acf62a2b9f..966bc74e68 100644
@@ -557,6 +557,14 @@ static void spapr_populate_cpu_dt(CPUState
 *cs, void *fdt, int offset,
                           pcc->radix_page_info->count *
                           sizeof(radix_AP_encodings[0]))));
     }
+
+    /*
+     * We set this property to let the guest know that it can use

the large

hw/ppc/spapr.c +5 -0
+     * decrementer and its width in bits.
+     */
+    if (env->large_decr_bits)
+        _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
+                              env->large_decr_bits)));
 }
 
 static void spapr_populate_cpus_dt_node(void *fdt,

sPAPRMachineState *spapr)

hw/ppc/spapr_caps.c +5 -1
index 1545a02729..44542fdbb2 100644
@@ -421,8 +421,43 @@ static void
 cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
 static void cap_large_decr_apply(sPAPRMachineState *spapr,
                                  uint8_t val, Error **errp)
 {
-    if (val)
+    PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+
+    if (!val)
+        return; /* Disabled by default */
Quoted 8 lines

hw/ppc/spapr_caps.c static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr, · line 432

Quoted 4 lines
hw/ppc/spapr_caps.c +4 -0
+
+    if (tcg_enabled()) {
+        if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
+                              spapr->max_compat_pvr)) {

IIUC this is strictly redundant with the check against hdecr_bits, yes, but will result in a more helpful error message. Is that right?

The error message will be more useful.
Also if I go and set hdecr_bits to 32 as suggested above the P9 check is required because otherwise you could set a 32 bit large decr on P7/P8.

Quoted 138 lines
Quoted 136 lines
hw/ppc/spapr_caps.c +11 -1
+            error_setg(errp,
+                "Large decrementer only supported on POWER9, try
-cpu POWER9");
+            return;
+        }
+        if ((val < 32) || (val > pcc->hdecr_bits)) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-
 large-decr=%d",
+                pcc->hdecr_bits);
+            return;
+        }
+    } else {
         error_setg(errp, "No large decrementer support, try cap-

large-decr=0");

hw/ppc/spapr_caps.c +17 -0
+    }
+}
+
+static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
+                                     PowerPCCPU *cpu,
+                                     uint8_t val, Error **errp)
+{
+    CPUPPCState *env = &cpu->env;
+    target_ulong lpcr = env->spr[SPR_LPCR];
+
+    if (val)
+        lpcr |= LPCR_LD;
+    else
+        lpcr &= ~LPCR_LD;
+    ppc_store_lpcr(cpu, lpcr);
+    env->large_decr_bits = val;
 }
 
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
@@ -511,6 +546,7 @@ sPAPRCapabilityInfo
 capability_table[SPAPR_CAP_NUM] = {
         .set = spapr_cap_set_uint8,
         .type = "int",
         .apply = cap_large_decr_apply,
+        .cpu_apply = cap_large_decr_cpu_apply,
     },
 };
target/ppc/cpu-qom.h +1 -0
index ae51fe754e..cced705e30 100644
@@ -190,6 +190,7 @@ typedef struct PowerPCCPUClass {
 #endif
     const PPCHash64Options *hash64_opts;
     struct ppc_radix_page_info *radix_page_info;
+    uint32_t hdecr_bits;
     void (*init_proc)(CPUPPCState *env);
     int  (*check_pow)(CPUPPCState *env);
     int (*handle_mmu_fault)(PowerPCCPU *cpu, vaddr eaddr, int rwx,

int mmu_idx);

target/ppc/cpu.h +7 -4
index 26604ddf98..8da333e9da 100644
@@ -1171,6 +1171,9 @@ struct CPUPPCState {
     uint32_t tm_vscr;
     uint64_t tm_dscr;
     uint64_t tm_tar;
+
+    /* Large Decrementer */
+    int large_decr_bits;
 };
 
 #define SET_FIT_PERIOD(a_, b_, c_, d_)          \
@@ -1321,10 +1324,10 @@ uint32_t cpu_ppc_load_atbu (CPUPPCState
 *env);
 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value);
 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value);
 bool ppc_decr_clear_on_delivery(CPUPPCState *env);
-uint32_t cpu_ppc_load_decr (CPUPPCState *env);
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value);
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env);
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value);
+target_ulong cpu_ppc_load_decr (CPUPPCState *env);
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value);
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env);
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value);
 uint64_t cpu_ppc_load_purr (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env);

uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env);

target/ppc/mmu-hash64.c +0 -0
index c431303eff..a2b1ec5040 100644
@@ -1109,7 +1109,7 @@ void ppc_store_lpcr(PowerPCCPU *cpu,
 target_ulong val)
     case POWERPC_MMU_3_00: /* P9 */
         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD

|
(LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE |
LPCR_AIL |
- LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR
|
+ LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR

| LPCR_LD |
                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE |
LPCR_EEE |
                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE
| LPCR_TC |
                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE |

LPCR_HDICE);

target/ppc/translate.c +1 -1
index 819221f246..b156be4d98 100644
@@ -7417,7 +7417,7 @@ void ppc_cpu_dump_state(CPUState *cs, FILE
 *f, fprintf_function cpu_fprintf,
 #if !defined(NO_TIMER_DUMP)
     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
 #if !defined(CONFIG_USER_ONLY)
-                " DECR %08" PRIu32
+                " DECR " TARGET_FMT_lu
 #endif
                 "\n",

cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)

target/ppc/translate_init.inc.c +1 -0
index 58542c0fe0..4e0bf1f47a 100644
@@ -8926,6 +8926,7 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void
 *data)
     /* segment page size remain the same */
     pcc->hash64_opts = &ppc_hash64_opts_POWER7;
     pcc->radix_page_info = &POWER9_radix_page_info;
+    pcc->hdecr_bits = 56;
 #endif
     pcc->excp_model = POWERPC_EXCP_POWER9;
     pcc->bus_model = PPC_FLAGS_INPUT_POWER9;
#4 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 2/4] target/ppc: Implement large decrementer support for TCG

Quoted 123 lines · Suraj Jitindar Singh
Quoted 52 lines · David Gibson
Quoted 51 lines · Suraj Jitindar Singh

Prior to POWER9 the decrementer was a 32-bit register which decremented
with each tick of the timebase. From POWER9 onwards the decrementer can
be set to operate in a mode called large decrementer where it acts as a
n-bit decrementing register which is visible as a 64-bit register, that
is the value of the decrementer is sign extended to 64 bits (where n is
implementation dependant).

The mode in which the decrementer operates is controlled by the LPCR_LD
bit in the logical paritition control register (LPCR).

Quoted 2 lines

From POWER9 onwards the HDEC (hypervisor decrementer) was enlarged to

h-bits, also sign extended to 64 bits (where h is implementation dependant). Note this isn't configurable and is always enabled.

For TCG we allow the user to configure a custom large decrementer size,
so long as it's at least 32 and less than the size of the HDEC (the restrictions imposed by the ISA).

---
 hw/ppc/ppc.c                    | 78 ++++++++++++++++++++++++++++-
------------
 hw/ppc/spapr.c                  |  8 +++++
 hw/ppc/spapr_caps.c             | 38 +++++++++++++++++++-
 target/ppc/cpu-qom.h            |  1 +
 target/ppc/cpu.h                | 11 +++---
 target/ppc/mmu-hash64.c         |  2 +-
 target/ppc/translate.c          |  2 +-
 target/ppc/translate_init.inc.c |  1 +
 8 files changed, 109 insertions(+), 32 deletions(-)
hw/ppc/ppc.c +0 -0
index d1e3d4cd20..853afeed6a 100644
@@ -744,10 +744,10 @@ bool ppc_decr_clear_on_delivery(CPUPPCState
 *env)
     return ((tb_env->flags & flags) ==
 PPC_DECR_UNDERFLOW_TRIGGERED);
 }
 
Quoted 8 lines

hw/ppc/ppc.c bool ppc_decr_clear_on_delivery(CPUPPCState · line 751

Quoted 4 lines
hw/ppc/ppc.c +1 -1
-static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env,
 uint64_t next)
+static inline uint64_t _cpu_ppc_load_decr(CPUPPCState *env,
 uint64_t next)

Hrm. Given how we use this - and how muldiv64 works, wouldn't it make
more sense to have it return int64_t (i.e. signed).

I mean, functionally it's the same. So sure

Quoted 49 lines
Quoted 49 lines
hw/ppc/ppc.c +8 -4
 {
     ppc_tb_t *tb_env = env->tb_env;
-    uint32_t decr;
+    uint64_t decr;
     int64_t diff;
 
     diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -758,27 +758,42 @@ static inline uint32_t
 _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
     }  else {
         decr = -muldiv64(-diff, tb_env->decr_freq,
 NANOSECONDS_PER_SECOND);
     }
-    LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
+    LOG_TB("%s: %016" PRIx64 "\n", __func__, decr);
 
     return decr;
 }
 
-uint32_t cpu_ppc_load_decr (CPUPPCState *env)
+target_ulong cpu_ppc_load_decr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
 
     if (kvm_enabled()) {
         return env->spr[SPR_DECR];
     }
 
-    return _cpu_ppc_load_decr(env, tb_env->decr_next);
+    decr = _cpu_ppc_load_decr(env, tb_env->decr_next);
+
+    /*
+     * If large decrementer is enabled then the decrementer is

signed extened

hw/ppc/ppc.c +7 -1
+     * to 64 bits, otherwise it is a 32 bit value.
+     */
+    if (env->spr[SPR_LPCR] & LPCR_LD)
+        return decr;
+    return (uint32_t) decr;
 }
 
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env)
 {
     ppc_tb_t *tb_env = env->tb_env;
+    uint64_t decr;
Quoted 9 lines
Quoted 6 lines
hw/ppc/ppc.c +3 -1
-    return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+    decr =  _cpu_ppc_load_decr(env, tb_env->hdecr_next);
+
+    /* If POWER9 or later then hdecr is sign extended to 64 bits

otherwise 32 */
+ if (env->mmu_model & POWERPC_MMU_3_00)

You already have a pcc->hdecr_bits. Wouldn't it make more sense to check against that than the cpu model directly?

Quoted 2 lines

The end result is the same since P9 is the only one with hdecr_bits defined. So I'll change it if you prefer.

I prefer - this way it would need changing again when we get a second cpu with large hdecr.

Quoted 31 lines
Quoted 28 lines
Quoted 16 lines
hw/ppc/ppc.c +4 -1
+        return decr;
+    return (uint32_t) decr;
 }
 
 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
@@ -832,13 +847,21 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
                                  QEMUTimer *timer,
                                  void (*raise_excp)(void *),
                                  void (*lower_excp)(PowerPCCPU *),
-                                 uint32_t decr, uint32_t value)
+                                 target_ulong decr, target_ulong
 value,
+                                 int nr_bits)

hw/ppc/ppc.c static void __cpu_ppc_store_decr(PowerPCCPU · line 863

Quoted 10 lines
hw/ppc/ppc.c +5 -0
 {
     CPUPPCState *env = &cpu->env;
     ppc_tb_t *tb_env = env->tb_env;
     uint64_t now, next;
+    bool negative;
+
+    /* Truncate value to decr_width and sign extend for simplicity
 */
+    value &= ((1ULL << nr_bits) - 1);
+    negative = !!(value & (1ULL << (nr_bits - 1)));

Could you simplify this by just using

Quoted 2 lines
	negative = (int64_t)decr < 0;

before you mask? Or would that be wrong in some edge case or other?

Quoted 4 lines

Value might not be a 64 bit number. It could be 56 bits for example where the 56th bit being set would indicate that it's negative. So we need to explicitly check as far as I can tell, unless I've missed something.

Ah, I thought the incoming value would already be sign-extended to 64-bits, but maybe not.

Quoted 6 lines

Casting it to a (int64_t) would only make it negative if the 64th bit was set correct?

But on a 56 bit decrementer you could load -1 as:
0x00FFFFFFFFFFFFFF
Which when cast to a (int64_t) wouldn't be negative.

Ok.

Quoted 328 lines
Quoted 61 lines
Quoted 61 lines
hw/ppc/ppc.c +3 -1
+    if (negative)
+        value |= (0xFFFFFFFFULL << nr_bits);
 
-    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
+    LOG_TB("%s: " TARGET_FMT_lx " => " TARGET_FMT_lx "\n",

__func__,

                 decr, value);
 
     if (kvm_enabled()) {
hw/ppc/ppc.c +0 -1
@@ -860,15 +883,15 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
      * an edge interrupt, so raise it here too.
      */
     if ((value < 3) ||
-        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value &

0x80000000)) ||
- ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value
& 0x80000000)

hw/ppc/ppc.c +1 -1
-          && !(decr & 0x80000000))) {
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && negative)
||
+        ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) &&
negative
+          && !(decr & (1ULL << (nr_bits - 1))))) {
         (*raise_excp)(cpu);
         return;
     }
 
     /* On MSB level based systems a 0 for the MSB stops interrupt

delivery */
- if (!(value & 0x80000000) && (tb_env->flags &
PPC_DECR_UNDERFLOW_LEVEL)) {

hw/ppc/ppc.c +4 -3
+    if (!negative && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
         (*lower_excp)(cpu);
     }
@@ -881,21 +904,24 @@ static void __cpu_ppc_store_decr(PowerPCCPU
 *cpu, uint64_t *nextp,
     timer_mod(timer, next);
 }
 
-static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t
 decr,
-                                       uint32_t value)
+static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu,
 target_ulong decr,
+                                       target_ulong value, int
 nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env-
 >decr_timer,
                          tb_env->decr_timer->cb,
 &cpu_ppc_decr_lower, decr,
-                         value);
+                         value, nr_bits);
 }
 
Quoted 11 lines
Quoted 8 lines
hw/ppc/ppc.c +3 -1
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    int nr_bits = 32;
+    if ((env->spr[SPR_LPCR] & LPCR_LD) && (env->large_decr_bits >

32))
+ nr_bits = env->large_decr_bits;

This would be simpler if you initialized large_decr_bits to 32 on cpus that don't have large decr, wouldn't it?

Correct, will do.

Quoted 32 lines
Quoted 32 lines
hw/ppc/ppc.c +1 -1
-    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
+    _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value,

nr_bits);
}

static void cpu_ppc_decr_cb(void *opaque)

hw/ppc/ppc.c +3 -3
@@ -905,23 +931,25 @@ static void cpu_ppc_decr_cb(void *opaque)
     cpu_ppc_decr_excp(cpu);
 }
 
-static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t
 hdecr,
-                                        uint32_t value)
+static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu,
 target_ulong hdecr,
+                                        target_ulong value, int
 nr_bits)
 {
     ppc_tb_t *tb_env = cpu->env.tb_env;
 
     if (tb_env->hdecr_timer != NULL) {
         __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env-
 >hdecr_timer,
                              tb_env->hdecr_timer->cb,
 &cpu_ppc_hdecr_lower,
-                             hdecr, value);
+                             hdecr, value, nr_bits);
     }
 }
 
Quoted 8 lines

hw/ppc/ppc.c static void cpu_ppc_decr_cb(void *opaque) · line 950

Quoted 6 lines
hw/ppc/ppc.c +3 -1
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+    int nr_bits = (pcc->hdecr_bits > 32) ? pcc->hdecr_bits : 32;

Similarly with hdecr_bits.

Yep

Quoted 57 lines
Quoted 57 lines
hw/ppc/ppc.c +1 -1
-    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
+    _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value,

nr_bits);
}

static void cpu_ppc_hdecr_cb(void *opaque)

hw/ppc/ppc.c +2 -2
@@ -951,8 +979,8 @@ static void cpu_ppc_set_tb_clk (void *opaque,
 uint32_t freq)
      * if a decrementer exception is pending when it enables
 msr_ee at startup,
      * it's not ready to handle it...
      */
-    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
-    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
+    _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
+    _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32);
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);

}

hw/ppc/spapr.c +3 -0
index acf62a2b9f..966bc74e68 100644
@@ -557,6 +557,14 @@ static void spapr_populate_cpu_dt(CPUState
 *cs, void *fdt, int offset,
                           pcc->radix_page_info->count *
                           sizeof(radix_AP_encodings[0]))));
     }
+
+    /*
+     * We set this property to let the guest know that it can use

the large

hw/ppc/spapr.c +5 -0
+     * decrementer and its width in bits.
+     */
+    if (env->large_decr_bits)
+        _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
+                              env->large_decr_bits)));
 }
 
 static void spapr_populate_cpus_dt_node(void *fdt,

sPAPRMachineState *spapr)

hw/ppc/spapr_caps.c +5 -1
index 1545a02729..44542fdbb2 100644
@@ -421,8 +421,43 @@ static void
 cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
 static void cap_large_decr_apply(sPAPRMachineState *spapr,
                                  uint8_t val, Error **errp)
 {
-    if (val)
+    PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
+
+    if (!val)
+        return; /* Disabled by default */
Quoted 8 lines

hw/ppc/spapr_caps.c static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr, · line 432

Quoted 4 lines
hw/ppc/spapr_caps.c +4 -0
+
+    if (tcg_enabled()) {
+        if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
+                              spapr->max_compat_pvr)) {

IIUC this is strictly redundant with the check against hdecr_bits, yes, but will result in a more helpful error message. Is that right?

The error message will be more useful.
Also if I go and set hdecr_bits to 32 as suggested above the P9 check is required because otherwise you could set a 32 bit large decr on P7/P8.

Quoted 138 lines
Quoted 136 lines
hw/ppc/spapr_caps.c +11 -1
+            error_setg(errp,
+                "Large decrementer only supported on POWER9, try
-cpu POWER9");
+            return;
+        }
+        if ((val < 32) || (val > pcc->hdecr_bits)) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-
 large-decr=%d",
+                pcc->hdecr_bits);
+            return;
+        }
+    } else {
         error_setg(errp, "No large decrementer support, try cap-

large-decr=0");

hw/ppc/spapr_caps.c +17 -0
+    }
+}
+
+static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
+                                     PowerPCCPU *cpu,
+                                     uint8_t val, Error **errp)
+{
+    CPUPPCState *env = &cpu->env;
+    target_ulong lpcr = env->spr[SPR_LPCR];
+
+    if (val)
+        lpcr |= LPCR_LD;
+    else
+        lpcr &= ~LPCR_LD;
+    ppc_store_lpcr(cpu, lpcr);
+    env->large_decr_bits = val;
 }
 
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
@@ -511,6 +546,7 @@ sPAPRCapabilityInfo
 capability_table[SPAPR_CAP_NUM] = {
         .set = spapr_cap_set_uint8,
         .type = "int",
         .apply = cap_large_decr_apply,
+        .cpu_apply = cap_large_decr_cpu_apply,
     },
 };
target/ppc/cpu-qom.h +1 -0
index ae51fe754e..cced705e30 100644
@@ -190,6 +190,7 @@ typedef struct PowerPCCPUClass {
 #endif
     const PPCHash64Options *hash64_opts;
     struct ppc_radix_page_info *radix_page_info;
+    uint32_t hdecr_bits;
     void (*init_proc)(CPUPPCState *env);
     int  (*check_pow)(CPUPPCState *env);
     int (*handle_mmu_fault)(PowerPCCPU *cpu, vaddr eaddr, int rwx,

int mmu_idx);

target/ppc/cpu.h +7 -4
index 26604ddf98..8da333e9da 100644
@@ -1171,6 +1171,9 @@ struct CPUPPCState {
     uint32_t tm_vscr;
     uint64_t tm_dscr;
     uint64_t tm_tar;
+
+    /* Large Decrementer */
+    int large_decr_bits;
 };
 
 #define SET_FIT_PERIOD(a_, b_, c_, d_)          \
@@ -1321,10 +1324,10 @@ uint32_t cpu_ppc_load_atbu (CPUPPCState
 *env);
 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value);
 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value);
 bool ppc_decr_clear_on_delivery(CPUPPCState *env);
-uint32_t cpu_ppc_load_decr (CPUPPCState *env);
-void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value);
-uint32_t cpu_ppc_load_hdecr (CPUPPCState *env);
-void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value);
+target_ulong cpu_ppc_load_decr (CPUPPCState *env);
+void cpu_ppc_store_decr (CPUPPCState *env, target_ulong value);
+target_ulong cpu_ppc_load_hdecr (CPUPPCState *env);
+void cpu_ppc_store_hdecr (CPUPPCState *env, target_ulong value);
 uint64_t cpu_ppc_load_purr (CPUPPCState *env);
 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env);

uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env);

target/ppc/mmu-hash64.c +0 -0
index c431303eff..a2b1ec5040 100644
@@ -1109,7 +1109,7 @@ void ppc_store_lpcr(PowerPCCPU *cpu,
 target_ulong val)
     case POWERPC_MMU_3_00: /* P9 */
         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD

|
(LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE |
LPCR_AIL |
- LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR
|
+ LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR

| LPCR_LD |
                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE |
LPCR_EEE |
                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE
| LPCR_TC |
                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE |

LPCR_HDICE);

target/ppc/translate.c +1 -1
index 819221f246..b156be4d98 100644
@@ -7417,7 +7417,7 @@ void ppc_cpu_dump_state(CPUState *cs, FILE
 *f, fprintf_function cpu_fprintf,
 #if !defined(NO_TIMER_DUMP)
     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
 #if !defined(CONFIG_USER_ONLY)
-                " DECR %08" PRIu32
+                " DECR " TARGET_FMT_lu
 #endif
                 "\n",

cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)

target/ppc/translate_init.inc.c +1 -0
index 58542c0fe0..4e0bf1f47a 100644
@@ -8926,6 +8926,7 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void
 *data)
     /* segment page size remain the same */
     pcc->hash64_opts = &ppc_hash64_opts_POWER7;
     pcc->radix_page_info = &POWER9_radix_page_info;
+    pcc->hdecr_bits = 56;
 #endif
     pcc->excp_model = POWERPC_EXCP_POWER9;
     pcc->bus_model = PPC_FLAGS_INPUT_POWER9;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#5 lore raw

PATCH 3/4 target/ppc: Implement large decrementer support for KVM

In reply to Suraj Jitindar Singh

Implement support to allow KVM guests to take advantage of the large decrementer introduced on POWER9 cpus.

To determine if the host can support the requested large decrementer size, we check it matches that specified in the ibm,dec-bits device-tree property. We also need to enable it in KVM by setting the LPCR_LD bit in the LPCR. Note that to do this we need to try and set the bit, then read it back to check the host allowed us to set it, if so we can use it but if we were unable to set it the host cannot support it and we must not use the large decrementer.

---
 hw/ppc/spapr_caps.c  | 17 +++++++++++++++--
 target/ppc/kvm.c     | 39 +++++++++++++++++++++++++++++++++++++++
 target/ppc/kvm_ppc.h | 12 ++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
hw/ppc/spapr_caps.c +15 -2
index 44542fdbb2..e07568fb94 100644
@@ -440,8 +440,16 @@ static void cap_large_decr_apply(sPAPRMachineState *spapr,
                 pcc->hdecr_bits);
             return;
         }
-    } else {
-        error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    } else if (kvm_enabled()) {
+        int kvm_nr_bits = kvmppc_get_cap_large_decr();
+
+        if (!kvm_nr_bits) {
+            error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+        } else if (val != kvm_nr_bits) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-large-decr=%d",
+                kvm_nr_bits);
+        }
     }
 }
 
@@ -452,6 +460,11 @@ static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
     CPUPPCState *env = &cpu->env;
     target_ulong lpcr = env->spr[SPR_LPCR];
 
+    if (kvm_enabled()) {
+        if (kvmppc_enable_cap_large_decr(cpu, !!val))
+            error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    }
+
     if (val)
         lpcr |= LPCR_LD;
     else
target/ppc/kvm.c +39 -0
index d01852fe31..3f650c8fc4 100644
@@ -91,6 +91,7 @@ static int cap_ppc_safe_cache;
 static int cap_ppc_safe_bounds_check;
 static int cap_ppc_safe_indirect_branch;
 static int cap_ppc_nested_kvm_hv;
+static int cap_large_decr;
 
 static uint32_t debug_inst_opcode;
 
@@ -124,6 +125,7 @@ static bool kvmppc_is_pr(KVMState *ks)
 
 static int kvm_ppc_register_host_cpu_type(MachineState *ms);
 static void kvmppc_get_cpu_characteristics(KVMState *s);
+static int kvmppc_get_dec_bits(void);
 
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
@@ -151,6 +153,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
     kvmppc_get_cpu_characteristics(s);
     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
+    cap_large_decr = kvmppc_get_dec_bits();
     /*
      * Note: setting it to false because there is not such capability
      * in KVM at this moment.
@@ -1927,6 +1930,15 @@ uint64_t kvmppc_get_clockfreq(void)
     return kvmppc_read_int_cpu_dt("clock-frequency");
 }
 
+static int kvmppc_get_dec_bits(void)
+{
+    int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
+
+    if (nr_bits > 0)
+        return nr_bits;
+    return 0;
+}
+
 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
  {
      PowerPCCPU *cpu = ppc_env_get_cpu(env);
@@ -2442,6 +2454,33 @@ bool kvmppc_has_cap_spapr_vfio(void)
     return cap_spapr_vfio;
 }
 
+int kvmppc_get_cap_large_decr(void)
+{
+    return cap_large_decr;
+}
+
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    CPUState *cs = CPU(cpu);
+    uint64_t lpcr;
+
+    kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+    /* Do we need to modify the LPCR? */
+    if (!!(lpcr & LPCR_LD) != !!enable) {
+        if (enable)
+            lpcr |= LPCR_LD;
+        else
+            lpcr &= ~LPCR_LD;
+        kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+        kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+
+        if (!!(lpcr & LPCR_LD) != !!enable)
+            return -1;
+    }
+
+    return 0;
+}
+
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
 {
     uint32_t host_pvr = mfpvr();
target/ppc/kvm_ppc.h +12 -0
index bdfaa4e70a..a79835bd14 100644
@@ -64,6 +64,8 @@ int kvmppc_get_cap_safe_bounds_check(void);
 int kvmppc_get_cap_safe_indirect_branch(void);
 bool kvmppc_has_cap_nested_kvm_hv(void);
 int kvmppc_set_cap_nested_kvm_hv(int enable);
+int kvmppc_get_cap_large_decr(void);
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable);
 int kvmppc_enable_hwrng(void);
 int kvmppc_put_books_sregs(PowerPCCPU *cpu);
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void);
@@ -332,6 +334,16 @@ static inline int kvmppc_set_cap_nested_kvm_hv(int enable)
     return -1;
 }
 
+static inline int kvmppc_get_cap_large_decr(void)
+{
+    return 0;
+}
+
+static inline int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    return -1;
+}
+
 static inline int kvmppc_enable_hwrng(void)
 {
     return -1;
Signature · 1 line
2.13.6
Reviewed-by on v1 3/4
#6 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 3/4] target/ppc: Implement large decrementer support for KVM

Quoted 11 lines · Suraj Jitindar Singh

Implement support to allow KVM guests to take advantage of the large decrementer introduced on POWER9 cpus.

To determine if the host can support the requested large decrementer size, we check it matches that specified in the ibm,dec-bits device-tree property. We also need to enable it in KVM by setting the LPCR_LD bit in the LPCR. Note that to do this we need to try and set the bit, then read it back to check the host allowed us to set it, if so we can use it but if we were unable to set it the host cannot support it and we must not use the large decrementer.

Quoted 2 lines

Although changes might be necessary to match it to things I've suggested in the earlier patches in the series.

Is the KVM side support for this already merged? If so, as of when?

Quoted 149 lines
---
 hw/ppc/spapr_caps.c  | 17 +++++++++++++++--
 target/ppc/kvm.c     | 39 +++++++++++++++++++++++++++++++++++++++
 target/ppc/kvm_ppc.h | 12 ++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
hw/ppc/spapr_caps.c +15 -2
index 44542fdbb2..e07568fb94 100644
@@ -440,8 +440,16 @@ static void cap_large_decr_apply(sPAPRMachineState *spapr,
                 pcc->hdecr_bits);
             return;
         }
-    } else {
-        error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    } else if (kvm_enabled()) {
+        int kvm_nr_bits = kvmppc_get_cap_large_decr();
+
+        if (!kvm_nr_bits) {
+            error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+        } else if (val != kvm_nr_bits) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-large-decr=%d",
+                kvm_nr_bits);
+        }
     }
 }
 
@@ -452,6 +460,11 @@ static void cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
     CPUPPCState *env = &cpu->env;
     target_ulong lpcr = env->spr[SPR_LPCR];
 
+    if (kvm_enabled()) {
+        if (kvmppc_enable_cap_large_decr(cpu, !!val))
+            error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+    }
+
     if (val)
         lpcr |= LPCR_LD;
     else
target/ppc/kvm.c +39 -0
index d01852fe31..3f650c8fc4 100644
@@ -91,6 +91,7 @@ static int cap_ppc_safe_cache;
 static int cap_ppc_safe_bounds_check;
 static int cap_ppc_safe_indirect_branch;
 static int cap_ppc_nested_kvm_hv;
+static int cap_large_decr;
 
 static uint32_t debug_inst_opcode;
 
@@ -124,6 +125,7 @@ static bool kvmppc_is_pr(KVMState *ks)
 
 static int kvm_ppc_register_host_cpu_type(MachineState *ms);
 static void kvmppc_get_cpu_characteristics(KVMState *s);
+static int kvmppc_get_dec_bits(void);
 
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
@@ -151,6 +153,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
     kvmppc_get_cpu_characteristics(s);
     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
+    cap_large_decr = kvmppc_get_dec_bits();
     /*
      * Note: setting it to false because there is not such capability
      * in KVM at this moment.
@@ -1927,6 +1930,15 @@ uint64_t kvmppc_get_clockfreq(void)
     return kvmppc_read_int_cpu_dt("clock-frequency");
 }
 
+static int kvmppc_get_dec_bits(void)
+{
+    int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
+
+    if (nr_bits > 0)
+        return nr_bits;
+    return 0;
+}
+
 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
  {
      PowerPCCPU *cpu = ppc_env_get_cpu(env);
@@ -2442,6 +2454,33 @@ bool kvmppc_has_cap_spapr_vfio(void)
     return cap_spapr_vfio;
 }
 
+int kvmppc_get_cap_large_decr(void)
+{
+    return cap_large_decr;
+}
+
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    CPUState *cs = CPU(cpu);
+    uint64_t lpcr;
+
+    kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+    /* Do we need to modify the LPCR? */
+    if (!!(lpcr & LPCR_LD) != !!enable) {
+        if (enable)
+            lpcr |= LPCR_LD;
+        else
+            lpcr &= ~LPCR_LD;
+        kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+        kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+
+        if (!!(lpcr & LPCR_LD) != !!enable)
+            return -1;
+    }
+
+    return 0;
+}
+
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
 {
     uint32_t host_pvr = mfpvr();
target/ppc/kvm_ppc.h +12 -0
index bdfaa4e70a..a79835bd14 100644
@@ -64,6 +64,8 @@ int kvmppc_get_cap_safe_bounds_check(void);
 int kvmppc_get_cap_safe_indirect_branch(void);
 bool kvmppc_has_cap_nested_kvm_hv(void);
 int kvmppc_set_cap_nested_kvm_hv(int enable);
+int kvmppc_get_cap_large_decr(void);
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable);
 int kvmppc_enable_hwrng(void);
 int kvmppc_put_books_sregs(PowerPCCPU *cpu);
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void);
@@ -332,6 +334,16 @@ static inline int kvmppc_set_cap_nested_kvm_hv(int enable)
     return -1;
 }
 
+static inline int kvmppc_get_cap_large_decr(void)
+{
+    return 0;
+}
+
+static inline int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    return -1;
+}
+
 static inline int kvmppc_enable_hwrng(void)
 {
     return -1;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 3/4] target/ppc: Implement large decrementer support for KVM

Quoted 22 lines · David Gibson
Quoted 18 lines · Suraj Jitindar Singh

Implement support to allow KVM guests to take advantage of the large
decrementer introduced on POWER9 cpus.

To determine if the host can support the requested large decrementer
size, we check it matches that specified in the ibm,dec-bits device-tree
property. We also need to enable it in KVM by setting the LPCR_LD bit in
the LPCR. Note that to do this we need to try and set the bit, then read
it back to check the host allowed us to set it, if so we can use it but
if we were unable to set it the host cannot support it and we must not
use the large decrementer.

Quoted 2 lines
Quoted 6 lines

Although changes might be necessary to match it to things I've suggested in the earlier patches in the series.

Is the KVM side support for this already merged? If so, as of when?

Yes, as of v4.13

Quoted 165 lines
Quoted 163 lines
---
 hw/ppc/spapr_caps.c  | 17 +++++++++++++++--
 target/ppc/kvm.c     | 39 +++++++++++++++++++++++++++++++++++++++
 target/ppc/kvm_ppc.h | 12 ++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
hw/ppc/spapr_caps.c +8 -2
index 44542fdbb2..e07568fb94 100644
@@ -440,8 +440,16 @@ static void
 cap_large_decr_apply(sPAPRMachineState *spapr,
                 pcc->hdecr_bits);
             return;
         }
-    } else {
-        error_setg(errp, "No large decrementer support, try cap-
 large-decr=0");
+    } else if (kvm_enabled()) {
+        int kvm_nr_bits = kvmppc_get_cap_large_decr();
+
+        if (!kvm_nr_bits) {
+            error_setg(errp, "No large decrementer support, try
 cap-large-decr=0");
+        } else if (val != kvm_nr_bits) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-

large-decr=%d",

hw/ppc/spapr_caps.c +7 -0
+                kvm_nr_bits);
+        }
     }
 }
@@ -452,6 +460,11 @@ static void
 cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
     CPUPPCState *env = &cpu->env;
     target_ulong lpcr = env->spr[SPR_LPCR];
 
+    if (kvm_enabled()) {
+        if (kvmppc_enable_cap_large_decr(cpu, !!val))
+            error_setg(errp, "No large decrementer support, try
 cap-large-decr=0");
+    }
+
     if (val)
         lpcr |= LPCR_LD;
     else
target/ppc/kvm.c +3 -0
index d01852fe31..3f650c8fc4 100644
@@ -91,6 +91,7 @@ static int cap_ppc_safe_cache;
 static int cap_ppc_safe_bounds_check;
 static int cap_ppc_safe_indirect_branch;
 static int cap_ppc_nested_kvm_hv;
+static int cap_large_decr;
 
 static uint32_t debug_inst_opcode;
 
@@ -124,6 +125,7 @@ static bool kvmppc_is_pr(KVMState *ks)
 
 static int kvm_ppc_register_host_cpu_type(MachineState *ms);
 static void kvmppc_get_cpu_characteristics(KVMState *s);
+static int kvmppc_get_dec_bits(void);
 
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
@@ -151,6 +153,7 @@ int kvm_arch_init(MachineState *ms, KVMState
 *s)
     cap_resize_hpt = kvm_vm_check_extension(s,
 KVM_CAP_SPAPR_RESIZE_HPT);
     kvmppc_get_cpu_characteristics(s);
     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s,
 KVM_CAP_PPC_NESTED_HV);
+    cap_large_decr = kvmppc_get_dec_bits();

/*
* Note: setting it to false because there is not such
capability
* in KVM at this moment.

target/ppc/kvm.c +9 -0
@@ -1927,6 +1930,15 @@ uint64_t kvmppc_get_clockfreq(void)
     return kvmppc_read_int_cpu_dt("clock-frequency");
 }
 
+static int kvmppc_get_dec_bits(void)
+{
+    int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
+
+    if (nr_bits > 0)
+        return nr_bits;
+    return 0;
+}
+
 static int kvmppc_get_pvinfo(CPUPPCState *env, struct
 kvm_ppc_pvinfo *pvinfo)
  {
      PowerPCCPU *cpu = ppc_env_get_cpu(env);
target/ppc/kvm.c +27 -0
@@ -2442,6 +2454,33 @@ bool kvmppc_has_cap_spapr_vfio(void)
     return cap_spapr_vfio;
 }
 
+int kvmppc_get_cap_large_decr(void)
+{
+    return cap_large_decr;
+}
+
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    CPUState *cs = CPU(cpu);
+    uint64_t lpcr;
+
+    kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+    /* Do we need to modify the LPCR? */
+    if (!!(lpcr & LPCR_LD) != !!enable) {
+        if (enable)
+            lpcr |= LPCR_LD;
+        else
+            lpcr &= ~LPCR_LD;
+        kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+        kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+
+        if (!!(lpcr & LPCR_LD) != !!enable)
+            return -1;
+    }
+
+    return 0;
+}
+
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
 {
     uint32_t host_pvr = mfpvr();
target/ppc/kvm_ppc.h +12 -0
index bdfaa4e70a..a79835bd14 100644
@@ -64,6 +64,8 @@ int kvmppc_get_cap_safe_bounds_check(void);
 int kvmppc_get_cap_safe_indirect_branch(void);
 bool kvmppc_has_cap_nested_kvm_hv(void);
 int kvmppc_set_cap_nested_kvm_hv(int enable);
+int kvmppc_get_cap_large_decr(void);
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable);
 int kvmppc_enable_hwrng(void);
 int kvmppc_put_books_sregs(PowerPCCPU *cpu);
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void);
@@ -332,6 +334,16 @@ static inline int
 kvmppc_set_cap_nested_kvm_hv(int enable)
     return -1;
 }
 
+static inline int kvmppc_get_cap_large_decr(void)
+{
+    return 0;
+}
+
+static inline int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu,
 int enable)
+{
+    return -1;
+}
+
 static inline int kvmppc_enable_hwrng(void)
 {
     return -1;
#8 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 3/4] target/ppc: Implement large decrementer support for KVM

Quoted 23 lines · Suraj Jitindar Singh
Quoted 22 lines · David Gibson
Quoted 20 lines · Suraj Jitindar Singh

Implement support to allow KVM guests to take advantage of the large
decrementer introduced on POWER9 cpus.

To determine if the host can support the requested large decrementer
size, we check it matches that specified in the ibm,dec-bits device-tree
property. We also need to enable it in KVM by setting the LPCR_LD bit in
the LPCR. Note that to do this we need to try and set the bit, then read
it back to check the host allowed us to set it, if so we can use it but
if we were unable to set it the host cannot support it and we must not
use the large decrementer.

Quoted 8 lines
Quoted 6 lines

Although changes might be necessary to match it to things I've suggested in the earlier patches in the series.

Is the KVM side support for this already merged? If so, as of when?

Yes, as of v4.13

Ah, so already in RHEL8. Great!

Quoted 167 lines
Quoted 166 lines
Quoted 164 lines

---
 hw/ppc/spapr_caps.c  | 17 +++++++++++++++--
 target/ppc/kvm.c     | 39 +++++++++++++++++++++++++++++++++++++++
 target/ppc/kvm_ppc.h | 12 ++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)
hw/ppc/spapr_caps.c +8 -2
index 44542fdbb2..e07568fb94 100644
@@ -440,8 +440,16 @@ static void
 cap_large_decr_apply(sPAPRMachineState *spapr,
                 pcc->hdecr_bits);
             return;
         }
-    } else {
-        error_setg(errp, "No large decrementer support, try cap-
 large-decr=0");
+    } else if (kvm_enabled()) {
+        int kvm_nr_bits = kvmppc_get_cap_large_decr();
+
+        if (!kvm_nr_bits) {
+            error_setg(errp, "No large decrementer support, try
 cap-large-decr=0");
+        } else if (val != kvm_nr_bits) {
+            error_setg(errp,
+                "Large decrementer size unsupported, try -cap-

large-decr=%d",

hw/ppc/spapr_caps.c +7 -0
+                kvm_nr_bits);
+        }
     }
 }
@@ -452,6 +460,11 @@ static void
 cap_large_decr_cpu_apply(sPAPRMachineState *spapr,
     CPUPPCState *env = &cpu->env;
     target_ulong lpcr = env->spr[SPR_LPCR];
 
+    if (kvm_enabled()) {
+        if (kvmppc_enable_cap_large_decr(cpu, !!val))
+            error_setg(errp, "No large decrementer support, try
 cap-large-decr=0");
+    }
+
     if (val)
         lpcr |= LPCR_LD;
     else
target/ppc/kvm.c +3 -0
index d01852fe31..3f650c8fc4 100644
@@ -91,6 +91,7 @@ static int cap_ppc_safe_cache;
 static int cap_ppc_safe_bounds_check;
 static int cap_ppc_safe_indirect_branch;
 static int cap_ppc_nested_kvm_hv;
+static int cap_large_decr;
 
 static uint32_t debug_inst_opcode;
 
@@ -124,6 +125,7 @@ static bool kvmppc_is_pr(KVMState *ks)
 
 static int kvm_ppc_register_host_cpu_type(MachineState *ms);
 static void kvmppc_get_cpu_characteristics(KVMState *s);
+static int kvmppc_get_dec_bits(void);
 
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
@@ -151,6 +153,7 @@ int kvm_arch_init(MachineState *ms, KVMState
 *s)
     cap_resize_hpt = kvm_vm_check_extension(s,
 KVM_CAP_SPAPR_RESIZE_HPT);
     kvmppc_get_cpu_characteristics(s);
     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s,
 KVM_CAP_PPC_NESTED_HV);
+    cap_large_decr = kvmppc_get_dec_bits();

/*
* Note: setting it to false because there is not such
capability
* in KVM at this moment.

target/ppc/kvm.c +9 -0
@@ -1927,6 +1930,15 @@ uint64_t kvmppc_get_clockfreq(void)
     return kvmppc_read_int_cpu_dt("clock-frequency");
 }
 
+static int kvmppc_get_dec_bits(void)
+{
+    int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
+
+    if (nr_bits > 0)
+        return nr_bits;
+    return 0;
+}
+
 static int kvmppc_get_pvinfo(CPUPPCState *env, struct
 kvm_ppc_pvinfo *pvinfo)
  {
      PowerPCCPU *cpu = ppc_env_get_cpu(env);
target/ppc/kvm.c +27 -0
@@ -2442,6 +2454,33 @@ bool kvmppc_has_cap_spapr_vfio(void)
     return cap_spapr_vfio;
 }
 
+int kvmppc_get_cap_large_decr(void)
+{
+    return cap_large_decr;
+}
+
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
+{
+    CPUState *cs = CPU(cpu);
+    uint64_t lpcr;
+
+    kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+    /* Do we need to modify the LPCR? */
+    if (!!(lpcr & LPCR_LD) != !!enable) {
+        if (enable)
+            lpcr |= LPCR_LD;
+        else
+            lpcr &= ~LPCR_LD;
+        kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+        kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
+
+        if (!!(lpcr & LPCR_LD) != !!enable)
+            return -1;
+    }
+
+    return 0;
+}
+
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
 {
     uint32_t host_pvr = mfpvr();
target/ppc/kvm_ppc.h +12 -0
index bdfaa4e70a..a79835bd14 100644
@@ -64,6 +64,8 @@ int kvmppc_get_cap_safe_bounds_check(void);
 int kvmppc_get_cap_safe_indirect_branch(void);
 bool kvmppc_has_cap_nested_kvm_hv(void);
 int kvmppc_set_cap_nested_kvm_hv(int enable);
+int kvmppc_get_cap_large_decr(void);
+int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable);
 int kvmppc_enable_hwrng(void);
 int kvmppc_put_books_sregs(PowerPCCPU *cpu);
 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void);
@@ -332,6 +334,16 @@ static inline int
 kvmppc_set_cap_nested_kvm_hv(int enable)
     return -1;
 }
 
+static inline int kvmppc_get_cap_large_decr(void)
+{
+    return 0;
+}
+
+static inline int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu,
 int enable)
+{
+    return -1;
+}
+
 static inline int kvmppc_enable_hwrng(void)
 {
     return -1;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#9 lore raw

PATCH 4/4 target/ppc/spapr: Enable the large decrementer by default on POWER9

In reply to Suraj Jitindar Singh

Enable the large decrementer by default on POWER9 cpu models. The default value applied is that provided in the cpu class.

---
 hw/ppc/spapr_caps.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr_caps.c +7 -0
index e07568fb94..f48aa367e3 100644
@@ -566,11 +566,18 @@ sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
                                                const char *cputype)
 {
+    PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(object_class_by_name(cputype));
     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     sPAPRCapabilities caps;
 
     caps = smc->default_caps;
 
+    caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = pcc->hdecr_bits;
+    if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_00,
+                               0, spapr->max_compat_pvr)) {
+        caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;
+    }
+
     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_07,
                                0, spapr->max_compat_pvr)) {
         caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
Signature · 1 line
2.13.6
#10 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 4/4] target/ppc/spapr: Enable the large decrementer by default on POWER9

Quoted 13 lines · Suraj Jitindar Singh

Enable the large decrementer by default on POWER9 cpu models. The default value applied is that provided in the cpu class.

---
 hw/ppc/spapr_caps.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr_caps.c +0 -0
index e07568fb94..f48aa367e3 100644

hw/ppc/spapr_caps.c sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = { · line 575

Quoted 10 lines
hw/ppc/spapr_caps.c +2 -0
@@ -566,11 +566,18 @@ sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
                                                const char *cputype)
 {
+    PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(object_class_by_name(cputype));
     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     sPAPRCapabilities caps;
 
     caps = smc->default_caps;
 
+    caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = pcc->hdecr_bits;

So, the rule with default_caps_with_cpu() is that it can reduce the value from the machine-global default, but never increase it (because that could change guest visible behaviour for existing machine versions).

I think the line above will do that.

hw/ppc/spapr_caps.c sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = { · line 578

Quoted 3 lines
hw/ppc/spapr_caps.c +3 -0
+    if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_00,
+                               0, spapr->max_compat_pvr)) {
+        caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;

And this part I think is redundant, because hdecr_bits won't be large for anything pre-POWER9.

Quoted 5 lines
hw/ppc/spapr_caps.c +2 -0
+    }
+
     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_07,
                                0, spapr->max_compat_pvr)) {
         caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#11 lore raw

Re: [Qemu-devel] [QEMU-PPC] [PATCH 4/4] target/ppc/spapr: Enable the large decrementer by default on POWER9

Quoted 27 lines · David Gibson
Quoted 26 lines · Suraj Jitindar Singh

Enable the large decrementer by default on POWER9 cpu models. The default value applied is that provided in the cpu class.

---
 hw/ppc/spapr_caps.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr_caps.c +1 -0
index e07568fb94..f48aa367e3 100644
@@ -566,11 +566,18 @@ sPAPRCapabilityInfo
 capability_table[SPAPR_CAP_NUM] = {
 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState
 *spapr,
                                                const char
 *cputype)
 {
+    PowerPCCPUClass *pcc =
 POWERPC_CPU_CLASS(object_class_by_name(cputype));
     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     sPAPRCapabilities caps;
 
     caps = smc->default_caps;
Quoted 8 lines

hw/ppc/spapr_caps.c sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = { · line 575

Quoted 1 line
hw/ppc/spapr_caps.c +1 -0
+    caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = pcc->hdecr_bits;

So, the rule with default_caps_with_cpu() is that it can reduce the value from the machine-global default, but never increase it (because that could change guest visible behaviour for existing machine versions).

I think the line above will do that.

Yep

Quoted 15 lines

hw/ppc/spapr_caps.c sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = { · line 578

Quoted 4 lines
hw/ppc/spapr_caps.c +3 -0
+    if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_00,
+                               0, spapr->max_compat_pvr)) {
+        caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;

And this part I think is redundant, because hdecr_bits won't be large for anything pre-POWER9.

Quoted 5 lines
hw/ppc/spapr_caps.c +2 -0
+    }
+
     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_07,
                                0, spapr->max_compat_pvr)) {
         caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
#12 lore raw
In reply to Suraj Jitindar Singh
Quoted 24 lines · Suraj Jitindar Singh

Add spapr_cap SPAPR_CAP_LARGE_DECREMENTER to be used to control the availability and size of the large decrementer made available to the guest.

---
 hw/ppc/spapr.c         |  2 ++
 hw/ppc/spapr_caps.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  5 ++++-
 3 files changed, 51 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +1 -0
index b6a571b6f1..acf62a2b9f 100644
@@ -2077,6 +2077,7 @@ static const VMStateDescription vmstate_spapr = {
         &vmstate_spapr_irq_map,
         &vmstate_spapr_cap_nested_kvm_hv,
         &vmstate_spapr_dtb,
+        &vmstate_spapr_cap_large_decr,
         NULL
     }
 };

hw/ppc/spapr.c static void spapr_machine_class_init(ObjectClass *oc, void *data) · line 4292

Quoted 4 lines
hw/ppc/spapr.c +1 -0
@@ -4288,6 +4289,7 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
     smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /* 64kiB */
     smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] = SPAPR_CAP_OFF;
+    smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;

This looks basically fine, but the name kind of suggests it's a boolean, whereas it's actually a number of bits. I wonder if just calling it "decrementer bits" would be clearer, with it defaulting to 32.

Quoted 104 lines
hw/ppc/spapr.c +0 -0
     spapr_caps_add_properties(smc, &error_abort);
     smc->irq = &spapr_irq_xics;
     smc->dr_phb_enabled = true;
hw/ppc/spapr_caps.c +45 -0
index 64f98ae68d..1545a02729 100644
@@ -182,6 +182,34 @@ static void spapr_cap_set_pagesize(Object *obj, Visitor *v, const char *name,
     spapr->eff.caps[cap->index] = val;
 }
 
+static void spapr_cap_get_uint8(Object *obj, Visitor *v, const char *name,
+                                void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    uint8_t val = spapr_get_cap(spapr, cap->index);
+
+    visit_type_uint8(v, name, &val, errp);
+}
+
+static void spapr_cap_set_uint8(Object *obj, Visitor *v, const char *name,
+                                void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    Error *local_err = NULL;
+    uint8_t val;
+
+    visit_type_uint8(v, name, &val, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    spapr->cmd_line_caps[cap->index] = true;
+    spapr->eff.caps[cap->index] = val;
+}
+
 static void cap_htm_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
 {
     if (!val) {
@@ -390,6 +418,13 @@ static void cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
     }
 }
 
+static void cap_large_decr_apply(sPAPRMachineState *spapr,
+                                 uint8_t val, Error **errp)
+{
+    if (val)
+        error_setg(errp, "No large decrementer support, try cap-large-decr=0");
+}
+
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
     [SPAPR_CAP_HTM] = {
         .name = "htm",
@@ -468,6 +503,15 @@ sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
         .type = "bool",
         .apply = cap_nested_kvm_hv_apply,
     },
+    [SPAPR_CAP_LARGE_DECREMENTER] = {
+        .name = "large-decr",
+        .description = "Size of Large Decrementer for the Guest (bits) 0=disabled",
+        .index = SPAPR_CAP_LARGE_DECREMENTER,
+        .get = spapr_cap_get_uint8,
+        .set = spapr_cap_set_uint8,
+        .type = "int",
+        .apply = cap_large_decr_apply,
+    },
 };
 
 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
@@ -596,6 +640,7 @@ SPAPR_CAP_MIG_STATE(cfpc, SPAPR_CAP_CFPC);
 SPAPR_CAP_MIG_STATE(sbbc, SPAPR_CAP_SBBC);
 SPAPR_CAP_MIG_STATE(ibs, SPAPR_CAP_IBS);
 SPAPR_CAP_MIG_STATE(nested_kvm_hv, SPAPR_CAP_NESTED_KVM_HV);
+SPAPR_CAP_MIG_STATE(large_decr, SPAPR_CAP_LARGE_DECREMENTER);
 
 void spapr_caps_init(sPAPRMachineState *spapr)
 {
include/hw/ppc/spapr.h +4 -1
index 59073a7579..8efc5e0779 100644
@@ -74,8 +74,10 @@ typedef enum {
 #define SPAPR_CAP_HPT_MAXPAGESIZE       0x06
 /* Nested KVM-HV */
 #define SPAPR_CAP_NESTED_KVM_HV         0x07
+/* Large Decrementer */
+#define SPAPR_CAP_LARGE_DECREMENTER     0x08
 /* Num Caps */
-#define SPAPR_CAP_NUM                   (SPAPR_CAP_NESTED_KVM_HV + 1)
+#define SPAPR_CAP_NUM                   (SPAPR_CAP_LARGE_DECREMENTER + 1)
 
 /*
  * Capability Values
@@ -828,6 +830,7 @@ extern const VMStateDescription vmstate_spapr_cap_cfpc;
 extern const VMStateDescription vmstate_spapr_cap_sbbc;
 extern const VMStateDescription vmstate_spapr_cap_ibs;
 extern const VMStateDescription vmstate_spapr_cap_nested_kvm_hv;
+extern const VMStateDescription vmstate_spapr_cap_large_decr;
 
 static inline uint8_t spapr_get_cap(sPAPRMachineState *spapr, int cap)
 {
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#13 lore raw
Quoted 32 lines · David Gibson
Quoted 31 lines · Suraj Jitindar Singh

Add spapr_cap SPAPR_CAP_LARGE_DECREMENTER to be used to control the availability and size of the large decrementer made available to the
guest.

---
 hw/ppc/spapr.c         |  2 ++
 hw/ppc/spapr_caps.c    | 45
+++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  5 ++++-
 3 files changed, 51 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +1 -0
index b6a571b6f1..acf62a2b9f 100644
@@ -2077,6 +2077,7 @@ static const VMStateDescription vmstate_spapr
 = {
         &vmstate_spapr_irq_map,
         &vmstate_spapr_cap_nested_kvm_hv,
         &vmstate_spapr_dtb,
+        &vmstate_spapr_cap_large_decr,
         NULL
     }

};

hw/ppc/spapr.c +0 -0
@@ -4288,6 +4289,7 @@ static void
 spapr_machine_class_init(ObjectClass *oc, void *data)
     smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
     smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /*

64kiB */

Quoted 8 lines
Quoted 3 lines
     smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] =

SPAPR_CAP_OFF;
+ smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;

This looks basically fine, but the name kind of suggests it's a boolean, whereas it's actually a number of bits. I wonder if just calling it "decrementer bits" would be clearer, with it defaulting to 32.

Yes, except there's a difference between a decrementer with 32 bits and a large decrementer with 32 bits...

SPAPR_CAP_LARGE_DECR_NR_BITS?

Quoted 120 lines
Quoted 118 lines
     spapr_caps_add_properties(smc, &error_abort);
     smc->irq = &spapr_irq_xics;
     smc->dr_phb_enabled = true;
hw/ppc/spapr_caps.c +28 -0
index 64f98ae68d..1545a02729 100644
@@ -182,6 +182,34 @@ static void spapr_cap_set_pagesize(Object
 *obj, Visitor *v, const char *name,
     spapr->eff.caps[cap->index] = val;
 }
 
+static void spapr_cap_get_uint8(Object *obj, Visitor *v, const
 char *name,
+                                void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    uint8_t val = spapr_get_cap(spapr, cap->index);
+
+    visit_type_uint8(v, name, &val, errp);
+}
+
+static void spapr_cap_set_uint8(Object *obj, Visitor *v, const
 char *name,
+                                void *opaque, Error **errp)
+{
+    sPAPRCapabilityInfo *cap = opaque;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
+    Error *local_err = NULL;
+    uint8_t val;
+
+    visit_type_uint8(v, name, &val, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    spapr->cmd_line_caps[cap->index] = true;
+    spapr->eff.caps[cap->index] = val;
+}
+

static void cap_htm_apply(sPAPRMachineState *spapr, uint8_t val,
Error **errp)

 {
     if (!val) {
hw/ppc/spapr_caps.c +7 -0
@@ -390,6 +418,13 @@ static void
 cap_nested_kvm_hv_apply(sPAPRMachineState *spapr,
     }
 }
 
+static void cap_large_decr_apply(sPAPRMachineState *spapr,
+                                 uint8_t val, Error **errp)
+{
+    if (val)
+        error_setg(errp, "No large decrementer support, try cap-
 large-decr=0");
+}
+
 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
     [SPAPR_CAP_HTM] = {
         .name = "htm",
hw/ppc/spapr_caps.c +9 -0
@@ -468,6 +503,15 @@ sPAPRCapabilityInfo
 capability_table[SPAPR_CAP_NUM] = {
         .type = "bool",
         .apply = cap_nested_kvm_hv_apply,
     },
+    [SPAPR_CAP_LARGE_DECREMENTER] = {
+        .name = "large-decr",
+        .description = "Size of Large Decrementer for the Guest
 (bits) 0=disabled",
+        .index = SPAPR_CAP_LARGE_DECREMENTER,
+        .get = spapr_cap_get_uint8,
+        .set = spapr_cap_set_uint8,
+        .type = "int",
+        .apply = cap_large_decr_apply,
+    },
 };

static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState
*spapr,

hw/ppc/spapr_caps.c +1 -0
@@ -596,6 +640,7 @@ SPAPR_CAP_MIG_STATE(cfpc, SPAPR_CAP_CFPC);
 SPAPR_CAP_MIG_STATE(sbbc, SPAPR_CAP_SBBC);
 SPAPR_CAP_MIG_STATE(ibs, SPAPR_CAP_IBS);
 SPAPR_CAP_MIG_STATE(nested_kvm_hv, SPAPR_CAP_NESTED_KVM_HV);
+SPAPR_CAP_MIG_STATE(large_decr, SPAPR_CAP_LARGE_DECREMENTER);
 
 void spapr_caps_init(sPAPRMachineState *spapr)
 {
include/hw/ppc/spapr.h +3 -1
index 59073a7579..8efc5e0779 100644
@@ -74,8 +74,10 @@ typedef enum {
 #define SPAPR_CAP_HPT_MAXPAGESIZE       0x06
 /* Nested KVM-HV */
 #define SPAPR_CAP_NESTED_KVM_HV         0x07
+/* Large Decrementer */
+#define SPAPR_CAP_LARGE_DECREMENTER     0x08
 /* Num Caps */
-#define SPAPR_CAP_NUM                   (SPAPR_CAP_NESTED_KVM_HV +
 1)
+#define
 SPAPR_CAP_NUM                   (SPAPR_CAP_LARGE_DECREMENTER + 1)
 

/*
* Capability Values

include/hw/ppc/spapr.h +1 -0
@@ -828,6 +830,7 @@ extern const VMStateDescription
 vmstate_spapr_cap_cfpc;
 extern const VMStateDescription vmstate_spapr_cap_sbbc;
 extern const VMStateDescription vmstate_spapr_cap_ibs;
 extern const VMStateDescription vmstate_spapr_cap_nested_kvm_hv;
+extern const VMStateDescription vmstate_spapr_cap_large_decr;
 
 static inline uint8_t spapr_get_cap(sPAPRMachineState *spapr, int

cap)
{

#14 lore raw
Quoted 42 lines · Suraj Jitindar Singh
Quoted 32 lines · David Gibson
Quoted 31 lines · Suraj Jitindar Singh

Add spapr_cap SPAPR_CAP_LARGE_DECREMENTER to be used to control the availability and size of the large decrementer made available to the
guest.

---
 hw/ppc/spapr.c         |  2 ++
 hw/ppc/spapr_caps.c    | 45
+++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  5 ++++-
 3 files changed, 51 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +1 -0
index b6a571b6f1..acf62a2b9f 100644
@@ -2077,6 +2077,7 @@ static const VMStateDescription vmstate_spapr
 = {
         &vmstate_spapr_irq_map,
         &vmstate_spapr_cap_nested_kvm_hv,
         &vmstate_spapr_dtb,
+        &vmstate_spapr_cap_large_decr,
         NULL
     }

};

hw/ppc/spapr.c +0 -0
@@ -4288,6 +4289,7 @@ static void
 spapr_machine_class_init(ObjectClass *oc, void *data)
     smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
     smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /*

64kiB */

Quoted 8 lines
Quoted 3 lines
     smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] =

SPAPR_CAP_OFF;
+ smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = 0;

This looks basically fine, but the name kind of suggests it's a boolean, whereas it's actually a number of bits. I wonder if just calling it "decrementer bits" would be clearer, with it defaulting to 32.

Quoted 2 lines

Yes, except there's a difference between a decrementer with 32 bits and a large decrementer with 32 bits...

Ok, and that difference is..?

Quoted 1 line

SPAPR_CAP_LARGE_DECR_NR_BITS?

That could work.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson