arm_gic.c 40.9 KB
Newer Older
1
/*
P
pbrook 已提交
2
 * ARM Generic/Distributed Interrupt Controller
P
pbrook 已提交
3
 *
P
pbrook 已提交
4
 * Copyright (c) 2006-2007 CodeSourcery.
P
pbrook 已提交
5 6
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GPL.
P
pbrook 已提交
8 9
 */

P
pbrook 已提交
10
/* This file contains implementation code for the RealView EB interrupt
11 12 13 14 15 16 17 18 19
 * controller, MPCore distributed interrupt controller and ARMv7-M
 * Nested Vectored Interrupt Controller.
 * It is compiled in two ways:
 *  (1) as a standalone file to produce a sysbus device which is a GIC
 *  that can be used on the realview board and as one of the builtin
 *  private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
 *  (2) by being directly #included into armv7m_nvic.c to produce the
 *  armv7m_nvic device.
 */
P
pbrook 已提交
20

21
#include "hw/sysbus.h"
22
#include "gic_internal.h"
23
#include "qom/cpu.h"
24

P
pbrook 已提交
25 26 27
//#define DEBUG_GIC

#ifdef DEBUG_GIC
28
#define DPRINTF(fmt, ...) \
29
do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
P
pbrook 已提交
30
#else
31
#define DPRINTF(fmt, ...) do {} while(0)
P
pbrook 已提交
32 33
#endif

34 35 36 37
static const uint8_t gic_id[] = {
    0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
};

P
Paul Brook 已提交
38
#define NUM_CPU(s) ((s)->num_cpu)
P
pbrook 已提交
39

40
static inline int gic_get_current_cpu(GICState *s)
41 42
{
    if (s->num_cpu > 1) {
43
        return current_cpu->cpu_index;
44 45 46 47
    }
    return 0;
}

48 49 50 51 52 53 54 55
/* Return true if this GIC config has interrupt groups, which is
 * true if we're a GICv2, or a GICv1 with the security extensions.
 */
static inline bool gic_has_groups(GICState *s)
{
    return s->revision == 2 || s->security_extn;
}

P
pbrook 已提交
56 57
/* TODO: Many places that call this routine could be optimized.  */
/* Update interrupt status after enabled or pending bits have been changed.  */
58
void gic_update(GICState *s)
P
pbrook 已提交
59 60 61 62
{
    int best_irq;
    int best_prio;
    int irq;
63
    int irq_level, fiq_level;
P
pbrook 已提交
64 65 66
    int cpu;
    int cm;

P
Paul Brook 已提交
67
    for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
P
pbrook 已提交
68 69
        cm = 1 << cpu;
        s->current_pending[cpu] = 1023;
70
        if (!(s->ctlr & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1))
71
            || !(s->cpu_ctlr[cpu] & (GICC_CTLR_EN_GRP0 | GICC_CTLR_EN_GRP1))) {
72
            qemu_irq_lower(s->parent_irq[cpu]);
73
            qemu_irq_lower(s->parent_fiq[cpu]);
74
            continue;
P
pbrook 已提交
75 76 77
        }
        best_prio = 0x100;
        best_irq = 1023;
78
        for (irq = 0; irq < s->num_irq; irq++) {
79 80
            if (GIC_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
                (irq < GIC_INTERNAL || GIC_TARGET(irq) & cm)) {
P
pbrook 已提交
81 82 83 84
                if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
                    best_prio = GIC_GET_PRIORITY(irq, cpu);
                    best_irq = irq;
                }
P
pbrook 已提交
85 86
            }
        }
87 88 89

        irq_level = fiq_level = 0;

90
        if (best_prio < s->priority_mask[cpu]) {
P
pbrook 已提交
91 92
            s->current_pending[cpu] = best_irq;
            if (best_prio < s->running_priority[cpu]) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106
                int group = GIC_TEST_GROUP(best_irq, cm);

                if (extract32(s->ctlr, group, 1) &&
                    extract32(s->cpu_ctlr[cpu], group, 1)) {
                    if (group == 0 && s->cpu_ctlr[cpu] & GICC_CTLR_FIQ_EN) {
                        DPRINTF("Raised pending FIQ %d (cpu %d)\n",
                                best_irq, cpu);
                        fiq_level = 1;
                    } else {
                        DPRINTF("Raised pending IRQ %d (cpu %d)\n",
                                best_irq, cpu);
                        irq_level = 1;
                    }
                }
P
pbrook 已提交
107
            }
P
pbrook 已提交
108
        }
109 110 111

        qemu_set_irq(s->parent_irq[cpu], irq_level);
        qemu_set_irq(s->parent_fiq[cpu], fiq_level);
P
pbrook 已提交
112 113 114
    }
}

115
void gic_set_pending_private(GICState *s, int cpu, int irq)
P
pbrook 已提交
116 117 118
{
    int cm = 1 << cpu;

119
    if (gic_test_pending(s, irq, cm)) {
P
pbrook 已提交
120
        return;
121
    }
P
pbrook 已提交
122 123 124 125 126 127

    DPRINTF("Set %d pending cpu %d\n", irq, cpu);
    GIC_SET_PENDING(irq, cm);
    gic_update(s);
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
                                 int cm, int target)
{
    if (level) {
        GIC_SET_LEVEL(irq, cm);
        if (GIC_TEST_EDGE_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
            DPRINTF("Set %d pending mask %x\n", irq, target);
            GIC_SET_PENDING(irq, target);
        }
    } else {
        GIC_CLEAR_LEVEL(irq, cm);
    }
}

static void gic_set_irq_generic(GICState *s, int irq, int level,
                                int cm, int target)
{
    if (level) {
        GIC_SET_LEVEL(irq, cm);
        DPRINTF("Set %d pending mask %x\n", irq, target);
        if (GIC_TEST_EDGE_TRIGGER(irq)) {
            GIC_SET_PENDING(irq, target);
        }
    } else {
        GIC_CLEAR_LEVEL(irq, cm);
    }
}

