Statistics
| Branch: | Revision:

root / hw / arm_gic.c @ 2558e0a6

History | View | Annotate | Download (25.3 kB)

1 5fafdf24 ths
/*
2 9ee6e8bb pbrook
 * ARM Generic/Distributed Interrupt Controller
3 e69954b9 pbrook
 *
4 9ee6e8bb pbrook
 * Copyright (c) 2006-2007 CodeSourcery.
5 e69954b9 pbrook
 * Written by Paul Brook
6 e69954b9 pbrook
 *
7 8e31bf38 Matthew Fernandez
 * This code is licensed under the GPL.
8 e69954b9 pbrook
 */
9 e69954b9 pbrook
10 9ee6e8bb pbrook
/* This file contains implementation code for the RealView EB interrupt
11 9ee6e8bb pbrook
   controller, MPCore distributed interrupt controller and ARMv7-M
12 9ee6e8bb pbrook
   Nested Vectored Interrupt Controller.  */
13 e69954b9 pbrook
14 a32134aa Mark Langsdorf
/* Maximum number of possible interrupts, determined by the GIC architecture */
15 a32134aa Mark Langsdorf
#define GIC_MAXIRQ 1020
16 e69954b9 pbrook
//#define DEBUG_GIC
17 e69954b9 pbrook
18 e69954b9 pbrook
#ifdef DEBUG_GIC
19 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) \
20 001faf32 Blue Swirl
do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
21 e69954b9 pbrook
#else
22 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do {} while(0)
23 e69954b9 pbrook
#endif
24 e69954b9 pbrook
25 9ee6e8bb pbrook
#ifdef NVIC
26 9ee6e8bb pbrook
static const uint8_t gic_id[] =
27 9ee6e8bb pbrook
{ 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 };
28 9ee6e8bb pbrook
/* The NVIC has 16 internal vectors.  However these are not exposed
29 9ee6e8bb pbrook
   through the normal GIC interface.  */
