Skip to contentSkip to conversation

sh4: more system emulator patches

Qemu-devel Magnus Damm 1 participant 1 message Nov 14, 2007

lore
Share & tools
lore
#0 lore raw

Hi everyone,

Here comes another batch of sh4 system emulator patches.

sh: add sh.h header
sh_serial: enable tx after reset
sh_intc: improve interrupt controller code
sh: connect cpu core to interrupt controller
sh_timer: add interrupt support

These patches mainly improve the sh interrupt controller code. Interrupt delivery is however disabled by default since the sh4 cpu core code still has some outstanding interrupt issues. People interested in hacking and testing can enable interrupt delivery by commenting out INTC_IRQS_DISABLED in hw/sh_intc.c.

Please apply in attached order. Thanks!

/ magnus

sh: add sh.h header

This patch adds the header file sh.h

---

 hw/sh.h        |   11 +++++++++++
 hw/sh7750.c    |    1 +
 hw/sh_serial.c |    1 +
 hw/sh_timer.c  |    1 +
 vl.h           |    5 -----
 5 files changed, 14 insertions(+), 5 deletions(-)
hw/sh.h new file +11 -0
@@ -0,0 +1,11 @@
+#ifndef __SH_H__
+#define __SH_H__
+
+#include "sh_intc.h"
+
+/* sh_serial.c */
+#define SH_SERIAL_FEAT_SCIF (1 << 0)
+void sh_serial_init (target_phys_addr_t base, int feat,
+		     uint32_t freq, CharDriverState *chr);
+
+#endif /* __SH_H__ */
hw/sh7750.c +1 -0
@@ -27,6 +27,7 @@
 #include "vl.h"
 #include "sh7750_regs.h"
 #include "sh7750_regnames.h"
+#include "sh.h"
 #include "sh_intc.h"
 
 #define NB_DEVICES 4
hw/sh_serial.c +1 -0
@@ -25,6 +25,7 @@
  * THE SOFTWARE.
  */
 #include "vl.h"
+#include "sh.h"
 #include <assert.h>
 
 //#define DEBUG_SERIAL
hw/sh_timer.c +1 -0
@@ -9,6 +9,7 @@
  */
 
 #include "vl.h"
+#include "sh.h"
 
 //#define DEBUG_TIMER
 
