Re: [kvm-unit-tests v2 09/10] powerpc: Support powernv machine with QEMU TCG

2023-03-23 Thread Thomas Huth

On 20/03/2023 08.03, Nicholas Piggin wrote:

This is a basic first pass at powernv support using OPAL (skiboot)
firmware.

The ACCEL is a bit clunky, now defaulting to tcg for powernv machine.
It also does not yet run in the run_tests.sh batch process, more work
is needed to exclude certain tests (e.g., rtas) and adjust parameters
(e.g., increase memory size) to allow powernv to work. For now it
can run single test cases.

Signed-off-by: Nicholas Piggin 
---
  lib/powerpc/asm/ppc_asm.h   |  5 +++
  lib/powerpc/asm/processor.h | 14 
  lib/powerpc/hcall.c |  4 +--
  lib/powerpc/io.c| 33 --
  lib/powerpc/io.h|  6 
  lib/powerpc/processor.c | 10 ++
  lib/powerpc/setup.c | 10 --
  lib/ppc64/asm/opal.h| 11 ++
  lib/ppc64/opal-calls.S  | 46 +
  lib/ppc64/opal.c| 67 +
  powerpc/Makefile.ppc64  |  2 ++
  powerpc/cstart64.S  |  7 
  powerpc/run | 30 ++---
  13 files changed, 234 insertions(+), 11 deletions(-)
  create mode 100644 lib/ppc64/asm/opal.h
  create mode 100644 lib/ppc64/opal-calls.S
  create mode 100644 lib/ppc64/opal.c

diff --git a/lib/powerpc/asm/ppc_asm.h b/lib/powerpc/asm/ppc_asm.h
index 6299ff5..5eec9d3 100644
--- a/lib/powerpc/asm/ppc_asm.h
+++ b/lib/powerpc/asm/ppc_asm.h
@@ -36,7 +36,12 @@
  #endif /* __BYTE_ORDER__ */
  
  /* Machine State Register definitions: */

+#define MSR_LE_BIT 0
  #define MSR_EE_BIT15  /* External Interrupts Enable */
+#define MSR_HV_BIT 60  /* Hypervisor mode */
  #define MSR_SF_BIT63  /* 64-bit mode */
  
+#define SPR_HSRR0	0x13A

+#define SPR_HSRR1  0x13B
+
  #endif /* _ASMPOWERPC_PPC_ASM_H */
diff --git a/lib/powerpc/asm/processor.h b/lib/powerpc/asm/processor.h
index ebfeff2..8084787 100644
--- a/lib/powerpc/asm/processor.h
+++ b/lib/powerpc/asm/processor.h
@@ -3,12 +3,26 @@
  
  #include 

  #include 
+#include 
  
  #ifndef __ASSEMBLY__

  void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void 
*);
  void do_handle_exception(struct pt_regs *regs);
  #endif /* __ASSEMBLY__ */
  
