Skip to contentSkip to conversation

ppc: Add "native" POWER8 platform

PATCH In review Benjamin Herrenschmidt 11 participants 198 messages Nov 11, 2015 – Mar 17, 2017 Page 2 of 2

lore
Share & tools
lore
#59 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 21/77] ppc: Rework generation of priv and inval interrupts

In reply to Benjamin Herrenschmidt
Quoted 13 lines · Benjamin Herrenschmidt

Recent server processors use the Hypervisor Emulation Assistance interrupt for illegal instructions and *some* type of SPR accesses.

Also the code was always generating inval instructions even for priv violations due to setting the wrong flags

Finally, the checking for PR/HV was open coded everywhere.

This reworks it all, using little helper macros for checking, and adding the HV interrupt (which gets converted back to program check in the slow path of excp_helper.c on CPUs that don't want it).

[snip]

Quoted 11 lines

static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)

target-ppc/translate.c +1 -1
@@ -4340,7 +4350,7 @@ static inline void gen_op_mfspr(DisasContext *ctx)
                 printf("Trying to read privileged spr %d (0x%03x) at "
                        TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
             }
-            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
+            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
         }
     } else {
         /* Not defined */

target-ppc/translate.c static inline void gen_op_mfspr(DisasContext *ctx) · line 4360

Quoted 3 lines
target-ppc/translate.c +0 -0
@@ -4348,7 +4358,25 @@ static inline void gen_op_mfspr(DisasContext *ctx)
                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
         printf("Trying to read invalid spr %d (0x%03x) at "
                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);

So, I'm not 100% following the logic below, but it looks like the existing code used SPR_NOACCESS to mark things which generated a privilege exception compared to NULL for things which generated an invalid instruction exception. Using that encoding, can you simplify the logic here? Alternatively can you use the logic here to avoid the SPR_NOACESS encoding?

Quoted 9 lines
target-ppc/translate.c +8 -1
-        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+
+        /* The behaviour depends on MSR:PR and SPR# bit 0x10,
+         * it can generate a priv, a hv emu or a no-op
+         */
+        if (sprn & 0x10) {
+            if (ctx->pr) {
+                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }

target-ppc/translate.c static inline void gen_op_mfspr(DisasContext *ctx) · line 4378

Quoted 10 lines
target-ppc/translate.c +10 -0
+        } else {
+            if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
+                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }
+        }
+#if !defined(CONFIG_USER_ONLY)
+        /* HV priv */
+        if (ctx->spr_cb[sprn].hea_read) {
+            gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+        }

If you're in PR mode, and it's an SPR with an hea_read function and has the 0x10 bit set, won't this call gen_priv_exception twice?

I also see no path here which will call gen_inval_exception(), is that right? If you're in HV mode and it's a truly invalid SPRN, isn't that what you'd want?

Quoted 74 lines
target-ppc/translate.c +20 -19
+#endif
     }
 }
 
@@ -4395,13 +4423,9 @@ static void gen_mtcrf(DisasContext *ctx)
 #if defined(TARGET_PPC64)
 static void gen_mtmsrd(DisasContext *ctx)
 {
-#if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-        return;
-    }
+    CHK_SV;
+
+#if !defined(CONFIG_USER_ONLY)
     if (ctx->opcode & 0x00010000) {
         /* Special form that does not need any synchronisation */
         TCGv t0 = tcg_temp_new();
@@ -4420,20 +4444,16 @@ static void gen_mtmsrd(DisasContext *ctx)
         /* Note that mtmsr is not always defined as context-synchronizing */
         gen_stop_exception(ctx);
     }
-#endif
+#endif /* !defined(CONFIG_USER_ONLY) */
 }
-#endif
+#endif /* defined(TARGET_PPC64) */
 
 static void gen_mtmsr(DisasContext *ctx)
 {
-#if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-        return;
-    }
-    if (ctx->opcode & 0x00010000) {
+    CHK_SV;
+
+#if !defined(CONFIG_USER_ONLY)
+   if (ctx->opcode & 0x00010000) {
         /* Special form that does not need any synchronisation */
         TCGv t0 = tcg_temp_new();
         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
@@ -4488,7 +4508,7 @@ static void gen_mtspr(DisasContext *ctx)
                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
             printf("Trying to write privileged spr %d (0x%03x) at "
                    TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
-            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
+            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
         }
     } else {
         /* Not defined */
@@ -4496,7 +4516,25 @@ static void gen_mtspr(DisasContext *ctx)
                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
         printf("Trying to write invalid spr %d (0x%03x) at "
                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
-        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+
+        /* The behaviour depends on MSR:PR and SPR# bit 0x10,
+         * it can generate a priv, a hv emu or a no-op
+         */
+        if (sprn & 0x10) {
+            if (ctx->pr) {
+                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }
+        } else {

target-ppc/translate.c static void gen_mtspr(DisasContext *ctx) · line 4537

Quoted 10 lines
target-ppc/translate.c +10 -0
+            if (ctx->pr || sprn == 0) {
+                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }
+        }
+#if !defined(CONFIG_USER_ONLY)
+        /* HV priv */
+        if (ctx->spr_cb[sprn].hea_write) {
+            gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+        }
+#endif

Same concerns here as for mfspr.

[snip]

target-ppc/translate.c static void gen_slbmfev(DisasContext *ctx) · line 4880

Quoted 12 lines
target-ppc/translate.c +0 -0
 /* tlbiel */
 static void gen_tlbiel(DisasContext *ctx)
 {

#if defined(CONFIG_USER_ONLY)

target-ppc/translate.c +2 -5
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
 #else
-    if (unlikely(ctx->pr || !ctx->hv)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+    CHK_SV;

You have CHK_SV here, but the original code checks for HV, as does your new code for tlbia and tlbiel, is that right?

[snip]

Quoted 4 lines

/* tlbsync */
static void gen_tlbsync(DisasContext *ctx)
{
#if defined(CONFIG_USER_ONLY)

target-ppc/translate.c static void gen_tlbie(DisasContext *ctx) · line 4912

Quoted 10 lines
target-ppc/translate.c +4 -6
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+    GEN_PRIV;
+#else    
+    CHK_HV;
+

Old code didn't check for HV, mode, but AFAICT it should have, so this looks correct.

[snip]

Quoted 4 lines
target-ppc/translate.c +0 -0
@@ -5941,18 +5921,16 @@ static void gen_mfapidi(DisasContext *ctx)
 static void gen_tlbiva(DisasContext *ctx)
 {
 #if defined(CONFIG_USER_ONLY)

target-ppc/translate.c static void gen_mfapidi(DisasContext *ctx) · line 5928

Quoted 10 lines
target-ppc/translate.c +3 -5
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
 #else
     TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+
+    CHK_SV;

Is the same thing as tlbivax, or some ancient instruction? AFAICT the ISA says tlbivax is hypervisor privileged.

Quoted 7 lines
target-ppc/translate.c +1 -1
     t0 = tcg_temp_new();
     gen_addr_reg_index(ctx, t0);
     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
     tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
 }

[snip]

target-ppc/translate.c static void gen_mfsri(DisasContext *ctx) · line 5767

Quoted 13 lines

static void gen_tlbivax_booke206(DisasContext *ctx) {
#if defined(CONFIG_USER_ONLY)

target-ppc/translate.c +2 -5
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
 #else
     TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
 
+    CHK_SV;

ISA says tlbivax is hypervisor privileged when the CPU has a hypervisor mode, which I guess booke206 probably doesn't?

Quoted 12 lines
     t0 = tcg_temp_new();
     gen_addr_reg_index(ctx, t0);
target-ppc/translate.c +1 -2
-
     gen_helper_booke206_tlbivax(cpu_env, t0);
     tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
 }
 
 static void gen_tlbilx_booke206(DisasContext *ctx)
 {
 #if defined(CONFIG_USER_ONLY)

target-ppc/translate.c static void gen_tlbsx_booke206(DisasContext *ctx) · line 6557

Quoted 10 lines
target-ppc/translate.c +2 -5
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
 #else
     TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
 
+    CHK_SV;

And apparently hv vs. sv privilege of tlbilx depends on the EPCR register. Again, may not be relevant for 2.06.

Quoted 10 lines
     t0 = tcg_temp_new();
     gen_addr_reg_index(ctx, t0);
target-ppc/translate.c +1 -1
@@ -6672,7 +6574,7 @@ static void gen_tlbilx_booke206(DisasContext *ctx)
     }
 
     tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
 }
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
#65 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 22/77] ppc: Add real mode CI load/store instructions for P7 and P8

In reply to Benjamin Herrenschmidt
Quoted 46 lines · Benjamin Herrenschmidt

Those instructions are only available in hypervisor real mode and allow cache inhibited garded access to devices in that mode.

---
 target-ppc/cpu.h            |  4 +++-
 target-ppc/translate.c      | 56 +++++++++++++++++++++++++++++++++++----------
 target-ppc/translate_init.c |  6 +++--
 3 files changed, 51 insertions(+), 15 deletions(-)
target-ppc/cpu.h +3 -1
index 23479b1..3d22a4f 100644
@@ -1913,6 +1913,8 @@ enum {
     PPC_POPCNTB        = 0x0000000000001000ULL,
     /*   string load / store                                                 */
     PPC_STRING         = 0x0000000000002000ULL,
+    /*   real mode cache inhibited load / store                              */
+    PPC_CILDST         = 0x0000000000004000ULL,
 
     /* Floating-point unit extensions                                        */
     /*   Optional floating point instructions                                */
@@ -2027,7 +2029,7 @@ enum {
                         | PPC_MFAPIDI | PPC_TLBIVA | PPC_TLBIVAX \
                         | PPC_4xx_COMMON | PPC_40x_ICBT | PPC_RFMCI \
                         | PPC_RFDI | PPC_DCR | PPC_DCRX | PPC_DCRUX \
-                        | PPC_POPCNTWD)
+                        | PPC_POPCNTWD | PPC_CILDST)
 
     /* extended type values */
 
target-ppc/translate.c +1 -1
index 3f657b1..4d01fd0 100644
@@ -189,7 +189,7 @@ struct DisasContext {
     uint32_t opcode;
     uint32_t exception;
     /* Routine used to access memory */
-    bool pr, hv;
+    bool pr, hv, dr;
     int mem_idx;
     int access_type;
     /* Translation flags */

target-ppc/translate.c typedef struct opcode_t { · line 383

Quoted 4 lines
target-ppc/translate.c +1 -0
@@ -380,9 +380,11 @@ typedef struct opcode_t {
 #if defined(CONFIG_USER_ONLY)
 #define CHK_HV GEN_PRIV
 #define CHK_SV GEN_PRIV
+#define CHK_HVDR GEN_PRIV

I'm guessing this is supposed to be CHK_HVRM as below.

target-ppc/translate.c static void glue(gen_, name##u)(DisasContext *ctx) · line 2892

Quoted 13 lines
target-ppc/translate.c +2 -1
 #else
 #define CHK_HV do { if (unlikely(ctx->pr || !ctx->hv)) GEN_PRIV; } while(0)
 #define CHK_SV do { if (unlikely(ctx->pr))  GEN_PRIV; }  while(0)
+#define CHK_HVRM do { if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) GEN_PRIV; } while(0)
 #endif
 
 #define CHK_NONE
@@ -2887,7 +2889,7 @@ static void glue(gen_, name##u)(DisasContext *ctx)
 }
 
 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
-static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
+static void glue(gen_, name##ux)(DisasContext *ctx)                           \

Extraneous change.

Quoted 174 lines
target-ppc/translate.c +40 -10
 {                                                                             \
     TCGv EA;                                                                  \
     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
@@ -2903,18 +2905,23 @@ static void glue(gen_, name##ux)(DisasContext *ctx)
     tcg_temp_free(EA);                                                        \
 }
 
-#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
+#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
 static void glue(gen_, name##x)(DisasContext *ctx)                            \
 {                                                                             \
     TCGv EA;                                                                  \
+    chk;                                                                      \
     gen_set_access_type(ctx, ACCESS_INT);                                     \
     EA = tcg_temp_new();                                                      \
     gen_addr_reg_index(ctx, EA);                                              \
     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
     tcg_temp_free(EA);                                                        \
 }
+
 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
-    GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
+    GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
+
+#define GEN_LDX_HVRM(name, ldop, opc2, opc3, type)                            \
+    GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
 
 #define GEN_LDS(name, ldop, op, type)                                         \
 GEN_LD(name, ldop, op | 0x20, type);                                          \
@@ -2940,6 +2947,12 @@ GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
 /* ldx */
 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
 
+/* CI load/store variants */
+GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
+GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
+GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
+GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
+
 static void gen_ld(DisasContext *ctx)
 {
     TCGv EA;
@@ -3058,10 +3071,11 @@ static void glue(gen_, name##ux)(DisasContext *ctx)
     tcg_temp_free(EA);                                                        \
 }
 
-#define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
+#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
 static void glue(gen_, name##x)(DisasContext *ctx)                            \
 {                                                                             \
     TCGv EA;                                                                  \
+    chk;                                                                      \
     gen_set_access_type(ctx, ACCESS_INT);                                     \
     EA = tcg_temp_new();                                                      \
     gen_addr_reg_index(ctx, EA);                                              \
@@ -3069,7 +3083,10 @@ static void glue(gen_, name##x)(DisasContext *ctx)                            \
     tcg_temp_free(EA);                                                        \
 }
 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
-    GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
+    GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
+
+#define GEN_STX_HVRM(name, stop, opc2, opc3, type)                            \
+    GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
 
 #define GEN_STS(name, stop, op, type)                                         \
 GEN_ST(name, stop, op | 0x20, type);                                          \
@@ -3086,6 +3103,10 @@ GEN_STS(stw, st32, 0x04, PPC_INTEGER);
 #if defined(TARGET_PPC64)
 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
+GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
+GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
+GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
+GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
 
 static void gen_std(DisasContext *ctx)
 {
@@ -3171,7 +3192,7 @@ static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
 }
-GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
+GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
 #endif  /* TARGET_PPC64 */
 
 /* sthbrx */
@@ -3197,7 +3218,7 @@ static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
 }
-GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
+GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
 #endif  /* TARGET_PPC64 */
 
 /***                    Integer load and store multiple                    ***/
@@ -10156,7 +10177,7 @@ GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
-#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
+#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
 #define GEN_LDS(name, ldop, op, type)                                         \
 GEN_LD(name, ldop, op | 0x20, type)                                           \
@@ -10173,7 +10194,13 @@ GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
-GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
+GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
+
+/* HV/P7 and later only */
+GEN_LDX_HVRM(ldcix, ld64, 0x15, 0x1b, PPC_CILDST)
+GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
+GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
+GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
 #endif
 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
@@ -10189,7 +10216,7 @@ GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
-#define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
+#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
 #define GEN_STS(name, stop, op, type)                                         \
 GEN_ST(name, stop, op | 0x20, type)                                           \
@@ -10203,7 +10230,11 @@ GEN_STS(stw, st32, 0x04, PPC_INTEGER)
 #if defined(TARGET_PPC64)
 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
-GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
+GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
+GEN_STX_HVRM(stdcix, st64, 0x15, 0x1f, PPC_CILDST)
+GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
+GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
+GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
 #endif
 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
@@ -11369,6 +11400,7 @@ void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
     ctx.spr_cb = env->spr_cb;
     ctx.pr = msr_pr;
     ctx.mem_idx = env->dmmu_idx;
+    ctx.dr = msr_dr;
 #if !defined(CONFIG_USER_ONLY)
     ctx.hv = !msr_pr && (msr_hv || !env->has_hv_mode);
 #endif
target-ppc/translate_init.c +4 -2
index 5210b25..8d82bc8 100644
@@ -8320,7 +8320,8 @@ POWERPC_FAMILY(POWER7)(ObjectClass *oc, void *data)
                        PPC_MEM_TLBIE | PPC_MEM_TLBSYNC |
                        PPC_64B | PPC_64H | PPC_ALTIVEC |
                        PPC_SEGMENT_64B | PPC_SLBI |
-                       PPC_POPCNTB | PPC_POPCNTWD;
+                       PPC_POPCNTB | PPC_POPCNTWD |
+                       PPC_CILDST;
     pcc->insns_flags2 = PPC2_VSX | PPC2_DFP | PPC2_DBRX | PPC2_ISA205 |
                         PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 |
                         PPC2_ATOMIC_ISA206 | PPC2_FP_CVT_ISA206 |
@@ -8397,7 +8398,8 @@ POWERPC_FAMILY(POWER8)(ObjectClass *oc, void *data)
                        PPC_MEM_TLBIE | PPC_MEM_TLBSYNC |
                        PPC_64B | PPC_64H | PPC_64BX | PPC_ALTIVEC |
                        PPC_SEGMENT_64B | PPC_SLBI |
-                       PPC_POPCNTB | PPC_POPCNTWD;
+                       PPC_POPCNTB | PPC_POPCNTWD |
+                       PPC_CILDST;
     pcc->insns_flags2 = PPC2_VSX | PPC2_VSX207 | PPC2_DFP | PPC2_DBRX |
                         PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 |
                         PPC2_ATOMIC_ISA206 | PPC2_FP_CVT_ISA206 |
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
Reviewed-by on v1 23/77
#68 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 23/77] ppc: Turn a bunch of booleans from int to bool

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt
Quoted 89 lines
---
 target-ppc/translate.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
target-ppc/translate.c +19 -20
index 4d01fd0..a5ab2eb 100644
@@ -189,21 +189,20 @@ struct DisasContext {
     uint32_t opcode;
     uint32_t exception;
     /* Routine used to access memory */
-    bool pr, hv, dr;
+    bool pr, hv, dr, le_mode;
     int mem_idx;
     int access_type;
     /* Translation flags */
-    int le_mode;
     TCGMemOp default_tcg_memop_mask;
 #if defined(TARGET_PPC64)
-    int sf_mode;
-    int has_cfar;
+    bool sf_mode;
+    bool has_cfar;
 #endif
-    int fpu_enabled;
-    int altivec_enabled;
-    int vsx_enabled;
-    int spe_enabled;
-    int tm_enabled;
+    bool fpu_enabled;
+    bool altivec_enabled;
+    bool vsx_enabled;
+    bool spe_enabled;
+    bool tm_enabled;
     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
     int singlestep_enabled;
     uint64_t insns_flags;
@@ -380,7 +379,7 @@ typedef struct opcode_t {
 #if defined(CONFIG_USER_ONLY)
 #define CHK_HV GEN_PRIV
 #define CHK_SV GEN_PRIV
-#define CHK_HVDR GEN_PRIV
+#define CHK_HVRM GEN_PRIV
 #else
 #define CHK_HV do { if (unlikely(ctx->pr || !ctx->hv)) GEN_PRIV; } while(0)
 #define CHK_SV do { if (unlikely(ctx->pr))  GEN_PRIV; }  while(0)
@@ -11407,31 +11406,31 @@ void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
     ctx.insns_flags = env->insns_flags;
     ctx.insns_flags2 = env->insns_flags2;
     ctx.access_type = -1;
-    ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
+    ctx.le_mode = !!(env->hflags & (1 << MSR_LE));
     ctx.default_tcg_memop_mask = ctx.le_mode ? MO_LE : MO_BE;
 #if defined(TARGET_PPC64)
     ctx.sf_mode = msr_is_64bit(env, env->msr);
     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
 #endif
-    ctx.fpu_enabled = msr_fp;
+    ctx.fpu_enabled = !!msr_fp;
     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
-        ctx.spe_enabled = msr_spe;
+        ctx.spe_enabled = !!msr_spe;
     else
-        ctx.spe_enabled = 0;
+        ctx.spe_enabled = false;
     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
-        ctx.altivec_enabled = msr_vr;
+        ctx.altivec_enabled = !!msr_vr;
     else
-        ctx.altivec_enabled = 0;
+        ctx.altivec_enabled = false;
     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
-        ctx.vsx_enabled = msr_vsx;
+        ctx.vsx_enabled = !!msr_vsx;
     } else {
-        ctx.vsx_enabled = 0;
+        ctx.vsx_enabled = false;
     }
 #if defined(TARGET_PPC64)
     if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
-        ctx.tm_enabled = msr_tm;
+        ctx.tm_enabled = !!msr_tm;
     } else {
-        ctx.tm_enabled = 0;
+        ctx.tm_enabled = false;
     }
 #endif
     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
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
#73 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 25/77] ppc: Add P7/P8 Power Management instructions

In reply to Benjamin Herrenschmidt
Quoted 5 lines · Benjamin Herrenschmidt

This adds the ISA 2.06 and later power management instructions (doze, nap, sleep and rvwinkle) and associated wakeup cause testing in LPCR

Looks fine, though I haven't checked against the ISA in detail.

Quoted 399 lines
---
 target-ppc/cpu.h            | 26 ++++++++++++-
 target-ppc/excp_helper.c    | 59 +++++++++++++++++++++++++++++
 target-ppc/helper.h         |  1 +
 target-ppc/translate.c      | 66 ++++++++++++++++++++++++++++++++
 target-ppc/translate_init.c | 92 ++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 241 insertions(+), 3 deletions(-)
target-ppc/cpu.h +25 -1
index 3d22a4f..a7236cf 100644
@@ -300,6 +300,15 @@ enum {
 };
 
 /*****************************************************************************/
+/* PM instructions */
+typedef enum {
+    PPC_PM_DOZE,
+    PPC_PM_NAP,
+    PPC_PM_SLEEP,
+    PPC_PM_RVWINKLE,
+} powerpc_pm_insn_t;
+
+/*****************************************************************************/
 /* Input pins model                                                          */
 typedef enum powerpc_input_t powerpc_input_t;
 enum powerpc_input_t {
@@ -490,6 +499,14 @@ struct ppc_slb_t {
 #define LPCR_LPES1        (1ull << (63-61))
 #define LPCR_AIL_SHIFT    (63-40)      /* Alternate interrupt location */
 #define LPCR_AIL          (3ull << LPCR_AIL_SHIFT)
+#define LPCR_P7_PECE0     (1ull << (63-49))
+#define LPCR_P7_PECE1     (1ull << (63-50))
+#define LPCR_P7_PECE2     (1ull << (63-51))
+#define LPCR_P8_PECE0     (1ull << (63-47))
+#define LPCR_P8_PECE1     (1ull << (63-48))
+#define LPCR_P8_PECE2     (1ull << (63-49))
+#define LPCR_P8_PECE3     (1ull << (63-50))
+#define LPCR_P8_PECE4     (1ull << (63-51))
 
 #define msr_sf   ((env->msr >> MSR_SF)   & 1)
 #define msr_isf  ((env->msr >> MSR_ISF)  & 1)
@@ -1126,6 +1143,11 @@ struct CPUPPCState {
      * instructions and SPRs are diallowed if MSR:HV is 0
      */
     bool has_hv_mode;
+    /* On P7/P8, set when in PM state, we need to handle resume
+     * in a special way (such as routing some resume causes to
+     * 0x100), so flag this here.
+     */
+    bool in_pm_state;
 #endif
 
     /* Those resources are used only during code translation */
@@ -2069,6 +2091,8 @@ enum {
     PPC2_FP_CVT_S64    = 0x0000000000010000ULL,
     /* Transactional Memory (ISA 2.07, Book II)                              */
     PPC2_TM            = 0x0000000000020000ULL,
+    /* Server PM instructgions (ISA 2.06, Book III)                          */
+    PPC2_PM_ISA206     = 0x0000000000040000ULL,
 
 #define PPC_TCG_INSNS2 (PPC2_BOOKE206 | PPC2_VSX | PPC2_PRCNTL | PPC2_DBRX | \
                         PPC2_ISA205 | PPC2_VSX207 | PPC2_PERM_ISA206 | \
@@ -2076,7 +2100,7 @@ enum {
                         PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206 | \
                         PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | \
                         PPC2_ALTIVEC_207 | PPC2_ISA207S | PPC2_DFP | \
-                        PPC2_FP_CVT_S64 | PPC2_TM)
+                        PPC2_FP_CVT_S64 | PPC2_TM | PPC2_PM_ISA206)
 };
 
 /*****************************************************************************/
target-ppc/excp_helper.c +59 -0
index 80a70f4..3f77df7 100644
@@ -100,6 +100,44 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)
     asrr0 = -1;
     asrr1 = -1;
 
+    /* check for special resume at 0x100 from doze/nap/sleep/winkle on P7/P8 */
+    if (env->in_pm_state) {
+        env->in_pm_state = false;
+
+        /* Pretend to be returning from doze always as we don't lose state */
+        msr |= (0x1ull << (63 - 47));
+
+        /* Non-machine check are routed to 0x100 with a wakeup cause
+         * encoded in SRR1
+         */
+        if (excp != POWERPC_EXCP_MCHECK) {
+            switch(excp) {
+            case POWERPC_EXCP_RESET:
+                msr |= 0x4ull << (63-45);
+                break;
+            case POWERPC_EXCP_EXTERNAL:
+                msr |= 0x8ull << (63-45);
+                break;
+            case POWERPC_EXCP_DECR:
+                msr |= 0x6ull << (63-45);
+                break;
+            case POWERPC_EXCP_SDOOR:
+                msr |= 0x5ull << (63-45);
+                break;
+            case POWERPC_EXCP_SDOOR_HV:
+                msr |= 0x3ull << (63-45);
+                break;
+            case POWERPC_EXCP_HV_MAINT:
+                msr |= 0xaull << (63-45);
+                break;
+            default:
+                cpu_abort(cs, "Unsupported exception %d in Power Save mode\n",
+                          excp);
+            }
+            excp = POWERPC_EXCP_RESET;
+        }
+    }
+
     /* Exception targetting modifiers
      *
      * LPES0 is supported on POWER7/8
@@ -898,6 +936,27 @@ void helper_store_msr(CPUPPCState *env, target_ulong val)
     }
 }
 
+#if defined(TARGET_PPC64)
+void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
+{
+    CPUState *cs;
+
+    cs = CPU(ppc_env_get_cpu(env));
+    cs->halted = 1;
+    env->in_pm_state = true;
+
+    /* Technically, nap doesn't set EE, but if we don't set it
+     * then ppc_hw_interrupt() won't deliver. We could add some
+     * other tests there based on LPCR but it's simpler to just
+     * whack EE in. It will be cleared by the 0x100 at wakeup
+     * anyway. It will still be observable by the guest in SRR1
+     * but this doesn't seem to be a problem.
+     */
+    env->msr |= (1ull << MSR_EE);
+    helper_raise_exception(env, EXCP_HLT);
+}
+#endif /* defined(TARGET_PPC64) */
+
 static inline void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr,
                           target_ulong msrm, int keep_msrh)
 {
target-ppc/helper.h +1 -0
index ff2d50b..8292dd8 100644
@@ -13,6 +13,7 @@ DEF_HELPER_1(rfci, void, env)
 DEF_HELPER_1(rfdi, void, env)
 DEF_HELPER_1(rfmci, void, env)
 #if defined(TARGET_PPC64)
+DEF_HELPER_2(pminsn, void, env, i32)
 DEF_HELPER_1(rfid, void, env)
 DEF_HELPER_1(hrfid, void, env)
 #endif
target-ppc/translate.c +66 -0
index ac62942..f76a0c3 100644
@@ -3567,6 +3567,68 @@ static void gen_wait(DisasContext *ctx)
     gen_exception_err(ctx, EXCP_HLT, 1);
 }
 
+#if defined(TARGET_PPC64)
+static void gen_doze(DisasContext *ctx)
+{
+#if defined(CONFIG_USER_ONLY)
+    GEN_PRIV;
+#else
+    TCGv_i32 t;
+
+    CHK_HV;
+    t = tcg_const_i32(PPC_PM_DOZE);
+    gen_helper_pminsn(cpu_env, t);
+    tcg_temp_free_i32(t);
+    gen_stop_exception(ctx);
+#endif /* defined(CONFIG_USER_ONLY) */
+}
+
+static void gen_nap(DisasContext *ctx)
+{
+#if defined(CONFIG_USER_ONLY)
+    GEN_PRIV;
+#else
+    TCGv_i32 t;
+
+    CHK_HV;
+    t = tcg_const_i32(PPC_PM_NAP);
+    gen_helper_pminsn(cpu_env, t);
+    tcg_temp_free_i32(t);
+    gen_stop_exception(ctx);
+#endif /* defined(CONFIG_USER_ONLY) */
+}
+
+static void gen_sleep(DisasContext *ctx)
+{
+#if defined(CONFIG_USER_ONLY)
+    GEN_PRIV;
+#else
+    TCGv_i32 t;
+
+    CHK_HV;
+    t = tcg_const_i32(PPC_PM_SLEEP);
+    gen_helper_pminsn(cpu_env, t);
+    tcg_temp_free_i32(t);
+    gen_stop_exception(ctx);
+#endif /* defined(CONFIG_USER_ONLY) */
+}
+
+static void gen_rvwinkle(DisasContext *ctx)
+{
+#if defined(CONFIG_USER_ONLY)
+    GEN_PRIV;
+#else
+    TCGv_i32 t;
+
+    CHK_HV;
+    t = tcg_const_i32(PPC_PM_RVWINKLE);
+    gen_helper_pminsn(cpu_env, t);
+    tcg_temp_free_i32(t);
+    gen_stop_exception(ctx);
+#endif /* defined(CONFIG_USER_ONLY) */
+}
+#endif /* #if defined(TARGET_PPC64) */
+
 /***                         Floating-point load                           ***/
 #define GEN_LDF(name, ldop, opc, type)                                        \
 static void glue(gen_, name)(DisasContext *ctx)                                       \
@@ -9828,6 +9890,10 @@ GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
 #if defined(TARGET_PPC64)
 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
+GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
+GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
+GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
+GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
 #endif
 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
target-ppc/translate_init.c +90 -2
index 8d82bc8..8a1ce85 100644
@@ -8297,10 +8297,45 @@ static bool ppc_pvr_match_power7(PowerPCCPUClass *pcc, uint32_t pvr)
     return false;
 }
 
+static bool cpu_has_work_POWER7(CPUState *cs)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    CPUPPCState *env = &cpu->env;
+
+    if (cs->halted) {
+        if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
+            return false;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_EXT)) &&
+            (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_DECR)) &&
+            (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_MCK)) &&
+            (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_HMI)) &&
+            (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
+            return true;
+        }
+        if (env->pending_interrupts & (1u << PPC_INTERRUPT_RESET)) {
+            return true;
+        }
+        return false;
+    } else {
+        return msr_ee && (cs->interrupt_request & CPU_INTERRUPT_HARD);
+    }
+}
+
 POWERPC_FAMILY(POWER7)(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
 
     dc->fw_name = "PowerPC,POWER7";
     dc->desc = "POWER7";
@@ -8309,6 +8344,7 @@ POWERPC_FAMILY(POWER7)(ObjectClass *oc, void *data)
     pcc->pcr_mask = PCR_COMPAT_2_05 | PCR_COMPAT_2_06;
     pcc->init_proc = init_proc_POWER7;
     pcc->check_pow = check_pow_nocheck;
+    cc->has_work = cpu_has_work_POWER7;
     pcc->insns_flags = PPC_INSNS_BASE | PPC_ISEL | PPC_STRING | PPC_MFTB |
                        PPC_FLOAT | PPC_FLOAT_FSEL | PPC_FLOAT_FRES |
                        PPC_FLOAT_FSQRT | PPC_FLOAT_FRSQRTE |
@@ -8325,7 +8361,8 @@ POWERPC_FAMILY(POWER7)(ObjectClass *oc, void *data)
     pcc->insns_flags2 = PPC2_VSX | PPC2_DFP | PPC2_DBRX | PPC2_ISA205 |
                         PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 |
                         PPC2_ATOMIC_ISA206 | PPC2_FP_CVT_ISA206 |
-                        PPC2_FP_TST_ISA206 | PPC2_FP_CVT_S64;
+                        PPC2_FP_TST_ISA206 | PPC2_FP_CVT_S64 |
+                        PPC2_PM_ISA206;
     pcc->msr_mask = (1ull << MSR_SF) |
                     (1ull << MSR_VR) |
                     (1ull << MSR_VSX) |
@@ -8375,10 +8412,53 @@ static bool ppc_pvr_match_power8(PowerPCCPUClass *pcc, uint32_t pvr)
     return false;
 }
 
+static bool cpu_has_work_POWER8(CPUState *cs)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    CPUPPCState *env = &cpu->env;
+
+    if (cs->halted) {
+        if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
+            return false;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_EXT)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_DECR)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_MCK)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_HMI)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_DOORBELL)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
+            return true;
+        }
+        if ((env->pending_interrupts & (1u << PPC_INTERRUPT_HDOORBELL)) &&
+            (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
+            return true;
+        }
+        if (env->pending_interrupts & (1u << PPC_INTERRUPT_RESET)) {
+            return true;
+        }
+        return false;
+    } else {
+        return msr_ee && (cs->interrupt_request & CPU_INTERRUPT_HARD);
+    }
+}
+
 POWERPC_FAMILY(POWER8)(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
 
     dc->fw_name = "PowerPC,POWER8";
     dc->desc = "POWER8";
@@ -8387,6 +8467,7 @@ POWERPC_FAMILY(POWER8)(ObjectClass *oc, void *data)
     pcc->pcr_mask = PCR_COMPAT_2_05 | PCR_COMPAT_2_06;
     pcc->init_proc = init_proc_POWER8;
     pcc->check_pow = check_pow_nocheck;
+    cc->has_work = cpu_has_work_POWER8;
     pcc->insns_flags = PPC_INSNS_BASE | PPC_ISEL | PPC_STRING | PPC_MFTB |
                        PPC_FLOAT | PPC_FLOAT_FSEL | PPC_FLOAT_FRES |
                        PPC_FLOAT_FSQRT | PPC_FLOAT_FRSQRTE |
@@ -8406,7 +8487,7 @@ POWERPC_FAMILY(POWER8)(ObjectClass *oc, void *data)
                         PPC2_FP_TST_ISA206 | PPC2_BCTAR_ISA207 |
                         PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
                         PPC2_ISA205 | PPC2_ISA207S | PPC2_FP_CVT_S64 |
-                        PPC2_TM;
+                        PPC2_TM | PPC2_PM_ISA206;
     pcc->msr_mask = (1ull << MSR_SF) |
                     (1ull << MSR_SHV) |
 		    (1ull << MSR_TM) |
@@ -8464,6 +8545,13 @@ void cpu_ppc_set_papr(PowerPCCPU *cpu)
     lpcr->default_value &= ~(LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_KBV);
     lpcr->default_value |= LPCR_LPES0 | LPCR_LPES1;
 
+    /* P7 and P8 has slightly different PECE bits, mostly because P8 adds
+     * bit 47 and 48 which are reserved on P7. Here we set them all, which
+     * will work as expected for both implementations
+     */
+    lpcr->default_value |= LPCR_P8_PECE0 | LPCR_P8_PECE1 | LPCR_P8_PECE2 |
+                           LPCR_P8_PECE3 | LPCR_P8_PECE4;
+
     /* We should be followed by a CPU reset but update the active value
      * just in case...
      */
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
#76 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 26/77] ppc/pnv: Add skeletton PowerNV platform

In reply to Benjamin Herrenschmidt
Quoted 4 lines · Benjamin Herrenschmidt

No devices yet, not even an interrupt controller, just to get started.

Quoted 8 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 hw/ppc/Makefile.objs              |   2 +
 hw/ppc/pnv.c                      | 600 ++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/pnv.h              |  36 +++
 4 files changed, 639 insertions(+)
 create mode 100644 hw/ppc/pnv.c
 create mode 100644 include/hw/ppc/pnv.h

Many of my comments below may be made irrelevant by later patches in the series.

Quoted 145 lines
default-configs/ppc64-softmmu.mak +1 -0
index bb71b23..96574c8 100644
@@ -40,6 +40,7 @@ CONFIG_I8259=y
 CONFIG_XILINX=y
 CONFIG_XILINX_ETHLITE=y
 CONFIG_PSERIES=y
+CONFIG_POWERNV=y
 CONFIG_PREP=y
 CONFIG_MAC=y
 CONFIG_E500=y
hw/ppc/Makefile.objs +2 -0
index c1ffc77..cd74c96 100644
@@ -4,6 +4,8 @@ obj-y += ppc.o ppc_booke.o
 obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
 obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o spapr_drc.o spapr_rng.o
+# IBM PowerNV
+obj-$(CONFIG_POWERNV) += pnv.o
 ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
 obj-y += spapr_pci_vfio.o
 endif
hw/ppc/pnv.c new file +114 -0
new file mode 100644
index 0000000..e68c9b1
@@ -0,0 +1,600 @@
+/*
+ * QEMU PowerPC PowerNV model
+ *
+ * Copyright (c) 2004-2007 Fabrice Bellard
+ * Copyright (c) 2007 Jocelyn Mayer
+ * Copyright (c) 2010 David Gibson, IBM Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+#include "sysemu/sysemu.h"
+#include "hw/hw.h"
+#include "hw/fw-path-provider.h"
+#include "elf.h"
+#include "net/net.h"
+#include "sysemu/block-backend.h"
+#include "sysemu/cpus.h"
+#include "sysemu/kvm.h"
+#include "sysemu/numa.h"
+#include "kvm_ppc.h"
+#include "mmu-hash64.h"
+#include "qom/cpu.h"
+
+#include "hw/boards.h"
+#include "hw/ppc/ppc.h"
+#include "hw/ppc/pnv.h"
+#include "hw/loader.h"
+
+#include "exec/address-spaces.h"
+#include "qemu/config-file.h"
+#include "qemu/error-report.h"
+#include "trace.h"
+#include "hw/nmi.h"
+
+#include "hw/compat.h"
+
+#include <libfdt.h>
+
+#define FDT_ADDR                0x01000000
+#define FDT_MAX_SIZE            0x00100000
+#define FW_MAX_SIZE             0x00400000
+#define FW_FILE_NAME            "skiboot.lid"
+#define KERNEL_FILE_NAME        "skiroot.lid"
+#define KERNEL_LOAD_ADDR        0x20000000
+
+#define TIMEBASE_FREQ           512000000ULL
+
+#define MAX_CPUS                255
+
+#define PHANDLE_XICP            0x00001111
+
+typedef struct sPowerNVMachineState sPowerNVMachineState;
+
+#define TYPE_POWERNV_MACHINE      "powernv-machine"
+#define POWERNV_MACHINE(obj) \
+    OBJECT_CHECK(sPowerNVMachineState, (obj), TYPE_POWERNV_MACHINE)
+
+/**
+ * sPowerNVMachineState:
+ */
+struct sPowerNVMachineState {
+    /*< private >*/
+    MachineState parent_obj;
+    PnvSystem sys;
+};
+
+static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
+                                     size_t maxsize)
+{
+    size_t maxcells = maxsize / sizeof(uint32_t);
+    int i, j, count;
+    uint32_t *p = prop;
+
+    for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
+        struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
+
+        if (!sps->page_shift) {
+            break;
+        }
+        for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
+            if (sps->enc[count].page_shift == 0) {
+                break;
+            }
+        }
+        if ((p - prop) >= (maxcells - 3 - count * 2)) {
+            break;
+        }
+        *(p++) = cpu_to_be32(sps->page_shift);
+        *(p++) = cpu_to_be32(sps->slb_enc);
+        *(p++) = cpu_to_be32(count);
+        for (j = 0; j < count; j++) {
+            *(p++) = cpu_to_be32(sps->enc[j].page_shift);
+            *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
+        }
+    }
+
+    return (p - prop) * sizeof(uint32_t);
+}

