Skip to contentSkip to conversation

booke timers

RFC PATCH Superseded Fabien Chouteau 2 participants 6 messages Jul 1, 2011 – Jul 5, 2011 Kbuild

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 booke timers

While working on the emulation of the freescale p2010 (e500v2) I realized that there's no implementation of booke's timers features. Currently mpc8544 uses ppc_emb (ppc_emb_timers_init) which is close but not exactly like booke (for example booke uses different SPR).

This is a first attempt for a distinct and clean implementation of booke's timers.

Please feel free to comment.

V2:
- Fix fixed timer, now trigger each time the selected bit switch from 0 to 1.
- Fix e500 criterion.
- Trigger an interrupt when user set DIE/FIE/WIE while DIS/FIS/WIS is already set.
- Minor fixes (mask definition, variable name...).
- rename ppc_emb to ppc_40x

---
 Makefile.target             |    2 +-
 hw/ppc.c                    |  126 +++++++-------------
 hw/ppc.h                    |   25 ++++-
 hw/ppc4xx_devs.c            |    2 +-
 hw/ppc_booke.c              |  266 +++++++++++++++++++++++++++++++++++++++++++
 hw/ppce500_mpc8544ds.c      |    4 +-
 hw/virtex_ml507.c           |   11 +--
 target-ppc/cpu.h            |   22 ++++
 target-ppc/helper.c         |   12 ++-
 target-ppc/translate_init.c |   43 +++++++-
 10 files changed, 412 insertions(+), 101 deletions(-)
 create mode 100644 hw/ppc_booke.c
Makefile.target +1 -1
index 38afdb8..f0d4062 100644
@@ -242,7 +242,7 @@ obj-i386-$(CONFIG_KVM) += kvmclock.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
 
 # shared objects
-obj-ppc-y = ppc.o
+obj-ppc-y = ppc.o ppc_booke.o
 obj-ppc-y += vga.o
 # PREP target
 obj-ppc-y += i8259.o mc146818rtc.o
hw/ppc.c +45 -81

264 lines not on this page · raw message

hw/ppc.h +24 -1
index 3ccf134..16df16a 100644
@@ -1,3 +1,5 @@
+void ppc_set_irq (CPUState *env, int n_IRQ, int level);
+
 /* PowerPC hardware exceptions management helpers */
 typedef void (*clk_setup_cb)(void *opaque, uint32_t freq);
 typedef struct clk_setup_t clk_setup_t;
@@ -11,6 +13,24 @@ static inline void clk_setup (clk_setup_t *clk, uint32_t freq)
         (*clk->cb)(clk->opaque, freq);
 }
 
+struct ppc_tb_t {
+    /* Time base management */
+    int64_t  tb_offset;    /* Compensation                    */
+    int64_t  atb_offset;   /* Compensation                    */
+    uint32_t tb_freq;      /* TB frequency                    */
+    /* Decrementer management */
+    uint64_t decr_next;    /* Tick for next decr interrupt    */
+    uint32_t decr_freq;    /* decrementer frequency           */
+    struct QEMUTimer *decr_timer;
+    /* Hypervisor decrementer management */
+    uint64_t hdecr_next;    /* Tick for next hdecr interrupt  */
+    struct QEMUTimer *hdecr_timer;
+    uint64_t purr_load;
+    uint64_t purr_start;
+    void *opaque;
+};
+
+uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
 clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
@@ -19,7 +39,7 @@ int ppc_dcr_init (CPUState *env, int (*dcr_read_error)(int dcrn),
                   int (*dcr_write_error)(int dcrn));
 int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
                       dcr_read_cb drc_read, dcr_write_cb dcr_write);