30 9ee6e8bb pbrook
#define GIC_BASE_IRQ    32
31 9ee6e8bb pbrook
#else
32 e69954b9 pbrook
static const uint8_t gic_id[] =
33 e69954b9 pbrook
{ 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
34 9ee6e8bb pbrook
#define GIC_BASE_IRQ    0
35 9ee6e8bb pbrook
#endif
36 e69954b9 pbrook
37 fe7e8758 Paul Brook
#define FROM_SYSBUSGIC(type, dev) \
38 fe7e8758 Paul Brook
    DO_UPCAST(type, gic, FROM_SYSBUS(gic_state, dev))
39 fe7e8758 Paul Brook
40 e69954b9 pbrook
typedef struct gic_irq_state
41 e69954b9 pbrook
{
42 41bf234d Rabin Vincent
    /* The enable bits are only banked for per-cpu interrupts.  */
43 41bf234d Rabin Vincent
    unsigned enabled:NCPU;
44 9ee6e8bb pbrook
    unsigned pending:NCPU;
45 9ee6e8bb pbrook
    unsigned active:NCPU;
46 a45db6c6 aurel32
    unsigned level:NCPU;
47 9ee6e8bb pbrook
    unsigned model:1; /* 0 = N:N, 1 = 1:N */
48 e69954b9 pbrook
    unsigned trigger:1; /* nonzero = edge triggered.  */
49 e69954b9 pbrook
} gic_irq_state;
50 e69954b9 pbrook
51 9ee6e8bb pbrook
#define ALL_CPU_MASK ((1 << NCPU) - 1)
52 c988bfad Paul Brook
#if NCPU > 1
53 c988bfad Paul Brook
#define NUM_CPU(s) ((s)->num_cpu)
54 c988bfad Paul Brook
#else
55 c988bfad Paul Brook
#define NUM_CPU(s) 1
56 c988bfad Paul Brook
#endif
57 9ee6e8bb pbrook
58 41bf234d Rabin Vincent
#define GIC_SET_ENABLED(irq, cm) s->irq_state[irq].enabled |= (cm)
59 41bf234d Rabin Vincent
#define GIC_CLEAR_ENABLED(irq, cm) s->irq_state[irq].enabled &= ~(cm)
60 41bf234d Rabin Vincent
#define GIC_TEST_ENABLED(irq, cm) ((s->irq_state[irq].enabled & (cm)) != 0)
61 9ee6e8bb pbrook
#define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
62 9ee6e8bb pbrook
#define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
63 9ee6e8bb pbrook
#define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
64 9ee6e8bb pbrook
#define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
65 9ee6e8bb pbrook
#define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
66 9ee6e8bb pbrook
#define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
67 e69954b9 pbrook
#define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
68 e69954b9 pbrook
#define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
69 e69954b9 pbrook
#define GIC_TEST_MODEL(irq) s->irq_state[irq].model
70 9ee6e8bb pbrook
#define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
71 9ee6e8bb pbrook
#define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
72 57d69a91 balrog
#define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
73 e69954b9 pbrook
#define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
74 e69954b9 pbrook
#define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
75 e69954b9 pbrook
#define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
76 9ee6e8bb pbrook
#define GIC_GET_PRIORITY(irq, cpu) \
77 9ee6e8bb pbrook
  (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
78 9ee6e8bb pbrook
#ifdef NVIC
79 9ee6e8bb pbrook
#define GIC_TARGET(irq) 1
80 9ee6e8bb pbrook
#else
81 9ee6e8bb pbrook
#define GIC_TARGET(irq) s->irq_target[irq]
82 9ee6e8bb pbrook
#endif
83 e69954b9 pbrook
84 e69954b9 pbrook
typedef struct gic_state
85 e69954b9 pbrook
{
86 fe7e8758 Paul Brook
    SysBusDevice busdev;
87 9ee6e8bb pbrook
    qemu_irq parent_irq[NCPU];
88 e69954b9 pbrook
    int enabled;
89 9ee6e8bb pbrook
    int cpu_enabled[NCPU];
90 e69954b9 pbrook
91 a32134aa Mark Langsdorf
    gic_irq_state irq_state[GIC_MAXIRQ];
92 9ee6e8bb pbrook
#ifndef NVIC
93 a32134aa Mark Langsdorf
    int irq_target[GIC_MAXIRQ];
94 9ee6e8bb pbrook
#endif
95 9ee6e8bb pbrook
    int priority1[32][NCPU];
96 a32134aa Mark Langsdorf
    int priority2[GIC_MAXIRQ - 32];
97 a32134aa Mark Langsdorf
    int last_active[GIC_MAXIRQ][NCPU];
98 9ee6e8bb pbrook
99 9ee6e8bb pbrook
    int priority_mask[NCPU];
100 9ee6e8bb pbrook
    int running_irq[NCPU];
101 9ee6e8bb pbrook
    int running_priority[NCPU];
102 9ee6e8bb pbrook
    int current_pending[NCPU];
103 9ee6e8bb pbrook
104 c988bfad Paul Brook
#if NCPU > 1
105 c988bfad Paul Brook
    int num_cpu;
106 c988bfad Paul Brook
#endif
107 c988bfad Paul Brook
108 e2c56465 Peter Maydell
    MemoryRegion iomem; /* Distributor */
109 e2c56465 Peter Maydell
#ifndef NVIC
110 e2c56465 Peter Maydell
    /* This is just so we can have an opaque pointer which identifies
111 e2c56465 Peter Maydell
     * both this GIC and which CPU interface we should be accessing.
112 e2c56465 Peter Maydell
     */
113 e2c56465 Peter Maydell
    struct gic_state *backref[NCPU];
114 e2c56465 Peter Maydell
    MemoryRegion cpuiomem[NCPU+1]; /* CPU interfaces */
115 e2c56465 Peter Maydell
#endif
116 a32134aa Mark Langsdorf
    uint32_t num_irq;
117 e69954b9 pbrook
} gic_state;
118 e69954b9 pbrook
119 e69954b9 pbrook
/* TODO: Many places that call this routine could be optimized.  */
120 e69954b9 pbrook
/* Update interrupt status after enabled or pending bits have been changed.  */
121 e69954b9 pbrook
static void gic_update(gic_state *s)
122 e69954b9 pbrook
{
123 e69954b9 pbrook
    int best_irq;
124 e69954b9 pbrook
    int best_prio;
125 e69954b9 pbrook
    int irq;
126 9ee6e8bb pbrook
    int level;
127 9ee6e8bb pbrook
    int cpu;
128 9ee6e8bb pbrook
    int cm;
129 9ee6e8bb pbrook
130 c988bfad Paul Brook
    for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
131 9ee6e8bb pbrook
        cm = 1 << cpu;
132 9ee6e8bb pbrook
        s->current_pending[cpu] = 1023;
133 9ee6e8bb pbrook
        if (!s->enabled || !s->cpu_enabled[cpu]) {
134 9ee6e8bb pbrook
            qemu_irq_lower(s->parent_irq[cpu]);
135 9ee6e8bb pbrook
            return;
136 9ee6e8bb pbrook
        }
137 9ee6e8bb pbrook
        best_prio = 0x100;
138 9ee6e8bb pbrook
        best_irq = 1023;
139 a32134aa Mark Langsdorf
        for (irq = 0; irq < s->num_irq; irq++) {
140 41bf234d Rabin Vincent
            if (GIC_TEST_ENABLED(irq, cm) && GIC_TEST_PENDING(irq, cm)) {
141 9ee6e8bb pbrook
                if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
142 9ee6e8bb pbrook
                    best_prio = GIC_GET_PRIORITY(irq, cpu);
143 9ee6e8bb pbrook
                    best_irq = irq;
144 9ee6e8bb pbrook
                }
145 e69954b9 pbrook
            }
146 e69954b9 pbrook
        }
147 9ee6e8bb pbrook
        level = 0;
148 9ee6e8bb pbrook
        if (best_prio <= s->priority_mask[cpu]) {
149 9ee6e8bb pbrook
            s->current_pending[cpu] = best_irq;
150 9ee6e8bb pbrook
            if (best_prio < s->running_priority[cpu]) {
151 9ee6e8bb pbrook
                DPRINTF("Raised pending IRQ %d\n", best_irq);
152 9ee6e8bb pbrook
                level = 1;
153 9ee6e8bb pbrook
            }
154 e69954b9 pbrook
        }
155 9ee6e8bb pbrook
        qemu_set_irq(s->parent_irq[cpu], level);
156 e69954b9 pbrook
    }
157 e69954b9 pbrook
}
158 e69954b9 pbrook
159 9ee6e8bb pbrook
static void __attribute__((unused))
160 9ee6e8bb pbrook
gic_set_pending_private(gic_state *s, int cpu, int irq)
161 9ee6e8bb pbrook
{
162 9ee6e8bb pbrook
    int cm = 1 << cpu;
163 9ee6e8bb pbrook
164 9ee6e8bb pbrook
    if (GIC_TEST_PENDING(irq, cm))
165 9ee6e8bb pbrook
        return;
166 9ee6e8bb pbrook
167 9ee6e8bb pbrook
    DPRINTF("Set %d pending cpu %d\n", irq, cpu);
168 9ee6e8bb pbrook
    GIC_SET_PENDING(irq, cm);
169 9ee6e8bb pbrook
    gic_update(s);
170 9ee6e8bb pbrook
}
171 9ee6e8bb pbrook
172 9ee6e8bb pbrook
/* Process a change in an external IRQ input.  */
173 e69954b9 pbrook
static void gic_set_irq(void *opaque, int irq, int level)
174 e69954b9 pbrook
{
175 e69954b9 pbrook
    gic_state *s = (gic_state *)opaque;
176 e69954b9 pbrook
    /* The first external input line is internal interrupt 32.  */
177 e69954b9 pbrook
    irq += 32;
178 9ee6e8bb pbrook
    if (level == GIC_TEST_LEVEL(irq, ALL_CPU_MASK))
179 e69954b9 pbrook
        return;
180 e69954b9 pbrook
181 e69954b9 pbrook
    if (level) {
182 9ee6e8bb pbrook
        GIC_SET_LEVEL(irq, ALL_CPU_MASK);
183 41bf234d Rabin Vincent
        if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, ALL_CPU_MASK)) {
184 9ee6e8bb pbrook
            DPRINTF("Set %d pending mask %x\n", irq, GIC_TARGET(irq));
185 9ee6e8bb pbrook
            GIC_SET_PENDING(irq, GIC_TARGET(irq));
186 e69954b9 pbrook
        }
187 e69954b9 pbrook
    } else {
188 9ee6e8bb pbrook
        GIC_CLEAR_LEVEL(irq, ALL_CPU_MASK);
189 e69954b9 pbrook
    }
190 e69954b9 pbrook
    gic_update(s);