P
pbrook 已提交
156
/* Process a change in an external IRQ input.  */
P
pbrook 已提交
157 158
static void gic_set_irq(void *opaque, int irq, int level)
{
159 160 161 162 163 164
    /* Meaning of the 'irq' parameter:
     *  [0..N-1] : external interrupts
     *  [N..N+31] : PPI (internal) interrupts for CPU 0
     *  [N+32..N+63] : PPI (internal interrupts for CPU 1
     *  ...
     */
165
    GICState *s = (GICState *)opaque;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    int cm, target;
    if (irq < (s->num_irq - GIC_INTERNAL)) {
        /* The first external input line is internal interrupt 32.  */
        cm = ALL_CPU_MASK;
        irq += GIC_INTERNAL;
        target = GIC_TARGET(irq);
    } else {
        int cpu;
        irq -= (s->num_irq - GIC_INTERNAL);
        cpu = irq / GIC_INTERNAL;
        irq %= GIC_INTERNAL;
        cm = 1 << cpu;
        target = cm;
    }

181 182
    assert(irq >= GIC_NR_SGIS);

183
    if (level == GIC_TEST_LEVEL(irq, cm)) {
P
pbrook 已提交
184
        return;
185
    }
P
pbrook 已提交
186

187 188
    if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
        gic_set_irq_11mpcore(s, irq, level, cm, target);
P
pbrook 已提交
189
    } else {
190
        gic_set_irq_generic(s, irq, level, cm, target);
P
pbrook 已提交
191
    }
192

P
pbrook 已提交
193 194 195
    gic_update(s);
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
                                            MemTxAttrs attrs)
{
    uint16_t pending_irq = s->current_pending[cpu];

    if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
        int group = GIC_TEST_GROUP(pending_irq, (1 << cpu));
        /* On a GIC without the security extensions, reading this register
         * behaves in the same way as a secure access to a GIC with them.
         */
        bool secure = !s->security_extn || attrs.secure;

        if (group == 0 && !secure) {
            /* Group0 interrupts hidden from Non-secure access */
            return 1023;
        }
        if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
            /* Group1 interrupts only seen by Secure access if
             * AckCtl bit set.
             */
            return 1022;
        }
    }
    return pending_irq;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
static int gic_get_group_priority(GICState *s, int cpu, int irq)
{
    /* Return the group priority of the specified interrupt
     * (which is the top bits of its priority, with the number
     * of bits masked determined by the applicable binary point register).
     */
    int bpr;
    uint32_t mask;

    if (gic_has_groups(s) &&
        !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
        GIC_TEST_GROUP(irq, (1 << cpu))) {
        bpr = s->abpr[cpu];
    } else {
        bpr = s->bpr[cpu];
    }

    /* a BPR of 0 means the group priority bits are [7:1];
     * a BPR of 1 means they are [7:2], and so on down to
     * a BPR of 7 meaning no group priority bits at all.
     */
    mask = ~0U << ((bpr & 7) + 1);

    return GIC_GET_PRIORITY(irq, cpu) & mask;
}

248
static void gic_activate_irq(GICState *s, int cpu, int irq)
P
pbrook 已提交
249
{
250 251 252 253 254 255 256 257 258 259
    /* Set the appropriate Active Priority Register bit for this IRQ,
     * and update the running priority.
     */
    int prio = gic_get_group_priority(s, cpu, irq);
    int preemption_level = prio >> (GIC_MIN_BPR + 1);
    int regno = preemption_level / 32;
    int bitno = preemption_level % 32;

    if (gic_has_groups(s) && GIC_TEST_GROUP(irq, (1 << cpu))) {
        s->nsapr[regno][cpu] &= (1 << bitno);
P
pbrook 已提交
260
    } else {
261
        s->apr[regno][cpu] &= (1 << bitno);
P
pbrook 已提交
262
    }
263 264

    s->running_priority[cpu] = prio;
265
    GIC_SET_ACTIVE(irq, 1 << cpu);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
}

static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
{
    /* Recalculate the current running priority for this CPU based
     * on the set bits in the Active Priority Registers.
     */
    int i;
    for (i = 0; i < GIC_NR_APRS; i++) {
        uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
        if (!apr) {
            continue;
        }
        return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
    }
    return 0x100;
}

static void gic_drop_prio(GICState *s, int cpu, int group)
{
    /* Drop the priority of the currently active interrupt in the
     * specified group.
     *
     * Note that we can guarantee (because of the requirement to nest
     * GICC_IAR reads [which activate an interrupt and raise priority]
     * with GICC_EOIR writes [which drop the priority for the interrupt])
     * that the interrupt we're being called for is the highest priority
     * active interrupt, meaning that it has the lowest set bit in the
     * APR registers.
     *
     * If the guest does not honour the ordering constraints then the
     * behaviour of the GIC is UNPREDICTABLE, which for us means that
     * the values of the APR registers might become incorrect and the
     * running priority will be wrong, so interrupts that should preempt
     * might not do so, and interrupts that should not preempt might do so.
     */
    int i;

    for (i = 0; i < GIC_NR_APRS; i++) {
        uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
        if (!*papr) {
            continue;
        }
        /* Clear lowest set bit */
        *papr &= *papr - 1;
        break;
    }

    s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
P
pbrook 已提交
315 316
}

