Skip to contentSkip to conversation

sparc64: reimplement tick timers

PATCH Applied Igor Kovalenko 2 participants 3 messages Jan 5, 2010 – Jan 6, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 9/9 sparc64: reimplement tick timers

sparc64 timer has tick counter which can be set and read, and tick compare value used as deadline to fire timer interrupt. The timer is not used as periodic timer, instead deadline is set each time new timer interrupt is needed.

This change implements sparc64 timers without periodic timers. It is not complete yet, cpu_tick_set_count does not really set counter value.

---
 hw/sun4u.c |  182 ++++++++++++++++++++++++++++++++++++++++++++++++++----------

 1 files changed, 152 insertions(+), 30 deletions(-)
hw/sun4u.c +0 -0
index 84a8043..35f4c6b 100644
@@ -281,6 +281,12 @@ void cpu_check_irqs(CPUState *env)

    }
 }

+static void cpu_kick_irq(CPUState *env)
+{
+    env->halted = 0;
+    cpu_check_irqs(env);
+}
+
 static void cpu_set_irq(void *opaque, int irq, int level) {
    CPUState *env = opaque;
@@ -302,6 +308,41 @@ typedef struct ResetData { uint64_t prom_addr;
 } ResetData;

+struct sun4u_timer
+{
+    const char *name;
+    uint32_t    frequency;
+    int      disabled;
+    uint64_t disabled_mask;
+    QEMUTimer *qtimer;
+};
+
+typedef struct sun4u_timer sun4u_timer;
+
+static sun4u_timer* sun4u_timer_create(const char* name, CPUState *env,
+                                       QEMUBHFunc *cb, uint32_t frequency,
+                                       int64_t period, uint64_t disabled_mask) +{
+    sun4u_timer *timer;
+
+    timer = qemu_mallocz(sizeof (sun4u_timer));
+
+    timer->name = name;
+    timer->frequency = frequency;
+    timer->disabled = 1;
+    timer->disabled_mask = disabled_mask;
+
+    timer->qtimer = qemu_new_timer(vm_clock, cb, env);
+
+    return timer;
+}
+
+static void sun4u_timer_reset(sun4u_timer *timer)
+{
+    timer->disabled = 1;
+    qemu_del_timer(timer->qtimer);
+}
+
 static void main_cpu_reset(void *opaque)
 {
    ResetData *s = (ResetData *)opaque;
@@ -309,15 +350,11 @@ static void main_cpu_reset(void *opaque) static unsigned int nr_resets;

    cpu_reset(env);
-    env->tick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->tick, TICK_MAX, 1);
-    ptimer_run(env->tick, 1);
-    env->stick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->stick, TICK_MAX, 1);
-    ptimer_run(env->stick, 1);
-    env->hstick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->hstick, TICK_MAX, 1);
-    ptimer_run(env->hstick, 1);
+
+    sun4u_timer_reset(env->tick);
+    sun4u_timer_reset(env->stick);
+    sun4u_timer_reset(env->hstick);
+
    env->gregs[1] = 0; // Memory start env->gregs[2] = ram_size; // Memory size env->gregs[3] = 0; // Machine description XXX @@ -334,44 +371,125 @@ static void tick_irq(void *opaque) {
    CPUState *env = opaque;