191 e69954b9 pbrook
}
192 e69954b9 pbrook
193 9ee6e8bb pbrook
static void gic_set_running_irq(gic_state *s, int cpu, int irq)
194 e69954b9 pbrook
{
195 9ee6e8bb pbrook
    s->running_irq[cpu] = irq;
196 9ee6e8bb pbrook
    if (irq == 1023) {
197 9ee6e8bb pbrook
        s->running_priority[cpu] = 0x100;
198 9ee6e8bb pbrook
    } else {
199 9ee6e8bb pbrook
        s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
200 9ee6e8bb pbrook
    }
201 e69954b9 pbrook
    gic_update(s);
202 e69954b9 pbrook
}
203 e69954b9 pbrook
204 9ee6e8bb pbrook
static uint32_t gic_acknowledge_irq(gic_state *s, int cpu)
205 e69954b9 pbrook
{
206 e69954b9 pbrook
    int new_irq;
207 9ee6e8bb pbrook
    int cm = 1 << cpu;
208 9ee6e8bb pbrook
    new_irq = s->current_pending[cpu];
209 9ee6e8bb pbrook
    if (new_irq == 1023
210 9ee6e8bb pbrook
            || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
211 e69954b9 pbrook
        DPRINTF("ACK no pending IRQ\n");
212 e69954b9 pbrook
        return 1023;
213 e69954b9 pbrook
    }
214 9ee6e8bb pbrook
    s->last_active[new_irq][cpu] = s->running_irq[cpu];
215 9ee6e8bb pbrook
    /* Clear pending flags for both level and edge triggered interrupts.
216 9ee6e8bb pbrook
       Level triggered IRQs will be reasserted once they become inactive.  */
217 9ee6e8bb pbrook
    GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
218 9ee6e8bb pbrook
    gic_set_running_irq(s, cpu, new_irq);
219 e69954b9 pbrook
    DPRINTF("ACK %d\n", new_irq);
220 e69954b9 pbrook
    return new_irq;
221 e69954b9 pbrook
}
222 e69954b9 pbrook
223 9ee6e8bb pbrook
static void gic_complete_irq(gic_state * s, int cpu, int irq)
224 e69954b9 pbrook
{
225 e69954b9 pbrook
    int update = 0;
226 9ee6e8bb pbrook
    int cm = 1 << cpu;
227 df628ff1 pbrook
    DPRINTF("EOI %d\n", irq);
228 a32134aa Mark Langsdorf
    if (irq >= s->num_irq) {
229 217bfb44 Peter Maydell
        /* This handles two cases:
230 217bfb44 Peter Maydell
         * 1. If software writes the ID of a spurious interrupt [ie 1023]
231 217bfb44 Peter Maydell
         * to the GICC_EOIR, the GIC ignores that write.
232 217bfb44 Peter Maydell
         * 2. If software writes the number of a non-existent interrupt
233 217bfb44 Peter Maydell
         * this must be a subcase of "value written does not match the last
234 217bfb44 Peter Maydell
         * valid interrupt value read from the Interrupt Acknowledge
235 217bfb44 Peter Maydell
         * register" and so this is UNPREDICTABLE. We choose to ignore it.
236 217bfb44 Peter Maydell
         */
237 217bfb44 Peter Maydell
        return;
238 217bfb44 Peter Maydell
    }
239 9ee6e8bb pbrook
    if (s->running_irq[cpu] == 1023)
240 e69954b9 pbrook
        return; /* No active IRQ.  */
241 217bfb44 Peter Maydell
    /* Mark level triggered interrupts as pending if they are still
242 217bfb44 Peter Maydell
       raised.  */
243 217bfb44 Peter Maydell
    if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
244 217bfb44 Peter Maydell
        && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
245 217bfb44 Peter Maydell
        DPRINTF("Set %d pending mask %x\n", irq, cm);
246 217bfb44 Peter Maydell
        GIC_SET_PENDING(irq, cm);
247 217bfb44 Peter Maydell
        update = 1;
248 e69954b9 pbrook
    }
249 9ee6e8bb pbrook
    if (irq != s->running_irq[cpu]) {
250 e69954b9 pbrook
        /* Complete an IRQ that is not currently running.  */
251 9ee6e8bb pbrook
        int tmp = s->running_irq[cpu];
252 9ee6e8bb pbrook
        while (s->last_active[tmp][cpu] != 1023) {
253 9ee6e8bb pbrook
            if (s->last_active[tmp][cpu] == irq) {
254 9ee6e8bb pbrook
                s->last_active[tmp][cpu] = s->last_active[irq][cpu];
255 e69954b9 pbrook
                break;
256 e69954b9 pbrook
            }
257 9ee6e8bb pbrook
            tmp = s->last_active[tmp][cpu];
258 e69954b9 pbrook
        }
259 e69954b9 pbrook
        if (update) {
260 e69954b9 pbrook
            gic_update(s);
261 e69954b9 pbrook
        }
262 e69954b9 pbrook
    } else {
263 e69954b9 pbrook
        /* Complete the current running IRQ.  */
264 9ee6e8bb pbrook
        gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
265 e69954b9 pbrook
    }
266 e69954b9 pbrook
}
267 e69954b9 pbrook
268 c227f099 Anthony Liguori
static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset)
269 e69954b9 pbrook
{
270 e69954b9 pbrook
    gic_state *s = (gic_state *)opaque;
271 e69954b9 pbrook
    uint32_t res;
272 e69954b9 pbrook
    int irq;
273 e69954b9 pbrook
    int i;
274 9ee6e8bb pbrook
    int cpu;
275 9ee6e8bb pbrook
    int cm;
276 9ee6e8bb pbrook
    int mask;
277 e69954b9 pbrook
278 9ee6e8bb pbrook
    cpu = gic_get_current_cpu();
279 9ee6e8bb pbrook
    cm = 1 << cpu;
280 e69954b9 pbrook
    if (offset < 0x100) {
281 9ee6e8bb pbrook
#ifndef NVIC
282 e69954b9 pbrook
        if (offset == 0)
283 e69954b9 pbrook
            return s->enabled;
284 e69954b9 pbrook
        if (offset == 4)
285 a32134aa Mark Langsdorf
            return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
286 e69954b9 pbrook
        if (offset < 0x08)
287 e69954b9 pbrook
            return 0;
288 b79f2265 Rob Herring
        if (offset >= 0x80) {
289 b79f2265 Rob Herring
            /* Interrupt Security , RAZ/WI */
290 b79f2265 Rob Herring
            return 0;
291 b79f2265 Rob Herring
        }
292 9ee6e8bb pbrook
#endif
293 e69954b9 pbrook
        goto bad_reg;
294 e69954b9 pbrook
    } else if (offset < 0x200) {
295 e69954b9 pbrook
        /* Interrupt Set/Clear Enable.  */
296 e69954b9 pbrook
        if (offset < 0x180)
297 e69954b9 pbrook
            irq = (offset - 0x100) * 8;
298 e69954b9 pbrook
        else
299 e69954b9 pbrook
            irq = (offset - 0x180) * 8;
300 9ee6e8bb pbrook
        irq += GIC_BASE_IRQ;
301 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
302 e69954b9 pbrook
            goto bad_reg;
303 e69954b9 pbrook
        res = 0;
304 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
305 41bf234d Rabin Vincent
            if (GIC_TEST_ENABLED(irq + i, cm)) {
306 e69954b9 pbrook
                res |= (1 << i);
307 e69954b9 pbrook
            }
308 e69954b9 pbrook
        }
309 e69954b9 pbrook
    } else if (offset < 0x300) {
310 e69954b9 pbrook
        /* Interrupt Set/Clear Pending.  */
311 e69954b9 pbrook
        if (offset < 0x280)
312 e69954b9 pbrook
            irq = (offset - 0x200) * 8;
313 e69954b9 pbrook
        else
314 e69954b9 pbrook
            irq = (offset - 0x280) * 8;
315 9ee6e8bb pbrook
        irq += GIC_BASE_IRQ;
316 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
317 e69954b9 pbrook
            goto bad_reg;
318 e69954b9 pbrook
        res = 0;
319 9ee6e8bb pbrook
        mask = (irq < 32) ?  cm : ALL_CPU_MASK;
320 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
321 9ee6e8bb pbrook
            if (GIC_TEST_PENDING(irq + i, mask)) {
322 e69954b9 pbrook
                res |= (1 << i);
323 e69954b9 pbrook
            }
324 e69954b9 pbrook
        }
