2 * Arm PrimeCell PL190 Vector Interrupt Controller
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
13 #include "qemu/module.h"
15 /* The number of virtual priority levels. 16 user vectors plus the
16 unvectored IRQ. Chained interrupts would require an additional level
19 #define PL190_NUM_PRIO 17
21 #define TYPE_PL190 "pl190"
22 #define PL190(obj) OBJECT_CHECK(PL190State, (obj), TYPE_PL190)
24 typedef struct PL190State
{
25 SysBusDevice parent_obj
;
32 uint8_t vect_control
[16];
33 uint32_t vect_addr
[PL190_NUM_PRIO
];
34 /* Mask containing interrupts with higher priority than this one. */
35 uint32_t prio_mask
[PL190_NUM_PRIO
+ 1];
37 /* Current priority level. */
39 int prev_prio
[PL190_NUM_PRIO
];
44 static const unsigned char pl190_id
[] =
45 { 0x90, 0x11, 0x04, 0x00, 0x0D, 0xf0, 0x05, 0xb1 };
47 static inline uint32_t pl190_irq_level(PL190State
*s
)
49 return (s
->level
| s
->soft_level
) & s
->irq_enable
& ~s
->fiq_select
;
52 /* Update interrupts. */
53 static void pl190_update(PL190State
*s
)
55 uint32_t level
= pl190_irq_level(s
);
58 set
= (level
& s
->prio_mask
[s
->priority
]) != 0;
59 qemu_set_irq(s
->irq
, set
);
60 set
= ((s
->level
| s
->soft_level
) & s
->fiq_select
) != 0;
61 qemu_set_irq(s
->fiq
, set
);
64 static void pl190_set_irq(void *opaque
, int irq
, int level
)
66 PL190State
*s
= (PL190State
*)opaque
;
69 s
->level
|= 1u << irq
;
71 s
->level
&= ~(1u << irq
);
75 static void pl190_update_vectors(PL190State
*s
)
82 for (i
= 0; i
< 16; i
++)
84 s
->prio_mask
[i
] = mask
;
85 if (s
->vect_control
[i
] & 0x20)
87 n
= s
->vect_control
[i
] & 0x1f;
91 s
->prio_mask
[16] = mask
;
95 static uint64_t pl190_read(void *opaque
, hwaddr offset
,
98 PL190State
*s
= (PL190State
*)opaque
;
101 if (offset
>= 0xfe0 && offset
< 0x1000) {
102 return pl190_id
[(offset
- 0xfe0) >> 2];
104 if (offset
>= 0x100 && offset
< 0x140) {
105 return s
->vect_addr
[(offset
- 0x100) >> 2];
107 if (offset
>= 0x200 && offset
< 0x240) {
108 return s
->vect_control
[(offset
- 0x200) >> 2];
110 switch (offset
>> 2) {
111 case 0: /* IRQSTATUS */
112 return pl190_irq_level(s
);
113 case 1: /* FIQSATUS */
114 return (s
->level
| s
->soft_level
) & s
->fiq_select
;
115 case 2: /* RAWINTR */
116 return s
->level
| s
->soft_level
;
117 case 3: /* INTSELECT */
118 return s
->fiq_select
;
119 case 4: /* INTENABLE */
120 return s
->irq_enable
;
121 case 6: /* SOFTINT */
122 return s
->soft_level
;
123 case 8: /* PROTECTION */
125 case 12: /* VECTADDR */
126 /* Read vector address at the start of an ISR. Increases the
127 * current priority level to that of the current interrupt.
129 * Since an enabled interrupt X at priority P causes prio_mask[Y]
130 * to have bit X set for all Y > P, this loop will stop with
131 * i == the priority of the highest priority set interrupt.
133 for (i
= 0; i
< s
->priority
; i
++) {
134 if ((s
->level
| s
->soft_level
) & s
->prio_mask
[i
+ 1]) {
139 /* Reading this value with no pending interrupts is undefined.
140 We return the default address. */
141 if (i
== PL190_NUM_PRIO
)
142 return s
->vect_addr
[16];
145 s
->prev_prio
[i
] = s
->priority
;
149 return s
->vect_addr
[s
->priority
];
150 case 13: /* DEFVECTADDR */
151 return s
->vect_addr
[16];
153 qemu_log_mask(LOG_GUEST_ERROR
,
154 "pl190_read: Bad offset %x\n", (int)offset
);
159 static void pl190_write(void *opaque
, hwaddr offset
,
160 uint64_t val
, unsigned size
)
162 PL190State
*s
= (PL190State
*)opaque
;
164 if (offset
>= 0x100 && offset
< 0x140) {
165 s
->vect_addr
[(offset
- 0x100) >> 2] = val
;
166 pl190_update_vectors(s
);
169 if (offset
>= 0x200 && offset
< 0x240) {
170 s
->vect_control
[(offset
- 0x200) >> 2] = val
;
171 pl190_update_vectors(s
);
174 switch (offset
>> 2) {
176 /* This is a readonly register, but linux tries to write to it
177 anyway. Ignore the write. */
179 case 3: /* INTSELECT */
182 case 4: /* INTENABLE */
183 s
->irq_enable
|= val
;
185 case 5: /* INTENCLEAR */
186 s
->irq_enable
&= ~val
;
188 case 6: /* SOFTINT */
189 s
->soft_level
|= val
;
191 case 7: /* SOFTINTCLEAR */
192 s
->soft_level
&= ~val
;
194 case 8: /* PROTECTION */
195 /* TODO: Protection (supervisor only access) is not implemented. */
196 s
->protected = val
& 1;
198 case 12: /* VECTADDR */
199 /* Restore the previous priority level. The value written is
201 if (s
->priority
< PL190_NUM_PRIO
)
202 s
->priority
= s
->prev_prio
[s
->priority
];
204 case 13: /* DEFVECTADDR */
205 s
->vect_addr
[16] = val
;
207 case 0xc0: /* ITCR */
209 qemu_log_mask(LOG_UNIMP
, "pl190: Test mode not implemented\n");
213 qemu_log_mask(LOG_GUEST_ERROR
,
214 "pl190_write: Bad offset %x\n", (int)offset
);
220 static const MemoryRegionOps pl190_ops
= {
222 .write
= pl190_write
,
223 .endianness
= DEVICE_NATIVE_ENDIAN
,
226 static void pl190_reset(DeviceState
*d
)
228 PL190State
*s
= PL190(d
);
231 for (i
= 0; i
< 16; i
++) {
233 s
->vect_control
[i
] = 0;
235 s
->vect_addr
[16] = 0;
236 s
->prio_mask
[17] = 0xffffffff;
237 s
->priority
= PL190_NUM_PRIO
;
238 pl190_update_vectors(s
);
241 static void pl190_init(Object
*obj
)
243 DeviceState
*dev
= DEVICE(obj
);
244 PL190State
*s
= PL190(obj
);
245 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
247 memory_region_init_io(&s
->iomem
, obj
, &pl190_ops
, s
, "pl190", 0x1000);
248 sysbus_init_mmio(sbd
, &s
->iomem
);
249 qdev_init_gpio_in(dev
, pl190_set_irq
, 32);
250 sysbus_init_irq(sbd
, &s
->irq
);
251 sysbus_init_irq(sbd
, &s
->fiq
);
254 static const VMStateDescription vmstate_pl190
= {
257 .minimum_version_id
= 1,
258 .fields
= (VMStateField
[]) {
259 VMSTATE_UINT32(level
, PL190State
),
260 VMSTATE_UINT32(soft_level
, PL190State
),
261 VMSTATE_UINT32(irq_enable
, PL190State
),
262 VMSTATE_UINT32(fiq_select
, PL190State
),
263 VMSTATE_UINT8_ARRAY(vect_control
, PL190State
, 16),
264 VMSTATE_UINT32_ARRAY(vect_addr
, PL190State
, PL190_NUM_PRIO
),
265 VMSTATE_UINT32_ARRAY(prio_mask
, PL190State
, PL190_NUM_PRIO
+1),
266 VMSTATE_INT32(protected, PL190State
),
267 VMSTATE_INT32(priority
, PL190State
),
268 VMSTATE_INT32_ARRAY(prev_prio
, PL190State
, PL190_NUM_PRIO
),
269 VMSTATE_END_OF_LIST()
273 static void pl190_class_init(ObjectClass
*klass
, void *data
)
275 DeviceClass
*dc
= DEVICE_CLASS(klass
);
277 dc
->reset
= pl190_reset
;
278 dc
->vmsd
= &vmstate_pl190
;
281 static const TypeInfo pl190_info
= {
283 .parent
= TYPE_SYS_BUS_DEVICE
,
284 .instance_size
= sizeof(PL190State
),
285 .instance_init
= pl190_init
,
286 .class_init
= pl190_class_init
,
289 static void pl190_register_types(void)
291 type_register_static(&pl190_info
);
294 type_init(pl190_register_types
)