2 * SuperH interrupt controller module
4 * Copyright (c) 2007 Magnus Damm
5 * Based on sh_timer.c and arm_timer.c by Paul Brook
6 * Copyright (c) 2005-2006 CodeSourcery.
8 * This code is licensed under the GPL.
11 #include "qemu/osdep.h"
13 #include "hw/sh4/sh_intc.h"
15 #include "hw/sh4/sh.h"
18 //#define DEBUG_INTC_SOURCES
20 #define INTC_A7(x) ((x) & 0x1fffffff)
22 void sh_intc_toggle_source(struct intc_source
*source
,
23 int enable_adj
, int assert_adj
)
25 int enable_changed
= 0;
26 int pending_changed
= 0;
29 if ((source
->enable_count
== source
->enable_max
) && (enable_adj
== -1))
32 source
->enable_count
+= enable_adj
;
34 if (source
->enable_count
== source
->enable_max
)
37 source
->asserted
+= assert_adj
;
39 old_pending
= source
->pending
;
40 source
->pending
= source
->asserted
&&
41 (source
->enable_count
== source
->enable_max
);
43 if (old_pending
!= source
->pending
)
46 if (pending_changed
) {
47 if (source
->pending
) {
48 source
->parent
->pending
++;
49 if (source
->parent
->pending
== 1) {
50 cpu_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
53 source
->parent
->pending
--;
54 if (source
->parent
->pending
== 0) {
55 cpu_reset_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
60 if (enable_changed
|| assert_adj
|| pending_changed
) {
61 #ifdef DEBUG_INTC_SOURCES
62 printf("sh_intc: (%d/%d/%d/%d) interrupt source 0x%x %s%s%s\n",
63 source
->parent
->pending
,
68 source
->asserted
? "asserted " :
69 assert_adj
? "deasserted" : "",
70 enable_changed
== 1 ? "enabled " :
71 enable_changed
== -1 ? "disabled " : "",
72 source
->pending
? "pending" : "");
77 static void sh_intc_set_irq (void *opaque
, int n
, int level
)
79 struct intc_desc
*desc
= opaque
;
80 struct intc_source
*source
= &(desc
->sources
[n
]);
82 if (level
&& !source
->asserted
)
83 sh_intc_toggle_source(source
, 0, 1);
84 else if (!level
&& source
->asserted
)
85 sh_intc_toggle_source(source
, 0, -1);
88 int sh_intc_get_pending_vector(struct intc_desc
*desc
, int imask
)
92 /* slow: use a linked lists of pending sources instead */
93 /* wrong: take interrupt priority into account (one list per priority) */
96 return -1; /* FIXME, update code to include priority per source */
99 for (i
= 0; i
< desc
->nr_sources
; i
++) {
100 struct intc_source
*source
= desc
->sources
+ i
;
102 if (source
->pending
) {
103 #ifdef DEBUG_INTC_SOURCES
104 printf("sh_intc: (%d) returning interrupt source 0x%x\n",
105 desc
->pending
, source
->vect
);
114 #define INTC_MODE_NONE 0
115 #define INTC_MODE_DUAL_SET 1
116 #define INTC_MODE_DUAL_CLR 2
117 #define INTC_MODE_ENABLE_REG 3
118 #define INTC_MODE_MASK_REG 4
119 #define INTC_MODE_IS_PRIO 8
121 static unsigned int sh_intc_mode(unsigned long address
,
122 unsigned long set_reg
, unsigned long clr_reg
)
124 if ((address
!= INTC_A7(set_reg
)) &&
125 (address
!= INTC_A7(clr_reg
)))
126 return INTC_MODE_NONE
;
128 if (set_reg
&& clr_reg
) {
129 if (address
== INTC_A7(set_reg
))
130 return INTC_MODE_DUAL_SET
;
132 return INTC_MODE_DUAL_CLR
;
136 return INTC_MODE_ENABLE_REG
;
138 return INTC_MODE_MASK_REG
;
141 static void sh_intc_locate(struct intc_desc
*desc
,
142 unsigned long address
,
143 unsigned long **datap
,
149 unsigned int i
, mode
;
151 /* this is slow but works for now */
153 if (desc
->mask_regs
) {
154 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
155 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
157 mode
= sh_intc_mode(address
, mr
->set_reg
, mr
->clr_reg
);
158 if (mode
== INTC_MODE_NONE
)
163 *enums
= mr
->enum_ids
;
164 *first
= mr
->reg_width
- 1;
170 if (desc
->prio_regs
) {
171 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
172 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
174 mode
= sh_intc_mode(address
, pr
->set_reg
, pr
->clr_reg
);
175 if (mode
== INTC_MODE_NONE
)
178 *modep
= mode
| INTC_MODE_IS_PRIO
;
180 *enums
= pr
->enum_ids
;
181 *first
= (pr
->reg_width
/ pr
->field_width
) - 1;
182 *width
= pr
->field_width
;
190 static void sh_intc_toggle_mask(struct intc_desc
*desc
, intc_enum id
,
191 int enable
, int is_group
)
193 struct intc_source
*source
= desc
->sources
+ id
;
198 if (!source
->next_enum_id
&& (!source
->enable_max
|| !source
->vect
)) {
199 #ifdef DEBUG_INTC_SOURCES
200 printf("sh_intc: reserved interrupt source %d modified\n", id
);
206 sh_intc_toggle_source(source
, enable
? 1 : -1, 0);
210 printf("setting interrupt group %d to %d\n", id
, !!enable
);
214 if ((is_group
|| !source
->vect
) && source
->next_enum_id
) {
215 sh_intc_toggle_mask(desc
, source
->next_enum_id
, enable
, 1);
220 printf("setting interrupt group %d to %d - done\n", id
, !!enable
);
225 static uint64_t sh_intc_read(void *opaque
, hwaddr offset
,
228 struct intc_desc
*desc
= opaque
;
229 intc_enum
*enum_ids
= NULL
;
230 unsigned int first
= 0;
231 unsigned int width
= 0;
232 unsigned int mode
= 0;
233 unsigned long *valuep
;
236 printf("sh_intc_read 0x%lx\n", (unsigned long) offset
);
239 sh_intc_locate(desc
, (unsigned long)offset
, &valuep
,
240 &enum_ids
, &first
, &width
, &mode
);
244 static void sh_intc_write(void *opaque
, hwaddr offset
,
245 uint64_t value
, unsigned size
)
247 struct intc_desc
*desc
= opaque
;
248 intc_enum
*enum_ids
= NULL
;
249 unsigned int first
= 0;
250 unsigned int width
= 0;
251 unsigned int mode
= 0;
253 unsigned long *valuep
;
257 printf("sh_intc_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
260 sh_intc_locate(desc
, (unsigned long)offset
, &valuep
,
261 &enum_ids
, &first
, &width
, &mode
);
264 case INTC_MODE_ENABLE_REG
| INTC_MODE_IS_PRIO
: break;
265 case INTC_MODE_DUAL_SET
: value
|= *valuep
; break;
266 case INTC_MODE_DUAL_CLR
: value
= *valuep
& ~value
; break;
270 for (k
= 0; k
<= first
; k
++) {
271 mask
= ((1 << width
) - 1) << ((first
- k
) * width
);
273 if ((*valuep
& mask
) == (value
& mask
))
276 printf("k = %d, first = %d, enum = %d, mask = 0x%08x\n",
277 k
, first
, enum_ids
[k
], (unsigned int)mask
);
279 sh_intc_toggle_mask(desc
, enum_ids
[k
], value
& mask
, 0);
285 printf("sh_intc_write 0x%lx -> 0x%08x\n", (unsigned long) offset
, value
);
289 static const MemoryRegionOps sh_intc_ops
= {
290 .read
= sh_intc_read
,
291 .write
= sh_intc_write
,
292 .endianness
= DEVICE_NATIVE_ENDIAN
,
295 struct intc_source
*sh_intc_source(struct intc_desc
*desc
, intc_enum id
)
298 return desc
->sources
+ id
;
303 static unsigned int sh_intc_register(MemoryRegion
*sysmem
,
304 struct intc_desc
*desc
,
305 const unsigned long address
,
308 const unsigned int index
)
311 MemoryRegion
*iomem
, *iomem_p4
, *iomem_a7
;
317 iomem
= &desc
->iomem
;
318 iomem_p4
= desc
->iomem_aliases
+ index
;
319 iomem_a7
= iomem_p4
+ 1;
321 #define SH_INTC_IOMEM_FORMAT "interrupt-controller-%s-%s-%s"
322 snprintf(name
, sizeof(name
), SH_INTC_IOMEM_FORMAT
, type
, action
, "p4");
323 memory_region_init_alias(iomem_p4
, NULL
, name
, iomem
, INTC_A7(address
), 4);
324 memory_region_add_subregion(sysmem
, P4ADDR(address
), iomem_p4
);
326 snprintf(name
, sizeof(name
), SH_INTC_IOMEM_FORMAT
, type
, action
, "a7");
327 memory_region_init_alias(iomem_a7
, NULL
, name
, iomem
, INTC_A7(address
), 4);
328 memory_region_add_subregion(sysmem
, A7ADDR(address
), iomem_a7
);
329 #undef SH_INTC_IOMEM_FORMAT
331 /* used to increment aliases index */
335 static void sh_intc_register_source(struct intc_desc
*desc
,
337 struct intc_group
*groups
,
341 struct intc_source
*s
;
343 if (desc
->mask_regs
) {
344 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
345 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
347 for (k
= 0; k
< ARRAY_SIZE(mr
->enum_ids
); k
++) {
348 if (mr
->enum_ids
[k
] != source
)
351 s
= sh_intc_source(desc
, mr
->enum_ids
[k
]);
358 if (desc
->prio_regs
) {
359 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
360 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
362 for (k
= 0; k
< ARRAY_SIZE(pr
->enum_ids
); k
++) {
363 if (pr
->enum_ids
[k
] != source
)
366 s
= sh_intc_source(desc
, pr
->enum_ids
[k
]);
374 for (i
= 0; i
< nr_groups
; i
++) {
375 struct intc_group
*gr
= groups
+ i
;
377 for (k
= 0; k
< ARRAY_SIZE(gr
->enum_ids
); k
++) {
378 if (gr
->enum_ids
[k
] != source
)
381 s
= sh_intc_source(desc
, gr
->enum_ids
[k
]);
390 void sh_intc_register_sources(struct intc_desc
*desc
,
391 struct intc_vect
*vectors
,
393 struct intc_group
*groups
,
397 struct intc_source
*s
;
399 for (i
= 0; i
< nr_vectors
; i
++) {
400 struct intc_vect
*vect
= vectors
+ i
;
402 sh_intc_register_source(desc
, vect
->enum_id
, groups
, nr_groups
);
403 s
= sh_intc_source(desc
, vect
->enum_id
);
405 s
->vect
= vect
->vect
;
407 #ifdef DEBUG_INTC_SOURCES
408 printf("sh_intc: registered source %d -> 0x%04x (%d/%d)\n",
409 vect
->enum_id
, s
->vect
, s
->enable_count
, s
->enable_max
);
415 for (i
= 0; i
< nr_groups
; i
++) {
416 struct intc_group
*gr
= groups
+ i
;
418 s
= sh_intc_source(desc
, gr
->enum_id
);
419 s
->next_enum_id
= gr
->enum_ids
[0];
421 for (k
= 1; k
< ARRAY_SIZE(gr
->enum_ids
); k
++) {
422 if (!gr
->enum_ids
[k
])
425 s
= sh_intc_source(desc
, gr
->enum_ids
[k
- 1]);
426 s
->next_enum_id
= gr
->enum_ids
[k
];
429 #ifdef DEBUG_INTC_SOURCES
430 printf("sh_intc: registered group %d (%d/%d)\n",
431 gr
->enum_id
, s
->enable_count
, s
->enable_max
);
437 int sh_intc_init(MemoryRegion
*sysmem
,
438 struct intc_desc
*desc
,
440 struct intc_mask_reg
*mask_regs
,
442 struct intc_prio_reg
*prio_regs
,
448 desc
->nr_sources
= nr_sources
;
449 desc
->mask_regs
= mask_regs
;
450 desc
->nr_mask_regs
= nr_mask_regs
;
451 desc
->prio_regs
= prio_regs
;
452 desc
->nr_prio_regs
= nr_prio_regs
;
453 /* Allocate 4 MemoryRegions per register (2 actions * 2 aliases).
455 desc
->iomem_aliases
= g_new0(MemoryRegion
,
456 (nr_mask_regs
+ nr_prio_regs
) * 4);
459 i
= sizeof(struct intc_source
) * nr_sources
;
460 desc
->sources
= g_malloc0(i
);
462 for (i
= 0; i
< desc
->nr_sources
; i
++) {
463 struct intc_source
*source
= desc
->sources
+ i
;
465 source
->parent
= desc
;
468 desc
->irqs
= qemu_allocate_irqs(sh_intc_set_irq
, desc
, nr_sources
);
470 memory_region_init_io(&desc
->iomem
, NULL
, &sh_intc_ops
, desc
,
471 "interrupt-controller", 0x100000000ULL
);
473 #define INT_REG_PARAMS(reg_struct, type, action, j) \
474 reg_struct->action##_reg, #type, #action, j
475 if (desc
->mask_regs
) {
476 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
477 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
479 j
+= sh_intc_register(sysmem
, desc
,
480 INT_REG_PARAMS(mr
, mask
, set
, j
));
481 j
+= sh_intc_register(sysmem
, desc
,
482 INT_REG_PARAMS(mr
, mask
, clr
, j
));
486 if (desc
->prio_regs
) {
487 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
488 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
490 j
+= sh_intc_register(sysmem
, desc
,
491 INT_REG_PARAMS(pr
, prio
, set
, j
));
492 j
+= sh_intc_register(sysmem
, desc
,
493 INT_REG_PARAMS(pr
, prio
, clr
, j
));
496 #undef INT_REG_PARAMS
501 /* Assert level <n> IRL interrupt.
502 0:deassert. 1:lowest priority,... 15:highest priority. */
503 void sh_intc_set_irl(void *opaque
, int n
, int level
)
505 struct intc_source
*s
= opaque
;
506 int i
, irl
= level
^ 15;
507 for (i
= 0; (s
= sh_intc_source(s
->parent
, s
->next_enum_id
)); i
++) {
509 sh_intc_toggle_source(s
, s
->enable_count
?0:1, s
->asserted
?0:1);
512 sh_intc_toggle_source(s
, 0, -1);