qemu/hw/intc/arm_gic.c
<<
>>
Prefs
   1/*
   2 * ARM Generic/Distributed Interrupt Controller
   3 *
   4 * Copyright (c) 2006-2007 CodeSourcery.
   5 * Written by Paul Brook
   6 *
   7 * This code is licensed under the GPL.
   8 */
   9
  10/* This file contains implementation code for the RealView EB interrupt
  11 * controller, MPCore distributed interrupt controller and ARMv7-M
  12 * Nested Vectored Interrupt Controller.
  13 * It is compiled in two ways:
  14 *  (1) as a standalone file to produce a sysbus device which is a GIC
  15 *  that can be used on the realview board and as one of the builtin
  16 *  private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
  17 *  (2) by being directly #included into armv7m_nvic.c to produce the
  18 *  armv7m_nvic device.
  19 */
  20
  21#include "qemu/osdep.h"
  22#include "hw/irq.h"
  23#include "hw/sysbus.h"
  24#include "gic_internal.h"
  25#include "qapi/error.h"
  26#include "hw/core/cpu.h"
  27#include "qemu/log.h"
  28#include "qemu/module.h"
  29#include "trace.h"
  30#include "sysemu/kvm.h"
  31
  32#include "hw/fdt_generic_util.h"
  33
  34/* #define DEBUG_GIC */
  35
  36#ifdef DEBUG_GIC
  37#define DEBUG_GIC_GATE 1
  38#else
  39#define DEBUG_GIC_GATE 0
  40#endif
  41
  42#define DPRINTF(fmt, ...) do {                                          \
  43        if (DEBUG_GIC_GATE) {                                           \
  44            fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__);      \
  45        }                                                               \
  46    } while (0)
  47
  48static const uint8_t gic_id_11mpcore[] = {
  49    0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  50};
  51
  52static const uint8_t gic_id_gicv1[] = {
  53    0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  54};
  55
  56static const uint8_t gic_id_gicv2[] = {
  57    0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  58};
  59
  60static inline int gic_get_current_cpu(GICState *s)
  61{
  62    if ((s->num_cpu > 1) && (current_cpu)) {
  63        return current_cpu->cpu_index % s->num_cpu;
  64    }
  65    return 0;
  66}
  67
  68static inline int gic_get_current_vcpu(GICState *s)
  69{
  70    return gic_get_current_cpu(s) + GIC_NCPU;
  71}
  72
  73/* Return true if this GIC config has interrupt groups, which is
  74 * true if we're a GICv2, or a GICv1 with the security extensions.
  75 */
  76static inline bool gic_has_groups(GICState *s)
  77{
  78    return s->revision == 2 || s->security_extn;
  79}
  80
  81static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
  82{
  83    return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
  84}
  85
  86static inline void gic_get_best_irq(GICState *s, int cpu,
  87                                    int *best_irq, int *best_prio, int *group)
  88{
  89    int irq;
  90    int cm = 1 << cpu;
  91
  92    *best_irq = 1023;
  93    *best_prio = 0x100;
  94
  95    for (irq = 0; irq < s->num_irq; irq++) {
  96        if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
  97            (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
  98            (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
  99            if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
 100                *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
 101                *best_irq = irq;
 102            }
 103        }
 104    }
 105
 106    if (*best_irq < 1023) {
 107        *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
 108    }
 109}
 110
 111static inline void gic_get_best_virq(GICState *s, int cpu,
 112                                     int *best_irq, int *best_prio, int *group)
 113{
 114    int lr_idx = 0;
 115
 116    *best_irq = 1023;
 117    *best_prio = 0x100;
 118
 119    for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
 120        uint32_t lr_entry = s->h_lr[lr_idx][cpu];
 121        int state = GICH_LR_STATE(lr_entry);
 122
 123        if (state == GICH_LR_STATE_PENDING) {
 124            int prio = GICH_LR_PRIORITY(lr_entry);
 125
 126            if (prio < *best_prio) {
 127                *best_prio = prio;
 128                *best_irq = GICH_LR_VIRT_ID(lr_entry);
 129                *group = GICH_LR_GROUP(lr_entry);
 130            }
 131        }
 132    }
 133}
 134
 135/* Return true if IRQ signaling is enabled for the given cpu and at least one
 136 * of the given groups:
 137 *   - in the non-virt case, the distributor must be enabled for one of the
 138 *   given groups
 139 *   - in the virt case, the virtual interface must be enabled.
 140 *   - in all cases, the (v)CPU interface must be enabled for one of the given
 141 *   groups.
 142 */
 143static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
 144                                    int group_mask)
 145{
 146    if (!virt && !(s->ctlr & group_mask)) {
 147        return false;
 148    }
 149
 150    if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
 151        return false;
 152    }
 153
 154    if (!(s->cpu_ctlr[cpu] & group_mask)) {
 155        return false;
 156    }
 157
 158    return true;
 159}
 160
 161/* TODO: Many places that call this routine could be optimized.  */
 162/* Update interrupt status after enabled or pending bits have been changed.  */
 163static inline void gic_update_internal(GICState *s, bool virt)
 164{
 165    int best_irq;
 166    int best_prio;
 167    int irq_level, fiq_level;
 168    int cpu, cpu_iface;
 169    int group = 0;
 170    qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
 171    qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
 172
 173    for (cpu = 0; cpu < s->num_cpu; cpu++) {
 174        cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
 175
 176        s->current_pending[cpu_iface] = 1023;
 177        if (!gic_irq_signaling_enabled(s, cpu, virt,
 178                                       GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
 179            qemu_irq_lower(irq_lines[cpu]);
 180            qemu_irq_lower(fiq_lines[cpu]);
 181            continue;
 182        }
 183
 184        if (virt) {
 185            gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
 186        } else {
 187            gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
 188        }
 189
 190        if (best_irq != 1023) {
 191            trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
 192                                     best_irq, best_prio,
 193                                     s->priority_mask[cpu_iface],
 194                                     s->running_priority[cpu_iface]);
 195        }
 196
 197        irq_level = fiq_level = 0;
 198
 199        if (best_prio < s->priority_mask[cpu_iface]) {
 200            s->current_pending[cpu_iface] = best_irq;
 201            if (best_prio < s->running_priority[cpu_iface]) {
 202                if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
 203                    if (group == 0 &&
 204                        s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
 205                        DPRINTF("Raised pending FIQ %d (cpu %d)\n",
 206                                best_irq, cpu_iface);
 207                        fiq_level = 1;
 208                        trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
 209                                                 fiq_level);
 210                    } else {
 211                        DPRINTF("Raised pending IRQ %d (cpu %d)\n",
 212                                best_irq, cpu_iface);
 213                        irq_level = 1;
 214                        trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
 215                                                 irq_level);
 216                    }
 217                }
 218            }
 219        }
 220
 221        qemu_set_irq(irq_lines[cpu], irq_level);
 222        qemu_set_irq(fiq_lines[cpu], fiq_level);
 223    }
 224}
 225
 226static void gic_update(GICState *s)
 227{
 228    gic_update_internal(s, false);
 229}
 230
 231/* Return true if this LR is empty, i.e. the corresponding bit
 232 * in ELRSR is set.
 233 */
 234static inline bool gic_lr_entry_is_free(uint32_t entry)
 235{
 236    return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
 237        && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
 238}
 239
 240/* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
 241 * corrsponding bit in EISR is set.
 242 */
 243static inline bool gic_lr_entry_is_eoi(uint32_t entry)
 244{
 245    return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
 246        && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
 247}
 248
 249static inline void gic_extract_lr_info(GICState *s, int cpu,
 250                                int *num_eoi, int *num_valid, int *num_pending)
 251{
 252    int lr_idx;
 253
 254    *num_eoi = 0;
 255    *num_valid = 0;
 256    *num_pending = 0;
 257
 258    for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
 259        uint32_t *entry = &s->h_lr[lr_idx][cpu];
 260
 261        if (gic_lr_entry_is_eoi(*entry)) {
 262            (*num_eoi)++;
 263        }
 264
 265        if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
 266            (*num_valid)++;
 267        }
 268
 269        if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
 270            (*num_pending)++;
 271        }
 272    }
 273}
 274
 275static void gic_compute_misr(GICState *s, int cpu)
 276{
 277    uint32_t value = 0;
 278    int vcpu = cpu + GIC_NCPU;
 279
 280    int num_eoi, num_valid, num_pending;
 281
 282    gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
 283
 284    /* EOI */
 285    if (num_eoi) {
 286        value |= R_GICH_MISR_EOI_MASK;
 287    }
 288
 289    /* U: true if only 0 or 1 LR entry is valid */
 290    if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
 291        value |= R_GICH_MISR_U_MASK;
 292    }
 293
 294    /* LRENP: EOICount is not 0 */
 295    if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
 296        ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
 297        value |= R_GICH_MISR_LRENP_MASK;
 298    }
 299
 300    /* NP: no pending interrupts */
 301    if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
 302        value |= R_GICH_MISR_NP_MASK;
 303    }
 304
 305    /* VGrp0E: group0 virq signaling enabled */
 306    if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
 307        (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
 308        value |= R_GICH_MISR_VGrp0E_MASK;
 309    }
 310
 311    /* VGrp0D: group0 virq signaling disabled */
 312    if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
 313        !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
 314        value |= R_GICH_MISR_VGrp0D_MASK;
 315    }
 316
 317    /* VGrp1E: group1 virq signaling enabled */
 318    if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
 319        (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
 320        value |= R_GICH_MISR_VGrp1E_MASK;
 321    }
 322
 323    /* VGrp1D: group1 virq signaling disabled */
 324    if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
 325        !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
 326        value |= R_GICH_MISR_VGrp1D_MASK;
 327    }
 328
 329    s->h_misr[cpu] = value;
 330}
 331
 332static void gic_update_maintenance(GICState *s)
 333{
 334    int cpu = 0;
 335    int maint_level;
 336
 337    for (cpu = 0; cpu < s->num_cpu; cpu++) {
 338        gic_compute_misr(s, cpu);
 339        maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
 340
 341        trace_gic_update_maintenance_irq(cpu, maint_level);
 342        qemu_set_irq(s->maintenance_irq[cpu], maint_level);
 343    }
 344}
 345
 346static void gic_update_virt(GICState *s)
 347{
 348    gic_update_internal(s, true);
 349    gic_update_maintenance(s);
 350}
 351
 352static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
 353                                 int cm, int target)
 354{
 355    if (level) {
 356        GIC_DIST_SET_LEVEL(irq, cm);
 357        if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
 358            DPRINTF("Set %d pending mask %x\n", irq, target);
 359            GIC_DIST_SET_PENDING(irq, target);
 360        }
 361    } else {
 362        GIC_DIST_CLEAR_LEVEL(irq, cm);
 363    }
 364}
 365
 366static void gic_set_irq_generic(GICState *s, int irq, int level,
 367                                int cm, int target)
 368{
 369    if (level) {
 370        GIC_DIST_SET_LEVEL(irq, cm);
 371        DPRINTF("Set %d pending mask %x\n", irq, target);
 372        if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
 373            GIC_DIST_SET_PENDING(irq, target);
 374        }
 375    } else {
 376        GIC_DIST_CLEAR_LEVEL(irq, cm);
 377    }
 378}
 379
 380/* Process a change in an external IRQ input.  */
 381static void gic_set_irq(void *opaque, int irq, int level)
 382{
 383    /* Meaning of the 'irq' parameter:
 384     *  [0..N-1] : external interrupts
 385     *  [N..N+31] : PPI (internal) interrupts for CPU 0
 386     *  [N+32..N+63] : PPI (internal interrupts for CPU 1
 387     *  ...
 388     */
 389    GICState *s = (GICState *)opaque;
 390    int cm, target;
 391    if (irq < (s->num_irq - GIC_INTERNAL)) {
 392        /* The first external input line is internal interrupt 32.  */
 393        cm = ALL_CPU_MASK;
 394        irq += GIC_INTERNAL;
 395        target = GIC_DIST_TARGET(irq);
 396    } else {
 397        int cpu;
 398        irq -= (s->num_irq - GIC_INTERNAL);
 399        cpu = irq / GIC_INTERNAL;
 400        irq %= GIC_INTERNAL;
 401        cm = 1 << cpu;
 402        target = cm;
 403    }
 404
 405    assert(irq >= GIC_NR_SGIS);
 406
 407    if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
 408        return;
 409    }
 410
 411    if (s->revision == REV_11MPCORE) {
 412        gic_set_irq_11mpcore(s, irq, level, cm, target);
 413    } else {
 414        gic_set_irq_generic(s, irq, level, cm, target);
 415    }
 416    trace_gic_set_irq(irq, level, cm, target);
 417
 418    gic_update(s);
 419}
 420
 421static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
 422                                            MemTxAttrs attrs)
 423{
 424    uint16_t pending_irq = s->current_pending[cpu];
 425
 426    if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
 427        int group = gic_test_group(s, pending_irq, cpu);
 428
 429        /* On a GIC without the security extensions, reading this register
 430         * behaves in the same way as a secure access to a GIC with them.
 431         */
 432        bool secure = !gic_cpu_ns_access(s, cpu, attrs);
 433
 434        if (group == 0 && !secure) {
 435            /* Group0 interrupts hidden from Non-secure access */
 436            return 1023;
 437        }
 438        if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
 439            /* Group1 interrupts only seen by Secure access if
 440             * AckCtl bit set.
 441             */
 442            return 1022;
 443        }
 444    }
 445    return pending_irq;
 446}
 447
 448static int gic_get_group_priority(GICState *s, int cpu, int irq)
 449{
 450    /* Return the group priority of the specified interrupt
 451     * (which is the top bits of its priority, with the number
 452     * of bits masked determined by the applicable binary point register).
 453     */
 454    int bpr;
 455    uint32_t mask;
 456
 457    if (gic_has_groups(s) &&
 458        !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
 459        gic_test_group(s, irq, cpu)) {
 460        bpr = s->abpr[cpu] - 1;
 461        assert(bpr >= 0);
 462    } else {
 463        bpr = s->bpr[cpu];
 464    }
 465
 466    /* a BPR of 0 means the group priority bits are [7:1];
 467     * a BPR of 1 means they are [7:2], and so on down to
 468     * a BPR of 7 meaning no group priority bits at all.
 469     */
 470    mask = ~0U << ((bpr & 7) + 1);
 471
 472    return gic_get_priority(s, irq, cpu) & mask;
 473}
 474
 475static void gic_activate_irq(GICState *s, int cpu, int irq)
 476{
 477    /* Set the appropriate Active Priority Register bit for this IRQ,
 478     * and update the running priority.
 479     */
 480    int prio = gic_get_group_priority(s, cpu, irq);
 481    int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
 482    int preemption_level = prio >> (min_bpr + 1);
 483    int regno = preemption_level / 32;
 484    int bitno = preemption_level % 32;
 485    uint32_t *papr = NULL;
 486
 487    if (gic_is_vcpu(cpu)) {
 488        assert(regno == 0);
 489        papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
 490    } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
 491        papr = &s->nsapr[regno][cpu];
 492    } else {
 493        papr = &s->apr[regno][cpu];
 494    }
 495
 496    *papr |= (1 << bitno);
 497
 498    s->running_priority[cpu] = prio;
 499    gic_set_active(s, irq, cpu);
 500}
 501
 502static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
 503{
 504    /* Recalculate the current running priority for this CPU based
 505     * on the set bits in the Active Priority Registers.
 506     */
 507    int i;
 508
 509    if (gic_is_vcpu(cpu)) {
 510        uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
 511        if (apr) {
 512            return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
 513        } else {
 514            return 0x100;
 515        }
 516    }
 517
 518    for (i = 0; i < GIC_NR_APRS; i++) {
 519        uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
 520        if (!apr) {
 521            continue;
 522        }
 523        return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
 524    }
 525    return 0x100;
 526}
 527
 528static void gic_drop_prio(GICState *s, int cpu, int group)
 529{
 530    /* Drop the priority of the currently active interrupt in the
 531     * specified group.
 532     *
 533     * Note that we can guarantee (because of the requirement to nest
 534     * GICC_IAR reads [which activate an interrupt and raise priority]
 535     * with GICC_EOIR writes [which drop the priority for the interrupt])
 536     * that the interrupt we're being called for is the highest priority
 537     * active interrupt, meaning that it has the lowest set bit in the
 538     * APR registers.
 539     *
 540     * If the guest does not honour the ordering constraints then the
 541     * behaviour of the GIC is UNPREDICTABLE, which for us means that
 542     * the values of the APR registers might become incorrect and the
 543     * running priority will be wrong, so interrupts that should preempt
 544     * might not do so, and interrupts that should not preempt might do so.
 545     */
 546    if (gic_is_vcpu(cpu)) {
 547        int rcpu = gic_get_vcpu_real_id(cpu);
 548
 549        if (s->h_apr[rcpu]) {
 550            /* Clear lowest set bit */
 551            s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
 552        }
 553    } else {
 554        int i;
 555
 556        for (i = 0; i < GIC_NR_APRS; i++) {
 557            uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
 558            if (!*papr) {
 559                continue;
 560            }
 561            /* Clear lowest set bit */
 562            *papr &= *papr - 1;
 563            break;
 564        }
 565    }
 566
 567    s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
 568}
 569
 570static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
 571{
 572    int src;
 573    uint32_t ret;
 574
 575    if (!gic_is_vcpu(cpu)) {
 576        /* Lookup the source CPU for the SGI and clear this in the
 577         * sgi_pending map.  Return the src and clear the overall pending
 578         * state on this CPU if the SGI is not pending from any CPUs.
 579         */
 580        assert(s->sgi_pending[irq][cpu] != 0);
 581        src = ctz32(s->sgi_pending[irq][cpu]);
 582        s->sgi_pending[irq][cpu] &= ~(1 << src);
 583        if (s->sgi_pending[irq][cpu] == 0) {
 584            gic_clear_pending(s, irq, cpu);
 585        }
 586        ret = irq | ((src & 0x7) << 10);
 587    } else {
 588        uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
 589        src = GICH_LR_CPUID(*lr_entry);
 590
 591        gic_clear_pending(s, irq, cpu);
 592        ret = irq | (src << 10);
 593    }
 594
 595    return ret;
 596}
 597
 598uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
 599{
 600    int ret, irq;
 601
 602    /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
 603     * for the case where this GIC supports grouping and the pending interrupt
 604     * is in the wrong group.
 605     */
 606    irq = gic_get_current_pending_irq(s, cpu, attrs);
 607    trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
 608                              gic_get_vcpu_real_id(cpu), irq);
 609
 610    if (irq >= GIC_MAXIRQ) {
 611        DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
 612        return irq;
 613    }
 614
 615    if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
 616        DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
 617        return 1023;
 618    }
 619
 620    gic_activate_irq(s, cpu, irq);
 621
 622    if (s->revision == REV_11MPCORE) {
 623        /* Clear pending flags for both level and edge triggered interrupts.
 624         * Level triggered IRQs will be reasserted once they become inactive.
 625         */
 626        gic_clear_pending(s, irq, cpu);
 627        ret = irq;
 628    } else {
 629        if (irq < GIC_NR_SGIS) {
 630            ret = gic_clear_pending_sgi(s, irq, cpu);
 631        } else {
 632            gic_clear_pending(s, irq, cpu);
 633            ret = irq;
 634        }
 635    }
 636
 637    if (gic_is_vcpu(cpu)) {
 638        gic_update_virt(s);
 639    } else {
 640        gic_update(s);
 641    }
 642    DPRINTF("ACK %d\n", irq);
 643    return ret;
 644}
 645
 646static uint32_t gic_fullprio_mask(GICState *s, int cpu)
 647{
 648    /*
 649     * Return a mask word which clears the unimplemented priority
 650     * bits from a priority value for an interrupt. (Not to be
 651     * confused with the group priority, whose mask depends on BPR.)
 652     */
 653    int priBits;
 654
 655    if (gic_is_vcpu(cpu)) {
 656        priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS;
 657    } else {
 658        priBits = s->n_prio_bits;
 659    }
 660    return ~0U << (8 - priBits);
 661}
 662
 663void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
 664                      MemTxAttrs attrs)
 665{
 666    if (s->security_extn && !attrs.secure) {
 667        if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
 668            return; /* Ignore Non-secure access of Group0 IRQ */
 669        }
 670        val = 0x80 | (val >> 1); /* Non-secure view */
 671    }
 672
 673    val &= gic_fullprio_mask(s, cpu);
 674
 675    if (irq < GIC_INTERNAL) {
 676        s->priority1[irq][cpu] = val;
 677    } else {
 678        s->priority2[(irq) - GIC_INTERNAL] = val;
 679    }
 680}
 681
 682static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
 683                                 MemTxAttrs attrs)
 684{
 685    uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
 686
 687    if (s->security_extn && !attrs.secure) {
 688        if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
 689            return 0; /* Non-secure access cannot read priority of Group0 IRQ */
 690        }
 691        prio = (prio << 1) & 0xff; /* Non-secure view */
 692    }
 693    return prio & gic_fullprio_mask(s, cpu);
 694}
 695
 696static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
 697                                  MemTxAttrs attrs)
 698{
 699    if (gic_cpu_ns_access(s, cpu, attrs)) {
 700        if (s->priority_mask[cpu] & 0x80) {
 701            /* Priority Mask in upper half */
 702            pmask = 0x80 | (pmask >> 1);
 703        } else {
 704            /* Non-secure write ignored if priority mask is in lower half */
 705            return;
 706        }
 707    }
 708    s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu);
 709}
 710
 711static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
 712{
 713    uint32_t pmask = s->priority_mask[cpu];
 714
 715    if (gic_cpu_ns_access(s, cpu, attrs)) {
 716        if (pmask & 0x80) {
 717            /* Priority Mask in upper half, return Non-secure view */
 718            pmask = (pmask << 1) & 0xff;
 719        } else {
 720            /* Priority Mask in lower half, RAZ */
 721            pmask = 0;
 722        }
 723    }
 724    return pmask;
 725}
 726
 727static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
 728{
 729    uint32_t ret = s->cpu_ctlr[cpu];
 730
 731    if (gic_cpu_ns_access(s, cpu, attrs)) {
 732        /* Construct the NS banked view of GICC_CTLR from the correct
 733         * bits of the S banked view. We don't need to move the bypass
 734         * control bits because we don't implement that (IMPDEF) part
 735         * of the GIC architecture.
 736         */
 737        ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
 738    }
 739    return ret;
 740}
 741
 742static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
 743                                MemTxAttrs attrs)
 744{
 745    uint32_t mask;
 746
 747    if (gic_cpu_ns_access(s, cpu, attrs)) {
 748        /* The NS view can only write certain bits in the register;
 749         * the rest are unchanged
 750         */
 751        mask = GICC_CTLR_EN_GRP1;
 752        if (s->revision == 2) {
 753            mask |= GICC_CTLR_EOIMODE_NS;
 754        }
 755        s->cpu_ctlr[cpu] &= ~mask;
 756        s->cpu_ctlr[cpu] |= (value << 1) & mask;
 757    } else {
 758        if (s->revision == 2) {
 759            mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
 760        } else {
 761            mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
 762        }
 763        s->cpu_ctlr[cpu] = value & mask;
 764    }
 765    DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
 766            "Group1 Interrupts %sabled\n", cpu,
 767            (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
 768            (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
 769}
 770
 771static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
 772{
 773    if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
 774        /* Idle priority */
 775        return 0xff;
 776    }
 777
 778    if (gic_cpu_ns_access(s, cpu, attrs)) {
 779        if (s->running_priority[cpu] & 0x80) {
 780            /* Running priority in upper half of range: return the Non-secure
 781             * view of the priority.
 782             */
 783            return s->running_priority[cpu] << 1;
 784        } else {
 785            /* Running priority in lower half of range: RAZ */
 786            return 0;
 787        }
 788    } else {
 789        return s->running_priority[cpu];
 790    }
 791}
 792
 793/* Return true if we should split priority drop and interrupt deactivation,
 794 * ie whether the relevant EOIMode bit is set.
 795 */
 796static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
 797{
 798    if (s->revision != 2) {
 799        /* Before GICv2 prio-drop and deactivate are not separable */
 800        return false;
 801    }
 802    if (gic_cpu_ns_access(s, cpu, attrs)) {
 803        return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
 804    }
 805    return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
 806}
 807
 808static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
 809{
 810    int group;
 811
 812    if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
 813        /*
 814         * This handles two cases:
 815         * 1. If software writes the ID of a spurious interrupt [ie 1023]
 816         * to the GICC_DIR, the GIC ignores that write.
 817         * 2. If software writes the number of a non-existent interrupt
 818         * this must be a subcase of "value written is not an active interrupt"
 819         * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
 820         * all IRQs potentially exist, so this limit does not apply.
 821         */
 822        return;
 823    }
 824
 825    if (!gic_eoi_split(s, cpu, attrs)) {
 826        /* This is UNPREDICTABLE; we choose to ignore it */
 827        qemu_log_mask(LOG_GUEST_ERROR,
 828                      "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
 829        return;
 830    }
 831
 832    if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
 833        /* This vIRQ does not have an LR entry which is either active or
 834         * pending and active. Increment EOICount and ignore the write.
 835         */
 836        int rcpu = gic_get_vcpu_real_id(cpu);
 837        s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
 838
 839        /* Update the virtual interface in case a maintenance interrupt should
 840         * be raised.
 841         */
 842        gic_update_virt(s);
 843        return;
 844    }
 845
 846    group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
 847
 848    if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
 849        DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
 850        return;
 851    }
 852
 853    gic_clear_active(s, irq, cpu);
 854}
 855
 856static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
 857{
 858    int cm = 1 << cpu;
 859    int group;
 860
 861    DPRINTF("EOI %d\n", irq);
 862    if (gic_is_vcpu(cpu)) {
 863        /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
 864         * running prio is < 0x100.
 865         */
 866        bool prio_drop = s->running_priority[cpu] < 0x100;
 867
 868        if (irq >= GIC_MAXIRQ) {
 869            /* Ignore spurious interrupt */
 870            return;
 871        }
 872
 873        gic_drop_prio(s, cpu, 0);
 874
 875        if (!gic_eoi_split(s, cpu, attrs)) {
 876            bool valid = gic_virq_is_valid(s, irq, cpu);
 877            if (prio_drop && !valid) {
 878                /* We are in a situation where:
 879                 *   - V_CTRL.EOIMode is false (no EOI split),
 880                 *   - The call to gic_drop_prio() cleared a bit in GICH_APR,
 881                 *   - This vIRQ does not have an LR entry which is either
 882                 *     active or pending and active.
 883                 * In that case, we must increment EOICount.
 884                 */
 885                int rcpu = gic_get_vcpu_real_id(cpu);
 886                s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
 887            } else if (valid) {
 888                gic_clear_active(s, irq, cpu);
 889            }
 890        }
 891
 892        gic_update_virt(s);
 893        return;
 894    }
 895
 896    if (irq >= s->num_irq) {
 897        /* This handles two cases:
 898         * 1. If software writes the ID of a spurious interrupt [ie 1023]
 899         * to the GICC_EOIR, the GIC ignores that write.
 900         * 2. If software writes the number of a non-existent interrupt
 901         * this must be a subcase of "value written does not match the last
 902         * valid interrupt value read from the Interrupt Acknowledge
 903         * register" and so this is UNPREDICTABLE. We choose to ignore it.
 904         */
 905        return;
 906    }
 907    if (s->running_priority[cpu] == 0x100) {
 908        return; /* No active IRQ.  */
 909    }
 910
 911    if (s->revision == REV_11MPCORE) {
 912        /* Mark level triggered interrupts as pending if they are still
 913           raised.  */
 914        if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
 915            && GIC_DIST_TEST_LEVEL(irq, cm)
 916            && (GIC_DIST_TARGET(irq) & cm) != 0) {
 917            DPRINTF("Set %d pending mask %x\n", irq, cm);
 918            GIC_DIST_SET_PENDING(irq, cm);
 919        }
 920    }
 921
 922    group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
 923
 924    if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
 925        DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
 926        return;
 927    }
 928
 929    /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
 930     * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
 931     * i.e. go ahead and complete the irq anyway.
 932     */
 933
 934    gic_drop_prio(s, cpu, group);
 935
 936    /* In GICv2 the guest can choose to split priority-drop and deactivate */
 937    if (!gic_eoi_split(s, cpu, attrs)) {
 938        gic_clear_active(s, irq, cpu);
 939    }
 940    gic_update(s);
 941}
 942
 943static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
 944{
 945    GICState *s = (GICState *)opaque;
 946    uint32_t res;
 947    int irq;
 948    int i;
 949    int cpu;
 950    int cm;
 951    int mask;
 952
 953    cpu = gic_get_current_cpu(s);
 954    cm = 1 << cpu;
 955    if (offset < 0x100) {
 956        if (offset == 0) {      /* GICD_CTLR */
 957            if (s->security_extn && !attrs.secure) {
 958                /* The NS bank of this register is just an alias of the
 959                 * EnableGrp1 bit in the S bank version.
 960                 */
 961                return extract32(s->ctlr, 1, 1);
 962            } else {
 963                return s->ctlr;
 964            }
 965        }
 966        if (offset == 4)
 967            /* Interrupt Controller Type Register */
 968            return ((s->num_irq / 32) - 1)
 969                    | ((s->num_cpu - 1) << 5)
 970                    | (s->security_extn << 10);
 971        if (offset < 0x08)
 972            return 0;
 973        if (offset >= 0x80) {
 974            /* Interrupt Group Registers: these RAZ/WI if this is an NS
 975             * access to a GIC with the security extensions, or if the GIC
 976             * doesn't have groups at all.
 977             */
 978            res = 0;
 979            if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
 980                /* Every byte offset holds 8 group status bits */
 981                irq = (offset - 0x080) * 8;
 982                if (irq >= s->num_irq) {
 983                    goto bad_reg;
 984                }
 985                for (i = 0; i < 8; i++) {
 986                    if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
 987                        res |= (1 << i);
 988                    }
 989                }
 990            }
 991            return res;
 992        }
 993        goto bad_reg;
 994    } else if (offset < 0x200) {
 995        /* Interrupt Set/Clear Enable.  */
 996        if (offset < 0x180)
 997            irq = (offset - 0x100) * 8;
 998        else
 999            irq = (offset - 0x180) * 8;
1000        if (irq >= s->num_irq)
1001            goto bad_reg;
1002        res = 0;
1003        for (i = 0; i < 8; i++) {
1004            if (s->security_extn && !attrs.secure &&
1005                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1006                continue; /* Ignore Non-secure access of Group0 IRQ */
1007            }
1008
1009            if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1010                res |= (1 << i);
1011            }
1012        }
1013    } else if (offset < 0x300) {
1014        /* Interrupt Set/Clear Pending.  */
1015        if (offset < 0x280)
1016            irq = (offset - 0x200) * 8;
1017        else
1018            irq = (offset - 0x280) * 8;
1019        if (irq >= s->num_irq)
1020            goto bad_reg;
1021        res = 0;
1022        mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1023        for (i = 0; i < 8; i++) {
1024            if (s->security_extn && !attrs.secure &&
1025                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1026                continue; /* Ignore Non-secure access of Group0 IRQ */
1027            }
1028
1029            if (gic_test_pending(s, irq + i, mask)) {
1030                res |= (1 << i);
1031            }
1032        }
1033    } else if (offset < 0x400) {
1034        /* Interrupt Set/Clear Active.  */
1035        if (offset < 0x380) {
1036            irq = (offset - 0x300) * 8;
1037        } else if (s->revision == 2) {
1038            irq = (offset - 0x380) * 8;
1039        } else {
1040            goto bad_reg;
1041        }
1042
1043        if (irq >= s->num_irq)
1044            goto bad_reg;
1045        res = 0;
1046        mask = (irq < GIC_INTERNAL) ?  cm : ALL_CPU_MASK;
1047        for (i = 0; i < 8; i++) {
1048            if (s->security_extn && !attrs.secure &&
1049                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1050                continue; /* Ignore Non-secure access of Group0 IRQ */
1051            }
1052
1053            if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1054                res |= (1 << i);
1055            }
1056        }
1057    } else if (offset < 0x800) {
1058        /* Interrupt Priority.  */
1059        irq = (offset - 0x400);
1060        if (irq >= s->num_irq)
1061            goto bad_reg;
1062        res = gic_dist_get_priority(s, cpu, irq, attrs);
1063    } else if (offset < 0xc00) {
1064        /* Interrupt CPU Target.  */
1065        if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
1066            /* For uniprocessor GICs these RAZ/WI */
1067            res = 0;
1068        } else {
1069            irq = (offset - 0x800);
1070            if (irq >= s->num_irq) {
1071                goto bad_reg;
1072            }
1073            if (irq < 29 && s->revision == REV_11MPCORE) {
1074                res = 0;
1075            } else if (irq < GIC_INTERNAL) {
1076                res = cm;
1077            } else {
1078                res = GIC_DIST_TARGET(irq);
1079            }
1080        }
1081    } else if (offset < 0xf00) {
1082        /* Interrupt Configuration.  */
1083        irq = (offset - 0xc00) * 4;
1084        if (irq >= s->num_irq)
1085            goto bad_reg;
1086        res = 0;
1087        for (i = 0; i < 4; i++) {
1088            if (s->security_extn && !attrs.secure &&
1089                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1090                continue; /* Ignore Non-secure access of Group0 IRQ */
1091            }
1092
1093            if (GIC_DIST_TEST_MODEL(irq + i)) {
1094                res |= (1 << (i * 2));
1095            }
1096            if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1097                res |= (2 << (i * 2));
1098            }
1099        }
1100    } else if (offset < 0xf10) {
1101        goto bad_reg;
1102    } else if (offset < 0xf30) {
1103        if (s->revision == REV_11MPCORE) {
1104            goto bad_reg;
1105        }
1106
1107        if (offset < 0xf20) {
1108            /* GICD_CPENDSGIRn */
1109            irq = (offset - 0xf10);
1110        } else {
1111            irq = (offset - 0xf20);
1112            /* GICD_SPENDSGIRn */
1113        }
1114
1115        if (s->security_extn && !attrs.secure &&
1116            !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1117            res = 0; /* Ignore Non-secure access of Group0 IRQ */
1118        } else {
1119            res = s->sgi_pending[irq][cpu];
1120        }
1121    } else if (offset < 0xfd0) {
1122        goto bad_reg;
1123    } else if (offset < 0x1000) {
1124        if (offset & 3) {
1125            res = 0;
1126        } else {
1127            switch (s->revision) {
1128            case REV_11MPCORE:
1129                res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
1130                break;
1131            case 1:
1132                res = gic_id_gicv1[(offset - 0xfd0) >> 2];
1133                break;
1134            case 2:
1135                res = gic_id_gicv2[(offset - 0xfd0) >> 2];
1136                break;
1137            default:
1138                res = 0;
1139            }
1140        }
1141    } else {
1142        g_assert_not_reached();
1143    }
1144    return res;
1145bad_reg:
1146    qemu_log_mask(LOG_GUEST_ERROR,
1147                  "gic_dist_readb: Bad offset %x\n", (int)offset);
1148    return 0;
1149}
1150
1151static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1152                                 unsigned size, MemTxAttrs attrs)
1153{
1154    switch (size) {
1155    case 1:
1156        *data = gic_dist_readb(opaque, offset, attrs);
1157        break;
1158    case 2:
1159        *data = gic_dist_readb(opaque, offset, attrs);
1160        *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1161        break;
1162    case 4:
1163        *data = gic_dist_readb(opaque, offset, attrs);
1164        *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1165        *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1166        *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1167        break;
1168    default:
1169        return MEMTX_ERROR;
1170    }
1171
1172    trace_gic_dist_read(offset, size, *data);
1173    return MEMTX_OK;
1174}
1175
1176static void gic_dist_writeb(void *opaque, hwaddr offset,
1177                            uint32_t value, MemTxAttrs attrs)
1178{
1179    GICState *s = (GICState *)opaque;
1180    int irq;
1181    int i;
1182    int cpu;
1183
1184    cpu = gic_get_current_cpu(s);
1185    if (offset < 0x100) {
1186        if (offset == 0) {
1187            if (s->security_extn && !attrs.secure) {
1188                /* NS version is just an alias of the S version's bit 1 */
1189                s->ctlr = deposit32(s->ctlr, 1, 1, value);
1190            } else if (gic_has_groups(s)) {
1191                s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1192            } else {
1193                s->ctlr = value & GICD_CTLR_EN_GRP0;
1194            }
1195            DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1196                    s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1197                    s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1198        } else if (offset < 4) {
1199            /* ignored.  */
1200        } else if (offset >= 0x80) {
1201            /* Interrupt Group Registers: RAZ/WI for NS access to secure
1202             * GIC, or for GICs without groups.
1203             */
1204            if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1205                /* Every byte offset holds 8 group status bits */
1206                irq = (offset - 0x80) * 8;
1207                if (irq >= s->num_irq) {
1208                    goto bad_reg;
1209                }
1210                for (i = 0; i < 8; i++) {
1211                    /* Group bits are banked for private interrupts */
1212                    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1213                    if (value & (1 << i)) {
1214                        /* Group1 (Non-secure) */
1215                        GIC_DIST_SET_GROUP(irq + i, cm);
1216                    } else {
1217                        /* Group0 (Secure) */
1218                        GIC_DIST_CLEAR_GROUP(irq + i, cm);
1219                    }
1220                }
1221            }
1222        } else {
1223            goto bad_reg;
1224        }
1225    } else if (offset < 0x180) {
1226        /* Interrupt Set Enable.  */
1227        irq = (offset - 0x100) * 8;
1228        if (irq >= s->num_irq)
1229            goto bad_reg;
1230        if (irq < GIC_NR_SGIS) {
1231            value = 0xff;
1232        }
1233
1234        for (i = 0; i < 8; i++) {
1235            if (value & (1 << i)) {
1236                int mask =
1237                    (irq < GIC_INTERNAL) ? (1 << cpu)
1238                                         : GIC_DIST_TARGET(irq + i);
1239                int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1240
1241                if (s->security_extn && !attrs.secure &&
1242                    !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1243                    continue; /* Ignore Non-secure access of Group0 IRQ */
1244                }
1245
1246                if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1247                    DPRINTF("Enabled IRQ %d\n", irq + i);
1248                    trace_gic_enable_irq(irq + i);
1249                }
1250                GIC_DIST_SET_ENABLED(irq + i, cm);
1251                /* If a raised level triggered IRQ enabled then mark
1252                   is as pending.  */
1253                if (GIC_DIST_TEST_LEVEL(irq + i, mask)
1254                        && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1255                    DPRINTF("Set %d pending mask %x\n", irq + i, mask);
1256                    GIC_DIST_SET_PENDING(irq + i, mask);
1257                }
1258            }
1259        }
1260    } else if (offset < 0x200) {
1261        /* Interrupt Clear Enable.  */
1262        irq = (offset - 0x180) * 8;
1263        if (irq >= s->num_irq)
1264            goto bad_reg;
1265        if (irq < GIC_NR_SGIS) {
1266            value = 0;
1267        }
1268
1269        for (i = 0; i < 8; i++) {
1270            if (value & (1 << i)) {
1271                int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1272
1273                if (s->security_extn && !attrs.secure &&
1274                    !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1275                    continue; /* Ignore Non-secure access of Group0 IRQ */
1276                }
1277
1278                if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1279                    DPRINTF("Disabled IRQ %d\n", irq + i);
1280                    trace_gic_disable_irq(irq + i);
1281                }
1282                GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1283            }
1284        }
1285    } else if (offset < 0x280) {
1286        /* Interrupt Set Pending.  */
1287        irq = (offset - 0x200) * 8;
1288        if (irq >= s->num_irq)
1289            goto bad_reg;
1290        if (irq < GIC_NR_SGIS) {
1291            value = 0;
1292        }
1293
1294        for (i = 0; i < 8; i++) {
1295            if (value & (1 << i)) {
1296                if (s->security_extn && !attrs.secure &&
1297                    !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1298                    continue; /* Ignore Non-secure access of Group0 IRQ */
1299                }
1300
1301                GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
1302            }
1303        }
1304    } else if (offset < 0x300) {
1305        /* Interrupt Clear Pending.  */
1306        irq = (offset - 0x280) * 8;
1307        if (irq >= s->num_irq)
1308            goto bad_reg;
1309        if (irq < GIC_NR_SGIS) {
1310            value = 0;
1311        }
1312
1313        for (i = 0; i < 8; i++) {
1314            if (s->security_extn && !attrs.secure &&
1315                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1316                continue; /* Ignore Non-secure access of Group0 IRQ */
1317            }
1318
1319            /* ??? This currently clears the pending bit for all CPUs, even
1320               for per-CPU interrupts.  It's unclear whether this is the
1321               corect behavior.  */
1322            if (value & (1 << i)) {
1323                GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1324            }
1325        }
1326    } else if (offset < 0x380) {
1327        /* Interrupt Set Active.  */
1328        if (s->revision != 2) {
1329            goto bad_reg;
1330        }
1331
1332        irq = (offset - 0x300) * 8;
1333        if (irq >= s->num_irq) {
1334            goto bad_reg;
1335        }
1336
1337        /* This register is banked per-cpu for PPIs */
1338        int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
1339
1340        for (i = 0; i < 8; i++) {
1341            if (s->security_extn && !attrs.secure &&
1342                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1343                continue; /* Ignore Non-secure access of Group0 IRQ */
1344            }
1345
1346            if (value & (1 << i)) {
1347                GIC_DIST_SET_ACTIVE(irq + i, cm);
1348            }
1349        }
1350    } else if (offset < 0x400) {
1351        /* Interrupt Clear Active.  */
1352        if (s->revision != 2) {
1353            goto bad_reg;
1354        }
1355
1356        irq = (offset - 0x380) * 8;
1357        if (irq >= s->num_irq) {
1358            goto bad_reg;
1359        }
1360
1361        /* This register is banked per-cpu for PPIs */
1362        int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
1363
1364        for (i = 0; i < 8; i++) {
1365            if (s->security_extn && !attrs.secure &&
1366                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1367                continue; /* Ignore Non-secure access of Group0 IRQ */
1368            }
1369
1370            if (value & (1 << i)) {
1371                GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
1372            }
1373        }
1374    } else if (offset < 0x800) {
1375        /* Interrupt Priority.  */
1376        irq = (offset - 0x400);
1377        if (irq >= s->num_irq)
1378            goto bad_reg;
1379        gic_dist_set_priority(s, cpu, irq, value, attrs);
1380    } else if (offset < 0xc00) {
1381        /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
1382         * annoying exception of the 11MPCore's GIC.
1383         */
1384        if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
1385            irq = (offset - 0x800);
1386            if (irq >= s->num_irq) {
1387                goto bad_reg;
1388            }
1389            if (irq < 29 && s->revision == REV_11MPCORE) {
1390                value = 0;
1391            } else if (irq < GIC_INTERNAL) {
1392                value = ALL_CPU_MASK;
1393            }
1394            s->irq_target[irq] = value & ALL_CPU_MASK;
1395        }
1396    } else if (offset < 0xf00) {
1397        /* Interrupt Configuration.  */
1398        irq = (offset - 0xc00) * 4;
1399        if (irq >= s->num_irq)
1400            goto bad_reg;
1401        if (irq < GIC_NR_SGIS)
1402            value |= 0xaa;
1403        for (i = 0; i < 4; i++) {
1404            if (s->security_extn && !attrs.secure &&
1405                !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1406                continue; /* Ignore Non-secure access of Group0 IRQ */
1407            }
1408
1409            if (s->revision == REV_11MPCORE) {
1410                if (value & (1 << (i * 2))) {
1411                    GIC_DIST_SET_MODEL(irq + i);
1412                } else {
1413                    GIC_DIST_CLEAR_MODEL(irq + i);
1414                }
1415            }
1416            if (value & (2 << (i * 2))) {
1417                GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1418            } else {
1419                GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1420            }
1421        }
1422    } else if (offset < 0xf10) {
1423        /* 0xf00 is only handled for 32-bit writes.  */
1424        goto bad_reg;
1425    } else if (offset < 0xf20) {
1426        /* GICD_CPENDSGIRn */
1427        if (s->revision == REV_11MPCORE) {
1428            goto bad_reg;
1429        }
1430        irq = (offset - 0xf10);
1431
1432        if (!s->security_extn || attrs.secure ||
1433            GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1434            s->sgi_pending[irq][cpu] &= ~value;
1435            if (s->sgi_pending[irq][cpu] == 0) {
1436                GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
1437            }
1438        }
1439    } else if (offset < 0xf30) {
1440        /* GICD_SPENDSGIRn */
1441        if (s->revision == REV_11MPCORE) {
1442            goto bad_reg;
1443        }
1444        irq = (offset - 0xf20);
1445
1446        if (!s->security_extn || attrs.secure ||
1447            GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1448            GIC_DIST_SET_PENDING(irq, 1 << cpu);
1449            s->sgi_pending[irq][cpu] |= value;
1450        }
1451    } else {
1452        goto bad_reg;
1453    }
1454    gic_update(s);
1455    return;
1456bad_reg:
1457    qemu_log_mask(LOG_GUEST_ERROR,
1458                  "gic_dist_writeb: Bad offset %x\n", (int)offset);
1459}
1460
1461static void gic_dist_writew(void *opaque, hwaddr offset,
1462                            uint32_t value, MemTxAttrs attrs)
1463{
1464    gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1465    gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1466}
1467
1468static void gic_dist_writel(void *opaque, hwaddr offset,
1469                            uint32_t value, MemTxAttrs attrs)
1470{
1471    GICState *s = (GICState *)opaque;
1472    if (offset == 0xf00) {
1473        int cpu;
1474        int irq;
1475        int mask;
1476        int target_cpu;
1477
1478        cpu = gic_get_current_cpu(s);
1479        irq = value & 0x3ff;
1480        switch ((value >> 24) & 3) {
1481        case 0:
1482            mask = (value >> 16) & ALL_CPU_MASK;
1483            break;
1484        case 1:
1485            mask = ALL_CPU_MASK ^ (1 << cpu);
1486            break;
1487        case 2:
1488            mask = 1 << cpu;
1489            break;
1490        default:
1491            DPRINTF("Bad Soft Int target filter\n");
1492            mask = ALL_CPU_MASK;
1493            break;
1494        }
1495        GIC_DIST_SET_PENDING(irq, mask);
1496        target_cpu = ctz32(mask);
1497        while (target_cpu < GIC_NCPU) {
1498            s->sgi_pending[irq][target_cpu] |= (1 << cpu);
1499            mask &= ~(1 << target_cpu);
1500            target_cpu = ctz32(mask);
1501        }
1502        gic_update(s);
1503        return;
1504    }
1505    gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1506    gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1507}
1508
1509static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1510                                  unsigned size, MemTxAttrs attrs)
1511{
1512    trace_gic_dist_write(offset, size, data);
1513
1514    switch (size) {
1515    case 1:
1516        gic_dist_writeb(opaque, offset, data, attrs);
1517        return MEMTX_OK;
1518    case 2:
1519        gic_dist_writew(opaque, offset, data, attrs);
1520        return MEMTX_OK;
1521    case 4:
1522        gic_dist_writel(opaque, offset, data, attrs);
1523        return MEMTX_OK;
1524    default:
1525        return MEMTX_ERROR;
1526    }
1527}
1528
1529static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
1530{
1531    /* Return the Nonsecure view of GICC_APR<regno>. This is the
1532     * second half of GICC_NSAPR.
1533     */
1534    switch (GIC_MIN_BPR) {
1535    case 0:
1536        if (regno < 2) {
1537            return s->nsapr[regno + 2][cpu];
1538        }
1539        break;
1540    case 1:
1541        if (regno == 0) {
1542            return s->nsapr[regno + 1][cpu];
1543        }
1544        break;
1545    case 2:
1546        if (regno == 0) {
1547            return extract32(s->nsapr[0][cpu], 16, 16);
1548        }
1549        break;
1550    case 3:
1551        if (regno == 0) {
1552            return extract32(s->nsapr[0][cpu], 8, 8);
1553        }
1554        break;
1555    default:
1556        g_assert_not_reached();
1557    }
1558    return 0;
1559}
1560
1561static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
1562                                         uint32_t value)
1563{
1564    /* Write the Nonsecure view of GICC_APR<regno>. */
1565    switch (GIC_MIN_BPR) {
1566    case 0:
1567        if (regno < 2) {
1568            s->nsapr[regno + 2][cpu] = value;
1569        }
1570        break;
1571    case 1:
1572        if (regno == 0) {
1573            s->nsapr[regno + 1][cpu] = value;
1574        }
1575        break;
1576    case 2:
1577        if (regno == 0) {
1578            s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
1579        }
1580        break;
1581    case 3:
1582        if (regno == 0) {
1583            s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
1584        }
1585        break;
1586    default:
1587        g_assert_not_reached();
1588    }
1589}
1590
1591static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1592                                uint64_t *data, MemTxAttrs attrs)
1593{
1594    switch (offset) {
1595    case 0x00: /* Control */
1596        *data = gic_get_cpu_control(s, cpu, attrs);
1597        break;
1598    case 0x04: /* Priority mask */
1599        *data = gic_get_priority_mask(s, cpu, attrs);
1600        break;
1601    case 0x08: /* Binary Point */
1602        if (gic_cpu_ns_access(s, cpu, attrs)) {
1603            if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1604                /* NS view of BPR when CBPR is 1 */
1605                *data = MIN(s->bpr[cpu] + 1, 7);
1606            } else {
1607                /* BPR is banked. Non-secure copy stored in ABPR. */
1608                *data = s->abpr[cpu];
1609            }
1610        } else {
1611            *data = s->bpr[cpu];
1612        }
1613        break;
1614    case 0x0c: /* Acknowledge */
1615        *data = gic_acknowledge_irq(s, cpu, attrs);
1616        break;
1617    case 0x14: /* Running Priority */
1618        *data = gic_get_running_priority(s, cpu, attrs);
1619        break;
1620    case 0x18: /* Highest Pending Interrupt */
1621        *data = gic_get_current_pending_irq(s, cpu, attrs);
1622        break;
1623    case 0x1c: /* Aliased Binary Point */
1624        /* GIC v2, no security: ABPR
1625         * GIC v1, no security: not implemented (RAZ/WI)
1626         * With security extensions, secure access: ABPR (alias of NS BPR)
1627         * With security extensions, nonsecure access: RAZ/WI
1628         */
1629        if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1630            *data = 0;
1631        } else {
1632            *data = s->abpr[cpu];
1633        }
1634        break;
1635    case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1636    {
1637        int regno = (offset - 0xd0) / 4;
1638        int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
1639
1640        if (regno >= nr_aprs || s->revision != 2) {
1641            *data = 0;
1642        } else if (gic_is_vcpu(cpu)) {
1643            *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
1644        } else if (gic_cpu_ns_access(s, cpu, attrs)) {
1645            /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1646            *data = gic_apr_ns_view(s, regno, cpu);
1647        } else {
1648            *data = s->apr[regno][cpu];
1649        }
1650        break;
1651    }
1652    case 0xe0: case 0xe4: case 0xe8: case 0xec:
1653    {
1654        int regno = (offset - 0xe0) / 4;
1655
1656        if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
1657            gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
1658            *data = 0;
1659        } else {
1660            *data = s->nsapr[regno][cpu];
1661        }
1662        break;
1663    }
1664    case 0xfc:
1665        *data = s->c_iidr;
1666        break;
1667    default:
1668        qemu_log_mask(LOG_GUEST_ERROR,
1669                      "gic_cpu_read: Bad offset %x\n", (int)offset);
1670        *data = 0;
1671        break;
1672    }
1673
1674    trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1675                       gic_get_vcpu_real_id(cpu), offset, *data);
1676    return MEMTX_OK;
1677}
1678
1679static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1680                                 uint32_t value, MemTxAttrs attrs)
1681{
1682    trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1683                        gic_get_vcpu_real_id(cpu), offset, value);
1684
1685    switch (offset) {
1686    case 0x00: /* Control */
1687        gic_set_cpu_control(s, cpu, value, attrs);
1688        break;
1689    case 0x04: /* Priority mask */
1690        gic_set_priority_mask(s, cpu, value, attrs);
1691        break;
1692    case 0x08: /* Binary Point */
1693        if (gic_cpu_ns_access(s, cpu, attrs)) {
1694            if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1695                /* WI when CBPR is 1 */
1696                return MEMTX_OK;
1697            } else {
1698                s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1699            }
1700        } else {
1701            int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
1702            s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1703        }
1704        break;
1705    case 0x10: /* End Of Interrupt */
1706        gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1707        return MEMTX_OK;
1708    case 0x1c: /* Aliased Binary Point */
1709        if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1710            /* unimplemented, or NS access: RAZ/WI */
1711            return MEMTX_OK;
1712        } else {
1713            s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1714        }
1715        break;
1716    case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1717    {
1718        int regno = (offset - 0xd0) / 4;
1719        int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
1720
1721        if (regno >= nr_aprs || s->revision != 2) {
1722            return MEMTX_OK;
1723        }
1724        if (gic_is_vcpu(cpu)) {
1725            s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
1726        } else if (gic_cpu_ns_access(s, cpu, attrs)) {
1727            /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1728            gic_apr_write_ns_view(s, regno, cpu, value);
1729        } else {
1730            s->apr[regno][cpu] = value;
1731        }
1732        break;
1733    }
1734    case 0xe0: case 0xe4: case 0xe8: case 0xec:
1735    {
1736        int regno = (offset - 0xe0) / 4;
1737
1738        if (regno >= GIC_NR_APRS || s->revision != 2) {
1739            return MEMTX_OK;
1740        }
1741        if (gic_is_vcpu(cpu)) {
1742            return MEMTX_OK;
1743        }
1744        if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1745            return MEMTX_OK;
1746        }
1747        s->nsapr[regno][cpu] = value;
1748        break;
1749    }
1750    /* TODO: Remove the 0x10000 offset together with deprecating map-stride.  */
1751    case 0x10000:
1752    case 0x1000:
1753        /* GICC_DIR */
1754        gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1755        break;
1756    default:
1757        qemu_log_mask(LOG_GUEST_ERROR,
1758                      "gic_cpu_write: Bad offset %x\n", (int)offset);
1759        return MEMTX_OK;
1760    }
1761
1762    if (gic_is_vcpu(cpu)) {
1763        gic_update_virt(s);
1764    } else {
1765        gic_update(s);
1766    }
1767
1768    return MEMTX_OK;
1769}
1770
1771/* Wrappers to read/write the GIC CPU interface for the current CPU */
1772static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1773                                    unsigned size, MemTxAttrs attrs)
1774{
1775    GICState *s = (GICState *)opaque;
1776    return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1777}
1778
1779static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1780                                     uint64_t value, unsigned size,
1781                                     MemTxAttrs attrs)
1782{
1783    GICState *s = (GICState *)opaque;
1784    return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1785}
1786
1787/* Wrappers to read/write the GIC CPU interface for a specific CPU.
1788 * These just decode the opaque pointer into GICState* + cpu id.
1789 */
1790static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1791                                   unsigned size, MemTxAttrs attrs)
1792{
1793    GICState **backref = (GICState **)opaque;
1794    GICState *s = *backref;
1795    int id = (backref - s->backref);
1796    return gic_cpu_read(s, id, addr, data, attrs);
1797}
1798
1799static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1800                                    uint64_t value, unsigned size,
1801                                    MemTxAttrs attrs)
1802{
1803    GICState **backref = (GICState **)opaque;
1804    GICState *s = *backref;
1805    int id = (backref - s->backref);
1806    return gic_cpu_write(s, id, addr, value, attrs);
1807}
1808
1809static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
1810                                    unsigned size, MemTxAttrs attrs)
1811{
1812    GICState *s = (GICState *)opaque;
1813
1814    return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
1815}
1816
1817static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
1818                                     uint64_t value, unsigned size,
1819                                     MemTxAttrs attrs)
1820{
1821    GICState *s = (GICState *)opaque;
1822
1823    return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
1824}
1825
1826static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1827{
1828    int lr_idx;
1829    uint32_t ret = 0;
1830
1831    for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1832        uint32_t *entry = &s->h_lr[lr_idx][cpu];
1833        ret = deposit32(ret, lr_idx - lr_start, 1,
1834                        gic_lr_entry_is_eoi(*entry));
1835    }
1836
1837    return ret;
1838}
1839
1840static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1841{
1842    int lr_idx;
1843    uint32_t ret = 0;
1844
1845    for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1846        uint32_t *entry = &s->h_lr[lr_idx][cpu];
1847        ret = deposit32(ret, lr_idx - lr_start, 1,
1848                        gic_lr_entry_is_free(*entry));
1849    }
1850
1851    return ret;
1852}
1853
1854static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1855{
1856    int vcpu = gic_get_current_vcpu(s);
1857    uint32_t ctlr;
1858    uint32_t abpr;
1859    uint32_t bpr;
1860    uint32_t prio_mask;
1861
1862    ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1863    abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1864    bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1865    prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1866
1867    gic_set_cpu_control(s, vcpu, ctlr, attrs);
1868    s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1869    s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1870    gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1871}
1872
1873static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1874                                uint64_t *data, MemTxAttrs attrs)
1875{
1876    GICState *s = ARM_GIC(opaque);
1877    int vcpu = cpu + GIC_NCPU;
1878
1879    switch (addr) {
1880    case A_GICH_HCR: /* Hypervisor Control */
1881        *data = s->h_hcr[cpu];
1882        break;
1883
1884    case A_GICH_VTR: /* VGIC Type */
1885        *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1886        *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1887                           GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1888        *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1889                           (7 - GIC_VIRT_MIN_BPR) - 1);
1890        break;
1891
1892    case A_GICH_VMCR: /* Virtual Machine Control */
1893        *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1894                           extract32(s->cpu_ctlr[vcpu], 0, 10));
1895        *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1896        *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1897        *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1898                           extract32(s->priority_mask[vcpu], 3, 5));
1899        break;
1900
1901    case A_GICH_MISR: /* Maintenance Interrupt Status */
1902        *data = s->h_misr[cpu];
1903        break;
1904
1905    case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
1906    case A_GICH_EISR1:
1907        *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1908        break;
1909
1910    case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
1911    case A_GICH_ELRSR1:
1912        *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1913        break;
1914
1915    case A_GICH_APR: /* Active Priorities */
1916        *data = s->h_apr[cpu];
1917        break;
1918
1919    case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1920    {
1921        int lr_idx = (addr - A_GICH_LR0) / 4;
1922
1923        if (lr_idx > s->num_lrs) {
1924            *data = 0;
1925        } else {
1926            *data = s->h_lr[lr_idx][cpu];
1927        }
1928        break;
1929    }
1930
1931    default:
1932        qemu_log_mask(LOG_GUEST_ERROR,
1933                      "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1934        return MEMTX_OK;
1935    }
1936
1937    trace_gic_hyp_read(addr, *data);
1938    return MEMTX_OK;
1939}
1940
1941static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1942                                 uint64_t value, MemTxAttrs attrs)
1943{
1944    GICState *s = ARM_GIC(opaque);
1945    int vcpu = cpu + GIC_NCPU;
1946
1947    trace_gic_hyp_write(addr, value);
1948
1949    switch (addr) {
1950    case A_GICH_HCR: /* Hypervisor Control */
1951        s->h_hcr[cpu] = value & GICH_HCR_MASK;
1952        break;
1953
1954    case A_GICH_VMCR: /* Virtual Machine Control */
1955        gic_vmcr_write(s, value, attrs);
1956        break;
1957
1958    case A_GICH_APR: /* Active Priorities */
1959        s->h_apr[cpu] = value;
1960        s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1961        break;
1962
1963    case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
1964    {
1965        int lr_idx = (addr - A_GICH_LR0) / 4;
1966
1967        if (lr_idx > s->num_lrs) {
1968            return MEMTX_OK;
1969        }
1970
1971        s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
1972        trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
1973        break;
1974    }
1975
1976    default:
1977        qemu_log_mask(LOG_GUEST_ERROR,
1978                      "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
1979        return MEMTX_OK;
1980    }
1981
1982    gic_update_virt(s);
1983    return MEMTX_OK;
1984}
1985
1986static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1987                                    unsigned size, MemTxAttrs attrs)
1988{
1989    GICState *s = (GICState *)opaque;
1990
1991    return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
1992}
1993
1994static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
1995                                     uint64_t value, unsigned size,
1996                                     MemTxAttrs attrs)
1997{
1998    GICState *s = (GICState *)opaque;
1999
2000    return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
2001}
2002
2003static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
2004                                    unsigned size, MemTxAttrs attrs)
2005{
2006    GICState **backref = (GICState **)opaque;
2007    GICState *s = *backref;
2008    int id = (backref - s->backref);
2009
2010    return gic_hyp_read(s, id, addr, data, attrs);
2011}
2012
2013static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
2014                                     uint64_t value, unsigned size,
2015                                     MemTxAttrs attrs)
2016{
2017    GICState **backref = (GICState **)opaque;
2018    GICState *s = *backref;
2019    int id = (backref - s->backref);
2020
2021    return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
2022
2023}
2024
2025static const MemoryRegionOps gic_ops[2] = {
2026    {
2027        .read_with_attrs = gic_dist_read,
2028        .write_with_attrs = gic_dist_write,
2029        .endianness = DEVICE_NATIVE_ENDIAN,
2030    },
2031    {
2032        .read_with_attrs = gic_thiscpu_read,
2033        .write_with_attrs = gic_thiscpu_write,
2034        .endianness = DEVICE_NATIVE_ENDIAN,
2035    }
2036};
2037
2038static const MemoryRegionOps gic_cpu_ops = {
2039    .read_with_attrs = gic_do_cpu_read,
2040    .write_with_attrs = gic_do_cpu_write,
2041    .endianness = DEVICE_NATIVE_ENDIAN,
2042};
2043
2044static const MemoryRegionOps gic_virt_ops[2] = {
2045    {
2046        .read_with_attrs = gic_thiscpu_hyp_read,
2047        .write_with_attrs = gic_thiscpu_hyp_write,
2048        .endianness = DEVICE_NATIVE_ENDIAN,
2049    },
2050    {
2051        .read_with_attrs = gic_thisvcpu_read,
2052        .write_with_attrs = gic_thisvcpu_write,
2053        .endianness = DEVICE_NATIVE_ENDIAN,
2054    }
2055};
2056
2057static const MemoryRegionOps gic_viface_ops = {
2058    .read_with_attrs = gic_do_hyp_read,
2059    .write_with_attrs = gic_do_hyp_write,
2060    .endianness = DEVICE_NATIVE_ENDIAN,
2061};
2062
2063static void arm_gic_realize(DeviceState *dev, Error **errp)
2064{
2065    /* Device instance realize function for the GIC sysbus device */
2066    int i;
2067    GICState *s = ARM_GIC(dev);
2068    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
2069    ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
2070    Error *local_err = NULL;
2071
2072    agc->parent_realize(dev, &local_err);
2073    if (local_err) {
2074        error_propagate(errp, local_err);
2075        return;
2076    }
2077
2078    if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
2079        error_setg(errp, "KVM with user space irqchip only works when the "
2080                         "host kernel supports KVM_CAP_ARM_USER_IRQ");
2081        return;
2082    }
2083
2084    if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS ||
2085       (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS :
2086        s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) {
2087        error_setg(errp, "num-priority-bits cannot be greater than %d"
2088                   " or less than %d", GIC_MAX_PRIORITY_BITS,
2089                   s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS :
2090                   GIC_MIN_PRIORITY_BITS);
2091        return;
2092    }
2093
2094    /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
2095     * enabled, virtualization extensions related interfaces (main virtual
2096     * interface (s->vifaceiomem[0]) and virtual CPU interface).
2097     */
2098    gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
2099
2100    /* Extra core-specific regions for the CPU interfaces. This is
2101     * necessary for "franken-GIC" implementations, for example on
2102     * Exynos 4.
2103     * NB that the memory region size of 0x100 applies for the 11MPCore
2104     * and also cores following the GIC v1 spec (ie A9).
2105     * GIC v2 defines a larger memory region (0x1000) so this will need
2106     * to be extended when we implement A15.
2107     */
2108    for (i = 0; i < s->num_cpu; i++) {
2109        s->backref[i] = s;
2110        memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
2111                              &s->backref[i], "gic_cpu", 0x100);
2112        sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2113    }
2114
2115    /* Extra core-specific regions for virtual interfaces. This is required by
2116     * the GICv2 specification.
2117     */
2118    if (s->virt_extn) {
2119        for (i = 0; i < s->num_cpu; i++) {
2120            memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2121                                  &gic_viface_ops, &s->backref[i],
2122                                  "gic_viface", 0x200);
2123            sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2124        }
2125    }
2126
2127    if (!s->c_iidr) {
2128        s->c_iidr |= s->revision << 16;
2129        s->c_iidr |= 0x43B;
2130    }
2131}
2132
2133static void arm_gic_fdt_auto_parent(FDTGenericIntc *obj, Error **errp)
2134{
2135    GICState *s = ARM_GIC(obj);
2136    CPUState *cs;
2137    int i = 0;
2138
2139    for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
2140        if (i >= s->num_cpu) {
2141            break;
2142        }
2143        qdev_connect_gpio_out_named(DEVICE(obj), "irq", i,
2144                                    qdev_get_gpio_in(DEVICE(cs), 0));
2145        i++;
2146    }
2147
2148    /* FIXME: Add some error checking */
2149}
2150
2151static const FDTGenericGPIOSet arm_gic_client_gpios [] = {
2152    {
2153        .names = &fdt_generic_gpio_name_set_interrupts,
2154        .gpios = (FDTGenericGPIOConnection []) {
2155            { .name = "irq",    .range = 8 },
2156            { .name = "virq",   .range = 8, .fdt_index = 8},
2157            { .name = "fiq",    .range = 8, .fdt_index = 16 },
2158            { .name = "vfiq",    .range = 8, .fdt_index = 24 },
2159            { .name = "maint",    .range = 4, .fdt_index = 32 },
2160            { },
2161        },
2162    },
2163    {
2164        .names = &fdt_generic_gpio_name_set_gpio,
2165        .gpios = (FDTGenericGPIOConnection []) {
2166            { .name = "pwr_cntrl", .range = 1, .fdt_index = 0 },
2167            { .name = "rst_cntrl", .range = 1, .fdt_index = 1 },
2168            { },
2169        },
2170    },
2171    { },
2172};
2173
2174static void arm_gic_class_init(ObjectClass *klass, void *data)
2175{
2176    DeviceClass *dc = DEVICE_CLASS(klass);
2177    ARMGICClass *agc = ARM_GIC_CLASS(klass);
2178    FDTGenericIntcClass *fgic = FDT_GENERIC_INTC_CLASS(klass);
2179    FDTGenericGPIOClass *fggc = FDT_GENERIC_GPIO_CLASS(klass);
2180
2181    agc->irq_handler = gic_set_irq;
2182    device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2183    fgic->auto_parent = arm_gic_fdt_auto_parent;
2184    fggc->client_gpios = arm_gic_client_gpios;
2185}
2186
2187static const TypeInfo arm_gic_info = {
2188    .name = TYPE_ARM_GIC,
2189    .parent = TYPE_ARM_GIC_COMMON,
2190    .instance_size = sizeof(GICState),
2191    .class_init = arm_gic_class_init,
2192    .class_size = sizeof(ARMGICClass),
2193};
2194
2195static void arm_gic_register_types(void)
2196{
2197    type_register_static(&arm_gic_info);
2198}
2199
2200type_init(arm_gic_register_types)
2201