325 e69954b9 pbrook
    } else if (offset < 0x400) {
326 e69954b9 pbrook
        /* Interrupt Active.  */
327 9ee6e8bb pbrook
        irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
328 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
329 e69954b9 pbrook
            goto bad_reg;
330 e69954b9 pbrook
        res = 0;
331 9ee6e8bb pbrook
        mask = (irq < 32) ?  cm : ALL_CPU_MASK;
332 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
333 9ee6e8bb pbrook
            if (GIC_TEST_ACTIVE(irq + i, mask)) {
334 e69954b9 pbrook
                res |= (1 << i);
335 e69954b9 pbrook
            }
336 e69954b9 pbrook
        }
337 e69954b9 pbrook
    } else if (offset < 0x800) {
338 e69954b9 pbrook
        /* Interrupt Priority.  */
339 9ee6e8bb pbrook
        irq = (offset - 0x400) + GIC_BASE_IRQ;
340 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
341 e69954b9 pbrook
            goto bad_reg;
342 9ee6e8bb pbrook
        res = GIC_GET_PRIORITY(irq, cpu);
343 9ee6e8bb pbrook
#ifndef NVIC
344 e69954b9 pbrook
    } else if (offset < 0xc00) {
345 e69954b9 pbrook
        /* Interrupt CPU Target.  */
346 9ee6e8bb pbrook
        irq = (offset - 0x800) + GIC_BASE_IRQ;
347 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
348 e69954b9 pbrook
            goto bad_reg;
349 9ee6e8bb pbrook
        if (irq >= 29 && irq <= 31) {
350 9ee6e8bb pbrook
            res = cm;
351 9ee6e8bb pbrook
        } else {
352 9ee6e8bb pbrook
            res = GIC_TARGET(irq);
353 9ee6e8bb pbrook
        }
354 e69954b9 pbrook
    } else if (offset < 0xf00) {
355 e69954b9 pbrook
        /* Interrupt Configuration.  */
356 9ee6e8bb pbrook
        irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
357 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
358 e69954b9 pbrook
            goto bad_reg;
359 e69954b9 pbrook
        res = 0;
360 e69954b9 pbrook
        for (i = 0; i < 4; i++) {
361 e69954b9 pbrook
            if (GIC_TEST_MODEL(irq + i))
362 e69954b9 pbrook
                res |= (1 << (i * 2));
363 e69954b9 pbrook
            if (GIC_TEST_TRIGGER(irq + i))
364 e69954b9 pbrook
                res |= (2 << (i * 2));
365 e69954b9 pbrook
        }
366 9ee6e8bb pbrook
#endif
367 e69954b9 pbrook
    } else if (offset < 0xfe0) {
368 e69954b9 pbrook
        goto bad_reg;
369 e69954b9 pbrook
    } else /* offset >= 0xfe0 */ {
370 e69954b9 pbrook
        if (offset & 3) {
371 e69954b9 pbrook
            res = 0;
372 e69954b9 pbrook
        } else {
373 e69954b9 pbrook
            res = gic_id[(offset - 0xfe0) >> 2];
374 e69954b9 pbrook
        }
375 e69954b9 pbrook
    }
376 e69954b9 pbrook
    return res;
377 e69954b9 pbrook
bad_reg:
378 2ac71179 Paul Brook
    hw_error("gic_dist_readb: Bad offset %x\n", (int)offset);
379 e69954b9 pbrook
    return 0;
380 e69954b9 pbrook
}
381 e69954b9 pbrook
382 c227f099 Anthony Liguori
static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset)
383 e69954b9 pbrook
{
384 e69954b9 pbrook
    uint32_t val;
385 e69954b9 pbrook
    val = gic_dist_readb(opaque, offset);
386 e69954b9 pbrook
    val |= gic_dist_readb(opaque, offset + 1) << 8;
387 e69954b9 pbrook
    return val;
388 e69954b9 pbrook
}
389 e69954b9 pbrook
390 c227f099 Anthony Liguori
static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset)
391 e69954b9 pbrook
{
392 e69954b9 pbrook
    uint32_t val;
393 9ee6e8bb pbrook
#ifdef NVIC
394 9ee6e8bb pbrook
    gic_state *s = (gic_state *)opaque;
395 9ee6e8bb pbrook
    uint32_t addr;
396 8da3ff18 pbrook
    addr = offset;
397 9ee6e8bb pbrook
    if (addr < 0x100 || addr > 0xd00)
398 fe7e8758 Paul Brook
        return nvic_readl(s, addr);
399 9ee6e8bb pbrook
#endif
400 e69954b9 pbrook
    val = gic_dist_readw(opaque, offset);
401 e69954b9 pbrook
    val |= gic_dist_readw(opaque, offset + 2) << 16;
402 e69954b9 pbrook
    return val;
403 e69954b9 pbrook
}
404 e69954b9 pbrook
405 c227f099 Anthony Liguori
static void gic_dist_writeb(void *opaque, target_phys_addr_t offset,
406 e69954b9 pbrook
                            uint32_t value)
