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/sysbus.h" 23 #include "gic_internal.h" 24 #include "qapi/error.h" 25 #include "qom/cpu.h" 26 #include "qemu/log.h" 27 #include "trace.h" 28 #include "sysemu/kvm.h" 29 30 /* #define DEBUG_GIC */ 31 32 #ifdef DEBUG_GIC 33 #define DEBUG_GIC_GATE 1 34 #else 35 #define DEBUG_GIC_GATE 0 36 #endif 37 38 #define DPRINTF(fmt, ...) do { \ 39 if (DEBUG_GIC_GATE) { \ 40 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ 41 } \ 42 } while (0) 43 44 static const uint8_t gic_id_11mpcore[] = { 45 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 46 }; 47 48 static const uint8_t gic_id_gicv1[] = { 49 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 50 }; 51 52 static const uint8_t gic_id_gicv2[] = { 53 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1 54 }; 55 56 static inline int gic_get_current_cpu(GICState *s) 57 { 58 if (s->num_cpu > 1) { 59 return current_cpu->cpu_index; 60 } 61 return 0; 62 } 63 64 /* Return true if this GIC config has interrupt groups, which is 65 * true if we're a GICv2, or a GICv1 with the security extensions. 66 */ 67 static inline bool gic_has_groups(GICState *s) 68 { 69 return s->revision == 2 || s->security_extn; 70 } 71 72 /* TODO: Many places that call this routine could be optimized. */ 73 /* Update interrupt status after enabled or pending bits have been changed. */ 74 void gic_update(GICState *s) 75 { 76 int best_irq; 77 int best_prio; 78 int irq; 79 int irq_level, fiq_level; 80 int cpu; 81 int cm; 82 83 for (cpu = 0; cpu < s->num_cpu; cpu++) { 84 cm = 1 << cpu; 85 s->current_pending[cpu] = 1023; 86 if (!(s->ctlr & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) 87 || !(s->cpu_ctlr[cpu] & (GICC_CTLR_EN_GRP0 | GICC_CTLR_EN_GRP1))) { 88 qemu_irq_lower(s->parent_irq[cpu]); 89 qemu_irq_lower(s->parent_fiq[cpu]); 90 continue; 91 } 92 best_prio = 0x100; 93 best_irq = 1023; 94 for (irq = 0; irq < s->num_irq; irq++) { 95 if (GIC_DIST_TEST_ENABLED(irq, cm) && 96 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 trace_gic_update_bestirq(cpu, best_irq, best_prio, 108 s->priority_mask[cpu], s->running_priority[cpu]); 109 } 110 111 irq_level = fiq_level = 0; 112 113 if (best_prio < s->priority_mask[cpu]) { 114 s->current_pending[cpu] = best_irq; 115 if (best_prio < s->running_priority[cpu]) { 116 int group = GIC_DIST_TEST_GROUP(best_irq, cm); 117 118 if (extract32(s->ctlr, group, 1) && 119 extract32(s->cpu_ctlr[cpu], group, 1)) { 120 if (group == 0 && s->cpu_ctlr[cpu] & GICC_CTLR_FIQ_EN) { 121 DPRINTF("Raised pending FIQ %d (cpu %d)\n", 122 best_irq, cpu); 123 fiq_level = 1; 124 trace_gic_update_set_irq(cpu, "fiq", fiq_level); 125 } else { 126 DPRINTF("Raised pending IRQ %d (cpu %d)\n", 127 best_irq, cpu); 128 irq_level = 1; 129 trace_gic_update_set_irq(cpu, "irq", irq_level); 130 } 131 } 132 } 133 } 134 135 qemu_set_irq(s->parent_irq[cpu], irq_level); 136 qemu_set_irq(s->parent_fiq[cpu], fiq_level); 137 } 138 } 139 140 void gic_set_pending_private(GICState *s, int cpu, int irq) 141 { 142 int cm = 1 << cpu; 143 144 if (gic_test_pending(s, irq, cm)) { 145 return; 146 } 147 148 DPRINTF("Set %d pending cpu %d\n", irq, cpu); 149 GIC_DIST_SET_PENDING(irq, cm); 150 gic_update(s); 151 } 152 153 static void gic_set_irq_11mpcore(GICState *s, int irq, int level, 154 int cm, int target) 155 { 156 if (level) { 157 GIC_DIST_SET_LEVEL(irq, cm); 158 if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) { 159 DPRINTF("Set %d pending mask %x\n", irq, target); 160 GIC_DIST_SET_PENDING(irq, target); 161 } 162 } else { 163 GIC_DIST_CLEAR_LEVEL(irq, cm); 164 } 165 } 166 167 static void gic_set_irq_generic(GICState *s, int irq, int level, 168 int cm, int target) 169 { 170 if (level) { 171 GIC_DIST_SET_LEVEL(irq, cm); 172 DPRINTF("Set %d pending mask %x\n", irq, target); 173 if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) { 174 GIC_DIST_SET_PENDING(irq, target); 175 } 176 } else { 177 GIC_DIST_CLEAR_LEVEL(irq, cm); 178 } 179 } 180 181 /* Process a change in an external IRQ input. */ 182 static void gic_set_irq(void *opaque, int irq, int level) 183 { 184 /* Meaning of the 'irq' parameter: 185 * [0..N-1] : external interrupts 186 * [N..N+31] : PPI (internal) interrupts for CPU 0 187 * [N+32..N+63] : PPI (internal interrupts for CPU 1 188 * ... 189 */ 190 GICState *s = (GICState *)opaque; 191 int cm, target; 192 if (irq < (s->num_irq - GIC_INTERNAL)) { 193 /* The first external input line is internal interrupt 32. */ 194 cm = ALL_CPU_MASK; 195 irq += GIC_INTERNAL; 196 target = GIC_DIST_TARGET(irq); 197 } else { 198 int cpu; 199 irq -= (s->num_irq - GIC_INTERNAL); 200 cpu = irq / GIC_INTERNAL; 201 irq %= GIC_INTERNAL; 202 cm = 1 << cpu; 203 target = cm; 204 } 205 206 assert(irq >= GIC_NR_SGIS); 207 208 if (level == GIC_DIST_TEST_LEVEL(irq, cm)) { 209 return; 210 } 211 212 if (s->revision == REV_11MPCORE) { 213 gic_set_irq_11mpcore(s, irq, level, cm, target); 214 } else { 215 gic_set_irq_generic(s, irq, level, cm, target); 216 } 217 trace_gic_set_irq(irq, level, cm, target); 218 219 gic_update(s); 220 } 221 222 static uint16_t gic_get_current_pending_irq(GICState *s, int cpu, 223 MemTxAttrs attrs) 224 { 225 uint16_t pending_irq = s->current_pending[cpu]; 226 227 if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) { 228 int group = GIC_DIST_TEST_GROUP(pending_irq, (1 << cpu)); 229 /* On a GIC without the security extensions, reading this register 230 * behaves in the same way as a secure access to a GIC with them. 231 */ 232 bool secure = !s->security_extn || attrs.secure; 233 234 if (group == 0 && !secure) { 235 /* Group0 interrupts hidden from Non-secure access */ 236 return 1023; 237 } 238 if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) { 239 /* Group1 interrupts only seen by Secure access if 240 * AckCtl bit set. 241 */ 242 return 1022; 243 } 244 } 245 return pending_irq; 246 } 247 248 static int gic_get_group_priority(GICState *s, int cpu, int irq) 249 { 250 /* Return the group priority of the specified interrupt 251 * (which is the top bits of its priority, with the number 252 * of bits masked determined by the applicable binary point register). 253 */ 254 int bpr; 255 uint32_t mask; 256 257 if (gic_has_groups(s) && 258 !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) && 259 GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 260 bpr = s->abpr[cpu] - 1; 261 assert(bpr >= 0); 262 } else { 263 bpr = s->bpr[cpu]; 264 } 265 266 /* a BPR of 0 means the group priority bits are [7:1]; 267 * a BPR of 1 means they are [7:2], and so on down to 268 * a BPR of 7 meaning no group priority bits at all. 269 */ 270 mask = ~0U << ((bpr & 7) + 1); 271 272 return GIC_DIST_GET_PRIORITY(irq, cpu) & mask; 273 } 274 275 static void gic_activate_irq(GICState *s, int cpu, int irq) 276 { 277 /* Set the appropriate Active Priority Register bit for this IRQ, 278 * and update the running priority. 279 */ 280 int prio = gic_get_group_priority(s, cpu, irq); 281 int preemption_level = prio >> (GIC_MIN_BPR + 1); 282 int regno = preemption_level / 32; 283 int bitno = preemption_level % 32; 284 285 if (gic_has_groups(s) && GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 286 s->nsapr[regno][cpu] |= (1 << bitno); 287 } else { 288 s->apr[regno][cpu] |= (1 << bitno); 289 } 290 291 s->running_priority[cpu] = prio; 292 GIC_DIST_SET_ACTIVE(irq, 1 << cpu); 293 } 294 295 static int gic_get_prio_from_apr_bits(GICState *s, int cpu) 296 { 297 /* Recalculate the current running priority for this CPU based 298 * on the set bits in the Active Priority Registers. 299 */ 300 int i; 301 for (i = 0; i < GIC_NR_APRS; i++) { 302 uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu]; 303 if (!apr) { 304 continue; 305 } 306 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 307 } 308 return 0x100; 309 } 310 311 static void gic_drop_prio(GICState *s, int cpu, int group) 312 { 313 /* Drop the priority of the currently active interrupt in the 314 * specified group. 315 * 316 * Note that we can guarantee (because of the requirement to nest 317 * GICC_IAR reads [which activate an interrupt and raise priority] 318 * with GICC_EOIR writes [which drop the priority for the interrupt]) 319 * that the interrupt we're being called for is the highest priority 320 * active interrupt, meaning that it has the lowest set bit in the 321 * APR registers. 322 * 323 * If the guest does not honour the ordering constraints then the 324 * behaviour of the GIC is UNPREDICTABLE, which for us means that 325 * the values of the APR registers might become incorrect and the 326 * running priority will be wrong, so interrupts that should preempt 327 * might not do so, and interrupts that should not preempt might do so. 328 */ 329 int i; 330 331 for (i = 0; i < GIC_NR_APRS; i++) { 332 uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu]; 333 if (!*papr) { 334 continue; 335 } 336 /* Clear lowest set bit */ 337 *papr &= *papr - 1; 338 break; 339 } 340 341 s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu); 342 } 343 344 uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs) 345 { 346 int ret, irq, src; 347 int cm = 1 << cpu; 348 349 /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately 350 * for the case where this GIC supports grouping and the pending interrupt 351 * is in the wrong group. 352 */ 353 irq = gic_get_current_pending_irq(s, cpu, attrs); 354 trace_gic_acknowledge_irq(cpu, irq); 355 356 if (irq >= GIC_MAXIRQ) { 357 DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq); 358 return irq; 359 } 360 361 if (GIC_DIST_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) { 362 DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq); 363 return 1023; 364 } 365 366 if (s->revision == REV_11MPCORE) { 367 /* Clear pending flags for both level and edge triggered interrupts. 368 * Level triggered IRQs will be reasserted once they become inactive. 369 */ 370 GIC_DIST_CLEAR_PENDING(irq, GIC_DIST_TEST_MODEL(irq) ? ALL_CPU_MASK 371 : cm); 372 ret = irq; 373 } else { 374 if (irq < GIC_NR_SGIS) { 375 /* Lookup the source CPU for the SGI and clear this in the 376 * sgi_pending map. Return the src and clear the overall pending 377 * state on this CPU if the SGI is not pending from any CPUs. 378 */ 379 assert(s->sgi_pending[irq][cpu] != 0); 380 src = ctz32(s->sgi_pending[irq][cpu]); 381 s->sgi_pending[irq][cpu] &= ~(1 << src); 382 if (s->sgi_pending[irq][cpu] == 0) { 383 GIC_DIST_CLEAR_PENDING(irq, 384 GIC_DIST_TEST_MODEL(irq) ? ALL_CPU_MASK 385 : cm); 386 } 387 ret = irq | ((src & 0x7) << 10); 388 } else { 389 /* Clear pending state for both level and edge triggered 390 * interrupts. (level triggered interrupts with an active line 391 * remain pending, see gic_test_pending) 392 */ 393 GIC_DIST_CLEAR_PENDING(irq, GIC_DIST_TEST_MODEL(irq) ? ALL_CPU_MASK 394 : cm); 395 ret = irq; 396 } 397 } 398 399 gic_activate_irq(s, cpu, irq); 400 gic_update(s); 401 DPRINTF("ACK %d\n", irq); 402 return ret; 403 } 404 405 void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val, 406 MemTxAttrs attrs) 407 { 408 if (s->security_extn && !attrs.secure) { 409 if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 410 return; /* Ignore Non-secure access of Group0 IRQ */ 411 } 412 val = 0x80 | (val >> 1); /* Non-secure view */ 413 } 414 415 if (irq < GIC_INTERNAL) { 416 s->priority1[irq][cpu] = val; 417 } else { 418 s->priority2[(irq) - GIC_INTERNAL] = val; 419 } 420 } 421 422 static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq, 423 MemTxAttrs attrs) 424 { 425 uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu); 426 427 if (s->security_extn && !attrs.secure) { 428 if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) { 429 return 0; /* Non-secure access cannot read priority of Group0 IRQ */ 430 } 431 prio = (prio << 1) & 0xff; /* Non-secure view */ 432 } 433 return prio; 434 } 435 436 static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask, 437 MemTxAttrs attrs) 438 { 439 if (s->security_extn && !attrs.secure) { 440 if (s->priority_mask[cpu] & 0x80) { 441 /* Priority Mask in upper half */ 442 pmask = 0x80 | (pmask >> 1); 443 } else { 444 /* Non-secure write ignored if priority mask is in lower half */ 445 return; 446 } 447 } 448 s->priority_mask[cpu] = pmask; 449 } 450 451 static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs) 452 { 453 uint32_t pmask = s->priority_mask[cpu]; 454 455 if (s->security_extn && !attrs.secure) { 456 if (pmask & 0x80) { 457 /* Priority Mask in upper half, return Non-secure view */ 458 pmask = (pmask << 1) & 0xff; 459 } else { 460 /* Priority Mask in lower half, RAZ */ 461 pmask = 0; 462 } 463 } 464 return pmask; 465 } 466 467 static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs) 468 { 469 uint32_t ret = s->cpu_ctlr[cpu]; 470 471 if (s->security_extn && !attrs.secure) { 472 /* Construct the NS banked view of GICC_CTLR from the correct 473 * bits of the S banked view. We don't need to move the bypass 474 * control bits because we don't implement that (IMPDEF) part 475 * of the GIC architecture. 476 */ 477 ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1; 478 } 479 return ret; 480 } 481 482 static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value, 483 MemTxAttrs attrs) 484 { 485 uint32_t mask; 486 487 if (s->security_extn && !attrs.secure) { 488 /* The NS view can only write certain bits in the register; 489 * the rest are unchanged 490 */ 491 mask = GICC_CTLR_EN_GRP1; 492 if (s->revision == 2) { 493 mask |= GICC_CTLR_EOIMODE_NS; 494 } 495 s->cpu_ctlr[cpu] &= ~mask; 496 s->cpu_ctlr[cpu] |= (value << 1) & mask; 497 } else { 498 if (s->revision == 2) { 499 mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK; 500 } else { 501 mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK; 502 } 503 s->cpu_ctlr[cpu] = value & mask; 504 } 505 DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, " 506 "Group1 Interrupts %sabled\n", cpu, 507 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis", 508 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis"); 509 } 510 511 static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs) 512 { 513 if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) { 514 /* Idle priority */ 515 return 0xff; 516 } 517 518 if (s->security_extn && !attrs.secure) { 519 if (s->running_priority[cpu] & 0x80) { 520 /* Running priority in upper half of range: return the Non-secure 521 * view of the priority. 522 */ 523 return s->running_priority[cpu] << 1; 524 } else { 525 /* Running priority in lower half of range: RAZ */ 526 return 0; 527 } 528 } else { 529 return s->running_priority[cpu]; 530 } 531 } 532 533 /* Return true if we should split priority drop and interrupt deactivation, 534 * ie whether the relevant EOIMode bit is set. 535 */ 536 static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs) 537 { 538 if (s->revision != 2) { 539 /* Before GICv2 prio-drop and deactivate are not separable */ 540 return false; 541 } 542 if (s->security_extn && !attrs.secure) { 543 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS; 544 } 545 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE; 546 } 547 548 static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 549 { 550 int cm = 1 << cpu; 551 int group; 552 553 if (irq >= s->num_irq) { 554 /* 555 * This handles two cases: 556 * 1. If software writes the ID of a spurious interrupt [ie 1023] 557 * to the GICC_DIR, the GIC ignores that write. 558 * 2. If software writes the number of a non-existent interrupt 559 * this must be a subcase of "value written is not an active interrupt" 560 * and so this is UNPREDICTABLE. We choose to ignore it. 561 */ 562 return; 563 } 564 565 group = gic_has_groups(s) && GIC_DIST_TEST_GROUP(irq, cm); 566 567 if (!gic_eoi_split(s, cpu, attrs)) { 568 /* This is UNPREDICTABLE; we choose to ignore it */ 569 qemu_log_mask(LOG_GUEST_ERROR, 570 "gic_deactivate_irq: GICC_DIR write when EOIMode clear"); 571 return; 572 } 573 574 if (s->security_extn && !attrs.secure && !group) { 575 DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq); 576 return; 577 } 578 579 GIC_DIST_CLEAR_ACTIVE(irq, cm); 580 } 581 582 void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs) 583 { 584 int cm = 1 << cpu; 585 int group; 586 587 DPRINTF("EOI %d\n", irq); 588 if (irq >= s->num_irq) { 589 /* This handles two cases: 590 * 1. If software writes the ID of a spurious interrupt [ie 1023] 591 * to the GICC_EOIR, the GIC ignores that write. 592 * 2. If software writes the number of a non-existent interrupt 593 * this must be a subcase of "value written does not match the last 594 * valid interrupt value read from the Interrupt Acknowledge 595 * register" and so this is UNPREDICTABLE. We choose to ignore it. 596 */ 597 return; 598 } 599 if (s->running_priority[cpu] == 0x100) { 600 return; /* No active IRQ. */ 601 } 602 603 if (s->revision == REV_11MPCORE) { 604 /* Mark level triggered interrupts as pending if they are still 605 raised. */ 606 if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm) 607 && GIC_DIST_TEST_LEVEL(irq, cm) 608 && (GIC_DIST_TARGET(irq) & cm) != 0) { 609 DPRINTF("Set %d pending mask %x\n", irq, cm); 610 GIC_DIST_SET_PENDING(irq, cm); 611 } 612 } 613 614 group = gic_has_groups(s) && GIC_DIST_TEST_GROUP(irq, cm); 615 616 if (s->security_extn && !attrs.secure && !group) { 617 DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq); 618 return; 619 } 620 621 /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1 622 * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1, 623 * i.e. go ahead and complete the irq anyway. 624 */ 625 626 gic_drop_prio(s, cpu, group); 627 628 /* In GICv2 the guest can choose to split priority-drop and deactivate */ 629 if (!gic_eoi_split(s, cpu, attrs)) { 630 GIC_DIST_CLEAR_ACTIVE(irq, cm); 631 } 632 gic_update(s); 633 } 634 635 static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs) 636 { 637 GICState *s = (GICState *)opaque; 638 uint32_t res; 639 int irq; 640 int i; 641 int cpu; 642 int cm; 643 int mask; 644 645 cpu = gic_get_current_cpu(s); 646 cm = 1 << cpu; 647 if (offset < 0x100) { 648 if (offset == 0) { /* GICD_CTLR */ 649 if (s->security_extn && !attrs.secure) { 650 /* The NS bank of this register is just an alias of the 651 * EnableGrp1 bit in the S bank version. 652 */ 653 return extract32(s->ctlr, 1, 1); 654 } else { 655 return s->ctlr; 656 } 657 } 658 if (offset == 4) 659 /* Interrupt Controller Type Register */ 660 return ((s->num_irq / 32) - 1) 661 | ((s->num_cpu - 1) << 5) 662 | (s->security_extn << 10); 663 if (offset < 0x08) 664 return 0; 665 if (offset >= 0x80) { 666 /* Interrupt Group Registers: these RAZ/WI if this is an NS 667 * access to a GIC with the security extensions, or if the GIC 668 * doesn't have groups at all. 669 */ 670 res = 0; 671 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 672 /* Every byte offset holds 8 group status bits */ 673 irq = (offset - 0x080) * 8 + GIC_BASE_IRQ; 674 if (irq >= s->num_irq) { 675 goto bad_reg; 676 } 677 for (i = 0; i < 8; i++) { 678 if (GIC_DIST_TEST_GROUP(irq + i, cm)) { 679 res |= (1 << i); 680 } 681 } 682 } 683 return res; 684 } 685 goto bad_reg; 686 } else if (offset < 0x200) { 687 /* Interrupt Set/Clear Enable. */ 688 if (offset < 0x180) 689 irq = (offset - 0x100) * 8; 690 else 691 irq = (offset - 0x180) * 8; 692 irq += GIC_BASE_IRQ; 693 if (irq >= s->num_irq) 694 goto bad_reg; 695 res = 0; 696 for (i = 0; i < 8; i++) { 697 if (s->security_extn && !attrs.secure && 698 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 699 continue; /* Ignore Non-secure access of Group0 IRQ */ 700 } 701 702 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 703 res |= (1 << i); 704 } 705 } 706 } else if (offset < 0x300) { 707 /* Interrupt Set/Clear Pending. */ 708 if (offset < 0x280) 709 irq = (offset - 0x200) * 8; 710 else 711 irq = (offset - 0x280) * 8; 712 irq += GIC_BASE_IRQ; 713 if (irq >= s->num_irq) 714 goto bad_reg; 715 res = 0; 716 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 717 for (i = 0; i < 8; i++) { 718 if (s->security_extn && !attrs.secure && 719 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 720 continue; /* Ignore Non-secure access of Group0 IRQ */ 721 } 722 723 if (gic_test_pending(s, irq + i, mask)) { 724 res |= (1 << i); 725 } 726 } 727 } else if (offset < 0x400) { 728 /* Interrupt Set/Clear Active. */ 729 if (offset < 0x380) { 730 irq = (offset - 0x300) * 8; 731 } else if (s->revision == 2) { 732 irq = (offset - 0x380) * 8; 733 } else { 734 goto bad_reg; 735 } 736 737 irq += GIC_BASE_IRQ; 738 if (irq >= s->num_irq) 739 goto bad_reg; 740 res = 0; 741 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK; 742 for (i = 0; i < 8; i++) { 743 if (s->security_extn && !attrs.secure && 744 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 745 continue; /* Ignore Non-secure access of Group0 IRQ */ 746 } 747 748 if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) { 749 res |= (1 << i); 750 } 751 } 752 } else if (offset < 0x800) { 753 /* Interrupt Priority. */ 754 irq = (offset - 0x400) + GIC_BASE_IRQ; 755 if (irq >= s->num_irq) 756 goto bad_reg; 757 res = gic_dist_get_priority(s, cpu, irq, attrs); 758 } else if (offset < 0xc00) { 759 /* Interrupt CPU Target. */ 760 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) { 761 /* For uniprocessor GICs these RAZ/WI */ 762 res = 0; 763 } else { 764 irq = (offset - 0x800) + GIC_BASE_IRQ; 765 if (irq >= s->num_irq) { 766 goto bad_reg; 767 } 768 if (irq < 29 && s->revision == REV_11MPCORE) { 769 res = 0; 770 } else if (irq < GIC_INTERNAL) { 771 res = cm; 772 } else { 773 res = GIC_DIST_TARGET(irq); 774 } 775 } 776 } else if (offset < 0xf00) { 777 /* Interrupt Configuration. */ 778 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 779 if (irq >= s->num_irq) 780 goto bad_reg; 781 res = 0; 782 for (i = 0; i < 4; i++) { 783 if (s->security_extn && !attrs.secure && 784 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 785 continue; /* Ignore Non-secure access of Group0 IRQ */ 786 } 787 788 if (GIC_DIST_TEST_MODEL(irq + i)) { 789 res |= (1 << (i * 2)); 790 } 791 if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 792 res |= (2 << (i * 2)); 793 } 794 } 795 } else if (offset < 0xf10) { 796 goto bad_reg; 797 } else if (offset < 0xf30) { 798 if (s->revision == REV_11MPCORE) { 799 goto bad_reg; 800 } 801 802 if (offset < 0xf20) { 803 /* GICD_CPENDSGIRn */ 804 irq = (offset - 0xf10); 805 } else { 806 irq = (offset - 0xf20); 807 /* GICD_SPENDSGIRn */ 808 } 809 810 if (s->security_extn && !attrs.secure && 811 !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 812 res = 0; /* Ignore Non-secure access of Group0 IRQ */ 813 } else { 814 res = s->sgi_pending[irq][cpu]; 815 } 816 } else if (offset < 0xfd0) { 817 goto bad_reg; 818 } else if (offset < 0x1000) { 819 if (offset & 3) { 820 res = 0; 821 } else { 822 switch (s->revision) { 823 case REV_11MPCORE: 824 res = gic_id_11mpcore[(offset - 0xfd0) >> 2]; 825 break; 826 case 1: 827 res = gic_id_gicv1[(offset - 0xfd0) >> 2]; 828 break; 829 case 2: 830 res = gic_id_gicv2[(offset - 0xfd0) >> 2]; 831 break; 832 default: 833 res = 0; 834 } 835 } 836 } else { 837 g_assert_not_reached(); 838 } 839 return res; 840 bad_reg: 841 qemu_log_mask(LOG_GUEST_ERROR, 842 "gic_dist_readb: Bad offset %x\n", (int)offset); 843 return 0; 844 } 845 846 static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data, 847 unsigned size, MemTxAttrs attrs) 848 { 849 switch (size) { 850 case 1: 851 *data = gic_dist_readb(opaque, offset, attrs); 852 return MEMTX_OK; 853 case 2: 854 *data = gic_dist_readb(opaque, offset, attrs); 855 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 856 return MEMTX_OK; 857 case 4: 858 *data = gic_dist_readb(opaque, offset, attrs); 859 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8; 860 *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16; 861 *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24; 862 return MEMTX_OK; 863 default: 864 return MEMTX_ERROR; 865 } 866 } 867 868 static void gic_dist_writeb(void *opaque, hwaddr offset, 869 uint32_t value, MemTxAttrs attrs) 870 { 871 GICState *s = (GICState *)opaque; 872 int irq; 873 int i; 874 int cpu; 875 876 cpu = gic_get_current_cpu(s); 877 if (offset < 0x100) { 878 if (offset == 0) { 879 if (s->security_extn && !attrs.secure) { 880 /* NS version is just an alias of the S version's bit 1 */ 881 s->ctlr = deposit32(s->ctlr, 1, 1, value); 882 } else if (gic_has_groups(s)) { 883 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1); 884 } else { 885 s->ctlr = value & GICD_CTLR_EN_GRP0; 886 } 887 DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n", 888 s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis", 889 s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis"); 890 } else if (offset < 4) { 891 /* ignored. */ 892 } else if (offset >= 0x80) { 893 /* Interrupt Group Registers: RAZ/WI for NS access to secure 894 * GIC, or for GICs without groups. 895 */ 896 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) { 897 /* Every byte offset holds 8 group status bits */ 898 irq = (offset - 0x80) * 8 + GIC_BASE_IRQ; 899 if (irq >= s->num_irq) { 900 goto bad_reg; 901 } 902 for (i = 0; i < 8; i++) { 903 /* Group bits are banked for private interrupts */ 904 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 905 if (value & (1 << i)) { 906 /* Group1 (Non-secure) */ 907 GIC_DIST_SET_GROUP(irq + i, cm); 908 } else { 909 /* Group0 (Secure) */ 910 GIC_DIST_CLEAR_GROUP(irq + i, cm); 911 } 912 } 913 } 914 } else { 915 goto bad_reg; 916 } 917 } else if (offset < 0x180) { 918 /* Interrupt Set Enable. */ 919 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ; 920 if (irq >= s->num_irq) 921 goto bad_reg; 922 if (irq < GIC_NR_SGIS) { 923 value = 0xff; 924 } 925 926 for (i = 0; i < 8; i++) { 927 if (value & (1 << i)) { 928 int mask = 929 (irq < GIC_INTERNAL) ? (1 << cpu) 930 : GIC_DIST_TARGET(irq + i); 931 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 932 933 if (s->security_extn && !attrs.secure && 934 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 935 continue; /* Ignore Non-secure access of Group0 IRQ */ 936 } 937 938 if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) { 939 DPRINTF("Enabled IRQ %d\n", irq + i); 940 trace_gic_enable_irq(irq + i); 941 } 942 GIC_DIST_SET_ENABLED(irq + i, cm); 943 /* If a raised level triggered IRQ enabled then mark 944 is as pending. */ 945 if (GIC_DIST_TEST_LEVEL(irq + i, mask) 946 && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) { 947 DPRINTF("Set %d pending mask %x\n", irq + i, mask); 948 GIC_DIST_SET_PENDING(irq + i, mask); 949 } 950 } 951 } 952 } else if (offset < 0x200) { 953 /* Interrupt Clear Enable. */ 954 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ; 955 if (irq >= s->num_irq) 956 goto bad_reg; 957 if (irq < GIC_NR_SGIS) { 958 value = 0; 959 } 960 961 for (i = 0; i < 8; i++) { 962 if (value & (1 << i)) { 963 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 964 965 if (s->security_extn && !attrs.secure && 966 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 967 continue; /* Ignore Non-secure access of Group0 IRQ */ 968 } 969 970 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) { 971 DPRINTF("Disabled IRQ %d\n", irq + i); 972 trace_gic_disable_irq(irq + i); 973 } 974 GIC_DIST_CLEAR_ENABLED(irq + i, cm); 975 } 976 } 977 } else if (offset < 0x280) { 978 /* Interrupt Set Pending. */ 979 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ; 980 if (irq >= s->num_irq) 981 goto bad_reg; 982 if (irq < GIC_NR_SGIS) { 983 value = 0; 984 } 985 986 for (i = 0; i < 8; i++) { 987 if (value & (1 << i)) { 988 if (s->security_extn && !attrs.secure && 989 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 990 continue; /* Ignore Non-secure access of Group0 IRQ */ 991 } 992 993 GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i)); 994 } 995 } 996 } else if (offset < 0x300) { 997 /* Interrupt Clear Pending. */ 998 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ; 999 if (irq >= s->num_irq) 1000 goto bad_reg; 1001 if (irq < GIC_NR_SGIS) { 1002 value = 0; 1003 } 1004 1005 for (i = 0; i < 8; i++) { 1006 if (s->security_extn && !attrs.secure && 1007 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1008 continue; /* Ignore Non-secure access of Group0 IRQ */ 1009 } 1010 1011 /* ??? This currently clears the pending bit for all CPUs, even 1012 for per-CPU interrupts. It's unclear whether this is the 1013 corect behavior. */ 1014 if (value & (1 << i)) { 1015 GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK); 1016 } 1017 } 1018 } else if (offset < 0x380) { 1019 /* Interrupt Set Active. */ 1020 if (s->revision != 2) { 1021 goto bad_reg; 1022 } 1023 1024 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ; 1025 if (irq >= s->num_irq) { 1026 goto bad_reg; 1027 } 1028 1029 /* This register is banked per-cpu for PPIs */ 1030 int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 1031 1032 for (i = 0; i < 8; i++) { 1033 if (s->security_extn && !attrs.secure && 1034 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1035 continue; /* Ignore Non-secure access of Group0 IRQ */ 1036 } 1037 1038 if (value & (1 << i)) { 1039 GIC_DIST_SET_ACTIVE(irq + i, cm); 1040 } 1041 } 1042 } else if (offset < 0x400) { 1043 /* Interrupt Clear Active. */ 1044 if (s->revision != 2) { 1045 goto bad_reg; 1046 } 1047 1048 irq = (offset - 0x380) * 8 + GIC_BASE_IRQ; 1049 if (irq >= s->num_irq) { 1050 goto bad_reg; 1051 } 1052 1053 /* This register is banked per-cpu for PPIs */ 1054 int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK; 1055 1056 for (i = 0; i < 8; i++) { 1057 if (s->security_extn && !attrs.secure && 1058 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1059 continue; /* Ignore Non-secure access of Group0 IRQ */ 1060 } 1061 1062 if (value & (1 << i)) { 1063 GIC_DIST_CLEAR_ACTIVE(irq + i, cm); 1064 } 1065 } 1066 } else if (offset < 0x800) { 1067 /* Interrupt Priority. */ 1068 irq = (offset - 0x400) + GIC_BASE_IRQ; 1069 if (irq >= s->num_irq) 1070 goto bad_reg; 1071 gic_dist_set_priority(s, cpu, irq, value, attrs); 1072 } else if (offset < 0xc00) { 1073 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the 1074 * annoying exception of the 11MPCore's GIC. 1075 */ 1076 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) { 1077 irq = (offset - 0x800) + GIC_BASE_IRQ; 1078 if (irq >= s->num_irq) { 1079 goto bad_reg; 1080 } 1081 if (irq < 29 && s->revision == REV_11MPCORE) { 1082 value = 0; 1083 } else if (irq < GIC_INTERNAL) { 1084 value = ALL_CPU_MASK; 1085 } 1086 s->irq_target[irq] = value & ALL_CPU_MASK; 1087 } 1088 } else if (offset < 0xf00) { 1089 /* Interrupt Configuration. */ 1090 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ; 1091 if (irq >= s->num_irq) 1092 goto bad_reg; 1093 if (irq < GIC_NR_SGIS) 1094 value |= 0xaa; 1095 for (i = 0; i < 4; i++) { 1096 if (s->security_extn && !attrs.secure && 1097 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) { 1098 continue; /* Ignore Non-secure access of Group0 IRQ */ 1099 } 1100 1101 if (s->revision == REV_11MPCORE) { 1102 if (value & (1 << (i * 2))) { 1103 GIC_DIST_SET_MODEL(irq + i); 1104 } else { 1105 GIC_DIST_CLEAR_MODEL(irq + i); 1106 } 1107 } 1108 if (value & (2 << (i * 2))) { 1109 GIC_DIST_SET_EDGE_TRIGGER(irq + i); 1110 } else { 1111 GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i); 1112 } 1113 } 1114 } else if (offset < 0xf10) { 1115 /* 0xf00 is only handled for 32-bit writes. */ 1116 goto bad_reg; 1117 } else if (offset < 0xf20) { 1118 /* GICD_CPENDSGIRn */ 1119 if (s->revision == REV_11MPCORE) { 1120 goto bad_reg; 1121 } 1122 irq = (offset - 0xf10); 1123 1124 if (!s->security_extn || attrs.secure || 1125 GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 1126 s->sgi_pending[irq][cpu] &= ~value; 1127 if (s->sgi_pending[irq][cpu] == 0) { 1128 GIC_DIST_CLEAR_PENDING(irq, 1 << cpu); 1129 } 1130 } 1131 } else if (offset < 0xf30) { 1132 /* GICD_SPENDSGIRn */ 1133 if (s->revision == REV_11MPCORE) { 1134 goto bad_reg; 1135 } 1136 irq = (offset - 0xf20); 1137 1138 if (!s->security_extn || attrs.secure || 1139 GIC_DIST_TEST_GROUP(irq, 1 << cpu)) { 1140 GIC_DIST_SET_PENDING(irq, 1 << cpu); 1141 s->sgi_pending[irq][cpu] |= value; 1142 } 1143 } else { 1144 goto bad_reg; 1145 } 1146 gic_update(s); 1147 return; 1148 bad_reg: 1149 qemu_log_mask(LOG_GUEST_ERROR, 1150 "gic_dist_writeb: Bad offset %x\n", (int)offset); 1151 } 1152 1153 static void gic_dist_writew(void *opaque, hwaddr offset, 1154 uint32_t value, MemTxAttrs attrs) 1155 { 1156 gic_dist_writeb(opaque, offset, value & 0xff, attrs); 1157 gic_dist_writeb(opaque, offset + 1, value >> 8, attrs); 1158 } 1159 1160 static void gic_dist_writel(void *opaque, hwaddr offset, 1161 uint32_t value, MemTxAttrs attrs) 1162 { 1163 GICState *s = (GICState *)opaque; 1164 if (offset == 0xf00) { 1165 int cpu; 1166 int irq; 1167 int mask; 1168 int target_cpu; 1169 1170 cpu = gic_get_current_cpu(s); 1171 irq = value & 0x3ff; 1172 switch ((value >> 24) & 3) { 1173 case 0: 1174 mask = (value >> 16) & ALL_CPU_MASK; 1175 break; 1176 case 1: 1177 mask = ALL_CPU_MASK ^ (1 << cpu); 1178 break; 1179 case 2: 1180 mask = 1 << cpu; 1181 break; 1182 default: 1183 DPRINTF("Bad Soft Int target filter\n"); 1184 mask = ALL_CPU_MASK; 1185 break; 1186 } 1187 GIC_DIST_SET_PENDING(irq, mask); 1188 target_cpu = ctz32(mask); 1189 while (target_cpu < GIC_NCPU) { 1190 s->sgi_pending[irq][target_cpu] |= (1 << cpu); 1191 mask &= ~(1 << target_cpu); 1192 target_cpu = ctz32(mask); 1193 } 1194 gic_update(s); 1195 return; 1196 } 1197 gic_dist_writew(opaque, offset, value & 0xffff, attrs); 1198 gic_dist_writew(opaque, offset + 2, value >> 16, attrs); 1199 } 1200 1201 static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data, 1202 unsigned size, MemTxAttrs attrs) 1203 { 1204 switch (size) { 1205 case 1: 1206 gic_dist_writeb(opaque, offset, data, attrs); 1207 return MEMTX_OK; 1208 case 2: 1209 gic_dist_writew(opaque, offset, data, attrs); 1210 return MEMTX_OK; 1211 case 4: 1212 gic_dist_writel(opaque, offset, data, attrs); 1213 return MEMTX_OK; 1214 default: 1215 return MEMTX_ERROR; 1216 } 1217 } 1218 1219 static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno) 1220 { 1221 /* Return the Nonsecure view of GICC_APR<regno>. This is the 1222 * second half of GICC_NSAPR. 1223 */ 1224 switch (GIC_MIN_BPR) { 1225 case 0: 1226 if (regno < 2) { 1227 return s->nsapr[regno + 2][cpu]; 1228 } 1229 break; 1230 case 1: 1231 if (regno == 0) { 1232 return s->nsapr[regno + 1][cpu]; 1233 } 1234 break; 1235 case 2: 1236 if (regno == 0) { 1237 return extract32(s->nsapr[0][cpu], 16, 16); 1238 } 1239 break; 1240 case 3: 1241 if (regno == 0) { 1242 return extract32(s->nsapr[0][cpu], 8, 8); 1243 } 1244 break; 1245 default: 1246 g_assert_not_reached(); 1247 } 1248 return 0; 1249 } 1250 1251 static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno, 1252 uint32_t value) 1253 { 1254 /* Write the Nonsecure view of GICC_APR<regno>. */ 1255 switch (GIC_MIN_BPR) { 1256 case 0: 1257 if (regno < 2) { 1258 s->nsapr[regno + 2][cpu] = value; 1259 } 1260 break; 1261 case 1: 1262 if (regno == 0) { 1263 s->nsapr[regno + 1][cpu] = value; 1264 } 1265 break; 1266 case 2: 1267 if (regno == 0) { 1268 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value); 1269 } 1270 break; 1271 case 3: 1272 if (regno == 0) { 1273 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value); 1274 } 1275 break; 1276 default: 1277 g_assert_not_reached(); 1278 } 1279 } 1280 1281 static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, 1282 uint64_t *data, MemTxAttrs attrs) 1283 { 1284 switch (offset) { 1285 case 0x00: /* Control */ 1286 *data = gic_get_cpu_control(s, cpu, attrs); 1287 break; 1288 case 0x04: /* Priority mask */ 1289 *data = gic_get_priority_mask(s, cpu, attrs); 1290 break; 1291 case 0x08: /* Binary Point */ 1292 if (s->security_extn && !attrs.secure) { 1293 if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1294 /* NS view of BPR when CBPR is 1 */ 1295 *data = MIN(s->bpr[cpu] + 1, 7); 1296 } else { 1297 /* BPR is banked. Non-secure copy stored in ABPR. */ 1298 *data = s->abpr[cpu]; 1299 } 1300 } else { 1301 *data = s->bpr[cpu]; 1302 } 1303 break; 1304 case 0x0c: /* Acknowledge */ 1305 *data = gic_acknowledge_irq(s, cpu, attrs); 1306 break; 1307 case 0x14: /* Running Priority */ 1308 *data = gic_get_running_priority(s, cpu, attrs); 1309 break; 1310 case 0x18: /* Highest Pending Interrupt */ 1311 *data = gic_get_current_pending_irq(s, cpu, attrs); 1312 break; 1313 case 0x1c: /* Aliased Binary Point */ 1314 /* GIC v2, no security: ABPR 1315 * GIC v1, no security: not implemented (RAZ/WI) 1316 * With security extensions, secure access: ABPR (alias of NS BPR) 1317 * With security extensions, nonsecure access: RAZ/WI 1318 */ 1319 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) { 1320 *data = 0; 1321 } else { 1322 *data = s->abpr[cpu]; 1323 } 1324 break; 1325 case 0xd0: case 0xd4: case 0xd8: case 0xdc: 1326 { 1327 int regno = (offset - 0xd0) / 4; 1328 1329 if (regno >= GIC_NR_APRS || s->revision != 2) { 1330 *data = 0; 1331 } else if (s->security_extn && !attrs.secure) { 1332 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 1333 *data = gic_apr_ns_view(s, regno, cpu); 1334 } else { 1335 *data = s->apr[regno][cpu]; 1336 } 1337 break; 1338 } 1339 case 0xe0: case 0xe4: case 0xe8: case 0xec: 1340 { 1341 int regno = (offset - 0xe0) / 4; 1342 1343 if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) || 1344 (s->security_extn && !attrs.secure)) { 1345 *data = 0; 1346 } else { 1347 *data = s->nsapr[regno][cpu]; 1348 } 1349 break; 1350 } 1351 default: 1352 qemu_log_mask(LOG_GUEST_ERROR, 1353 "gic_cpu_read: Bad offset %x\n", (int)offset); 1354 *data = 0; 1355 break; 1356 } 1357 return MEMTX_OK; 1358 } 1359 1360 static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset, 1361 uint32_t value, MemTxAttrs attrs) 1362 { 1363 switch (offset) { 1364 case 0x00: /* Control */ 1365 gic_set_cpu_control(s, cpu, value, attrs); 1366 break; 1367 case 0x04: /* Priority mask */ 1368 gic_set_priority_mask(s, cpu, value, attrs); 1369 break; 1370 case 0x08: /* Binary Point */ 1371 if (s->security_extn && !attrs.secure) { 1372 if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) { 1373 /* WI when CBPR is 1 */ 1374 return MEMTX_OK; 1375 } else { 1376 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1377 } 1378 } else { 1379 s->bpr[cpu] = MAX(value & 0x7, GIC_MIN_BPR); 1380 } 1381 break; 1382 case 0x10: /* End Of Interrupt */ 1383 gic_complete_irq(s, cpu, value & 0x3ff, attrs); 1384 return MEMTX_OK; 1385 case 0x1c: /* Aliased Binary Point */ 1386 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) { 1387 /* unimplemented, or NS access: RAZ/WI */ 1388 return MEMTX_OK; 1389 } else { 1390 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR); 1391 } 1392 break; 1393 case 0xd0: case 0xd4: case 0xd8: case 0xdc: 1394 { 1395 int regno = (offset - 0xd0) / 4; 1396 1397 if (regno >= GIC_NR_APRS || s->revision != 2) { 1398 return MEMTX_OK; 1399 } 1400 if (s->security_extn && !attrs.secure) { 1401 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */ 1402 gic_apr_write_ns_view(s, regno, cpu, value); 1403 } else { 1404 s->apr[regno][cpu] = value; 1405 } 1406 break; 1407 } 1408 case 0xe0: case 0xe4: case 0xe8: case 0xec: 1409 { 1410 int regno = (offset - 0xe0) / 4; 1411 1412 if (regno >= GIC_NR_APRS || s->revision != 2) { 1413 return MEMTX_OK; 1414 } 1415 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) { 1416 return MEMTX_OK; 1417 } 1418 s->nsapr[regno][cpu] = value; 1419 break; 1420 } 1421 case 0x1000: 1422 /* GICC_DIR */ 1423 gic_deactivate_irq(s, cpu, value & 0x3ff, attrs); 1424 break; 1425 default: 1426 qemu_log_mask(LOG_GUEST_ERROR, 1427 "gic_cpu_write: Bad offset %x\n", (int)offset); 1428 return MEMTX_OK; 1429 } 1430 gic_update(s); 1431 return MEMTX_OK; 1432 } 1433 1434 /* Wrappers to read/write the GIC CPU interface for the current CPU */ 1435 static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data, 1436 unsigned size, MemTxAttrs attrs) 1437 { 1438 GICState *s = (GICState *)opaque; 1439 return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs); 1440 } 1441 1442 static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr, 1443 uint64_t value, unsigned size, 1444 MemTxAttrs attrs) 1445 { 1446 GICState *s = (GICState *)opaque; 1447 return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs); 1448 } 1449 1450 /* Wrappers to read/write the GIC CPU interface for a specific CPU. 1451 * These just decode the opaque pointer into GICState* + cpu id. 1452 */ 1453 static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data, 1454 unsigned size, MemTxAttrs attrs) 1455 { 1456 GICState **backref = (GICState **)opaque; 1457 GICState *s = *backref; 1458 int id = (backref - s->backref); 1459 return gic_cpu_read(s, id, addr, data, attrs); 1460 } 1461 1462 static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr, 1463 uint64_t value, unsigned size, 1464 MemTxAttrs attrs) 1465 { 1466 GICState **backref = (GICState **)opaque; 1467 GICState *s = *backref; 1468 int id = (backref - s->backref); 1469 return gic_cpu_write(s, id, addr, value, attrs); 1470 } 1471 1472 static const MemoryRegionOps gic_ops[2] = { 1473 { 1474 .read_with_attrs = gic_dist_read, 1475 .write_with_attrs = gic_dist_write, 1476 .endianness = DEVICE_NATIVE_ENDIAN, 1477 }, 1478 { 1479 .read_with_attrs = gic_thiscpu_read, 1480 .write_with_attrs = gic_thiscpu_write, 1481 .endianness = DEVICE_NATIVE_ENDIAN, 1482 } 1483 }; 1484 1485 static const MemoryRegionOps gic_cpu_ops = { 1486 .read_with_attrs = gic_do_cpu_read, 1487 .write_with_attrs = gic_do_cpu_write, 1488 .endianness = DEVICE_NATIVE_ENDIAN, 1489 }; 1490 1491 /* This function is used by nvic model */ 1492 void gic_init_irqs_and_distributor(GICState *s) 1493 { 1494 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops); 1495 } 1496 1497 static void arm_gic_realize(DeviceState *dev, Error **errp) 1498 { 1499 /* Device instance realize function for the GIC sysbus device */ 1500 int i; 1501 GICState *s = ARM_GIC(dev); 1502 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 1503 ARMGICClass *agc = ARM_GIC_GET_CLASS(s); 1504 Error *local_err = NULL; 1505 1506 agc->parent_realize(dev, &local_err); 1507 if (local_err) { 1508 error_propagate(errp, local_err); 1509 return; 1510 } 1511 1512 if (kvm_enabled() && !kvm_arm_supports_user_irq()) { 1513 error_setg(errp, "KVM with user space irqchip only works when the " 1514 "host kernel supports KVM_CAP_ARM_USER_IRQ"); 1515 return; 1516 } 1517 1518 /* This creates distributor and main CPU interface (s->cpuiomem[0]) */ 1519 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops); 1520 1521 /* Extra core-specific regions for the CPU interfaces. This is 1522 * necessary for "franken-GIC" implementations, for example on 1523 * Exynos 4. 1524 * NB that the memory region size of 0x100 applies for the 11MPCore 1525 * and also cores following the GIC v1 spec (ie A9). 1526 * GIC v2 defines a larger memory region (0x1000) so this will need 1527 * to be extended when we implement A15. 1528 */ 1529 for (i = 0; i < s->num_cpu; i++) { 1530 s->backref[i] = s; 1531 memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops, 1532 &s->backref[i], "gic_cpu", 0x100); 1533 sysbus_init_mmio(sbd, &s->cpuiomem[i+1]); 1534 } 1535 } 1536 1537 static void arm_gic_class_init(ObjectClass *klass, void *data) 1538 { 1539 DeviceClass *dc = DEVICE_CLASS(klass); 1540 ARMGICClass *agc = ARM_GIC_CLASS(klass); 1541 1542 device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize); 1543 } 1544 1545 static const TypeInfo arm_gic_info = { 1546 .name = TYPE_ARM_GIC, 1547 .parent = TYPE_ARM_GIC_COMMON, 1548 .instance_size = sizeof(GICState), 1549 .class_init = arm_gic_class_init, 1550 .class_size = sizeof(ARMGICClass), 1551 }; 1552 1553 static void arm_gic_register_types(void) 1554 { 1555 type_register_static(&arm_gic_info); 1556 } 1557 1558 type_init(arm_gic_register_types) 1559