317
uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
P
pbrook 已提交
318
{
319
    int ret, irq, src;
P
pbrook 已提交
320
    int cm = 1 << cpu;
321 322 323 324 325

    /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
     * for the case where this GIC supports grouping and the pending interrupt
     * is in the wrong group.
     */
326
    irq = gic_get_current_pending_irq(s, cpu, attrs);
327 328 329 330 331 332 333 334

    if (irq >= GIC_MAXIRQ) {
        DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
        return irq;
    }

    if (GIC_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) {
        DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
P
pbrook 已提交
335 336
        return 1023;
    }
337

338
    if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        /* Clear pending flags for both level and edge triggered interrupts.
         * Level triggered IRQs will be reasserted once they become inactive.
         */
        GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
        ret = irq;
    } else {
        if (irq < GIC_NR_SGIS) {
            /* Lookup the source CPU for the SGI and clear this in the
             * sgi_pending map.  Return the src and clear the overall pending
             * state on this CPU if the SGI is not pending from any CPUs.
             */
            assert(s->sgi_pending[irq][cpu] != 0);
            src = ctz32(s->sgi_pending[irq][cpu]);
            s->sgi_pending[irq][cpu] &= ~(1 << src);
            if (s->sgi_pending[irq][cpu] == 0) {
                GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
            }
            ret = irq | ((src & 0x7) << 10);
        } else {
            /* Clear pending state for both level and edge triggered
             * interrupts. (level triggered interrupts with an active line
             * remain pending, see gic_test_pending)
             */
            GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
            ret = irq;
        }
    }

367 368
    gic_activate_irq(s, cpu, irq);
    gic_update(s);
369 370
    DPRINTF("ACK %d\n", irq);
    return ret;
P
pbrook 已提交
371 372
}

373 374
void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val,
                      MemTxAttrs attrs)
375
{
376 377 378 379 380 381 382
    if (s->security_extn && !attrs.secure) {
        if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
            return; /* Ignore Non-secure access of Group0 IRQ */
        }
        val = 0x80 | (val >> 1); /* Non-secure view */
    }

383 384 385 386 387 388 389
    if (irq < GIC_INTERNAL) {
        s->priority1[irq][cpu] = val;
    } else {
        s->priority2[(irq) - GIC_INTERNAL] = val;
    }
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
static uint32_t gic_get_priority(GICState *s, int cpu, int irq,
                                 MemTxAttrs attrs)
{
    uint32_t prio = GIC_GET_PRIORITY(irq, cpu);

    if (s->security_extn && !attrs.secure) {
        if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
            return 0; /* Non-secure access cannot read priority of Group0 IRQ */
        }
        prio = (prio << 1) & 0xff; /* Non-secure view */
    }
    return prio;
}

static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
                                  MemTxAttrs attrs)
{
    if (s->security_extn && !attrs.secure) {
        if (s->priority_mask[cpu] & 0x80) {
            /* Priority Mask in upper half */
            pmask = 0x80 | (pmask >> 1);
        } else {
            /* Non-secure write ignored if priority mask is in lower half */
            return;
        }
    }
    s->priority_mask[cpu] = pmask;
}

static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
{
    uint32_t pmask = s->priority_mask[cpu];

    if (s->security_extn && !attrs.secure) {
        if (pmask & 0x80) {
            /* Priority Mask in upper half, return Non-secure view */
            pmask = (pmask << 1) & 0xff;
        } else {
            /* Priority Mask in lower half, RAZ */
            pmask = 0;
        }
    }
    return pmask;
}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
{
    uint32_t ret = s->cpu_ctlr[cpu];

    if (s->security_extn && !attrs.secure) {
        /* Construct the NS banked view of GICC_CTLR from the correct
         * bits of the S banked view. We don't need to move the bypass
         * control bits because we don't implement that (IMPDEF) part
         * of the GIC architecture.
         */
        ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
    }
    return ret;
}