407 e69954b9 pbrook
{
408 e69954b9 pbrook
    gic_state *s = (gic_state *)opaque;
409 e69954b9 pbrook
    int irq;
410 e69954b9 pbrook
    int i;
411 9ee6e8bb pbrook
    int cpu;
412 e69954b9 pbrook
413 9ee6e8bb pbrook
    cpu = gic_get_current_cpu();
414 e69954b9 pbrook
    if (offset < 0x100) {
415 9ee6e8bb pbrook
#ifdef NVIC
416 9ee6e8bb pbrook
        goto bad_reg;
417 9ee6e8bb pbrook
#else
418 e69954b9 pbrook
        if (offset == 0) {
419 e69954b9 pbrook
            s->enabled = (value & 1);
420 e69954b9 pbrook
            DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
421 e69954b9 pbrook
        } else if (offset < 4) {
422 e69954b9 pbrook
            /* ignored.  */
423 b79f2265 Rob Herring
        } else if (offset >= 0x80) {
424 b79f2265 Rob Herring
            /* Interrupt Security Registers, RAZ/WI */
425 e69954b9 pbrook
        } else {
426 e69954b9 pbrook
            goto bad_reg;
427 e69954b9 pbrook
        }
428 9ee6e8bb pbrook
#endif
429 e69954b9 pbrook
    } else if (offset < 0x180) {
430 e69954b9 pbrook
        /* Interrupt Set Enable.  */
431 9ee6e8bb pbrook
        irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
432 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
433 e69954b9 pbrook
            goto bad_reg;
434 9ee6e8bb pbrook
        if (irq < 16)
435 9ee6e8bb pbrook
          value = 0xff;
436 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
437 e69954b9 pbrook
            if (value & (1 << i)) {
438 9ee6e8bb pbrook
                int mask = (irq < 32) ? (1 << cpu) : GIC_TARGET(irq);
439 41bf234d Rabin Vincent
                int cm = (irq < 32) ? (1 << cpu) : ALL_CPU_MASK;
440 41bf234d Rabin Vincent
441 41bf234d Rabin Vincent
                if (!GIC_TEST_ENABLED(irq + i, cm)) {
442 e69954b9 pbrook
                    DPRINTF("Enabled IRQ %d\n", irq + i);
443 41bf234d Rabin Vincent
                }
444 41bf234d Rabin Vincent
                GIC_SET_ENABLED(irq + i, cm);
445 e69954b9 pbrook
                /* If a raised level triggered IRQ enabled then mark
446 e69954b9 pbrook
                   is as pending.  */
447 9ee6e8bb pbrook
                if (GIC_TEST_LEVEL(irq + i, mask)
448 9ee6e8bb pbrook
                        && !GIC_TEST_TRIGGER(irq + i)) {
449 9ee6e8bb pbrook
                    DPRINTF("Set %d pending mask %x\n", irq + i, mask);
450 9ee6e8bb pbrook
                    GIC_SET_PENDING(irq + i, mask);
451 9ee6e8bb pbrook
                }
452 e69954b9 pbrook
            }
453 e69954b9 pbrook
        }
454 e69954b9 pbrook
    } else if (offset < 0x200) {
455 e69954b9 pbrook
        /* Interrupt Clear Enable.  */
456 9ee6e8bb pbrook
        irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
457 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
458 e69954b9 pbrook
            goto bad_reg;
459 9ee6e8bb pbrook
        if (irq < 16)
460 9ee6e8bb pbrook
          value = 0;
461 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
462 e69954b9 pbrook
            if (value & (1 << i)) {
463 41bf234d Rabin Vincent
                int cm = (irq < 32) ? (1 << cpu) : ALL_CPU_MASK;
464 41bf234d Rabin Vincent
465 41bf234d Rabin Vincent
                if (GIC_TEST_ENABLED(irq + i, cm)) {
466 e69954b9 pbrook
                    DPRINTF("Disabled IRQ %d\n", irq + i);
467 41bf234d Rabin Vincent
                }
468 41bf234d Rabin Vincent
                GIC_CLEAR_ENABLED(irq + i, cm);
469 e69954b9 pbrook
            }
470 e69954b9 pbrook
        }
471 e69954b9 pbrook
    } else if (offset < 0x280) {
472 e69954b9 pbrook
        /* Interrupt Set Pending.  */
473 9ee6e8bb pbrook
        irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
474 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
475 e69954b9 pbrook
            goto bad_reg;
476 9ee6e8bb pbrook
        if (irq < 16)
477 9ee6e8bb pbrook
          irq = 0;
478 9ee6e8bb pbrook
479 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
480 e69954b9 pbrook
            if (value & (1 << i)) {
481 9ee6e8bb pbrook
                GIC_SET_PENDING(irq + i, GIC_TARGET(irq));
482 e69954b9 pbrook
            }
483 e69954b9 pbrook
        }
484 e69954b9 pbrook
    } else if (offset < 0x300) {
485 e69954b9 pbrook
        /* Interrupt Clear Pending.  */
486 9ee6e8bb pbrook
        irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
487 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
488 e69954b9 pbrook
            goto bad_reg;
489 e69954b9 pbrook
        for (i = 0; i < 8; i++) {
490 9ee6e8bb pbrook
            /* ??? This currently clears the pending bit for all CPUs, even
491 9ee6e8bb pbrook
               for per-CPU interrupts.  It's unclear whether this is the
492 9ee6e8bb pbrook
               corect behavior.  */
493 e69954b9 pbrook
            if (value & (1 << i)) {
494 9ee6e8bb pbrook
                GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
495 e69954b9 pbrook
            }
496 e69954b9 pbrook
        }
497 e69954b9 pbrook
    } else if (offset < 0x400) {
498 e69954b9 pbrook
        /* Interrupt Active.  */
499 e69954b9 pbrook
        goto bad_reg;
500 e69954b9 pbrook
    } else if (offset < 0x800) {
501 e69954b9 pbrook
        /* Interrupt Priority.  */
502 9ee6e8bb pbrook
        irq = (offset - 0x400) + GIC_BASE_IRQ;
503 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
504 e69954b9 pbrook
            goto bad_reg;
505 9ee6e8bb pbrook
        if (irq < 32) {
506 9ee6e8bb pbrook
            s->priority1[irq][cpu] = value;
507 9ee6e8bb pbrook
        } else {
508 9ee6e8bb pbrook
            s->priority2[irq - 32] = value;
509 9ee6e8bb pbrook
        }
510 9ee6e8bb pbrook
#ifndef NVIC
511 e69954b9 pbrook
    } else if (offset < 0xc00) {
512 e69954b9 pbrook
        /* Interrupt CPU Target.  */
513 9ee6e8bb pbrook
        irq = (offset - 0x800) + GIC_BASE_IRQ;
514 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
515 e69954b9 pbrook
            goto bad_reg;
516 9ee6e8bb pbrook
        if (irq < 29)
517 9ee6e8bb pbrook
            value = 0;
518 9ee6e8bb pbrook
        else if (irq < 32)
519 9ee6e8bb pbrook
            value = ALL_CPU_MASK;
520 9ee6e8bb pbrook
        s->irq_target[irq] = value & ALL_CPU_MASK;
521 e69954b9 pbrook
    } else if (offset < 0xf00) {
522 e69954b9 pbrook
        /* Interrupt Configuration.  */
523 9ee6e8bb pbrook
        irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
524 a32134aa Mark Langsdorf
        if (irq >= s->num_irq)
525 e69954b9 pbrook
            goto bad_reg;
526 9ee6e8bb pbrook
        if (irq < 32)
527 9ee6e8bb pbrook
            value |= 0xaa;
528 e69954b9 pbrook
        for (i = 0; i < 4; i++) {
529 e69954b9 pbrook
            if (value & (1 << (i * 2))) {
530 e69954b9 pbrook
                GIC_SET_MODEL(irq + i);
531 e69954b9 pbrook
            } else {
532 e69954b9 pbrook
                GIC_CLEAR_MODEL(irq + i);
533 e69954b9 pbrook
            }
534 e69954b9 pbrook
            if (value & (2 << (i * 2))) {
535 e69954b9 pbrook
                GIC_SET_TRIGGER(irq + i);
536 e69954b9 pbrook
            } else {
537 e69954b9 pbrook
                GIC_CLEAR_TRIGGER(irq + i);
538 e69954b9 pbrook
            }
539 e69954b9 pbrook
        }
540 9ee6e8bb pbrook
#endif
541 e69954b9 pbrook
    } else {
542 9ee6e8bb pbrook
        /* 0xf00 is only handled for 32-bit writes.  */
543 e69954b9 pbrook
        goto bad_reg;
544 e69954b9 pbrook
    }