vl.h +0 -5
@@ -1557,11 +1557,6 @@ int sh7750_register_io_device(struct SH7
 #define TMU012_FEAT_EXTCLK (1 << 2)
 void tmu012_init(uint32_t base, int feat, uint32_t freq);
 
-/* sh_serial.c */
-#define SH_SERIAL_FEAT_SCIF (1 << 0)
-void sh_serial_init (target_phys_addr_t base, int feat,
-		     uint32_t freq, CharDriverState *chr);
-
 /* tc58128.c */
 int tc58128_init(struct SH7750State *s, char *zone1, char *zone2);
 

sh_serial: enable tx after reset

This patch enables tx directly after reset so linux kernel early printk works without boot loader.

---

 hw/sh_serial.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sh_serial.c +1 -1
@@ -291,7 +291,7 @@ void sh_serial_init (target_phys_addr_t 
 
     s->smr = 0;
     s->brr = 0xff;
-    s->scr = 0;
+    s->scr = 1 << 5; /* pretend that TX is enabled so early printk works */
     s->sptr = 0;
 
     if (feat & SH_SERIAL_FEAT_SCIF) {

sh_intc: improve interrupt controller code

This patch improves the sh interrupt controller code. All interrupt sources provided by cpu-specific intc tables are now fully emulated. Interrupts are however still not delivered since the cpu core needs more work. Brave users can comment out INTC_IRQS_DISABLED to enable interrupt delivery.

---

 hw/sh_intc.c |  124 +++++++++++++++++++++++++++++++++++++++++++++-------------
 hw/sh_intc.h |    8 +++
 2 files changed, 104 insertions(+), 28 deletions(-)
hw/sh_intc.c +97 -27

189 lines not on this page · raw message

hw/sh_intc.h +7 -1
@@ -35,9 +35,11 @@ struct intc_source {
     unsigned short vect;
     intc_enum next_enum_id;
 
-    int asserted;
+    int asserted; /* emulates the interrupt signal line from device to intc */
     int enable_count;
     int enable_max;
+    int pending; /* emulates the result of signal and masking */
+    struct intc_desc *parent;
 };
 
 struct intc_desc {
@@ -49,9 +51,13 @@ struct intc_desc {
     int nr_prio_regs;
 
     int iomemtype;
+    int pending; /* number of interrupt sources that has pending set */
 };
 
+unsigned int sh_intc_get_pending_vector(struct intc_desc *desc);
 struct intc_source *sh_intc_source(struct intc_desc *desc, intc_enum id);
+void sh_intc_toggle_source(struct intc_source *source,
+			   int enable_adj, int assert_adj);
 
 void sh_intc_register_sources(struct intc_desc *desc,
 			      struct intc_vect *vectors,

sh: connect cpu core to interrupt controller

This patch connects the cpu core to the interrupt controller.

---

 cpu-exec.c          |    8 +++++++-
 hw/sh7750.c         |    2 ++
 target-sh4/cpu.h    |    3 +++
 target-sh4/helper.c |   47 +++++++++++++++++++++++++++++++----------------
 4 files changed, 43 insertions(+), 17 deletions(-)
cpu-exec.c +7 -1
@@ -517,7 +517,13 @@ int cpu_exec(CPUState *env1)
                         do_interrupt(env);
                     }
 #elif defined(TARGET_SH4)
-		    /* XXXXX */
+                    if ((interrupt_request & CPU_INTERRUPT_HARD)
+                       && (!(env->sr & SR_BL))
+                       && (((env->sr >> 4) & 0xf) != 0x0f)) {
+                        env->exception_index = EXCP_SH_INTERRUPT |
+			  sh_intc_get_pending_vector(env->intc_handle);
+                        do_interrupt(env);
+                    }
 #elif defined(TARGET_ALPHA)
                     if (interrupt_request & CPU_INTERRUPT_HARD) {
                         do_interrupt(env);
hw/sh7750.c +2 -0
@@ -550,6 +550,8 @@ SH7750State *sh7750_init(CPUSH4State * c
 			     _INTC_ARRAY(vectors),
 			     _INTC_ARRAY(groups));
 
+    cpu->intc_handle = &s->intc;
+
     sh_serial_init(0x1fe00000, 0, s->periph_freq, serial_hds[0]);
     sh_serial_init(0x1fe80000, SH_SERIAL_FEAT_SCIF,
 		   s->periph_freq, serial_hds[1]);
target-sh4/cpu.h +3 -0
@@ -121,6 +121,7 @@ typedef struct CPUSH4State {
     int exception_index;
      CPU_COMMON tlb_t utlb[UTLB_SIZE];	/* unified translation table */
     tlb_t itlb[ITLB_SIZE];	/* instruction translation table */
+    void *intc_handle;
 } CPUSH4State;
 
 CPUSH4State *cpu_sh4_init(void);
@@ -163,4 +164,6 @@ enum {
 #define MMUCR_AT (1<<0)
 #define MMUCR_SV (1<<8)
 
+#define EXCP_SH_INTERRUPT 0x8000
+
 #endif				/* _CPU_SH4_H */
target-sh4/helper.c +31 -16

63 lines not on this page · raw message

sh_timer: add interrupt support

This patch adds interrupt support to the sh timer code.

---

 hw/sh.h       |    8 ++++++++
 hw/sh7750.c   |   12 +++++++++---
 hw/sh_timer.c |   32 ++++++++++++++++++--------------
 vl.h          |    6 ------
 4 files changed, 35 insertions(+), 23 deletions(-)
hw/sh.h +8 -0
@@ -8,4 +8,12 @@
 void sh_serial_init (target_phys_addr_t base, int feat,
 		     uint32_t freq, CharDriverState *chr);
 
+/* sh_timer.c */
+#define TMU012_FEAT_TOCR   (1 << 0)
+#define TMU012_FEAT_3CHAN  (1 << 1)
+#define TMU012_FEAT_EXTCLK (1 << 2)
+void tmu012_init(target_phys_addr_t base, int feat, uint32_t freq,
+		 struct intc_source *ch0_irq, struct intc_source *ch1_irq,
+		 struct intc_source *ch2_irq0, struct intc_source *ch2_irq1);
+
 #endif /* __SH_H__ */
hw/sh7750.c +9 -3
@@ -558,8 +558,11 @@ SH7750State *sh7750_init(CPUSH4State * c
 
     tmu012_init(0x1fd80000,
 		TMU012_FEAT_TOCR | TMU012_FEAT_3CHAN | TMU012_FEAT_EXTCLK,
-		s->periph_freq);
-
+		s->periph_freq,
+		sh_intc_source(&s->intc, TMU0),
+		sh_intc_source(&s->intc, TMU1),
+		sh_intc_source(&s->intc, TMU2_TUNI),
+		sh_intc_source(&s->intc, TMU2_TICPI));
 
     if (cpu_model & (SH_CPU_SH7750 | SH_CPU_SH7750S | SH_CPU_SH7751)) {
         sh_intc_register_sources(&s->intc, 
@@ -577,7 +580,10 @@ SH7750State *sh7750_init(CPUSH4State * c
         sh_intc_register_sources(&s->intc, 
 				 _INTC_ARRAY(vectors_tmu34),
 				 NULL, 0);
-        tmu012_init(0x1e100000, 0, s->periph_freq);
+        tmu012_init(0x1e100000, 0, s->periph_freq,
+		    sh_intc_source(&s->intc, TMU3),
+		    sh_intc_source(&s->intc, TMU4),
+		    NULL, NULL);
     }
 
     if (cpu_model & (SH_CPU_SH7751_ALL)) {
hw/sh_timer.c +18 -14
@@ -32,23 +32,23 @@ typedef struct {
     uint32_t tcpr;
     int freq;
     int int_level;
+    int old_level;
     int feat;
     int enabled;
-    qemu_irq irq;
+    struct intc_source *irq;
 } sh_timer_state;
 
 /* Check all active timers, and schedule the next timer interrupt. */
 
 static void sh_timer_update(sh_timer_state *s)
 {
-#if 0 /* not yet */
-    /* Update interrupts.  */
-    if (s->int_level && (s->tcr & TIMER_TCR_UNIE)) {
-        qemu_irq_raise(s->irq);
-    } else {
-        qemu_irq_lower(s->irq);
-    }
-#endif
+    int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE);
+
+    if (new_level != s->old_level)
+      sh_intc_toggle_source(s->irq, 0, new_level ? 1 : -1);
+
+    s->old_level = s->int_level;
+    s->int_level = new_level;
 }
 
 uint32_t sh_timer_read(void *opaque, target_phys_addr_t offset)
@@ -184,7 +184,7 @@ static void sh_timer_tick(void *opaque)
     sh_timer_update(s);
 }
 
-static void *sh_timer_init(uint32_t freq, int feat)
+static void *sh_timer_init(uint32_t freq, int feat, struct intc_source *irq)
 {
     sh_timer_state *s;
     QEMUBH *bh;
@@ -197,6 +197,7 @@ static void *sh_timer_init(uint32_t freq
     s->tcpr = 0xdeadbeef;
     s->tcor = 0;
     s->enabled = 0;
+    s->irq = irq;
 
     bh = qemu_bh_new(sh_timer_tick, s);
     s->timer = ptimer_init(bh);
@@ -304,7 +305,9 @@ static CPUWriteMemoryFunc *tmu012_writef
     tmu012_write
 };
 
-void tmu012_init(uint32_t base, int feat, uint32_t freq)
+void tmu012_init(target_phys_addr_t base, int feat, uint32_t freq,
+		 struct intc_source *ch0_irq, struct intc_source *ch1_irq,
+		 struct intc_source *ch2_irq0, struct intc_source *ch2_irq1)
 {
     int iomemtype;
     tmu012_state *s;
@@ -313,10 +316,11 @@ void tmu012_init(uint32_t base, int feat
     s = (tmu012_state *)qemu_mallocz(sizeof(tmu012_state));
     s->base = base;
     s->feat = feat;
-    s->timer[0] = sh_timer_init(freq, timer_feat);
-    s->timer[1] = sh_timer_init(freq, timer_feat);
+    s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
+    s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
     if (feat & TMU012_FEAT_3CHAN)
-        s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT);
+        s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT,
+				    ch2_irq0); /* ch2_irq1 not supported */
     iomemtype = cpu_register_io_memory(0, tmu012_readfn,
                                        tmu012_writefn, s);
     cpu_register_physical_memory(base, 0x00001000, iomemtype);
vl.h +0 -6
@@ -1361,12 +1361,6 @@ typedef struct {
 
 int sh7750_register_io_device(struct SH7750State *s,
 			      sh7750_io_device * device);
-/* sh_timer.c */
-#define TMU012_FEAT_TOCR   (1 << 0)
-#define TMU012_FEAT_3CHAN  (1 << 1)
-#define TMU012_FEAT_EXTCLK (1 << 2)
-void tmu012_init(uint32_t base, int feat, uint32_t freq);
-
 /* tc58128.c */
 int tc58128_init(struct SH7750State *s, char *zone1, char *zone2);