static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
                                MemTxAttrs attrs)
{
    uint32_t mask;

    if (s->security_extn && !attrs.secure) {
        /* The NS view can only write certain bits in the register;
         * the rest are unchanged
         */
        mask = GICC_CTLR_EN_GRP1;
        if (s->revision == 2) {
            mask |= GICC_CTLR_EOIMODE_NS;
        }
        s->cpu_ctlr[cpu] &= ~mask;
        s->cpu_ctlr[cpu] |= (value << 1) & mask;
    } else {
        if (s->revision == 2) {
            mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
        } else {
            mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
        }
        s->cpu_ctlr[cpu] = value & mask;
    }
    DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
            "Group1 Interrupts %sabled\n", cpu,
            (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
            (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
{
    if (s->security_extn && !attrs.secure) {
        if (s->running_priority[cpu] & 0x80) {
            /* Running priority in upper half of range: return the Non-secure
             * view of the priority.
             */
            return s->running_priority[cpu] << 1;
        } else {
            /* Running priority in lower half of range: RAZ */
            return 0;
        }
    } else {
        return s->running_priority[cpu];
    }
}

496
void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
P
pbrook 已提交
497
{
P
pbrook 已提交
498
    int cm = 1 << cpu;
499 500
    int group;

P
pbrook 已提交
501
    DPRINTF("EOI %d\n", irq);
502
    if (irq >= s->num_irq) {
503 504 505 506 507 508 509 510 511 512
        /* This handles two cases:
         * 1. If software writes the ID of a spurious interrupt [ie 1023]
         * to the GICC_EOIR, the GIC ignores that write.
         * 2. If software writes the number of a non-existent interrupt
         * this must be a subcase of "value written does not match the last
         * valid interrupt value read from the Interrupt Acknowledge
         * register" and so this is UNPREDICTABLE. We choose to ignore it.
         */
        return;
    }
513
    if (s->running_priority[cpu] == 0x100) {
P
pbrook 已提交
514
        return; /* No active IRQ.  */
515
    }
516 517 518 519 520 521 522 523 524

    if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
        /* Mark level triggered interrupts as pending if they are still
           raised.  */
        if (!GIC_TEST_EDGE_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
            && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
            DPRINTF("Set %d pending mask %x\n", irq, cm);
            GIC_SET_PENDING(irq, cm);
        }
P
pbrook 已提交
525
    }
526

527 528 529
    group = gic_has_groups(s) && GIC_TEST_GROUP(irq, cm);

    if (s->security_extn && !attrs.secure && !group) {
530 531 532 533 534 535 536 537 538
        DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
        return;
    }

    /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
     * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
     * i.e. go ahead and complete the irq anyway.
     */

539
    gic_drop_prio(s, cpu, group);
540
    GIC_CLEAR_ACTIVE(irq, cm);
541
    gic_update(s);
P
pbrook 已提交
542 543
}

544
static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
P
pbrook 已提交
545
{
546
    GICState *s = (GICState *)opaque;
P
pbrook 已提交
547 548 549
    uint32_t res;
    int irq;
    int i;
P
pbrook 已提交
550 551 552
    int cpu;
    int cm;
    int mask;
P
pbrook 已提交
553

554
    cpu = gic_get_current_cpu(s);
P
pbrook 已提交
555
    cm = 1 << cpu;
P
pbrook 已提交
556
    if (offset < 0x100) {
557 558 559 560 561 562 563 564 565 566
        if (offset == 0) {      /* GICD_CTLR */
            if (s->security_extn && !attrs.secure) {
                /* The NS bank of this register is just an alias of the
                 * EnableGrp1 bit in the S bank version.
                 */
                return extract32(s->ctlr, 1, 1);
            } else {
                return s->ctlr;
            }
        }
P
pbrook 已提交
567
        if (offset == 4)
568 569 570 571
            /* Interrupt Controller Type Register */
            return ((s->num_irq / 32) - 1)
                    | ((NUM_CPU(s) - 1) << 5)
                    | (s->security_extn << 10);
P
pbrook 已提交
572 573
        if (offset < 0x08)
            return 0;
574
        if (offset >= 0x80) {
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
            /* Interrupt Group Registers: these RAZ/WI if this is an NS
             * access to a GIC with the security extensions, or if the GIC
             * doesn't have groups at all.
             */
            res = 0;
            if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
                /* Every byte offset holds 8 group status bits */
                irq = (offset - 0x080) * 8 + GIC_BASE_IRQ;
                if (irq >= s->num_irq) {
                    goto bad_reg;
                }
                for (i = 0; i < 8; i++) {
                    if (GIC_TEST_GROUP(irq + i, cm)) {
                        res |= (1 << i);
                    }
                }
            }
            return res;
593
        }
P
pbrook 已提交
594 595 596 597 598 599 600
        goto bad_reg;
    } else if (offset < 0x200) {
        /* Interrupt Set/Clear Enable.  */
        if (offset < 0x180)
            irq = (offset - 0x100) * 8;
        else
            irq = (offset - 0x180) * 8;
P
pbrook 已提交
601
        irq += GIC_BASE_IRQ;
602
        if (irq >= s->num_irq)
P
pbrook 已提交
603 604 605
            goto bad_reg;
        res = 0;
        for (i = 0; i < 8; i++) {
606
            if (GIC_TEST_ENABLED(irq + i, cm)) {
P
pbrook 已提交
607 608 609 610 611 612 613 614 615
                res |= (1 << i);
            }
        }
    } else if (offset < 0x300) {
        /* Interrupt Set/Clear Pending.  */
        if (offset < 0x280)
            irq = (offset - 0x200) * 8;
        else
            irq = (offset - 0x280) * 8;
P
pbrook 已提交
616
        irq += GIC_BASE_IRQ;
617
        if (irq >= s->num_irq)
P
pbrook 已提交
618 619
            goto bad_reg;
        res = 0;
R
Rusty Russell 已提交
620
        mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
P
pbrook 已提交
621
        for (i = 0; i < 8; i++) {
622
            if (gic_test_pending(s, irq + i, mask)) {
P
pbrook 已提交
623 624 625 626 627
                res |= (1 << i);
            }
        }
    } else if (offset < 0x400) {
        /* Interrupt Active.  */
P
pbrook 已提交
628
        irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
629
        if (irq >= s->num_irq)
P
pbrook 已提交
630 631
            goto bad_reg;
        res = 0;
R
Rusty Russell 已提交
632
        mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
P
pbrook 已提交
633
        for (i = 0; i < 8; i++) {
P
pbrook 已提交
634
            if (GIC_TEST_ACTIVE(irq + i, mask)) {
P
pbrook 已提交
635 636 637 638 639
                res |= (1 << i);
            }
        }
    } else if (offset < 0x800) {
        /* Interrupt Priority.  */
P
pbrook 已提交
640
        irq = (offset - 0x400) + GIC_BASE_IRQ;
641
        if (irq >= s->num_irq)
P
pbrook 已提交
642
            goto bad_reg;
643
        res = gic_get_priority(s, cpu, irq, attrs);
P
pbrook 已提交
644 645
    } else if (offset < 0xc00) {
        /* Interrupt CPU Target.  */
646 647 648
        if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
            /* For uniprocessor GICs these RAZ/WI */
            res = 0;
P
pbrook 已提交
649
        } else {
650 651 652 653 654 655 656 657 658
            irq = (offset - 0x800) + GIC_BASE_IRQ;
            if (irq >= s->num_irq) {
                goto bad_reg;
            }
            if (irq >= 29 && irq <= 31) {
                res = cm;
            } else {
                res = GIC_TARGET(irq);
            }
P
pbrook 已提交
659
        }
P
pbrook 已提交
660 661
    } else if (offset < 0xf00) {
        /* Interrupt Configuration.  */
662
        irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
663
        if (irq >= s->num_irq)
P
pbrook 已提交
664 665 666 667 668
            goto bad_reg;
        res = 0;
        for (i = 0; i < 4; i++) {
            if (GIC_TEST_MODEL(irq + i))
                res |= (1 << (i * 2));
669
            if (GIC_TEST_EDGE_TRIGGER(irq + i))
P
pbrook 已提交
670 671
                res |= (2 << (i * 2));
        }
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    } else if (offset < 0xf10) {
        goto bad_reg;
    } else if (offset < 0xf30) {
        if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
            goto bad_reg;
        }

        if (offset < 0xf20) {
            /* GICD_CPENDSGIRn */
            irq = (offset - 0xf10);
        } else {
            irq = (offset - 0xf20);
            /* GICD_SPENDSGIRn */
        }

        res = s->sgi_pending[irq][cpu];
P
pbrook 已提交
688 689 690 691 692 693 694 695 696 697 698
    } else if (offset < 0xfe0) {
        goto bad_reg;
    } else /* offset >= 0xfe0 */ {
        if (offset & 3) {
            res = 0;
        } else {
            res = gic_id[(offset - 0xfe0) >> 2];
        }
    }
    return res;