545 e69954b9 pbrook
    gic_update(s);
546 e69954b9 pbrook
    return;
547 e69954b9 pbrook
bad_reg:
548 2ac71179 Paul Brook
    hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset);
549 e69954b9 pbrook
}
550 e69954b9 pbrook
551 c227f099 Anthony Liguori
static void gic_dist_writew(void *opaque, target_phys_addr_t offset,
552 e69954b9 pbrook
                            uint32_t value)
553 e69954b9 pbrook
{
554 e69954b9 pbrook
    gic_dist_writeb(opaque, offset, value & 0xff);
555 e69954b9 pbrook
    gic_dist_writeb(opaque, offset + 1, value >> 8);
556 e69954b9 pbrook
}
557 e69954b9 pbrook
558 c227f099 Anthony Liguori
static void gic_dist_writel(void *opaque, target_phys_addr_t offset,
559 e69954b9 pbrook
                            uint32_t value)
560 e69954b9 pbrook
{
561 9ee6e8bb pbrook
    gic_state *s = (gic_state *)opaque;
562 9ee6e8bb pbrook
#ifdef NVIC
563 9ee6e8bb pbrook
    uint32_t addr;
564 8da3ff18 pbrook
    addr = offset;
565 9ee6e8bb pbrook
    if (addr < 0x100 || (addr > 0xd00 && addr != 0xf00)) {
566 fe7e8758 Paul Brook
        nvic_writel(s, addr, value);
567 9ee6e8bb pbrook
        return;
568 9ee6e8bb pbrook
    }
569 9ee6e8bb pbrook
#endif
570 8da3ff18 pbrook
    if (offset == 0xf00) {
571 9ee6e8bb pbrook
        int cpu;
572 9ee6e8bb pbrook
        int irq;
573 9ee6e8bb pbrook
        int mask;
574 9ee6e8bb pbrook
575 9ee6e8bb pbrook
        cpu = gic_get_current_cpu();
576 9ee6e8bb pbrook
        irq = value & 0x3ff;
577 9ee6e8bb pbrook
        switch ((value >> 24) & 3) {
578 9ee6e8bb pbrook
        case 0:
579 9ee6e8bb pbrook
            mask = (value >> 16) & ALL_CPU_MASK;
580 9ee6e8bb pbrook
            break;
581 9ee6e8bb pbrook
        case 1:
582 fa250144 Adam Lackorzynski
            mask = ALL_CPU_MASK ^ (1 << cpu);
583 9ee6e8bb pbrook
            break;
584 9ee6e8bb pbrook
        case 2:
585 fa250144 Adam Lackorzynski
            mask = 1 << cpu;
586 9ee6e8bb pbrook
            break;
587 9ee6e8bb pbrook
        default:
588 9ee6e8bb pbrook
            DPRINTF("Bad Soft Int target filter\n");
589 9ee6e8bb pbrook
            mask = ALL_CPU_MASK;
590 9ee6e8bb pbrook
            break;
591 9ee6e8bb pbrook
        }
592 9ee6e8bb pbrook
        GIC_SET_PENDING(irq, mask);
593 9ee6e8bb pbrook
        gic_update(s);
594 9ee6e8bb pbrook
        return;
595 9ee6e8bb pbrook
    }
596 e69954b9 pbrook
    gic_dist_writew(opaque, offset, value & 0xffff);
597 e69954b9 pbrook
    gic_dist_writew(opaque, offset + 2, value >> 16);
598 e69954b9 pbrook
}
599 e69954b9 pbrook
600 755c0802 Avi Kivity
static const MemoryRegionOps gic_dist_ops = {
601 755c0802 Avi Kivity
    .old_mmio = {
602 755c0802 Avi Kivity
        .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
603 755c0802 Avi Kivity
        .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
604 755c0802 Avi Kivity
    },
605 755c0802 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
606 e69954b9 pbrook
};
607 e69954b9 pbrook
608 9ee6e8bb pbrook
#ifndef NVIC
609 9ee6e8bb pbrook
static uint32_t gic_cpu_read(gic_state *s, int cpu, int offset)
610 e69954b9 pbrook
{
611 e69954b9 pbrook
    switch (offset) {
612 e69954b9 pbrook
    case 0x00: /* Control */
613 9ee6e8bb pbrook
        return s->cpu_enabled[cpu];
614 e69954b9 pbrook
    case 0x04: /* Priority mask */
615 9ee6e8bb pbrook
        return s->priority_mask[cpu];
616 e69954b9 pbrook
    case 0x08: /* Binary Point */
617 e69954b9 pbrook
        /* ??? Not implemented.  */
618 e69954b9 pbrook
        return 0;
619 e69954b9 pbrook
    case 0x0c: /* Acknowledge */
620 9ee6e8bb pbrook
        return gic_acknowledge_irq(s, cpu);
621 66a0a2cb Dong Xu Wang
    case 0x14: /* Running Priority */
622 9ee6e8bb pbrook
        return s->running_priority[cpu];
623 e69954b9 pbrook
    case 0x18: /* Highest Pending Interrupt */
624 9ee6e8bb pbrook
        return s->current_pending[cpu];
625 e69954b9 pbrook
    default:
626 2ac71179 Paul Brook
        hw_error("gic_cpu_read: Bad offset %x\n", (int)offset);
627 e69954b9 pbrook
        return 0;
628 e69954b9 pbrook
    }
629 e69954b9 pbrook
}
630 e69954b9 pbrook
631 9ee6e8bb pbrook
static void gic_cpu_write(gic_state *s, int cpu, int offset, uint32_t value)
632 e69954b9 pbrook
{
633 e69954b9 pbrook
    switch (offset) {
634 e69954b9 pbrook
    case 0x00: /* Control */
635 9ee6e8bb pbrook
        s->cpu_enabled[cpu] = (value & 1);
636 f7c70325 Paul Brook
        DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled ? "En" : "Dis");
637 e69954b9 pbrook
        break;
638 e69954b9 pbrook
    case 0x04: /* Priority mask */
639 9ee6e8bb pbrook
        s->priority_mask[cpu] = (value & 0xff);
640 e69954b9 pbrook
        break;
641 e69954b9 pbrook
    case 0x08: /* Binary Point */
642 e69954b9 pbrook
        /* ??? Not implemented.  */
643 e69954b9 pbrook
        break;
644 e69954b9 pbrook
    case 0x10: /* End Of Interrupt */
645 9ee6e8bb pbrook
        return gic_complete_irq(s, cpu, value & 0x3ff);
646 e69954b9 pbrook
    default:
647 2ac71179 Paul Brook
        hw_error("gic_cpu_write: Bad offset %x\n", (int)offset);
648 e69954b9 pbrook
        return;
649 e69954b9 pbrook
    }
650 e69954b9 pbrook
    gic_update(s);