-clk_setup_cb ppc_emb_timers_init (CPUState *env, uint32_t freq,
+clk_setup_cb ppc_40x_timers_init (CPUState *env, uint32_t freq,
                                   unsigned int decr_excp);
 
 /* Embedded PowerPC reset */
@@ -55,3 +75,6 @@ enum {
 #define FW_CFG_PPC_KVM_PID      (FW_CFG_ARCH_LOCAL + 0x07)
 
 #define PPC_SERIAL_MM_BAUDBASE 399193
+
+/* ppc_booke.c */
+void ppc_booke_timers_init (CPUState *env, uint32_t freq);
hw/ppc4xx_devs.c +1 -1
index 68bdfaa..073c6a2 100644
@@ -55,7 +55,7 @@ CPUState *ppc4xx_init (const char *cpu_model,
     cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
     cpu_clk->opaque = env;
     /* Set time-base frequency to sysclk */
-    tb_clk->cb = ppc_emb_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
+    tb_clk->cb = ppc_40x_timers_init(env, sysclk, PPC_INTERRUPT_PIT);
     tb_clk->opaque = env;
     ppc_dcr_init(env, NULL, NULL);
     /* Register qemu callbacks */
hw/ppc_booke.c new file +266 -0

266 lines not on this page · raw message

hw/ppce500_mpc8544ds.c +1 -3
index 6b57fbf..83672c9 100644
@@ -237,9 +237,7 @@ static void mpc8544ds_init(ram_addr_t ram_size,
         exit(1);
     }
 
-    /* XXX register timer? */
-    ppc_emb_timers_init(env, 400000000, PPC_INTERRUPT_DECR);
-    ppc_dcr_init(env, NULL, NULL);
+    ppc_booke_timers_init(env, 400000000);
 
     /* Register reset handler */
     qemu_register_reset(mpc8544ds_cpu_reset, env);
hw/virtex_ml507.c +2 -9
index fa60515..1da478f 100644
@@ -81,7 +81,6 @@ static void mmubooke_create_initial_mapping(CPUState *env,
 static CPUState *ppc440_init_xilinx(ram_addr_t *ram_size,
                                     int do_init,
                                     const char *cpu_model,
-                                    clk_setup_t *cpu_clk, clk_setup_t *tb_clk,
                                     uint32_t sysclk)
 {
     CPUState *env;
@@ -93,11 +92,7 @@ static CPUState *ppc440_init_xilinx(ram_addr_t *ram_size,
         exit(1);
     }
 
-    cpu_clk->cb = NULL; /* We don't care about CPU clock frequency changes */
-    cpu_clk->opaque = env;
-    /* Set time-base frequency to sysclk */
-    tb_clk->cb = ppc_emb_timers_init(env, sysclk, PPC_INTERRUPT_DECR);
-    tb_clk->opaque = env;
+    ppc_booke_timers_init(env, sysclk);
 
     ppc_dcr_init(env, NULL, NULL);
 
@@ -198,7 +193,6 @@ static void virtex_init(ram_addr_t ram_size,
     ram_addr_t phys_ram;
     ram_addr_t phys_flash;
     qemu_irq irq[32], *cpu_irq;
-    clk_setup_t clk_setup[7];
     int kernel_size;
     int i;
 
@@ -208,8 +202,7 @@ static void virtex_init(ram_addr_t ram_size,
     }
 
     memset(clk_setup, 0, sizeof(clk_setup));
-    env = ppc440_init_xilinx(&ram_size, 1, cpu_model, &clk_setup[0],
-                             &clk_setup[1], 400000000);
+    env = ppc440_init_xilinx(&ram_size, 1, cpu_model, 400000000);
     qemu_register_reset(main_cpu_reset, env);
 
     phys_ram = qemu_ram_alloc(NULL, "ram", ram_size);
target-ppc/cpu.h +22 -0
index 6d60d14..28c1d03 100644
@@ -982,8 +982,28 @@ struct CPUPPCState {
 #if !defined(CONFIG_USER_ONLY)
     void *load_info;    /* Holds boot loading state.  */
 #endif
+
+    /* booke timers */
+
+    /* Specifies bit locations of the Time Base used to signal a fixed timer
+     * exception on a transition from 0 to 1. (watchdog or fixed-interval timer)
+     */
+    uint8_t fit_period[4];
+    uint8_t wdt_period[4];
 };
 
+#define SET_FIT_PERIOD(a_, b_, c_, d_)          \
+env->fit_period[0] = (a_);                      \
+ env->fit_period[1] = (b_);                     \
+ env->fit_period[2] = (c_);                     \
+ env->fit_period[3] = (d_);                     \
+
+#define SET_WDT_PERIOD(a_, b_, c_, d_)          \
+env->wdt_period[0] = (a_);                      \
+ env->wdt_period[1] = (b_);                     \
+ env->wdt_period[2] = (c_);                     \
+ env->wdt_period[3] = (d_);                     \
+
 #if !defined(CONFIG_USER_ONLY)
 /* Context used internally during MMU translations */
 typedef struct mmu_ctx_t mmu_ctx_t;
@@ -1774,6 +1794,8 @@ enum {
 
     /* BookE 2.06 PowerPC specification                                      */
     PPC2_BOOKE206      = 0x0000000000000001ULL,
+    /* e500 support                                                          */
+    PPC2_E500          = 0x0000000000000002ULL,
 };
 
 /*****************************************************************************/
target-ppc/helper.c +9 -3
index 395ea0e..6f0ad2d 100644
@@ -2815,7 +2815,9 @@ void ppc_hw_interrupt (CPUPPCState *env)
     if (msr_ee != 0) {
         /* Watchdog timer on embedded PowerPC */
         if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
-            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
+            if (env->excp_model != POWERPC_EXCP_BOOKE) {
+                env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
+            }
             powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
             return;
         }
@@ -2826,7 +2828,9 @@ void ppc_hw_interrupt (CPUPPCState *env)
         }
         /* Fixed interval timer on embedded PowerPC */
         if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
-            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
+            if (env->excp_model != POWERPC_EXCP_BOOKE) {
+                env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
+            }
             powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
             return;
         }
@@ -2838,7 +2842,9 @@ void ppc_hw_interrupt (CPUPPCState *env)
         }
         /* Decrementer exception */
         if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
-            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
+            if (env->excp_model != POWERPC_EXCP_BOOKE) {
+                env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
+            }
             powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
             return;
         }
target-ppc/translate_init.c +41 -2

133 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 21 lines · On Fri, 1 Jul 2011 16:13:41 +0200 Fabien Chouteau <[email protected]> wrote:
hw/ppc.c +5 -3
+uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
 {
     /* TB time in tb periods */
     return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
@@ -678,18 +661,23 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
                 decr, value);
     now = qemu_get_clock_ns(vm_clock);
     next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
-    if (is_excp)
+    if (is_excp) {
         next += *nextp - now;
-    if (next == now)
+    }
+    if (next == now) {
         next++;
+    }
     *nextp = next;
     /* Adjust timer */
     qemu_mod_timer(timer, next);
     /* If we set a negative value and the decrementer was positive,
-     * raise an exception.

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp, · line 682

Quoted 10 lines
hw/ppc.c +5 -1
+     * raise an exception (not for booke).
      */
-    if ((value & 0x80000000) && !(decr & 0x80000000))
+    if (! (env->insns_flags & PPC_BOOKE)
+        && (value & 0x80000000)
+        && !(decr & 0x80000000)) {
         (*raise_excp)(env);
+    }
 }
 

Also, load_decr should go from this:

    if (diff >= 0)
        decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
    else
        decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());

