2 * i.MX31 Vectored Interrupt Controller
4 * Note this is NOT the PL192 provided by ARM, but
5 * a custom implementation by Freescale.
7 * Copyright (c) 2008 OKL
8 * Copyright (c) 2011 NICTA Pty Ltd
9 * Originally written by Hans Jiang
10 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
12 * This code is licensed under the GPL version 2 or later. See
13 * the COPYING file in the top-level directory.
15 * TODO: implement vectors.
18 #include "qemu/osdep.h"
19 #include "hw/intc/imx_avic.h"
22 #ifndef DEBUG_IMX_AVIC
23 #define DEBUG_IMX_AVIC 0
26 #define DPRINTF(fmt, args...) \
28 if (DEBUG_IMX_AVIC) { \
29 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_AVIC, \
34 static const VMStateDescription vmstate_imx_avic
= {
35 .name
= TYPE_IMX_AVIC
,
37 .minimum_version_id
= 1,
38 .fields
= (VMStateField
[]) {
39 VMSTATE_UINT64(pending
, IMXAVICState
),
40 VMSTATE_UINT64(enabled
, IMXAVICState
),
41 VMSTATE_UINT64(is_fiq
, IMXAVICState
),
42 VMSTATE_UINT32(intcntl
, IMXAVICState
),
43 VMSTATE_UINT32(intmask
, IMXAVICState
),
44 VMSTATE_UINT32_ARRAY(prio
, IMXAVICState
, PRIO_WORDS
),
49 static inline int imx_avic_prio(IMXAVICState
*s
, int irq
)
51 uint32_t word
= irq
/ PRIO_PER_WORD
;
52 uint32_t part
= 4 * (irq
% PRIO_PER_WORD
);
53 return 0xf & (s
->prio
[word
] >> part
);
56 /* Update interrupts. */
57 static void imx_avic_update(IMXAVICState
*s
)
60 uint64_t new = s
->pending
& s
->enabled
;
63 flags
= new & s
->is_fiq
;
64 qemu_set_irq(s
->fiq
, !!flags
);
66 flags
= new & ~s
->is_fiq
;
67 if (!flags
|| (s
->intmask
== 0x1f)) {
68 qemu_set_irq(s
->irq
, !!flags
);
73 * Take interrupt if there's a pending interrupt with
74 * priority higher than the value of intmask
76 for (i
= 0; i
< IMX_AVIC_NUM_IRQS
; i
++) {
77 if (flags
& (1UL << i
)) {
78 if (imx_avic_prio(s
, i
) > s
->intmask
) {
79 qemu_set_irq(s
->irq
, 1);
84 qemu_set_irq(s
->irq
, 0);
87 static void imx_avic_set_irq(void *opaque
, int irq
, int level
)
89 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
92 DPRINTF("Raising IRQ %d, prio %d\n",
93 irq
, imx_avic_prio(s
, irq
));
94 s
->pending
|= (1ULL << irq
);
96 DPRINTF("Clearing IRQ %d, prio %d\n",
97 irq
, imx_avic_prio(s
, irq
));
98 s
->pending
&= ~(1ULL << irq
);
105 static uint64_t imx_avic_read(void *opaque
,
106 hwaddr offset
, unsigned size
)
108 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
110 DPRINTF("read(offset = 0x%" HWADDR_PRIx
")\n", offset
);
112 switch (offset
>> 2) {
113 case 0: /* INTCNTL */
116 case 1: /* Normal Interrupt Mask Register, NIMASK */
119 case 2: /* Interrupt Enable Number Register, INTENNUM */
120 case 3: /* Interrupt Disable Number Register, INTDISNUM */
123 case 4: /* Interrupt Enabled Number Register High */
124 return s
->enabled
>> 32;
126 case 5: /* Interrupt Enabled Number Register Low */
127 return s
->enabled
& 0xffffffffULL
;
129 case 6: /* Interrupt Type Register High */
130 return s
->is_fiq
>> 32;
132 case 7: /* Interrupt Type Register Low */
133 return s
->is_fiq
& 0xffffffffULL
;
135 case 8: /* Normal Interrupt Priority Register 7 */
136 case 9: /* Normal Interrupt Priority Register 6 */
137 case 10:/* Normal Interrupt Priority Register 5 */
138 case 11:/* Normal Interrupt Priority Register 4 */
139 case 12:/* Normal Interrupt Priority Register 3 */
140 case 13:/* Normal Interrupt Priority Register 2 */
141 case 14:/* Normal Interrupt Priority Register 1 */
142 case 15:/* Normal Interrupt Priority Register 0 */
143 return s
->prio
[15-(offset
>>2)];
145 case 16: /* Normal interrupt vector and status register */
148 * This returns the highest priority
149 * outstanding interrupt. Where there is more than
150 * one pending IRQ with the same priority,
151 * take the highest numbered one.
153 uint64_t flags
= s
->pending
& s
->enabled
& ~s
->is_fiq
;
157 for (i
= 63; i
>= 0; --i
) {
158 if (flags
& (1ULL<<i
)) {
159 int irq_prio
= imx_avic_prio(s
, i
);
160 if (irq_prio
> prio
) {
167 imx_avic_set_irq(s
, irq
, 0);
168 return irq
<< 16 | prio
;
170 return 0xffffffffULL
;
172 case 17:/* Fast Interrupt vector and status register */
174 uint64_t flags
= s
->pending
& s
->enabled
& s
->is_fiq
;
175 int i
= ctz64(flags
);
177 imx_avic_set_irq(opaque
, i
, 0);
180 return 0xffffffffULL
;
182 case 18:/* Interrupt source register high */
183 return s
->pending
>> 32;
185 case 19:/* Interrupt source register low */
186 return s
->pending
& 0xffffffffULL
;
188 case 20:/* Interrupt Force Register high */
189 case 21:/* Interrupt Force Register low */
192 case 22:/* Normal Interrupt Pending Register High */
193 return (s
->pending
& s
->enabled
& ~s
->is_fiq
) >> 32;
195 case 23:/* Normal Interrupt Pending Register Low */
196 return (s
->pending
& s
->enabled
& ~s
->is_fiq
) & 0xffffffffULL
;
198 case 24: /* Fast Interrupt Pending Register High */
199 return (s
->pending
& s
->enabled
& s
->is_fiq
) >> 32;
201 case 25: /* Fast Interrupt Pending Register Low */
202 return (s
->pending
& s
->enabled
& s
->is_fiq
) & 0xffffffffULL
;
204 case 0x40: /* AVIC vector 0, use for WFI WAR */
208 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
209 HWADDR_PRIx
"\n", TYPE_IMX_AVIC
, __func__
, offset
);
214 static void imx_avic_write(void *opaque
, hwaddr offset
,
215 uint64_t val
, unsigned size
)
217 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
219 /* Vector Registers not yet supported */
220 if (offset
>= 0x100 && offset
<= 0x2fc) {
221 qemu_log_mask(LOG_UNIMP
, "[%s]%s: vector %d ignored\n",
222 TYPE_IMX_AVIC
, __func__
, (int)((offset
- 0x100) >> 2));
226 DPRINTF("(0x%" HWADDR_PRIx
") = 0x%x\n", offset
, (unsigned int)val
);
228 switch (offset
>> 2) {
229 case 0: /* Interrupt Control Register, INTCNTL */
230 s
->intcntl
= val
& (ABFEN
| NIDIS
| FIDIS
| NIAD
| FIAD
| NM
);
231 if (s
->intcntl
& ABFEN
) {
232 s
->intcntl
&= ~(val
& ABFLAG
);
236 case 1: /* Normal Interrupt Mask Register, NIMASK */
237 s
->intmask
= val
& 0x1f;
240 case 2: /* Interrupt Enable Number Register, INTENNUM */
241 DPRINTF("enable(%d)\n", (int)val
);
243 s
->enabled
|= (1ULL << val
);
246 case 3: /* Interrupt Disable Number Register, INTDISNUM */
247 DPRINTF("disable(%d)\n", (int)val
);
249 s
->enabled
&= ~(1ULL << val
);
252 case 4: /* Interrupt Enable Number Register High */
253 s
->enabled
= (s
->enabled
& 0xffffffffULL
) | (val
<< 32);
256 case 5: /* Interrupt Enable Number Register Low */
257 s
->enabled
= (s
->enabled
& 0xffffffff00000000ULL
) | val
;
260 case 6: /* Interrupt Type Register High */
261 s
->is_fiq
= (s
->is_fiq
& 0xffffffffULL
) | (val
<< 32);
264 case 7: /* Interrupt Type Register Low */
265 s
->is_fiq
= (s
->is_fiq
& 0xffffffff00000000ULL
) | val
;
268 case 8: /* Normal Interrupt Priority Register 7 */
269 case 9: /* Normal Interrupt Priority Register 6 */
270 case 10:/* Normal Interrupt Priority Register 5 */
271 case 11:/* Normal Interrupt Priority Register 4 */
272 case 12:/* Normal Interrupt Priority Register 3 */
273 case 13:/* Normal Interrupt Priority Register 2 */
274 case 14:/* Normal Interrupt Priority Register 1 */
275 case 15:/* Normal Interrupt Priority Register 0 */
276 s
->prio
[15-(offset
>>2)] = val
;
279 /* Read-only registers, writes ignored */
280 case 16:/* Normal Interrupt Vector and Status register */
281 case 17:/* Fast Interrupt vector and status register */
282 case 18:/* Interrupt source register high */
283 case 19:/* Interrupt source register low */
286 case 20:/* Interrupt Force Register high */
287 s
->pending
= (s
->pending
& 0xffffffffULL
) | (val
<< 32);
290 case 21:/* Interrupt Force Register low */
291 s
->pending
= (s
->pending
& 0xffffffff00000000ULL
) | val
;
294 case 22:/* Normal Interrupt Pending Register High */
295 case 23:/* Normal Interrupt Pending Register Low */
296 case 24: /* Fast Interrupt Pending Register High */
297 case 25: /* Fast Interrupt Pending Register Low */
301 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
302 HWADDR_PRIx
"\n", TYPE_IMX_AVIC
, __func__
, offset
);
307 static const MemoryRegionOps imx_avic_ops
= {
308 .read
= imx_avic_read
,
309 .write
= imx_avic_write
,
310 .endianness
= DEVICE_NATIVE_ENDIAN
,
313 static void imx_avic_reset(DeviceState
*dev
)
315 IMXAVICState
*s
= IMX_AVIC(dev
);
322 memset(s
->prio
, 0, sizeof s
->prio
);
325 static void imx_avic_init(Object
*obj
)
327 DeviceState
*dev
= DEVICE(obj
);
328 IMXAVICState
*s
= IMX_AVIC(obj
);
329 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
331 memory_region_init_io(&s
->iomem
, obj
, &imx_avic_ops
, s
,
332 TYPE_IMX_AVIC
, 0x1000);
333 sysbus_init_mmio(sbd
, &s
->iomem
);
335 qdev_init_gpio_in(dev
, imx_avic_set_irq
, IMX_AVIC_NUM_IRQS
);
336 sysbus_init_irq(sbd
, &s
->irq
);
337 sysbus_init_irq(sbd
, &s
->fiq
);
341 static void imx_avic_class_init(ObjectClass
*klass
, void *data
)
343 DeviceClass
*dc
= DEVICE_CLASS(klass
);
345 dc
->vmsd
= &vmstate_imx_avic
;
346 dc
->reset
= imx_avic_reset
;
347 dc
->desc
= "i.MX Advanced Vector Interrupt Controller";
350 static const TypeInfo imx_avic_info
= {
351 .name
= TYPE_IMX_AVIC
,
352 .parent
= TYPE_SYS_BUS_DEVICE
,
353 .instance_size
= sizeof(IMXAVICState
),
354 .instance_init
= imx_avic_init
,
355 .class_init
= imx_avic_class_init
,
358 static void imx_avic_register_types(void)
360 type_register_static(&imx_avic_info
);
363 type_init(imx_avic_register_types
)