bad_reg:
P
Peter Maydell 已提交
699 700
    qemu_log_mask(LOG_GUEST_ERROR,
                  "gic_dist_readb: Bad offset %x\n", (int)offset);
P
pbrook 已提交
701 702 703
    return 0;
}

704 705
static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
                                 unsigned size, MemTxAttrs attrs)
P
pbrook 已提交
706
{
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    switch (size) {
    case 1:
        *data = gic_dist_readb(opaque, offset, attrs);
        return MEMTX_OK;
    case 2:
        *data = gic_dist_readb(opaque, offset, attrs);
        *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
        return MEMTX_OK;
    case 4:
        *data = gic_dist_readb(opaque, offset, attrs);
        *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
        *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
        *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
        return MEMTX_OK;
    default:
        return MEMTX_ERROR;
    }
P
pbrook 已提交
724 725
}

A
Avi Kivity 已提交
726
static void gic_dist_writeb(void *opaque, hwaddr offset,
727
                            uint32_t value, MemTxAttrs attrs)
P
pbrook 已提交
728
{
729
    GICState *s = (GICState *)opaque;
P
pbrook 已提交
730 731
    int irq;
    int i;
P
pbrook 已提交
732
    int cpu;
P
pbrook 已提交
733

734
    cpu = gic_get_current_cpu(s);
P
pbrook 已提交
735 736
    if (offset < 0x100) {
        if (offset == 0) {
737 738 739 740 741 742 743 744 745 746 747
            if (s->security_extn && !attrs.secure) {
                /* NS version is just an alias of the S version's bit 1 */
                s->ctlr = deposit32(s->ctlr, 1, 1, value);
            } else if (gic_has_groups(s)) {
                s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
            } else {
                s->ctlr = value & GICD_CTLR_EN_GRP0;
            }
            DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
                    s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
                    s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
P
pbrook 已提交
748 749
        } else if (offset < 4) {
            /* ignored.  */
750
        } else if (offset >= 0x80) {
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
            /* Interrupt Group Registers: RAZ/WI for NS access to secure
             * GIC, or for GICs without groups.
             */
            if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
                /* Every byte offset holds 8 group status bits */
                irq = (offset - 0x80) * 8 + GIC_BASE_IRQ;
                if (irq >= s->num_irq) {
                    goto bad_reg;
                }
                for (i = 0; i < 8; i++) {
                    /* Group bits are banked for private interrupts */
                    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
                    if (value & (1 << i)) {
                        /* Group1 (Non-secure) */
                        GIC_SET_GROUP(irq + i, cm);
                    } else {
                        /* Group0 (Secure) */
                        GIC_CLEAR_GROUP(irq + i, cm);
                    }
                }
            }
P
pbrook 已提交
772 773 774 775 776
        } else {
            goto bad_reg;
        }
    } else if (offset < 0x180) {
        /* Interrupt Set Enable.  */
P
pbrook 已提交
777
        irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
778
        if (irq >= s->num_irq)
P
pbrook 已提交
779
            goto bad_reg;
780 781 782 783
        if (irq < GIC_NR_SGIS) {
            value = 0xff;
        }

P
pbrook 已提交
784 785
        for (i = 0; i < 8; i++) {
            if (value & (1 << i)) {
786 787
                int mask =
                    (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
R
Rusty Russell 已提交
788
                int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
789 790

                if (!GIC_TEST_ENABLED(irq + i, cm)) {
P
pbrook 已提交
791
                    DPRINTF("Enabled IRQ %d\n", irq + i);
792 793
                }
                GIC_SET_ENABLED(irq + i, cm);
P
pbrook 已提交
794 795
                /* If a raised level triggered IRQ enabled then mark
                   is as pending.  */
P
pbrook 已提交
796
                if (GIC_TEST_LEVEL(irq + i, mask)
797
                        && !GIC_TEST_EDGE_TRIGGER(irq + i)) {
P
pbrook 已提交
798 799 800
                    DPRINTF("Set %d pending mask %x\n", irq + i, mask);
                    GIC_SET_PENDING(irq + i, mask);
                }
P
pbrook 已提交
801 802 803 804
            }
        }
    } else if (offset < 0x200) {
        /* Interrupt Clear Enable.  */
P
pbrook 已提交
805
        irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
806
        if (irq >= s->num_irq)
P
pbrook 已提交
807
            goto bad_reg;
808 809 810 811
        if (irq < GIC_NR_SGIS) {
            value = 0;
        }

P
pbrook 已提交
812 813
        for (i = 0; i < 8; i++) {
            if (value & (1 << i)) {
R
Rusty Russell 已提交
814
                int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
815 816

                if (GIC_TEST_ENABLED(irq + i, cm)) {
P
pbrook 已提交
817
                    DPRINTF("Disabled IRQ %d\n", irq + i);
818 819
                }
                GIC_CLEAR_ENABLED(irq + i, cm);
P
pbrook 已提交
820 821 822 823
            }
        }
    } else if (offset < 0x280) {
        /* Interrupt Set Pending.  */
P
pbrook 已提交
824
        irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
825
        if (irq >= s->num_irq)
P
pbrook 已提交
826
            goto bad_reg;
827
        if (irq < GIC_NR_SGIS) {
828
            value = 0;
829
        }
P
pbrook 已提交
830

P
pbrook 已提交
831 832
        for (i = 0; i < 8; i++) {
            if (value & (1 << i)) {
833
                GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
P
pbrook 已提交
834 835 836 837
            }
        }
    } else if (offset < 0x300) {
        /* Interrupt Clear Pending.  */
P
pbrook 已提交
838
        irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
839
        if (irq >= s->num_irq)
P
pbrook 已提交
840
            goto bad_reg;
841 842 843 844
        if (irq < GIC_NR_SGIS) {
            value = 0;
        }

P
pbrook 已提交
845
        for (i = 0; i < 8; i++) {
P
pbrook 已提交
846 847 848
            /* ??? This currently clears the pending bit for all CPUs, even
               for per-CPU interrupts.  It's unclear whether this is the
               corect behavior.  */
P
pbrook 已提交
849
            if (value & (1 << i)) {
P
pbrook 已提交
850
                GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
P
pbrook 已提交
851 852 853 854 855 856 857
            }
        }
    } else if (offset < 0x400) {
        /* Interrupt Active.  */
        goto bad_reg;
    } else if (offset < 0x800) {
        /* Interrupt Priority.  */
P
pbrook 已提交
858
        irq = (offset - 0x400) + GIC_BASE_IRQ;
859
        if (irq >= s->num_irq)
P
pbrook 已提交
860
            goto bad_reg;
861
        gic_set_priority(s, cpu, irq, value, attrs);
P
pbrook 已提交
862
    } else if (offset < 0xc00) {
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
        /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
         * annoying exception of the 11MPCore's GIC.
         */
        if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
            irq = (offset - 0x800) + GIC_BASE_IRQ;
            if (irq >= s->num_irq) {
                goto bad_reg;
            }
            if (irq < 29) {
                value = 0;
            } else if (irq < GIC_INTERNAL) {
                value = ALL_CPU_MASK;
            }
            s->irq_target[irq] = value & ALL_CPU_MASK;
        }
P
pbrook 已提交
878 879
    } else if (offset < 0xf00) {
        /* Interrupt Configuration.  */
P
pbrook 已提交
880
        irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
881
        if (irq >= s->num_irq)
P
pbrook 已提交
882
            goto bad_reg;
883
        if (irq < GIC_NR_SGIS)
P
pbrook 已提交
884
            value |= 0xaa;
P
pbrook 已提交
885
        for (i = 0; i < 4; i++) {
886 887 888 889 890 891
            if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
                if (value & (1 << (i * 2))) {
                    GIC_SET_MODEL(irq + i);
                } else {
                    GIC_CLEAR_MODEL(irq + i);
                }
P
pbrook 已提交
892 893
            }
            if (value & (2 << (i * 2))) {
894
                GIC_SET_EDGE_TRIGGER(irq + i);
P
pbrook 已提交
895
            } else {
896
                GIC_CLEAR_EDGE_TRIGGER(irq + i);
P
pbrook 已提交
897 898
            }
        }