+/*

+ * If this returns true on PowerNV / OPAL machines which run in hypervisor
+ * mode. False on pseries / PAPR machines that run in guest mode.


s/If this/This/

 Thomas



Re: [kvm-unit-tests v2 09/10] powerpc: Support powernv machine with QEMU TCG

2023-03-20 Thread Nicholas Piggin
On Mon Mar 20, 2023 at 7:47 PM AEST, Cédric Le Goater wrote:
> Hello Nick,
>
> On 3/20/23 08:03, Nicholas Piggin wrote:
> > This is a basic first pass at powernv support using OPAL (skiboot)
> > firmware.
> > 
> > The ACCEL is a bit clunky, now defaulting to tcg for powernv machine.
> > It also does not yet run in the run_tests.sh batch process, more work
> > is needed to exclude certain tests (e.g., rtas) and adjust parameters
> > (e.g., increase memory size) to allow powernv to work. For now it
> > can run single test cases.
>
> Why do you need to load OPAL ? for the shutdown ? because the UART ops
> could be done directly using MMIOs on the LPC IO space.

Don't really need it but I thought it would be easier to begin with, and
then I thought actually it's nice to have this kind of test harness for
skiboot as well. So I would hope to keep the skiboot bios option even if
a no-bios version was done.

[...]

> >   void io_init(void)
> >   {
> > -   rtas_init();
> > +   if (machine_is_powernv())
> > +   opal_init();
> > +   else
> > +   rtas_init();
> >   }

[...]

> > @@ -195,6 +197,8 @@ void setup(const void *fdt)
> > freemem += initrd_size;
> > }
> >   
> > +   opal_init();
> > +
>
> This opal_init() call seems redundant with io_init().

Oh you're right good catch, that might be an old piece before I cleaned
it up. I'll have to fix that and re-test it.

Thanks,
Nick


Re: [kvm-unit-tests v2 09/10] powerpc: Support powernv machine with QEMU TCG

2023-03-20 Thread Cédric Le Goater

Hello Nick,

On 3/20/23 08:03, Nicholas Piggin wrote:

This is a basic first pass at powernv support using OPAL (skiboot)
firmware.

The ACCEL is a bit clunky, now defaulting to tcg for powernv machine.
It also does not yet run in the run_tests.sh batch process, more work
is needed to exclude certain tests (e.g., rtas) and adjust parameters
(e.g., increase memory size) to allow powernv to work. For now it
can run single test cases.


Why do you need to load OPAL ? for the shutdown ? because the UART ops
could be done directly using MMIOs on the LPC IO space.


Signed-off-by: Nicholas Piggin 
---
  lib/powerpc/asm/ppc_asm.h   |  5 +++
  lib/powerpc/asm/processor.h | 14 
  lib/powerpc/hcall.c |  4 +--
  lib/powerpc/io.c| 33 --
  lib/powerpc/io.h|  6 
  lib/powerpc/processor.c | 10 ++
  lib/powerpc/setup.c | 10 --
  lib/ppc64/asm/opal.h| 11 ++
  lib/ppc64/opal-calls.S  | 46 +
  lib/ppc64/opal.c| 67 +
  powerpc/Makefile.ppc64  |  2 ++
  powerpc/cstart64.S  |  7 
  powerpc/run | 30 ++---
  13 files changed, 234 insertions(+), 11 deletions(-)
  create mode 100644 lib/ppc64/asm/opal.h
  create mode 100644 lib/ppc64/opal-calls.S
  create mode 100644 lib/ppc64/opal.c

diff --git a/lib/powerpc/asm/ppc_asm.h b/lib/powerpc/asm/ppc_asm.h
index 6299ff5..5eec9d3 100644
--- a/lib/powerpc/asm/ppc_asm.h
+++ b/lib/powerpc/asm/ppc_asm.h
@@ -36,7 +36,12 @@
  #endif /* __BYTE_ORDER__ */
  
  /* Machine State Register definitions: */

+#define MSR_LE_BIT 0
  #define MSR_EE_BIT15  /* External Interrupts Enable */
+#define MSR_HV_BIT 60  /* Hypervisor mode */
  #define MSR_SF_BIT63  /* 64-bit mode */
  
+#define SPR_HSRR0	0x13A

+#define SPR_HSRR1  0x13B
+
  #endif /* _ASMPOWERPC_PPC_ASM_H */
diff --git a/lib/powerpc/asm/processor.h b/lib/powerpc/asm/processor.h
index ebfeff2..8084787 100644
--- a/lib/powerpc/asm/processor.h
+++ b/lib/powerpc/asm/processor.h
@@ -3,12 +3,26 @@
  
  #include 

  #include 
+#include 
  
  #ifndef __ASSEMBLY__

  void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void 
*);
  void do_handle_exception(struct pt_regs *regs);
  #endif /* __ASSEMBLY__ */
  
+/*

+ * If this returns true on PowerNV / OPAL machines which run in hypervisor
+ * mode. False on pseries / PAPR machines that run in guest mode.
+ */
+static inline bool machine_is_powernv(void)
+{
+   uint64_t msr;
+
+   asm volatile ("mfmsr %[msr]" : [msr] "=r" (msr));
+
+   return !!(msr & (1ULL << MSR_HV_BIT));
+}
+
  static inline uint64_t get_tb(void)
  {
uint64_t tb;
diff --git a/lib/powerpc/hcall.c b/lib/powerpc/hcall.c
index 711cb1b..37e52f5 100644
--- a/lib/powerpc/hcall.c
+++ b/lib/powerpc/hcall.c
@@ -25,7 +25,7 @@ int hcall_have_broken_sc1(void)
return r3 == (unsigned long)H_PRIVILEGE;
  }
  
