root / hw / arm_gic.c @ 83fa1010
History | View | Annotate | Download (15.3 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | e69954b9 | pbrook | * ARM AMBA Generic/Distributed Interrupt Controller
|
3 | e69954b9 | pbrook | *
|
4 | e69954b9 | pbrook | * Copyright (c) 2006 CodeSourcery.
|
5 | e69954b9 | pbrook | * Written by Paul Brook
|
6 | e69954b9 | pbrook | *
|
7 | e69954b9 | pbrook | * This code is licenced under the GPL.
|
8 | e69954b9 | pbrook | */
|
9 | e69954b9 | pbrook | |
10 | e69954b9 | pbrook | /* TODO: Some variants of this controller can handle multiple CPUs.
|
11 | e69954b9 | pbrook | Currently only single CPU operation is implemented. */
|
12 | e69954b9 | pbrook | |
13 | e69954b9 | pbrook | #include "vl.h" |
14 | e69954b9 | pbrook | #include "arm_pic.h" |
15 | e69954b9 | pbrook | |
16 | e69954b9 | pbrook | //#define DEBUG_GIC
|
17 | e69954b9 | pbrook | |
18 | e69954b9 | pbrook | #ifdef DEBUG_GIC
|
19 | e69954b9 | pbrook | #define DPRINTF(fmt, args...) \
|
20 | df628ff1 | pbrook | do { printf("arm_gic: " fmt , ##args); } while (0) |
21 | e69954b9 | pbrook | #else
|
22 | e69954b9 | pbrook | #define DPRINTF(fmt, args...) do {} while(0) |
23 | e69954b9 | pbrook | #endif
|
24 | e69954b9 | pbrook | |
25 | e69954b9 | pbrook | /* Distributed interrupt controller. */
|
26 | e69954b9 | pbrook | |
27 | e69954b9 | pbrook | static const uint8_t gic_id[] = |
28 | e69954b9 | pbrook | { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; |
29 | e69954b9 | pbrook | |
30 | e69954b9 | pbrook | #define GIC_NIRQ 96 |
31 | e69954b9 | pbrook | |
32 | e69954b9 | pbrook | typedef struct gic_irq_state |
33 | e69954b9 | pbrook | { |
34 | e69954b9 | pbrook | unsigned enabled:1; |
35 | e69954b9 | pbrook | unsigned pending:1; |
36 | e69954b9 | pbrook | unsigned active:1; |
37 | e69954b9 | pbrook | unsigned level:1; |
38 | e69954b9 | pbrook | unsigned model:1; /* 0 = 1:N, 1 = N:N */ |
39 | e69954b9 | pbrook | unsigned trigger:1; /* nonzero = edge triggered. */ |
40 | e69954b9 | pbrook | } gic_irq_state; |
41 | e69954b9 | pbrook | |
42 | e69954b9 | pbrook | #define GIC_SET_ENABLED(irq) s->irq_state[irq].enabled = 1 |
43 | e69954b9 | pbrook | #define GIC_CLEAR_ENABLED(irq) s->irq_state[irq].enabled = 0 |
44 | e69954b9 | pbrook | #define GIC_TEST_ENABLED(irq) s->irq_state[irq].enabled
|
45 | e69954b9 | pbrook | #define GIC_SET_PENDING(irq) s->irq_state[irq].pending = 1 |
46 | e69954b9 | pbrook | #define GIC_CLEAR_PENDING(irq) s->irq_state[irq].pending = 0 |
47 | e69954b9 | pbrook | #define GIC_TEST_PENDING(irq) s->irq_state[irq].pending
|
48 | e69954b9 | pbrook | #define GIC_SET_ACTIVE(irq) s->irq_state[irq].active = 1 |
49 | e69954b9 | pbrook | #define GIC_CLEAR_ACTIVE(irq) s->irq_state[irq].active = 0 |
50 | e69954b9 | pbrook | #define GIC_TEST_ACTIVE(irq) s->irq_state[irq].active
|
51 | e69954b9 | pbrook | #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1 |
52 | e69954b9 | pbrook | #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0 |
53 | e69954b9 | pbrook | #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
|
54 | e69954b9 | pbrook | #define GIC_SET_LEVEL(irq) s->irq_state[irq].level = 1 |
55 | e69954b9 | pbrook | #define GIC_CLEAR_LEVEL(irq) s->irq_state[irq].level = 0 |
56 | e69954b9 | pbrook | #define GIC_TEST_LEVEL(irq) s->irq_state[irq].level
|
57 | e69954b9 | pbrook | #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1 |
58 | e69954b9 | pbrook | #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0 |
59 | e69954b9 | pbrook | #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
|
60 | e69954b9 | pbrook | |
61 | e69954b9 | pbrook | typedef struct gic_state |
62 | e69954b9 | pbrook | { |
63 | e69954b9 | pbrook | uint32_t base; |
64 | d537cf6c | pbrook | qemu_irq parent_irq; |
65 | e69954b9 | pbrook | int enabled;
|
66 | e69954b9 | pbrook | int cpu_enabled;
|
67 | e69954b9 | pbrook | |
68 | e69954b9 | pbrook | gic_irq_state irq_state[GIC_NIRQ]; |
69 | e69954b9 | pbrook | int irq_target[GIC_NIRQ];
|
70 | e69954b9 | pbrook | int priority[GIC_NIRQ];
|
71 | e69954b9 | pbrook | int last_active[GIC_NIRQ];
|
72 | e69954b9 | pbrook | |
73 | e69954b9 | pbrook | int priority_mask;
|
74 | e69954b9 | pbrook | int running_irq;
|
75 | e69954b9 | pbrook | int running_priority;
|
76 | e69954b9 | pbrook | int current_pending;
|
77 | e69954b9 | pbrook | } gic_state; |
78 | e69954b9 | pbrook | |
79 | e69954b9 | pbrook | /* TODO: Many places that call this routine could be optimized. */
|
80 | e69954b9 | pbrook | /* Update interrupt status after enabled or pending bits have been changed. */
|
81 | e69954b9 | pbrook | static void gic_update(gic_state *s) |
82 | e69954b9 | pbrook | { |
83 | e69954b9 | pbrook | int best_irq;
|
84 | e69954b9 | pbrook | int best_prio;
|
85 | e69954b9 | pbrook | int irq;
|
86 | e69954b9 | pbrook | |
87 | e69954b9 | pbrook | s->current_pending = 1023;
|
88 | e69954b9 | pbrook | if (!s->enabled || !s->cpu_enabled) {
|
89 | d537cf6c | pbrook | qemu_irq_lower(s->parent_irq); |
90 | e69954b9 | pbrook | return;
|
91 | e69954b9 | pbrook | } |
92 | e69954b9 | pbrook | best_prio = 0x100;
|
93 | e69954b9 | pbrook | best_irq = 1023;
|
94 | e69954b9 | pbrook | for (irq = 0; irq < 96; irq++) { |
95 | e69954b9 | pbrook | if (GIC_TEST_ENABLED(irq) && GIC_TEST_PENDING(irq)) {
|
96 | e69954b9 | pbrook | if (s->priority[irq] < best_prio) {
|
97 | e69954b9 | pbrook | best_prio = s->priority[irq]; |
98 | e69954b9 | pbrook | best_irq = irq; |
99 | e69954b9 | pbrook | } |
100 | e69954b9 | pbrook | } |
101 | e69954b9 | pbrook | } |
102 | e69954b9 | pbrook | if (best_prio > s->priority_mask) {
|
103 | d537cf6c | pbrook | qemu_irq_lower(s->parent_irq); |
104 | e69954b9 | pbrook | } else {
|
105 | e69954b9 | pbrook | s->current_pending = best_irq; |
106 | e69954b9 | pbrook | if (best_prio < s->running_priority) {
|
107 | e69954b9 | pbrook | DPRINTF("Raised pending IRQ %d\n", best_irq);
|
108 | d537cf6c | pbrook | qemu_irq_raise(s->parent_irq); |
109 | e69954b9 | pbrook | } |
110 | e69954b9 | pbrook | } |
111 | e69954b9 | pbrook | } |
112 | e69954b9 | pbrook | |
113 | e69954b9 | pbrook | static void gic_set_irq(void *opaque, int irq, int level) |
114 | e69954b9 | pbrook | { |
115 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
116 | e69954b9 | pbrook | /* The first external input line is internal interrupt 32. */
|
117 | e69954b9 | pbrook | irq += 32;
|
118 | 5fafdf24 | ths | if (level == GIC_TEST_LEVEL(irq))
|
119 | e69954b9 | pbrook | return;
|
120 | e69954b9 | pbrook | |
121 | e69954b9 | pbrook | if (level) {
|
122 | e69954b9 | pbrook | GIC_SET_LEVEL(irq); |
123 | e69954b9 | pbrook | if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq)) {
|
124 | e69954b9 | pbrook | DPRINTF("Set %d pending\n", irq);
|
125 | e69954b9 | pbrook | GIC_SET_PENDING(irq); |
126 | e69954b9 | pbrook | } |
127 | e69954b9 | pbrook | } else {
|
128 | e69954b9 | pbrook | GIC_CLEAR_LEVEL(irq); |
129 | e69954b9 | pbrook | } |
130 | e69954b9 | pbrook | gic_update(s); |
131 | e69954b9 | pbrook | } |
132 | e69954b9 | pbrook | |
133 | e69954b9 | pbrook | static void gic_set_running_irq(gic_state *s, int irq) |
134 | e69954b9 | pbrook | { |
135 | e69954b9 | pbrook | s->running_irq = irq; |
136 | df628ff1 | pbrook | if (irq == 1023) |
137 | df628ff1 | pbrook | s->running_priority = 0x100;
|
138 | df628ff1 | pbrook | else
|
139 | df628ff1 | pbrook | s->running_priority = s->priority[irq]; |
140 | e69954b9 | pbrook | gic_update(s); |
141 | e69954b9 | pbrook | } |
142 | e69954b9 | pbrook | |
143 | e69954b9 | pbrook | static uint32_t gic_acknowledge_irq(gic_state *s)
|
144 | e69954b9 | pbrook | { |
145 | e69954b9 | pbrook | int new_irq;
|
146 | e69954b9 | pbrook | new_irq = s->current_pending; |
147 | e69954b9 | pbrook | if (new_irq == 1023 || s->priority[new_irq] >= s->running_priority) { |
148 | e69954b9 | pbrook | DPRINTF("ACK no pending IRQ\n");
|
149 | e69954b9 | pbrook | return 1023; |
150 | e69954b9 | pbrook | } |
151 | d537cf6c | pbrook | qemu_irq_lower(s->parent_irq); |
152 | e69954b9 | pbrook | s->last_active[new_irq] = s->running_irq; |
153 | e69954b9 | pbrook | /* For level triggered interrupts we clear the pending bit while
|
154 | e69954b9 | pbrook | the interrupt is active. */
|
155 | e69954b9 | pbrook | GIC_CLEAR_PENDING(new_irq); |
156 | e69954b9 | pbrook | gic_set_running_irq(s, new_irq); |
157 | e69954b9 | pbrook | DPRINTF("ACK %d\n", new_irq);
|
158 | e69954b9 | pbrook | return new_irq;
|
159 | e69954b9 | pbrook | } |
160 | e69954b9 | pbrook | |
161 | e69954b9 | pbrook | static void gic_complete_irq(gic_state * s, int irq) |
162 | e69954b9 | pbrook | { |
163 | e69954b9 | pbrook | int update = 0; |
164 | df628ff1 | pbrook | DPRINTF("EOI %d\n", irq);
|
165 | e69954b9 | pbrook | if (s->running_irq == 1023) |
166 | e69954b9 | pbrook | return; /* No active IRQ. */ |
167 | e69954b9 | pbrook | if (irq != 1023) { |
168 | e69954b9 | pbrook | /* Mark level triggered interrupts as pending if they are still
|
169 | e69954b9 | pbrook | raised. */
|
170 | e69954b9 | pbrook | if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq)
|
171 | e69954b9 | pbrook | && GIC_TEST_LEVEL(irq)) { |
172 | e69954b9 | pbrook | GIC_SET_PENDING(irq); |
173 | e69954b9 | pbrook | update = 1;
|
174 | e69954b9 | pbrook | } |
175 | e69954b9 | pbrook | } |
176 | e69954b9 | pbrook | if (irq != s->running_irq) {
|
177 | e69954b9 | pbrook | /* Complete an IRQ that is not currently running. */
|
178 | e69954b9 | pbrook | int tmp = s->running_irq;
|
179 | e69954b9 | pbrook | while (s->last_active[tmp] != 1023) { |
180 | e69954b9 | pbrook | if (s->last_active[tmp] == irq) {
|
181 | e69954b9 | pbrook | s->last_active[tmp] = s->last_active[irq]; |
182 | e69954b9 | pbrook | break;
|
183 | e69954b9 | pbrook | } |
184 | e69954b9 | pbrook | tmp = s->last_active[tmp]; |
185 | e69954b9 | pbrook | } |
186 | e69954b9 | pbrook | if (update) {
|
187 | e69954b9 | pbrook | gic_update(s); |
188 | e69954b9 | pbrook | } |
189 | e69954b9 | pbrook | } else {
|
190 | e69954b9 | pbrook | /* Complete the current running IRQ. */
|
191 | e69954b9 | pbrook | gic_set_running_irq(s, s->last_active[s->running_irq]); |
192 | e69954b9 | pbrook | } |
193 | e69954b9 | pbrook | } |
194 | e69954b9 | pbrook | |
195 | e69954b9 | pbrook | static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset) |
196 | e69954b9 | pbrook | { |
197 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
198 | e69954b9 | pbrook | uint32_t res; |
199 | e69954b9 | pbrook | int irq;
|
200 | e69954b9 | pbrook | int i;
|
201 | e69954b9 | pbrook | |
202 | e69954b9 | pbrook | offset -= s->base + 0x1000;
|
203 | e69954b9 | pbrook | if (offset < 0x100) { |
204 | e69954b9 | pbrook | if (offset == 0) |
205 | e69954b9 | pbrook | return s->enabled;
|
206 | e69954b9 | pbrook | if (offset == 4) |
207 | e69954b9 | pbrook | return (GIC_NIRQ / 32) - 1; |
208 | e69954b9 | pbrook | if (offset < 0x08) |
209 | e69954b9 | pbrook | return 0; |
210 | e69954b9 | pbrook | goto bad_reg;
|
211 | e69954b9 | pbrook | } else if (offset < 0x200) { |
212 | e69954b9 | pbrook | /* Interrupt Set/Clear Enable. */
|
213 | e69954b9 | pbrook | if (offset < 0x180) |
214 | e69954b9 | pbrook | irq = (offset - 0x100) * 8; |
215 | e69954b9 | pbrook | else
|
216 | e69954b9 | pbrook | irq = (offset - 0x180) * 8; |
217 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
218 | e69954b9 | pbrook | goto bad_reg;
|
219 | e69954b9 | pbrook | res = 0;
|
220 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
221 | e69954b9 | pbrook | if (GIC_TEST_ENABLED(irq + i)) {
|
222 | e69954b9 | pbrook | res |= (1 << i);
|
223 | e69954b9 | pbrook | } |
224 | e69954b9 | pbrook | } |
225 | e69954b9 | pbrook | } else if (offset < 0x300) { |
226 | e69954b9 | pbrook | /* Interrupt Set/Clear Pending. */
|
227 | e69954b9 | pbrook | if (offset < 0x280) |
228 | e69954b9 | pbrook | irq = (offset - 0x200) * 8; |
229 | e69954b9 | pbrook | else
|
230 | e69954b9 | pbrook | irq = (offset - 0x280) * 8; |
231 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
232 | e69954b9 | pbrook | goto bad_reg;
|
233 | e69954b9 | pbrook | res = 0;
|
234 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
235 | e69954b9 | pbrook | if (GIC_TEST_PENDING(irq + i)) {
|
236 | e69954b9 | pbrook | res |= (1 << i);
|
237 | e69954b9 | pbrook | } |
238 | e69954b9 | pbrook | } |
239 | e69954b9 | pbrook | } else if (offset < 0x400) { |
240 | e69954b9 | pbrook | /* Interrupt Active. */
|
241 | e69954b9 | pbrook | irq = (offset - 0x300) * 8; |
242 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
243 | e69954b9 | pbrook | goto bad_reg;
|
244 | e69954b9 | pbrook | res = 0;
|
245 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
246 | e69954b9 | pbrook | if (GIC_TEST_ACTIVE(irq + i)) {
|
247 | e69954b9 | pbrook | res |= (1 << i);
|
248 | e69954b9 | pbrook | } |
249 | e69954b9 | pbrook | } |
250 | e69954b9 | pbrook | } else if (offset < 0x800) { |
251 | e69954b9 | pbrook | /* Interrupt Priority. */
|
252 | e69954b9 | pbrook | irq = offset - 0x400;
|
253 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
254 | e69954b9 | pbrook | goto bad_reg;
|
255 | e69954b9 | pbrook | res = s->priority[irq]; |
256 | e69954b9 | pbrook | } else if (offset < 0xc00) { |
257 | e69954b9 | pbrook | /* Interrupt CPU Target. */
|
258 | e69954b9 | pbrook | irq = offset - 0x800;
|
259 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
260 | e69954b9 | pbrook | goto bad_reg;
|
261 | e69954b9 | pbrook | res = s->irq_target[irq]; |
262 | e69954b9 | pbrook | } else if (offset < 0xf00) { |
263 | e69954b9 | pbrook | /* Interrupt Configuration. */
|
264 | e69954b9 | pbrook | irq = (offset - 0xc00) * 2; |
265 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
266 | e69954b9 | pbrook | goto bad_reg;
|
267 | e69954b9 | pbrook | res = 0;
|
268 | e69954b9 | pbrook | for (i = 0; i < 4; i++) { |
269 | e69954b9 | pbrook | if (GIC_TEST_MODEL(irq + i))
|
270 | e69954b9 | pbrook | res |= (1 << (i * 2)); |
271 | e69954b9 | pbrook | if (GIC_TEST_TRIGGER(irq + i))
|
272 | e69954b9 | pbrook | res |= (2 << (i * 2)); |
273 | e69954b9 | pbrook | } |
274 | e69954b9 | pbrook | } else if (offset < 0xfe0) { |
275 | e69954b9 | pbrook | goto bad_reg;
|
276 | e69954b9 | pbrook | } else /* offset >= 0xfe0 */ { |
277 | e69954b9 | pbrook | if (offset & 3) { |
278 | e69954b9 | pbrook | res = 0;
|
279 | e69954b9 | pbrook | } else {
|
280 | e69954b9 | pbrook | res = gic_id[(offset - 0xfe0) >> 2]; |
281 | e69954b9 | pbrook | } |
282 | e69954b9 | pbrook | } |
283 | e69954b9 | pbrook | return res;
|
284 | e69954b9 | pbrook | bad_reg:
|
285 | e69954b9 | pbrook | cpu_abort (cpu_single_env, "gic_dist_readb: Bad offset %x\n", offset);
|
286 | e69954b9 | pbrook | return 0; |
287 | e69954b9 | pbrook | } |
288 | e69954b9 | pbrook | |
289 | e69954b9 | pbrook | static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset) |
290 | e69954b9 | pbrook | { |
291 | e69954b9 | pbrook | uint32_t val; |
292 | e69954b9 | pbrook | val = gic_dist_readb(opaque, offset); |
293 | e69954b9 | pbrook | val |= gic_dist_readb(opaque, offset + 1) << 8; |
294 | e69954b9 | pbrook | return val;
|
295 | e69954b9 | pbrook | } |
296 | e69954b9 | pbrook | |
297 | e69954b9 | pbrook | static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset) |
298 | e69954b9 | pbrook | { |
299 | e69954b9 | pbrook | uint32_t val; |
300 | e69954b9 | pbrook | val = gic_dist_readw(opaque, offset); |
301 | e69954b9 | pbrook | val |= gic_dist_readw(opaque, offset + 2) << 16; |
302 | e69954b9 | pbrook | return val;
|
303 | e69954b9 | pbrook | } |
304 | e69954b9 | pbrook | |
305 | e69954b9 | pbrook | static void gic_dist_writeb(void *opaque, target_phys_addr_t offset, |
306 | e69954b9 | pbrook | uint32_t value) |
307 | e69954b9 | pbrook | { |
308 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
309 | e69954b9 | pbrook | int irq;
|
310 | e69954b9 | pbrook | int i;
|
311 | e69954b9 | pbrook | |
312 | e69954b9 | pbrook | offset -= s->base + 0x1000;
|
313 | e69954b9 | pbrook | if (offset < 0x100) { |
314 | e69954b9 | pbrook | if (offset == 0) { |
315 | e69954b9 | pbrook | s->enabled = (value & 1);
|
316 | e69954b9 | pbrook | DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis"); |
317 | e69954b9 | pbrook | } else if (offset < 4) { |
318 | e69954b9 | pbrook | /* ignored. */
|
319 | e69954b9 | pbrook | } else {
|
320 | e69954b9 | pbrook | goto bad_reg;
|
321 | e69954b9 | pbrook | } |
322 | e69954b9 | pbrook | } else if (offset < 0x180) { |
323 | e69954b9 | pbrook | /* Interrupt Set Enable. */
|
324 | e69954b9 | pbrook | irq = (offset - 0x100) * 8; |
325 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
326 | e69954b9 | pbrook | goto bad_reg;
|
327 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
328 | e69954b9 | pbrook | if (value & (1 << i)) { |
329 | e69954b9 | pbrook | if (!GIC_TEST_ENABLED(irq + i))
|
330 | e69954b9 | pbrook | DPRINTF("Enabled IRQ %d\n", irq + i);
|
331 | e69954b9 | pbrook | GIC_SET_ENABLED(irq + i); |
332 | e69954b9 | pbrook | /* If a raised level triggered IRQ enabled then mark
|
333 | e69954b9 | pbrook | is as pending. */
|
334 | e69954b9 | pbrook | if (GIC_TEST_LEVEL(irq + i) && !GIC_TEST_TRIGGER(irq + i))
|
335 | e69954b9 | pbrook | GIC_SET_PENDING(irq + i); |
336 | e69954b9 | pbrook | } |
337 | e69954b9 | pbrook | } |
338 | e69954b9 | pbrook | } else if (offset < 0x200) { |
339 | e69954b9 | pbrook | /* Interrupt Clear Enable. */
|
340 | e69954b9 | pbrook | irq = (offset - 0x180) * 8; |
341 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
342 | e69954b9 | pbrook | goto bad_reg;
|
343 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
344 | e69954b9 | pbrook | if (value & (1 << i)) { |
345 | e69954b9 | pbrook | if (GIC_TEST_ENABLED(irq + i))
|
346 | e69954b9 | pbrook | DPRINTF("Disabled IRQ %d\n", irq + i);
|
347 | e69954b9 | pbrook | GIC_CLEAR_ENABLED(irq + i); |
348 | e69954b9 | pbrook | } |
349 | e69954b9 | pbrook | } |
350 | e69954b9 | pbrook | } else if (offset < 0x280) { |
351 | e69954b9 | pbrook | /* Interrupt Set Pending. */
|
352 | e69954b9 | pbrook | irq = (offset - 0x200) * 8; |
353 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
354 | e69954b9 | pbrook | goto bad_reg;
|
355 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
356 | e69954b9 | pbrook | if (value & (1 << i)) { |
357 | e69954b9 | pbrook | GIC_SET_PENDING(irq + i); |
358 | e69954b9 | pbrook | } |
359 | e69954b9 | pbrook | } |
360 | e69954b9 | pbrook | } else if (offset < 0x300) { |
361 | e69954b9 | pbrook | /* Interrupt Clear Pending. */
|
362 | e69954b9 | pbrook | irq = (offset - 0x280) * 8; |
363 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
364 | e69954b9 | pbrook | goto bad_reg;
|
365 | e69954b9 | pbrook | for (i = 0; i < 8; i++) { |
366 | e69954b9 | pbrook | if (value & (1 << i)) { |
367 | e69954b9 | pbrook | GIC_CLEAR_PENDING(irq + i); |
368 | e69954b9 | pbrook | } |
369 | e69954b9 | pbrook | } |
370 | e69954b9 | pbrook | } else if (offset < 0x400) { |
371 | e69954b9 | pbrook | /* Interrupt Active. */
|
372 | e69954b9 | pbrook | goto bad_reg;
|
373 | e69954b9 | pbrook | } else if (offset < 0x800) { |
374 | e69954b9 | pbrook | /* Interrupt Priority. */
|
375 | e69954b9 | pbrook | irq = offset - 0x400;
|
376 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
377 | e69954b9 | pbrook | goto bad_reg;
|
378 | e69954b9 | pbrook | s->priority[irq] = value; |
379 | e69954b9 | pbrook | } else if (offset < 0xc00) { |
380 | e69954b9 | pbrook | /* Interrupt CPU Target. */
|
381 | e69954b9 | pbrook | irq = offset - 0x800;
|
382 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
383 | e69954b9 | pbrook | goto bad_reg;
|
384 | e69954b9 | pbrook | s->irq_target[irq] = value; |
385 | e69954b9 | pbrook | } else if (offset < 0xf00) { |
386 | e69954b9 | pbrook | /* Interrupt Configuration. */
|
387 | 326199c2 | pbrook | irq = (offset - 0xc00) * 4; |
388 | e69954b9 | pbrook | if (irq >= GIC_NIRQ)
|
389 | e69954b9 | pbrook | goto bad_reg;
|
390 | e69954b9 | pbrook | for (i = 0; i < 4; i++) { |
391 | e69954b9 | pbrook | if (value & (1 << (i * 2))) { |
392 | e69954b9 | pbrook | GIC_SET_MODEL(irq + i); |
393 | e69954b9 | pbrook | } else {
|
394 | e69954b9 | pbrook | GIC_CLEAR_MODEL(irq + i); |
395 | e69954b9 | pbrook | } |
396 | e69954b9 | pbrook | if (value & (2 << (i * 2))) { |
397 | e69954b9 | pbrook | GIC_SET_TRIGGER(irq + i); |
398 | e69954b9 | pbrook | } else {
|
399 | e69954b9 | pbrook | GIC_CLEAR_TRIGGER(irq + i); |
400 | e69954b9 | pbrook | } |
401 | e69954b9 | pbrook | } |
402 | e69954b9 | pbrook | } else {
|
403 | e69954b9 | pbrook | /* 0xf00 is only handled for word writes. */
|
404 | e69954b9 | pbrook | goto bad_reg;
|
405 | e69954b9 | pbrook | } |
406 | e69954b9 | pbrook | gic_update(s); |
407 | e69954b9 | pbrook | return;
|
408 | e69954b9 | pbrook | bad_reg:
|
409 | e69954b9 | pbrook | cpu_abort (cpu_single_env, "gic_dist_writeb: Bad offset %x\n", offset);
|
410 | e69954b9 | pbrook | } |
411 | e69954b9 | pbrook | |
412 | e69954b9 | pbrook | static void gic_dist_writew(void *opaque, target_phys_addr_t offset, |
413 | e69954b9 | pbrook | uint32_t value) |
414 | e69954b9 | pbrook | { |
415 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
416 | e69954b9 | pbrook | if (offset - s->base == 0xf00) { |
417 | e69954b9 | pbrook | GIC_SET_PENDING(value & 0x3ff);
|
418 | e69954b9 | pbrook | gic_update(s); |
419 | e69954b9 | pbrook | return;
|
420 | e69954b9 | pbrook | } |
421 | e69954b9 | pbrook | gic_dist_writeb(opaque, offset, value & 0xff);
|
422 | e69954b9 | pbrook | gic_dist_writeb(opaque, offset + 1, value >> 8); |
423 | e69954b9 | pbrook | } |
424 | e69954b9 | pbrook | |
425 | e69954b9 | pbrook | static void gic_dist_writel(void *opaque, target_phys_addr_t offset, |
426 | e69954b9 | pbrook | uint32_t value) |
427 | e69954b9 | pbrook | { |
428 | e69954b9 | pbrook | gic_dist_writew(opaque, offset, value & 0xffff);
|
429 | e69954b9 | pbrook | gic_dist_writew(opaque, offset + 2, value >> 16); |
430 | e69954b9 | pbrook | } |
431 | e69954b9 | pbrook | |
432 | e69954b9 | pbrook | static CPUReadMemoryFunc *gic_dist_readfn[] = {
|
433 | e69954b9 | pbrook | gic_dist_readb, |
434 | e69954b9 | pbrook | gic_dist_readw, |
435 | e69954b9 | pbrook | gic_dist_readl |
436 | e69954b9 | pbrook | }; |
437 | e69954b9 | pbrook | |
438 | e69954b9 | pbrook | static CPUWriteMemoryFunc *gic_dist_writefn[] = {
|
439 | e69954b9 | pbrook | gic_dist_writeb, |
440 | e69954b9 | pbrook | gic_dist_writew, |
441 | e69954b9 | pbrook | gic_dist_writel |
442 | e69954b9 | pbrook | }; |
443 | e69954b9 | pbrook | |
444 | e69954b9 | pbrook | static uint32_t gic_cpu_read(void *opaque, target_phys_addr_t offset) |
445 | e69954b9 | pbrook | { |
446 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
447 | e69954b9 | pbrook | offset -= s->base; |
448 | e69954b9 | pbrook | switch (offset) {
|
449 | e69954b9 | pbrook | case 0x00: /* Control */ |
450 | e69954b9 | pbrook | return s->cpu_enabled;
|
451 | e69954b9 | pbrook | case 0x04: /* Priority mask */ |
452 | e69954b9 | pbrook | return s->priority_mask;
|
453 | e69954b9 | pbrook | case 0x08: /* Binary Point */ |
454 | e69954b9 | pbrook | /* ??? Not implemented. */
|
455 | e69954b9 | pbrook | return 0; |
456 | e69954b9 | pbrook | case 0x0c: /* Acknowledge */ |
457 | e69954b9 | pbrook | return gic_acknowledge_irq(s);
|
458 | e69954b9 | pbrook | case 0x14: /* Runing Priority */ |
459 | e69954b9 | pbrook | return s->running_priority;
|
460 | e69954b9 | pbrook | case 0x18: /* Highest Pending Interrupt */ |
461 | e69954b9 | pbrook | return s->current_pending;
|
462 | e69954b9 | pbrook | default:
|
463 | bea6030d | ths | cpu_abort (cpu_single_env, "gic_cpu_read: Bad offset %x\n", offset);
|
464 | e69954b9 | pbrook | return 0; |
465 | e69954b9 | pbrook | } |
466 | e69954b9 | pbrook | } |
467 | e69954b9 | pbrook | |
468 | e69954b9 | pbrook | static void gic_cpu_write(void *opaque, target_phys_addr_t offset, |
469 | e69954b9 | pbrook | uint32_t value) |
470 | e69954b9 | pbrook | { |
471 | e69954b9 | pbrook | gic_state *s = (gic_state *)opaque; |
472 | e69954b9 | pbrook | offset -= s->base; |
473 | e69954b9 | pbrook | switch (offset) {
|
474 | e69954b9 | pbrook | case 0x00: /* Control */ |
475 | e69954b9 | pbrook | s->cpu_enabled = (value & 1);
|
476 | e69954b9 | pbrook | DPRINTF("CPU %sabled\n", s->cpu_enabled ? "En" : "Dis"); |
477 | e69954b9 | pbrook | break;
|
478 | e69954b9 | pbrook | case 0x04: /* Priority mask */ |
479 | e69954b9 | pbrook | s->priority_mask = (value & 0x3ff);
|
480 | e69954b9 | pbrook | break;
|
481 | e69954b9 | pbrook | case 0x08: /* Binary Point */ |
482 | e69954b9 | pbrook | /* ??? Not implemented. */
|
483 | e69954b9 | pbrook | break;
|
484 | e69954b9 | pbrook | case 0x10: /* End Of Interrupt */ |
485 | e69954b9 | pbrook | return gic_complete_irq(s, value & 0x3ff); |
486 | e69954b9 | pbrook | default:
|
487 | bea6030d | ths | cpu_abort (cpu_single_env, "gic_cpu_write: Bad offset %x\n", offset);
|
488 | e69954b9 | pbrook | return;
|
489 | e69954b9 | pbrook | } |
490 | e69954b9 | pbrook | gic_update(s); |
491 | e69954b9 | pbrook | } |
492 | e69954b9 | pbrook | |
493 | e69954b9 | pbrook | static CPUReadMemoryFunc *gic_cpu_readfn[] = {
|
494 | e69954b9 | pbrook | gic_cpu_read, |
495 | e69954b9 | pbrook | gic_cpu_read, |
496 | e69954b9 | pbrook | gic_cpu_read |
497 | e69954b9 | pbrook | }; |
498 | e69954b9 | pbrook | |
499 | e69954b9 | pbrook | static CPUWriteMemoryFunc *gic_cpu_writefn[] = {
|
500 | e69954b9 | pbrook | gic_cpu_write, |
501 | e69954b9 | pbrook | gic_cpu_write, |
502 | e69954b9 | pbrook | gic_cpu_write |
503 | e69954b9 | pbrook | }; |
504 | e69954b9 | pbrook | |
505 | e69954b9 | pbrook | static void gic_reset(gic_state *s) |
506 | e69954b9 | pbrook | { |
507 | e69954b9 | pbrook | int i;
|
508 | e69954b9 | pbrook | memset(s->irq_state, 0, GIC_NIRQ * sizeof(gic_irq_state)); |
509 | e69954b9 | pbrook | s->priority_mask = 0xf0;
|
510 | e69954b9 | pbrook | s->current_pending = 1023;
|
511 | e69954b9 | pbrook | s->running_irq = 1023;
|
512 | e69954b9 | pbrook | s->running_priority = 0x100;
|
513 | e69954b9 | pbrook | for (i = 0; i < 15; i++) { |
514 | e69954b9 | pbrook | GIC_SET_ENABLED(i); |
515 | e69954b9 | pbrook | GIC_SET_TRIGGER(i); |
516 | e69954b9 | pbrook | } |
517 | e69954b9 | pbrook | s->enabled = 0;
|
518 | e69954b9 | pbrook | s->cpu_enabled = 0;
|
519 | e69954b9 | pbrook | } |
520 | e69954b9 | pbrook | |
521 | d537cf6c | pbrook | qemu_irq *arm_gic_init(uint32_t base, qemu_irq parent_irq) |
522 | e69954b9 | pbrook | { |
523 | e69954b9 | pbrook | gic_state *s; |
524 | d537cf6c | pbrook | qemu_irq *qi; |
525 | e69954b9 | pbrook | int iomemtype;
|
526 | e69954b9 | pbrook | |
527 | e69954b9 | pbrook | s = (gic_state *)qemu_mallocz(sizeof(gic_state));
|
528 | e69954b9 | pbrook | if (!s)
|
529 | e69954b9 | pbrook | return NULL; |
530 | d537cf6c | pbrook | qi = qemu_allocate_irqs(gic_set_irq, s, GIC_NIRQ); |
531 | e69954b9 | pbrook | s->parent_irq = parent_irq; |
532 | e69954b9 | pbrook | if (base != 0xffffffff) { |
533 | e69954b9 | pbrook | iomemtype = cpu_register_io_memory(0, gic_cpu_readfn,
|
534 | e69954b9 | pbrook | gic_cpu_writefn, s); |
535 | 187337f8 | pbrook | cpu_register_physical_memory(base, 0x00001000, iomemtype);
|
536 | e69954b9 | pbrook | iomemtype = cpu_register_io_memory(0, gic_dist_readfn,
|
537 | e69954b9 | pbrook | gic_dist_writefn, s); |
538 | 187337f8 | pbrook | cpu_register_physical_memory(base + 0x1000, 0x00001000, iomemtype); |
539 | e69954b9 | pbrook | s->base = base; |
540 | e69954b9 | pbrook | } else {
|
541 | e69954b9 | pbrook | s->base = 0;
|
542 | e69954b9 | pbrook | } |
543 | e69954b9 | pbrook | gic_reset(s); |
544 | d537cf6c | pbrook | return qi;
|
545 | e69954b9 | pbrook | } |