hw/ppc/pnv.c line 124

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+
+#define _FDT(exp) \
+    do { \
+        int ret = (exp);                                           \
+        if (ret < 0) {                                             \
+            fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
+                    #exp, fdt_strerror(ret));                      \
+            exit(1);                                               \
+        }                                                          \
+    } while (0)

We should probably make a file where helper routines used by both spapr.c and pnv.c can live.

Quoted 101 lines
hw/ppc/pnv.c new file +101 -0
+
+static void powernv_populate_memory_node(void *fdt, int nodeid, hwaddr start,
+                                         hwaddr size)
+{
+    /* Probablly bogus, need to match with what's going on in CPU nodes */
+    uint32_t chip_id[] = {
+        cpu_to_be32(0x0), cpu_to_be32(nodeid)
+    };
+    char *mem_name;
+    uint64_t mem_reg_property[2];
+
+    mem_reg_property[0] = cpu_to_be64(start);
+    mem_reg_property[1] = cpu_to_be64(size);
+
+    mem_name = g_strdup_printf("memory@"TARGET_FMT_lx, start);
+    _FDT((fdt_begin_node(fdt, mem_name)));
+    g_free(mem_name);
+    _FDT((fdt_property_string(fdt, "device_type", "memory")));
+    _FDT((fdt_property(fdt, "reg", mem_reg_property,
+                       sizeof(mem_reg_property))));
+    _FDT((fdt_property(fdt, "ibm,chip-id", chip_id, sizeof(chip_id))));
+    _FDT((fdt_end_node(fdt)));
+}
+
+static int powernv_populate_memory(void *fdt)
+{
+    hwaddr mem_start, node_size;
+    int i, nb_nodes = nb_numa_nodes;
+    NodeInfo *nodes = numa_info;
+    NodeInfo ramnode;
+
+    /* No NUMA nodes, assume there is just one node with whole RAM */
+    if (!nb_numa_nodes) {
+        nb_nodes = 1;
+        ramnode.node_mem = ram_size;
+        nodes = &ramnode;
+    }
+
+    for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
+        if (!nodes[i].node_mem) {
+            continue;
+        }
+        if (mem_start >= ram_size) {
+            node_size = 0;
+        } else {
+            node_size = nodes[i].node_mem;
+            if (node_size > ram_size - mem_start) {
+                node_size = ram_size - mem_start;
+            }
+        }
+        for ( ; node_size; ) {
+            hwaddr sizetmp = pow2floor(node_size);
+
+            /* mem_start != 0 here */
+            if (ctzl(mem_start) < ctzl(sizetmp)) {
+                sizetmp = 1ULL << ctzl(mem_start);
+            }
+
+            powernv_populate_memory_node(fdt, i, mem_start, sizetmp);
+            node_size -= sizetmp;
+            mem_start += sizetmp;
+        }
+    }
+
+    return 0;
+}
+
+static void powernv_create_cpu_node(void *fdt, CPUState *cs, int smt_threads)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    CPUPPCState *env = &cpu->env;
+    DeviceClass *dc = DEVICE_GET_CLASS(cs);
+    PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
+    uint32_t servers_prop[smt_threads];
+    uint32_t gservers_prop[smt_threads * 2];
+    int i, index = ppc_get_vcpu_dt_id(cpu);
+    uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
+                       0xffffffff, 0xffffffff};
+    uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
+    uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
+    uint32_t page_sizes_prop[64];
+    size_t page_sizes_prop_size;
+    char *nodename;
+
+    if ((index % smt_threads) != 0) {
+        return;
+    }
+
+    nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
+
+    _FDT((fdt_begin_node(fdt, nodename)));
+
+    g_free(nodename);
+
+    _FDT((fdt_property_cell(fdt, "reg", index)));
+    _FDT((fdt_property_string(fdt, "device_type", "cpu")));
+
+    _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
+    _FDT((fdt_property_cell(fdt, "d-cache-block-size",
+                            env->dcache_line_size)));
+    _FDT((fdt_property_cell(fdt, "d-cache-line-size",

hw/ppc/pnv.c line 235

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+                            env->dcache_line_size)));
+    _FDT((fdt_property_cell(fdt, "i-cache-block-size",
+                            env->icache_line_size)));
+    _FDT((fdt_property_cell(fdt, "i-cache-line-size",
+                            env->icache_line_size)));
+
+    if (pcc->l1_dcache_size) {
+        _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size)));
+    } else {
+        fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");

Hmm (note to self) should probably change a bunch of these both in spapr.c and pnv.c from explicit fprintfs() to modern error_report() and similar.

Quoted 60 lines
hw/ppc/pnv.c new file +60 -0
+    }
+    if (pcc->l1_icache_size) {
+        _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size)));
+    } else {
+        fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
+    }
+
+    _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
+    _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
+    _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
+    _FDT((fdt_property_string(fdt, "status", "okay")));
+    _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
+
+    if (env->spr_cb[SPR_PURR].oea_read) {
+        _FDT((fdt_property(fdt, "ibm,purr", NULL, 0)));
+    }
+
+    if (env->mmu_model & POWERPC_MMU_1TSEG) {
+        _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
+                           segs, sizeof(segs))));
+    }
+
+    /* Advertise VMX/VSX (vector extensions) if available
+     *   0 / no property == no vector extensions
+     *   1               == VMX / Altivec available
+     *   2               == VSX available */
+    if (env->insns_flags & PPC_ALTIVEC) {
+        uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
+
+        _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
+    }
+
+    /* Advertise DFP (Decimal Floating Point) if available
+     *   0 / no property == no DFP
+     *   1               == DFP available */
+    if (env->insns_flags2 & PPC2_DFP) {
+        _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
+    }
+
+    page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
+                                                  sizeof(page_sizes_prop));
+    if (page_sizes_prop_size) {
+        _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
+                           page_sizes_prop, page_sizes_prop_size)));
+    }
+
+    /* XXX Just a hack for now */
+    _FDT((fdt_property_cell(fdt, "ibm,chip-id", 0)));
+
+    if (cpu->cpu_version) {
+        _FDT((fdt_property_cell(fdt, "cpu-version", cpu->cpu_version)));
+    }
+
+    /* Build interrupt servers and gservers properties */
+    for (i = 0; i < smt_threads; i++) {
+        servers_prop[i] = cpu_to_be32(index + i);
+        /* Hack, direct the group queues back to cpu 0 */
+        gservers_prop[i*2] = cpu_to_be32(index + i);
+        gservers_prop[i*2 + 1] = 0;
+    }

hw/ppc/pnv.c line 305

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",
+                       servers_prop, sizeof(servers_prop))));
+    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
+                       gservers_prop, sizeof(gservers_prop))));
+
+    _FDT((fdt_end_node(fdt)));
+}
+
+static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t initrd_size)
+{

So.. does it make sense for qemu to create a device tree for powernv, rather than leaving it to Opal?

Quoted 19 lines
hw/ppc/pnv.c new file +19 -0
+    void *fdt;
+    CPUState *cs;
+    int smt = kvmppc_smt_threads();
+    uint32_t start_prop = cpu_to_be32(initrd_base);
+    uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
+    char *buf;
+    const char plat_compat[] = "qemu,powernv\0ibm,powernv";
+
+    fdt = g_malloc0(FDT_MAX_SIZE);
+    _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
+    _FDT((fdt_finish_reservemap(fdt)));
+
+    /* Root node */
+    _FDT((fdt_begin_node(fdt, "")));
+    _FDT((fdt_property_string(fdt, "model", "IBM PowerNV (emulated by qemu)")));
+    _FDT((fdt_property(fdt, "compatible", plat_compat, sizeof(plat_compat))));
+
+    /*
+     * Add info to guest to indentify which host is it being run on

hw/ppc/pnv.c line 334

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+     * and what is the uuid of the guest
+     */
+    if (kvmppc_get_host_model(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-model", buf)));
+        g_free(buf);
+    }
+    if (kvmppc_get_host_serial(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-serial", buf)));
+        g_free(buf);
+    }

Since you're emulating a "bare metal" machine, surely the host properties aren't relevant here.

Quoted 44 lines
hw/ppc/pnv.c new file +44 -0
+
+    buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1],
+                          qemu_uuid[2], qemu_uuid[3], qemu_uuid[4],
+                          qemu_uuid[5], qemu_uuid[6], qemu_uuid[7],
+                          qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
+                          qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
+                          qemu_uuid[14], qemu_uuid[15]);
+
+    _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
+    g_free(buf);
+
+    _FDT((fdt_begin_node(fdt, "chosen")));
+    _FDT((fdt_property(fdt, "linux,initrd-start",
+                       &start_prop, sizeof(start_prop))));
+    _FDT((fdt_property(fdt, "linux,initrd-end",
+                       &end_prop, sizeof(end_prop))));
+    _FDT((fdt_end_node(fdt)));
+
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
+
+    /* cpus */
+    _FDT((fdt_begin_node(fdt, "cpus")));
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
+
+    CPU_FOREACH(cs) {
+        powernv_create_cpu_node(fdt, cs, smt);
+    }
+
+    _FDT((fdt_end_node(fdt)));
+
+    /* Memory */
+    _FDT((powernv_populate_memory(fdt)));
+
+    /* /hypervisor node */
+    if (kvm_enabled()) {
+        uint8_t hypercall[16];
+
+        /* indicate KVM hypercall interface */
+        _FDT((fdt_begin_node(fdt, "hypervisor")));
+        _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
+        if (kvmppc_has_cap_fixup_hcalls()) {
+            /*

hw/ppc/pnv.c line 388

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+             * Older KVM versions with older guest kernels were broken with the
+             * magic page, don't allow the guest to map it.
+             */
+            kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
+                                 sizeof(hypercall));
+            _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
+                              sizeof(hypercall))));
+        }
+        _FDT((fdt_end_node(fdt)));
+    }

And a hypercall interface surely doesn't make sense for powernv.

Quoted 11 lines
hw/ppc/pnv.c new file +11 -0
+
+    _FDT((fdt_end_node(fdt))); /* close root node */
+    _FDT((fdt_finish(fdt)));
+
+    return fdt;
+}
+
+static void powernv_cpu_reset(void *opaque)
+{
+    PowerPCCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);

hw/ppc/pnv.c line 409

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+    CPUPPCState *env = &cpu->env;
+
+    cpu_reset(cs);
+
+    env->spr[SPR_PIR] = ppc_get_vcpu_dt_id(cpu);
+    env->spr[SPR_HIOR] = 0;
+    env->gpr[3] = FDT_ADDR;
+    env->nip = 0x10;
+    env->msr |= MSR_HVB;
+}

So, I believe the qemu-ishly correct way of doing this is to have the cpu initialization in the cpu code, rather than the platform code, as much as possible. On PAPR we kind of get away with initialization in the platform code on the grounds that it's a paravirt platform, but powernv doesn't have that excuse.

But this may well be stuff that changes in later patches, so..

hw/ppc/pnv.c line 415

Quoted 5 lines
hw/ppc/pnv.c new file +5 -0
+static const VMStateDescription vmstate_powernv = {
+    .name = "powernv",
+    .version_id = 1,
+    .minimum_version_id = 1,
+};

It might be best to leave out the vmstate entirely until you're ready to implement migration, rather than having a partial, probably not working migration implementation.

hw/ppc/pnv.c line 426

Quoted 11 lines
hw/ppc/pnv.c new file +11 -0
+
+static void pnv_create_chip(PnvSystem *sys, unsigned int chip_no)
+{
+    PnvChip *chip = &sys->chips[chip_no];
+
+    if (chip_no >= PNV_MAX_CHIPS) {
+            return;
+    }
+
+    /* XXX Improve chip numbering to better match HW */
+    chip->chip_id = chip_no;

I think modern qemu conventions would suggest creating the chips as QOM objects rather than having a fixed array.

Quoted 74 lines
hw/ppc/pnv.c new file +74 -0
+}
+
+static void ppc_powernv_init(MachineState *machine)
+{
+    ram_addr_t ram_size = machine->ram_size;
+    const char *cpu_model = machine->cpu_model;
+    const char *kernel_filename = machine->kernel_filename;
+    const char *initrd_filename = machine->initrd_filename;
+    uint32_t initrd_base = 0;
+    long initrd_size = 0;
+    PowerPCCPU *cpu;
+    CPUPPCState *env;
+    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *ram = g_new(MemoryRegion, 1);
+    sPowerNVMachineState *pnv_machine = POWERNV_MACHINE(machine);
+    PnvSystem *sys = &pnv_machine->sys;
+    long fw_size;
+    char *filename;
+    void *fdt;
+    int i;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = kvm_enabled() ? "host" : "POWER8";
+    }
+
+    for (i = 0; i < smp_cpus; i++) {
+        cpu = cpu_ppc_init(cpu_model);
+        if (cpu == NULL) {
+            fprintf(stderr, "Unable to find PowerPC CPU definition\n");
+            exit(1);
+        }
+        env = &cpu->env;
+
+        /* Set time-base frequency to 512 MHz */
+        cpu_ppc_tb_init(env, TIMEBASE_FREQ);
+
+        /* MSR[IP] doesn't exist nowadays */
+        env->msr_mask &= ~(1 << 6);
+
+        qemu_register_reset(powernv_cpu_reset, cpu);
+    }
+
+    /* allocate RAM */
+    memory_region_allocate_system_memory(ram, NULL, "ppc_powernv.ram", ram_size);
+    memory_region_add_subregion(sysmem, 0, ram);
+
+    /* XXX We should decide how many chips to create based on #cores and
+     * Venice vs. Murano vs. Naples chip type etc..., for now, just create
+     * one chip. Also creation of the CPUs should be done per-chip
+     */
+    sys->num_chips = 1;
+
+    /* Create only one PHB for now until I figure out what's wrong
+     * when I create more (resource assignment failures in Linux)
+     */
+    pnv_create_chip(sys, 0);
+
+    if (bios_name == NULL) {
+        bios_name = FW_FILE_NAME;
+    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
+    if (fw_size < 0) {
+        hw_error("qemu: could not load OPAL '%s'\n", filename);
+        exit(1);
+    }
+    g_free(filename);
+
+
+    if (kernel_filename == NULL) {
+        kernel_filename = KERNEL_FILE_NAME;
+    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
Quoted 1 line

kernel_filename);

The commit withe Opal image should go in before this, no?

Quoted 142 lines
hw/ppc/pnv.c new file +100 -0
+    fw_size = load_image_targphys(filename, 0x20000000, 0x2000000);
+    if (fw_size < 0) {
+        hw_error("qemu: could not load kernel'%s'\n", filename);
+        exit(1);
+    }
+    g_free(filename);
+
+    /* load initrd */
+    if (initrd_filename) {
+            /* Try to locate the initrd in the gap between the kernel
+             * and the firmware. Add a bit of space just in case
+             */
+            initrd_base = 0x40000000;
+            initrd_size = load_image_targphys(initrd_filename, initrd_base,
+                                              0x10000000); // 128MB max
+            if (initrd_size < 0) {
+                    fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
+                            initrd_filename);
+                    exit(1);
+            }
+    } else {
+            initrd_base = 0;
+            initrd_size = 0;
+    }
+    fdt = powernv_create_fdt(sys, initrd_base, initrd_size);
+    cpu_physical_memory_write(FDT_ADDR, fdt, fdt_totalsize(fdt));
+}
+
+static int powernv_kvm_type(const char *vm_type)
+{
+    /* Always force PR KVM */
+    return 2;
+}
+
+static void ppc_cpu_do_nmi_on_cpu(void *arg)
+{
+    CPUState *cs = arg;
+
+    cpu_synchronize_state(cs);
+    ppc_cpu_do_system_reset(cs);
+}
+
+static void powernv_nmi(NMIState *n, int cpu_index, Error **errp)
+{
+    CPUState *cs;
+
+    CPU_FOREACH(cs) {
+        async_run_on_cpu(cs, ppc_cpu_do_nmi_on_cpu, cs);
+    }
+}
+
+static void powernv_machine_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+    NMIClass *nc = NMI_CLASS(oc);
+
+    mc->init = ppc_powernv_init;
+    mc->block_default_type = IF_SCSI;
+    mc->max_cpus = MAX_CPUS;
+    mc->no_parallel = 1;
+    mc->default_boot_order = NULL;
+    mc->kvm_type = powernv_kvm_type;
+
+    nc->nmi_monitor_handler = powernv_nmi;
+}
+
+static const TypeInfo powernv_machine_info = {
+    .name          = TYPE_POWERNV_MACHINE,
+    .parent        = TYPE_MACHINE,
+    .abstract      = true,
+    .instance_size = sizeof(sPowerNVMachineState),
+    .class_init    = powernv_machine_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { TYPE_NMI },
+        { }
+    },
+};
+
+static void powernv_machine_2_5_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+
+    mc->name = "powernv-2.5";
+    mc->desc = "PowerNV v2.5";
+    mc->alias = "powernv";
+}
+
+static const TypeInfo powernv_machine_2_5_info = {
+    .name          = MACHINE_TYPE_NAME("powernv-2.5"),
+    .parent        = TYPE_POWERNV_MACHINE,
+    .class_init    = powernv_machine_2_5_class_init,
+};
+
+static void powernv_machine_register_types(void)
+{
+    type_register_static(&powernv_machine_info);
+    type_register_static(&powernv_machine_2_5_info);
+}
+
+type_init(powernv_machine_register_types)
include/hw/ppc/pnv.h new file +36 -0
new file mode 100644
index 0000000..9a48c16
@@ -0,0 +1,36 @@
+#ifndef _HW_LPC_H
+#define _HW_LPC_H
+/*
+ * QEMU PowerNV various definitions
+ *
+ * Copyright (c) 2014 BenH
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/hw.h"
+
+/* Should we turn that into a QOjb of some sort ? */
+typedef struct PnvChip {
+    uint32_t         chip_id;
+} PnvChip;
+
+typedef struct PnvSystem {
+    uint32_t  num_chips;
+#define PNV_MAX_CHIPS		1
+    PnvChip   chips[PNV_MAX_CHIPS];
+} PnvSystem;
+
+#endif /* _HW_PNV_LPC_H */
+
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
#60 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 21/77] ppc: Rework generation of priv and inval interrupts

In reply to David Gibson
Quoted 7 lines · David Gibson


So, I'm not 100% following the logic below, but it looks like the existing code used SPR_NOACCESS to mark things which generated a privilege exception compared to NULL for things which generated an invalid instruction exception.  Using that encoding, can you simplify the logic here?  Alternatively can you use the logic here to avoid the SPR_NOACESS encoding?

Well, so the SPR_NOACCESS has to do with how you react to a known SPR who has explicit access permissions. The logic below is described in the ISA for an unknown SPR number.

I don't know whether the access permission of "known" SPRs always honor the 0x10 bit trick, and changing that in qemu would be a fairly large patch. So I'd rather stick to the logic here for "unknown" SPRs which matches the ISA definition.

I'll update the patch though for arch 2.07 as it defines a few reserved SPRs as no-ops.

However:

Quoted 12 lines
Quoted 12 lines

-        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR); +
+        /* The behaviour depends on MSR:PR and SPR# bit 0x10, +         * it can generate a priv, a hv emu or a no-op +         */
+        if (sprn & 0x10) {
+            if (ctx->pr) {
+                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +            }
+        } else {
+            if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {

Quoted 8 lines
Quoted 8 lines

+                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +            }
+        }
+#if !defined(CONFIG_USER_ONLY)
+        /* HV priv */
+        if (ctx->spr_cb[sprn].hea_read) {
+            gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +        }

That latest bit is bogus.

Quoted 2 lines

If you're in PR mode, and it's an SPR with an hea_read function and has the 0x10 bit set, won't this call gen_priv_exception twice?

Yes, I've removed it. It should be handled by the SPR_NOACCESS.

Quoted 5 lines

I also see no path here which will call gen_inval_exception(), is that
right?  If you're in HV mode and it's a truly invalid SPRN, isn't that
what you'd want?

No, the ISA says it's a nop.

Cheers,
Ben.

Quoted 212 lines
Quoted 78 lines

+#endif
     }
 }

target-ppc/translate.c +3 -7
@@ -4395,13 +4423,9 @@ static void gen_mtcrf(DisasContext *ctx)
  #if defined(TARGET_PPC64)
  static void gen_mtmsrd(DisasContext *ctx)
  {
-#if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-        return;
-    }
+    CHK_SV;
+
+#if !defined(CONFIG_USER_ONLY)
      if (ctx->opcode & 0x00010000) {
          /* Special form that does not need any synchronisation */
          TCGv t0 = tcg_temp_new();
@@ -4420,20 +4444,16 @@ static void gen_mtmsrd(DisasContext *ctx)
          /* Note that mtmsr is not always defined as context-

synchronizing */
         gen_stop_exception(ctx);
     }

target-ppc/translate.c +1 -1
-#endif
+#endif /* !defined(CONFIG_USER_ONLY) */

 }

(quoted diff) +1 -1
-#endif
+#endif /* defined(TARGET_PPC64) */


 static void gen_mtmsr(DisasContext *ctx)
 {

target-ppc/translate.c +4 -8
-#if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
-        return;
-    }
-    if (ctx->opcode & 0x00010000) {
+    CHK_SV;
+
+#if !defined(CONFIG_USER_ONLY)
+   if (ctx->opcode & 0x00010000) {

         /* Special form that does not need any synchronisation */ TCGv t0 = tcg_temp_new();
         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));

(quoted diff) +0 -0
@@ -4488,7 +4508,7 @@ static void gen_mtspr(DisasContext *ctx)

                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
             printf("Trying to write privileged spr %d (0x%03x) at "
                    TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4); -            gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG); +            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG); }
     } else {
         /* Not defined */

(quoted diff) +0 -0
@@ -4496,7 +4516,25 @@ static void gen_mtspr(DisasContext *ctx)
                   TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);

         printf("Trying to write invalid spr %d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4); -        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);

target-ppc/translate.c +9 -0
+
+        /* The behaviour depends on MSR:PR and SPR# bit 0x10,
+         * it can generate a priv, a hv emu or a no-op
+         */
+        if (sprn & 0x10) {
+            if (ctx->pr) {
+                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }
+        } else {

target-ppc/translate.c static void gen_mtspr(DisasContext *ctx) · line 4537

Quoted 10 lines
target-ppc/translate.c +10 -0
+            if (ctx->pr || sprn == 0) {
+                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+            }
+        }
+#if !defined(CONFIG_USER_ONLY)
+        /* HV priv */
+        if (ctx->spr_cb[sprn].hea_write) {
+            gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
+        }
+#endif

Same concerns here as for mfspr.

[snip]

Quoted 4 lines

 /* tlbiel */
 static void gen_tlbiel(DisasContext *ctx)
 {
 #if defined(CONFIG_USER_ONLY)

Quoted 8 lines

-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
 #else
-    if (unlikely(ctx->pr || !ctx->hv)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }
+    CHK_SV;

You have CHK_SV here, but the original code checks for HV, as does your new code for tlbia and tlbiel, is that right?

[snip]

Quoted 5 lines

 /* tlbsync */
 static void gen_tlbsync(DisasContext *ctx)
 {
 #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);

target-ppc/translate.c static void gen_tlbie(DisasContext *ctx) · line 4912

Quoted 9 lines
target-ppc/translate.c +4 -5
-#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+    GEN_PRIV;
+#else    
+    CHK_HV;
+

Old code didn't check for HV, mode, but AFAICT it should have, so this
looks correct.

[snip]

Quoted 4 lines
target-ppc/translate.c +0 -0
@@ -5941,18 +5921,16 @@ static void gen_mfapidi(DisasContext *ctx)
  static void gen_tlbiva(DisasContext *ctx)
  {
  #if defined(CONFIG_USER_ONLY)

target-ppc/translate.c static void gen_mfapidi(DisasContext *ctx) · line 5928

Quoted 10 lines
target-ppc/translate.c +3 -5
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
  #else
      TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+
+    CHK_SV;

Is the same thing as tlbivax, or some ancient instruction?  AFAICT the
ISA says tlbivax is hypervisor privileged.

Quoted 7 lines
target-ppc/translate.c +1 -1
      t0 = tcg_temp_new();
      gen_addr_reg_index(ctx, t0);
      gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
      tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
  }

[snip]

Quoted 5 lines

 static void gen_tlbivax_booke206(DisasContext *ctx) {
 #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;

Quoted 8 lines

 #else
     TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

ISA says tlbivax is hypervisor privileged when the CPU has a hypervisor mode, which I guess booke206 probably doesn't?

Quoted 14 lines

     t0 = tcg_temp_new();
     gen_addr_reg_index(ctx, t0);
-
     gen_helper_booke206_tlbivax(cpu_env, t0);
     tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

 }

 static void gen_tlbilx_booke206(DisasContext *ctx) {
 #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;

Quoted 8 lines

 #else
     TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

And apparently hv vs. sv privilege of tlbilx depends on the EPCR register.  Again, may not be relevant for 2.06.

Quoted 11 lines

     t0 = tcg_temp_new();
     gen_addr_reg_index(ctx, t0);

(quoted diff) +0 -0
@@ -6672,7 +6574,7 @@ static void gen_tlbilx_booke206(DisasContext
 *ctx)
      }


     tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

 }

#62 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 21/77] ppc: Rework generation of priv and inval interrupts

In reply to David Gibson
Quoted 14 lines · David Gibson

snip]

Quoted 4 lines

  /* tlbiel */
  static void gen_tlbiel(DisasContext *ctx)
  {
  #if defined(CONFIG_USER_ONLY)

Quoted 8 lines

-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
  #else
-    if (unlikely(ctx->pr || !ctx->hv)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }
+    CHK_SV;

Quoted 2 lines

You have CHK_SV here, but the original code checks for HV, as does your new code for tlbia and tlbiel, is that right?

Yes. tlbiel is supervisor accessible (for weird reasons).

Quoted 10 lines

[snip]

Quoted 9 lines

  /* tlbsync */
  static void gen_tlbsync(DisasContext *ctx)
  {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;

Quoted 8 lines

target-ppc/translate.c static void gen_tlbie(DisasContext *ctx) · line 4912

Quoted 5 lines

-    }
+    GEN_PRIV;

target-ppc/translate.c +3 -0
+#else    
+    CHK_HV;
+

Old code didn't check for HV, mode, but AFAICT it should have, so this looks correct.

Yes, this is a hypervisor instruction.

Quoted 10 lines

[snip]

Quoted 9 lines
target-ppc/translate.c +1 -2
@@ -5941,18 +5921,16 @@ static void gen_mfapidi(DisasContext *ctx)
   static void gen_tlbiva(DisasContext *ctx)
   {
   #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
   #else
       TCGv t0;
-    if (unlikely(ctx->pr)) {
Quoted 8 lines

target-ppc/translate.c static void gen_dcbf(DisasContext *ctx) · line 4563

Quoted 5 lines
target-ppc/translate.c +2 -3
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+
+    CHK_SV;

Is the same thing as tlbivax, or some ancient instruction?  AFAICT the ISA says tlbivax is hypervisor privileged.

"tlbiva" is the 4xx variant, there is no hypervisor mode on these things.

Quoted 23 lines
Quoted 7 lines
target-ppc/translate.c +1 -1
       t0 = tcg_temp_new();
       gen_addr_reg_index(ctx, t0);
       gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
       tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
   }

[snip]

Quoted 5 lines

  static void gen_tlbivax_booke206(DisasContext *ctx) {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;

Quoted 8 lines

  #else
      TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

Quoted 2 lines

ISA says tlbivax is hypervisor privileged when the CPU has a hypervisor mode, which I guess booke206 probably doesn't?

Right so here, the "problem" is that afaik, TCG doesn't implement the BookE hypervisor mode. So with my limited BookE testing ability I prefer sticking to a mechanical replacement that matches the existing code. It can be fixed later if necessary.

Quoted 17 lines
Quoted 17 lines

      t0 = tcg_temp_new();
      gen_addr_reg_index(ctx, t0);
-
      gen_helper_booke206_tlbivax(cpu_env, t0);
      tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

  }

  static void gen_tlbilx_booke206(DisasContext *ctx) {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
  #else
      TCGv t0;
-    if (unlikely(ctx->pr)) {

Quoted 8 lines
Quoted 5 lines

-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

And apparently hv vs. sv privilege of tlbilx depends on the EPCR register.  Again, may not be relevant for 2.06.

Well, here too, I basically preserve existing BookE TCG behaviour, whether it's correct or not. That can be fixed separately if somebody cares about BookE HV mode.

Quoted 12 lines

Review static void gen_tlbilx_booke206(DisasContext

Quoted 4 lines

      t0 = tcg_temp_new();
      gen_addr_reg_index(ctx, t0);

(quoted diff) +0 -0
@@ -6672,7 +6574,7 @@ static void gen_tlbilx_booke206(DisasContext

*ctx)

Quoted 6 lines
(quoted diff) +0 -0
       }


      tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

  }

#66 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 22/77] ppc: Add real mode CI load/store instructions for P7 and P8

In reply to David Gibson
Quoted 8 lines · David Gibson

target-ppc/translate.c typedef struct opcode_t { · line 383

Quoted 6 lines
target-ppc/translate.c +1 -0
@@ -380,9 +380,11 @@ typedef struct opcode_t {
  #if defined(CONFIG_USER_ONLY)
  #define CHK_HV GEN_PRIV
  #define CHK_SV GEN_PRIV
+#define CHK_HVDR GEN_PRIV

I'm guessing this is supposed to be CHK_HVRM as below.

Indeed. I had test built ... odd.

Quoted 7 lines
Quoted 7 lines
target-ppc/translate.c +1 -0
  #else
  #define CHK_HV do { if (unlikely(ctx->pr || !ctx->hv)) GEN_PRIV; } while(0)
  #define CHK_SV do { if (unlikely(ctx->pr))  GEN_PRIV; }  while(0)
+#define CHK_HVRM do { if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) GEN_PRIV; } while(0)
  #endif


 #define CHK_NONE

Quoted 8 lines

target-ppc/translate.c static void glue(gen_, name##u)(DisasContext *ctx) · line 2892

Quoted 6 lines
(quoted diff) +0 -0
@@ -2887,7 +2889,7 @@ static void glue(gen_, name##u)(DisasContext *ctx)
  }


 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \

target-ppc/translate.c +1 -1
-static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
+static void glue(gen_, name##ux)(DisasContext *ctx)                           \

Extraneous change.

Fixed. Thanks.

Cheers,
Ben.

#71 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 24/77] ppc: Move exception generation code out of line

In reply to David Gibson
Quoted 7 lines · David Gibson
Quoted 2 lines · Benjamin Herrenschmidt

There's no point inlining this, if you hit the exception case you exit anyway,

That doesn't quite seem relevant - IIUC this is affecting inlining in the code generation path, rather than the code execution path.

Well, the generation path is also slightly performance sensitive. My point was making the error path inline wasn't buying us anything.

Quoted 4 lines
Quoted 2 lines

and not inlining saves about 100K of code size (and cache footprint).

That sounds like a win, though.

Yup.

  .../...

Quoted 9 lines
Quoted 5 lines

-static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)

(quoted diff) +2 -0
+static void __attribute__((noinline))
+gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t

error)

I thought we generally avoided bare gcc attributes in qemu, but I don't see a helper macro for it and I do see it used in a few other places, so I guess its ok.

That was my guess too...

Quoted 26 lines
Quoted 25 lines

 {
     TCGv_i32 t0, t1;
     if (ctx->exception == POWERPC_EXCP_NONE) {

(quoted diff) +0 -0
@@ -293,7 +294,8 @@ static inline void

gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t ctx->exception = (excp);
 }

target-ppc/translate.c +2 -1
-static inline void gen_exception(DisasContext *ctx, uint32_t excp)
+static void __attribute__((noinline))
+gen_exception(DisasContext *ctx, uint32_t excp)

 {
     TCGv_i32 t0;
     if (ctx->exception == POWERPC_EXCP_NONE) {

target-ppc/translate.c +0 -0
@@ -305,7 +307,8 @@ static inline void gen_exception(DisasContext
 *ctx, uint32_t excp)
      ctx->exception = (excp);
  }

target-ppc/translate.c +2 -1
-static inline void gen_debug_exception(DisasContext *ctx)
+static void __attribute__((noinline))
+gen_debug_exception(DisasContext *ctx)

 {
     TCGv_i32 t0;

#77 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 26/77] ppc/pnv: Add skeletton PowerNV platform

In reply to David Gibson
Quoted 16 lines · David Gibson
Quoted 10 lines · Benjamin Herrenschmidt

No devices yet, not even an interrupt controller, just to get started.

---
 default-configs/ppc64-softmmu.mak |   1 +
 hw/ppc/Makefile.objs              |   2 +
 hw/ppc/pnv.c                      | 600
++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/pnv.h              |  36 +++

Quoted 3 lines
 4 files changed, 639 insertions(+)

 create mode 100644 hw/ppc/pnv.c
 create mode 100644 include/hw/ppc/pnv.h

Quoted 2 lines

Many of my comments below may be made irrelevant by later patches in the series.

Heh, well there is where the "meat" of the new platform starts showing up :-)

 .../...

Quoted 10 lines
Quoted 9 lines

+#define _FDT(exp) \
+    do { \
+        int ret = (exp);                                           \
+        if (ret < 0) {                                             \
+            fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
+                    #exp, fdt_strerror(ret));                      \
+            exit(1);                                               \
+        }                                                          \
+    } while (0)

Quoted 2 lines

We should probably make a file where helper routines used by both spapr.c and pnv.c can live.

Probably but I'd see that as a later cleanup rather than doing it in this series...

.../...

Quoted 8 lines
Quoted 4 lines

+    if (pcc->l1_dcache_size) {
+        _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size))); +    } else {
+        fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");

Hmm (note to self) should probably change a bunch of these both in spapr.c and pnv.c from explicit fprintfs() to modern error_report() and similar.

That's a train I completely missed, but yes.

  .../...

Quoted 6 lines
Quoted 6 lines

+    }
+    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s", +                       servers_prop, sizeof(servers_prop)))); +    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s", +                       gservers_prop, sizeof(gservers_prop)))); +

Quoted 8 lines

hw/ppc/pnv.c line 305

Quoted 5 lines

+    _FDT((fdt_end_node(fdt)));

hw/ppc/pnv.c new file +4 -0
+}
+
+static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t initrd_size)
+{

So.. does it make sense for qemu to create a device tree for powernv, rather than leaving it to Opal?

Well, OPAL only creates a device-tree if you are on an FSP machine in which case it expects a complex data structure (HDAT) coming from the FSP to use as a source of info.

On OpenPower machines, which is closer to what we simulate here, we do get a device-tree as an input in OPAL, it's generated by HostBoot.

Now, I am not running HostBoot in qemu because most of what it does is completely irrelevant to an emulated system (training the various links, initializing the memory buffer chips, etc...).

However we do need to pass a number of platform information to OPAL which HB does via the device-tree, such as which cores are enabled, the memory map configured for PCI, which PHBs are enabled, etc...  so creating a DT in qemu makes sense, it mimmics HB in essence.

OPAL will enrich that device-tree before starting Linux.

Quoted 13 lines
Quoted 4 lines

+    /*
+     * Add info to guest to indentify which host is it being run on +     * and what is the uuid of the guest
+     */

Quoted 8 lines

+    if (kvmppc_get_host_model(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-model", buf))); +        g_free(buf);
+    }
+    if (kvmppc_get_host_serial(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-serial", buf))); +        g_free(buf);
+    }

Quoted 2 lines

Since you're emulating a "bare metal" machine, surely the host properties aren't relevant here.

They may or may not. But yes, I can take that out.

Quoted 47 lines
Quoted 47 lines

+    buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1], +                          qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], +                          qemu_uuid[5], qemu_uuid[6], qemu_uuid[7], +                          qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], +                          qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], +                          qemu_uuid[14], qemu_uuid[15]);