-void putchar(int c)

+void papr_putchar(int c)
  {
unsigned long vty = 0;  /* 0 == default */
unsigned long nr_chars = 1;
@@ -34,7 +34,7 @@ void putchar(int c)
hcall(H_PUT_TERM_CHAR, vty, nr_chars, chars);
  }
  
-int __getchar(void)

+int __papr_getchar(void)
  {
register unsigned long r3 asm("r3") = H_GET_TERM_CHAR;
register unsigned long r4 asm("r4") = 0; /* 0 == default vty */
diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
index a381688..a3a64ce 100644
--- a/lib/powerpc/io.c
+++ b/lib/powerpc/io.c
@@ -9,13 +9,39 @@
  #include 
  #include 
  #include 
+#include 
  #include "io.h"
  
  static struct spinlock print_lock;
  
+struct opal {

+   uint64_t base;
+   uint64_t entry;
+};
+extern struct opal opal;
+
+void putchar(int c)
+{
+   if (machine_is_powernv())
+   opal_putchar(c);
+   else
+   papr_putchar(c);
+}
+
+int __getchar(void)
+{
+   if (machine_is_powernv())
+   return __opal_getchar();
+   else
+   return __papr_getchar();
+}
+
  void io_init(void)
  {
-   rtas_init();
+   if (machine_is_powernv())
+   opal_init();
+   else
+   rtas_init();
  }
  
  void puts(const char *s)

@@ -38,7 +64,10 @@ void exit(int code)
  // FIXME: change this print-exit/rtas-poweroff to chr_testdev_exit(),
  //maybe by plugging chr-testdev into a spapr-vty.
printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-   rtas_power_off();
+   if (machine_is_powernv())
+   opal_power_off();
+   else
+   rtas_power_off();
halt(code);
__builtin_unreachable();
  }
diff --git a/lib/powerpc/io.h b/lib/powerpc/io.h
index d4f21ba..a2aed7b 100644
--- a/lib/powerpc/io.h
+++ b/lib/powerpc/io.h
@@ -8,6 +8,12 @@
  #define _POWERPC_IO_H_
  
  extern void io_init(void);

+extern void opal_init(void);
+extern void 

[kvm-unit-tests v2 09/10] powerpc: Support powernv machine with QEMU TCG

2023-03-20 Thread Nicholas Piggin
This is a basic first pass at powernv support using OPAL (skiboot)
firmware.

The ACCEL is a bit clunky, now defaulting to tcg for powernv machine.
It also does not yet run in the run_tests.sh batch process, more work
is needed to exclude certain tests (e.g., rtas) and adjust parameters
(e.g., increase memory size) to allow powernv to work. For now it
can run single test cases.

Signed-off-by: Nicholas Piggin 
---
 lib/powerpc/asm/ppc_asm.h   |  5 +++
 lib/powerpc/asm/processor.h | 14 
 lib/powerpc/hcall.c |  4 +--
 lib/powerpc/io.c| 33 --
 lib/powerpc/io.h|  6 
 lib/powerpc/processor.c | 10 ++
 lib/powerpc/setup.c | 10 --
 lib/ppc64/asm/opal.h| 11 ++
 lib/ppc64/opal-calls.S  | 46 +
 lib/ppc64/opal.c| 67 +
 powerpc/Makefile.ppc64  |  2 ++
 powerpc/cstart64.S  |  7 
 powerpc/run | 30 ++---
 13 files changed, 234 insertions(+), 11 deletions(-)
 create mode 100644 lib/ppc64/asm/opal.h
 create mode 100644 lib/ppc64/opal-calls.S
 create mode 100644 lib/ppc64/opal.c

diff --git a/lib/powerpc/asm/ppc_asm.h b/lib/powerpc/asm/ppc_asm.h
index 6299ff5..5eec9d3 100644
--- a/lib/powerpc/asm/ppc_asm.h
+++ b/lib/powerpc/asm/ppc_asm.h
@@ -36,7 +36,12 @@
 #endif /* __BYTE_ORDER__ */
 
 /* Machine State Register definitions: */