to something like this:

    if (diff >= 0) {
        decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
    } else if (env->insns_flags & PPC_BOOKE) {
        decr = 0;
    } else {
        decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
    }
Quoted 25 lines
hw/ppc_booke.c new file +25 -0
+/* Return the location of the bit of time base at which the FIT will raise an
+   interrupt */
+static uint8_t booke_get_fit_target(CPUState *env)
+{
+    uint8_t fp = (env->spr[SPR_BOOKE_TCR] & TCR_FP_MASK) >> TCR_FP_SHIFT;
+
+    /* Only for e500 */
+    if (env->insns_flags2 & PPC2_E500) {
+        uint32_t fpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_FPEXT_MASK)
+            >> TCR_E500_FPEXT_SHIFT;
+        fp |= fpext << 2;
+    } else {
+        fp = env->fit_period[fp];
+    }
+
+    return fp;
+}
+
+/* Return the location of the bit of time base at which the WDT will raise an
+   interrupt */
+static uint8_t booke_get_wdt_target(CPUState *env)
+{
+    uint8_t wp = (env->spr[SPR_BOOKE_TCR] & TCR_WP_MASK) >> TCR_WP_SHIFT;
+
+    /* Only for e500 */

hw/ppc_booke.c line 106

Quoted 10 lines
hw/ppc_booke.c new file +10 -0
+    if (env->insns_flags2 & PPC2_E500) {
+        uint32_t wpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_WPEXT_MASK)
+            >> TCR_E500_WPEXT_SHIFT;
+        wp |= wpext << 2;
+    } else {
+        wp = env->wdt_period[wp];
+    }
+
+    return wp;
+}