hw/ppc/pnv.c new file +41 -0
+
+    _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
+    g_free(buf);
+
+    _FDT((fdt_begin_node(fdt, "chosen")));
+    _FDT((fdt_property(fdt, "linux,initrd-start",
+                       &start_prop, sizeof(start_prop))));
+    _FDT((fdt_property(fdt, "linux,initrd-end",
+                       &end_prop, sizeof(end_prop))));
+    _FDT((fdt_end_node(fdt)));
+
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
+
+    /* cpus */
+    _FDT((fdt_begin_node(fdt, "cpus")));
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
+
+    CPU_FOREACH(cs) {
+        powernv_create_cpu_node(fdt, cs, smt);
+    }
+
+    _FDT((fdt_end_node(fdt)));
+
+    /* Memory */
+    _FDT((powernv_populate_memory(fdt)));
+
+    /* /hypervisor node */
+    if (kvm_enabled()) {
+        uint8_t hypercall[16];
+
+        /* indicate KVM hypercall interface */
+        _FDT((fdt_begin_node(fdt, "hypervisor")));
+        _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
+        if (kvmppc_has_cap_fixup_hcalls()) {
+            /*
+             * Older KVM versions with older guest kernels were broken with the
+             * magic page, don't allow the guest to map it.
+             */
+            kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
Quoted 8 lines
Quoted 6 lines

+                                 sizeof(hypercall));
+            _FDT((fdt_property(fdt, "hcall-instructions", hypercall, +                              sizeof(hypercall))));
+        }
+        _FDT((fdt_end_node(fdt)));
+    }

And a hypercall interface surely doesn't make sense for powernv.

It's qemu paravirt, it exist on G5 too :-) It's for PR KVM, it allows to speed up some bits and pieces. But yeah we don't yet really "support" it at this point. However we might.

Quoted 28 lines
Quoted 11 lines
hw/ppc/pnv.c new file +11 -0
+
+    _FDT((fdt_end_node(fdt))); /* close root node */
+    _FDT((fdt_finish(fdt)));
+
+    return fdt;
+}
+
+static void powernv_cpu_reset(void *opaque)
+{
+    PowerPCCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);

hw/ppc/pnv.c line 409

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+    CPUPPCState *env = &cpu->env;
+
+    cpu_reset(cs);
+
+    env->spr[SPR_PIR] = ppc_get_vcpu_dt_id(cpu);
+    env->spr[SPR_HIOR] = 0;
+    env->gpr[3] = FDT_ADDR;
+    env->nip = 0x10;
+    env->msr |= MSR_HVB;
+}

So, I believe the qemu-ishly correct way of doing this is to have the cpu initialization in the cpu code, rather than the platform code, as much as possible.  On PAPR we kind of get away with initialization in the platform code on the grounds that it's a paravirt platform, but powernv doesn't have that excuse.

Quoted 1 line

But this may well be stuff that changes in later patches, so..

Well no, not really. But here too, we mimmic the state as coming out of HostBoot, which isn't quite the same thing. We need to provide the FDT entry, etc...

The "real" reset state of a P8 isn't something we can easily simulate...

It runs some microcode from a SEEPROM with a small microcontroller which initializes a core, which then runs some HB code off it's L3 cache etc... really not something we want to do in qemu at least for now.

So the initial state here is somewhat in between full virt and paravirt, we simulate a platform that has been partially initialized by HostBoot, to the state it has when it enters OPAL.

Quoted 9 lines

hw/ppc/pnv.c line 415

Quoted 5 lines
hw/ppc/pnv.c new file +5 -0
+static const VMStateDescription vmstate_powernv = {
+    .name = "powernv",
+    .version_id = 1,
+    .minimum_version_id = 1,
+};

It might be best to leave out the vmstate entirely until you're ready to implement migration, rather than having a partial, probably not working migration implementation.

Ok.

Quoted 6 lines
Quoted 6 lines
hw/ppc/pnv.c new file +6 -0
+
+static void pnv_create_chip(PnvSystem *sys, unsigned int chip_no)
+{
+    PnvChip *chip = &sys->chips[chip_no];
+
+    if (chip_no >= PNV_MAX_CHIPS) {
Quoted 8 lines
Quoted 5 lines

+            return;
+    }
+
+    /* XXX Improve chip numbering to better match HW */ +    chip->chip_id = chip_no;

I think modern qemu conventions would suggest creating the chips as QOM objects rather than having a fixed array.

Yeah, more code & much larger memory footprint for the same result :-)

I can look into it but it's low priority. I still want to rework some of that chip stuff in future patches anyway.

Quoted 69 lines
Quoted 69 lines
hw/ppc/pnv.c new file +69 -0
+}
+
+static void ppc_powernv_init(MachineState *machine)
+{
+    ram_addr_t ram_size = machine->ram_size;
+    const char *cpu_model = machine->cpu_model;
+    const char *kernel_filename = machine->kernel_filename;
+    const char *initrd_filename = machine->initrd_filename;
+    uint32_t initrd_base = 0;
+    long initrd_size = 0;
+    PowerPCCPU *cpu;
+    CPUPPCState *env;
+    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *ram = g_new(MemoryRegion, 1);
+    sPowerNVMachineState *pnv_machine = POWERNV_MACHINE(machine);
+    PnvSystem *sys = &pnv_machine->sys;
+    long fw_size;
+    char *filename;
+    void *fdt;
+    int i;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = kvm_enabled() ? "host" : "POWER8";
+    }
+
+    for (i = 0; i < smp_cpus; i++) {
+        cpu = cpu_ppc_init(cpu_model);
+        if (cpu == NULL) {
+            fprintf(stderr, "Unable to find PowerPC CPU definition\n");
+            exit(1);
+        }
+        env = &cpu->env;
+
+        /* Set time-base frequency to 512 MHz */
+        cpu_ppc_tb_init(env, TIMEBASE_FREQ);
+
+        /* MSR[IP] doesn't exist nowadays */
+        env->msr_mask &= ~(1 << 6);
+
+        qemu_register_reset(powernv_cpu_reset, cpu);
+    }
+
+    /* allocate RAM */
+    memory_region_allocate_system_memory(ram, NULL, "ppc_powernv.ram", ram_size);
+    memory_region_add_subregion(sysmem, 0, ram);
+
+    /* XXX We should decide how many chips to create based on #cores and
+     * Venice vs. Murano vs. Naples chip type etc..., for now, just create
+     * one chip. Also creation of the CPUs should be done per-chip
+     */
+    sys->num_chips = 1;
+
+    /* Create only one PHB for now until I figure out what's wrong
+     * when I create more (resource assignment failures in Linux)
+     */
+    pnv_create_chip(sys, 0);
+
+    if (bios_name == NULL) {
+        bios_name = FW_FILE_NAME;
+    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
+    if (fw_size < 0) {
+        hw_error("qemu: could not load OPAL '%s'\n", filename);
+        exit(1);
+    }
+    g_free(filename);
+
Quoted 8 lines
Quoted 6 lines

+
+    if (kernel_filename == NULL) {
+        kernel_filename = KERNEL_FILE_NAME; +    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, kernel_filename);

The commit withe Opal image should go in before this, no?

Now this is a bit of an open discussion at the moment :-)

The way OPAL is built on OPP machines today is by essentially building a complete flash image which contains HostBoot, OPAL and the petitboot- based bootloader which contains a Linux kernel etc...

We could create a target without HB and with a slimmed down Linux but it would still probably be about 12MB I reckon, if not more. It feels a bit "big" to ship as a binary as part of qemu...

We would also have to add code to qemu to "find" OPAL in that image, and then add a model for the flash controller.

The other option is to bundle just OPAL itself. However that means you can't go anywhere without a -kernel argument, which would then be either a petitboot-based bootloader or your actual target kernel.

Cheers,
Ben.

#63 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 21/77] ppc: Rework generation of priv and inval interrupts

In reply to Benjamin Herrenschmidt
Quoted 86 lines · Benjamin Herrenschmidt
Quoted 14 lines · David Gibson

snip]

Quoted 4 lines

  /* tlbiel */
  static void gen_tlbiel(DisasContext *ctx)
  {
  #if defined(CONFIG_USER_ONLY)

Quoted 8 lines

-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
  #else
-    if (unlikely(ctx->pr || !ctx->hv)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }
+    CHK_SV;

Quoted 2 lines

You have CHK_SV here, but the original code checks for HV, as does your new code for tlbia and tlbiel, is that right?

Yes. tlbiel is supervisor accessible (for weird reasons).

Quoted 10 lines

[snip]

Quoted 9 lines

  /* tlbsync */
  static void gen_tlbsync(DisasContext *ctx)
  {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -#else
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;

Quoted 8 lines

target-ppc/translate.c static void gen_tlbie(DisasContext *ctx) · line 4912

Quoted 5 lines

-    }
+    GEN_PRIV;

target-ppc/translate.c +3 -0
+#else    
+    CHK_HV;
+

Old code didn't check for HV, mode, but AFAICT it should have, so this looks correct.

Yes, this is a hypervisor instruction.

Quoted 10 lines

[snip]

Quoted 9 lines
target-ppc/translate.c +1 -2
@@ -5941,18 +5921,16 @@ static void gen_mfapidi(DisasContext *ctx)
   static void gen_tlbiva(DisasContext *ctx)
   {
   #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
+    GEN_PRIV;
   #else
       TCGv t0;
-    if (unlikely(ctx->pr)) {
Quoted 8 lines

target-ppc/translate.c static void gen_dcbf(DisasContext *ctx) · line 4563

Quoted 5 lines
target-ppc/translate.c +2 -3
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
-        return;
-    }
+
+    CHK_SV;

Is the same thing as tlbivax, or some ancient instruction?  AFAICT the ISA says tlbivax is hypervisor privileged.

"tlbiva" is the 4xx variant, there is no hypervisor mode on these things.

Quoted 23 lines
Quoted 7 lines
target-ppc/translate.c +1 -1
       t0 = tcg_temp_new();
       gen_addr_reg_index(ctx, t0);
       gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
       tcg_temp_free(t0);
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */
   }

[snip]

Quoted 13 lines

  static void gen_tlbivax_booke206(DisasContext *ctx) {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
  #else
      TCGv t0;
-    if (unlikely(ctx->pr)) {
-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

Quoted 7 lines
Quoted 2 lines

ISA says tlbivax is hypervisor privileged when the CPU has a hypervisor mode, which I guess booke206 probably doesn't?

Right so here, the "problem" is that afaik, TCG doesn't implement the BookE hypervisor mode. So with my limited BookE testing ability I prefer sticking to a mechanical replacement that matches the existing code. It can be fixed later if necessary.

Fair enough.

Quoted 43 lines
Quoted 17 lines
Quoted 17 lines

      t0 = tcg_temp_new();
      gen_addr_reg_index(ctx, t0);
-
      gen_helper_booke206_tlbivax(cpu_env, t0);
      tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

  }

  static void gen_tlbilx_booke206(DisasContext *ctx) {
  #if defined(CONFIG_USER_ONLY)
-    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); +    GEN_PRIV;
  #else
      TCGv t0;
-    if (unlikely(ctx->pr)) {

Quoted 8 lines
Quoted 5 lines

-        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC); -        return;
-    }

+    CHK_SV;

And apparently hv vs. sv privilege of tlbilx depends on the EPCR register.  Again, may not be relevant for 2.06.

Well, here too, I basically preserve existing BookE TCG behaviour, whether it's correct or not. That can be fixed separately if somebody cares about BookE HV mode.

Quoted 12 lines

Review static void gen_tlbilx_booke206(DisasContext

Quoted 4 lines

      t0 = tcg_temp_new();
      gen_addr_reg_index(ctx, t0);

(quoted diff) +0 -0
@@ -6672,7 +6574,7 @@ static void gen_tlbilx_booke206(DisasContext

*ctx)

Quoted 6 lines
(quoted diff) +0 -0
       }


      tcg_temp_free(t0);

(quoted diff) +1 -1
-#endif
+#endif /* defined(CONFIG_USER_ONLY) */

  }

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
#61 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 21/77] ppc: Rework generation of priv and inval interrupts

In reply to Benjamin Herrenschmidt
Quoted 13 lines · Benjamin Herrenschmidt
Quoted 7 lines · David Gibson


So, I'm not 100% following the logic below, but it looks like the existing code used SPR_NOACCESS to mark things which generated a privilege exception compared to NULL for things which generated an invalid instruction exception.  Using that encoding, can you simplify the logic here?  Alternatively can you use the logic here to avoid the SPR_NOACESS encoding?

Well, so the SPR_NOACCESS has to do with how you react to a known SPR who has explicit access permissions. The logic below is described in the ISA for an unknown SPR number.

Quoted 7 lines

I don't know whether the access permission of "known" SPRs always honor the 0x10 bit trick, and changing that in qemu would be a fairly large patch. So I'd rather stick to the logic here for "unknown" SPRs which matches the ISA definition.

I'll update the patch though for arch 2.07 as it defines a few reserved SPRs as no-ops.

Ok, that makes sense.

Quoted 30 lines

However:

Quoted 12 lines
Quoted 12 lines

-        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR); +
+        /* The behaviour depends on MSR:PR and SPR# bit 0x10, +         * it can generate a priv, a hv emu or a no-op +         */
+        if (sprn & 0x10) {
+            if (ctx->pr) {
+                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +            }
+        } else {
+            if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {

Quoted 8 lines
Quoted 8 lines

+                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +            }
+        }
+#if !defined(CONFIG_USER_ONLY)
+        /* HV priv */
+        if (ctx->spr_cb[sprn].hea_read) {
+            gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR); +        }

That latest bit is bogus.

Quoted 2 lines

If you're in PR mode, and it's an SPR with an hea_read function and has the 0x10 bit set, won't this call gen_priv_exception twice?

Yes, I've removed it. It should be handled by the SPR_NOACCESS.

Quoted 7 lines
Quoted 5 lines

I also see no path here which will call gen_inval_exception(), is that
right?  If you're in HV mode and it's a truly invalid SPRN, isn't that
what you'd want?

No, the ISA says it's a nop.

Huh, ok.

Some comments referencing the ISA might be useful here.

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
Reviewed-by on v1 31/77
#89 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 31/77] ppc/xics: Remove unused xics_set_irq_type()

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt

In fact, this is a sufficiently good clean up that I think I'll merge it shortly, regardless of what happens with the rest of the pnv series.

Quoted 39 lines
---
 hw/intc/xics.c        | 11 -----------
 include/hw/ppc/xics.h |  1 -
 2 files changed, 12 deletions(-)
hw/intc/xics.c +0 -11
index 165ff0b..197df33 100644
@@ -678,17 +678,6 @@ void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
         lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
 }
 
-void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
-{
-    int src = xics_find_source(icp, irq);
-    ICSState *ics;
-
-    assert(src >= 0);
-
-    ics = &icp->ics[src];
-    ics_set_irq_type(ics, irq - ics->offset, lsi);
-}
-
 /*
  * XICS
  */
include/hw/ppc/xics.h +0 -1
index ad39c8c..8e7998f 100644
@@ -166,7 +166,6 @@ struct ICSIRQState {
 #define XICS_IRQS_SPAPR               1024
 
 qemu_irq xics_get_qirq(XICSState *icp, int irq);
-void xics_set_irq_type(XICSState *icp, int irq, bool lsi);
 
 int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi);
 int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align);
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
#91 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 32/77] ppc/xics: Replace "icp" with "xics" in most places

In reply to Benjamin Herrenschmidt
Quoted 9 lines · Benjamin Herrenschmidt

The "ICP" is a different object than the "XICS". For historical reasons, we have a number of places where we name a variable "icp" while it contains a XICSState pointer. There *is* an ICPState structure too so this makes the code really confusing.

This is a mechanical replacement of all those instances to use the name "xics" instead. There should be no functional change.

Ah, good idea. I think I'll take this one independently as well.

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
#84 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 28/77] ppc/xics: Rename existing XICS classe to XICS_SPAPR

In reply to Benjamin Herrenschmidt
Quoted 4 lines · Benjamin Herrenschmidt

The common class doesn't change, the KVM one is SPAPR specific, this is a preliminary change to make it easier to support "native" XICS.

So changing the variable names is fine, but I believe actually changing the type name from "xics" to "xics-spapr" will break migration for existing sPAPR guests. There are some ways to wangle that, but I think it's going to be much simpler to leave the existing name as "xics" and add a new "xics-native".

You can still change the name of the TYPE_XICS macro to TYPE_XICS_SPAPR if you want.

Quoted 270 lines
---
 hw/intc/xics.c        | 26 +++++++++++++-------------
 hw/intc/xics_kvm.c    |  6 +++---
 hw/ppc/spapr.c        |  6 +++---
 hw/ppc/spapr_events.c |  2 +-
 hw/ppc/spapr_pci.c    |  8 ++++----
 hw/ppc/spapr_vio.c    |  2 +-
 include/hw/ppc/xics.h | 25 +++++++++++++------------
 7 files changed, 38 insertions(+), 37 deletions(-)
hw/intc/xics.c +13 -13
index 9ff5796..bcea1f0 100644
@@ -711,7 +711,7 @@ static int ics_find_free_block(ICSState *ics, int num, int alignnum)
     return -1;
 }
 
-int xics_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
+int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
 {
     ICSState *ics = &icp->ics[src];
     int irq;
@@ -742,7 +742,7 @@ int xics_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
  * Allocate block of consecutive IRQs, and return the number of the first IRQ in the block.
  * If align==true, aligns the first IRQ number to num.
  */
-int xics_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align)
+int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align)
 {
     int i, first = -1;
     ICSState *ics = &icp->ics[src];
@@ -787,7 +787,7 @@ static void ics_free(ICSState *ics, int srcno, int num)
     }
 }
 
-void xics_free(XICSState *icp, int irq, int num)
+void xics_spapr_free(XICSState *icp, int irq, int num)
 {
     int src = xics_find_source(icp, irq);
 
@@ -1006,7 +1006,7 @@ static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
     }
 }
 
-static void xics_realize(DeviceState *dev, Error **errp)
+static void xics_spapr_realize(DeviceState *dev, Error **errp)
 {
     XICSState *icp = XICS(dev);
     Error *error = NULL;
@@ -1045,7 +1045,7 @@ static void xics_realize(DeviceState *dev, Error **errp)
     }
 }
 
-static void xics_initfn(Object *obj)
+static void xics_spapr_initfn(Object *obj)
 {
     XICSState *xics = XICS(obj);
 
@@ -1054,29 +1054,29 @@ static void xics_initfn(Object *obj)
     xics->ics->icp = xics;
 }
 
-static void xics_class_init(ObjectClass *oc, void *data)
+static void xics_spapr_class_init(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
-    XICSStateClass *xsc = XICS_CLASS(oc);
+    XICSStateClass *xsc = XICS_SPAPR_CLASS(oc);
 
-    dc->realize = xics_realize;
+    dc->realize = xics_spapr_realize;
     xsc->set_nr_irqs = xics_set_nr_irqs;
     xsc->set_nr_servers = xics_set_nr_servers;
 }
 
-static const TypeInfo xics_info = {
-    .name          = TYPE_XICS,
+static const TypeInfo xics_spapr_info = {
+    .name          = TYPE_XICS_SPAPR,
     .parent        = TYPE_XICS_COMMON,
     .instance_size = sizeof(XICSState),
     .class_size = sizeof(XICSStateClass),
-    .class_init    = xics_class_init,
-    .instance_init = xics_initfn,
+    .class_init    = xics_spapr_class_init,
+    .instance_init = xics_spapr_initfn,
 };
 
 static void xics_register_types(void)
 {
     type_register_static(&xics_common_info);
-    type_register_static(&xics_info);
+    type_register_static(&xics_spapr_info);
     type_register_static(&ics_info);
     type_register_static(&icp_info);
 }
hw/intc/xics_kvm.c +3 -3
index d58729c..03ae801 100644
@@ -490,8 +490,8 @@ static void xics_kvm_class_init(ObjectClass *oc, void *data)
     xsc->set_nr_servers = xics_kvm_set_nr_servers;
 }
 
-static const TypeInfo xics_kvm_info = {
-    .name          = TYPE_KVM_XICS,
+static const TypeInfo xics_spapr_kvm_info = {
+    .name          = TYPE_XICS_SPAPR_KVM,
     .parent        = TYPE_XICS_COMMON,
     .instance_size = sizeof(KVMXICSState),
     .class_init    = xics_kvm_class_init,
@@ -500,7 +500,7 @@ static const TypeInfo xics_kvm_info = {
 
 static void xics_kvm_register_types(void)
 {
-    type_register_static(&xics_kvm_info);
+    type_register_static(&xics_spapr_kvm_info);
     type_register_static(&ics_kvm_info);
     type_register_static(&icp_kvm_info);
 }
hw/ppc/spapr.c +3 -3
index 610629e..bf94426 100644
@@ -119,7 +119,7 @@ static XICSState *xics_system_init(MachineState *machine,
         Error *err = NULL;
 
         if (machine_kernel_irqchip_allowed(machine)) {
-            icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs, &err);
+            icp = try_create_xics(TYPE_XICS_SPAPR_KVM, nr_servers, nr_irqs, &err);
         }
         if (machine_kernel_irqchip_required(machine) && !icp) {
             error_report("kernel_irqchip requested but unavailable: %s",
@@ -128,7 +128,7 @@ static XICSState *xics_system_init(MachineState *machine,
     }
 
     if (!icp) {
-        icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs, &error_abort);
+        icp = try_create_xics(TYPE_XICS_SPAPR, nr_servers, nr_irqs, &error_abort);
     }
 
     return icp;
@@ -1768,7 +1768,7 @@ static void ppc_spapr_init(MachineState *machine)
     spapr->icp = xics_system_init(machine,
                                   DIV_ROUND_UP(max_cpus * kvmppc_smt_threads(),
                                                smp_threads),
-                                  XICS_IRQS);
+                                  XICS_IRQS_SPAPR);
 
     if (smc->dr_lmb_enabled) {
         spapr_validate_node_memory(machine);
hw/ppc/spapr_events.c +1 -1
index 744ea62..3b3663e 100644
@@ -587,7 +587,7 @@ out_no_events:
 void spapr_events_init(sPAPRMachineState *spapr)
 {
     QTAILQ_INIT(&spapr->pending_events);
-    spapr->check_exception_irq = xics_alloc(spapr->icp, 0, 0, false);
+    spapr->check_exception_irq = xics_spapr_alloc(spapr->icp, 0, 0, false);
     spapr->epow_notifier.notify = spapr_powerdown_req;
     qemu_register_powerdown_notifier(&spapr->epow_notifier);
     spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
hw/ppc/spapr_pci.c +4 -4
index 55fa8db..8b613a8 100644
@@ -313,7 +313,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
             return;
         }
 
-        xics_free(spapr->icp, msi->first_irq, msi->num);
+        xics_spapr_free(spapr->icp, msi->first_irq, msi->num);
         if (msi_present(pdev)) {
             spapr_msi_setmsg(pdev, 0, false, 0, num);
         }
@@ -351,7 +351,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
     }
 
     /* Allocate MSIs */
-    irq = xics_alloc_block(spapr->icp, 0, req_num, false,
+    irq = xics_spapr_alloc_block(spapr->icp, 0, req_num, false,
                            ret_intr_type == RTAS_TYPE_MSI);
     if (!irq) {
         error_report("Cannot allocate MSIs for device %x", config_addr);
@@ -1360,7 +1360,7 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < PCI_NUM_PINS; i++) {
         uint32_t irq;
 
-        irq = xics_alloc_block(spapr->icp, 0, 1, true, false);
+        irq = xics_spapr_alloc_block(spapr->icp, 0, 1, true, false);
         if (!irq) {
             error_setg(errp, "spapr_allocate_lsi failed");
             return;
@@ -1717,7 +1717,7 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,
     _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges));
     _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
     _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
-    _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", XICS_IRQS));
+    _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", XICS_IRQS_SPAPR));
 
     /* Build the interrupt-map, this must matches what is done
      * in pci_spapr_map_irq
hw/ppc/spapr_vio.c +1 -1
index c51eb8e..7b718cc 100644
@@ -462,7 +462,7 @@ static void spapr_vio_busdev_realize(DeviceState *qdev, Error **errp)
         dev->qdev.id = id;
     }
 
-    dev->irq = xics_alloc(spapr->icp, 0, dev->irq, false);
+    dev->irq = xics_spapr_alloc(spapr->icp, 0, dev->irq, false);
     if (!dev->irq) {
         error_setg(errp, "can't allocate IRQ");
         return;
include/hw/ppc/xics.h +13 -12
index 355a966..8d33dfa 100644
@@ -32,20 +32,20 @@
 #define TYPE_XICS_COMMON "xics-common"
 #define XICS_COMMON(obj) OBJECT_CHECK(XICSState, (obj), TYPE_XICS_COMMON)
 
-#define TYPE_XICS "xics"
-#define XICS(obj) OBJECT_CHECK(XICSState, (obj), TYPE_XICS)
+#define TYPE_XICS_SPAPR "xics-spapr"
+#define XICS(obj) OBJECT_CHECK(XICSState, (obj), TYPE_XICS_SPAPR)
 
-#define TYPE_KVM_XICS "xics-kvm"
-#define KVM_XICS(obj) OBJECT_CHECK(KVMXICSState, (obj), TYPE_KVM_XICS)
+#define TYPE_XICS_SPAPR_KVM "xics-spapr-kvm"
+#define KVM_XICS(obj) OBJECT_CHECK(KVMXICSState, (obj), TYPE_XICS_SPAPR_KVM)
 
 #define XICS_COMMON_CLASS(klass) \
      OBJECT_CLASS_CHECK(XICSStateClass, (klass), TYPE_XICS_COMMON)
-#define XICS_CLASS(klass) \
-     OBJECT_CLASS_CHECK(XICSStateClass, (klass), TYPE_XICS)
+#define XICS_SPAPR_CLASS(klass) \
+     OBJECT_CLASS_CHECK(XICSStateClass, (klass), TYPE_XICS_SPAPR)
 #define XICS_COMMON_GET_CLASS(obj) \
      OBJECT_GET_CLASS(XICSStateClass, (obj), TYPE_XICS_COMMON)
-#define XICS_GET_CLASS(obj) \
-     OBJECT_GET_CLASS(XICSStateClass, (obj), TYPE_XICS)
+#define XICS_SPAPR_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(XICSStateClass, (obj), TYPE_XICS_SPAPR)
 
 #define XICS_IPI        0x2
 #define XICS_BUID       0x1
@@ -157,13 +157,14 @@ struct ICSIRQState {
     uint8_t flags;
 };
 
-#define XICS_IRQS               1024
+#define XICS_IRQS_SPAPR               1024
 
 qemu_irq xics_get_qirq(XICSState *icp, int irq);
 void xics_set_irq_type(XICSState *icp, int irq, bool lsi);
-int xics_alloc(XICSState *icp, int src, int irq_hint, bool lsi);
-int xics_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align);
-void xics_free(XICSState *icp, int irq, int num);
+
+int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi);
+int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align);
+void xics_spapr_free(XICSState *icp, int irq, int num);
 
 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu);
 
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
#78 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 26/77] ppc/pnv: Add skeletton PowerNV platform

In reply to Benjamin Herrenschmidt
Quoted 35 lines · Benjamin Herrenschmidt
Quoted 16 lines · David Gibson
Quoted 10 lines · Benjamin Herrenschmidt

No devices yet, not even an interrupt controller, just to get started.

---
 default-configs/ppc64-softmmu.mak |   1 +
 hw/ppc/Makefile.objs              |   2 +
 hw/ppc/pnv.c                      | 600
++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/pnv.h              |  36 +++

Quoted 3 lines
 4 files changed, 639 insertions(+)

 create mode 100644 hw/ppc/pnv.c
 create mode 100644 include/hw/ppc/pnv.h

Quoted 2 lines

Many of my comments below may be made irrelevant by later patches in the series.

Heh, well there is where the "meat" of the new platform starts showing up :-)

 .../...

Quoted 10 lines
Quoted 9 lines

+#define _FDT(exp) \
+    do { \
+        int ret = (exp);                                           \
+        if (ret < 0) {                                             \
+            fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
+                    #exp, fdt_strerror(ret));                      \
+            exit(1);                                               \
+        }                                                          \
+    } while (0)

Quoted 5 lines
Quoted 2 lines

We should probably make a file where helper routines used by both spapr.c and pnv.c can live.

Probably but I'd see that as a later cleanup rather than doing it in this series...

Ok.

Quoted 42 lines

.../...

Quoted 8 lines
Quoted 4 lines

+    if (pcc->l1_dcache_size) {
+        _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size))); +    } else {
+        fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");

Hmm (note to self) should probably change a bunch of these both in spapr.c and pnv.c from explicit fprintfs() to modern error_report() and similar.

That's a train I completely missed, but yes.

  .../...

Quoted 6 lines
Quoted 6 lines

+    }
+    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s", +                       servers_prop, sizeof(servers_prop)))); +    _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s", +                       gservers_prop, sizeof(gservers_prop)))); +

Quoted 8 lines

hw/ppc/pnv.c line 305

Quoted 5 lines

+    _FDT((fdt_end_node(fdt)));

hw/ppc/pnv.c new file +4 -0
+}
+
+static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t initrd_size)
+{

So.. does it make sense for qemu to create a device tree for powernv, rather than leaving it to Opal?

Well, OPAL only creates a device-tree if you are on an FSP machine in which case it expects a complex data structure (HDAT) coming from the FSP to use as a source of info.

On OpenPower machines, which is closer to what we simulate here, we do get a device-tree as an input in OPAL, it's generated by HostBoot.

Now, I am not running HostBoot in qemu because most of what it does is completely irrelevant to an emulated system (training the various links, initializing the memory buffer chips, etc...).

Quoted 6 lines

However we do need to pass a number of platform information to OPAL which HB does via the device-tree, such as which cores are enabled, the memory map configured for PCI, which PHBs are enabled, etc...  so creating a DT in qemu makes sense, it mimmics HB in essence.

OPAL will enrich that device-tree before starting Linux.

Ok. Some comments mentioning that you're simulating the exit state from HostBoot would be good then.

Quoted 74 lines
Quoted 13 lines
Quoted 4 lines

+    /*
+     * Add info to guest to indentify which host is it being run on +     * and what is the uuid of the guest
+     */

Quoted 8 lines

+    if (kvmppc_get_host_model(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-model", buf))); +        g_free(buf);
+    }
+    if (kvmppc_get_host_serial(&buf)) {
+        _FDT((fdt_property_string(fdt, "host-serial", buf))); +        g_free(buf);
+    }

Quoted 2 lines

Since you're emulating a "bare metal" machine, surely the host properties aren't relevant here.

They may or may not. But yes, I can take that out.

Quoted 47 lines
Quoted 47 lines

+    buf = g_strdup_printf(UUID_FMT, qemu_uuid[0], qemu_uuid[1], +                          qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], +                          qemu_uuid[5], qemu_uuid[6], qemu_uuid[7], +                          qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], +                          qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], +                          qemu_uuid[14], qemu_uuid[15]);

hw/ppc/pnv.c new file +41 -0
+
+    _FDT((fdt_property_string(fdt, "vm,uuid", buf)));
+    g_free(buf);
+
+    _FDT((fdt_begin_node(fdt, "chosen")));
+    _FDT((fdt_property(fdt, "linux,initrd-start",
+                       &start_prop, sizeof(start_prop))));
+    _FDT((fdt_property(fdt, "linux,initrd-end",
+                       &end_prop, sizeof(end_prop))));
+    _FDT((fdt_end_node(fdt)));
+
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
+
+    /* cpus */
+    _FDT((fdt_begin_node(fdt, "cpus")));
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
+
+    CPU_FOREACH(cs) {
+        powernv_create_cpu_node(fdt, cs, smt);
+    }
+
+    _FDT((fdt_end_node(fdt)));
+
+    /* Memory */
+    _FDT((powernv_populate_memory(fdt)));
+
+    /* /hypervisor node */
+    if (kvm_enabled()) {
+        uint8_t hypercall[16];
+
+        /* indicate KVM hypercall interface */
+        _FDT((fdt_begin_node(fdt, "hypervisor")));
+        _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
+        if (kvmppc_has_cap_fixup_hcalls()) {
+            /*
+             * Older KVM versions with older guest kernels were broken with the
+             * magic page, don't allow the guest to map it.
+             */
+            kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
Quoted 8 lines
Quoted 6 lines

+                                 sizeof(hypercall));
+            _FDT((fdt_property(fdt, "hcall-instructions", hypercall, +                              sizeof(hypercall))));
+        }
+        _FDT((fdt_end_node(fdt)));
+    }

And a hypercall interface surely doesn't make sense for powernv.

Quoted 3 lines

It's qemu paravirt, it exist on G5 too :-) It's for PR KVM, it allows to speed up some bits and pieces. But yeah we don't yet really "support" it at this point. However we might.

Ah, yes, I forgot about that.

Quoted 37 lines
Quoted 28 lines
Quoted 11 lines
hw/ppc/pnv.c new file +11 -0
+
+    _FDT((fdt_end_node(fdt))); /* close root node */
+    _FDT((fdt_finish(fdt)));
+
+    return fdt;
+}
+
+static void powernv_cpu_reset(void *opaque)
+{
+    PowerPCCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);

hw/ppc/pnv.c line 409

Quoted 10 lines
hw/ppc/pnv.c new file +10 -0
+    CPUPPCState *env = &cpu->env;
+
+    cpu_reset(cs);
+
+    env->spr[SPR_PIR] = ppc_get_vcpu_dt_id(cpu);
+    env->spr[SPR_HIOR] = 0;
+    env->gpr[3] = FDT_ADDR;
+    env->nip = 0x10;
+    env->msr |= MSR_HVB;
+}

So, I believe the qemu-ishly correct way of doing this is to have the cpu initialization in the cpu code, rather than the platform code, as much as possible.  On PAPR we kind of get away with initialization in the platform code on the grounds that it's a paravirt platform, but powernv doesn't have that excuse.

Quoted 1 line

But this may well be stuff that changes in later patches, so..

Well no, not really. But here too, we mimmic the state as coming out of HostBoot, which isn't quite the same thing. We need to provide the FDT entry, etc...

The "real" reset state of a P8 isn't something we can easily simulate...

Quoted 8 lines

It runs some microcode from a SEEPROM with a small microcontroller which initializes a core, which then runs some HB code off it's L3 cache etc... really not something we want to do in qemu at least for now.

So the initial state here is somewhat in between full virt and paravirt, we simulate a platform that has been partially initialized by HostBoot, to the state it has when it enters OPAL.

Ok, that makes sense, but I think it needs a bit more explanation in the code to that effect.

Quoted 120 lines
Quoted 9 lines

hw/ppc/pnv.c line 415

Quoted 5 lines
hw/ppc/pnv.c new file +5 -0
+static const VMStateDescription vmstate_powernv = {
+    .name = "powernv",
+    .version_id = 1,
+    .minimum_version_id = 1,
+};

It might be best to leave out the vmstate entirely until you're ready to implement migration, rather than having a partial, probably not working migration implementation.

Ok.

Quoted 6 lines
Quoted 6 lines
hw/ppc/pnv.c new file +6 -0
+
+static void pnv_create_chip(PnvSystem *sys, unsigned int chip_no)
+{
+    PnvChip *chip = &sys->chips[chip_no];
+
+    if (chip_no >= PNV_MAX_CHIPS) {
Quoted 8 lines
Quoted 5 lines

+            return;
+    }
+
+    /* XXX Improve chip numbering to better match HW */ +    chip->chip_id = chip_no;

I think modern qemu conventions would suggest creating the chips as QOM objects rather than having a fixed array.

Yeah, more code & much larger memory footprint for the same result :-)

I can look into it but it's low priority. I still want to rework some of that chip stuff in future patches anyway.

Quoted 69 lines
Quoted 69 lines
hw/ppc/pnv.c new file +69 -0
+}
+
+static void ppc_powernv_init(MachineState *machine)
+{
+    ram_addr_t ram_size = machine->ram_size;
+    const char *cpu_model = machine->cpu_model;
+    const char *kernel_filename = machine->kernel_filename;
+    const char *initrd_filename = machine->initrd_filename;
+    uint32_t initrd_base = 0;
+    long initrd_size = 0;
+    PowerPCCPU *cpu;
+    CPUPPCState *env;
+    MemoryRegion *sysmem = get_system_memory();
+    MemoryRegion *ram = g_new(MemoryRegion, 1);
+    sPowerNVMachineState *pnv_machine = POWERNV_MACHINE(machine);
+    PnvSystem *sys = &pnv_machine->sys;
+    long fw_size;
+    char *filename;
+    void *fdt;
+    int i;
+
+    /* init CPUs */
+    if (cpu_model == NULL) {
+        cpu_model = kvm_enabled() ? "host" : "POWER8";
+    }
+
+    for (i = 0; i < smp_cpus; i++) {
+        cpu = cpu_ppc_init(cpu_model);
+        if (cpu == NULL) {
+            fprintf(stderr, "Unable to find PowerPC CPU definition\n");
+            exit(1);
+        }
+        env = &cpu->env;
+
+        /* Set time-base frequency to 512 MHz */
+        cpu_ppc_tb_init(env, TIMEBASE_FREQ);
+
+        /* MSR[IP] doesn't exist nowadays */
+        env->msr_mask &= ~(1 << 6);
+
+        qemu_register_reset(powernv_cpu_reset, cpu);
+    }
+
+    /* allocate RAM */
+    memory_region_allocate_system_memory(ram, NULL, "ppc_powernv.ram", ram_size);
+    memory_region_add_subregion(sysmem, 0, ram);
+
+    /* XXX We should decide how many chips to create based on #cores and
+     * Venice vs. Murano vs. Naples chip type etc..., for now, just create
+     * one chip. Also creation of the CPUs should be done per-chip
+     */
+    sys->num_chips = 1;
+
+    /* Create only one PHB for now until I figure out what's wrong
+     * when I create more (resource assignment failures in Linux)
+     */
+    pnv_create_chip(sys, 0);
+
+    if (bios_name == NULL) {
+        bios_name = FW_FILE_NAME;
+    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
+    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
+    if (fw_size < 0) {
+        hw_error("qemu: could not load OPAL '%s'\n", filename);
+        exit(1);
+    }
+    g_free(filename);
+
Quoted 8 lines
Quoted 6 lines

+
+    if (kernel_filename == NULL) {
+        kernel_filename = KERNEL_FILE_NAME; +    }
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, kernel_filename);

The commit withe Opal image should go in before this, no?

Now this is a bit of an open discussion at the moment :-)