651 e69954b9 pbrook
}
652 e2c56465 Peter Maydell
653 e2c56465 Peter Maydell
/* Wrappers to read/write the GIC CPU interface for the current CPU */
654 e2c56465 Peter Maydell
static uint64_t gic_thiscpu_read(void *opaque, target_phys_addr_t addr,
655 e2c56465 Peter Maydell
                                 unsigned size)
656 e2c56465 Peter Maydell
{
657 e2c56465 Peter Maydell
    gic_state *s = (gic_state *)opaque;
658 e2c56465 Peter Maydell
    return gic_cpu_read(s, gic_get_current_cpu(), addr & 0xff);
659 e2c56465 Peter Maydell
}
660 e2c56465 Peter Maydell
661 e2c56465 Peter Maydell
static void gic_thiscpu_write(void *opaque, target_phys_addr_t addr,
662 e2c56465 Peter Maydell
                              uint64_t value, unsigned size)
663 e2c56465 Peter Maydell
{
664 e2c56465 Peter Maydell
    gic_state *s = (gic_state *)opaque;
665 e2c56465 Peter Maydell
    gic_cpu_write(s, gic_get_current_cpu(), addr & 0xff, value);
666 e2c56465 Peter Maydell
}
667 e2c56465 Peter Maydell
668 e2c56465 Peter Maydell
/* Wrappers to read/write the GIC CPU interface for a specific CPU.
669 e2c56465 Peter Maydell
 * These just decode the opaque pointer into gic_state* + cpu id.
670 e2c56465 Peter Maydell
 */
671 e2c56465 Peter Maydell
static uint64_t gic_do_cpu_read(void *opaque, target_phys_addr_t addr,
672 e2c56465 Peter Maydell
                                unsigned size)
673 e2c56465 Peter Maydell
{
674 e2c56465 Peter Maydell
    gic_state **backref = (gic_state **)opaque;
675 e2c56465 Peter Maydell
    gic_state *s = *backref;
676 e2c56465 Peter Maydell
    int id = (backref - s->backref);
677 e2c56465 Peter Maydell
    return gic_cpu_read(s, id, addr & 0xff);
678 e2c56465 Peter Maydell
}
679 e2c56465 Peter Maydell
680 e2c56465 Peter Maydell
static void gic_do_cpu_write(void *opaque, target_phys_addr_t addr,
681 e2c56465 Peter Maydell
                             uint64_t value, unsigned size)
682 e2c56465 Peter Maydell
{
683 e2c56465 Peter Maydell
    gic_state **backref = (gic_state **)opaque;
684 e2c56465 Peter Maydell
    gic_state *s = *backref;
685 e2c56465 Peter Maydell
    int id = (backref - s->backref);
686 e2c56465 Peter Maydell
    gic_cpu_write(s, id, addr & 0xff, value);
687 e2c56465 Peter Maydell
}
688 e2c56465 Peter Maydell
689 e2c56465 Peter Maydell
static const MemoryRegionOps gic_thiscpu_ops = {
690 e2c56465 Peter Maydell
    .read = gic_thiscpu_read,
691 e2c56465 Peter Maydell
    .write = gic_thiscpu_write,
692 e2c56465 Peter Maydell
    .endianness = DEVICE_NATIVE_ENDIAN,
693 e2c56465 Peter Maydell
};
694 e2c56465 Peter Maydell
695 e2c56465 Peter Maydell
static const MemoryRegionOps gic_cpu_ops = {
696 e2c56465 Peter Maydell
    .read = gic_do_cpu_read,
697 e2c56465 Peter Maydell
    .write = gic_do_cpu_write,
698 e2c56465 Peter Maydell
    .endianness = DEVICE_NATIVE_ENDIAN,
699 e2c56465 Peter Maydell
};
700 9ee6e8bb pbrook
#endif
701 e69954b9 pbrook
702 e69954b9 pbrook
static void gic_reset(gic_state *s)
703 e69954b9 pbrook
{
704 e69954b9 pbrook
    int i;
705 a32134aa Mark Langsdorf
    memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
706 c988bfad Paul Brook
    for (i = 0 ; i < NUM_CPU(s); i++) {
707 9ee6e8bb pbrook
        s->priority_mask[i] = 0xf0;
708 9ee6e8bb pbrook
        s->current_pending[i] = 1023;
709 9ee6e8bb pbrook
        s->running_irq[i] = 1023;
710 9ee6e8bb pbrook
        s->running_priority[i] = 0x100;
711 9ee6e8bb pbrook
#ifdef NVIC
712 9ee6e8bb pbrook
        /* The NVIC doesn't have per-cpu interfaces, so enable by default.  */
713 9ee6e8bb pbrook
        s->cpu_enabled[i] = 1;
714 9ee6e8bb pbrook
#else
715 9ee6e8bb pbrook
        s->cpu_enabled[i] = 0;
716 9ee6e8bb pbrook
#endif
717 9ee6e8bb pbrook
    }
718 e57ec016 pbrook
    for (i = 0; i < 16; i++) {
719 41bf234d Rabin Vincent
        GIC_SET_ENABLED(i, ALL_CPU_MASK);
720 e69954b9 pbrook
        GIC_SET_TRIGGER(i);
721 e69954b9 pbrook
    }
722 9ee6e8bb pbrook
#ifdef NVIC
723 9ee6e8bb pbrook
    /* The NVIC is always enabled.  */
724 9ee6e8bb pbrook
    s->enabled = 1;
725 9ee6e8bb pbrook
#else
726 e69954b9 pbrook
    s->enabled = 0;
727 9ee6e8bb pbrook
#endif
728 e69954b9 pbrook
}
729 e69954b9 pbrook
730 23e39294 pbrook
static void gic_save(QEMUFile *f, void *opaque)
731 23e39294 pbrook
{
732 23e39294 pbrook
    gic_state *s = (gic_state *)opaque;
733 23e39294 pbrook
    int i;
734 23e39294 pbrook
    int j;
735 23e39294 pbrook
736 23e39294 pbrook
    qemu_put_be32(f, s->enabled);
737 c988bfad Paul Brook
    for (i = 0; i < NUM_CPU(s); i++) {
738 23e39294 pbrook
        qemu_put_be32(f, s->cpu_enabled[i]);
739 23e39294 pbrook
        for (j = 0; j < 32; j++)
740 23e39294 pbrook
            qemu_put_be32(f, s->priority1[j][i]);
741 a32134aa Mark Langsdorf
        for (j = 0; j < s->num_irq; j++)
742 23e39294 pbrook
            qemu_put_be32(f, s->last_active[j][i]);
743 23e39294 pbrook
        qemu_put_be32(f, s->priority_mask[i]);
744 23e39294 pbrook
        qemu_put_be32(f, s->running_irq[i]);
745 23e39294 pbrook
        qemu_put_be32(f, s->running_priority[i]);
746 23e39294 pbrook
        qemu_put_be32(f, s->current_pending[i]);
747 23e39294 pbrook
    }
748 a32134aa Mark Langsdorf
    for (i = 0; i < s->num_irq - 32; i++) {
749 23e39294 pbrook
        qemu_put_be32(f, s->priority2[i]);
750 23e39294 pbrook
    }
751 a32134aa Mark Langsdorf
    for (i = 0; i < s->num_irq; i++) {
752 c2e2343e Dmitry Koshelev
#ifndef NVIC
753 c2e2343e Dmitry Koshelev
        qemu_put_be32(f, s->irq_target[i]);
754 c2e2343e Dmitry Koshelev
#endif
755 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].enabled);
756 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].pending);
757 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].active);
758 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].level);
759 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].model);
760 23e39294 pbrook
        qemu_put_byte(f, s->irq_state[i].trigger);