+#define MSR_LE_BIT 0
 #define MSR_EE_BIT 15  /* External Interrupts Enable */
+#define MSR_HV_BIT 60  /* Hypervisor mode */
 #define MSR_SF_BIT 63  /* 64-bit mode */
 
+#define SPR_HSRR0  0x13A
+#define SPR_HSRR1  0x13B
+
 #endif /* _ASMPOWERPC_PPC_ASM_H */
diff --git a/lib/powerpc/asm/processor.h b/lib/powerpc/asm/processor.h
index ebfeff2..8084787 100644
--- a/lib/powerpc/asm/processor.h
+++ b/lib/powerpc/asm/processor.h
@@ -3,12 +3,26 @@
 
 #include 
 #include 
+#include 
 
 #ifndef __ASSEMBLY__
 void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void 
*);
 void do_handle_exception(struct pt_regs *regs);
 #endif /* __ASSEMBLY__ */
 
+/*
+ * If this returns true on PowerNV / OPAL machines which run in hypervisor
+ * mode. False on pseries / PAPR machines that run in guest mode.
+ */
+static inline bool machine_is_powernv(void)
+{
+   uint64_t msr;
+
+   asm volatile ("mfmsr %[msr]" : [msr] "=r" (msr));
+
+   return !!(msr & (1ULL << MSR_HV_BIT));
+}
+
 static inline uint64_t get_tb(void)
 {
uint64_t tb;
diff --git a/lib/powerpc/hcall.c b/lib/powerpc/hcall.c
index 711cb1b..37e52f5 100644
--- a/lib/powerpc/hcall.c
+++ b/lib/powerpc/hcall.c
@@ -25,7 +25,7 @@ int hcall_have_broken_sc1(void)
return r3 == (unsigned long)H_PRIVILEGE;
 }
 
-void putchar(int c)
+void papr_putchar(int c)
 {
unsigned long vty = 0;  /* 0 == default */
unsigned long nr_chars = 1;
@@ -34,7 +34,7 @@ void putchar(int c)
hcall(H_PUT_TERM_CHAR, vty, nr_chars, chars);
 }
 
-int __getchar(void)
+int __papr_getchar(void)
 {
register unsigned long r3 asm("r3") = H_GET_TERM_CHAR;
register unsigned long r4 asm("r4") = 0; /* 0 == default vty */
diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
index a381688..a3a64ce 100644
--- a/lib/powerpc/io.c
+++ b/lib/powerpc/io.c
@@ -9,13 +9,39 @@
 #include 
 #include 
 #include 
+#include 
 #include "io.h"
 
 static struct spinlock print_lock;
 
+struct opal {
+   uint64_t base;
+   uint64_t entry;
+};
+extern struct opal opal;
+
+void putchar(int c)
+{
+   if (machine_is_powernv())
+   opal_putchar(c);
+   else
+   papr_putchar(c);
+}
+
+int __getchar(void)
+{
+   if (machine_is_powernv())
+   return __opal_getchar();
+   else
+   return __papr_getchar();
+}
+
 void io_init(void)
 {
-   rtas_init();
+   if (machine_is_powernv())
+   opal_init();
+   else
+   rtas_init();
 }
 
 void puts(const char *s)
@@ -38,7 +64,10 @@ void exit(int code)
 // FIXME: change this print-exit/rtas-poweroff to chr_testdev_exit(),
 //maybe by plugging chr-testdev into a spapr-vty.
printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-   rtas_power_off();
+   if (machine_is_powernv())
+   opal_power_off();
+   else
+   rtas_power_off();
halt(code);
__builtin_unreachable();
 }
diff --git a/lib/powerpc/io.h b/lib/powerpc/io.h
index d4f21ba..a2aed7b 100644
--- a/lib/powerpc/io.h
+++ b/lib/powerpc/io.h
@@ -8,6 +8,12 @@
 #define _POWERPC_IO_H_
 
 extern void io_init(void);
+extern void opal_init(void);
+extern void opal_power_off(void);
 extern void putchar(int c);
+extern void opal_putchar(int c);
+extern void papr_putchar(int c);
+extern int __opal_getchar(void);
+extern int __papr_getchar(void);
 
 #endif
diff --git a/lib/powerpc/processor.c b/lib/powerpc/processor.c