The way OPAL is built on OPP machines today is by essentially building a complete flash image which contains HostBoot, OPAL and the petitboot- based bootloader which contains a Linux kernel etc...

We could create a target without HB and with a slimmed down Linux but it would still probably be about 12MB I reckon, if not more. It feels a bit "big" to ship as a binary as part of qemu...

Quoted 6 lines

We would also have to add code to qemu to "find" OPAL in that image, and then add a model for the flash controller.

The other option is to bundle just OPAL itself. However that means you can't go anywhere without a -kernel argument, which would then be either a petitboot-based bootloader or your actual target kernel.

Hm, ok. But in order for this to be usable, we need some way to get a suitable image. So medium term, I think it makes sense to include both opal and PetitBoot, so you can boot the guest like a real machine.

However, including only Opal and requiring -kernel would be a reasonable interim step.

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
#80 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 27/77] ppc/pnv: Add XSCOM infrastructure

In reply to Benjamin Herrenschmidt
Quoted 68 lines · Benjamin Herrenschmidt

XSCOM is an interface to a sideband bus provided by the POWER8 chip pervasive unit, which gives access to a number of facilities in the chip that are needed by the OPAL firmware and to a lesser extent, Linux. This is among others how the PCI Host bridges get configured at boot or how the LPC bus is accessed.

This provides a simple bus and device type for devices sitting on XSCOM along with some facilities to optionally generate corresponding device-tree nodes

---
 hw/ppc/Makefile.objs       |   2 +-
 hw/ppc/pnv.c               |  11 ++
 hw/ppc/pnv_xscom.c         | 415 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/pnv.h       |   2 +
 include/hw/ppc/pnv_xscom.h |  73 ++++++++
 5 files changed, 502 insertions(+), 1 deletion(-)
 create mode 100644 hw/ppc/pnv_xscom.c
 create mode 100644 include/hw/ppc/pnv_xscom.h
hw/ppc/Makefile.objs +1 -1
index cd74c96..2a7dd42 100644
@@ -5,7 +5,7 @@ obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
 obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o spapr_drc.o spapr_rng.o
 # IBM PowerNV
-obj-$(CONFIG_POWERNV) += pnv.o
+obj-$(CONFIG_POWERNV) += pnv.o pnv_xscom.o
 ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
 obj-y += spapr_pci_vfio.o
 endif
hw/ppc/pnv.c +8 -0
index e68c9b1..2eac877 100644
@@ -41,6 +41,7 @@
 #include "hw/ppc/ppc.h"
 #include "hw/ppc/pnv.h"
 #include "hw/loader.h"
+#include "hw/ppc/pnv_xscom.h"
 
 #include "exec/address-spaces.h"
 #include "qemu/config-file.h"
@@ -310,6 +311,7 @@ static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t i
     uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
     char *buf;
     const char plat_compat[] = "qemu,powernv\0ibm,powernv";
+    unsigned int i;
 
     fdt = g_malloc0(FDT_MAX_SIZE);
     _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
@@ -367,6 +369,12 @@ static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t i
     /* Memory */
     _FDT((powernv_populate_memory(fdt)));
 
+    /* For each chip */
+    for (i = 0; i < sys->num_chips; i++) {
+        /* Populate XSCOM */
+        _FDT((xscom_populate_fdt(sys->chips[i].xscom, fdt)));
+    }
+
     /* /hypervisor node */
     if (kvm_enabled()) {
         uint8_t hypercall[16];

hw/ppc/pnv.c static void pnv_create_chip(PnvSystem *sys, unsigned int chip_no) · line 437

Quoted 6 lines
hw/ppc/pnv.c +3 -0
@@ -424,6 +432,9 @@ static void pnv_create_chip(PnvSystem *sys, unsigned int chip_no)
 
     /* XXX Improve chip numbering to better match HW */
     chip->chip_id = chip_no;
+
+    /* Set up XSCOM bus */
+    xscom_create(chip);

Hmm.. I'm thinking it probably makes sense to unify the representation of "chip" and "xscom" since there's a 1:1 correspondance. To be QOMishly correct, I think the right way would be a SysBusDevice for each chip, implementing the xscom MMIOs, then an array of link properties under the Machine object to find the right scoms by chip id.

Quoted 64 lines
hw/ppc/pnv.c +0 -0
 }
 
 static void ppc_powernv_init(MachineState *machine)
hw/ppc/pnv_xscom.c new file +55 -0
new file mode 100644
index 0000000..bb35422
@@ -0,0 +1,415 @@
+
+/*
+ * QEMU PowerNV XSCOM bus definitions
+ *
+ * Copyright (c) 2010 David Gibson, IBM Corporation <[email protected]>
+ * Based on the s390 virtio bus code:
+ * Copyright (c) 2009 Alexander Graf <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* TODO: Add some infrastructure for "random stuff" and FIRs that
+ * various units might want to deal with without creating actual
+ * XSCOM devices.
+ *
+ * For example, HB LPC XSCOM in the PIBAM
+ */
+#include "hw/hw.h"
+#include "sysemu/sysemu.h"
+#include "hw/boards.h"
+#include "monitor/monitor.h"
+#include "hw/loader.h"
+#include "elf.h"
+#include "hw/sysbus.h"
+#include "sysemu/kvm.h"
+#include "sysemu/device_tree.h"
+#include "kvm_ppc.h"
+
+#include "hw/ppc/pnv_xscom.h"
+
+#include <libfdt.h>
+
+#define TYPE_XSCOM "xscom"
+#define XSCOM(obj) OBJECT_CHECK(XScomState, (obj), TYPE_XSCOM)
+
+#define XSCOM_SIZE        0x800000000ull
+#define XSCOM_BASE(chip)  (0x3fc0000000000ull + ((uint64_t)(chip)) * XSCOM_SIZE)
+
+//#define TRACE_SCOMS
+
+typedef struct XScomState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+    /*< public >*/

hw/ppc/pnv_xscom.c line 65

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+
+    MemoryRegion mem;
+    int32_t chip_id;
+    XScomBus *bus;
+} XScomState;
+
+static uint32_t xscom_to_pcb_addr(uint64_t addr)
+{
+        addr &= (XSCOM_SIZE - 1);
+        return ((addr >> 4) & ~0xfull) | ((addr >> 3) & 0xf);

Wow, that's a pretty weird address transform.

hw/ppc/pnv_xscom.c line 78

Quoted 13 lines
hw/ppc/pnv_xscom.c new file +13 -0
+}
+
+static void xscom_complete(uint64_t hmer_bits)
+{
+    CPUState *cs = current_cpu;
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    CPUPPCState *env = &cpu->env;
+
+    cpu_synchronize_state(cs);
+    env->spr[SPR_HMER] |= hmer_bits;
+
+    /* XXX Need a CPU helper to set HMER, also handle gneeration
+     * of HMIs

Not sure what you're referring to here. Nothing more should be needed to set the HMER - because you've called cpu_synchronize_state() it will be marked dirty and flushed back to KVM before re-entry.

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+     */
+}
+
+static XScomDevice *xscom_find_target(XScomState *s, uint32_t pcb_addr, uint32_t *range)
+{
+    BusChild *bc;
+
+    QTAILQ_FOREACH(bc, &s->bus->bus.children, sibling) {
+        DeviceState *qd = bc->child;
+        XScomDevice *xd = XSCOM_DEVICE(qd);

hw/ppc/pnv_xscom.c line 98

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+        unsigned int i;
+
+        for (i = 0; i < MAX_XSCOM_RANGES; i++) {
+            if (xd->ranges[i].addr <= pcb_addr &&
+                (xd->ranges[i].addr + xd->ranges[i].size) > pcb_addr) {
+                *range = i;
+                return xd;
+            }
+        }
+    }

I'm wondering if it makes sense to construct a custom AddressSpace and use the existing address space lookup logic from exec.c and memory.c rather than implementing your own.

Quoted 38 lines
hw/ppc/pnv_xscom.c new file +38 -0
+    return NULL;
+}
+
+static bool xscom_dispatch_read(XScomState *s, uint32_t pcb_addr, uint64_t *out_val)
+{
+    uint32_t range, offset;
+    struct XScomDevice *xd = xscom_find_target(s, pcb_addr, &range);
+    XScomDeviceClass *xc;
+
+    if (!xd) {
+        return false;
+    }
+    xc = XSCOM_DEVICE_GET_CLASS(xd);
+    if (!xc->read) {
+        return false;
+    }
+    offset = pcb_addr - xd->ranges[range].addr;
+    return xc->read(xd, range, offset, out_val);
+}
+
+static bool xscom_dispatch_write(XScomState *s, uint32_t pcb_addr, uint64_t val)
+{
+    uint32_t range, offset;
+    struct XScomDevice *xd = xscom_find_target(s, pcb_addr, &range);
+    XScomDeviceClass *xc;
+
+    if (!xd) {
+        return false;
+    }
+    xc = XSCOM_DEVICE_GET_CLASS(xd);
+    if (!xc->write) {
+        return false;
+    }
+    offset = pcb_addr - xd->ranges[range].addr;
+    return xc->write(xd, range, offset, val);
+}
+
+static uint64_t xscom_read(void *opaque, hwaddr addr, unsigned width)

hw/ppc/pnv_xscom.c line 146

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+{
+    XScomState *s = opaque;
+    uint32_t pcba = xscom_to_pcb_addr(addr);
+    uint64_t val;
+
+    assert(width == 8);
+
+#ifdef TRACE_SCOMS
+    printf("XSCOM_READ(0x%x:0x%x)\n", s->chip_id, pcba);
+#endif

You should be using the built in trace infrastructure here - it's really not that much of a pain. Put

	trace_xscom_read(s->chip_id, pcba)

here, put a suitable format in trace-events, and ./configure --enable-trace-backends=stderr

Quoted 85 lines
hw/ppc/pnv_xscom.c new file +85 -0
+
+    /* Handle some SCOMs here before dispatch */
+    switch(pcba) {
+    case 0xf000f:
+        val = 0x221EF04980000000;
+        break;
+    case 0x1010c00:     /* PIBAM FIR */
+    case 0x1010c03:     /* PIBAM FIR MASK */
+    case 0x2020007:     /* ADU stuff */
+    case 0x2020009:     /* ADU stuff */
+    case 0x202000f:     /* ADU stuff */
+        val = 0;
+        break;
+    case 0x2013f00:     /* PBA stuff */
+    case 0x2013f01:     /* PBA stuff */
+    case 0x2013f02:     /* PBA stuff */
+    case 0x2013f03:     /* PBA stuff */
+    case 0x2013f04:     /* PBA stuff */
+    case 0x2013f05:     /* PBA stuff */
+    case 0x2013f06:     /* PBA stuff */
+    case 0x2013f07:     /* PBA stuff */
+        val = 0;
+        break;
+    default:
+        if (!xscom_dispatch_read(s, pcba, &val)) {
+            xscom_complete(HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
+            return 0;
+        }
+    }
+
+    xscom_complete(HMER_XSCOM_DONE);
+    return val;
+}
+
+static void xscom_write(void *opaque, hwaddr addr, uint64_t val,
+                        unsigned width)
+{
+    XScomState *s = opaque;
+    uint32_t pcba = xscom_to_pcb_addr(addr);
+
+    assert(width == 8);
+
+#ifdef TRACE_SCOMS
+    printf("XSCOM_WRITE(0x%x:0x%x, 0x%016llx)\n",
+           s->chip_id, pcba, (unsigned long long)val);
+#endif
+    /* Handle some SCOMs here before dispatch */
+    switch(pcba) {
+        /* We ignore writes to these */
+    case 0xf000f:       /* chip id is RO */
+    case 0x1010c00:     /* PIBAM FIR */
+    case 0x1010c01:     /* PIBAM FIR */
+    case 0x1010c02:     /* PIBAM FIR */
+    case 0x1010c03:     /* PIBAM FIR MASK */
+    case 0x1010c04:     /* PIBAM FIR MASK */
+    case 0x1010c05:     /* PIBAM FIR MASK */
+    case 0x2020007:     /* ADU stuff */
+    case 0x2020009:     /* ADU stuff */
+    case 0x202000f:     /* ADU stuff */
+        break;
+    default:
+        if (!xscom_dispatch_write(s, pcba, val)) {
+            xscom_complete(HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
+            return;
+        }
+    }
+
+    xscom_complete(HMER_XSCOM_DONE);
+}
+
+static const MemoryRegionOps xscom_ops = {
+    .read = xscom_read,
+    .write = xscom_write,
+    .valid.min_access_size = 8,
+    .valid.max_access_size = 8,
+    .impl.min_access_size = 8,
+    .impl.max_access_size = 8,
+    .endianness = DEVICE_BIG_ENDIAN,
+};
+
+static int xscom_init(SysBusDevice *dev)
+{
+    XScomState *s = XSCOM(dev);
+
+    s->chip_id = -1;

hw/ppc/pnv_xscom.c line 241

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+    return 0;
+}
+
+static void xscom_realize(DeviceState *dev, Error **errp)
+{
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    XScomState *s = XSCOM(dev);
+    char *name;
+
+    assert(s->chip_id >= 0);

So, this assert could be tripped if the user explicitly instantiated an xscom device which they probably shouldn't do, but could. So, it probably makes sense to use error_setg() here instead of assert().

Quoted 47 lines
hw/ppc/pnv_xscom.c new file +47 -0
+    name = g_strdup_printf("xscom-%x", s->chip_id);
+    memory_region_init_io(&s->mem, OBJECT(s), &xscom_ops, s, name, XSCOM_SIZE);
+    sysbus_init_mmio(sbd, &s->mem);
+    sysbus_mmio_map(sbd, 0, XSCOM_BASE(s->chip_id));
+}
+
+static Property xscom_properties[] = {
+        DEFINE_PROP_INT32("chip_id", XScomState, chip_id, 0),
+        DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xscom_class_init(ObjectClass *klass, void *data)
+{
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = xscom_properties;
+    dc->realize = xscom_realize;
+    k->init = xscom_init;
+}
+
+static const TypeInfo xscom_info = {
+    .name          = TYPE_XSCOM,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XScomState),
+    .class_init    = xscom_class_init,
+};
+
+static void xscom_bus_class_init(ObjectClass *klass, void *data)
+{
+}
+
+static const TypeInfo xscom_bus_info = {
+    .name = TYPE_XSCOM_BUS,
+    .parent = TYPE_BUS,
+    .class_init = xscom_bus_class_init,
+    .instance_size = sizeof(XScomBus),
+};
+
+void xscom_create(PnvChip *chip)
+{
+    DeviceState *dev;
+    XScomState *xdev;
+    BusState *qbus;
+    XScomBus *xb;
+
+    dev = qdev_create(NULL, TYPE_XSCOM);

hw/ppc/pnv_xscom.c line 298

Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+    qdev_prop_set_uint32(dev, "chip_id", chip->chip_id);
+    qdev_init_nofail(dev);
+
+    /* Create bus on bridge device */
+    qbus = qbus_create(TYPE_XSCOM_BUS, dev, "xscom");
+    xb = DO_UPCAST(XScomBus, bus, qbus);
+    xb->chip_id = chip->chip_id;
+    xdev = XSCOM(dev);
+    xdev->bus = xb;
+    chip->xscom = xb;

I believe the qbus_create() is usually invoked by the bridge's init function, rather than externally.

Quoted 213 lines
hw/ppc/pnv_xscom.c new file +117 -0
+}
+
+#define _FDT(exp) \
+    do { \
+        int ret = (exp);                                           \
+        if (ret < 0) {                                             \
+            fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
+                    #exp, fdt_strerror(ret));                      \
+            exit(1);                                               \
+        }                                                          \
+    } while (0)
+
+
+int xscom_populate_fdt(XScomBus *xb, void *fdt)
+{
+    BusChild *bc;
+    char *name;
+    const char compat[] = "ibm,power8-xscom\0ibm,xscom";
+    uint64_t reg[] = { cpu_to_be64(XSCOM_BASE(xb->chip_id)),
+                       cpu_to_be64(XSCOM_SIZE) };
+
+    name = g_strdup_printf("xscom@%llx", (unsigned long long)be64_to_cpu(reg[0]));
+    _FDT((fdt_begin_node(fdt, name)));
+    g_free(name);
+    _FDT((fdt_property_cell(fdt, "ibm,chip-id", xb->chip_id)));
+    _FDT((fdt_property_cell(fdt, "#address-cells", 1)));
+    _FDT((fdt_property_cell(fdt, "#size-cells", 1)));
+    _FDT((fdt_property(fdt, "reg", reg, sizeof(reg))));
+    _FDT((fdt_property(fdt, "compatible", compat, sizeof(compat)))); 
+    _FDT((fdt_property(fdt, "scom-controller", NULL, 0))); 
+
+    QTAILQ_FOREACH(bc, &xb->bus.children, sibling) {
+        DeviceState *qd = bc->child;
+        XScomDevice *xd = XSCOM_DEVICE(qd);
+        XScomDeviceClass *xc = XSCOM_DEVICE_GET_CLASS(xd);
+        uint32_t reg[MAX_XSCOM_RANGES * 2];
+        unsigned int i, sz = 0;
+        void *cp, *p;
+
+        /* Some XSCOM slaves may not be represented in the DT */
+        if (!xc->dt_name) {
+            continue;
+        }
+        name = g_strdup_printf("%s@%x", xc->dt_name, xd->ranges[0].addr);
+        _FDT((fdt_begin_node(fdt, name)));
+        g_free(name);
+        for (i = 0; i < MAX_XSCOM_RANGES; i++) {
+            if (xd->ranges[i].size == 0) {
+                break;
+            }
+            reg[sz++] = cpu_to_be32(xd->ranges[i].addr);
+            reg[sz++] = cpu_to_be32(xd->ranges[i].size);
+        }
+        _FDT((fdt_property(fdt, "reg", reg, sz * 4)));
+        if (xc->devnode) {
+            _FDT((xc->devnode(xd, fdt)));
+        }
+#define MAX_COMPATIBLE_PROP     1024
+        cp = p = g_malloc0(MAX_COMPATIBLE_PROP);
+        i = 0;
+        while((p - cp) < MAX_COMPATIBLE_PROP) {
+            int l;
+            if (xc->dt_compatible[i] == NULL) {
+                break;
+            }
+            l = strlen(xc->dt_compatible[i]);
+            if (l >= (MAX_COMPATIBLE_PROP - i)) {
+                break;
+            }
+            strcpy(p, xc->dt_compatible[i++]);
+            p += l + 1;
+        }
+        _FDT((fdt_property(fdt, "compatible", cp, p - cp)));
+        _FDT((fdt_end_node(fdt)));
+    }
+
+    _FDT((fdt_end_node(fdt)));
+
+    return 0;
+}
+
+static int xscom_qdev_init(DeviceState *qdev)
+{
+    XScomDevice *xdev = (XScomDevice *)qdev;
+    XScomDeviceClass *xc = XSCOM_DEVICE_GET_CLASS(xdev);
+
+    if (xc->init) {
+        return xc->init(xdev);
+    }
+    return 0;
+}
+
+static void xscom_device_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *k = DEVICE_CLASS(klass);
+    k->init = xscom_qdev_init;
+    k->bus_type = TYPE_XSCOM_BUS;
+}
+
+static const TypeInfo xscom_dev_info = {
+    .name = TYPE_XSCOM_DEVICE,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(XScomDevice),
+    .abstract = true,
+    .class_size = sizeof(XScomDeviceClass),
+    .class_init = xscom_device_class_init,
+};
+
+static void xscom_register_types(void)
+{
+    type_register_static(&xscom_info);
+    type_register_static(&xscom_bus_info);
+    type_register_static(&xscom_dev_info);
+}
+
+type_init(xscom_register_types)
+
include/hw/ppc/pnv.h +2 -0
index 9a48c16..cb157eb 100644
@@ -20,10 +20,12 @@
  */
 
 #include "hw/hw.h"
+typedef struct XScomBus XScomBus;
 
 /* Should we turn that into a QOjb of some sort ? */
 typedef struct PnvChip {
     uint32_t         chip_id;
+    XScomBus         *xscom;
 } PnvChip;
 
 typedef struct PnvSystem {
include/hw/ppc/pnv_xscom.h new file +73 -0
new file mode 100644
index 0000000..99de078
@@ -0,0 +1,73 @@
+#ifndef _HW_XSCOM_H
+#define _HW_XSCOM_H
+/*
+ * QEMU PowerNV XSCOM bus definitions
+ *
+ * Copyright (c) 2010 David Gibson, IBM Corporation <[email protected]>
+ * Based on the s390 virtio bus definitions:
+ * Copyright (c) 2009 Alexander Graf <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <hw/ppc/pnv.h>
+
+#define TYPE_XSCOM_DEVICE "xscom-device"
+#define XSCOM_DEVICE(obj) \
+     OBJECT_CHECK(XScomDevice, (obj), TYPE_XSCOM_DEVICE)
+#define XSCOM_DEVICE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(XScomDeviceClass, (klass), TYPE_XSCOM_DEVICE)
+#define XSCOM_DEVICE_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(XScomDeviceClass, (obj), TYPE_XSCOM_DEVICE)
+
+#define TYPE_XSCOM_BUS "xscom-bus"
+#define XSCOM_BUS(obj) OBJECT_CHECK(XScomBus, (obj), TYPE_XSCOM_BUS)
+
+typedef struct XScomDevice XScomDevice;
+typedef struct XScomBus XScomBus;
+
+typedef struct XScomDeviceClass {
+    DeviceClass parent_class;
+
+    const char *dt_name;
+    const char **dt_compatible;
+    int (*init)(XScomDevice *dev);
+    int (*devnode)(XScomDevice *dev, void *fdt);
+
+    /* Actual XScom accesses */
+    bool (*read)(XScomDevice *dev, uint32_t range, uint32_t offset, uint64_t *out_val);
+    bool (*write)(XScomDevice *dev, uint32_t range, uint32_t offset, uint64_t val);
+} XScomDeviceClass;
+
+typedef struct XScomRange {
+    uint32_t addr;
+    uint32_t size;
+} XScomRange;
+
+struct XScomDevice {
+    DeviceState qdev;
+#define MAX_XSCOM_RANGES	4
+    struct XScomRange ranges[MAX_XSCOM_RANGES];
+};
+
+struct XScomBus {
+    BusState bus;
+    uint32_t chip_id;
+};
+
+extern void xscom_create(PnvChip *chip);
+extern int xscom_populate_fdt(XScomBus *xscom, void *fdt);
+
+
+#endif /* _HW_XSCOM_H */
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
#86 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 29/77] ppc/xics: Move SPAPR specific code to a separate file

In reply to Benjamin Herrenschmidt
Quoted 4 lines · Benjamin Herrenschmidt

Leave the core ICP/ICS logic in xics.c and move the top level class wrapper, hypercall and RTAS handlers to xics_spapr.c

Concept looks fine.

Quoted 961 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 hw/intc/Makefile.objs             |   1 +
 hw/intc/xics.c                    | 390 ++----------------------------------
 hw/intc/xics_spapr.c              | 401 ++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/xics.h             |  23 +++
 5 files changed, 437 insertions(+), 379 deletions(-)
 create mode 100644 hw/intc/xics_spapr.c
default-configs/ppc64-softmmu.mak +1 -0
index 96574c8..516a6e2 100644
@@ -50,6 +50,7 @@ CONFIG_ETSEC=y
 CONFIG_LIBDECNUMBER=y
 # For pSeries
 CONFIG_XICS=$(CONFIG_PSERIES)
+CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(and $(CONFIG_PSERIES),$(CONFIG_KVM))
 # For PReP
 CONFIG_MC146818RTC=y
hw/intc/Makefile.objs +1 -0
index 004b0c2..e24cb03 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_OMAP) += omap_intc.o
 obj-$(CONFIG_OPENPIC_KVM) += openpic_kvm.o
 obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
+obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/xics.c +11 -379
index bcea1f0..38cacd8 100644
@@ -33,7 +33,7 @@
 #include "qemu/error-report.h"
 #include "qapi/visitor.h"
 
-static int get_cpu_index_by_dt_id(int cpu_dt_id)
+int get_cpu_index_by_dt_id(int cpu_dt_id)
 {
     PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
 
@@ -224,7 +224,7 @@ static void icp_resend(XICSState *icp, int server)
     ics_resend(icp->ics);
 }
 
-static void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
+void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
 {
     ICPState *ss = icp->ss + server;
     uint8_t old_cppr;
@@ -248,7 +248,7 @@ static void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
     }
 }
 
-static void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
+void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
 {
     ICPState *ss = icp->ss + server;
 
@@ -258,7 +258,7 @@ static void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
     }
 }
 
-static uint32_t icp_accept(ICPState *ss)
+uint32_t icp_accept(ICPState *ss)
 {
     uint32_t xirr = ss->xirr;
 
@@ -271,7 +271,7 @@ static uint32_t icp_accept(ICPState *ss)
     return xirr;
 }
 
-static void icp_eoi(XICSState *icp, int server, uint32_t xirr)
+void icp_eoi(XICSState *icp, int server, uint32_t xirr)
 {
     ICPState *ss = icp->ss + server;
 
@@ -372,12 +372,6 @@ static const TypeInfo icp_info = {
 /*
  * ICS: Source layer
  */
-static int ics_valid_irq(ICSState *ics, uint32_t nr)
-{
-    return (nr >= ics->offset)
-        && (nr < (ics->offset + ics->nr_irqs));
-}
-
 static void resend_msi(ICSState *ics, int srcno)
 {
     ICSIRQState *irq = ics->irqs + srcno;
@@ -462,8 +456,8 @@ static void write_xive_lsi(ICSState *ics, int srcno)
     resend_lsi(ics, srcno);
 }
 
-static void ics_write_xive(ICSState *ics, int nr, int server,
-                           uint8_t priority, uint8_t saved_priority)
+void ics_write_xive(ICSState *ics, int nr, int server,
+		    uint8_t priority, uint8_t saved_priority)
 {
     int srcno = nr - ics->offset;
     ICSIRQState *irq = ics->irqs + srcno;
@@ -640,7 +634,7 @@ static const TypeInfo ics_info = {
 /*
  * Exported functions
  */
-static int xics_find_source(XICSState *icp, int irq)
+int xics_find_source(XICSState *icp, int irq)
 {
     int sources = 1;
     int src;
@@ -668,7 +662,7 @@ qemu_irq xics_get_qirq(XICSState *icp, int irq)
     return NULL;
 }
 
-static void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
+void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
 {
     assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
 
@@ -687,310 +681,16 @@ void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
     ics_set_irq_type(ics, irq - ics->offset, lsi);
 }
 
-#define ICS_IRQ_FREE(ics, srcno)   \
-    (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
-
-static int ics_find_free_block(ICSState *ics, int num, int alignnum)
-{
-    int first, i;
-
-    for (first = 0; first < ics->nr_irqs; first += alignnum) {
-        if (num > (ics->nr_irqs - first)) {
-            return -1;
-        }
-        for (i = first; i < first + num; ++i) {
-            if (!ICS_IRQ_FREE(ics, i)) {
-                break;
-            }
-        }
-        if (i == (first + num)) {
-            return first;
-        }
-    }
-
-    return -1;
-}
-
-int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
-{
-    ICSState *ics = &icp->ics[src];
-    int irq;
-
-    if (irq_hint) {
-        assert(src == xics_find_source(icp, irq_hint));
-        if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
-            trace_xics_alloc_failed_hint(src, irq_hint);
-            return -1;
-        }
-        irq = irq_hint;
-    } else {
-        irq = ics_find_free_block(ics, 1, 1);
-        if (irq < 0) {
-            trace_xics_alloc_failed_no_left(src);
-            return -1;
-        }
-        irq += ics->offset;
-    }
-
-    ics_set_irq_type(ics, irq - ics->offset, lsi);
-    trace_xics_alloc(src, irq);
-
-    return irq;
-}
-
-/*
- * Allocate block of consecutive IRQs, and return the number of the first IRQ in the block.
- * If align==true, aligns the first IRQ number to num.
- */
-int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align)
-{
-    int i, first = -1;
-    ICSState *ics = &icp->ics[src];
-
-    assert(src == 0);
-    /*
-     * MSIMesage::data is used for storing VIRQ so
-     * it has to be aligned to num to support multiple
-     * MSI vectors. MSI-X is not affected by this.
-     * The hint is used for the first IRQ, the rest should
-     * be allocated continuously.
-     */
-    if (align) {
-        assert((num == 1) || (num == 2) || (num == 4) ||
-               (num == 8) || (num == 16) || (num == 32));
-        first = ics_find_free_block(ics, num, num);
-    } else {
-        first = ics_find_free_block(ics, num, 1);
-    }
-
-    if (first >= 0) {
-        for (i = first; i < first + num; ++i) {
-            ics_set_irq_type(ics, i, lsi);
-        }
-    }
-    first += ics->offset;
-
-    trace_xics_alloc_block(src, first, num, lsi, align);
-
-    return first;
-}
-
-static void ics_free(ICSState *ics, int srcno, int num)
-{
-    int i;
-
-    for (i = srcno; i < srcno + num; ++i) {
-        if (ICS_IRQ_FREE(ics, i)) {
-            trace_xics_ics_free_warn(ics - ics->icp->ics, i + ics->offset);
-        }
-        memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
-    }
-}
-
-void xics_spapr_free(XICSState *icp, int irq, int num)
-{
-    int src = xics_find_source(icp, irq);
-
-    if (src >= 0) {
-        ICSState *ics = &icp->ics[src];
-
-        /* FIXME: implement multiple sources */
-        assert(src == 0);
-
-        trace_xics_ics_free(ics - icp->ics, irq, num);
-        ics_free(ics, irq - ics->offset, num);
-    }
-}
-
-/*
- * Guest interfaces
- */
-
-static target_ulong h_cppr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                           target_ulong opcode, target_ulong *args)
-{
-    CPUState *cs = CPU(cpu);
-    target_ulong cppr = args[0];
-
-    icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
-    return H_SUCCESS;
-}
-
-static target_ulong h_ipi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                          target_ulong opcode, target_ulong *args)
-{
-    target_ulong server = get_cpu_index_by_dt_id(args[0]);
-    target_ulong mfrr = args[1];
-
-    if (server >= spapr->icp->nr_servers) {
-        return H_PARAMETER;
-    }
-
-    icp_set_mfrr(spapr->icp, server, mfrr);
-    return H_SUCCESS;
-}
-
-static target_ulong h_xirr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                           target_ulong opcode, target_ulong *args)
-{
-    CPUState *cs = CPU(cpu);
-    uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
-
-    args[0] = xirr;
-    return H_SUCCESS;
-}
-
-static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                             target_ulong opcode, target_ulong *args)
-{
-    CPUState *cs = CPU(cpu);
-    ICPState *ss = &spapr->icp->ss[cs->cpu_index];
-    uint32_t xirr = icp_accept(ss);
-
-    args[0] = xirr;
-    args[1] = cpu_get_host_ticks();
-    return H_SUCCESS;
-}
-
-static target_ulong h_eoi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                          target_ulong opcode, target_ulong *args)
-{
-    CPUState *cs = CPU(cpu);
-    target_ulong xirr = args[0];
-
-    icp_eoi(spapr->icp, cs->cpu_index, xirr);
-    return H_SUCCESS;
-}
-
-static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                            target_ulong opcode, target_ulong *args)
-{
-    CPUState *cs = CPU(cpu);
-    ICPState *ss = &spapr->icp->ss[cs->cpu_index];
-
-    args[0] = ss->xirr;
-    args[1] = ss->mfrr;
-
-    return H_SUCCESS;
-}
-
-static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                          uint32_t token,
-                          uint32_t nargs, target_ulong args,
-                          uint32_t nret, target_ulong rets)
-{
-    ICSState *ics = spapr->icp->ics;
-    uint32_t nr, server, priority;
-
-    if ((nargs != 3) || (nret != 1)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    nr = rtas_ld(args, 0);
-    server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
-    priority = rtas_ld(args, 2);
-
-    if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
-        || (priority > 0xff)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    ics_write_xive(ics, nr, server, priority, priority);
-
-    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
-}
-
-static void rtas_get_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                          uint32_t token,
-                          uint32_t nargs, target_ulong args,
-                          uint32_t nret, target_ulong rets)
-{
-    ICSState *ics = spapr->icp->ics;
-    uint32_t nr;
-
-    if ((nargs != 1) || (nret != 3)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    nr = rtas_ld(args, 0);
-
-    if (!ics_valid_irq(ics, nr)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
-    rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
-    rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
-}
-
-static void rtas_int_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                         uint32_t token,
-                         uint32_t nargs, target_ulong args,
-                         uint32_t nret, target_ulong rets)
-{
-    ICSState *ics = spapr->icp->ics;
-    uint32_t nr;
-
-    if ((nargs != 1) || (nret != 1)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    nr = rtas_ld(args, 0);
-
-    if (!ics_valid_irq(ics, nr)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
-                   ics->irqs[nr - ics->offset].priority);
-
-    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
-}
-
-static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
-                        uint32_t token,
-                        uint32_t nargs, target_ulong args,
-                        uint32_t nret, target_ulong rets)
-{
-    ICSState *ics = spapr->icp->ics;
-    uint32_t nr;
-
-    if ((nargs != 1) || (nret != 1)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    nr = rtas_ld(args, 0);
-
-    if (!ics_valid_irq(ics, nr)) {
-        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
-        return;
-    }
-
-    ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
-                   ics->irqs[nr - ics->offset].saved_priority,
-                   ics->irqs[nr - ics->offset].saved_priority);
-
-    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
-}
-
 /*
  * XICS
  */
 
-static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
+void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
 {
     icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
 }
 
-static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
-                                Error **errp)
+void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp)
 {
     int i;
 
@@ -1006,77 +706,9 @@ static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
     }
 }
 
-static void xics_spapr_realize(DeviceState *dev, Error **errp)
-{
-    XICSState *icp = XICS(dev);
-    Error *error = NULL;
-    int i;
-
-    if (!icp->nr_servers) {
-        error_setg(errp, "Number of servers needs to be greater 0");
-        return;
-    }
-
-    /* Registration of global state belongs into realize */
-    spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_set_xive);
-    spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_get_xive);
-    spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_int_off);
-    spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_int_on);
-
-    spapr_register_hypercall(H_CPPR, h_cppr);
-    spapr_register_hypercall(H_IPI, h_ipi);
-    spapr_register_hypercall(H_XIRR, h_xirr);
-    spapr_register_hypercall(H_XIRR_X, h_xirr_x);
-    spapr_register_hypercall(H_EOI, h_eoi);
-    spapr_register_hypercall(H_IPOLL, h_ipoll);
-
-    object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
-    if (error) {
-        error_propagate(errp, error);
-        return;
-    }
-
-    for (i = 0; i < icp->nr_servers; i++) {
-        object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
-        if (error) {
-            error_propagate(errp, error);
-            return;
-        }
-    }
-}
-
-static void xics_spapr_initfn(Object *obj)
-{
-    XICSState *xics = XICS(obj);
-
-    xics->ics = ICS(object_new(TYPE_ICS));
-    object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
-    xics->ics->icp = xics;
-}
-
-static void xics_spapr_class_init(ObjectClass *oc, void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(oc);
-    XICSStateClass *xsc = XICS_SPAPR_CLASS(oc);
-
-    dc->realize = xics_spapr_realize;
-    xsc->set_nr_irqs = xics_set_nr_irqs;
-    xsc->set_nr_servers = xics_set_nr_servers;
-}
-
-static const TypeInfo xics_spapr_info = {
-    .name          = TYPE_XICS_SPAPR,
-    .parent        = TYPE_XICS_COMMON,
-    .instance_size = sizeof(XICSState),
-    .class_size = sizeof(XICSStateClass),
-    .class_init    = xics_spapr_class_init,
-    .instance_init = xics_spapr_initfn,
-};
-
 static void xics_register_types(void)
 {
     type_register_static(&xics_common_info);
-    type_register_static(&xics_spapr_info);
     type_register_static(&ics_info);
     type_register_static(&icp_info);
 }