899
    } else if (offset < 0xf10) {
P
pbrook 已提交
900
        /* 0xf00 is only handled for 32-bit writes.  */
P
pbrook 已提交
901
        goto bad_reg;
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
    } else if (offset < 0xf20) {
        /* GICD_CPENDSGIRn */
        if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
            goto bad_reg;
        }
        irq = (offset - 0xf10);

        s->sgi_pending[irq][cpu] &= ~value;
        if (s->sgi_pending[irq][cpu] == 0) {
            GIC_CLEAR_PENDING(irq, 1 << cpu);
        }
    } else if (offset < 0xf30) {
        /* GICD_SPENDSGIRn */
        if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
            goto bad_reg;
        }
        irq = (offset - 0xf20);

        GIC_SET_PENDING(irq, 1 << cpu);
        s->sgi_pending[irq][cpu] |= value;
    } else {
        goto bad_reg;
P
pbrook 已提交
924 925 926 927
    }
    gic_update(s);
    return;
bad_reg:
P
Peter Maydell 已提交
928 929
    qemu_log_mask(LOG_GUEST_ERROR,
                  "gic_dist_writeb: Bad offset %x\n", (int)offset);
P
pbrook 已提交
930 931
}

A
Avi Kivity 已提交
932
static void gic_dist_writew(void *opaque, hwaddr offset,
933
                            uint32_t value, MemTxAttrs attrs)
P
pbrook 已提交
934
{
935 936
    gic_dist_writeb(opaque, offset, value & 0xff, attrs);
    gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
P
pbrook 已提交
937 938
}

A
Avi Kivity 已提交
939
static void gic_dist_writel(void *opaque, hwaddr offset,
940
                            uint32_t value, MemTxAttrs attrs)
P
pbrook 已提交
941
{
942
    GICState *s = (GICState *)opaque;
943
    if (offset == 0xf00) {
P
pbrook 已提交
944 945 946
        int cpu;
        int irq;
        int mask;
947
        int target_cpu;
P
pbrook 已提交
948

949
        cpu = gic_get_current_cpu(s);
P
pbrook 已提交
950 951 952 953 954 955
        irq = value & 0x3ff;
        switch ((value >> 24) & 3) {
        case 0:
            mask = (value >> 16) & ALL_CPU_MASK;
            break;
        case 1:
956
            mask = ALL_CPU_MASK ^ (1 << cpu);
P
pbrook 已提交
957 958
            break;
        case 2:
959
            mask = 1 << cpu;
P
pbrook 已提交
960 961 962 963 964 965 966
            break;
        default:
            DPRINTF("Bad Soft Int target filter\n");
            mask = ALL_CPU_MASK;
            break;
        }
        GIC_SET_PENDING(irq, mask);
967 968 969 970 971 972
        target_cpu = ctz32(mask);
        while (target_cpu < GIC_NCPU) {
            s->sgi_pending[irq][target_cpu] |= (1 << cpu);
            mask &= ~(1 << target_cpu);
            target_cpu = ctz32(mask);
        }
P
pbrook 已提交
973 974 975
        gic_update(s);
        return;
    }
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
    gic_dist_writew(opaque, offset, value & 0xffff, attrs);
    gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
}

