Statistics
| Branch: | Revision:

root / hw / arm_gic.c @ 70705261

History | View | Annotate | Download (15.3 kB)

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