hw/intc/xics_spapr.c new file +401 -0
new file mode 100644
index 0000000..820fe79
@@ -0,0 +1,401 @@
+/*
+ * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
+ *
+ * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics
+ *
+ * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "hw/hw.h"
+#include "trace.h"
+#include "qemu/timer.h"
+#include "hw/ppc/spapr.h"
+#include "hw/ppc/xics.h"
+#include "qemu/error-report.h"
+#include "qapi/visitor.h"
+
+/*
+ * Guest interfaces
+ */
+
+static target_ulong h_cppr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                           target_ulong opcode, target_ulong *args)
+{
+    CPUState *cs = CPU(cpu);
+    target_ulong cppr = args[0];
+
+    icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
+    return H_SUCCESS;
+}
+
+static target_ulong h_ipi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                          target_ulong opcode, target_ulong *args)
+{
+    target_ulong server = get_cpu_index_by_dt_id(args[0]);
+    target_ulong mfrr = args[1];
+
+    if (server >= spapr->icp->nr_servers) {
+        return H_PARAMETER;
+    }
+
+    icp_set_mfrr(spapr->icp, server, mfrr);
+    return H_SUCCESS;
+}
+
+static target_ulong h_xirr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                           target_ulong opcode, target_ulong *args)
+{
+    CPUState *cs = CPU(cpu);
+    uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
+
+    args[0] = xirr;
+    return H_SUCCESS;
+}
+
+static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                             target_ulong opcode, target_ulong *args)
+{
+    CPUState *cs = CPU(cpu);
+    ICPState *ss = &spapr->icp->ss[cs->cpu_index];
+    uint32_t xirr = icp_accept(ss);
+
+    args[0] = xirr;
+    args[1] = cpu_get_host_ticks();
+    return H_SUCCESS;
+}
+
+static target_ulong h_eoi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                          target_ulong opcode, target_ulong *args)
+{
+    CPUState *cs = CPU(cpu);
+    target_ulong xirr = args[0];
+
+    icp_eoi(spapr->icp, cs->cpu_index, xirr);
+    return H_SUCCESS;
+}
+
+static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                            target_ulong opcode, target_ulong *args)
+{
+    CPUState *cs = CPU(cpu);
+    ICPState *ss = &spapr->icp->ss[cs->cpu_index];
+
+    args[0] = ss->xirr;
+    args[1] = ss->mfrr;
+
+    return H_SUCCESS;
+}
+
+static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                          uint32_t token,
+                          uint32_t nargs, target_ulong args,
+                          uint32_t nret, target_ulong rets)
+{
+    ICSState *ics = spapr->icp->ics;
+    uint32_t nr, server, priority;
+
+    if ((nargs != 3) || (nret != 1)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    nr = rtas_ld(args, 0);
+    server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
+    priority = rtas_ld(args, 2);
+
+    if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
+        || (priority > 0xff)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    ics_write_xive(ics, nr, server, priority, priority);
+
+    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
+}
+
+static void rtas_get_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                          uint32_t token,
+                          uint32_t nargs, target_ulong args,
+                          uint32_t nret, target_ulong rets)
+{
+    ICSState *ics = spapr->icp->ics;
+    uint32_t nr;
+
+    if ((nargs != 1) || (nret != 3)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    nr = rtas_ld(args, 0);
+
+    if (!ics_valid_irq(ics, nr)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
+    rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
+    rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
+}
+
+static void rtas_int_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                         uint32_t token,
+                         uint32_t nargs, target_ulong args,
+                         uint32_t nret, target_ulong rets)
+{
+    ICSState *ics = spapr->icp->ics;
+    uint32_t nr;
+
+    if ((nargs != 1) || (nret != 1)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    nr = rtas_ld(args, 0);
+
+    if (!ics_valid_irq(ics, nr)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
+                   ics->irqs[nr - ics->offset].priority);
+
+    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
+}
+
+static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
+                        uint32_t token,
+                        uint32_t nargs, target_ulong args,
+                        uint32_t nret, target_ulong rets)
+{
+    ICSState *ics = spapr->icp->ics;
+    uint32_t nr;
+
+    if ((nargs != 1) || (nret != 1)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    nr = rtas_ld(args, 0);
+
+    if (!ics_valid_irq(ics, nr)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
+                   ics->irqs[nr - ics->offset].saved_priority,
+                   ics->irqs[nr - ics->offset].saved_priority);
+
+    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
+}
+
+static void xics_spapr_realize(DeviceState *dev, Error **errp)
+{
+    XICSState *icp = XICS(dev);
+    Error *error = NULL;
+    int i;
+
+    if (!icp->nr_servers) {
+        error_setg(errp, "Number of servers needs to be greater 0");
+        return;
+    }
+
+    /* Registration of global state belongs into realize */
+    spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_set_xive);
+    spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_get_xive);
+    spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_int_off);
+    spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_int_on);
+
+    spapr_register_hypercall(H_CPPR, h_cppr);
+    spapr_register_hypercall(H_IPI, h_ipi);
+    spapr_register_hypercall(H_XIRR, h_xirr);
+    spapr_register_hypercall(H_XIRR_X, h_xirr_x);
+    spapr_register_hypercall(H_EOI, h_eoi);
+    spapr_register_hypercall(H_IPOLL, h_ipoll);
+
+    object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
+    if (error) {
+        error_propagate(errp, error);
+        return;
+    }
+
+    for (i = 0; i < icp->nr_servers; i++) {
+        object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
+        if (error) {
+            error_propagate(errp, error);
+            return;
+        }
+    }
+}
+
+static void xics_spapr_initfn(Object *obj)
+{
+    XICSState *xics = XICS(obj);
+
+    xics->ics = ICS(object_new(TYPE_ICS));
+    object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
+    xics->ics->icp = xics;
+}
+
+static void xics_spapr_class_init(ObjectClass *oc, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+    XICSStateClass *xsc = XICS_SPAPR_CLASS(oc);
+
+    dc->realize = xics_spapr_realize;
+    xsc->set_nr_irqs = xics_set_nr_irqs;
+    xsc->set_nr_servers = xics_set_nr_servers;
+}
+
+static const TypeInfo xics_spapr_info = {
+    .name          = TYPE_XICS_SPAPR,
+    .parent        = TYPE_XICS_COMMON,
+    .instance_size = sizeof(XICSState),
+    .class_size = sizeof(XICSStateClass),
+    .class_init    = xics_spapr_class_init,
+    .instance_init = xics_spapr_initfn,
+};
+
+#define ICS_IRQ_FREE(ics, srcno)   \
+    (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
+
+static int ics_find_free_block(ICSState *ics, int num, int alignnum)
+{
+    int first, i;
+
+    for (first = 0; first < ics->nr_irqs; first += alignnum) {
+        if (num > (ics->nr_irqs - first)) {
+            return -1;
+        }
+        for (i = first; i < first + num; ++i) {
+            if (!ICS_IRQ_FREE(ics, i)) {
+                break;
+            }
+        }
+        if (i == (first + num)) {
+            return first;
+        }
+    }
+
+    return -1;
+}
+
+int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
+{
+    ICSState *ics = &icp->ics[src];
+    int irq;
+
+    if (irq_hint) {
+        assert(src == xics_find_source(icp, irq_hint));
+        if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
+            trace_xics_alloc_failed_hint(src, irq_hint);
+            return -1;
+        }
+        irq = irq_hint;
+    } else {
+        irq = ics_find_free_block(ics, 1, 1);
+        if (irq < 0) {
+            trace_xics_alloc_failed_no_left(src);
+            return -1;
+        }
+        irq += ics->offset;
+    }
+
+    ics_set_irq_type(ics, irq - ics->offset, lsi);
+    trace_xics_alloc(src, irq);
+
+    return irq;
+}
+
+/*
+ * Allocate block of consecutive IRQs, and return the number of the first IRQ in the block.
+ * If align==true, aligns the first IRQ number to num.
+ */
+int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align)
+{
+    int i, first = -1;
+    ICSState *ics = &icp->ics[src];
+
+    assert(src == 0);
+    /*
+     * MSIMesage::data is used for storing VIRQ so
+     * it has to be aligned to num to support multiple
+     * MSI vectors. MSI-X is not affected by this.
+     * The hint is used for the first IRQ, the rest should
+     * be allocated continuously.
+     */
+    if (align) {
+        assert((num == 1) || (num == 2) || (num == 4) ||
+               (num == 8) || (num == 16) || (num == 32));
+        first = ics_find_free_block(ics, num, num);
+    } else {
+        first = ics_find_free_block(ics, num, 1);
+    }
+
+    if (first >= 0) {
+        for (i = first; i < first + num; ++i) {
+            ics_set_irq_type(ics, i, lsi);
+        }
+    }
+    first += ics->offset;
+
+    trace_xics_alloc_block(src, first, num, lsi, align);
+
+    return first;
+}
+
+static void ics_free(ICSState *ics, int srcno, int num)
+{
+    int i;
+
+    for (i = srcno; i < srcno + num; ++i) {
+        if (ICS_IRQ_FREE(ics, i)) {
+            trace_xics_ics_free_warn(ics - ics->icp->ics, i + ics->offset);
+        }
+        memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
+    }
+}
+
+void xics_spapr_free(XICSState *icp, int irq, int num)
+{
+    int src = xics_find_source(icp, irq);
+
+    if (src >= 0) {
+        ICSState *ics = &icp->ics[src];
+
+        /* FIXME: implement multiple sources */
+        assert(src == 0);
+
+        trace_xics_ics_free(ics - icp->ics, irq, num);
+        ics_free(ics, irq - ics->offset, num);
+    }
+}
+
+static void xics_spapr_register_types(void)
+{
+    type_register_static(&xics_spapr_info);
+}
+
+type_init(xics_spapr_register_types)
include/hw/ppc/xics.h +23 -0
index 8d33dfa..8efff94 100644
@@ -141,6 +141,12 @@ struct ICSState {
     XICSState *icp;
 };
 
+static inline bool ics_valid_irq(ICSState *ics, uint32_t nr)
+{
+    return (nr >= ics->offset)
+        && (nr < (ics->offset + ics->nr_irqs));
+}
+
 struct ICSIRQState {
     uint32_t server;
     uint8_t priority;
@@ -168,4 +174,21 @@ void xics_spapr_free(XICSState *icp, int irq, int num);
 
 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu);
 
+/* Internal XICS interfaces */
+int get_cpu_index_by_dt_id(int cpu_dt_id);
+
+void icp_set_cppr(XICSState *icp, int server, uint8_t cppr);
+void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr);
+uint32_t icp_accept(ICPState *ss);
+void icp_eoi(XICSState *icp, int server, uint32_t xirr);
+
+void ics_write_xive(ICSState *ics, int nr, int server,
+                    uint8_t priority, uint8_t saved_priority);
+
+void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
+
+void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp);
+void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
+int xics_find_source(XICSState *icp, int irq);
+
 #endif /* __XICS_H__ */
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
#81 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 27/77] ppc/pnv: Add XSCOM infrastructure

In reply to David Gibson
Quoted 5 lines · David Gibson

Hmm.. I'm thinking it probably makes sense to unify the representation of "chip" and "xscom" since there's a 1:1 correspondance.  To be QOMishly correct, I think the right way would be a SysBusDevice for each chip, implementing the xscom MMIOs, then an array of link properties under the Machine object to find the right scoms by chip id.

But there is more than xscom's under each chip ... there's PHBs, LPC controllers (though those are currently under XSCOM) and possibly other completely unrelated things...

To be honest I've never been a big fan of that whole QOM business and am a bit confused as to how that would all work together.

Cheers,
Ben.

#82 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 27/77] ppc/pnv: Add XSCOM infrastructure

In reply to David Gibson
Quoted 7 lines · David Gibson

hw/ppc/pnv_xscom.c line 65

Quoted 5 lines
hw/ppc/pnv_xscom.c new file +4 -0
+static uint32_t xscom_to_pcb_addr(uint64_t addr)
+{
+        addr &= (XSCOM_SIZE - 1);
+        return ((addr >> 4) & ~0xfull) | ((addr >> 3) & 0xf);

Wow, that's a pretty weird address transform.

Indeed :-) That's how it is in HW. It's also a bit different between chip generations.

Quoted 10 lines
Quoted 10 lines
hw/ppc/pnv_xscom.c new file +10 -0
+}
+
+static void xscom_complete(uint64_t hmer_bits)
+{
+    CPUState *cs = current_cpu;
+    PowerPCCPU *cpu = POWERPC_CPU(cs);
+    CPUPPCState *env = &cpu->env;
+
+    cpu_synchronize_state(cs);
+    env->spr[SPR_HMER] |= hmer_bits;
Quoted 8 lines
Quoted 3 lines

+
+    /* XXX Need a CPU helper to set HMER, also handle gneeration +     * of HMIs

Not sure what you're referring to here.  Nothing more should be needed to set the HMER - because you've called cpu_synchronize_state() it will be marked dirty and flushed back to KVM before re-entry.

No it's not that. Setting HMER can potentially generate an HMI interrupt (if enabled in HMEER). We never use the interrupt corresponding to XSCOMs in FW though, so that's why I haven't bothered yet.

Quoted 17 lines
Quoted 17 lines

+     */

hw/ppc/pnv_xscom.c new file +16 -0
+}
+
+static XScomDevice *xscom_find_target(XScomState *s, uint32_t pcb_addr, uint32_t *range)
+{
+    BusChild *bc;
+
+    QTAILQ_FOREACH(bc, &s->bus->bus.children, sibling) {
+        DeviceState *qd = bc->child;
+        XScomDevice *xd = XSCOM_DEVICE(qd);
+        unsigned int i;
+
+        for (i = 0; i < MAX_XSCOM_RANGES; i++) {
+            if (xd->ranges[i].addr <= pcb_addr &&
+                (xd->ranges[i].addr + xd->ranges[i].size) > pcb_addr) {
+                *range = i;
+                return xd;
Quoted 8 lines
Quoted 3 lines

+            }
+        }
+    }

I'm wondering if it makes sense to construct a custom AddressSpace and
use the existing address space lookup logic from exec.c and memory.c rather than implementing your own.

Maybe but we'd then have to shift everything by 3 bits, which means the "XSCOM addresses" would no longer match the doc unless we use some kind of macro to do the shifting.

Quoted 46 lines
Quoted 46 lines

+    return NULL;

hw/ppc/pnv_xscom.c new file +45 -0
+}
+
+static bool xscom_dispatch_read(XScomState *s, uint32_t pcb_addr, uint64_t *out_val)
+{
+    uint32_t range, offset;
+    struct XScomDevice *xd = xscom_find_target(s, pcb_addr, &range);
+    XScomDeviceClass *xc;
+
+    if (!xd) {
+        return false;
+    }
+    xc = XSCOM_DEVICE_GET_CLASS(xd);
+    if (!xc->read) {
+        return false;
+    }
+    offset = pcb_addr - xd->ranges[range].addr;
+    return xc->read(xd, range, offset, out_val);
+}
+
+static bool xscom_dispatch_write(XScomState *s, uint32_t pcb_addr, uint64_t val)
+{
+    uint32_t range, offset;
+    struct XScomDevice *xd = xscom_find_target(s, pcb_addr, &range);
+    XScomDeviceClass *xc;
+
+    if (!xd) {
+        return false;
+    }
+    xc = XSCOM_DEVICE_GET_CLASS(xd);
+    if (!xc->write) {
+        return false;
+    }
+    offset = pcb_addr - xd->ranges[range].addr;
+    return xc->write(xd, range, offset, val);
+}
+
+static uint64_t xscom_read(void *opaque, hwaddr addr, unsigned width)
+{
+    XScomState *s = opaque;
+    uint32_t pcba = xscom_to_pcb_addr(addr);
+    uint64_t val;
+
+    assert(width == 8);
+
+#ifdef TRACE_SCOMS
Quoted 8 lines
Quoted 2 lines

+    printf("XSCOM_READ(0x%x:0x%x)\n", s->chip_id, pcba); +#endif

You should be using the built in trace infrastructure here - it's really not that much of a pain.  Put trace_xscom_read(s->chip_id, pcba)
here, put a suitable format in trace-events, and ./configure --enable-trace-backends=stderr

I'll investigate this.

Quoted 91 lines
Quoted 91 lines
hw/ppc/pnv_xscom.c new file +91 -0
+
+    /* Handle some SCOMs here before dispatch */
+    switch(pcba) {
+    case 0xf000f:
+        val = 0x221EF04980000000;
+        break;
+    case 0x1010c00:     /* PIBAM FIR */
+    case 0x1010c03:     /* PIBAM FIR MASK */
+    case 0x2020007:     /* ADU stuff */
+    case 0x2020009:     /* ADU stuff */
+    case 0x202000f:     /* ADU stuff */
+        val = 0;
+        break;
+    case 0x2013f00:     /* PBA stuff */
+    case 0x2013f01:     /* PBA stuff */
+    case 0x2013f02:     /* PBA stuff */
+    case 0x2013f03:     /* PBA stuff */
+    case 0x2013f04:     /* PBA stuff */
+    case 0x2013f05:     /* PBA stuff */
+    case 0x2013f06:     /* PBA stuff */
+    case 0x2013f07:     /* PBA stuff */
+        val = 0;
+        break;
+    default:
+        if (!xscom_dispatch_read(s, pcba, &val)) {
+            xscom_complete(HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
+            return 0;
+        }
+    }
+
+    xscom_complete(HMER_XSCOM_DONE);
+    return val;
+}
+
+static void xscom_write(void *opaque, hwaddr addr, uint64_t val,
+                        unsigned width)
+{
+    XScomState *s = opaque;
+    uint32_t pcba = xscom_to_pcb_addr(addr);
+
+    assert(width == 8);
+
+#ifdef TRACE_SCOMS
+    printf("XSCOM_WRITE(0x%x:0x%x, 0x%016llx)\n",
+           s->chip_id, pcba, (unsigned long long)val);
+#endif
+    /* Handle some SCOMs here before dispatch */
+    switch(pcba) {
+        /* We ignore writes to these */
+    case 0xf000f:       /* chip id is RO */
+    case 0x1010c00:     /* PIBAM FIR */
+    case 0x1010c01:     /* PIBAM FIR */
+    case 0x1010c02:     /* PIBAM FIR */
+    case 0x1010c03:     /* PIBAM FIR MASK */
+    case 0x1010c04:     /* PIBAM FIR MASK */
+    case 0x1010c05:     /* PIBAM FIR MASK */
+    case 0x2020007:     /* ADU stuff */
+    case 0x2020009:     /* ADU stuff */
+    case 0x202000f:     /* ADU stuff */
+        break;
+    default:
+        if (!xscom_dispatch_write(s, pcba, val)) {
+            xscom_complete(HMER_XSCOM_FAIL | HMER_XSCOM_DONE);
+            return;
+        }
+    }
+
+    xscom_complete(HMER_XSCOM_DONE);
+}
+
+static const MemoryRegionOps xscom_ops = {
+    .read = xscom_read,
+    .write = xscom_write,
+    .valid.min_access_size = 8,
+    .valid.max_access_size = 8,
+    .impl.min_access_size = 8,
+    .impl.max_access_size = 8,
+    .endianness = DEVICE_BIG_ENDIAN,
+};
+
+static int xscom_init(SysBusDevice *dev)
+{
+    XScomState *s = XSCOM(dev);
+
+    s->chip_id = -1;
+    return 0;
+}
+
+static void xscom_realize(DeviceState *dev, Error **errp)
+{
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Quoted 8 lines
Quoted 4 lines

+    XScomState *s = XSCOM(dev);
+    char *name;
+
+    assert(s->chip_id >= 0);

So, this assert could be tripped if the user explicitly instantiated an xscom device which they probably shouldn't do, but could.  So, it probably makes sense to use error_setg() here instead of assert().

No idea what error_setg() is, I'll look into it :)

Quoted 53 lines
Quoted 2 lines

+    name = g_strdup_printf("xscom-%x", s->chip_id);
+    memory_region_init_io(&s->mem, OBJECT(s), &xscom_ops, s, name,

XSCOM_SIZE);

Quoted 50 lines

+    sysbus_init_mmio(sbd, &s->mem);
+    sysbus_mmio_map(sbd, 0, XSCOM_BASE(s->chip_id));

hw/ppc/pnv_xscom.c new file +48 -0
+}
+
+static Property xscom_properties[] = {
+        DEFINE_PROP_INT32("chip_id", XScomState, chip_id, 0),
+        DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xscom_class_init(ObjectClass *klass, void *data)
+{
+    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = xscom_properties;
+    dc->realize = xscom_realize;
+    k->init = xscom_init;
+}
+
+static const TypeInfo xscom_info = {
+    .name          = TYPE_XSCOM,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XScomState),
+    .class_init    = xscom_class_init,
+};
+
+static void xscom_bus_class_init(ObjectClass *klass, void *data)
+{
+}
+
+static const TypeInfo xscom_bus_info = {
+    .name = TYPE_XSCOM_BUS,
+    .parent = TYPE_BUS,
+    .class_init = xscom_bus_class_init,
+    .instance_size = sizeof(XScomBus),
+};
+
+void xscom_create(PnvChip *chip)
+{
+    DeviceState *dev;
+    XScomState *xdev;
+    BusState *qbus;
+    XScomBus *xb;
+
+    dev = qdev_create(NULL, TYPE_XSCOM);
+    qdev_prop_set_uint32(dev, "chip_id", chip->chip_id);
+    qdev_init_nofail(dev);
+
+    /* Create bus on bridge device */
+    qbus = qbus_create(TYPE_XSCOM_BUS, dev, "xscom");
Quoted 8 lines
Quoted 5 lines

+    xb = DO_UPCAST(XScomBus, bus, qbus);
+    xb->chip_id = chip->chip_id;
+    xdev = XSCOM(dev);
+    xdev->bus = xb;
+    chip->xscom = xb;

I believe the qbus_create() is usually invoked by the bridge's init function, rather than externally.

Init or realize ?

Cheers,
Ben.

#167 lore raw
In reply to Alexey Kardashevskiy
Quoted 48 lines · Alexey Kardashevskiy
Quoted 26 lines · Benjamin Herrenschmidt
Quoted 8 lines · Alexey Kardashevskiy

p/qemu-powernv/ppc64-softmmu/qemu-system-ppc64 -m 2048 -machine powernv \
-nographic -vga none -initrd t/le.cpio -kernel t/vml420le -bios \
skiboot.lid -smp 1,threads=1

just hangs at:

Quoted 16 lines

[1491287872,5] INIT: Waiting for kernel...
[1493257423,5] Assuming kernel at 0x20000000 [1494710040,5] INIT: Kernel loaded, size: 0 bytes (0 = unknown preload)
[1497506414,5] INIT: 64-bit LE kernel discovered [1500827972,5] INIT: 64-bit kernel entry at 0x20010000 [1505594383,3] OCC: No HOMER detected, assuming no pstates [1507983930,3] ELOG: Error getting buffer to log error [1556792870,5] Free space in HEAP memory regions:
[1559724738,5] Region ibm,firmware-heap free: 12778984 [1561377946,5] Region ibm,firmware-allocs-memory@0000000000000000
free: 376992
[1563789914,5] Total free: 13155976
[1565066925,5] INIT: Starting kernel at 0x20010000, fdt at 0x30350610 (size
0x2ce4)

Quoted 3 lines

Hrm, works for me, I've been testing various LE kernels including a full ubuntu distro in there, we need to debug that further. Does that same kernel actually work on real HW ?

Ok, as we figured out, CONFIG_PPC_EARLY_DEBUG is responsible for this as it does hypercalls in the very beginning.

Quoted 9 lines
Quoted 7 lines

If I try LE disk image (ubuntu 14), it just crashes:

p/qemu-powernv/ppc64-softmmu/qemu-system-ppc64 -m 2048 -machine powernv \

(quoted diff) +0 -2
-nographic -vga none img/u14_32GB_cuda7.qcow2 -bios skiboot.lid \
-smp 1,threads=1

qemu: hardware error: qemu: could not load kernel'(null)'

Right, we don't load kernels from disk, you need to pass a -kernel that

Worth mentioning as well ;)

Quoted 7 lines
Quoted 5 lines

typically is the openpower bootloader (Linux + petitboot). My plan is to make the pnv platform automatically extract these things from a ROM image of an openpower eval board (aka palmetto) which you can build from github. It's a bit too big to include as a binary in qemu however (about 16M).

git submodule?

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

Alex

#6 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 02/77] ppc: Use split I/D mmu modes to avoid flushes on interrupts

In reply to Benjamin Herrenschmidt
Quoted 6 lines · Benjamin Herrenschmidt

We rework the way the MMU indices are calculated, providing separate indices for I and D side based on MSR:IR and MSR:DR respectively, and thus no longer need to flush the TLB on context changes. This also adds correct support for HV as a separate address space.

Paolo had a patch set poking at the same places a while back to speed up the ppc target by almost 10%.

Paolo, what happened to those patches? Would you prefer to rebase them on top of the HV bits or have Ben look into them while he's at it anyway? ;)

Alex

Quoted 203 lines
---
 target-ppc/cpu.h         | 11 +++++++---
 target-ppc/excp_helper.c | 11 ----------
 target-ppc/helper_regs.h | 54 +++++++++++++++++++++++++++++++++++++++++-------
 target-ppc/machine.c     |  4 +++-
 target-ppc/translate.c   |  7 ++++---
 5 files changed, 62 insertions(+), 25 deletions(-)
target-ppc/cpu.h +8 -3
index 9ef0859..aaa7117 100644
@@ -462,6 +462,8 @@ struct ppc_slb_t {
 #define MSR_EP   6  /* Exception prefix on 601                               */
 #define MSR_IR   5  /* Instruction relocate                                  */
 #define MSR_DR   4  /* Data relocate                                         */
+#define MSR_IS   5  /* Instruction address space (BookE)                     */
+#define MSR_DS   4  /* Data address space (BookE)                            */
 #define MSR_PE   3  /* Protection enable on 403                              */
 #define MSR_PX   2  /* Protection exclusive on 403                  x        */
 #define MSR_PMM  2  /* Performance monitor mark on POWER            x        */
@@ -505,6 +507,8 @@ struct ppc_slb_t {
 #define msr_ep   ((env->msr >> MSR_EP)   & 1)
 #define msr_ir   ((env->msr >> MSR_IR)   & 1)
 #define msr_dr   ((env->msr >> MSR_DR)   & 1)
+#define msr_is   ((env->msr >> MSR_IS)   & 1)
+#define msr_ds   ((env->msr >> MSR_DS)   & 1)
 #define msr_pe   ((env->msr >> MSR_PE)   & 1)
 #define msr_px   ((env->msr >> MSR_PX)   & 1)
 #define msr_pmm  ((env->msr >> MSR_PMM)  & 1)
@@ -944,7 +948,7 @@ struct ppc_segment_page_sizes {
 
 /*****************************************************************************/
 /* The whole PowerPC CPU context */
-#define NB_MMU_MODES 3
+#define NB_MMU_MODES    8
 
 #define PPC_CPU_OPCODES_LEN          0x40
 #define PPC_CPU_INDIRECT_OPCODES_LEN 0x20
@@ -1108,7 +1112,8 @@ struct CPUPPCState {
     /* Those resources are used only in QEMU core */
     target_ulong hflags;      /* hflags is a MSR & HFLAGS_MASK         */
     target_ulong hflags_nmsr; /* specific hflags, not coming from MSR */
-    int mmu_idx;         /* precomputed MMU index to speed up mem accesses */
+    int immu_idx;         /* precomputed MMU index to speed up insn access */
+    int dmmu_idx;         /* precomputed MMU index to speed up data accesses */
 
     /* Power management */
     int (*check_pow)(CPUPPCState *env);
@@ -1249,7 +1254,7 @@ int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val);
 #define MMU_USER_IDX 0
 static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 {
-    return env->mmu_idx;
+    return ifetch ? env->immu_idx : env->dmmu_idx;
 }
 
 #include "exec/cpu-all.h"
target-ppc/excp_helper.c +0 -11
index 4250106..3e39098 100644
@@ -623,9 +623,6 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)
 
     if (env->spr[SPR_LPCR] & LPCR_AIL) {
         new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
-    } else if (msr & ((1 << MSR_IR) | (1 << MSR_DR))) {
-        /* If we disactivated any translation, flush TLBs */
-        tlb_flush(cs, 1);
     }
 
 #ifdef TARGET_PPC64
@@ -674,14 +671,6 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)
     /* Reset exception state */
     cs->exception_index = POWERPC_EXCP_NONE;
     env->error_code = 0;
-
-    if ((env->mmu_model == POWERPC_MMU_BOOKE) ||
-        (env->mmu_model == POWERPC_MMU_BOOKE206)) {
-        /* XXX: The BookE changes address space when switching modes,
-                we should probably implement that as different MMU indexes,
-                but for the moment we do it the slow way and flush all.  */
-        tlb_flush(cs, 1);
-    }
 }
 
 void ppc_cpu_do_interrupt(CPUState *cs)
target-ppc/helper_regs.h +47 -7
index 271fddf..f7edd5b 100644
@@ -41,11 +41,50 @@ static inline void hreg_swap_gpr_tgpr(CPUPPCState *env)
 
 static inline void hreg_compute_mem_idx(CPUPPCState *env)
 {
-    /* Precompute MMU index */
-    if (msr_pr == 0 && msr_hv != 0) {
-        env->mmu_idx = 2;
+    /* This is our encoding for server processors
+     *
+     *   0 = Guest User space virtual mode
+     *   1 = Guest Kernel space virtual mode
+     *   2 = Guest Kernel space real mode
+     *   3 = HV User space virtual mode
+     *   4 = HV Kernel space virtual mode
+     *   5 = HV Kernel space real mode
+     *
+     * The combination PR=1 IR&DR=0 is invalid, we will treat
+     * it as IR=DR=1
+     *
+     * For BookE, we need 8 MMU modes as follow:
+     *
+     *  0 = AS 0 HV User space
+     *  1 = AS 0 HV Kernel space
+     *  2 = AS 1 HV User space
+     *  3 = AS 1 HV Kernel space
+     *  4 = AS 0 Guest User space
+     *  5 = AS 0 Guest Kernel space
+     *  6 = AS 1 Guest User space
+     *  7 = AS 1 Guest Kernel space
+     */
+    if (env->mmu_model & POWERPC_MMU_BOOKE) {
+        env->immu_idx = env->dmmu_idx = msr_pr ? 0 : 1;
+        env->immu_idx += msr_is ? 2 : 0;
+        env->dmmu_idx += msr_ds ? 2 : 0;
+        env->immu_idx += msr_gs ? 4 : 0;
+        env->dmmu_idx += msr_gs ? 4 : 0;
     } else {
-        env->mmu_idx = 1 - msr_pr;
+        /* First calucalte a base value independent of HV */
+        if (msr_pr != 0) {
+            /* User space, ignore IR and DR */
+            env->immu_idx = env->dmmu_idx = 0;
+        } else {
+            /* Kernel, setup a base I/D value */
+            env->immu_idx = msr_ir ? 1 : 2;
+            env->dmmu_idx = msr_dr ? 1 : 2;
+        }
+        /* Then offset it for HV */
+        if (msr_hv) {
+            env->immu_idx += 3;
+            env->dmmu_idx += 3;
+        }
     }
 }
 
@@ -82,9 +121,10 @@ static inline int hreg_store_msr(CPUPPCState *env, target_ulong value,
     }
     if (((value >> MSR_IR) & 1) != msr_ir ||
         ((value >> MSR_DR) & 1) != msr_dr) {
-        /* Flush all tlb when changing translation mode */
-        tlb_flush(cs, 1);
-        excp = POWERPC_EXCP_NONE;
+        cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
+    }
+    if ((env->mmu_model & POWERPC_MMU_BOOKE) &&
+        ((value >> MSR_GS) & 1) != msr_gs) {
         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
     }
     if (unlikely((env->flags & POWERPC_FLAG_TGPR) &&
target-ppc/machine.c +3 -1
index f4ac761..b969492 100644
@@ -90,9 +90,11 @@ static int cpu_load_old(QEMUFile *f, void *opaque, int version_id)
     qemu_get_betls(f, &env->nip);
     qemu_get_betls(f, &env->hflags);
     qemu_get_betls(f, &env->hflags_nmsr);
-    qemu_get_sbe32s(f, &env->mmu_idx);
     qemu_get_sbe32(f); /* Discard unused power_mode */
 
+    /* Ignore saved mmu_idx, recompute */
+    hreg_compute_mem_idx(env);
+
     return 0;
 }
 
target-ppc/translate.c +4 -3
index 308ad68..6d9f252 100644
@@ -11220,8 +11220,9 @@ void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
                 env->nip, env->lr, env->ctr, cpu_read_xer(env),
                 cs->cpu_index);
     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
-                TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
-                env->hflags, env->mmu_idx);
+                TARGET_FMT_lx " iidx %d didx %d\n",
+                env->msr, env->spr[SPR_HID0],
+                env->hflags, env->immu_idx, env->dmmu_idx);
 #if !defined(NO_TIMER_DUMP)
     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
 #if !defined(CONFIG_USER_ONLY)
@@ -11426,7 +11427,7 @@ void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
     ctx.spr_cb = env->spr_cb;
     ctx.pr = msr_pr;
     ctx.hv = !msr_pr && msr_hv;
-    ctx.mem_idx = env->mmu_idx;
+    ctx.mem_idx = env->dmmu_idx;
     ctx.insns_flags = env->insns_flags;
     ctx.insns_flags2 = env->insns_flags2;
     ctx.access_type = -1;
#7 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 02/77] ppc: Use split I/D mmu modes to avoid flushes on interrupts

Quoted 5 lines · Alexander Graf
Quoted 5 lines

We rework the way the MMU indices are calculated, providing separate indices for I and D side based on MSR:IR and MSR:DR respectively, and thus no longer need to flush the TLB on context changes. This also adds correct support for HV as a separate address space.

Quoted 7 lines
Quoted 1 line

Paolo had a patch set poking at the same places a while back to speed up the ppc target by almost 10%.

Paolo, what happened to those patches? Would you prefer to rebase them on top of the HV bits or have Ben look into them while he's at it anyway? ;)

Ben decided to do it this way, which avoids a proliferation of MMU modes. The net effect of his patches is the same if not better.

Paolo

#168 lore raw
In reply to Alexander Graf
Quoted 4 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

It is a flash image, we could use an Open Power machine flash image "as-is" provided we taught qemu to extract skiboot (aka OPAL) from it.

Cheers,
Ben.

#169 lore raw
Quoted 4 lines

Am 28.11.2015 um 08:59 schrieb Benjamin Herrenschmidt <[email protected]>:

Quoted 8 lines
Quoted 5 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

It is a flash image, we could use an Open Power machine flash image "as-is" provided we taught qemu to extract skiboot (aka OPAL) from it.

That's probably the best way. If it's memory mapped, you might not even have to extract anything - just jump to its location ;).

The -kernel syntax is useful to have, but should really be something "on top" of the normal system boot rather than its primary boot interface.

Alex

#170 lore raw
Quoted 12 lines · Alexander Graf
Quoted 12 lines

Am 28.11.2015 um 08:59 schrieb Benjamin Herrenschmidt <benh@kernel. crashing.org>:

Quoted 7 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it
in using -pflash and thus model things even more closely and allow users
to just take the ROM image as is.

Quoted 6 lines
Quoted 3 lines

It is a flash image, we could use an Open Power machine flash image "as-is"
provided we taught qemu to extract skiboot (aka OPAL) from it.

That's probably the best way. If it's memory mapped, you might not even have to extract anything - just jump to its location ;).

It's not memory mapped sadly (well not on P8...)

The way a real P8 boots is first a little internal uC executes code from a i2c seeprom to initialize a core and some cache. It then copies a payload from flash into that cache. That payload is HostBoot, which is a huge thing that initializes a pile of stuff such as the internal busses, SMP links, links to memory buffers, trains memory etc... it then constructs an embryo device-tree and loads & run OPAL (aka skiboot).

The model I chose for powernv is to simulate a boot at the hostboot -> skiboot transition point.

Quoted 3 lines

The -kernel syntax is useful to have, but should really be something "on top" of the normal system boot rather than its primary boot interface.

I know, I'm just not keen on committing a 16M "BIOS" to qemu tree ;-)

In any case, this is easy to change / fix. There's a lot more work on the rest of the series first.

Cheers,
Ben.

#171 lore raw
In reply to Benjamin Herrenschmidt
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

It is a flash image, we could use an Open Power machine flash image "as-is" provided we taught qemu to extract skiboot (aka OPAL) from it.

Couldn't we add an offset argument to load_image_targphys() or make that an extra routine ? If so, we could then load directly from an openpower pnor file.

I gave it a quick (and dirty) try and a powernv guest runs fine up to petitboot with just :

qemu-system-ppc64 -m 2G -M powernv -bios ~/work/open-power/images/palmetto.pnor -nographic -nodefaults -serial stdio

The pnor file is compiled from github. The patch is below (without the dirty cut and paste I did in loader.c). The offset for the PAYLOAD and BOOTKERNEL partitions are hard coded but I guess we don't need to read the flash partition table in qemu, not yet.

Cheers,

C.

hw/ppc/pnv.c +7 -3
Index: qemu-powernv.git/hw/ppc/pnv.c
===================================================================
@@ -69,7 +69,7 @@
 
 #define FDT_ADDR                0x01000000
 #define FDT_MAX_SIZE            0x00100000
-#define FW_MAX_SIZE             0x00400000
+#define FW_MAX_SIZE             0x04000000
 #define FW_FILE_NAME            "skiboot.lid"
 #define KERNEL_FILE_NAME        "skiroot.lid"
 #define KERNEL_LOAD_ADDR        0x20000000