static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
                                  unsigned size, MemTxAttrs attrs)
{
    switch (size) {
    case 1:
        gic_dist_writeb(opaque, offset, data, attrs);
        return MEMTX_OK;
    case 2:
        gic_dist_writew(opaque, offset, data, attrs);
        return MEMTX_OK;
    case 4:
        gic_dist_writel(opaque, offset, data, attrs);
        return MEMTX_OK;
    default:
        return MEMTX_ERROR;
    }
P
pbrook 已提交
996 997
}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
{
    /* Return the Nonsecure view of GICC_APR<regno>. This is the
     * second half of GICC_NSAPR.
     */
    switch (GIC_MIN_BPR) {
    case 0:
        if (regno < 2) {
            return s->nsapr[regno + 2][cpu];
        }
        break;
    case 1:
        if (regno == 0) {
            return s->nsapr[regno + 1][cpu];
        }
        break;
    case 2:
        if (regno == 0) {
            return extract32(s->nsapr[0][cpu], 16, 16);
        }
        break;
    case 3:
        if (regno == 0) {
            return extract32(s->nsapr[0][cpu], 8, 8);
        }
        break;
    default:
        g_assert_not_reached();
    }
    return 0;
}

static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
                                         uint32_t value)
{
    /* Write the Nonsecure view of GICC_APR<regno>. */
    switch (GIC_MIN_BPR) {
    case 0:
        if (regno < 2) {
            s->nsapr[regno + 2][cpu] = value;
        }
        break;
    case 1:
        if (regno == 0) {
            s->nsapr[regno + 1][cpu] = value;
        }
        break;
    case 2:
        if (regno == 0) {
            s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
        }
        break;
    case 3:
        if (regno == 0) {
            s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
        }
        break;
    default:
        g_assert_not_reached();
    }
}

1060 1061
static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
                                uint64_t *data, MemTxAttrs attrs)
P
pbrook 已提交
1062 1063 1064
{
    switch (offset) {
    case 0x00: /* Control */
1065
        *data = gic_get_cpu_control(s, cpu, attrs);
1066
        break;
P
pbrook 已提交
1067
    case 0x04: /* Priority mask */
1068
        *data = gic_get_priority_mask(s, cpu, attrs);
1069
        break;
P
pbrook 已提交
1070
    case 0x08: /* Binary Point */
1071 1072 1073 1074 1075 1076
        if (s->security_extn && !attrs.secure) {
            /* BPR is banked. Non-secure copy stored in ABPR. */
            *data = s->abpr[cpu];
        } else {
            *data = s->bpr[cpu];
        }
1077
        break;
P
pbrook 已提交
1078
    case 0x0c: /* Acknowledge */
1079
        *data = gic_acknowledge_irq(s, cpu, attrs);
1080
        break;
D
Dong Xu Wang 已提交
1081
    case 0x14: /* Running Priority */
1082
        *data = gic_get_running_priority(s, cpu, attrs);
1083
        break;
P
pbrook 已提交
1084
    case 0x18: /* Highest Pending Interrupt */
1085
        *data = gic_get_current_pending_irq(s, cpu, attrs);
1086
        break;
1087
    case 0x1c: /* Aliased Binary Point */
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
        /* GIC v2, no security: ABPR
         * GIC v1, no security: not implemented (RAZ/WI)
         * With security extensions, secure access: ABPR (alias of NS BPR)
         * With security extensions, nonsecure access: RAZ/WI
         */
        if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
            *data = 0;
        } else {
            *data = s->abpr[cpu];
        }
1098
        break;
1099
    case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    {
        int regno = (offset - 0xd0) / 4;

        if (regno >= GIC_NR_APRS || s->revision != 2) {
            *data = 0;
        } else if (s->security_extn && !attrs.secure) {
            /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
            *data = gic_apr_ns_view(s, regno, cpu);
        } else {
            *data = s->apr[regno][cpu];
        }
1111
        break;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    }
    case 0xe0: case 0xe4: case 0xe8: case 0xec:
    {
        int regno = (offset - 0xe0) / 4;

        if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
            (s->security_extn && !attrs.secure)) {
            *data = 0;
        } else {
            *data = s->nsapr[regno][cpu];
        }
        break;
    }
P
pbrook 已提交
1125
    default:
P
Peter Maydell 已提交
1126 1127
        qemu_log_mask(LOG_GUEST_ERROR,
                      "gic_cpu_read: Bad offset %x\n", (int)offset);
1128
        return MEMTX_ERROR;
P
pbrook 已提交
1129
    }
1130
    return MEMTX_OK;
P
pbrook 已提交
1131 1132
}

1133 1134
static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
                                 uint32_t value, MemTxAttrs attrs)