e500 fp/wp is expressed as bits from the MSB side of TB, so we need to subtract from 63 to get something sane -- and document that fit/wdt_period is from the LSB side.

Quoted 8 lines
hw/ppc_booke.c new file +8 -0
+static void booke_update_fixed_timer(CPUState         *env,
+                                     uint8_t           target_bit,
+                                     uint64_t          *next,
+                                     struct QEMUTimer *timer)
+{
+    ppc_tb_t *tb_env = env->tb_env;
+    uint64_t lapse;
+    uint64_t tb;

hw/ppc_booke.c line 125

Quoted 10 lines
hw/ppc_booke.c new file +10 -0
+    uint64_t period = 1 << (target_bit + 1);
+    uint64_t now;
+
+    now = qemu_get_clock_ns(vm_clock);
+    tb  = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
+
+    if (tb <= (1 << target_bit)) {
+        lapse = (1 << target_bit) - tb;
+    } else {
+        lapse = period - ((tb - (1 << target_bit)) % period);

We know period is a power of two, so just do "& (period - 1)".

That should let you get rid of the special case for "tb <= (1 << target_bit)" as well.

-Scott

#2 lore raw
Quoted 23 lines · Scott Wood
Quoted 8 lines · On Fri, 1 Jul 2011 16:13:41 +0200 Fabien Chouteau <[email protected]> wrote:
hw/ppc_booke.c new file +8 -0
+static void booke_update_fixed_timer(CPUState         *env,
+                                     uint8_t           target_bit,
+                                     uint64_t          *next,
+                                     struct QEMUTimer *timer)
+{
+    ppc_tb_t *tb_env = env->tb_env;
+    uint64_t lapse;
+    uint64_t tb;

hw/ppc_booke.c line 125

Quoted 10 lines
hw/ppc_booke.c new file +10 -0
+    uint64_t period = 1 << (target_bit + 1);
+    uint64_t now;
+
+    now = qemu_get_clock_ns(vm_clock);
+    tb  = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
+
+    if (tb <= (1 << target_bit)) {
+        lapse = (1 << target_bit) - tb;
+    } else {
+        lapse = period - ((tb - (1 << target_bit)) % period);

We know period is a power of two, so just do "& (period - 1)".

Quoted 3 lines

That should let you get rid of the special case for "tb <= (1 << target_bit)" as well.

Do you mean "lapse = period - ((tb - (1 << target_bit)) & (period - 1));" ?

I don't see how this solves the "tb <= (1 << target_bit)" case.

Signature · 1 line
Fabien Chouteau
#3 lore raw
Quoted 24 lines · On Mon, 4 Jul 2011 17:06:54 +0200 Fabien Chouteau <[email protected]> wrote:
Quoted 23 lines · Scott Wood
Quoted 8 lines · On Fri, 1 Jul 2011 16:13:41 +0200 Fabien Chouteau <[email protected]> wrote:
hw/ppc_booke.c new file +8 -0
+static void booke_update_fixed_timer(CPUState         *env,
+                                     uint8_t           target_bit,
+                                     uint64_t          *next,
+                                     struct QEMUTimer *timer)
+{
+    ppc_tb_t *tb_env = env->tb_env;
+    uint64_t lapse;
+    uint64_t tb;

hw/ppc_booke.c line 125

Quoted 10 lines
hw/ppc_booke.c new file +10 -0
+    uint64_t period = 1 << (target_bit + 1);
+    uint64_t now;
+
+    now = qemu_get_clock_ns(vm_clock);
+    tb  = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
+
+    if (tb <= (1 << target_bit)) {
+        lapse = (1 << target_bit) - tb;
+    } else {
+        lapse = period - ((tb - (1 << target_bit)) % period);

We know period is a power of two, so just do "& (period - 1)".

Quoted 5 lines
Quoted 3 lines

That should let you get rid of the special case for "tb <= (1 << target_bit)" as well.

Do you mean "lapse = period - ((tb - (1 << target_bit)) & (period - 1));" ?

Yes.

Or more simply:

lapse = period - ((tb - period) & (period - 1));

Quoted 1 line

I don't see how this solves the "tb <= (1 << target_bit)" case.

Actually, since everything is unsigned the special case shouldn't be needed regardless.

-Scott

#4 lore raw
Quoted 31 lines · Scott Wood
Quoted 28 lines · On Mon, 4 Jul 2011 17:06:54 +0200 Fabien Chouteau <[email protected]> wrote:
Quoted 26 lines · Scott Wood
Quoted 8 lines · On Fri, 1 Jul 2011 16:13:41 +0200 Fabien Chouteau <[email protected]> wrote:
hw/ppc_booke.c new file +8 -0
+static void booke_update_fixed_timer(CPUState         *env,
+                                     uint8_t           target_bit,
+                                     uint64_t          *next,
+                                     struct QEMUTimer *timer)
+{
+    ppc_tb_t *tb_env = env->tb_env;
+    uint64_t lapse;
+    uint64_t tb;

hw/ppc_booke.c line 125

Quoted 10 lines
hw/ppc_booke.c new file +10 -0
+    uint64_t period = 1 << (target_bit + 1);
+    uint64_t now;
+
+    now = qemu_get_clock_ns(vm_clock);
+    tb  = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
+
+    if (tb <= (1 << target_bit)) {
+        lapse = (1 << target_bit) - tb;
+    } else {
+        lapse = period - ((tb - (1 << target_bit)) % period);

We know period is a power of two, so just do "& (period - 1)".

That should let you get rid of the special case for "tb <= (1 << target_bit)" as well.

Quoted 8 lines
Quoted 1 line

Do you mean "lapse = period - ((tb - (1 << target_bit)) & (period - 1));" ?

Yes.

Or more simply:

lapse = period - ((tb - period) & (period - 1));

Are you sure? Note that period != (1 << target_bit).

Quoted 4 lines
Quoted 1 line

I don't see how this solves the "tb <= (1 << target_bit)" case.

Actually, since everything is unsigned the special case shouldn't be needed regardless.

You're right about this one, it's tricky though :)

Signature · 1 line
Fabien Chouteau
#5 lore raw
Quoted 5 lines · On Tue, 5 Jul 2011 18:41:13 +0200 Fabien Chouteau <[email protected]> wrote:
Quoted 4 lines · Scott Wood
Quoted 1 line · On Mon, 4 Jul 2011 17:06:54 +0200 Fabien Chouteau <[email protected]> wrote:

Do you mean "lapse = period - ((tb - (1 << target_bit)) & (period - 1));" ?

Quoted 8 lines
Quoted 6 lines

Yes.

Or more simply:

lapse = period - ((tb - period) & (period - 1));

Are you sure? Note that period != (1 << target_bit).

Ah, right -- misread that.

-Scott