@@ -902,7 +902,9 @@ static void ppc_powernv_init(MachineStat
 {
     ram_addr_t ram_size = machine->ram_size;
     const char *cpu_model = machine->cpu_model;
+#if 0
     const char *kernel_filename = machine->kernel_filename;
+#endif
     const char *initrd_filename = machine->initrd_filename;
     uint32_t initrd_base = 0;
     long initrd_size = 0;
@@ -998,19 +1000,20 @@ static void ppc_powernv_init(MachineStat
         bios_name = FW_FILE_NAME;
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
-    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
+    fw_size = load_image_targphys_offset(filename, 0, FW_MAX_SIZE, 0x961000);
     if (fw_size < 0) {
         hw_error("qemu: could not load OPAL '%s'\n", filename);
         exit(1);
     }
+#if 0
     g_free(filename);
 
     if (kernel_filename == NULL) {
         kernel_filename = KERNEL_FILE_NAME;
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, kernel_filename);
-    fw_size = load_image_targphys(filename, 0x20000000, 0x2000000);
+#endif
+    fw_size = load_image_targphys_offset(filename, 0x20000000, 0x2000000, 0xa61000);
     if (fw_size < 0) {
         hw_error("qemu: could not load kernel'%s'\n", filename);
         exit(1);
#172 lore raw
Quoted 4 lines · Cédric Le Goater

The pnor file is compiled from github. The patch is below (without the dirty cut and paste I did in loader.c). The offset for the PAYLOAD and BOOTKERNEL partitions are hard coded but I guess we don't need to read the flash partition table in qemu, not yet.

In practice we should read the partition tables, I don't like hard coded offsets... But we should probably create a proper "flash driver" that does a bunch of this, and also adds the BMC style flash interface so OPAL can write to nvram.

Cheers,
Ben.

#173 lore raw
Quoted 10 lines · Benjamin Herrenschmidt
Quoted 4 lines · Cédric Le Goater

The pnor file is compiled from github. The patch is below (without the dirty cut and paste I did in loader.c). The offset for the PAYLOAD and BOOTKERNEL partitions are hard coded but I guess we don't need to read the flash partition table in qemu, not yet.

In practice we should read the partition tables, I don't like hard coded offsets... But we should probably create a proper "flash driver" that does a bunch of this, and also adds the BMC style flash interface so OPAL can write to nvram.

yes that would be better but I don't measure what it takes to implement the LPC/AHB bridge to access the PNOR.

C.

#174 lore raw
Quoted 7 lines · Cédric Le Goater
Quoted 4 lines

In practice we should read the partition tables, I don't like hard coded offsets... But we should probably create a proper "flash driver" that does a bunch of this, and also adds the BMC style flash interface so OPAL can write to nvram.

yes that would be better but I don't measure what it takes to implement the LPC/AHB bridge to access the PNOR.

Yeah that's probably a little bit of work, but then there's also work being done to protect the flash using IPMI commands to do the flashing, so that could be an alternate option. I like emulating more of the BMC though, including the flash controller, as it would improve our testing coverage.

Cheers,
Ben.

#97 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 35/77] ppc/xics: Move xics_set_nr_irqs() to xics_spapr.c and xics_kvm.c

In reply to Benjamin Herrenschmidt
Quoted 50 lines · Benjamin Herrenschmidt

It will not be used by the native implementation. This allows us to also remove the include of spapr.h from the common code

---
 hw/intc/xics.c        | 12 ------------
 hw/intc/xics_kvm.c    | 13 ++++++++++++-
 hw/intc/xics_spapr.c  | 13 ++++++++++++-
 include/hw/ppc/xics.h |  1 -
 4 files changed, 24 insertions(+), 15 deletions(-)
hw/intc/xics.c +0 -12
index c4ac057..0c355f4 100644
@@ -28,7 +28,6 @@
 #include "hw/hw.h"
 #include "trace.h"
 #include "qemu/timer.h"
-#include "hw/ppc/spapr.h"
 #include "hw/ppc/xics.h"
 #include "qemu/error-report.h"
 #include "qapi/visitor.h"
@@ -694,17 +693,6 @@ void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
  * XICS
  */
 
-void xics_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)
-{
-    ICSState *ics = QLIST_FIRST(&xics->ics);
-
-    /* This needs to be deprecated ... */
-    xics->nr_irqs = nr_irqs;
-    if (ics) {
-        ics->nr_irqs = nr_irqs;
-    }
-}
-
 void xics_set_nr_servers(XICSState *xics, uint32_t nr_servers, Error **errp)
 {
     int i;
hw/intc/xics_kvm.c +1 -0
index a478d25..6fbc715 100644
@@ -468,6 +468,17 @@ fail:
     kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
 }
 
+static void xics_kvm_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)

hw/intc/xics_kvm.c fail: · line 481

Quoted 10 lines
hw/intc/xics_kvm.c +10 -0
+{
+    ICSState *ics = QLIST_FIRST(&xics->ics);
+
+    /* This needs to be deprecated ... */
+    xics->nr_irqs = nr_irqs;
+    if (ics) {
+        ics->nr_irqs = nr_irqs;
+    }
+}
+

Looks like you have a bit of unnecessary churn here, since you unified xics_kvm_set_nr_irqs() with xics_set_nr_irqs() earlier in the series only to split them again here.

Quoted 54 lines
hw/intc/xics_kvm.c +1 -1
 static void xics_kvm_initfn(Object *obj)
 {
     XICSState *xics = XICS_COMMON(obj);
@@ -488,7 +499,7 @@ static void xics_kvm_class_init(ObjectClass *oc, void *data)
 
     dc->realize = xics_kvm_realize;
     xsc->cpu_setup = xics_kvm_cpu_setup;
-    xsc->set_nr_irqs = xics_set_nr_irqs;
+    xsc->set_nr_irqs = xics_kvm_set_nr_irqs;
     xsc->set_nr_servers = xics_kvm_set_nr_servers;
 }
 
hw/intc/xics_spapr.c +12 -1
index d75fcf0..3092f8d 100644
@@ -267,13 +267,24 @@ static void xics_spapr_initfn(Object *obj)
     QLIST_INSERT_HEAD(&xics->ics, ics, list);
 }
 
+static void xics_spapr_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)
+{
+    ICSState *ics = QLIST_FIRST(&xics->ics);
+
+    /* This needs to be deprecated ... */
+    xics->nr_irqs = nr_irqs;
+    if (ics) {
+        ics->nr_irqs = nr_irqs;
+    }
+}
+
 static void xics_spapr_class_init(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
     XICSStateClass *xsc = XICS_SPAPR_CLASS(oc);
 
     dc->realize = xics_spapr_realize;
-    xsc->set_nr_irqs = xics_set_nr_irqs;
+    xsc->set_nr_irqs = xics_spapr_set_nr_irqs;
     xsc->set_nr_servers = xics_set_nr_servers;
 }
 
include/hw/ppc/xics.h +0 -1
index 51a63cb..9e5b751 100644
@@ -189,7 +189,6 @@ void ics_write_xive(ICSState *ics, int nr, int server,
 
 void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
 
-void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp);
 void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
 ICSState *xics_find_source(XICSState *icp, int irq);
 
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
#99 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 36/77] ppc/xics: Use a helper to add a new ICS

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt

Hmm.. does it make sense for the helper to do the object_new() and add_child() as well?

Quoted 48 lines
---
 hw/intc/xics.c        | 6 ++++++
 hw/intc/xics_spapr.c  | 3 +--
 include/hw/ppc/xics.h | 1 +
 3 files changed, 8 insertions(+), 2 deletions(-)
hw/intc/xics.c +6 -0
index 0c355f4..3cd696f 100644
@@ -90,6 +90,12 @@ static void xics_common_reset(DeviceState *d)
     }
 }
 
+void xics_add_ics(XICSState *xics, ICSState *ics)
+{
+    ics->xics = xics;
+    QLIST_INSERT_HEAD(&xics->ics, ics, list);
+}
+
 static void xics_prop_get_nr_irqs(Object *obj, Visitor *v,
                                   void *opaque, const char *name, Error **errp)
 {
hw/intc/xics_spapr.c +1 -2
index 3092f8d..fc331d8 100644
@@ -263,8 +263,7 @@ static void xics_spapr_initfn(Object *obj)
 
     ics = ICS(object_new(TYPE_ICS));    
     object_property_add_child(obj, "ics", OBJECT(ics), NULL);
-    ics->xics = xics;
-    QLIST_INSERT_HEAD(&xics->ics, ics, list);
+    xics_add_ics(xics, ics);
 }
 
 static void xics_spapr_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)
include/hw/ppc/xics.h +1 -0
index 9e5b751..5acb329 100644
@@ -191,5 +191,6 @@ void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
 
 void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
 ICSState *xics_find_source(XICSState *icp, int irq);
+void xics_add_ics(XICSState *xics, ICSState *ics);
 
 #endif /* __XICS_H__ */
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
Reviewed-by on v1 34/77
#95 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 34/77] ppc/xics: An ICS with offset 0 is assumed to be uninitialized

In reply to Benjamin Herrenschmidt
Quoted 4 lines · Benjamin Herrenschmidt

This will make life easier for dealing with dynamically configured ICSes such as PHB3

Quoted 17 lines
---
 include/hw/ppc/xics.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/ppc/xics.h +1 -1
index 12fc584..51a63cb 100644
@@ -145,7 +145,7 @@ struct ICSState {
 
 static inline bool ics_valid_irq(ICSState *ics, uint32_t nr)
 {
-    return (nr >= ics->offset)
+    return (ics->offset != 0) && (nr >= ics->offset)
         && (nr < (ics->offset + ics->nr_irqs));
 }
 
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
#93 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 33/77] ppc/xics: Make the ICSState a list

In reply to Benjamin Herrenschmidt
Quoted 90 lines · Benjamin Herrenschmidt

Instead of an array of fixed sized blocks, use a list, as we will need to have sources with variable number of interrupts. SPAPR only uses a single entry. Native will create more. If performance becomes an issue we can add some hashed lookup but for now this will do fine.

---
 hw/intc/xics.c        | 86 +++++++++++++++++++++++++++++++--------------------
 hw/intc/xics_kvm.c    | 28 +++++++++--------
 hw/intc/xics_spapr.c  | 75 ++++++++++++++++++++++++--------------------
 hw/ppc/spapr_events.c |  2 +-
 hw/ppc/spapr_pci.c    |  4 +--
 hw/ppc/spapr_vio.c    |  2 +-
 include/hw/ppc/xics.h | 10 +++---
 7 files changed, 118 insertions(+), 89 deletions(-)
hw/intc/xics.c +13 -9
index d21471f..c4ac057 100644
@@ -79,13 +79,16 @@ void xics_cpu_setup(XICSState *xics, PowerPCCPU *cpu)
 static void xics_common_reset(DeviceState *d)
 {
     XICSState *xics = XICS_COMMON(d);
+    ICSState *ics;
     int i;
 
     for (i = 0; i < xics->nr_servers; i++) {
         device_reset(DEVICE(&xics->ss[i]));
     }
 
-    device_reset(DEVICE(xics->ics));
+    QLIST_FOREACH(ics, &xics->ics, list) {
+        device_reset(DEVICE(ics));
+    }
 }
 
 static void xics_prop_get_nr_irqs(Object *obj, Visitor *v,
@@ -117,7 +120,6 @@ static void xics_prop_set_nr_irqs(Object *obj, Visitor *v,
     }
 
     assert(info->set_nr_irqs);
-    assert(xics->ics);
     info->set_nr_irqs(xics, value, errp);
 }
 
@@ -195,33 +197,35 @@ static void ics_reject(ICSState *ics, int nr);
 static void ics_resend(ICSState *ics);
 static void ics_eoi(ICSState *ics, int nr);
 
-static void icp_check_ipi(XICSState *xics, int server)
+static void icp_check_ipi(ICPState *ss, int server)
 {
-    ICPState *ss = xics->ss + server;
-
     if (XISR(ss) && (ss->pending_priority <= ss->mfrr)) {
         return;
     }
 
     trace_xics_icp_check_ipi(server, ss->mfrr);
 
-    if (XISR(ss)) {
-        ics_reject(xics->ics, XISR(ss));
+    if (XISR(ss) && ss->xirr_owner) {
+        ics_reject(ss->xirr_owner, XISR(ss));
     }
 
     ss->xirr = (ss->xirr & ~XISR_MASK) | XICS_IPI;
     ss->pending_priority = ss->mfrr;
+    ss->xirr_owner = NULL;
     qemu_irq_raise(ss->output);
 }
 
 static void icp_resend(XICSState *xics, int server)
 {
     ICPState *ss = xics->ss + server;
+    ICSState *ics;
 
     if (ss->mfrr < CPPR(ss)) {
-        icp_check_ipi(xics, server);
+        icp_check_ipi(ss, server);
+    }
+    QLIST_FOREACH(ics, &xics->ics, list) {
+        ics_resend(ics);
     }
-    ics_resend(xics->ics);
 }
 
 void icp_set_cppr(XICSState *xics, int server, uint8_t cppr)

hw/intc/xics.c void icp_set_cppr(XICSState *xics, int server, uint8_t cppr) · line 249

Quoted 8 lines
hw/intc/xics.c +4 -1
@@ -239,7 +243,10 @@ void icp_set_cppr(XICSState *xics, int server, uint8_t cppr)
             ss->xirr &= ~XISR_MASK; /* Clear XISR */
             ss->pending_priority = 0xff;
             qemu_irq_lower(ss->output);
-            ics_reject(xics->ics, old_xisr);
+            if (ss->xirr_owner) {
+                ics_reject(ss->xirr_owner, old_xisr);
+                ss->xirr_owner = NULL;
+            }

To match the layour of the rest of things, this little fragment should probably go into an icp_reject() function.

Quoted 48 lines
hw/intc/xics.c +12 -3
         }
     } else {
         if (!XISR(ss)) {
@@ -254,7 +261,7 @@ void icp_set_mfrr(XICSState *xics, int server, uint8_t mfrr)
 
     ss->mfrr = mfrr;
     if (mfrr < CPPR(ss)) {
-        icp_check_ipi(xics, server);
+        icp_check_ipi(ss, server);
     }
 }
 
@@ -265,6 +272,7 @@ uint32_t icp_accept(ICPState *ss)
     qemu_irq_lower(ss->output);
     ss->xirr = ss->pending_priority << 24;
     ss->pending_priority = 0xff;
+    ss->xirr_owner = NULL;
 
     trace_xics_icp_accept(xirr, ss->xirr);
 
@@ -282,30 +290,40 @@ uint32_t icp_ipoll(ICPState *ss, uint32_t *mfrr)
 void icp_eoi(XICSState *xics, int server, uint32_t xirr)
 {
     ICPState *ss = xics->ss + server;
+    ICSState *ics;
+    uint32_t irq;
 
     /* Send EOI -> ICS */
     ss->xirr = (ss->xirr & ~CPPR_MASK) | (xirr & CPPR_MASK);
     trace_xics_icp_eoi(server, xirr, ss->xirr);
-    ics_eoi(xics->ics, xirr & XISR_MASK);
+    irq = xirr & XISR_MASK;
+    QLIST_FOREACH(ics, &xics->ics, list) {
+        if (ics_valid_irq(ics, irq)) {
+            ics_eoi(ics, irq);
+        }
+    }
     if (!XISR(ss)) {
         icp_resend(xics, server);
     }
 }
 
-static void icp_irq(XICSState *xics, int server, int nr, uint8_t priority)
+static void icp_irq(ICSState *ics, int server, int nr, uint8_t priority)
 {
+    XICSState *xics = ics->xics;
     ICPState *ss = xics->ss + server;
 

hw/intc/xics.c uint32_t icp_ipoll(ICPState *ss, uint32_t *mfrr) · line 321

Quoted 10 lines
hw/intc/xics.c +2 -3
     trace_xics_icp_irq(server, nr, priority);
 
     if ((priority >= CPPR(ss))
         || (XISR(ss) && (ss->pending_priority <= priority))) {
-        ics_reject(xics->ics, nr);
+        ics_reject(ics, nr);
     } else {
-        if (XISR(ss)) {
-            ics_reject(xics->ics, XISR(ss));
+        if (XISR(ss) && ss->xirr_owner) {

I'm guessing the only case where we should get XISR(ss) && !ss->xirr_owner will be an IPI, is that right?

Quoted 82 lines
hw/intc/xics.c +14 -17
+            ics_reject(ss->xirr_owner, XISR(ss));
+            ss->xirr_owner = NULL;
         }
         ss->xirr = (ss->xirr & ~XISR_MASK) | (nr & XISR_MASK);
+        ss->xirr_owner = ics;
         ss->pending_priority = priority;
         trace_xics_icp_raise(ss->xirr, ss->pending_priority);
         qemu_irq_raise(ss->output);
@@ -388,8 +406,7 @@ static void resend_msi(ICSState *ics, int srcno)
     if (irq->status & XICS_STATUS_REJECTED) {
         irq->status &= ~XICS_STATUS_REJECTED;
         if (irq->priority != 0xff) {
-            icp_irq(ics->xics, irq->server, srcno + ics->offset,
-                    irq->priority);
+            icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
         }
     }
 }
@@ -402,7 +419,7 @@ static void resend_lsi(ICSState *ics, int srcno)
         && (irq->status & XICS_STATUS_ASSERTED)
         && !(irq->status & XICS_STATUS_SENT)) {
         irq->status |= XICS_STATUS_SENT;
-        icp_irq(ics->xics, irq->server, srcno + ics->offset, irq->priority);
+        icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
     }
 }
 
@@ -417,7 +434,7 @@ static void set_irq_msi(ICSState *ics, int srcno, int val)
             irq->status |= XICS_STATUS_MASKED_PENDING;
             trace_xics_masked_pending();
         } else  {
-            icp_irq(ics->xics, irq->server, srcno + ics->offset, irq->priority);
+            icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
         }
     }
 }
@@ -456,7 +473,7 @@ static void write_xive_msi(ICSState *ics, int srcno)
     }
 
     irq->status &= ~XICS_STATUS_MASKED_PENDING;
-    icp_irq(ics->xics, irq->server, srcno + ics->offset, irq->priority);
+    icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
 }
 
 static void write_xive_lsi(ICSState *ics, int srcno)