761 23e39294 pbrook
    }
762 23e39294 pbrook
}
763 23e39294 pbrook
764 23e39294 pbrook
static int gic_load(QEMUFile *f, void *opaque, int version_id)
765 23e39294 pbrook
{
766 23e39294 pbrook
    gic_state *s = (gic_state *)opaque;
767 23e39294 pbrook
    int i;
768 23e39294 pbrook
    int j;
769 23e39294 pbrook
770 c2e2343e Dmitry Koshelev
    if (version_id != 2)
771 23e39294 pbrook
        return -EINVAL;
772 23e39294 pbrook
773 23e39294 pbrook
    s->enabled = qemu_get_be32(f);
774 c988bfad Paul Brook
    for (i = 0; i < NUM_CPU(s); i++) {
775 23e39294 pbrook
        s->cpu_enabled[i] = qemu_get_be32(f);
776 23e39294 pbrook
        for (j = 0; j < 32; j++)
777 23e39294 pbrook
            s->priority1[j][i] = qemu_get_be32(f);
778 a32134aa Mark Langsdorf
        for (j = 0; j < s->num_irq; j++)
779 23e39294 pbrook
            s->last_active[j][i] = qemu_get_be32(f);
780 23e39294 pbrook
        s->priority_mask[i] = qemu_get_be32(f);
781 23e39294 pbrook
        s->running_irq[i] = qemu_get_be32(f);
782 23e39294 pbrook
        s->running_priority[i] = qemu_get_be32(f);
783 23e39294 pbrook
        s->current_pending[i] = qemu_get_be32(f);
784 23e39294 pbrook
    }
785 a32134aa Mark Langsdorf
    for (i = 0; i < s->num_irq - 32; i++) {
786 23e39294 pbrook
        s->priority2[i] = qemu_get_be32(f);
787 23e39294 pbrook
    }
788 a32134aa Mark Langsdorf
    for (i = 0; i < s->num_irq; i++) {
789 c2e2343e Dmitry Koshelev
#ifndef NVIC
790 c2e2343e Dmitry Koshelev
        s->irq_target[i] = qemu_get_be32(f);
791 c2e2343e Dmitry Koshelev
#endif
792 23e39294 pbrook
        s->irq_state[i].enabled = qemu_get_byte(f);
793 23e39294 pbrook
        s->irq_state[i].pending = qemu_get_byte(f);
794 23e39294 pbrook
        s->irq_state[i].active = qemu_get_byte(f);
795 23e39294 pbrook
        s->irq_state[i].level = qemu_get_byte(f);
796 23e39294 pbrook
        s->irq_state[i].model = qemu_get_byte(f);
797 23e39294 pbrook
        s->irq_state[i].trigger = qemu_get_byte(f);
798 23e39294 pbrook
    }
799 23e39294 pbrook
800 23e39294 pbrook
    return 0;
801 23e39294 pbrook
}
802 23e39294 pbrook
803 c988bfad Paul Brook
#if NCPU > 1
804 a32134aa Mark Langsdorf
static void gic_init(gic_state *s, int num_cpu, int num_irq)
805 c988bfad Paul Brook
#else
806 a32134aa Mark Langsdorf
static void gic_init(gic_state *s, int num_irq)
807 c988bfad Paul Brook
#endif
808 e69954b9 pbrook
{
809 9ee6e8bb pbrook
    int i;
810 e69954b9 pbrook
811 c988bfad Paul Brook
#if NCPU > 1
812 c988bfad Paul Brook
    s->num_cpu = num_cpu;
813 c988bfad Paul Brook
#endif
814 a32134aa Mark Langsdorf
    s->num_irq = num_irq + GIC_BASE_IRQ;
815 a32134aa Mark Langsdorf
    if (s->num_irq > GIC_MAXIRQ) {
816 a32134aa Mark Langsdorf
        hw_error("requested %u interrupt lines exceeds GIC maximum %d\n",
817 a32134aa Mark Langsdorf
                 num_irq, GIC_MAXIRQ);
818 a32134aa Mark Langsdorf
    }
819 a32134aa Mark Langsdorf
    qdev_init_gpio_in(&s->busdev.qdev, gic_set_irq, s->num_irq - 32);
820 c988bfad Paul Brook
    for (i = 0; i < NUM_CPU(s); i++) {
821 fe7e8758 Paul Brook
        sysbus_init_irq(&s->busdev, &s->parent_irq[i]);
822 e69954b9 pbrook
    }
823 755c0802 Avi Kivity
    memory_region_init_io(&s->iomem, &gic_dist_ops, s, "gic_dist", 0x1000);
824 e2c56465 Peter Maydell
#ifndef NVIC
825 e2c56465 Peter Maydell
    /* Memory regions for the CPU interfaces (NVIC doesn't have these):
826 e2c56465 Peter Maydell
     * a region for "CPU interface for this core", then a region for
827 e2c56465 Peter Maydell
     * "CPU interface for core 0", "for core 1", ...
828 e2c56465 Peter Maydell
     * NB that the memory region size of 0x100 applies for the 11MPCore
829 e2c56465 Peter Maydell
     * and also cores following the GIC v1 spec (ie A9).
830 e2c56465 Peter Maydell
     * GIC v2 defines a larger memory region (0x1000) so this will need
831 e2c56465 Peter Maydell
     * to be extended when we implement A15.
832 e2c56465 Peter Maydell
     */
833 e2c56465 Peter Maydell
    memory_region_init_io(&s->cpuiomem[0], &gic_thiscpu_ops, s,
834 e2c56465 Peter Maydell
                          "gic_cpu", 0x100);
835 e2c56465 Peter Maydell
    for (i = 0; i < NUM_CPU(s); i++) {
836 e2c56465 Peter Maydell
        s->backref[i] = s;
837 e2c56465 Peter Maydell
        memory_region_init_io(&s->cpuiomem[i+1], &gic_cpu_ops, &s->backref[i],
838 e2c56465 Peter Maydell
                              "gic_cpu", 0x100);
839 e2c56465 Peter Maydell
    }
840 e2c56465 Peter Maydell
#endif
841 e2c56465 Peter Maydell
842 e69954b9 pbrook
    gic_reset(s);
843 c2e2343e Dmitry Koshelev
    register_savevm(NULL, "arm_gic", -1, 2, gic_save, gic_load, s);
844 e69954b9 pbrook
}