-    if (!(env->tick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_TIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->tick; +
+    if (timer->disabled)
+    {
+        fprintf(logfile, "tick_irq: softint disabled\n");
+        return;
    }
+    else
+    {
+        fprintf(logfile, "tick: fire\n");
+    }
+
+    env->softint |= SOFTINT_TM;
+    cpu_kick_irq(env);
 }

 static void stick_irq(void *opaque)
 {
    CPUState *env = opaque;

-    if (!(env->stick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_STIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->stick; +
+    if (timer->disabled)
+    {
+        CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
+        return;
    }
+    else
+    {
+        CPUIRQ_DPRINTF("stick: fire\n");
+    }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
 }

 static void hstick_irq(void *opaque)
 {
    CPUState *env = opaque;

-    if (!(env->hstick_cmpr & TICK_INT_DIS)) {
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->hstick; +
+    if (timer->disabled)
+    {
+        CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
+        return;
+    }
+    else
+    {
+        CPUIRQ_DPRINTF("hstick: fire\n");
    }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
 }

 void cpu_tick_set_count(void *opaque, uint64_t count)
 {
-    ptimer_set_count(opaque, -count);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = count & ~timer->disabled_mask;
+    timer->disabled = (count & timer->disabled_mask) ? 1 : 0; +
+    fprintf(logfile, "%s (ignored) set_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count, timer->disabled?"disabled":"enabled", opaque);
+
+    // TODO: save offset in our timer
 }

 uint64_t cpu_tick_get_count(void *opaque)
 {
-    return -ptimer_get_count(opaque);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = muldiv64(qemu_get_clock(vm_clock), timer->frequency, get_ticks_per_sec());
+
+    fprintf(logfile, "%s get_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count, timer->disabled?"disabled":"enabled", opaque);
+
+    if (timer->disabled)
+        real_count |= timer->disabled_mask;
+
+    return real_count;
 }

 void cpu_tick_set_limit(void *opaque, uint64_t limit)
 {
-    ptimer_set_limit(opaque, -limit, 0);
+    sun4u_timer *timer = opaque;
+
+    int64_t now = qemu_get_clock(vm_clock);
+
+    int64_t real_limit = limit & ~timer->disabled_mask;
+    int64_t expires = muldiv64(now, timer->frequency, get_ticks_per_sec()) & ~timer->disabled_mask;
+    int64_t current_tick = expires;
+    int64_t delta = real_limit - current_tick;
+    if (delta < 0)
+        delta = 1;
+
+    timer->disabled = (limit & timer->disabled_mask) ? 1 : 0; +
+    fprintf(logfile, "%s set_limit limit=0x%016lx (%s) p=%p "
+            "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
+           timer->name, real_limit, timer->disabled?"disabled":"enabled",
+                           opaque, limit, current_tick, delta); +
+    if (!real_limit)
+    {
+        fprintf(logfile, "%s set_limit limit=ZERO - not starting timer\n",
+                timer->name);
+        qemu_del_timer(timer->qtimer);
+    }
+    else if (timer->disabled)
+    {
+        qemu_del_timer(timer->qtimer);
+    }
+    else
+    {
+        qemu_mod_timer(timer->qtimer, now + muldiv64(delta, get_ticks_per_sec(),
+                                                     timer->frequency));
+    }
 }

 static void ebus_mmio_mapfunc(PCIDevice *pci_dev, int region_num, @@ -558,9 +676,12 @@ device_init(ram_register_devices);
 static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef) {
    CPUState *env;
-    QEMUBH *bh;
    ResetData *reset_info;

+    uint32_t   tick_frequency = 10*1000000;
+    uint32_t  stick_frequency = 10*1000000;
+    uint32_t hstick_frequency = 10*1000000;
+
    if (!cpu_model)
        cpu_model = hwdef->default_cpu_model;
    env = cpu_init(cpu_model);
@@ -568,17 +689,18 @@ static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
        fprintf(stderr, "Unable to find Sparc CPU definition\n"); exit(1);
    }
-    bh = qemu_bh_new(tick_irq, env);
-    env->tick = ptimer_init(bh);
-    ptimer_set_period(env->tick, 1ULL);

-    bh = qemu_bh_new(stick_irq, env);
-    env->stick = ptimer_init(bh);
-    ptimer_set_period(env->stick, 1ULL);
+    env->tick = sun4u_timer_create("tick", env, tick_irq,
+                                    tick_frequency, 1ULL,
+                                    TICK_NPT_MASK); +
+    env->stick = sun4u_timer_create("stick", env, stick_irq,
+                                     stick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);

-    bh = qemu_bh_new(hstick_irq, env);
-    env->hstick = ptimer_init(bh);
-    ptimer_set_period(env->hstick, 1ULL);
+    env->hstick = sun4u_timer_create("hstick", env, hstick_irq,
+                                     hstick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);

    reset_info = qemu_mallocz(sizeof(ResetData));
    reset_info->env = env;

#1 lore raw
Quoted 33 lines · Igor Kovalenko

sparc64 timer has tick counter which can be set and read, and tick compare value used as deadline to fire timer interrupt. The timer is not used as periodic timer, instead deadline is set each time new timer interrupt is needed.

This change implements sparc64 timers without periodic timers. It is not complete yet, cpu_tick_set_count does not really set counter value.

---
 hw/sun4u.c |  182 ++++++++++++++++++++++++++++++++++++++++++++++++++----------

 1 files changed, 152 insertions(+), 30 deletions(-)
hw/sun4u.c +6 -0
index 84a8043..35f4c6b 100644
@@ -281,6 +281,12 @@ void cpu_check_irqs(CPUState *env)
     }
  }
 
+static void cpu_kick_irq(CPUState *env)
+{
+    env->halted = 0;
+    cpu_check_irqs(env);
+}
+
  static void cpu_set_irq(void *opaque, int irq, int level)
  {
     CPUState *env = opaque;
@@ -302,6 +308,41 @@ typedef struct ResetData {
     uint64_t prom_addr;

hw/sun4u.c typedef struct ResetData { · line 318

Quoted 10 lines
hw/sun4u.c +8 -0
  } ResetData;
 
+struct sun4u_timer
+{
+    const char *name;
+    uint32_t    frequency;
+    int      disabled;
+    uint64_t disabled_mask;
+    QEMUTimer *qtimer;
+};

The formatting seems to be off. Please don't use tabs. The prefix 'sun4u_' does not fit hstick and it's not CamelCase, how about CPUTimer?

Quoted 10 lines
hw/sun4u.c +10 -0
+
+typedef struct sun4u_timer sun4u_timer;
+
+static sun4u_timer* sun4u_timer_create(const char* name, CPUState *env,
+                                       QEMUBHFunc *cb, uint32_t frequency,
+                                       int64_t period, uint64_t disabled_mask)
+{
+    sun4u_timer *timer;
+
+    timer = qemu_mallocz(sizeof (sun4u_timer));

hw/sun4u.c typedef struct ResetData { · line 338

Quoted 10 lines
hw/sun4u.c +10 -0
+
+    timer->name = name;
+    timer->frequency = frequency;
+    timer->disabled = 1;
+    timer->disabled_mask = disabled_mask;
+
+    timer->qtimer = qemu_new_timer(vm_clock, cb, env);
+
+    return timer;
+}

The parameter 'period' is not used.

Quoted 32 lines
hw/sun4u.c +12 -9
+
+static void sun4u_timer_reset(sun4u_timer *timer)
+{
+    timer->disabled = 1;
+    qemu_del_timer(timer->qtimer);
+}
+
  static void main_cpu_reset(void *opaque)
  {
     ResetData *s = (ResetData *)opaque;
@@ -309,15 +350,11 @@ static void main_cpu_reset(void *opaque)
     static unsigned int nr_resets;
 
     cpu_reset(env);
-    env->tick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->tick, TICK_MAX, 1);
-    ptimer_run(env->tick, 1);
-    env->stick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->stick, TICK_MAX, 1);
-    ptimer_run(env->stick, 1);
-    env->hstick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->hstick, TICK_MAX, 1);
-    ptimer_run(env->hstick, 1);
+
+    sun4u_timer_reset(env->tick);
+    sun4u_timer_reset(env->stick);
+    sun4u_timer_reset(env->hstick);
+

    env->gregs[1] = 0; // Memory start
    env->gregs[2] = ram_size; // Memory size
    env->gregs[3] = 0; // Machine description XXX

hw/sun4u.c static void tick_irq(void *opaque) · line 377

Quoted 10 lines
hw/sun4u.c +4 -3
@@ -334,44 +371,125 @@ static void tick_irq(void *opaque)
  {
     CPUState *env = opaque;
 
-    if (!(env->tick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_TIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->tick;
+
+    if (timer->disabled)
+    {

Coding style.

hw/sun4u.c static void tick_irq(void *opaque) · line 382

Quoted 5 lines
hw/sun4u.c +4 -0
+        fprintf(logfile, "tick_irq: softint disabled\n");
+        return;
     }
+    else
+    {

Coding style.

Quoted 8 lines
hw/sun4u.c +5 -0
+        fprintf(logfile, "tick: fire\n");
+    }
+
+    env->softint |= SOFTINT_TM;
+    cpu_kick_irq(env);
  }
 
  static void stick_irq(void *opaque)

hw/sun4u.c static void tick_irq(void *opaque) · line 397

Quoted 10 lines
hw/sun4u.c +4 -3
  {
     CPUState *env = opaque;
 
-    if (!(env->stick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_STIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->stick;
+
+    if (timer->disabled)
+    {

Coding style.

hw/sun4u.c static void tick_irq(void *opaque) · line 402

Quoted 5 lines
hw/sun4u.c +4 -0
+        CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
+        return;
     }
+    else
+    {

Coding style.

Quoted 11 lines
hw/sun4u.c +5 -0
+        CPUIRQ_DPRINTF("stick: fire\n");
+    }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
  }
 
  static void hstick_irq(void *opaque)
  {
     CPUState *env = opaque;
 

hw/sun4u.c static void tick_irq(void *opaque) · line 421

Quoted 10 lines
hw/sun4u.c +8 -2
-    if (!(env->hstick_cmpr & TICK_INT_DIS)) {
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->hstick;
+
+    if (timer->disabled)
+    {
+        CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
+        return;
+    }
+    else

Coding style.

Quoted 9 lines
hw/sun4u.c +5 -0
+    {
+        CPUIRQ_DPRINTF("hstick: fire\n");
     }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
  }
 
  void cpu_tick_set_count(void *opaque, uint64_t count)

hw/sun4u.c static void tick_irq(void *opaque) · line 439

Quoted 10 lines
hw/sun4u.c +7 -1
  {
-    ptimer_set_count(opaque, -count);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = count & ~timer->disabled_mask;
+    timer->disabled = (count & timer->disabled_mask) ? 1 : 0;
+
+    fprintf(logfile, "%s (ignored) set_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count,
 timer->disabled?"disabled":"enabled", opaque);

I think logging this is not necessary, please create TICK_DPRINTF() or something.

hw/sun4u.c static void tick_irq(void *opaque) · line 449

Quoted 11 lines
hw/sun4u.c +5 -1
+
+    // TODO: save offset in our timer
  }
 
  uint64_t cpu_tick_get_count(void *opaque)
  {
-    return -ptimer_get_count(opaque);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = muldiv64(qemu_get_clock(vm_clock),
 timer->frequency, get_ticks_per_sec());

The line is wrapped, perhaps the line is too long?

hw/sun4u.c static void tick_irq(void *opaque) · line 453

Quoted 4 lines
hw/sun4u.c +3 -0
+
+    fprintf(logfile, "%s get_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count,
 timer->disabled?"disabled":"enabled", opaque);

DPRINTF()

Quoted 16 lines
hw/sun4u.c +11 -1
+
+    if (timer->disabled)
+        real_count |= timer->disabled_mask;
+
+    return real_count;
  }
 
  void cpu_tick_set_limit(void *opaque, uint64_t limit)
  {
-    ptimer_set_limit(opaque, -limit, 0);
+    sun4u_timer *timer = opaque;
+
+    int64_t now = qemu_get_clock(vm_clock);
+
+    int64_t real_limit = limit & ~timer->disabled_mask;
+    int64_t expires = muldiv64(now, timer->frequency,
Quoted 1 line

get_ticks_per_sec()) & ~timer->disabled_mask;

Line too long.

hw/sun4u.c

Quoted 11 lines

+    int64_t current_tick = expires;
+    int64_t delta = real_limit - current_tick;
+    if (delta < 0)
+        delta = 1;

hw/sun4u.c +7 -0
+
+    timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;
+
+    fprintf(logfile, "%s set_limit limit=0x%016lx (%s) p=%p "
+            "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
+           timer->name, real_limit, timer->disabled?"disabled":"enabled",
+                           opaque, limit, current_tick, delta);

DPRINTF()

Quoted 3 lines

+
+    if (!real_limit)
+    {

Coding style.

Quoted 6 lines

+        fprintf(logfile, "%s set_limit limit=ZERO - not starting timer\n",
+                timer->name);
+        qemu_del_timer(timer->qtimer);
+    }
+    else if (timer->disabled)
+    {

Coding style.

Quoted 4 lines

+        qemu_del_timer(timer->qtimer);
+    }
+    else
+    {

Coding style.

Quoted 2 lines

+        qemu_mod_timer(timer->qtimer, now + muldiv64(delta, get_ticks_per_sec(),

Line too long.

Quoted 47 lines

+                                                     timer->frequency));
+    }
 }

 static void ebus_mmio_mapfunc(PCIDevice *pci_dev, int region_num,

hw/sun4u.c +14 -10
@@ -558,9 +676,12 @@ device_init(ram_register_devices);
  static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
  {
     CPUState *env;
-    QEMUBH *bh;
     ResetData *reset_info;
 
+    uint32_t   tick_frequency = 10*1000000;
+    uint32_t  stick_frequency = 10*1000000;
+    uint32_t hstick_frequency = 10*1000000;
+
     if (!cpu_model)
         cpu_model = hwdef->default_cpu_model;
     env = cpu_init(cpu_model);
@@ -568,17 +689,18 @@ static CPUState *cpu_devinit(const char
 *cpu_model, const struct hwdef *hwdef)
         fprintf(stderr, "Unable to find Sparc CPU definition\n");
         exit(1);
     }
-    bh = qemu_bh_new(tick_irq, env);
-    env->tick = ptimer_init(bh);
-    ptimer_set_period(env->tick, 1ULL);
 
-    bh = qemu_bh_new(stick_irq, env);
-    env->stick = ptimer_init(bh);
-    ptimer_set_period(env->stick, 1ULL);
+    env->tick = sun4u_timer_create("tick", env, tick_irq,
+                                    tick_frequency, 1ULL,
+                                    TICK_NPT_MASK);
+
+    env->stick = sun4u_timer_create("stick", env, stick_irq,
+                                     stick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);
 
-    bh = qemu_bh_new(hstick_irq, env);
-    env->hstick = ptimer_init(bh);
-    ptimer_set_period(env->hstick, 1ULL);
+    env->hstick = sun4u_timer_create("hstick", env, hstick_irq,
+                                     hstick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);
 
     reset_info = qemu_mallocz(sizeof(ResetData));
Quoted 4 lines

    reset_info->env = env;

After this, are ptimers not going to be used anymore for Sparc64? Then Makefile etc. changes should be included.

Anyway, thank you for your efforts.

#2 lore raw
Quoted 339 lines · Blue Swirl
Quoted 33 lines · Igor Kovalenko

sparc64 timer has tick counter which can be set and read, and tick compare value used as deadline to fire timer interrupt. The timer is not used as periodic timer, instead deadline is set each time new timer interrupt is needed.

This change implements sparc64 timers without periodic timers. It is not complete yet, cpu_tick_set_count does not really set counter value.

---
 hw/sun4u.c |  182 ++++++++++++++++++++++++++++++++++++++++++++++++++----------

 1 files changed, 152 insertions(+), 30 deletions(-)
hw/sun4u.c +6 -0
index 84a8043..35f4c6b 100644
@@ -281,6 +281,12 @@ void cpu_check_irqs(CPUState *env)
     }
  }
 
+static void cpu_kick_irq(CPUState *env)
+{
+    env->halted = 0;
+    cpu_check_irqs(env);
+}
+
  static void cpu_set_irq(void *opaque, int irq, int level)
  {
     CPUState *env = opaque;
@@ -302,6 +308,41 @@ typedef struct ResetData {
     uint64_t prom_addr;

hw/sun4u.c typedef struct ResetData { · line 318

Quoted 10 lines
hw/sun4u.c +8 -0
  } ResetData;
 
+struct sun4u_timer
+{
+    const char *name;
+    uint32_t    frequency;
+    int      disabled;
+    uint64_t disabled_mask;
+    QEMUTimer *qtimer;
+};

The formatting seems to be off. Please don't use tabs. The prefix 'sun4u_' does not fit hstick and it's not CamelCase, how about CPUTimer?

Quoted 10 lines
hw/sun4u.c +10 -0
+
+typedef struct sun4u_timer sun4u_timer;
+
+static sun4u_timer* sun4u_timer_create(const char* name, CPUState *env,
+                                       QEMUBHFunc *cb, uint32_t frequency,
+                                       int64_t period, uint64_t disabled_mask)
+{
+    sun4u_timer *timer;
+
+    timer = qemu_mallocz(sizeof (sun4u_timer));

hw/sun4u.c typedef struct ResetData { · line 338

Quoted 10 lines
hw/sun4u.c +10 -0
+
+    timer->name = name;
+    timer->frequency = frequency;
+    timer->disabled = 1;
+    timer->disabled_mask = disabled_mask;
+
+    timer->qtimer = qemu_new_timer(vm_clock, cb, env);
+
+    return timer;
+}

The parameter 'period' is not used.

Quoted 32 lines
hw/sun4u.c +12 -9
+
+static void sun4u_timer_reset(sun4u_timer *timer)
+{
+    timer->disabled = 1;
+    qemu_del_timer(timer->qtimer);
+}
+
  static void main_cpu_reset(void *opaque)
  {
     ResetData *s = (ResetData *)opaque;
@@ -309,15 +350,11 @@ static void main_cpu_reset(void *opaque)
     static unsigned int nr_resets;
 
     cpu_reset(env);
-    env->tick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->tick, TICK_MAX, 1);
-    ptimer_run(env->tick, 1);
-    env->stick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->stick, TICK_MAX, 1);
-    ptimer_run(env->stick, 1);
-    env->hstick_cmpr = TICK_INT_DIS | 0;
-    ptimer_set_limit(env->hstick, TICK_MAX, 1);
-    ptimer_run(env->hstick, 1);
+
+    sun4u_timer_reset(env->tick);
+    sun4u_timer_reset(env->stick);
+    sun4u_timer_reset(env->hstick);
+

    env->gregs[1] = 0; // Memory start
    env->gregs[2] = ram_size; // Memory size
    env->gregs[3] = 0; // Machine description XXX

hw/sun4u.c static void tick_irq(void *opaque) · line 377

Quoted 10 lines
hw/sun4u.c +4 -3
@@ -334,44 +371,125 @@ static void tick_irq(void *opaque)
  {
     CPUState *env = opaque;
 
-    if (!(env->tick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_TIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->tick;
+
+    if (timer->disabled)
+    {

Coding style.

hw/sun4u.c static void tick_irq(void *opaque) · line 382

Quoted 5 lines
hw/sun4u.c +4 -0
+        fprintf(logfile, "tick_irq: softint disabled\n");
+        return;
     }
+    else
+    {

Coding style.

Quoted 8 lines
hw/sun4u.c +5 -0
+        fprintf(logfile, "tick: fire\n");
+    }
+
+    env->softint |= SOFTINT_TM;
+    cpu_kick_irq(env);
  }
 
  static void stick_irq(void *opaque)

hw/sun4u.c static void tick_irq(void *opaque) · line 397

Quoted 10 lines
hw/sun4u.c +4 -3
  {
     CPUState *env = opaque;
 
-    if (!(env->stick_cmpr & TICK_INT_DIS)) {
-        env->softint |= SOFTINT_STIMER;
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->stick;
+
+    if (timer->disabled)
+    {

Coding style.

hw/sun4u.c static void tick_irq(void *opaque) · line 402

Quoted 5 lines
hw/sun4u.c +4 -0
+        CPUIRQ_DPRINTF("stick_irq: softint disabled\n");
+        return;
     }
+    else
+    {

Coding style.

Quoted 11 lines
hw/sun4u.c +5 -0
+        CPUIRQ_DPRINTF("stick: fire\n");
+    }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
  }
 
  static void hstick_irq(void *opaque)
  {
     CPUState *env = opaque;
 

hw/sun4u.c static void tick_irq(void *opaque) · line 421

Quoted 10 lines
hw/sun4u.c +8 -2
-    if (!(env->hstick_cmpr & TICK_INT_DIS)) {
-        cpu_interrupt(env, CPU_INTERRUPT_TIMER);
+    sun4u_timer* timer = (sun4u_timer*) env->hstick;
+
+    if (timer->disabled)
+    {
+        CPUIRQ_DPRINTF("hstick_irq: softint disabled\n");
+        return;
+    }
+    else

Coding style.

Quoted 9 lines
hw/sun4u.c +5 -0
+    {
+        CPUIRQ_DPRINTF("hstick: fire\n");
     }
+
+    env->softint |= SOFTINT_SM;
+    cpu_kick_irq(env);
  }
 
  void cpu_tick_set_count(void *opaque, uint64_t count)

hw/sun4u.c static void tick_irq(void *opaque) · line 439

Quoted 10 lines
hw/sun4u.c +7 -1
  {
-    ptimer_set_count(opaque, -count);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = count & ~timer->disabled_mask;
+    timer->disabled = (count & timer->disabled_mask) ? 1 : 0;
+
+    fprintf(logfile, "%s (ignored) set_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count,
 timer->disabled?"disabled":"enabled", opaque);

I think logging this is not necessary, please create TICK_DPRINTF() or something.

hw/sun4u.c static void tick_irq(void *opaque) · line 449

Quoted 11 lines
hw/sun4u.c +5 -1
+
+    // TODO: save offset in our timer
  }
 
  uint64_t cpu_tick_get_count(void *opaque)
  {
-    return -ptimer_get_count(opaque);
+    sun4u_timer *timer = opaque;
+
+    uint64_t real_count = muldiv64(qemu_get_clock(vm_clock),
 timer->frequency, get_ticks_per_sec());

The line is wrapped, perhaps the line is too long?

hw/sun4u.c static void tick_irq(void *opaque) · line 453

Quoted 4 lines
hw/sun4u.c +3 -0
+
+    fprintf(logfile, "%s get_count count=0x%016lx (%s) p=%p\n",
+           timer->name, real_count,
 timer->disabled?"disabled":"enabled", opaque);

DPRINTF()

Quoted 16 lines
hw/sun4u.c +11 -1
+
+    if (timer->disabled)
+        real_count |= timer->disabled_mask;
+
+    return real_count;
  }
 
  void cpu_tick_set_limit(void *opaque, uint64_t limit)
  {
-    ptimer_set_limit(opaque, -limit, 0);
+    sun4u_timer *timer = opaque;
+
+    int64_t now = qemu_get_clock(vm_clock);
+
+    int64_t real_limit = limit & ~timer->disabled_mask;
+    int64_t expires = muldiv64(now, timer->frequency,
Quoted 1 line

get_ticks_per_sec()) & ~timer->disabled_mask;

Line too long.

hw/sun4u.c

Quoted 11 lines

+    int64_t current_tick = expires;
+    int64_t delta = real_limit - current_tick;
+    if (delta < 0)
+        delta = 1;

hw/sun4u.c +7 -0
+
+    timer->disabled = (limit & timer->disabled_mask) ? 1 : 0;
+
+    fprintf(logfile, "%s set_limit limit=0x%016lx (%s) p=%p "
+            "called with limit=0x%016lx at 0x%016lx (delta=0x%016lx)\n",
+           timer->name, real_limit, timer->disabled?"disabled":"enabled",
+                           opaque, limit, current_tick, delta);

DPRINTF()

Quoted 3 lines

+
+    if (!real_limit)
+    {

Coding style.

Quoted 6 lines

+        fprintf(logfile, "%s set_limit limit=ZERO - not starting timer\n",
+                timer->name);
+        qemu_del_timer(timer->qtimer);
+    }
+    else if (timer->disabled)
+    {

Coding style.

Quoted 4 lines

+        qemu_del_timer(timer->qtimer);
+    }
+    else
+    {

Coding style.

Quoted 2 lines

+        qemu_mod_timer(timer->qtimer, now + muldiv64(delta, get_ticks_per_sec(),

Line too long.

Quoted 47 lines

+                                                     timer->frequency));
+    }
 }

 static void ebus_mmio_mapfunc(PCIDevice *pci_dev, int region_num,

hw/sun4u.c +14 -10
@@ -558,9 +676,12 @@ device_init(ram_register_devices);
  static CPUState *cpu_devinit(const char *cpu_model, const struct hwdef *hwdef)
  {
     CPUState *env;
-    QEMUBH *bh;
     ResetData *reset_info;
 
+    uint32_t   tick_frequency = 10*1000000;
+    uint32_t  stick_frequency = 10*1000000;
+    uint32_t hstick_frequency = 10*1000000;
+
     if (!cpu_model)
         cpu_model = hwdef->default_cpu_model;
     env = cpu_init(cpu_model);
@@ -568,17 +689,18 @@ static CPUState *cpu_devinit(const char
 *cpu_model, const struct hwdef *hwdef)
         fprintf(stderr, "Unable to find Sparc CPU definition\n");
         exit(1);
     }
-    bh = qemu_bh_new(tick_irq, env);
-    env->tick = ptimer_init(bh);
-    ptimer_set_period(env->tick, 1ULL);
 
-    bh = qemu_bh_new(stick_irq, env);
-    env->stick = ptimer_init(bh);
-    ptimer_set_period(env->stick, 1ULL);
+    env->tick = sun4u_timer_create("tick", env, tick_irq,
+                                    tick_frequency, 1ULL,
+                                    TICK_NPT_MASK);
+
+    env->stick = sun4u_timer_create("stick", env, stick_irq,
+                                     stick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);
 
-    bh = qemu_bh_new(hstick_irq, env);
-    env->hstick = ptimer_init(bh);
-    ptimer_set_period(env->hstick, 1ULL);
+    env->hstick = sun4u_timer_create("hstick", env, hstick_irq,
+                                     hstick_frequency, 1ULL,
+                                     TICK_SOFTINT_DISABLE);
 
     reset_info = qemu_mallocz(sizeof(ResetData));
Quoted 4 lines

    reset_info->env = env;

After this, are ptimers not going to be used anymore for Sparc64? Then Makefile etc. changes should be included.

Quoted 1 line

Anyway, thank you for your efforts.

Thanks for the review!

I'm not sure at the moment we need to discard ptimer implementation for sparc64. I'll work with the rest of this series according to review comments, assuming those are otherwise not bad to be committed. We can deal with (the only, at the moment) interrupt source separately.

Signature · 2 lines
Kind regards,
Igor V. Kovalenko