@@ -642,28 +659,23 @@ static const TypeInfo ics_info = {
 /*
  * Exported functions
  */
-int xics_find_source(XICSState *xics, int irq)
+ICSState *xics_find_source(XICSState *xics, int irq)
 {
-    int sources = 1;
-    int src;
+    ICSState *ics;
 
-    /* FIXME: implement multiple sources */
-    for (src = 0; src < sources; ++src) {
-        ICSState *ics = &xics->ics[src];
+    QLIST_FOREACH(ics, &xics->ics, list) {
         if (ics_valid_irq(ics, irq)) {
-            return src;
+            return ics;
         }
     }
-
-    return -1;
+    return NULL;
 }
 
 qemu_irq xics_get_qirq(XICSState *xics, int irq)
 {
-    int src = xics_find_source(xics, irq);
+    ICSState *ics = xics_find_source(xics, irq);
 
-    if (src >= 0) {
-        ICSState *ics = &xics->ics[src];
+    if (ics) {
         return ics->qirqs[irq - ics->offset];
     }
 

hw/intc/xics.c void ics_set_irq_type(ICSState *ics, int srcno, bool lsi) · line 701

Quoted 7 lines
hw/intc/xics.c +3 -1
@@ -684,7 +696,13 @@ void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
 
 void xics_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)
 {
-    xics->nr_irqs = xics->ics->nr_irqs = nr_irqs;
+    ICSState *ics = QLIST_FIRST(&xics->ics);
+
+    /* This needs to be deprecated ... */

Yeah..

I can't even remember what this was really for. We probably need to move it to a property on the ics objects, with a backwards compat shim for PAPR to copy the xics "master" object value to the single ics.

Quoted 67 lines
hw/intc/xics.c +4 -0
+    xics->nr_irqs = nr_irqs;
+    if (ics) {
+        ics->nr_irqs = nr_irqs;
+    }
 }
 
 void xics_set_nr_servers(XICSState *xics, uint32_t nr_servers, Error **errp)
hw/intc/xics_kvm.c +14 -12
index 7d86157..a478d25 100644
@@ -356,11 +356,6 @@ static void xics_kvm_cpu_setup(XICSState *xics, PowerPCCPU *cpu)
     }
 }
 
-static void xics_kvm_set_nr_irqs(XICSState *xics, uint32_t nr_irqs, Error **errp)
-{
-    xics->nr_irqs = xics->ics->nr_irqs = nr_irqs;
-}
-
 static void xics_kvm_set_nr_servers(XICSState *xics, uint32_t nr_servers,
                                     Error **errp)
 {
@@ -391,6 +386,7 @@ static void xics_kvm_realize(DeviceState *dev, Error **errp)
 {
     KVMXICSState *xicskvm = KVM_XICS(dev);
     XICSState *xics = XICS_COMMON(dev);
+    ICSState *ics;
     int i, rc;
     Error *error = NULL;
     struct kvm_create_device xics_create_device = {
@@ -442,10 +438,12 @@ static void xics_kvm_realize(DeviceState *dev, Error **errp)
 
     xicskvm->kernel_xics_fd = xics_create_device.fd;
 
-    object_property_set_bool(OBJECT(xics->ics), true, "realized", &error);
-    if (error) {
-        error_propagate(errp, error);
-        goto fail;
+    QLIST_FOREACH(ics, &xics->ics, list) {
+        object_property_set_bool(OBJECT(ics), true, "realized", &error);
+        if (error) {
+            error_propagate(errp, error);
+            goto fail;
+        }
     }
 
     assert(xics->nr_servers);
@@ -473,10 +471,14 @@ fail:
 static void xics_kvm_initfn(Object *obj)
 {
     XICSState *xics = XICS_COMMON(obj);
+    ICSState *ics;
+
+    QLIST_INIT(&xics->ics);
 
-    xics->ics = ICS(object_new(TYPE_KVM_ICS));
-    object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
-    xics->ics->xics = xics;
+    ics = ICS(object_new(TYPE_KVM_ICS));
+    object_property_add_child(obj, "ics", OBJECT(ics), NULL);
+    ics->xics = xics;
+    QLIST_INSERT_HEAD(&xics->ics, ics, list);
 }
 
 static void xics_kvm_class_init(ObjectClass *oc, void *data)

hw/intc/xics_kvm.c static void xics_kvm_class_init(ObjectClass *oc, void *data) · line 491

Quoted 5 lines
hw/intc/xics_kvm.c +1 -1
@@ -486,7 +488,7 @@ static void xics_kvm_class_init(ObjectClass *oc, void *data)
 
     dc->realize = xics_kvm_realize;
     xsc->cpu_setup = xics_kvm_cpu_setup;
-    xsc->set_nr_irqs = xics_kvm_set_nr_irqs;
+    xsc->set_nr_irqs = xics_set_nr_irqs;

Wow.. why were xics_kvm_set_nr_irqs and xics_set_nr_irqs ever separate, I wonder.

Quoted 10 lines
hw/intc/xics_kvm.c +0 -0
     xsc->set_nr_servers = xics_kvm_set_nr_servers;
 }
 
hw/intc/xics_spapr.c +0 -0
index fb508cd..d75fcf0 100644
@@ -111,10 +111,10 @@ static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                           uint32_t nargs, target_ulong args,
                           uint32_t nret, target_ulong rets)

hw/intc/xics_spapr.c static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr, · line 120

Quoted 10 lines
hw/intc/xics_spapr.c +2 -2
 {
-    ICSState *ics = spapr->xics->ics;
+    ICSState *ics = QLIST_FIRST(&spapr->xics->ics);
     uint32_t nr, server, priority;
 
-    if ((nargs != 3) || (nret != 1)) {
+    if ((nargs != 3) || (nret != 1) || !ics) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }

!ics should probably be a HW_ERROR (or even an assert).

Quoted 107 lines
hw/intc/xics_spapr.c +26 -17
@@ -139,10 +139,10 @@ static void rtas_get_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                           uint32_t nargs, target_ulong args,
                           uint32_t nret, target_ulong rets)
 {
-    ICSState *ics = spapr->xics->ics;
+    ICSState *ics = QLIST_FIRST(&spapr->xics->ics);
     uint32_t nr;
 
-    if ((nargs != 1) || (nret != 3)) {
+    if ((nargs != 1) || (nret != 3) || !ics) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }
@@ -164,10 +164,10 @@ static void rtas_int_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                          uint32_t nargs, target_ulong args,
                          uint32_t nret, target_ulong rets)
 {
-    ICSState *ics = spapr->xics->ics;
+    ICSState *ics = QLIST_FIRST(&spapr->xics->ics);
     uint32_t nr;
 
-    if ((nargs != 1) || (nret != 1)) {
+    if ((nargs != 1) || (nret != 1) || !ics) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }
@@ -190,10 +190,10 @@ static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                         uint32_t nargs, target_ulong args,
                         uint32_t nret, target_ulong rets)
 {
-    ICSState *ics = spapr->xics->ics;
+    ICSState *ics = QLIST_FIRST(&spapr->xics->ics);
     uint32_t nr;
 
-    if ((nargs != 1) || (nret != 1)) {
+    if ((nargs != 1) || (nret != 1) || !ics) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }
@@ -215,6 +215,7 @@ static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 static void xics_spapr_realize(DeviceState *dev, Error **errp)
 {
     XICSState *xics = XICS(dev);
+    ICSState *ics;
     Error *error = NULL;
     int i;
 
@@ -236,10 +237,12 @@ static void xics_spapr_realize(DeviceState *dev, Error **errp)
     spapr_register_hypercall(H_EOI, h_eoi);
     spapr_register_hypercall(H_IPOLL, h_ipoll);
 
-    object_property_set_bool(OBJECT(xics->ics), true, "realized", &error);
-    if (error) {
-        error_propagate(errp, error);
-        return;
+    QLIST_FOREACH(ics, &xics->ics, list) {
+        object_property_set_bool(OBJECT(ics), true, "realized", &error);
+        if (error) {
+            error_propagate(errp, error);
+            return;
+        }
     }
 
     for (i = 0; i < xics->nr_servers; i++) {
@@ -254,10 +257,14 @@ static void xics_spapr_realize(DeviceState *dev, Error **errp)
 static void xics_spapr_initfn(Object *obj)
 {
     XICSState *xics = XICS(obj);
+    ICSState *ics;
+
+    QLIST_INIT(&xics->ics);
 
-    xics->ics = ICS(object_new(TYPE_ICS));
-    object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
-    xics->ics->xics = xics;
+    ics = ICS(object_new(TYPE_ICS));    
+    object_property_add_child(obj, "ics", OBJECT(ics), NULL);
+    ics->xics = xics;
+    QLIST_INSERT_HEAD(&xics->ics, ics, list);
 }
 
 static void xics_spapr_class_init(ObjectClass *oc, void *data)
@@ -303,29 +310,31 @@ static int ics_find_free_block(ICSState *ics, int num, int alignnum)
     return -1;
 }
 
-int xics_spapr_alloc(XICSState *xics, int src, int irq_hint, bool lsi)
+int xics_spapr_alloc(XICSState *xics, int irq_hint, bool lsi)
 {
-    ICSState *ics = &xics->ics[src];
+    ICSState *ics = QLIST_FIRST(&xics->ics);
     int irq;
 
+    if (!ics) {
+        return -1;
+    }
     if (irq_hint) {
-        assert(src == xics_find_source(xics, irq_hint));
         if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
-            trace_xics_alloc_failed_hint(src, irq_hint);
+            trace_xics_alloc_failed_hint(0, irq_hint);
             return -1;
         }
         irq = irq_hint;
     } else {
         irq = ics_find_free_block(ics, 1, 1);
         if (irq < 0) {

hw/intc/xics_spapr.c static int ics_find_free_block(ICSState *ics, int num, int alignnum) · line 337

Quoted 10 lines
hw/intc/xics_spapr.c +2 -2
-            trace_xics_alloc_failed_no_left(src);
+            trace_xics_alloc_failed_no_left(0);
             return -1;
         }
         irq += ics->offset;
     }
 
     ics_set_irq_type(ics, irq - ics->offset, lsi);
-    trace_xics_alloc(src, irq);
+    trace_xics_alloc(0, irq);

Actually we should really deprecate xics_spapr_alloc(). I hadn't realised it at the time, but dynamically allocating resources like this is a PITA for migration and cross-version compatibility. So we should avoid it, instead having the caller explicitly assign numbers based on explicit parameters.

Quoted 120 lines
hw/intc/xics_spapr.c +11 -13
     return irq;
 }
@@ -334,12 +343,15 @@ int xics_spapr_alloc(XICSState *xics, int src, int irq_hint, bool lsi)
  * Allocate block of consecutive IRQs, and return the number of the first IRQ in the block.
  * If align==true, aligns the first IRQ number to num.
  */
-int xics_spapr_alloc_block(XICSState *xics, int src, int num, bool lsi, bool align)
+int xics_spapr_alloc_block(XICSState *xics, int num, bool lsi, bool align)
 {
+    ICSState *ics = QLIST_FIRST(&xics->ics);
     int i, first = -1;
-    ICSState *ics = &xics->ics[src];
 
-    assert(src == 0);
+    if (!ics) {
+        return -1;
+    }
+
     /*
      * MSIMesage::data is used for storing VIRQ so
      * it has to be aligned to num to support multiple
@@ -362,7 +374,7 @@ int xics_spapr_alloc_block(XICSState *xics, int src, int num, bool lsi, bool ali
     }
     first += ics->offset;
 
-    trace_xics_alloc_block(src, first, num, lsi, align);
+    trace_xics_alloc_block(0, first, num, lsi, align);
 
     return first;
 }
@@ -373,7 +385,7 @@ static void ics_free(ICSState *ics, int srcno, int num)
 
     for (i = srcno; i < srcno + num; ++i) {
         if (ICS_IRQ_FREE(ics, i)) {
-            trace_xics_ics_free_warn(ics - ics->xics->ics, i + ics->offset);
+            trace_xics_ics_free_warn(0, i + ics->offset);
         }
         memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
     }
@@ -381,15 +393,10 @@ static void ics_free(ICSState *ics, int srcno, int num)
 
 void xics_spapr_free(XICSState *xics, int irq, int num)
 {
-    int src = xics_find_source(xics, irq);
-
-    if (src >= 0) {
-        ICSState *ics = &xics->ics[src];
-
-        /* FIXME: implement multiple sources */
-        assert(src == 0);
+    ICSState *ics = xics_find_source(xics, irq);
 
-        trace_xics_ics_free(ics - xics->ics, irq, num);
+    if (ics) {
+        trace_xics_ics_free(0, irq, num);
         ics_free(ics, irq - ics->offset, num);
     }
 }
hw/ppc/spapr_events.c +1 -1
index c06deea..6335ead 100644
@@ -587,7 +587,7 @@ out_no_events:
 void spapr_events_init(sPAPRMachineState *spapr)
 {
     QTAILQ_INIT(&spapr->pending_events);
-    spapr->check_exception_irq = xics_spapr_alloc(spapr->xics, 0, 0, false);
+    spapr->check_exception_irq = xics_spapr_alloc(spapr->xics, 0, false);
     spapr->epow_notifier.notify = spapr_powerdown_req;
     qemu_register_powerdown_notifier(&spapr->epow_notifier);
     spapr_rtas_register(RTAS_CHECK_EXCEPTION, "check-exception",
hw/ppc/spapr_pci.c +2 -2
index cf3192e..9b13f85 100644
@@ -351,7 +351,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
     }
 
     /* Allocate MSIs */
-    irq = xics_spapr_alloc_block(spapr->xics, 0, req_num, false,
+    irq = xics_spapr_alloc_block(spapr->xics, req_num, false,
                            ret_intr_type == RTAS_TYPE_MSI);
     if (!irq) {
         error_report("Cannot allocate MSIs for device %x", config_addr);
@@ -1360,7 +1360,7 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < PCI_NUM_PINS; i++) {
         uint32_t irq;
 
-        irq = xics_spapr_alloc_block(spapr->xics, 0, 1, true, false);
+        irq = xics_spapr_alloc_block(spapr->xics, 1, true, false);
         if (!irq) {
             error_setg(errp, "spapr_allocate_lsi failed");
             return;
hw/ppc/spapr_vio.c +1 -1
index fc731eb..1a84815 100644
@@ -462,7 +462,7 @@ static void spapr_vio_busdev_realize(DeviceState *qdev, Error **errp)
         dev->qdev.id = id;
     }
 
-    dev->irq = xics_spapr_alloc(spapr->xics, 0, dev->irq, false);
+    dev->irq = xics_spapr_alloc(spapr->xics, dev->irq, false);
     if (!dev->irq) {
         error_setg(errp, "can't allocate IRQ");
         return;
include/hw/ppc/xics.h +1 -1
index e670e89..12fc584 100644
@@ -79,7 +79,7 @@ struct XICSState {
     uint32_t nr_servers;
     uint32_t nr_irqs;
     ICPState *ss;
-    ICSState *ics;
+    QLIST_HEAD(, ICSState) ics;
 };
 
 #define TYPE_ICP "icp"

include/hw/ppc/xics.h struct ICPState { · line 108

Quoted 4 lines
include/hw/ppc/xics.h +1 -0
@@ -105,6 +105,7 @@ struct ICPState {
     DeviceState parent_obj;
     /*< public >*/
     CPUState *cs;
+    ICSState *xirr_owner;

Currently xirr_owner will be lost across migration, which will break things.

Am I right in thinking that it's basically an optimization, and could be reconstructed from the XISR value and list of ICS ranges? If so we can do that easily enough in a post_load function.

Quoted 30 lines
include/hw/ppc/xics.h +4 -3
     uint32_t xirr;
     uint8_t pending_priority;
     uint8_t mfrr;
@@ -139,6 +140,7 @@ struct ICSState {
     qemu_irq *qirqs;
     ICSIRQState *irqs;
     XICSState *xics;
+    QLIST_ENTRY(ICSState) list;
 };
 
 static inline bool ics_valid_irq(ICSState *ics, uint32_t nr)
@@ -167,8 +169,8 @@ struct ICSIRQState {
 
 qemu_irq xics_get_qirq(XICSState *icp, int irq);
 
-int xics_spapr_alloc(XICSState *icp, int src, int irq_hint, bool lsi);
-int xics_spapr_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align);
+int xics_spapr_alloc(XICSState *icp, int irq_hint, bool lsi);
+int xics_spapr_alloc_block(XICSState *icp, int num, bool lsi, bool align);
 void xics_spapr_free(XICSState *icp, int irq, int num);
 
 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu);
@@ -189,6 +191,6 @@ void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
 
 void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp);
 void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
-int xics_find_source(XICSState *icp, int irq);
+ICSState *xics_find_source(XICSState *icp, int irq);
 
 #endif /* __XICS_H__ */
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
#101 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 37/77] ppc/xics: Split ICS into base class and "simple" implementation

In reply to Benjamin Herrenschmidt
Quoted 8 lines · Benjamin Herrenschmidt

The existing implementation becomes the "ics-simple" subclass of ICS, so there should be no change in behaviour for SPAPR.

This will allow different implementations for the source controllers such as the MSI support of PHB3 on Power8 which uses in-memory state tables for example.

[snip]

hw/intc/xics_spapr.c static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr, · line 115

Quoted 10 lines
hw/intc/xics_spapr.c +1 -1
index fc331d8..f7d444a 100644
@@ -112,7 +112,7 @@ static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
                           uint32_t nret, target_ulong rets)
 {
     ICSState *ics = QLIST_FIRST(&spapr->xics->ics);
-    uint32_t nr, server, priority;
+    uint32_t nr, src_no, server, priority;

Nit: elsewhere I use srcno without a _ for this value.

[snip]

include/hw/ppc/xics.h struct ICPState { · line 120

Quoted 10 lines
include/hw/ppc/xics.h +2 -0
index 5acb329..93a627b 100644
@@ -116,6 +116,9 @@ struct ICPState {
 #define TYPE_ICS "ics"
 #define ICS(obj) OBJECT_CHECK(ICSState, (obj), TYPE_ICS)
 
+#define TYPE_ICS_SIMPLE "ics-simple"
+#define ICS_SIMPLE(obj) OBJECT_CHECK(ICSState, (obj), TYPE_ICS_SIMPLE)

Again, changing type names will break migration, I believe. So better to leave this as ics and add "ics-base" and "ics-native" or similar.

Quoted 28 lines
include/hw/ppc/xics.h +7 -3
+
 #define TYPE_KVM_ICS "icskvm"
 #define KVM_ICS(obj) OBJECT_CHECK(ICSState, (obj), TYPE_KVM_ICS)
 
@@ -129,6 +132,9 @@ struct ICSStateClass {
 
     void (*pre_save)(ICSState *s);
     int (*post_load)(ICSState *s, int version_id);
+    void (*reject)(ICSState *s, uint32_t irq);
+    void (*resend)(ICSState *s);
+    void (*eoi)(ICSState *s, uint32_t irq);
 };
 
 struct ICSState {
@@ -184,10 +190,10 @@ uint32_t icp_accept(ICPState *ss);
 uint32_t icp_ipoll(ICPState *ss, uint32_t *mfrr);
 void icp_eoi(XICSState *icp, int server, uint32_t xirr);
 
-void ics_write_xive(ICSState *ics, int nr, int server,
-                    uint8_t priority, uint8_t saved_priority);
+void ics_simple_write_xive(ICSState *ics, int nr, int server,
+                           uint8_t priority, uint8_t saved_priority);
 
-void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
+void ics_simple_set_irq_type(ICSState *ics, int srcno, bool lsi);
 
 void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
 ICSState *xics_find_source(XICSState *icp, int irq);
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
#106 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 39/77] ppc/xics: Add xics to the monitor "info pic" command

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt

Useful to debug interrupt problems.

Ugh.. I can see the use of this, but we really want to just deprecate info pic entirely, it's an awful, awful interface.

I think the right way to do this is to allow some state introspection via the QOM interfaces on the xics devices themselves, but I'm not immediately sure how to go about that.

Quoted 161 lines
---
 hmp-commands-info.hx  |  2 ++
 hw/intc/xics.c        | 38 ++++++++++++++++++++++++++++++++++++++
 hw/ppc/ppc.c          | 14 ++++++++++++++
 include/hw/ppc/ppc.h  |  2 ++
 include/hw/ppc/xics.h |  2 ++
 monitor.c             |  3 +++
 6 files changed, 61 insertions(+)
hmp-commands-info.hx +2 -0
index 9b71351..2f1dc86 100644
@@ -203,6 +203,8 @@ ETEXI
         .mhandler.cmd = sun4m_hmp_info_pic,
 #elif defined(TARGET_LM32)
         .mhandler.cmd = lm32_hmp_info_pic,
+#elif defined(TARGET_PPC)
+        .mhandler.cmd = ppc_hmp_info_pic,
 #else
         .mhandler.cmd = hmp_info_pic,
 #endif
hw/intc/xics.c +38 -0
index 4b33e6d..d027a24 100644
@@ -31,6 +31,9 @@
 #include "hw/ppc/xics.h"
 #include "qemu/error-report.h"
 #include "qapi/visitor.h"
+#include "monitor/monitor.h"
+
+static XICSState *g_xics;
 
 int get_cpu_index_by_dt_id(int cpu_dt_id)
 {
@@ -170,6 +173,9 @@ static void xics_common_initfn(Object *obj)
     object_property_add(obj, "nr_servers", "int",
                         xics_prop_get_nr_servers, xics_prop_set_nr_servers,
                         NULL, NULL, NULL);
+
+    /* For exclusive use of monitor command */
+    g_xics = XICS_COMMON(obj);
 }
 
 static void xics_common_class_init(ObjectClass *oc, void *data)
@@ -614,6 +620,38 @@ static int ics_dispatch_post_load(void *opaque, int version_id)
     return 0;
 }
 
+void xics_hmp_info_pic(Monitor *mon, const QDict *qdict)
+{
+    ICSState *ics;
+    uint32_t i;
+
+    for (i = 0; i < g_xics->nr_servers; i++) {
+        ICPState *icp = &g_xics->ss[i];
+
+        if (!icp->output) {
+            continue;
+        }
+        monitor_printf(mon, "CPU %d XIRR=%08x (%p) PP=%02x MFRR=%02x\n",
+                       i, icp->xirr, icp->xirr_owner,
+                       icp->pending_priority, icp->mfrr);
+    }
+    QLIST_FOREACH(ics, &g_xics->ics, list) {
+        monitor_printf(mon, "ICS %4x..%4x %p\n",
+                       ics->offset, ics->offset + ics->nr_irqs - 1, ics);
+        for (i = 0; i < ics->nr_irqs; i++) {
+            ICSIRQState *irq = ics->irqs + i;
+
+            if (!(irq->flags & XICS_FLAGS_IRQ_MASK)) {
+                continue;
+            }
+            monitor_printf(mon, "  %4x %s %02x %02x\n",
+                           ics->offset + i,
+                           (irq->flags & XICS_FLAGS_IRQ_LSI) ? "LSI" : "MSI",
+                           irq->priority, irq->status);
+        }
+    }
+}
+
 static const VMStateDescription vmstate_ics_simple_irq = {
     .name = "ics/irq",
     .version_id = 2,
hw/ppc/ppc.c +14 -0
index 2c604ef..3b14f09 100644
@@ -24,6 +24,7 @@
 #include "hw/hw.h"
 #include "hw/ppc/ppc.h"
 #include "hw/ppc/ppc_e500.h"
+#include "hw/i386/pc.h"
 #include "qemu/timer.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpus.h"
@@ -35,6 +36,10 @@
 #include "kvm_ppc.h"
 #include "trace.h"
 
+#if defined(TARGET_PPC64)
+#include "hw/ppc/xics.h"
+#endif
+
 //#define PPC_DEBUG_IRQ
 //#define PPC_DEBUG_TB
 
@@ -1337,3 +1342,12 @@ PowerPCCPU *ppc_get_vcpu_by_dt_id(int cpu_dt_id)
 
     return NULL;
 }
+
+void ppc_hmp_info_pic(Monitor *mon, const QDict *qdict)
+{
+    /* Call in turn every PIC around. OpenPIC doesn't have one yet */
+#ifdef TARGET_PPC64
+    xics_hmp_info_pic(mon, qdict);
+#endif
+    hmp_info_pic(mon, qdict);
+}
include/hw/ppc/ppc.h +2 -0
index 14efd0c..d5c648d 100644
@@ -1,6 +1,8 @@
 #ifndef HW_PPC_H
 #define HW_PPC_H 1
 
+void ppc_hmp_info_pic(Monitor *mon, const QDict *qdict);
+
 void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level);
 
 /* PowerPC hardware exceptions management helpers */
include/hw/ppc/xics.h +2 -0
index f32f409..1cf7037 100644
@@ -213,4 +213,6 @@ void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers, Error **errp);
 ICSState *xics_find_source(XICSState *icp, int irq);
 void xics_add_ics(XICSState *xics, ICSState *ics);
 
+void xics_hmp_info_pic(Monitor *mon, const QDict *qdict);
+
 #endif /* __XICS_H__ */
monitor.c +3 -0
index 3295840..988477e 100644
@@ -76,6 +76,9 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/block-backend.h"
+#if defined(TARGET_PPC)
+#include "hw/ppc/ppc.h"
+#endif
 
 /* for hmp_info_irq/pic */
 #if defined(TARGET_SPARC)
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
#103 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 38/77] ppc/xics: Add "native" XICS subclass

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt

This provides MMIO based ICP access as found on POWER8

Mostly looks sound.

I note that the links registers don't actually do anything at this point, I assume thats intentional. It probably deserves a comment somewhere (in the commit message at least).

Quoted 42 lines
---
 default-configs/ppc64-softmmu.mak |   3 +-
 hw/intc/Makefile.objs             |   1 +
 hw/intc/xics_native.c             | 294 ++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/xics.h             |  14 ++
 4 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 hw/intc/xics_native.c
default-configs/ppc64-softmmu.mak +2 -1
index 516a6e2..d30176e 100644
@@ -49,8 +49,9 @@ CONFIG_PLATFORM_BUS=y
 CONFIG_ETSEC=y
 CONFIG_LIBDECNUMBER=y
 # For pSeries
-CONFIG_XICS=$(CONFIG_PSERIES)
+CONFIG_XICS=$(or $(CONFIG_PSERIES),$(CONFIG_POWERNV))
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
+CONFIG_XICS_NATIVE=$(CONFIG_POWERNV)
 CONFIG_XICS_KVM=$(and $(CONFIG_PSERIES),$(CONFIG_KVM))
 # For PReP
 CONFIG_MC146818RTC=y
hw/intc/Makefile.objs +1 -0
index e24cb03..104a169 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_OPENPIC_KVM) += openpic_kvm.o
 obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
 obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
+obj-$(CONFIG_XICS_NATIVE) += xics_native.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/xics_native.c new file +0 -0
new file mode 100644
index 0000000..3f488f3

hw/intc/xics_native.c line 6

Quoted 6 lines
hw/intc/xics_native.c new file +6 -0
@@ -0,0 +1,294 @@
+/*
+ * QEMU PowerPC hardware System Emulator
+ *
+ * Native version of ICS/ICP
+ *
+ * Copyright (c) 2010,2011 David Gibson, IBM Corporation.

It should probably have your copyright too; I'm pretty sure this is not all my code ;).

Quoted 244 lines
hw/intc/xics_native.c new file +244 -0
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "hw/hw.h"
+#include "trace.h"
+#include "qemu/timer.h"
+#include "hw/ppc/xics.h"
+#include "qemu/error-report.h"
+#include "qapi/visitor.h"
+
+#include <libfdt.h>
+
+//#define DEBUG_MM(fmt...)      printf(fmt)
+#define DEBUG_MM(fmt...)        do { } while(0)
+
+static void xics_native_initfn(Object *obj)
+{
+    XICSState *xics = XICS_NATIVE(obj);
+
+    QLIST_INIT(&xics->ics);
+}
+
+static uint64_t icp_mm_read(void *opaque, hwaddr addr, unsigned width)
+{
+    XICSState *s = opaque;
+    int32_t cpu_id, server;
+    uint32_t val;
+    ICPState *ss;
+    bool byte0 = (width == 1 && (addr & 0x3) == 0);
+
+    cpu_id = (addr & (ICP_MM_SIZE - 1)) >> 12;
+    server = get_cpu_index_by_dt_id(cpu_id);
+    if (server < 0) {
+        fprintf(stderr, "XICS: Bad ICP server %d\n", server);
+        goto bad_access;
+    }
+    ss = &s->ss[server];
+
+    switch(addr & 0xffc) {
+    case 0: /* poll */
+        val = icp_ipoll(ss, NULL);
+        if (byte0) {
+            val >>= 24;
+        } else if (width != 4) {
+            goto bad_access;
+        }
+        break;
+    case 4: /* xirr */
+        if (byte0) {
+            val = icp_ipoll(ss, NULL) >> 24;
+        } else if (width == 4) {
+            val = icp_accept(ss);
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 12:
+        if (byte0) {
+            val = ss->mfrr;
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 16:
+        if (width == 4) {
+            val = ss->links[0];
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 20:
+        if (width == 4) {
+            val = ss->links[1];
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 24:
+        if (width == 4) {
+            val = ss->links[2];
+        } else {
+            goto bad_access;
+        }
+        break;
+    default:
+bad_access:
+        fprintf(stderr, "XICS: Bad ICP access %llx/%d\n",
+                (unsigned long long)addr, width);
+        val = 0xffffffff;
+    }
+    DEBUG_MM("icp_mm_read(addr=%016llx,serv=0x%x/%d,off=%d,w=%d,val=0x%08x)\n",
+             (unsigned long long)addr, cpu_id, server, (int)(addr & 0xffc),
+             width, val);
+
+    return val;
+}
+
+static void icp_mm_write(void *opaque, hwaddr addr, uint64_t val,
+                        unsigned width)
+{
+    XICSState *s = opaque;
+    int32_t cpu_id, server;
+    ICPState *ss;
+    bool byte0 = (width == 1 && (addr & 0x3) == 0);
+
+    cpu_id = (addr & (ICP_MM_SIZE - 1)) >> 12;
+    server = get_cpu_index_by_dt_id(cpu_id);
+    if (server < 0) {
+        fprintf(stderr, "XICS: Bad ICP server %d\n", server);
+        goto bad_access;
+    }
+    ss = &s->ss[server];
+
+    DEBUG_MM("icp_mm_write(addr=%016llx,serv=0x%x/%d,off=%d,w=%d,val=0x%08x)\n",
+             (unsigned long long)addr, cpu_id, server,
+             (int)(addr & 0xffc), width, (uint32_t)val);
+
+    switch(addr & 0xffc) {
+    case 4: /* xirr */
+        if (byte0) {
+            icp_set_cppr(s, server, val);
+        } else if (width == 4) {
+            icp_eoi(s, server, val);
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 12:
+        if (byte0) {
+            icp_set_mfrr(s, server, val);
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 16:
+        if (width == 4) {
+            ss->links[0] = val;
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 20:
+        if (width == 4) {
+            ss->links[1] = val;
+        } else {
+            goto bad_access;
+        }
+        break;
+    case 24:
+        if (width == 4) {
+            ss->links[2] = val;
+        } else {
+            goto bad_access;
+        }
+        break;
+    default:
+ bad_access:
+        val = 0xffffffff;
+    }
+}
+
+static const MemoryRegionOps icp_mm_ops = {
+    .read = icp_mm_read,
+    .write = icp_mm_write,
+    .valid.min_access_size = 1,
+    .valid.max_access_size = 4,
+    .impl.min_access_size = 1,
+    .impl.max_access_size = 4,
+    .endianness = DEVICE_BIG_ENDIAN,
+};
+
+#define _FDT(exp) \
+    do { \
+        int ret = (exp);                                           \
+        if (ret < 0) {                                             \
+            fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
+                    #exp, fdt_strerror(ret));                      \
+            exit(1);                                               \
+        }                                                          \
+    } while (0)
+
+void xics_create_native_icp_node(XICSState *s, void *fdt,
+                                 uint32_t base, uint32_t count)
+{
+    uint64_t addr;
+    char *name;
+    const char compat[] = "IBM,power8-icp\0IBM,ppc-xicp";
+    uint32_t irange[2], i, rsize;
+    uint64_t *reg;
+
+    addr = ICP_MM_BASE | (base << 12);
+
+    irange[0] = cpu_to_be32(base);
+    irange[1] = cpu_to_be32(count);
+
+    rsize = sizeof(uint64_t) * 2 * count;
+    reg = g_malloc(rsize);
+    for (i = 0; i < count; i++) {
+        reg[i * 2] = cpu_to_be64(addr | ((base + i) * 0x1000));
+        reg[i * 2 + 1] = cpu_to_be64(0x1000);
+    }
+
+    name = g_strdup_printf("interrupt-controller@%"PRIX64, addr);
+
+    /* interrupt controller */
+    _FDT((fdt_begin_node(fdt, name)));
+    g_free(name);
+
+    _FDT((fdt_property(fdt, "compatible", compat, sizeof(compat))));
+    _FDT((fdt_property(fdt, "reg", reg, rsize)));
+    _FDT((fdt_property_string(fdt, "device_type",
+                              "PowerPC-External-Interrupt-Presentation")));
+    _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
+    _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
+                       irange, sizeof(irange))));
+    _FDT((fdt_property_cell(fdt, "#interrupt-cells", 1)));
+    _FDT((fdt_property_cell(fdt, "#address-cells", 0)));
+    _FDT((fdt_end_node(fdt)));
+}
+
+static void xics_native_realize(DeviceState *dev, Error **errp)
+{
+    XICSState *s = XICS_NATIVE(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    Error *error = NULL;
+    int i;
+

hw/intc/xics_native.c line 261

Quoted 10 lines
hw/intc/xics_native.c new file +10 -0
+    if (!s->nr_servers) {
+        error_setg(errp, "Number of servers needs to be greater 0");
+        return;
+    }
+
+    /* Register MMIO regions */
+    memory_region_init_io(&s->icp_mmio, OBJECT(s), &icp_mm_ops, s, "icp",
+                          ICP_MM_SIZE);
+    sysbus_init_mmio(sbd, &s->icp_mmio);
+    sysbus_mmio_map(sbd, 0, ICP_MM_BASE);

As a rule it's better to have MMIO addresses of things as a property and have the thing creating them assign it, rather than having the knowledge of the address internally.

Quoted 7 lines
hw/intc/xics_native.c new file +7 -0
+
+    for (i = 0; i < s->nr_servers; i++) {
+        object_property_set_bool(OBJECT(&s->ss[i]), true, "realized", &error);
+        if (error) {
+            error_propagate(errp, error);
+            return;
+        }

hw/intc/xics_native.c line 278

Quoted 10 lines
hw/intc/xics_native.c new file +10 -0
+    }
+}
+
+static void xics_native_class_init(ObjectClass *oc, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+    XICSStateClass *xsc = XICS_NATIVE_CLASS(oc);
+
+    dc->realize = xics_native_realize;
+    xsc->set_nr_servers = xics_set_nr_servers;

I thought you weren't going to be using the set_nr_servers stuff in the native mode xics?

Quoted 71 lines
hw/intc/xics_native.c new file +16 -0
+}
+
+static const TypeInfo xics_native_info = {
+    .name          = TYPE_XICS_NATIVE,
+    .parent        = TYPE_XICS_COMMON,
+    .instance_size = sizeof(XICSState),
+    .class_size = sizeof(XICSStateClass),
+    .class_init    = xics_native_class_init,
+    .instance_init = xics_native_initfn,
+};
+
+static void xics_native_register_types(void)
+{
+    type_register_static(&xics_native_info);
+}
+type_init(xics_native_register_types)
include/hw/ppc/xics.h +14 -0
index 93a627b..f32f409 100644
@@ -38,6 +38,9 @@
 #define TYPE_XICS_SPAPR_KVM "xics-spapr-kvm"
 #define KVM_XICS(obj) OBJECT_CHECK(KVMXICSState, (obj), TYPE_XICS_SPAPR_KVM)
 
+#define TYPE_XICS_NATIVE "xics-native"
+#define XICS_NATIVE(obj) OBJECT_CHECK(XICSState, (obj), TYPE_XICS_NATIVE)
+
 #define XICS_COMMON_CLASS(klass) \
      OBJECT_CLASS_CHECK(XICSStateClass, (klass), TYPE_XICS_COMMON)
 #define XICS_SPAPR_CLASS(klass) \
@@ -46,6 +49,8 @@
      OBJECT_GET_CLASS(XICSStateClass, (obj), TYPE_XICS_COMMON)
 #define XICS_SPAPR_GET_CLASS(obj) \
      OBJECT_GET_CLASS(XICSStateClass, (obj), TYPE_XICS_SPAPR)
+#define XICS_NATIVE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(XICSStateClass, (klass), TYPE_XICS_NATIVE)
 
 #define XICS_IPI        0x2
 #define XICS_BUID       0x1
@@ -80,6 +85,7 @@ struct XICSState {
     uint32_t nr_irqs;
     ICPState *ss;
     QLIST_HEAD(, ICSState) ics;
+    MemoryRegion icp_mmio;
 };
 
 #define TYPE_ICP "icp"
@@ -111,8 +117,13 @@ struct ICPState {
     uint8_t mfrr;
     qemu_irq output;
     bool cap_irq_xics_enabled;
+    uint32_t links[3];
 };
 
+/* This should be an XSCOM BAR ... the size is arbitrary as well */
+#define ICP_MM_BASE     0x0003FFFF80000000
+#define ICP_MM_SIZE     0x0000000010000000
+
 #define TYPE_ICS "ics"
 #define ICS(obj) OBJECT_CHECK(ICSState, (obj), TYPE_ICS)
 
@@ -181,6 +192,9 @@ void xics_spapr_free(XICSState *icp, int irq, int num);
 
 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu);
 
+void xics_create_native_icp_node(XICSState *s, void *fdt,
+                                 uint32_t base, uint32_t count);
+
 /* Internal XICS interfaces */
 int get_cpu_index_by_dt_id(int cpu_dt_id);
 
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
#104 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 38/77] ppc/xics: Add "native" XICS subclass

In reply to Benjamin Herrenschmidt
Quoted 3 lines · Benjamin Herrenschmidt

This provides MMIO based ICP access as found on POWER8

[snip]

hw/intc/xics_native.c line 248

Quoted 4 lines
hw/intc/xics_native.c new file +4 -0
+static void xics_native_realize(DeviceState *dev, Error **errp)
+{
+    XICSState *s = XICS_NATIVE(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);

Ah.. here's another wrinkle. So the current XICS is defined as a SysBusDevice, but that's not actually correct because it doesn't have MMIO. But your xics native does have MMIO, so my understanding is that SysBusDevice is appropriate. Not sure how to handle that

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
#112 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

In reply to Benjamin Herrenschmidt
Quoted 19 lines · Benjamin Herrenschmidt
Quoted 11 lines · Alexey Kardashevskiy
Quoted 8 lines · Benjamin Herrenschmidt

This adds a model of the POWER8 LPC controller. It is then used by the PowerNV code to attach a UART and RTC, which, with the right version of OPAL firmware, will provide a working console.

This version of the LPC controller model doesn't yet implement support for the SerIRQ deserializer present in the Naples version of the chip though some preliminary work is there.

Is this LPC controller one per a chip or per a machine?

Per chip but we usually only wire one up per machine.

Quoted 3 lines

In general it is quite nice when "-nodefaults" does not create neither PHB nor LPC so the user can add them manually with parameters different than defaults.

Quoted 5 lines

In this case though, PHB and LPC bridges are all part of the P8 chip, and I'm trying to represent that topology as best as possible.

I think "-nodefaults" for Pnv should only be about the devices we attach to the LPC/PHB not the busses themselves.

Exactly what is and isn't covered by -nodefaults is a bit of a mess - part of the topic of my talk at KVM Forum.

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

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
#108 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 40/77] ppc/pnv: Wire up XICS native with PowerNV platform

In reply to Benjamin Herrenschmidt
Quoted 1 line · Benjamin Herrenschmidt

Looks sound enough, but will probably need some rework due to other suggested changes in the new xics stuff.

Quoted 152 lines
---
 hw/ppc/pnv.c          | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/pnv.h  |  2 ++
 include/hw/ppc/xics.h |  2 ++
 3 files changed, 73 insertions(+)
hw/ppc/pnv.c +69 -0
index 2eac877..a7a9b0f 100644
@@ -41,6 +41,7 @@
 #include "hw/ppc/ppc.h"
 #include "hw/ppc/pnv.h"
 #include "hw/loader.h"
+#include "hw/ppc/xics.h"
 #include "hw/ppc/pnv_xscom.h"
 
 #include "exec/address-spaces.h"
@@ -81,6 +82,59 @@ struct sPowerNVMachineState {
     PnvSystem sys;
 };
 
+static XICSState *try_create_xics(const char *type, int nr_servers,
+                                  int nr_irqs, Error **errp)
+{
+    Error *err = NULL;
+    DeviceState *dev;
+
+    dev = qdev_create(NULL, type);
+    qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
+    object_property_set_bool(OBJECT(dev), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        object_unparent(OBJECT(dev));
+        return NULL;
+    }
+
+    return XICS_COMMON(dev);
+}
+
+static XICSState *xics_system_init(int nr_servers, int nr_irqs)
+{
+    XICSState *xics = NULL;
+
+#if 0 /* Some fixing needed to handle native ICS in KVM mode */
+    if (kvm_enabled()) {
+        QemuOpts *machine_opts = qemu_get_machine_opts();
+        bool irqchip_allowed = qemu_opt_get_bool(machine_opts,
+                                                "kernel_irqchip", true);
+        bool irqchip_required = qemu_opt_get_bool(machine_opts,
+                                                  "kernel_irqchip", false);
+        if (irqchip_allowed) {
+                icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs,
+                                      &error_abort);
+        }
+
+        if (irqchip_required && !icp) {
+            perror("Failed to create in-kernel XICS\n");
+            abort();
+        }
+    }
+#endif
+
+    if (!xics) {
+        xics = try_create_xics(TYPE_XICS_NATIVE, nr_servers, nr_irqs,
+                               &error_abort);
+    }
+
+    if (!xics) {
+        perror("Failed to create XICS\n");
+        abort();
+    }
+    return xics;
+}
+
 static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
                                      size_t maxsize)
 {
@@ -366,6 +420,13 @@ static void *powernv_create_fdt(PnvSystem *sys, uint32_t initrd_base, uint32_t i
 
     _FDT((fdt_end_node(fdt)));
 
+    /* ICPs */
+    CPU_FOREACH(cs) {
+        PowerPCCPU *cpu = POWERPC_CPU(cs);
+        uint32_t base_server = ppc_get_vcpu_dt_id(cpu);
+        xics_create_native_icp_node(sys->xics, fdt, base_server, smt);
+    }
+
     /* Memory */
     _FDT((powernv_populate_memory(fdt)));
 
@@ -451,11 +512,17 @@ static void ppc_powernv_init(MachineState *machine)
     MemoryRegion *ram = g_new(MemoryRegion, 1);
     sPowerNVMachineState *pnv_machine = POWERNV_MACHINE(machine);
     PnvSystem *sys = &pnv_machine->sys;
+    XICSState *xics;
     long fw_size;
     char *filename;
     void *fdt;
     int i;
 
+    /* Set up Interrupt Controller before we create the VCPUs */
+    xics = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads,
+                            XICS_IRQS_POWERNV);
+    sys->xics = xics;
+
     /* init CPUs */
     if (cpu_model == NULL) {
         cpu_model = kvm_enabled() ? "host" : "POWER8";
@@ -475,6 +542,8 @@ static void ppc_powernv_init(MachineState *machine)
         /* MSR[IP] doesn't exist nowadays */
         env->msr_mask &= ~(1 << 6);
 
+        xics_cpu_setup(xics, cpu);
+
         qemu_register_reset(powernv_cpu_reset, cpu);
     }
 
include/hw/ppc/pnv.h +2 -0
index cb157eb..80617b4 100644
@@ -21,6 +21,7 @@
 
 #include "hw/hw.h"
 typedef struct XScomBus XScomBus;
+typedef struct XICSState XICSState;
 
 /* Should we turn that into a QOjb of some sort ? */
 typedef struct PnvChip {
@@ -29,6 +30,7 @@ typedef struct PnvChip {
 } PnvChip;
 
 typedef struct PnvSystem {
+    XICSState *xics;
     uint32_t  num_chips;
 #define PNV_MAX_CHIPS		1
     PnvChip   chips[PNV_MAX_CHIPS];
include/hw/ppc/xics.h +2 -0
index 1cf7037..85d2fb9 100644
@@ -183,6 +183,8 @@ struct ICSIRQState {
 };
 
 #define XICS_IRQS_SPAPR               1024
+#define XICS_IRQS_POWERNV             (1 << 19)
+
 
 qemu_irq xics_get_qirq(XICSState *icp, int irq);
 
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
#113 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

In reply to David Gibson
Quoted 23 lines · David Gibson
Quoted 22 lines · Benjamin Herrenschmidt
Quoted 11 lines · Alexey Kardashevskiy
Quoted 8 lines · Benjamin Herrenschmidt

This adds a model of the POWER8 LPC controller. It is then used by the PowerNV code to attach a UART and RTC, which, with the right version of OPAL firmware, will provide a working console.

This version of the LPC controller model doesn't yet implement support for the SerIRQ deserializer present in the Naples version of the chip though some preliminary work is there.

Is this LPC controller one per a chip or per a machine?

Per chip but we usually only wire one up per machine.

Quoted 3 lines

In general it is quite nice when "-nodefaults" does not create neither PHB nor LPC so the user can add them manually with parameters different than defaults.

In this case though, PHB and LPC bridges are all part of the P8 chip, and I'm trying to represent that topology as best as possible.

Quoted 8 lines
Quoted 2 lines

I think "-nodefaults" for Pnv should only be about the devices we attach to the LPC/PHB not the busses themselves.

Exactly what is and isn't covered by -nodefaults is a bit of a mess - part of the topic of my talk at KVM Forum.

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

POWER8 chips all have 8 threads per core but we do not always assume -smt ...,threads=8, how are LPC or PHB different? PHB is more interesting - how is the user supposed to add more? And there always will be the default one which properties are set in a separate way (via -global, not -device). I found it sometime really annoying to debug the existing pseries which always adds a default PHB (I know, this was to make libvirt happy but this is not the case here).

Out of curiosity - if we have 2 chips, will the system work if the second chip does not get any LPC or PHB attached?

Signature · 1 line
Alexey
#114 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

Quoted 5 lines · Alexey Kardashevskiy
Quoted 2 lines

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

POWER8 chips all have 8 threads per core but we do not always assume -smt ...,threads=8, how are LPC or PHB different?

First, for pseries which is paravirtualized it's a different can of worms completely. For powernv, we *should* represent all 8 threads, we just can't yet due to TCG limitations.

Quoted 1 line

PHB is more interesting - how is the user supposed to add more?

That's an open question. Since we model a real P8 chip we can only model the PHBs as they exist on it, which is up to 3 per chip at very specific XSCOM addresses. We could try to model some non-existing P8 chip with more but bad things will happen when the FW try to assign interrupt numbers for example.

We simulate a machine that has been primed by HostBoot before OPAL starts. So we rely on what the device-tree tells us of what PHB were enabled but appart from that, we have to stick to the limitations.

Quoted 8 lines

And there always will be the default one
which properties are set in a separate way (via -global, not -device). I found it sometime really annoying to debug the existing pseries which always adds a default PHB (I know, this was to make libvirt happy but this is not the case here).

Out of curiosity - if we have 2 chips, will the system work if the second chip does not get any LPC or PHB attached?

This is something I need to look into, there's a lot of work needed to properly model "chips" that I haven't done yet, but what is there is sufficient for a lot of usages already.

Cheers,
Ben.

#115 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

Quoted 10 lines · Benjamin Herrenschmidt
Quoted 5 lines · Alexey Kardashevskiy
Quoted 2 lines

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

POWER8 chips all have 8 threads per core but we do not always assume -smt ...,threads=8, how are LPC or PHB different?

First, for pseries which is paravirtualized it's a different can of worms completely. For powernv, we *should* represent all 8 threads, we just can't yet due to TCG limitations.

Out of curiosity - for pseries we should not? I know it works with various numbers of threads but is not that because we also control guest linux kernel and, for example, the Other OS (AIX) might be upset on non-multiply-of-2 number of threads?

Quoted 18 lines
Quoted 1 line

PHB is more interesting - how is the user supposed to add more?

That's an open question. Since we model a real P8 chip we can only model the PHBs as they exist on it, which is up to 3 per chip at very specific XSCOM addresses. We could try to model some non-existing P8 chip with more but bad things will happen when the FW try to assign interrupt numbers for example.

We simulate a machine that has been primed by HostBoot before OPAL starts. So we rely on what the device-tree tells us of what PHB were enabled but appart from that, we have to stick to the limitations.

Quoted 6 lines

And there always will be the default one
which properties are set in a separate way (via -global, not -device). I found it sometime really annoying to debug the existing pseries which always adds a default PHB (I know, this was to make libvirt happy but this is not the case here).

Quoted 6 lines
Quoted 2 lines

Out of curiosity - if we have 2 chips, will the system work if the second chip does not get any LPC or PHB attached?

This is something I need to look into, there's a lot of work needed to properly model "chips" that I haven't done yet, but what is there is sufficient for a lot of usages already.

For now, if possible, I'd suggest implementing -nodefaults with no defaults whatsoever and create a config somewhere in the qemu tree to pass it via -readconfig to get reasonably configured machine so people will know what is expected to work but there will still be possibility for experiments (do not we secretly hope that other vendors will start designing/manufacturing their ppc64 chips?). It could be a config file per an actual POWER8 chip (we have two already).

Signature · 1 line
Alexey
#116 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

Quoted 8 lines · Alexey Kardashevskiy
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 5 lines · Alexey Kardashevskiy
Quoted 2 lines

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

POWER8 chips all have 8 threads per core but we do not always assume -smt ...,threads=8, how are LPC or PHB different?

Quoted 8 lines
Quoted 3 lines

First, for pseries which is paravirtualized it's a different can of worms completely. For powernv, we *should* represent all 8 threads, we just can't yet due to TCG limitations.

Out of curiosity - for pseries we should not? I know it works with various numbers of threads but is not that because we also control guest linux kernel and, for example, the Other OS (AIX) might be upset on non-multiply-of-2 number of threads?

There are several different cases here and I'm not sure which you're thinking about.

1) Guest has different number of threads-per-core than the host

This one is just fine - PAPR defines how the guest should get the number of threads from the device tree, and qemu sets that correctly.

2) Guest threads-per-core not a power of two

The PAPR thread mechanism allows this to be communicated to the guest, and I don't know if PAPR explicitly permits or prohibitis this situation. Guests could get confused by it, although that's arguably a guest bug.

2) "Partially filled core", e.g. guest has 8 threads-per-core declared but only one vcpu available

This is the only one I can see as relying on Linux guest behaviour. We kind of get away with this by accident with a Linux guest - it will try to bring up all 8 threads, but fail non fatally. We shouldn't allow this situation, although we do right now. Bharata posted a patch which would prevent this in qemu, and I have a BZ to make libvirt not allow this construction either.

Quoted 25 lines
Quoted 18 lines
Quoted 1 line

PHB is more interesting - how is the user supposed to add more?

That's an open question. Since we model a real P8 chip we can only model the PHBs as they exist on it, which is up to 3 per chip at very specific XSCOM addresses. We could try to model some non-existing P8 chip with more but bad things will happen when the FW try to assign interrupt numbers for example.

We simulate a machine that has been primed by HostBoot before OPAL starts. So we rely on what the device-tree tells us of what PHB were enabled but appart from that, we have to stick to the limitations.

Quoted 6 lines

And there always will be the default one
which properties are set in a separate way (via -global, not -device). I found it sometime really annoying to debug the existing pseries which always adds a default PHB (I know, this was to make libvirt happy but this is not the case here).

Quoted 6 lines
Quoted 2 lines

Out of curiosity - if we have 2 chips, will the system work if the second chip does not get any LPC or PHB attached?

This is something I need to look into, there's a lot of work needed to properly model "chips" that I haven't done yet, but what is there is sufficient for a lot of usages already.

Quoted 7 lines

For now, if possible, I'd suggest implementing -nodefaults with no defaults whatsoever and create a config somewhere in the qemu tree to pass it via -readconfig to get reasonably configured machine so people will know what is expected to work but there will still be possibility for experiments (do not we secretly hope that other vendors will start designing/manufacturing their ppc64 chips?). It could be a config file per an actual POWER8 chip (we have two already).

I can see some benefit to that approach, but it does stray away from current qemu practice (in general, not just compared to spapr). Hmm.. not sure.

What I do think would be a good idea is to represent a POWER8 "chip" as a instantiable qdev device, which will create the scoms and PHBs under itself as per the hardware. We can add device properties as needed to make that construction more flexible.

We probably don't want to link the number of CPUs to the chip qdevs, partly because that doesn't really fit the qemu model, but also because we'll probably want some extra flexibility. e.g. making a UP system for experimentation, even though a single chip would have multiple cores (IIUC) - SMP TCG is super slow, so we probably want that.

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
#118 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

In reply to Alexey Kardashevskiy
Quoted 5 lines · Alexey Kardashevskiy
Quoted 4 lines · Benjamin Herrenschmidt
Quoted 3 lines · Alexey Kardashevskiy
Quoted 2 lines

But on the whole I agree with you, since the LPC is part of the P8 chip, I think it makes sense to include it even with -nodefaults.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

POWER8 chips all have 8 threads per core but we do not always assume -smt ...,threads=8, how are LPC or PHB different?

First, for pseries which is paravirtualized it's a different can of worms completely. For powernv, we *should* represent all 8 threads, we just can't yet due to TCG limitations.

Out of curiosity - for pseries we should not?

Not that we should not, more like, it makes sense to offer whatever choice, it would be indeed better to have the ability to support them however.

Quoted 4 lines

I know it works with various
numbers of threads but is not that because we also control guest linux kernel and, for example, the Other OS (AIX) might be upset on non-multiply-of-2 number of threads?

Quite possibly. I have some plans to add threads support, just haven't got to it yet :-)

Quoted 26 lines
Quoted 8 lines
Quoted 2 lines

PHB is more interesting - how is the user supposed to add more?

That's an open question. Since we model a real P8 chip we can only model the PHBs as they exist on it, which is up to 3 per chip at very specific XSCOM addresses. We could try to model some non-existing P8 chip with more but bad things will happen when the FW try to assign interrupt numbers for example.

 >

Quoted 3 lines

We simulate a machine that has been primed by HostBoot before OPAL starts. So we rely on what the device-tree tells us of what PHB were enabled but appart from that, we have to stick to the limitations.

 >

Quoted 6 lines
Quoted 6 lines

And there always will be the default one
which properties are set in a separate way (via -global, not -device). I found it sometime really annoying to debug the existing pseries which always adds a default PHB (I know, this was to make libvirt happy but this is not the case here).

Quoted 6 lines
Quoted 2 lines

Out of curiosity - if we have 2 chips, will the system work if the second chip does not get any LPC or PHB attached?

This is something I need to look into, there's a lot of work needed to properly model "chips" that I haven't done yet, but what is there is sufficient for a lot of usages already.

Quoted 7 lines

For now, if possible, I'd suggest implementing -nodefaults with no defaults whatsoever and create a config somewhere in the qemu tree to pass it via -readconfig to get reasonably configured machine so people will know what is expected to work but there will still be possibility for experiments (do not we secretly hope that other vendors will start designing/manufacturing their ppc64 chips?). It could be a config file per an actual POWER8 chip (we have two already).

There could be, but we'd need ways to specify a bunch of things, it's not that trivial. IE, the xscom addresses of the 3 ranges for each PHB for example etc.. For now let's keep things simple.

I also am in no hurry to support migration with powernv, so we have quite a bit of flexibility to change things.

Cheers,
Ben.

#117 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 41/77] ppc/pnv: Add LPC controller and hook it up with a UART and RTC

In reply to David Gibson
Quoted 9 lines · David Gibson

There are several different cases here and I'm not sure which you're thinking about.

1) Guest has different number of threads-per-core than the host

This one is just fine - PAPR defines how the guest should get the number of threads from the device tree, and qemu sets that correctly.

Quoted 6 lines

2) Guest threads-per-core not a power of two

The PAPR thread mechanism allows this to be communicated to the guest, and I don't know if PAPR explicitly permits or prohibitis this situation.  Guests could get confused by it, although that's arguably a guest bug.

Linux only supports powers of 2

 .../...

Quoted 11 lines
Quoted 7 lines

For now, if possible, I'd suggest implementing -nodefaults with no defaults whatsoever and create a config somewhere in the qemu tree to pass it via -readconfig to get reasonably configured machine so people will know what is expected to work but there will still be possibility for experiments (do not we secretly hope that other vendors will start designing/manufacturing their ppc64 chips?). It could be a config file per an actual POWER8 chip (we have two already).

I can see some benefit to that approach, but it does stray away from current qemu practice (in general, not just compared to spapr). Hmm.. not sure.

I don't see an urgent need. We can do that in a separate series if we want to. As I said in another email, I don't want powernv to be "set in stone" in term of ABIs & migration for a while, so we have latitude to play with the model for a while before we lock things down. At least until after P9.

Quoted 4 lines

What I do think would be a good idea is to represent a POWER8 "chip" as a instantiable qdev device, which will create the scoms and PHBs under itself as per the hardware.  We can add device properties as needed to make that construction more flexible.

It would also instanciate the CPUs. I want to change that in the current model, just haven't got to it yet.

Quoted 5 lines

We probably don't want to link the number of CPUs to the chip qdevs, partly because that doesn't really fit the qemu model, but also because we'll probably want some extra flexibility.  e.g. making a UP system for experimentation, even though a single chip would have multiple cores (IIUC) - SMP TCG is super slow, so we probably want that.

We want to eventually represent the EX units properly on XSCOM (the cores). So we will need that tie between chip and CPU. But we don't have to mark all cores in a chip as "enabled". It's ok to have chips with only one good core for example.

Cheers,
Ben.

#175 lore raw
In reply to Cédric Le Goater
Quoted 17 lines · Cédric Le Goater
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

It is a flash image, we could use an Open Power machine flash image "as-is" provided we taught qemu to extract skiboot (aka OPAL) from it.

Couldn't we add an offset argument to load_image_targphys() or make that an extra routine ? If so, we could then load directly from an openpower pnor file.

I gave it a quick (and dirty) try and a powernv guest runs fine up to petitboot with just :

Quoted 6 lines

qemu-system-ppc64 -m 2G -M powernv -bios ~/work/open-power/images/palmetto.pnor -nographic -nodefaults -serial stdio

The pnor file is compiled from github. The patch is below (without the dirty cut and paste I did in loader.c). The offset for the PAYLOAD and BOOTKERNEL partitions are hard coded but I guess we don't need to read the flash partition table in qemu, not yet.

One downside to this is that if we don't fall back to being able to load skiboot.lid it becomes more annoying to boot a gcov enabled skiboot as typical PNOR layout only gives 1MB for skiboot, and gcov builds bloat that a *lot*.

We probably don't want NVRAM writes going back to a single system wide PNOR image too, so while using pnor file is great for simulating what hardware does, may not work as the solution for long term model.

#176 lore raw
Quoted 25 lines · Stewart Smith
Quoted 17 lines · Cédric Le Goater
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

How does real hardware store petitboot? If it's flash, you could pass it in using -pflash and thus model things even more closely and allow users to just take the ROM image as is.

It is a flash image, we could use an Open Power machine flash image "as-is" provided we taught qemu to extract skiboot (aka OPAL) from it.

Couldn't we add an offset argument to load_image_targphys() or make that an extra routine ? If so, we could then load directly from an openpower pnor file.

I gave it a quick (and dirty) try and a powernv guest runs fine up to petitboot with just :

Quoted 6 lines

qemu-system-ppc64 -m 2G -M powernv -bios ~/work/open-power/images/palmetto.pnor -nographic -nodefaults -serial stdio

The pnor file is compiled from github. The patch is below (without the dirty cut and paste I did in loader.c). The offset for the PAYLOAD and BOOTKERNEL partitions are hard coded but I guess we don't need to read the flash partition table in qemu, not yet.

Quoted 4 lines

One downside to this is that if we don't fall back to being able to load skiboot.lid it becomes more annoying to boot a gcov enabled skiboot as typical PNOR layout only gives 1MB for skiboot, and gcov builds bloat that a *lot*.

I guess that what we can imagine having a bigger partition for skiboot in the case of gcov ? This will require a custom pnor build. Might be too complex. Or we could use a -pflash option to load the pnor and an optional -bios for skiboot if we want a custom one.

Quoted 3 lines

We probably don't want NVRAM writes going back to a single system wide PNOR image too, so while using pnor file is great for simulating what hardware does, may not work as the solution for long term model.

we can use a memory backend to start with, which is also much simpler than having to handle the block backend like the cfi pflash is doing. a guest could use its own private pnor if a block backend is needed.

C.

#179 lore raw

Re: [Qemu-devel] [PATCH 71/77] ppc: Add dummy ACOP SPR

In reply to Benjamin Herrenschmidt
Quoted 23 lines · Benjamin Herrenschmidt
---
 target-ppc/cpu.h            | 1 +
 target-ppc/translate_init.c | 4 ++++
 2 files changed, 5 insertions(+)
target-ppc/cpu.h +1 -0
index bf8892a..aa328a7 100644
@@ -1361,6 +1361,7 @@ static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 #define SPR_SRR1              (0x01B)
 #define SPR_CFAR              (0x01C)
 #define SPR_AMR               (0x01D)
+#define SPR_ACOP              (0x01F)
 #define SPR_BOOKE_PID         (0x030)
 #define SPR_BOOKE_DECAR       (0x036)
 #define SPR_BOOKE_CSRR0       (0x03A)
target-ppc/translate_init.c +0 -0
index b5fd076..4ec532c 100644

target-ppc/translate_init.c static void gen_spr_power8_book4(CPUPPCState *env) · line 8234

Quoted 9 lines
target-ppc/translate_init.c +4 -0
@@ -8226,6 +8226,10 @@ static void gen_spr_power8_book4(CPUPPCState *env)
                     &spr_read_generic, SPR_NOACCESS,
                     &spr_read_generic, &spr_write_generic,
                     0);
+    spr_register(env, SPR_ACOP, "ACOP",
+                 SPR_NOACCESS, SPR_NOACCESS,
+                 &spr_read_generic, &spr_write_generic,
+                 0);
 #endif
 }

I think this patch should be merged with the next one (where you change the ACOP hunk in translate_init.c again.

Thomas

#181 lore raw

Re: [Qemu-devel] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

In reply to Benjamin Herrenschmidt
Quoted 44 lines · Benjamin Herrenschmidt

WORT and PID this time

---
 target-ppc/cpu.h            |  2 ++
 target-ppc/translate_init.c | 16 ++++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
target-ppc/cpu.h +2 -0
index aa328a7..6179fbc 100644
@@ -1363,6 +1363,7 @@ static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 #define SPR_AMR               (0x01D)
 #define SPR_ACOP              (0x01F)
 #define SPR_BOOKE_PID         (0x030)
+#define SPR_BOOKS_PID         (0x030)
 #define SPR_BOOKE_DECAR       (0x036)
 #define SPR_BOOKE_CSRR0       (0x03A)
 #define SPR_BOOKE_CSRR1       (0x03B)
@@ -1716,6 +1717,7 @@ static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 #define SPR_POWER_SPMC1       (0x37C)
 #define SPR_POWER_SPMC2       (0x37D)
 #define SPR_POWER_MMCRS       (0x37E)
+#define SPR_WORT              (0x37F)
 #define SPR_PPR               (0x380)
 #define SPR_750_GQR0          (0x390)
 #define SPR_440_DNV0          (0x390)
target-ppc/translate_init.c +4 -4
index 4ec532c..bfdf028 100644
@@ -8226,10 +8226,18 @@ static void gen_spr_power8_book4(CPUPPCState *env)
                     &spr_read_generic, SPR_NOACCESS,
                     &spr_read_generic, &spr_write_generic,
                     0);
-    spr_register(env, SPR_ACOP, "ACOP",
-                 SPR_NOACCESS, SPR_NOACCESS,
-                 &spr_read_generic, &spr_write_generic,
-                 0);
+    spr_register_kvm(env, SPR_ACOP, "ACOP",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_ACOP, 0);

target-ppc/translate_init.c static void gen_spr_power8_book4(CPUPPCState *env) · line 8242

Quoted 10 lines
target-ppc/translate_init.c +8 -0
+    spr_register_kvm(env, SPR_BOOKS_PID, "PID",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_PID, 0);
+    spr_register_kvm(env, SPR_WORT, "WORT",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_WORT, 0);
 #endif
 }

AFAICT all patches where you define new SPRs with spr_register_kvm[_hv] are also important independently of the rest of your patch series - otherwise these registers are currently lost during migration since they are not sync'ed with the KVM part in the kernel right now.

So if you've got some spare time, could you maybe extract all those patches that define new SPRs with spr_register_kvm[_hv] and send them as a separate patch series? That could help to fix future migration issues, and also would decrease the size of your really huge "Add native POWER8 platform" patch series a little bit!

Thanks,
Thomas

#182 lore raw

Re: [Qemu-devel] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 5 lines · Thomas Huth

So if you've got some spare time, could you maybe extract all those patches that define new SPRs with spr_register_kvm[_hv] and send them as a separate patch series? That could help to fix future migration issues, and also would decrease the size of your really huge "Add native POWER8 platform" patch series a little bit!

Time is the problem :-) My tree is bitrotting right now, I am completely caught up with a few other things.

I'm trying to get somebody to pick up that work.

Cheers,
Ben.

#183 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

In reply to Thomas Huth
Quoted 61 lines · Thomas Huth
Quoted 44 lines · Benjamin Herrenschmidt

WORT and PID this time

---
 target-ppc/cpu.h            |  2 ++
 target-ppc/translate_init.c | 16 ++++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
target-ppc/cpu.h +2 -0
index aa328a7..6179fbc 100644
@@ -1363,6 +1363,7 @@ static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 #define SPR_AMR               (0x01D)
 #define SPR_ACOP              (0x01F)
 #define SPR_BOOKE_PID         (0x030)
+#define SPR_BOOKS_PID         (0x030)
 #define SPR_BOOKE_DECAR       (0x036)
 #define SPR_BOOKE_CSRR0       (0x03A)
 #define SPR_BOOKE_CSRR1       (0x03B)
@@ -1716,6 +1717,7 @@ static inline int cpu_mmu_index (CPUPPCState *env, bool ifetch)
 #define SPR_POWER_SPMC1       (0x37C)
 #define SPR_POWER_SPMC2       (0x37D)
 #define SPR_POWER_MMCRS       (0x37E)
+#define SPR_WORT              (0x37F)
 #define SPR_PPR               (0x380)
 #define SPR_750_GQR0          (0x390)
 #define SPR_440_DNV0          (0x390)
target-ppc/translate_init.c +4 -4
index 4ec532c..bfdf028 100644
@@ -8226,10 +8226,18 @@ static void gen_spr_power8_book4(CPUPPCState *env)
                     &spr_read_generic, SPR_NOACCESS,
                     &spr_read_generic, &spr_write_generic,
                     0);
-    spr_register(env, SPR_ACOP, "ACOP",
-                 SPR_NOACCESS, SPR_NOACCESS,
-                 &spr_read_generic, &spr_write_generic,
-                 0);
+    spr_register_kvm(env, SPR_ACOP, "ACOP",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_ACOP, 0);

target-ppc/translate_init.c static void gen_spr_power8_book4(CPUPPCState *env) · line 8242

Quoted 10 lines
target-ppc/translate_init.c +8 -0
+    spr_register_kvm(env, SPR_BOOKS_PID, "PID",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_PID, 0);
+    spr_register_kvm(env, SPR_WORT, "WORT",
+                     SPR_NOACCESS, SPR_NOACCESS,
+                     &spr_read_generic, &spr_write_generic,
+                     KVM_REG_PPC_WORT, 0);
 #endif
 }

AFAICT all patches where you define new SPRs with spr_register_kvm[_hv] are also important independently of the rest of your patch series - otherwise these registers are currently lost during migration since they are not sync'ed with the KVM part in the kernel right now.

Quoted 5 lines

So if you've got some spare time, could you maybe extract all those patches that define new SPRs with spr_register_kvm[_hv] and send them as a separate patch series? That could help to fix future migration issues, and also would decrease the size of your really huge "Add native POWER8 platform" patch series a little bit!

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?). That would remove 15/20 patches from the list below I believe. May be more. I will go through the email thread to see what can be taken.