P
pbrook 已提交
1135 1136 1137
{
    switch (offset) {
    case 0x00: /* Control */
1138
        gic_set_cpu_control(s, cpu, value, attrs);
P
pbrook 已提交
1139 1140
        break;
    case 0x04: /* Priority mask */
1141
        gic_set_priority_mask(s, cpu, value, attrs);
P
pbrook 已提交
1142 1143
        break;
    case 0x08: /* Binary Point */
1144 1145 1146 1147 1148
        if (s->security_extn && !attrs.secure) {
            s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
        } else {
            s->bpr[cpu] = MAX(value & 0x7, GIC_MIN_BPR);
        }
P
pbrook 已提交
1149 1150
        break;
    case 0x10: /* End Of Interrupt */
1151
        gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1152
        return MEMTX_OK;
1153
    case 0x1c: /* Aliased Binary Point */
1154 1155 1156 1157 1158
        if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
            /* unimplemented, or NS access: RAZ/WI */
            return MEMTX_OK;
        } else {
            s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1159 1160
        }
        break;
1161
    case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    {
        int regno = (offset - 0xd0) / 4;

        if (regno >= GIC_NR_APRS || s->revision != 2) {
            return MEMTX_OK;
        }
        if (s->security_extn && !attrs.secure) {
            /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
            gic_apr_write_ns_view(s, regno, cpu, value);
        } else {
            s->apr[regno][cpu] = value;
        }
        break;
    }
    case 0xe0: case 0xe4: case 0xe8: case 0xec:
    {
        int regno = (offset - 0xe0) / 4;

        if (regno >= GIC_NR_APRS || s->revision != 2) {
            return MEMTX_OK;
        }
        if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
            return MEMTX_OK;
        }
        s->nsapr[regno][cpu] = value;
1187
        break;
1188
    }
P
pbrook 已提交
1189
    default:
P
Peter Maydell 已提交
1190 1191
        qemu_log_mask(LOG_GUEST_ERROR,
                      "gic_cpu_write: Bad offset %x\n", (int)offset);
1192
        return MEMTX_ERROR;
P
pbrook 已提交
1193 1194
    }
    gic_update(s);
1195
    return MEMTX_OK;
P
pbrook 已提交
1196
}
1197 1198

/* Wrappers to read/write the GIC CPU interface for the current CPU */
1199 1200
static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
                                    unsigned size, MemTxAttrs attrs)
1201
{
1202
    GICState *s = (GICState *)opaque;
1203
    return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1204 1205
}

1206 1207 1208
static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
                                     uint64_t value, unsigned size,
                                     MemTxAttrs attrs)
1209
{
1210
    GICState *s = (GICState *)opaque;
1211
    return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1212 1213 1214
}

/* Wrappers to read/write the GIC CPU interface for a specific CPU.
1215
 * These just decode the opaque pointer into GICState* + cpu id.
1216
 */
1217 1218
static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
                                   unsigned size, MemTxAttrs attrs)
1219
{
1220 1221
    GICState **backref = (GICState **)opaque;
    GICState *s = *backref;
1222
    int id = (backref - s->backref);
1223
    return gic_cpu_read(s, id, addr, data, attrs);
1224 1225
}

1226 1227 1228
static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
                                    uint64_t value, unsigned size,
                                    MemTxAttrs attrs)
1229
{
1230 1231
    GICState **backref = (GICState **)opaque;
    GICState *s = *backref;
1232
    int id = (backref - s->backref);
1233
    return gic_cpu_write(s, id, addr, value, attrs);
1234 1235
}

P
Pavel Fedin 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
static const MemoryRegionOps gic_ops[2] = {
    {
        .read_with_attrs = gic_dist_read,
        .write_with_attrs = gic_dist_write,
        .endianness = DEVICE_NATIVE_ENDIAN,
    },
    {
        .read_with_attrs = gic_thiscpu_read,
        .write_with_attrs = gic_thiscpu_write,
        .endianness = DEVICE_NATIVE_ENDIAN,
    }
1247 1248 1249
};

static const MemoryRegionOps gic_cpu_ops = {
1250 1251
    .read_with_attrs = gic_do_cpu_read,
    .write_with_attrs = gic_do_cpu_write,
1252 1253
    .endianness = DEVICE_NATIVE_ENDIAN,
};
P
pbrook 已提交
1254

P
Pavel Fedin 已提交
1255
/* This function is used by nvic model */
1256
void gic_init_irqs_and_distributor(GICState *s)
P
pbrook 已提交
1257
{
P
Pavel Fedin 已提交
1258
    gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1259 1260
}

1261
static void arm_gic_realize(DeviceState *dev, Error **errp)
1262
{
1263
    /* Device instance realize function for the GIC sysbus device */
1264
    int i;
1265 1266
    GICState *s = ARM_GIC(dev);
    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1267
    ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
1268
    Error *local_err = NULL;
1269

1270 1271 1272
    agc->parent_realize(dev, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
1273 1274
        return;
    }
1275

P
Pavel Fedin 已提交
1276 1277
    /* This creates distributor and main CPU interface (s->cpuiomem[0]) */
    gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1278

P
Pavel Fedin 已提交
1279 1280 1281
    /* Extra core-specific regions for the CPU interfaces. This is
     * necessary for "franken-GIC" implementations, for example on
     * Exynos 4.
1282 1283 1284 1285 1286 1287 1288
     * NB that the memory region size of 0x100 applies for the 11MPCore
     * and also cores following the GIC v1 spec (ie A9).
     * GIC v2 defines a larger memory region (0x1000) so this will need
     * to be extended when we implement A15.
     */
    for (i = 0; i < NUM_CPU(s); i++) {
        s->backref[i] = s;
1289 1290
        memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
                              &s->backref[i], "gic_cpu", 0x100);
P
Pavel Fedin 已提交
1291
        sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
1292 1293 1294 1295 1296 1297
    }
}

static void arm_gic_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
1298
    ARMGICClass *agc = ARM_GIC_CLASS(klass);
1299 1300 1301

    agc->parent_realize = dc->realize;
    dc->realize = arm_gic_realize;
1302 1303
}

1304
static const TypeInfo arm_gic_info = {
1305 1306
    .name = TYPE_ARM_GIC,
    .parent = TYPE_ARM_GIC_COMMON,
1307
    .instance_size = sizeof(GICState),
1308
    .class_init = arm_gic_class_init,
1309
    .class_size = sizeof(ARMGICClass),
1310 1311 1312 1313 1314 1315 1316 1317
};

static void arm_gic_register_types(void)
{
    type_register_static(&arm_gic_info);
}

type_init(arm_gic_register_types)