]> Git Repo - qemu.git/blame - hw/intc/pl190.c
apic: fix incorrect handling of ExtINT interrupts wrt processor priority
[qemu.git] / hw / intc / pl190.c
CommitLineData
5fafdf24 1/*
cdbdb648
PB
2 * Arm PrimeCell PL190 Vector Interrupt Controller
3 *
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
cdbdb648
PB
8 */
9
83c9f4ca 10#include "hw/sysbus.h"
cdbdb648
PB
11
12/* The number of virtual priority levels. 16 user vectors plus the
13 unvectored IRQ. Chained interrupts would require an additional level
14 if implemented. */
15
16#define PL190_NUM_PRIO 17
17
7fc3266f
AF
18#define TYPE_PL190 "pl190"
19#define PL190(obj) OBJECT_CHECK(PL190State, (obj), TYPE_PL190)
20
aefbc256 21typedef struct PL190State {
7fc3266f
AF
22 SysBusDevice parent_obj;
23
7f8293bf 24 MemoryRegion iomem;
cdbdb648
PB
25 uint32_t level;
26 uint32_t soft_level;
27 uint32_t irq_enable;
28 uint32_t fiq_select;
cdbdb648
PB
29 uint8_t vect_control[16];
30 uint32_t vect_addr[PL190_NUM_PRIO];
31 /* Mask containing interrupts with higher priority than this one. */
32 uint32_t prio_mask[PL190_NUM_PRIO + 1];
33 int protected;
34 /* Current priority level. */
35 int priority;
36 int prev_prio[PL190_NUM_PRIO];
d537cf6c
PB
37 qemu_irq irq;
38 qemu_irq fiq;
aefbc256 39} PL190State;
cdbdb648
PB
40
41static const unsigned char pl190_id[] =
42{ 0x90, 0x11, 0x04, 0x00, 0x0D, 0xf0, 0x05, 0xb1 };
43
aefbc256 44static inline uint32_t pl190_irq_level(PL190State *s)
cdbdb648
PB
45{
46 return (s->level | s->soft_level) & s->irq_enable & ~s->fiq_select;
47}
48
49/* Update interrupts. */
aefbc256 50static void pl190_update(PL190State *s)
cdbdb648
PB
51{
52 uint32_t level = pl190_irq_level(s);
53 int set;
54
55 set = (level & s->prio_mask[s->priority]) != 0;
d537cf6c 56 qemu_set_irq(s->irq, set);
cdbdb648 57 set = ((s->level | s->soft_level) & s->fiq_select) != 0;
d537cf6c 58 qemu_set_irq(s->fiq, set);
cdbdb648
PB
59}
60
61static void pl190_set_irq(void *opaque, int irq, int level)
62{
aefbc256 63 PL190State *s = (PL190State *)opaque;
cdbdb648
PB
64
65 if (level)
66 s->level |= 1u << irq;
67 else
68 s->level &= ~(1u << irq);
69 pl190_update(s);
70}
71
aefbc256 72static void pl190_update_vectors(PL190State *s)
cdbdb648
PB
73{
74 uint32_t mask;
75 int i;
76 int n;
77
78 mask = 0;
79 for (i = 0; i < 16; i++)
80 {
81 s->prio_mask[i] = mask;
82 if (s->vect_control[i] & 0x20)
83 {
84 n = s->vect_control[i] & 0x1f;
85 mask |= 1 << n;
86 }
87 }
88 s->prio_mask[16] = mask;
89 pl190_update(s);
90}
91
a8170e5e 92static uint64_t pl190_read(void *opaque, hwaddr offset,
7f8293bf 93 unsigned size)
cdbdb648 94{
aefbc256 95 PL190State *s = (PL190State *)opaque;
cdbdb648
PB
96 int i;
97
cdbdb648
PB
98 if (offset >= 0xfe0 && offset < 0x1000) {
99 return pl190_id[(offset - 0xfe0) >> 2];
100 }
101 if (offset >= 0x100 && offset < 0x140) {
102 return s->vect_addr[(offset - 0x100) >> 2];
103 }
104 if (offset >= 0x200 && offset < 0x240) {
105 return s->vect_control[(offset - 0x200) >> 2];
106 }
107 switch (offset >> 2) {
108 case 0: /* IRQSTATUS */
109 return pl190_irq_level(s);
110 case 1: /* FIQSATUS */
111 return (s->level | s->soft_level) & s->fiq_select;
112 case 2: /* RAWINTR */
113 return s->level | s->soft_level;
114 case 3: /* INTSELECT */
115 return s->fiq_select;
116 case 4: /* INTENABLE */
117 return s->irq_enable;
118 case 6: /* SOFTINT */
119 return s->soft_level;
120 case 8: /* PROTECTION */
121 return s->protected;
122 case 12: /* VECTADDR */
123 /* Read vector address at the start of an ISR. Increases the
14c126ba
BF
124 * current priority level to that of the current interrupt.
125 *
126 * Since an enabled interrupt X at priority P causes prio_mask[Y]
127 * to have bit X set for all Y > P, this loop will stop with
128 * i == the priority of the highest priority set interrupt.
129 */
130 for (i = 0; i < s->priority; i++) {
131 if ((s->level | s->soft_level) & s->prio_mask[i + 1]) {
132 break;
133 }
134 }
135
cdbdb648
PB
136 /* Reading this value with no pending interrupts is undefined.
137 We return the default address. */
138 if (i == PL190_NUM_PRIO)
139 return s->vect_addr[16];
140 if (i < s->priority)
141 {
142 s->prev_prio[i] = s->priority;
143 s->priority = i;
144 pl190_update(s);
145 }
146 return s->vect_addr[s->priority];
147 case 13: /* DEFVECTADDR */
148 return s->vect_addr[16];
149 default:
fd271e81
PM
150 qemu_log_mask(LOG_GUEST_ERROR,
151 "pl190_read: Bad offset %x\n", (int)offset);
cdbdb648
PB
152 return 0;
153 }
154}
155
a8170e5e 156static void pl190_write(void *opaque, hwaddr offset,
7f8293bf 157 uint64_t val, unsigned size)
cdbdb648 158{
aefbc256 159 PL190State *s = (PL190State *)opaque;
cdbdb648 160
cdbdb648
PB
161 if (offset >= 0x100 && offset < 0x140) {
162 s->vect_addr[(offset - 0x100) >> 2] = val;
163 pl190_update_vectors(s);
164 return;
165 }
166 if (offset >= 0x200 && offset < 0x240) {
167 s->vect_control[(offset - 0x200) >> 2] = val;
168 pl190_update_vectors(s);
169 return;
170 }
171 switch (offset >> 2) {
172 case 0: /* SELECT */
173 /* This is a readonly register, but linux tries to write to it
174 anyway. Ignore the write. */
175 break;
176 case 3: /* INTSELECT */
177 s->fiq_select = val;
178 break;
179 case 4: /* INTENABLE */
180 s->irq_enable |= val;
181 break;
182 case 5: /* INTENCLEAR */
183 s->irq_enable &= ~val;
184 break;
185 case 6: /* SOFTINT */
186 s->soft_level |= val;
187 break;
188 case 7: /* SOFTINTCLEAR */
189 s->soft_level &= ~val;
190 break;
191 case 8: /* PROTECTION */
192 /* TODO: Protection (supervisor only access) is not implemented. */
193 s->protected = val & 1;
194 break;
195 case 12: /* VECTADDR */
196 /* Restore the previous priority level. The value written is
197 ignored. */
198 if (s->priority < PL190_NUM_PRIO)
199 s->priority = s->prev_prio[s->priority];
200 break;
201 case 13: /* DEFVECTADDR */
730986e4 202 s->vect_addr[16] = val;
cdbdb648
PB
203 break;
204 case 0xc0: /* ITCR */
2ac71179 205 if (val) {
2d746989 206 qemu_log_mask(LOG_UNIMP, "pl190: Test mode not implemented\n");
2ac71179 207 }
cdbdb648
PB
208 break;
209 default:
fd271e81
PM
210 qemu_log_mask(LOG_GUEST_ERROR,
211 "pl190_write: Bad offset %x\n", (int)offset);
cdbdb648
PB
212 return;
213 }
214 pl190_update(s);
215}
216
7f8293bf
AK
217static const MemoryRegionOps pl190_ops = {
218 .read = pl190_read,
219 .write = pl190_write,
220 .endianness = DEVICE_NATIVE_ENDIAN,
cdbdb648
PB
221};
222
ac49d750 223static void pl190_reset(DeviceState *d)
cdbdb648 224{
7fc3266f
AF
225 PL190State *s = PL190(d);
226 int i;
cdbdb648 227
7fc3266f
AF
228 for (i = 0; i < 16; i++) {
229 s->vect_addr[i] = 0;
230 s->vect_control[i] = 0;
cdbdb648 231 }
7fc3266f
AF
232 s->vect_addr[16] = 0;
233 s->prio_mask[17] = 0xffffffff;
234 s->priority = PL190_NUM_PRIO;
235 pl190_update_vectors(s);
cdbdb648
PB
236}
237
7fc3266f 238static int pl190_init(SysBusDevice *sbd)
cdbdb648 239{
7fc3266f
AF
240 DeviceState *dev = DEVICE(sbd);
241 PL190State *s = PL190(dev);
cdbdb648 242
1437c94b 243 memory_region_init_io(&s->iomem, OBJECT(s), &pl190_ops, s, "pl190", 0x1000);
7fc3266f
AF
244 sysbus_init_mmio(sbd, &s->iomem);
245 qdev_init_gpio_in(dev, pl190_set_irq, 32);
246 sysbus_init_irq(sbd, &s->irq);
247 sysbus_init_irq(sbd, &s->fiq);
81a322d4 248 return 0;
cdbdb648 249}
97aff481 250
ac49d750
PM
251static const VMStateDescription vmstate_pl190 = {
252 .name = "pl190",
253 .version_id = 1,
254 .minimum_version_id = 1,
255 .fields = (VMStateField[]) {
aefbc256
AF
256 VMSTATE_UINT32(level, PL190State),
257 VMSTATE_UINT32(soft_level, PL190State),
258 VMSTATE_UINT32(irq_enable, PL190State),
259 VMSTATE_UINT32(fiq_select, PL190State),
260 VMSTATE_UINT8_ARRAY(vect_control, PL190State, 16),
261 VMSTATE_UINT32_ARRAY(vect_addr, PL190State, PL190_NUM_PRIO),
262 VMSTATE_UINT32_ARRAY(prio_mask, PL190State, PL190_NUM_PRIO+1),
263 VMSTATE_INT32(protected, PL190State),
264 VMSTATE_INT32(priority, PL190State),
265 VMSTATE_INT32_ARRAY(prev_prio, PL190State, PL190_NUM_PRIO),
ac49d750
PM
266 VMSTATE_END_OF_LIST()
267 }
268};
269
999e12bb
AL
270static void pl190_class_init(ObjectClass *klass, void *data)
271{
39bffca2 272 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
273 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
274
275 k->init = pl190_init;
39bffca2
AL
276 dc->reset = pl190_reset;
277 dc->vmsd = &vmstate_pl190;
999e12bb
AL
278}
279
8c43a6f0 280static const TypeInfo pl190_info = {
7fc3266f 281 .name = TYPE_PL190,
39bffca2 282 .parent = TYPE_SYS_BUS_DEVICE,
aefbc256 283 .instance_size = sizeof(PL190State),
39bffca2 284 .class_init = pl190_class_init,
ac49d750
PM
285};
286
83f7d43a 287static void pl190_register_types(void)
97aff481 288{
39bffca2 289 type_register_static(&pl190_info);
97aff481
PB
290}
291
83f7d43a 292type_init(pl190_register_types)
This page took 0.823351 seconds and 4 git commands to generate.