Would follow the xics patches (plenty of conflicts lately), mmu patches (but David has been working on this so it is shrinking), some pci fixes and helpers (should ping maintainers). We would then be at a point where only the core pnv patches are left. I think I am being very optimistic :)

I can start with a first serie. Suggestions welcomed. List below.

065f7f9b1e15 ppc: Fix CFAR updates
f385d52fe9f2 ppc: Add slbfee. instruction
2bf0d4f4239a ppc: Add dummy logmpp instruction
8102a4cc9c30 ppc: Print HSRR0/HSRR1 in "info registers"
730a9b4dc941 ppc: Add KVM numbers to some P8 SPRs
48adf38e9cab ppc: A couple more dummy POWER8 Book4 regs
bce7c738ec1b ppc: Add dummy ACOP SPR
34f1af75e75e ppc: Add dummy CIABR SPR
64f92045e284 ppc: Add dummy POWER8 PSPB SPR
ab003604993b ppc: Add dummy POWER8 MPPR register
b81cf8c41288 ppc: Add dummy write to VTB
490af9c31d8b ppc: Add a few more P8 PMU SPRs
84886e8c188d ppc: Add POWER8 IAMR register
5046c10f9e58 ppc: Fix writing to AMR/UAMOR
c188b86cac90 ppc: Initialize AMOR in PAPR mode
ee62b5581094 ppc: Add dummy SPR_IC for POWER8
83e02b2026ef ppc: SPURR & PURR are HV writeable and privileged
906a513acb1a ppc: LPCR is a HV resource
27f45e80a104 ppc: Add placeholder SPRs for DPDES and DHDES on P8
c1fc6d316f87 ppc: Initial HDEC support
9f5d6c607af5 ppc: Enforce setting MSR:EE,IR and DR when MSR:PR is set
a356d76a11bb ppc: Fix conditions for delivering external interrupts to a guest 6ba5e576d1cc ppc/pnv+spapr: Add "ibm,pa-features" property to the device-tree cf768bc434e5 ppc: Fix 64K pages support in full emulation
dbd6e54fd9c5 ppc: Add proper real mode translation support
7250469a21ce ppc: Cosmetic, align some comments
d94d8c4e7b3f ppc: Use a helper to filter writes to LPCR
0694e0d87fe3 ppc: Update LPCR definitions
20596a008f27 ppc/pnv: Create a default PCI layout
4b393cbdb438 ppc/pnv: Add model for Power8 PHB3 PCIe Host bridge
fa93c24cd3dc pci: Don't call pci_irq_handler() for a negative intx
8872d909a8fb pci: Use the new pci_can_add_device() to enforce devfn_min/max 9204957062b8 qdev: Add a hook for a bus to device if it can add devices 00b55e345926 pci-bridge: Set a supported devfn_min for bridge
e53d84a2716a ppc/pnv: Add OCC model stub with interrupt support
0b6c38406589 ppc/pnv: Add cut down PSI bridge model and hookup external interrupt 2649daef3f73 ppc/pnv: Add LPC controller and hook it up with a UART and RTC 81fff5adcc87 ppc/pnv: Wire up XICS native with PowerNV platform
aed1f862f0cc ppc/xics: Add xics to the monitor "info pic" command
8a9d65a58764 ppc/xics: Add "native" XICS subclass
2f29b84050d0 ppc/xics: Split ICS into base class and "simple" implementation f16e1abac820 ppc/xics: Use a helper to add a new ICS
8311defd2daa ppc/xics: Move xics_set_nr_irqs() to xics_spapr.c and xics_kvm.c 80ff29cbe02c ppc/xics: An ICS with offset 0 is assumed to be uninitialized b051717058cd ppc/xics: Make the ICSState a list
c1498ed62ab9 ppc/xics: Replace "icp" with "xics" in most places
de147c5277ee ppc/xics: Remove unused xics_set_irq_type()
e8322cdda768 ppc/xics: Implement H_IPOLL using an accessor
14597dd4d7c5 ppc/xics: Move SPAPR specific code to a separate file
ea78b4729fe7 ppc/xics: Rename existing XICS classe to XICS_SPAPR
b68322388931 ppc/pnv: Add XSCOM infrastructure
e567fa64df0a ppc/pnv: Add skeletton PowerNV platform
a926d8369e26 ppc: Add P7/P8 Power Management instructions
bba3bf8362cf ppc: Move exception generation code out of line
15411d6de866 ppc: Turn a bunch of booleans from int to bool
554cf2e208d4 ppc: Add real mode CI load/store instructions for P7 and P8 e75b02f8786e ppc: Rework generation of priv and inval interrupts
ec7fde6eb1a6 ppc: Fix generation if ISI/DSI vs. HV mode
91d6d6b8c720 ppc: Fix POWER7 and POWER8 exception definitions
886802ba3a7b ppc: Rework POWER7 & POWER8 exception model
72a37f87f790 ppc: Add PPC_64H instruction flag to POWER7 and POWER8
036b3623c02d ppc: Get out of emulation on SMT "OR" ops
5bf1a7bf1e45 ppc: Fix sign extension issue in mtmsr(d) emulation
fbac2f43e655 ppc: Change 'invalid' bit mask of tlbiel and tlbie
f308c5b1006f ppc: tlbie, tlbia and tlbisync are HV only
317658992d97 ppc: Better figure out if processor has HV mode
e25b26973a4b ppc: Create cpu_ppc_set_papr() helper
14bffe9681df ppc: Fix rfi/rfid/hrfi/... emulation
fb21ea327c61 ppc: Fix hreg_store_msr() so that non-HV mode cannot alter MSR:HV 35fd16185bea ppc: Add number of threads per core to the processor definition e408897526e9 ppc: Add a bunch of hypervisor SPRs to Book3s
5b287e66c751 ppc: Add macros to register hypervisor mode SPRs
295ccb0507dc ppc: Update SPR definitions
ed011d4fb179 ppc: Do some batching of TCG tlb flushes
048a18af1c8d ppc: Use split I/D mmu modes to avoid flushes on interrupts 681fc8d20391 ppc: Remove MMU_MODEn_SUFFIX definitions
065f7f9b1e15 ppc: Fix CFAR updates
2d3b7c0164e1 Merge remote-tracking branch 'remotes/amit-virtio-rng/tags/rng-for-2.6-1' into staging

Quoted 1 line

From :

https://github.com/legoater/qemu/commits/powernv-ipmi

$ git diff 2d3b7c0164e1..065f7f9b1e15  | diffstat 
 default-configs/ppc64-softmmu.mak   |    5 
 hmp-commands-info.hx                |    2 
 hw/intc/Makefile.objs               |    2 
 hw/intc/xics.c                      |  727 +++++++-----------------
 hw/intc/xics_kvm.c                  |   91 +--
 hw/intc/xics_native.c               |  295 +++++++++
 hw/intc/xics_spapr.c                |  429 ++++++++++++++
 hw/pci-bridge/pci_bridge_dev.c      |    2 
 hw/pci-host/Makefile.objs           |    2 
 hw/pci-host/pnv_phb3.c              | 1084 ++++++++++++++++++++++++++++++++++++
 hw/pci-host/pnv_phb3_msi.c          |  354 +++++++++++
 hw/pci-host/pnv_phb3_pbcq.c         |  315 ++++++++++
 hw/pci-host/pnv_phb3_rc.c           |  130 ++++
 hw/pci/pci.c                        |   26 
 hw/ppc/Makefile.objs                |    2 
 hw/ppc/pnv.c                        |  864 ++++++++++++++++++++++++++++
 hw/ppc/pnv_lpc.c                    |  527 +++++++++++++++++
 hw/ppc/pnv_occ.c                    |  126 ++++
 hw/ppc/pnv_psi.c                    |  594 +++++++++++++++++++
 hw/ppc/pnv_xscom.c                  |  415 +++++++++++++
 hw/ppc/ppc.c                        |   31 -
 hw/ppc/spapr.c                      |   28 
 hw/ppc/spapr_events.c               |    8 
 hw/ppc/spapr_hcall.c                |   14 
 hw/ppc/spapr_pci.c                  |   12 
 hw/ppc/spapr_vio.c                  |    2 
 include/hw/pci-host/pnv_phb3.h      |  145 ++++
 include/hw/pci-host/pnv_phb3_regs.h |  505 ++++++++++++++++
 include/hw/pci-host/spapr.h         |    2 
 include/hw/pci/pci_bus.h            |    1 
 include/hw/ppc/pnv.h                |   67 ++
 include/hw/ppc/pnv_xscom.h          |   73 ++
 include/hw/ppc/ppc.h                |    2 
 include/hw/ppc/spapr.h              |    2 
 include/hw/ppc/spapr_vio.h          |    2 
 include/hw/ppc/xics.h               |   81 ++
 include/hw/qdev-core.h              |    1 
 linux-user/main.c                   |    1 
 monitor.c                           |    3 
 qdev-monitor.c                      |   13 
 target-ppc/cpu-qom.h                |    1 
 target-ppc/cpu.h                    |  148 ++++
 target-ppc/excp_helper.c            |  369 +++++++-----
 target-ppc/helper.h                 |    6 
 target-ppc/helper_regs.h            |   75 ++
 target-ppc/machine.c                |    5 
 target-ppc/mmu-hash32.c             |    4 
 target-ppc/mmu-hash64.c             |  344 +++++++++--
 target-ppc/mmu-hash64.h             |    1 
 target-ppc/mmu_helper.c             |   17 
 target-ppc/timebase_helper.c        |   10 
 target-ppc/translate.c              | 1013 ++++++++++++++++++---------------
 target-ppc/translate_init.c         |  738 ++++++++++++++++++++++--
 53 files changed, 8350 insertions(+), 1366 deletions(-)

Thanks,

C.

#184 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 4 lines · Cédric Le Goater
Quoted 3 lines · Thomas Huth
Quoted 2 lines · Benjamin Herrenschmidt

WORT and PID this time

...

Quoted 11 lines
Quoted 10 lines

AFAICT all patches where you define new SPRs with spr_register_kvm[_hv] are also important independently of the rest of your patch series - otherwise these registers are currently lost during migration since they are not sync'ed with the KVM part in the kernel right now.

So if you've got some spare time, could you maybe extract all those patches that define new SPRs with spr_register_kvm[_hv] and send them as a separate patch series? That could help to fix future migration issues, and also would decrease the size of your really huge "Add native POWER8 platform" patch series a little bit!

Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

Thomas

#185 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 1 line · Thomas Huth

On 09.03.2016 21:04, Cédric Le Goater wrote:

....

Quoted 19 lines
Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

Quoted 8 lines
5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

FWIW, I just saw today (by doing some more experiments with kvm-unit-tests) that the IAMR register is also not migrated yet ... so it would be nice if you could include the related patches for IAMR, too, and wire the KVM part up with KVM_REG_PPC_IAMR...

Thomas

#186 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 27 lines · Thomas Huth
Quoted 1 line · Thomas Huth

On 09.03.2016 21:04, Cédric Le Goater wrote:

....

Quoted 24 lines
Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs
Quoted 8 lines
Quoted 3 lines

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

FWIW, I just saw today (by doing some more experiments with kvm-unit-tests) that the IAMR register is also not migrated yet ... so it would be nice if you could include the related patches for IAMR, too, and wire the KVM part up with KVM_REG_PPC_IAMR...

OK. So we should be targeting something like :

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s

ppc: LPCR is a HV resource
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add a few more P8 PMU SPRs
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy POWER8 PSPB SPR
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Also, there seem to be an issue with qemu's HEAD on ppc64el with the random device :

-object rng-random,filename=/dev/urandom,id=gid0 -device spapr-rng,rng=gid0

qemu "hangs". This is a vague description for a symptom ... Does that ring a bell or do I need to dig in to get more info ?

Thanks,

C.

#187 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 43 lines · Cédric Le Goater
Quoted 27 lines · Thomas Huth
Quoted 1 line · Thomas Huth

On 09.03.2016 21:04, Cédric Le Goater wrote:

....

Quoted 24 lines
Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs
Quoted 8 lines
Quoted 3 lines

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

FWIW, I just saw today (by doing some more experiments with kvm-unit-tests) that the IAMR register is also not migrated yet ... so it would be nice if you could include the related patches for IAMR, too, and wire the KVM part up with KVM_REG_PPC_IAMR...

OK. So we should be targeting something like :

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s

Quoted 14 lines

ppc: LPCR is a HV resource
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add a few more P8 PMU SPRs
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy POWER8 PSPB SPR
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Sounds good - but you likely can drop the "Add a few more P8 PMU SPRs" from your list since it has already been queued by David already (see
https://github.com/dgibson/qemu/commits/ppc-for-2.6), and the PSPB patch is also not required anymore since I submitted a similar patch to David already when I discovered that it is lost during migration.

Quoted 7 lines

Also, there seem to be an issue with qemu's HEAD on ppc64el with the random device :

-object rng-random,filename=/dev/urandom,id=gid0 -device spapr-rng,rng=gid0

qemu "hangs". This is a vague description for a symptom ... Does that ring a bell or do I need to dig in to get more info ?

Works for me™ ... could you supply more information? Where does it hang? Which exact level of QEMU are you using? ... and please open a new mail thread for this, since it's off-topic to this mail thread.

Thomas

#188 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 59 lines · Thomas Huth
Quoted 43 lines · Cédric Le Goater
Quoted 27 lines · Thomas Huth
Quoted 1 line · Thomas Huth

On 09.03.2016 21:04, Cédric Le Goater wrote:

....

Quoted 24 lines
Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs
Quoted 8 lines
Quoted 3 lines

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

FWIW, I just saw today (by doing some more experiments with kvm-unit-tests) that the IAMR register is also not migrated yet ... so it would be nice if you could include the related patches for IAMR, too, and wire the KVM part up with KVM_REG_PPC_IAMR...

OK. So we should be targeting something like :

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s

Quoted 14 lines

ppc: LPCR is a HV resource
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add a few more P8 PMU SPRs
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy POWER8 PSPB SPR
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Quoted 5 lines

Sounds good - but you likely can drop the "Add a few more P8 PMU SPRs" from your list since it has already been queued by David already (see
https://github.com/dgibson/qemu/commits/ppc-for-2.6), and the PSPB patch is also not required anymore since I submitted a similar patch to David already when I discovered that it is lost during migration.

Here is a first port on Dave's 2.6 branch. I tried to keep the patchset minimal but I had to pull a few extra patches to keep them more or less in sync with the original version from Ben.

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s
ppc: Add number of threads per core to the processor definition
ppc: Fix hreg_store_msr() so that non-HV mode cannot alter MSR:HV
ppc: Create cpu_ppc_set_papr() helper
ppc: Better figure out if processor has HV mode
ppc: Add placeholder SPRs for DPDES and DHDES on P8
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Available here:

https://github.com/legoater/qemu/commits/for-2.6

If you want to take a look, I did some quick test on KVM and TCG on a ppc64le ubuntu host but no migration.

These two will probably need a merge as the first breaks the compile, and I modified the second.

ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register

I guess that I will send on the list for review after.

Thanks,

C.

#189 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 94 lines · Cédric Le Goater
Quoted 59 lines · Thomas Huth
Quoted 43 lines · Cédric Le Goater
Quoted 27 lines · Thomas Huth
Quoted 1 line · Thomas Huth

On 09.03.2016 21:04, Cédric Le Goater wrote:

....

Quoted 24 lines
Quoted 4 lines

I have been maintaining a port of Ben's patchset on the latest qemu for other parts which should come after pnv is merged so I have a framework to test such sub-patchsets. I also have time to work on them but clearly not the expertise in all areas !

That would be great if you could take care of this!

Quoted 4 lines

What would be nice is to identify the most obvious ones, non controversial that could be merged after a few iterations. I have a vague idea, the ones Reviewed-by David obviously being good candidates, the definition of new SPRs (even the dummy ones ?).

I really like to see the KVM SPRs patches first - since they are fixing potential problems with migration of the _current_ KVM machines already! And being bug fixes, maybe these patches could even be included for QEMU 2.6 already? (i.e. before the hard freeze at the end of March)

So my wish-list for a first small patch series looks like this:

5b287e66c7513209  ppc: Add macros to register hypervisor mode SPRs
34f1af75e75e7ba0  ppc: Add dummy CIABR SPR
48adf38e9cab4663  ppc: A couple more dummy POWER8 Book4 regs
730a9b4dc9414818  ppc: Add KVM numbers to some P8 SPRs
Quoted 8 lines
Quoted 3 lines

There are a couple of other patches touching the SPRs initialization, but they are not important with regards to migration... so not sure whether it makes sense to include them now already...

FWIW, I just saw today (by doing some more experiments with kvm-unit-tests) that the IAMR register is also not migrated yet ... so it would be nice if you could include the related patches for IAMR, too, and wire the KVM part up with KVM_REG_PPC_IAMR...

OK. So we should be targeting something like :

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s

Quoted 14 lines

ppc: LPCR is a HV resource
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add a few more P8 PMU SPRs
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy POWER8 PSPB SPR
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Quoted 5 lines

Sounds good - but you likely can drop the "Add a few more P8 PMU SPRs" from your list since it has already been queued by David already (see
https://github.com/dgibson/qemu/commits/ppc-for-2.6), and the PSPB patch is also not required anymore since I submitted a similar patch to David already when I discovered that it is lost during migration.

Here is a first port on Dave's 2.6 branch. I tried to keep the patchset minimal but I had to pull a few extra patches to keep them more or less in sync with the original version from Ben.

ppc: Update SPR definitions
ppc: Add macros to register hypervisor mode SPRs
ppc: Add a bunch of hypervisor SPRs to Book3s
ppc: Add number of threads per core to the processor definition
ppc: Fix hreg_store_msr() so that non-HV mode cannot alter MSR:HV
ppc: Create cpu_ppc_set_papr() helper
ppc: Better figure out if processor has HV mode
ppc: Add placeholder SPRs for DPDES and DHDES on P8
ppc: SPURR & PURR are HV writeable and privileged
ppc: Add dummy SPR_IC for POWER8
ppc: Initialize AMOR in PAPR mode
ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register
ppc: Add dummy write to VTB
ppc: Add dummy POWER8 MPPR register
ppc: Add dummy CIABR SPR
ppc: Add dummy ACOP SPR
ppc: A couple more dummy POWER8 Book4 regs
ppc: Add KVM numbers to some P8 SPRs

Available here:

https://github.com/legoater/qemu/commits/for-2.6

Quoted 8 lines

If you want to take a look, I did some quick test on KVM and TCG on a ppc64le ubuntu host but no migration.

These two will probably need a merge as the first breaks the compile, and I modified the second.

ppc: Fix writing to AMR/UAMOR
ppc: Add POWER8 IAMR register

Modifying patches is fine as long as you mention the changes in the patch description (in square brackets) and add your Sob - looking at your tree, you already did this for the "ppc: Add POWER8 IAMR register" patch, so that should be OK.

I'd maybe also squash the "Add KVM numbers to some P8 SPRs" patch into the "Add a bunch of hypervisor SPRs to Book3s" to reduce the code churn a little bit (note that the patch description for the "Add a bunch of hypervisor SPRs to Book3s" patch is misleading - DAWR and DAWRX need to be synchronized for migration, since the guest can set this register via the H_SET_MODE hypercall).

You could maybe also merge "Add dummy ACOP SPR" with "A couple more dummy POWER8 Book4 regs" since the latter patch completely reworks the definition of the ACOP register.

Thomas

#190 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Hello Thomas,

Quoted 6 lines · Thomas Huth

I'd maybe also squash the "Add KVM numbers to some P8 SPRs" patch into the "Add a bunch of hypervisor SPRs to Book3s" to reduce the code churn a little bit (note that the patch description for the "Add a bunch of hypervisor SPRs to Book3s" patch is misleading - DAWR and DAWRX need to be synchronized for migration, since the guest can set this register via the H_SET_MODE hypercall).

So shall I just remove the comment :

"We don't give them a KVM reg number yet as no current KVM version
supports HV mode."

from patch "Add a bunch of hypervisor SPRs to Book3s" after squashing in "Add KVM numbers to some P8 SPRs" ?

Thanks,

C.

#191 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 10 lines · Cédric Le Goater

Hello Thomas,

Quoted 6 lines · Thomas Huth

I'd maybe also squash the "Add KVM numbers to some P8 SPRs" patch into the "Add a bunch of hypervisor SPRs to Book3s" to reduce the code churn a little bit (note that the patch description for the "Add a bunch of hypervisor SPRs to Book3s" patch is misleading - DAWR and DAWRX need to be synchronized for migration, since the guest can set this register via the H_SET_MODE hypercall).

Quoted 7 lines

So shall I just remove the comment :

"We don't give them a KVM reg number yet as no current KVM version
supports HV mode."

from patch "Add a bunch of hypervisor SPRs to Book3s" after squashing in "Add KVM numbers to some P8 SPRs" ?

Either that, or change it into something like:

"We don't give them a KVM reg number to most of the registers yet as no current KVM version supports HV mode. For DAWR and DAWRX, the KVM reg number is needed since this register can be set by the guest via the H_SET_MODE hypercall."

?

Thomas

#192 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 72/77] ppc: A couple more dummy POWER8 Book4 regs

Quoted 19 lines · Thomas Huth
Quoted 10 lines · Cédric Le Goater

Hello Thomas,

Quoted 6 lines · Thomas Huth

I'd maybe also squash the "Add KVM numbers to some P8 SPRs" patch into the "Add a bunch of hypervisor SPRs to Book3s" to reduce the code churn a little bit (note that the patch description for the "Add a bunch of hypervisor SPRs to Book3s" patch is misleading - DAWR and DAWRX need to be synchronized for migration, since the guest can set this register via the H_SET_MODE hypercall).

Quoted 7 lines

So shall I just remove the comment :

"We don't give them a KVM reg number yet as no current KVM version
supports HV mode."

from patch "Add a bunch of hypervisor SPRs to Book3s" after squashing in "Add KVM numbers to some P8 SPRs" ?

Quoted 6 lines

Either that, or change it into something like:

"We don't give them a KVM reg number to most of the registers yet as no current KVM version supports HV mode. For DAWR and DAWRX, the KVM reg number is needed since this register can be set by the guest via the H_SET_MODE hypercall."

OK. Good. I should be sending a first patchset soon the list, after a few checks.

Thanks,

C.

#134 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 48/77] ppc/pnv: Add model for Power8 PHB3 PCIe Host bridge

In reply to Benjamin Herrenschmidt

Ben,

Quick question,

Quoted 48 lines
hw/pci-host/pnv_phb3.c new file +48 -0
+/* This is called whenever the PHB LSI, MSI source ID register or
+ * the PBCQ irq filters are written.
+ */
+void pnv_phb3_remap_irqs(PnvPhb3State *phb)
+{
+    uint32_t local, global, count, mask, comp;
+    uint64_t baren;
+
+    /* First check if we are enabled. Unlike real HW we don't separate TX and RX
+     * so we enable if both are set
+     */
+    baren = phb->pbcq->nest_regs[PBCQ_NEST_BAR_EN];
+    if (!(baren & PBCQ_NEST_BAR_EN_IRSN_RX) ||
+        !(baren & PBCQ_NEST_BAR_EN_IRSN_TX)) {
+        phb->lsi_ics->offset = 0;
+        return;
+    }
+
+    /* Grab local LSI source ID */
+    local = GETFIELD(PHB_LSI_SRC_ID, phb->regs[PHB_LSI_SOURCE_ID >> 3]) << 3;
+
+    /* Grab global one and compare */
+    global = GETFIELD(PBCQ_NEST_LSI_SRC,
+                      phb->pbcq->nest_regs[PBCQ_NEST_LSI_SRC_ID]) << 3;
+    if (global != local) {
+        /* This happens during initialization, let's come back when we
+         * are properly configured
+         */
+        phb->lsi_ics->offset = 0;
+        return;
+    }
+
+    /* Get the base on the powerbus */
+    comp = GETFIELD(PBCQ_NEST_IRSN_COMP,
+                    phb->pbcq->nest_regs[PBCQ_NEST_IRSN_COMPARE]);
+    mask = GETFIELD(PBCQ_NEST_IRSN_COMP,
+                    phb->pbcq->nest_regs[PBCQ_NEST_IRSN_MASK]);
+    count = ((~mask) + 1) & 0x7ffff;
+    phb->total_irq = count;
+
+    /* Sanity checks */
+    if ((global + 8) > count) {
+        DBG_MAP(phb, "LSIs out of reach: LSI base=%d total irq=%d",
+                global, count);
+    }
+    if (count > 2048) {
+        DBG_MAP(phb, "More interrupts than supported: %d", count);
+    }

hw/pci-host/pnv_phb3.c line 420

Quoted 10 lines
hw/pci-host/pnv_phb3.c new file +10 -0
+    if ((comp & mask) != comp) {
+        DBG_MAP(phb, "IRQ compare bits not in mask: comp=0x%x mask=0x%x",
+                comp, mask);
+        comp &= mask;
+    }
+    /* Setup LSI offset */
+    phb->lsi_ics->offset = comp + global;
+
+    /* Setup MSI offset */
+    pnv_phb3_msi_update_config(phb->msis, comp, count);

I changed that to :

    pnv_phb3_msi_update_config(phb->msis, comp, count - PHB_NUM_LSI);

else the IRQ numbers overlap with the LSI and I think this why we were uselessly looping on the EOI.

Correct ?

C.

Quoted 5 lines
hw/pci-host/pnv_phb3.c new file +5 -0
+
+    DBG_MAP(phb, "Initialized for %d interrupts @0x%x, LSI off=%d",
+            count, comp, global);
+}
+
#135 lore raw

Re: [Qemu-devel] [Qemu-ppc] [PATCH 48/77] ppc/pnv: Add model for Power8 PHB3 PCIe Host bridge

Quoted 9 lines · Cédric Le Goater

I changed that to :

    pnv_phb3_msi_update_config(phb->msis, comp, count - PHB_NUM_LSI);

else the IRQ numbers overlap with the LSI and I think this why we were
uselessly looping on the EOI.

Correct ?

Quite possibly though to be honest, I have swapped out a lot of that out of my brain :-)

Cheers,
Ben.