1 /* 2 * ARM Generic Interrupt Controller v3 3 * 4 * Copyright (c) 2016 Linaro Limited 5 * Written by Peter Maydell 6 * 7 * This code is licensed under the GPL, version 2 or (at your option) 8 * any later version. 9 */ 10 11 /* This file contains the code for the system register interface 12 * portions of the GICv3. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "qemu/bitops.h" 17 #include "qemu/log.h" 18 #include "qemu/main-loop.h" 19 #include "trace.h" 20 #include "gicv3_internal.h" 21 #include "hw/irq.h" 22 #include "cpu.h" 23 24 void gicv3_set_gicv3state(CPUState *cpu, GICv3CPUState *s) 25 { 26 ARMCPU *arm_cpu = ARM_CPU(cpu); 27 CPUARMState *env = &arm_cpu->env; 28 29 env->gicv3state = (void *)s; 30 }; 31 32 static GICv3CPUState *icc_cs_from_env(CPUARMState *env) 33 { 34 return env->gicv3state; 35 } 36 37 static bool gicv3_use_ns_bank(CPUARMState *env) 38 { 39 /* Return true if we should use the NonSecure bank for a banked GIC 40 * CPU interface register. Note that this differs from the 41 * access_secure_reg() function because GICv3 banked registers are 42 * banked even for AArch64, unlike the other CPU system registers. 43 */ 44 return !arm_is_secure_below_el3(env); 45 } 46 47 /* The minimum BPR for the virtual interface is a configurable property */ 48 static inline int icv_min_vbpr(GICv3CPUState *cs) 49 { 50 return 7 - cs->vprebits; 51 } 52 53 /* Simple accessor functions for LR fields */ 54 static uint32_t ich_lr_vintid(uint64_t lr) 55 { 56 return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH); 57 } 58 59 static uint32_t ich_lr_pintid(uint64_t lr) 60 { 61 return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH); 62 } 63 64 static uint32_t ich_lr_prio(uint64_t lr) 65 { 66 return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH); 67 } 68 69 static int ich_lr_state(uint64_t lr) 70 { 71 return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH); 72 } 73 74 static bool icv_access(CPUARMState *env, int hcr_flags) 75 { 76 /* Return true if this ICC_ register access should really be 77 * directed to an ICV_ access. hcr_flags is a mask of 78 * HCR_EL2 bits to check: we treat this as an ICV_ access 79 * if we are in NS EL1 and at least one of the specified 80 * HCR_EL2 bits is set. 81 * 82 * ICV registers fall into four categories: 83 * * access if NS EL1 and HCR_EL2.FMO == 1: 84 * all ICV regs with '0' in their name 85 * * access if NS EL1 and HCR_EL2.IMO == 1: 86 * all ICV regs with '1' in their name 87 * * access if NS EL1 and either IMO or FMO == 1: 88 * CTLR, DIR, PMR, RPR 89 */ 90 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 91 bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO); 92 93 return flagmatch && arm_current_el(env) == 1 94 && !arm_is_secure_below_el3(env); 95 } 96 97 static int read_vbpr(GICv3CPUState *cs, int grp) 98 { 99 /* Read VBPR value out of the VMCR field (caller must handle 100 * VCBPR effects if required) 101 */ 102 if (grp == GICV3_G0) { 103 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT, 104 ICH_VMCR_EL2_VBPR0_LENGTH); 105 } else { 106 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT, 107 ICH_VMCR_EL2_VBPR1_LENGTH); 108 } 109 } 110 111 static void write_vbpr(GICv3CPUState *cs, int grp, int value) 112 { 113 /* Write new VBPR1 value, handling the "writing a value less than 114 * the minimum sets it to the minimum" semantics. 115 */ 116 int min = icv_min_vbpr(cs); 117 118 if (grp != GICV3_G0) { 119 min++; 120 } 121 122 value = MAX(value, min); 123 124 if (grp == GICV3_G0) { 125 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT, 126 ICH_VMCR_EL2_VBPR0_LENGTH, value); 127 } else { 128 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT, 129 ICH_VMCR_EL2_VBPR1_LENGTH, value); 130 } 131 } 132 133 static uint32_t icv_fullprio_mask(GICv3CPUState *cs) 134 { 135 /* Return a mask word which clears the unimplemented priority bits 136 * from a priority value for a virtual interrupt. (Not to be confused 137 * with the group priority, whose mask depends on the value of VBPR 138 * for the interrupt group.) 139 */ 140 return ~0U << (8 - cs->vpribits); 141 } 142 143 static int ich_highest_active_virt_prio(GICv3CPUState *cs) 144 { 145 /* Calculate the current running priority based on the set bits 146 * in the ICH Active Priority Registers. 147 */ 148 int i; 149 int aprmax = 1 << (cs->vprebits - 5); 150 151 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0])); 152 153 for (i = 0; i < aprmax; i++) { 154 uint32_t apr = cs->ich_apr[GICV3_G0][i] | 155 cs->ich_apr[GICV3_G1NS][i]; 156 157 if (!apr) { 158 continue; 159 } 160 return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1); 161 } 162 /* No current active interrupts: return idle priority */ 163 return 0xff; 164 } 165 166 static int hppvi_index(GICv3CPUState *cs) 167 { 168 /* Return the list register index of the highest priority pending 169 * virtual interrupt, as per the HighestPriorityVirtualInterrupt 170 * pseudocode. If no pending virtual interrupts, return -1. 171 */ 172 int idx = -1; 173 int i; 174 /* Note that a list register entry with a priority of 0xff will 175 * never be reported by this function; this is the architecturally 176 * correct behaviour. 177 */ 178 int prio = 0xff; 179 180 if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) { 181 /* Both groups disabled, definitely nothing to do */ 182 return idx; 183 } 184 185 for (i = 0; i < cs->num_list_regs; i++) { 186 uint64_t lr = cs->ich_lr_el2[i]; 187 int thisprio; 188 189 if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) { 190 /* Not Pending */ 191 continue; 192 } 193 194 /* Ignore interrupts if relevant group enable not set */ 195 if (lr & ICH_LR_EL2_GROUP) { 196 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) { 197 continue; 198 } 199 } else { 200 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) { 201 continue; 202 } 203 } 204 205 thisprio = ich_lr_prio(lr); 206 207 if (thisprio < prio) { 208 prio = thisprio; 209 idx = i; 210 } 211 } 212 213 return idx; 214 } 215 216 static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group) 217 { 218 /* Return a mask word which clears the subpriority bits from 219 * a priority value for a virtual interrupt in the specified group. 220 * This depends on the VBPR value. 221 * If using VBPR0 then: 222 * a BPR of 0 means the group priority bits are [7:1]; 223 * a BPR of 1 means they are [7:2], and so on down to 224 * a BPR of 7 meaning no group priority bits at all. 225 * If using VBPR1 then: 226 * a BPR of 0 is impossible (the minimum value is 1) 227 * a BPR of 1 means the group priority bits are [7:1]; 228 * a BPR of 2 means they are [7:2], and so on down to 229 * a BPR of 7 meaning the group priority is [7]. 230 * 231 * Which BPR to use depends on the group of the interrupt and 232 * the current ICH_VMCR_EL2.VCBPR settings. 233 * 234 * This corresponds to the VGroupBits() pseudocode. 235 */ 236 int bpr; 237 238 if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) { 239 group = GICV3_G0; 240 } 241 242 bpr = read_vbpr(cs, group); 243 if (group == GICV3_G1NS) { 244 assert(bpr > 0); 245 bpr--; 246 } 247 248 return ~0U << (bpr + 1); 249 } 250 251 static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr) 252 { 253 /* Return true if we can signal this virtual interrupt defined by 254 * the given list register value; see the pseudocode functions 255 * CanSignalVirtualInterrupt and CanSignalVirtualInt. 256 * Compare also icc_hppi_can_preempt() which is the non-virtual 257 * equivalent of these checks. 258 */ 259 int grp; 260 uint32_t mask, prio, rprio, vpmr; 261 262 if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) { 263 /* Virtual interface disabled */ 264 return false; 265 } 266 267 /* We don't need to check that this LR is in Pending state because 268 * that has already been done in hppvi_index(). 269 */ 270 271 prio = ich_lr_prio(lr); 272 vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT, 273 ICH_VMCR_EL2_VPMR_LENGTH); 274 275 if (prio >= vpmr) { 276 /* Priority mask masks this interrupt */ 277 return false; 278 } 279 280 rprio = ich_highest_active_virt_prio(cs); 281 if (rprio == 0xff) { 282 /* No running interrupt so we can preempt */ 283 return true; 284 } 285 286 grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0; 287 288 mask = icv_gprio_mask(cs, grp); 289 290 /* We only preempt a running interrupt if the pending interrupt's 291 * group priority is sufficient (the subpriorities are not considered). 292 */ 293 if ((prio & mask) < (rprio & mask)) { 294 return true; 295 } 296 297 return false; 298 } 299 300 static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs, 301 uint32_t *misr) 302 { 303 /* Return a set of bits indicating the EOI maintenance interrupt status 304 * for each list register. The EOI maintenance interrupt status is 305 * 1 if LR.State == 0 && LR.HW == 0 && LR.EOI == 1 306 * (see the GICv3 spec for the ICH_EISR_EL2 register). 307 * If misr is not NULL then we should also collect the information 308 * about the MISR.EOI, MISR.NP and MISR.U bits. 309 */ 310 uint32_t value = 0; 311 int validcount = 0; 312 bool seenpending = false; 313 int i; 314 315 for (i = 0; i < cs->num_list_regs; i++) { 316 uint64_t lr = cs->ich_lr_el2[i]; 317 318 if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI)) 319 == ICH_LR_EL2_EOI) { 320 value |= (1 << i); 321 } 322 if ((lr & ICH_LR_EL2_STATE_MASK)) { 323 validcount++; 324 } 325 if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) { 326 seenpending = true; 327 } 328 } 329 330 if (misr) { 331 if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) { 332 *misr |= ICH_MISR_EL2_U; 333 } 334 if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) { 335 *misr |= ICH_MISR_EL2_NP; 336 } 337 if (value) { 338 *misr |= ICH_MISR_EL2_EOI; 339 } 340 } 341 return value; 342 } 343 344 static uint32_t maintenance_interrupt_state(GICv3CPUState *cs) 345 { 346 /* Return a set of bits indicating the maintenance interrupt status 347 * (as seen in the ICH_MISR_EL2 register). 348 */ 349 uint32_t value = 0; 350 351 /* Scan list registers and fill in the U, NP and EOI bits */ 352 eoi_maintenance_interrupt_state(cs, &value); 353 354 if (cs->ich_hcr_el2 & (ICH_HCR_EL2_LRENPIE | ICH_HCR_EL2_EOICOUNT_MASK)) { 355 value |= ICH_MISR_EL2_LRENP; 356 } 357 358 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) && 359 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) { 360 value |= ICH_MISR_EL2_VGRP0E; 361 } 362 363 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) && 364 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) { 365 value |= ICH_MISR_EL2_VGRP0D; 366 } 367 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) && 368 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) { 369 value |= ICH_MISR_EL2_VGRP1E; 370 } 371 372 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) && 373 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) { 374 value |= ICH_MISR_EL2_VGRP1D; 375 } 376 377 return value; 378 } 379 380 static void gicv3_cpuif_virt_update(GICv3CPUState *cs) 381 { 382 /* Tell the CPU about any pending virtual interrupts or 383 * maintenance interrupts, following a change to the state 384 * of the CPU interface relevant to virtual interrupts. 385 * 386 * CAUTION: this function will call qemu_set_irq() on the 387 * CPU maintenance IRQ line, which is typically wired up 388 * to the GIC as a per-CPU interrupt. This means that it 389 * will recursively call back into the GIC code via 390 * gicv3_redist_set_irq() and thus into the CPU interface code's 391 * gicv3_cpuif_update(). It is therefore important that this 392 * function is only called as the final action of a CPU interface 393 * register write implementation, after all the GIC state 394 * fields have been updated. gicv3_cpuif_update() also must 395 * not cause this function to be called, but that happens 396 * naturally as a result of there being no architectural 397 * linkage between the physical and virtual GIC logic. 398 */ 399 int idx; 400 int irqlevel = 0; 401 int fiqlevel = 0; 402 int maintlevel = 0; 403 ARMCPU *cpu = ARM_CPU(cs->cpu); 404 405 idx = hppvi_index(cs); 406 trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx); 407 if (idx >= 0) { 408 uint64_t lr = cs->ich_lr_el2[idx]; 409 410 if (icv_hppi_can_preempt(cs, lr)) { 411 /* Virtual interrupts are simple: G0 are always FIQ, and G1 IRQ */ 412 if (lr & ICH_LR_EL2_GROUP) { 413 irqlevel = 1; 414 } else { 415 fiqlevel = 1; 416 } 417 } 418 } 419 420 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_EN) && 421 maintenance_interrupt_state(cs) != 0) { 422 maintlevel = 1; 423 } 424 425 trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel, 426 irqlevel, maintlevel); 427 428 qemu_set_irq(cs->parent_vfiq, fiqlevel); 429 qemu_set_irq(cs->parent_virq, irqlevel); 430 qemu_set_irq(cpu->gicv3_maintenance_interrupt, maintlevel); 431 } 432 433 static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 434 { 435 GICv3CPUState *cs = icc_cs_from_env(env); 436 int regno = ri->opc2 & 3; 437 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0; 438 uint64_t value = cs->ich_apr[grp][regno]; 439 440 trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 441 return value; 442 } 443 444 static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 445 uint64_t value) 446 { 447 GICv3CPUState *cs = icc_cs_from_env(env); 448 int regno = ri->opc2 & 3; 449 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0; 450 451 trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 452 453 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU; 454 455 gicv3_cpuif_virt_update(cs); 456 return; 457 } 458 459 static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri) 460 { 461 GICv3CPUState *cs = icc_cs_from_env(env); 462 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS; 463 uint64_t bpr; 464 bool satinc = false; 465 466 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) { 467 /* reads return bpr0 + 1 saturated to 7, writes ignored */ 468 grp = GICV3_G0; 469 satinc = true; 470 } 471 472 bpr = read_vbpr(cs, grp); 473 474 if (satinc) { 475 bpr++; 476 bpr = MIN(bpr, 7); 477 } 478 479 trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr); 480 481 return bpr; 482 } 483 484 static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, 485 uint64_t value) 486 { 487 GICv3CPUState *cs = icc_cs_from_env(env); 488 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS; 489 490 trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1, 491 gicv3_redist_affid(cs), value); 492 493 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) { 494 /* reads return bpr0 + 1 saturated to 7, writes ignored */ 495 return; 496 } 497 498 write_vbpr(cs, grp, value); 499 500 gicv3_cpuif_virt_update(cs); 501 } 502 503 static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri) 504 { 505 GICv3CPUState *cs = icc_cs_from_env(env); 506 uint64_t value; 507 508 value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT, 509 ICH_VMCR_EL2_VPMR_LENGTH); 510 511 trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value); 512 return value; 513 } 514 515 static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri, 516 uint64_t value) 517 { 518 GICv3CPUState *cs = icc_cs_from_env(env); 519 520 trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value); 521 522 value &= icv_fullprio_mask(cs); 523 524 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT, 525 ICH_VMCR_EL2_VPMR_LENGTH, value); 526 527 gicv3_cpuif_virt_update(cs); 528 } 529 530 static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri) 531 { 532 GICv3CPUState *cs = icc_cs_from_env(env); 533 int enbit; 534 uint64_t value; 535 536 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT; 537 value = extract64(cs->ich_vmcr_el2, enbit, 1); 538 539 trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0, 540 gicv3_redist_affid(cs), value); 541 return value; 542 } 543 544 static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri, 545 uint64_t value) 546 { 547 GICv3CPUState *cs = icc_cs_from_env(env); 548 int enbit; 549 550 trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0, 551 gicv3_redist_affid(cs), value); 552 553 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT; 554 555 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value); 556 gicv3_cpuif_virt_update(cs); 557 } 558 559 static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri) 560 { 561 GICv3CPUState *cs = icc_cs_from_env(env); 562 uint64_t value; 563 564 /* Note that the fixed fields here (A3V, SEIS, IDbits, PRIbits) 565 * should match the ones reported in ich_vtr_read(). 566 */ 567 value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) | 568 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT); 569 570 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) { 571 value |= ICC_CTLR_EL1_EOIMODE; 572 } 573 574 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) { 575 value |= ICC_CTLR_EL1_CBPR; 576 } 577 578 trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value); 579 return value; 580 } 581 582 static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 583 uint64_t value) 584 { 585 GICv3CPUState *cs = icc_cs_from_env(env); 586 587 trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value); 588 589 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT, 590 1, value & ICC_CTLR_EL1_CBPR ? 1 : 0); 591 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT, 592 1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0); 593 594 gicv3_cpuif_virt_update(cs); 595 } 596 597 static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri) 598 { 599 GICv3CPUState *cs = icc_cs_from_env(env); 600 int prio = ich_highest_active_virt_prio(cs); 601 602 trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio); 603 return prio; 604 } 605 606 static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri) 607 { 608 GICv3CPUState *cs = icc_cs_from_env(env); 609 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS; 610 int idx = hppvi_index(cs); 611 uint64_t value = INTID_SPURIOUS; 612 613 if (idx >= 0) { 614 uint64_t lr = cs->ich_lr_el2[idx]; 615 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0; 616 617 if (grp == thisgrp) { 618 value = ich_lr_vintid(lr); 619 } 620 } 621 622 trace_gicv3_icv_hppir_read(grp, gicv3_redist_affid(cs), value); 623 return value; 624 } 625 626 static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp) 627 { 628 /* Activate the interrupt in the specified list register 629 * by moving it from Pending to Active state, and update the 630 * Active Priority Registers. 631 */ 632 uint32_t mask = icv_gprio_mask(cs, grp); 633 int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask; 634 int aprbit = prio >> (8 - cs->vprebits); 635 int regno = aprbit / 32; 636 int regbit = aprbit % 32; 637 638 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT; 639 cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT; 640 cs->ich_apr[grp][regno] |= (1 << regbit); 641 } 642 643 static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri) 644 { 645 GICv3CPUState *cs = icc_cs_from_env(env); 646 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS; 647 int idx = hppvi_index(cs); 648 uint64_t intid = INTID_SPURIOUS; 649 650 if (idx >= 0) { 651 uint64_t lr = cs->ich_lr_el2[idx]; 652 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0; 653 654 if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) { 655 intid = ich_lr_vintid(lr); 656 if (!gicv3_intid_is_special(intid)) { 657 icv_activate_irq(cs, idx, grp); 658 } else { 659 /* Interrupt goes from Pending to Invalid */ 660 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT; 661 /* We will now return the (bogus) ID from the list register, 662 * as per the pseudocode. 663 */ 664 } 665 } 666 } 667 668 trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1, 669 gicv3_redist_affid(cs), intid); 670 671 gicv3_cpuif_virt_update(cs); 672 673 return intid; 674 } 675 676 static int icc_highest_active_prio(GICv3CPUState *cs) 677 { 678 /* Calculate the current running priority based on the set bits 679 * in the Active Priority Registers. 680 */ 681 int i; 682 683 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) { 684 uint32_t apr = cs->icc_apr[GICV3_G0][i] | 685 cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i]; 686 687 if (!apr) { 688 continue; 689 } 690 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1); 691 } 692 /* No current active interrupts: return idle priority */ 693 return 0xff; 694 } 695 696 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group) 697 { 698 /* Return a mask word which clears the subpriority bits from 699 * a priority value for an interrupt in the specified group. 700 * This depends on the BPR value. For CBPR0 (S or NS): 701 * a BPR of 0 means the group priority bits are [7:1]; 702 * a BPR of 1 means they are [7:2], and so on down to 703 * a BPR of 7 meaning no group priority bits at all. 704 * For CBPR1 NS: 705 * a BPR of 0 is impossible (the minimum value is 1) 706 * a BPR of 1 means the group priority bits are [7:1]; 707 * a BPR of 2 means they are [7:2], and so on down to 708 * a BPR of 7 meaning the group priority is [7]. 709 * 710 * Which BPR to use depends on the group of the interrupt and 711 * the current ICC_CTLR.CBPR settings. 712 * 713 * This corresponds to the GroupBits() pseudocode. 714 */ 715 int bpr; 716 717 if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) || 718 (group == GICV3_G1NS && 719 cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) { 720 group = GICV3_G0; 721 } 722 723 bpr = cs->icc_bpr[group] & 7; 724 725 if (group == GICV3_G1NS) { 726 assert(bpr > 0); 727 bpr--; 728 } 729 730 return ~0U << (bpr + 1); 731 } 732 733 static bool icc_no_enabled_hppi(GICv3CPUState *cs) 734 { 735 /* Return true if there is no pending interrupt, or the 736 * highest priority pending interrupt is in a group which has been 737 * disabled at the CPU interface by the ICC_IGRPEN* register enable bits. 738 */ 739 return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0); 740 } 741 742 static bool icc_hppi_can_preempt(GICv3CPUState *cs) 743 { 744 /* Return true if we have a pending interrupt of sufficient 745 * priority to preempt. 746 */ 747 int rprio; 748 uint32_t mask; 749 750 if (icc_no_enabled_hppi(cs)) { 751 return false; 752 } 753 754 if (cs->hppi.prio >= cs->icc_pmr_el1) { 755 /* Priority mask masks this interrupt */ 756 return false; 757 } 758 759 rprio = icc_highest_active_prio(cs); 760 if (rprio == 0xff) { 761 /* No currently running interrupt so we can preempt */ 762 return true; 763 } 764 765 mask = icc_gprio_mask(cs, cs->hppi.grp); 766 767 /* We only preempt a running interrupt if the pending interrupt's 768 * group priority is sufficient (the subpriorities are not considered). 769 */ 770 if ((cs->hppi.prio & mask) < (rprio & mask)) { 771 return true; 772 } 773 774 return false; 775 } 776 777 void gicv3_cpuif_update(GICv3CPUState *cs) 778 { 779 /* Tell the CPU about its highest priority pending interrupt */ 780 int irqlevel = 0; 781 int fiqlevel = 0; 782 ARMCPU *cpu = ARM_CPU(cs->cpu); 783 CPUARMState *env = &cpu->env; 784 785 g_assert(qemu_mutex_iothread_locked()); 786 787 trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq, 788 cs->hppi.grp, cs->hppi.prio); 789 790 if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) { 791 /* If a Security-enabled GIC sends a G1S interrupt to a 792 * Security-disabled CPU, we must treat it as if it were G0. 793 */ 794 cs->hppi.grp = GICV3_G0; 795 } 796 797 if (icc_hppi_can_preempt(cs)) { 798 /* We have an interrupt: should we signal it as IRQ or FIQ? 799 * This is described in the GICv3 spec section 4.6.2. 800 */ 801 bool isfiq; 802 803 switch (cs->hppi.grp) { 804 case GICV3_G0: 805 isfiq = true; 806 break; 807 case GICV3_G1: 808 isfiq = (!arm_is_secure(env) || 809 (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3))); 810 break; 811 case GICV3_G1NS: 812 isfiq = arm_is_secure(env); 813 break; 814 default: 815 g_assert_not_reached(); 816 } 817 818 if (isfiq) { 819 fiqlevel = 1; 820 } else { 821 irqlevel = 1; 822 } 823 } 824 825 trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel); 826 827 qemu_set_irq(cs->parent_fiq, fiqlevel); 828 qemu_set_irq(cs->parent_irq, irqlevel); 829 } 830 831 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri) 832 { 833 GICv3CPUState *cs = icc_cs_from_env(env); 834 uint32_t value = cs->icc_pmr_el1; 835 836 if (icv_access(env, HCR_FMO | HCR_IMO)) { 837 return icv_pmr_read(env, ri); 838 } 839 840 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) && 841 (env->cp15.scr_el3 & SCR_FIQ)) { 842 /* NS access and Group 0 is inaccessible to NS: return the 843 * NS view of the current priority 844 */ 845 if ((value & 0x80) == 0) { 846 /* Secure priorities not visible to NS */ 847 value = 0; 848 } else if (value != 0xff) { 849 value = (value << 1) & 0xff; 850 } 851 } 852 853 trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value); 854 855 return value; 856 } 857 858 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri, 859 uint64_t value) 860 { 861 GICv3CPUState *cs = icc_cs_from_env(env); 862 863 if (icv_access(env, HCR_FMO | HCR_IMO)) { 864 return icv_pmr_write(env, ri, value); 865 } 866 867 trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value); 868 869 value &= 0xff; 870 871 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) && 872 (env->cp15.scr_el3 & SCR_FIQ)) { 873 /* NS access and Group 0 is inaccessible to NS: return the 874 * NS view of the current priority 875 */ 876 if (!(cs->icc_pmr_el1 & 0x80)) { 877 /* Current PMR in the secure range, don't allow NS to change it */ 878 return; 879 } 880 value = (value >> 1) | 0x80; 881 } 882 cs->icc_pmr_el1 = value; 883 gicv3_cpuif_update(cs); 884 } 885 886 static void icc_activate_irq(GICv3CPUState *cs, int irq) 887 { 888 /* Move the interrupt from the Pending state to Active, and update 889 * the Active Priority Registers 890 */ 891 uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp); 892 int prio = cs->hppi.prio & mask; 893 int aprbit = prio >> 1; 894 int regno = aprbit / 32; 895 int regbit = aprbit % 32; 896 897 cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit); 898 899 if (irq < GIC_INTERNAL) { 900 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1); 901 cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0); 902 gicv3_redist_update(cs); 903 } else if (irq < GICV3_LPI_INTID_START) { 904 gicv3_gicd_active_set(cs->gic, irq); 905 gicv3_gicd_pending_clear(cs->gic, irq); 906 gicv3_update(cs->gic, irq, 1); 907 } else { 908 gicv3_redist_lpi_pending(cs, irq, 0); 909 } 910 } 911 912 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env) 913 { 914 /* Return the highest priority pending interrupt register value 915 * for group 0. 916 */ 917 bool irq_is_secure; 918 919 if (cs->hppi.prio == 0xff) { 920 return INTID_SPURIOUS; 921 } 922 923 /* Check whether we can return the interrupt or if we should return 924 * a special identifier, as per the CheckGroup0ForSpecialIdentifiers 925 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM 926 * is always zero.) 927 */ 928 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) && 929 (cs->hppi.grp != GICV3_G1NS)); 930 931 if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) { 932 return INTID_SPURIOUS; 933 } 934 if (irq_is_secure && !arm_is_secure(env)) { 935 /* Secure interrupts not visible to Nonsecure */ 936 return INTID_SPURIOUS; 937 } 938 939 if (cs->hppi.grp != GICV3_G0) { 940 /* Indicate to EL3 that there's a Group 1 interrupt for the other 941 * state pending. 942 */ 943 return irq_is_secure ? INTID_SECURE : INTID_NONSECURE; 944 } 945 946 return cs->hppi.irq; 947 } 948 949 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env) 950 { 951 /* Return the highest priority pending interrupt register value 952 * for group 1. 953 */ 954 bool irq_is_secure; 955 956 if (cs->hppi.prio == 0xff) { 957 return INTID_SPURIOUS; 958 } 959 960 /* Check whether we can return the interrupt or if we should return 961 * a special identifier, as per the CheckGroup1ForSpecialIdentifiers 962 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM 963 * is always zero.) 964 */ 965 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) && 966 (cs->hppi.grp != GICV3_G1NS)); 967 968 if (cs->hppi.grp == GICV3_G0) { 969 /* Group 0 interrupts not visible via HPPIR1 */ 970 return INTID_SPURIOUS; 971 } 972 if (irq_is_secure) { 973 if (!arm_is_secure(env)) { 974 /* Secure interrupts not visible in Non-secure */ 975 return INTID_SPURIOUS; 976 } 977 } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) { 978 /* Group 1 non-secure interrupts not visible in Secure EL1 */ 979 return INTID_SPURIOUS; 980 } 981 982 return cs->hppi.irq; 983 } 984 985 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri) 986 { 987 GICv3CPUState *cs = icc_cs_from_env(env); 988 uint64_t intid; 989 990 if (icv_access(env, HCR_FMO)) { 991 return icv_iar_read(env, ri); 992 } 993 994 if (!icc_hppi_can_preempt(cs)) { 995 intid = INTID_SPURIOUS; 996 } else { 997 intid = icc_hppir0_value(cs, env); 998 } 999 1000 if (!gicv3_intid_is_special(intid)) { 1001 icc_activate_irq(cs, intid); 1002 } 1003 1004 trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid); 1005 return intid; 1006 } 1007 1008 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri) 1009 { 1010 GICv3CPUState *cs = icc_cs_from_env(env); 1011 uint64_t intid; 1012 1013 if (icv_access(env, HCR_IMO)) { 1014 return icv_iar_read(env, ri); 1015 } 1016 1017 if (!icc_hppi_can_preempt(cs)) { 1018 intid = INTID_SPURIOUS; 1019 } else { 1020 intid = icc_hppir1_value(cs, env); 1021 } 1022 1023 if (!gicv3_intid_is_special(intid)) { 1024 icc_activate_irq(cs, intid); 1025 } 1026 1027 trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid); 1028 return intid; 1029 } 1030 1031 static void icc_drop_prio(GICv3CPUState *cs, int grp) 1032 { 1033 /* Drop the priority of the currently active interrupt in 1034 * the specified group. 1035 * 1036 * Note that we can guarantee (because of the requirement to nest 1037 * ICC_IAR reads [which activate an interrupt and raise priority] 1038 * with ICC_EOIR writes [which drop the priority for the interrupt]) 1039 * that the interrupt we're being called for is the highest priority 1040 * active interrupt, meaning that it has the lowest set bit in the 1041 * APR registers. 1042 * 1043 * If the guest does not honour the ordering constraints then the 1044 * behaviour of the GIC is UNPREDICTABLE, which for us means that 1045 * the values of the APR registers might become incorrect and the 1046 * running priority will be wrong, so interrupts that should preempt 1047 * might not do so, and interrupts that should not preempt might do so. 1048 */ 1049 int i; 1050 1051 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[grp]); i++) { 1052 uint64_t *papr = &cs->icc_apr[grp][i]; 1053 1054 if (!*papr) { 1055 continue; 1056 } 1057 /* Clear the lowest set bit */ 1058 *papr &= *papr - 1; 1059 break; 1060 } 1061 1062 /* running priority change means we need an update for this cpu i/f */ 1063 gicv3_cpuif_update(cs); 1064 } 1065 1066 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs) 1067 { 1068 /* Return true if we should split priority drop and interrupt 1069 * deactivation, ie whether the relevant EOIMode bit is set. 1070 */ 1071 if (arm_is_el3_or_mon(env)) { 1072 return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3; 1073 } 1074 if (arm_is_secure_below_el3(env)) { 1075 return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE; 1076 } else { 1077 return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE; 1078 } 1079 } 1080 1081 static int icc_highest_active_group(GICv3CPUState *cs) 1082 { 1083 /* Return the group with the highest priority active interrupt. 1084 * We can do this by just comparing the APRs to see which one 1085 * has the lowest set bit. 1086 * (If more than one group is active at the same priority then 1087 * we're in UNPREDICTABLE territory.) 1088 */ 1089 int i; 1090 1091 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) { 1092 int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]); 1093 int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]); 1094 int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]); 1095 1096 if (g1nsctz < g0ctz && g1nsctz < g1ctz) { 1097 return GICV3_G1NS; 1098 } 1099 if (g1ctz < g0ctz) { 1100 return GICV3_G1; 1101 } 1102 if (g0ctz < 32) { 1103 return GICV3_G0; 1104 } 1105 } 1106 /* No set active bits? UNPREDICTABLE; return -1 so the caller 1107 * ignores the spurious EOI attempt. 1108 */ 1109 return -1; 1110 } 1111 1112 static void icc_deactivate_irq(GICv3CPUState *cs, int irq) 1113 { 1114 if (irq < GIC_INTERNAL) { 1115 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0); 1116 gicv3_redist_update(cs); 1117 } else { 1118 gicv3_gicd_active_clear(cs->gic, irq); 1119 gicv3_update(cs->gic, irq, 1); 1120 } 1121 } 1122 1123 static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs) 1124 { 1125 /* Return true if we should split priority drop and interrupt 1126 * deactivation, ie whether the virtual EOIMode bit is set. 1127 */ 1128 return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM; 1129 } 1130 1131 static int icv_find_active(GICv3CPUState *cs, int irq) 1132 { 1133 /* Given an interrupt number for an active interrupt, return the index 1134 * of the corresponding list register, or -1 if there is no match. 1135 * Corresponds to FindActiveVirtualInterrupt pseudocode. 1136 */ 1137 int i; 1138 1139 for (i = 0; i < cs->num_list_regs; i++) { 1140 uint64_t lr = cs->ich_lr_el2[i]; 1141 1142 if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) { 1143 return i; 1144 } 1145 } 1146 1147 return -1; 1148 } 1149 1150 static void icv_deactivate_irq(GICv3CPUState *cs, int idx) 1151 { 1152 /* Deactivate the interrupt in the specified list register index */ 1153 uint64_t lr = cs->ich_lr_el2[idx]; 1154 1155 if (lr & ICH_LR_EL2_HW) { 1156 /* Deactivate the associated physical interrupt */ 1157 int pirq = ich_lr_pintid(lr); 1158 1159 if (pirq < INTID_SECURE) { 1160 icc_deactivate_irq(cs, pirq); 1161 } 1162 } 1163 1164 /* Clear the 'active' part of the state, so ActivePending->Pending 1165 * and Active->Invalid. 1166 */ 1167 lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT; 1168 cs->ich_lr_el2[idx] = lr; 1169 } 1170 1171 static void icv_increment_eoicount(GICv3CPUState *cs) 1172 { 1173 /* Increment the EOICOUNT field in ICH_HCR_EL2 */ 1174 int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT, 1175 ICH_HCR_EL2_EOICOUNT_LENGTH); 1176 1177 cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT, 1178 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1); 1179 } 1180 1181 static int icv_drop_prio(GICv3CPUState *cs) 1182 { 1183 /* Drop the priority of the currently active virtual interrupt 1184 * (favouring group 0 if there is a set active bit at 1185 * the same priority for both group 0 and group 1). 1186 * Return the priority value for the bit we just cleared, 1187 * or 0xff if no bits were set in the AP registers at all. 1188 * Note that though the ich_apr[] are uint64_t only the low 1189 * 32 bits are actually relevant. 1190 */ 1191 int i; 1192 int aprmax = 1 << (cs->vprebits - 5); 1193 1194 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0])); 1195 1196 for (i = 0; i < aprmax; i++) { 1197 uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i]; 1198 uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i]; 1199 int apr0count, apr1count; 1200 1201 if (!*papr0 && !*papr1) { 1202 continue; 1203 } 1204 1205 /* We can't just use the bit-twiddling hack icc_drop_prio() does 1206 * because we need to return the bit number we cleared so 1207 * it can be compared against the list register's priority field. 1208 */ 1209 apr0count = ctz32(*papr0); 1210 apr1count = ctz32(*papr1); 1211 1212 if (apr0count <= apr1count) { 1213 *papr0 &= *papr0 - 1; 1214 return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1); 1215 } else { 1216 *papr1 &= *papr1 - 1; 1217 return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1); 1218 } 1219 } 1220 return 0xff; 1221 } 1222 1223 static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri, 1224 uint64_t value) 1225 { 1226 /* Deactivate interrupt */ 1227 GICv3CPUState *cs = icc_cs_from_env(env); 1228 int idx; 1229 int irq = value & 0xffffff; 1230 1231 trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value); 1232 1233 if (irq >= GICV3_MAXIRQ) { 1234 /* Also catches special interrupt numbers and LPIs */ 1235 return; 1236 } 1237 1238 if (!icv_eoi_split(env, cs)) { 1239 return; 1240 } 1241 1242 idx = icv_find_active(cs, irq); 1243 1244 if (idx < 0) { 1245 /* No list register matching this, so increment the EOI count 1246 * (might trigger a maintenance interrupt) 1247 */ 1248 icv_increment_eoicount(cs); 1249 } else { 1250 icv_deactivate_irq(cs, idx); 1251 } 1252 1253 gicv3_cpuif_virt_update(cs); 1254 } 1255 1256 static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri, 1257 uint64_t value) 1258 { 1259 /* End of Interrupt */ 1260 GICv3CPUState *cs = icc_cs_from_env(env); 1261 int irq = value & 0xffffff; 1262 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS; 1263 int idx, dropprio; 1264 1265 trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1, 1266 gicv3_redist_affid(cs), value); 1267 1268 if (gicv3_intid_is_special(irq)) { 1269 return; 1270 } 1271 1272 /* We implement the IMPDEF choice of "drop priority before doing 1273 * error checks" (because that lets us avoid scanning the AP 1274 * registers twice). 1275 */ 1276 dropprio = icv_drop_prio(cs); 1277 if (dropprio == 0xff) { 1278 /* No active interrupt. It is CONSTRAINED UNPREDICTABLE 1279 * whether the list registers are checked in this 1280 * situation; we choose not to. 1281 */ 1282 return; 1283 } 1284 1285 idx = icv_find_active(cs, irq); 1286 1287 if (idx < 0) { 1288 /* No valid list register corresponding to EOI ID */ 1289 icv_increment_eoicount(cs); 1290 } else { 1291 uint64_t lr = cs->ich_lr_el2[idx]; 1292 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0; 1293 int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp); 1294 1295 if (thisgrp == grp && lr_gprio == dropprio) { 1296 if (!icv_eoi_split(env, cs)) { 1297 /* Priority drop and deactivate not split: deactivate irq now */ 1298 icv_deactivate_irq(cs, idx); 1299 } 1300 } 1301 } 1302 1303 gicv3_cpuif_virt_update(cs); 1304 } 1305 1306 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri, 1307 uint64_t value) 1308 { 1309 /* End of Interrupt */ 1310 GICv3CPUState *cs = icc_cs_from_env(env); 1311 int irq = value & 0xffffff; 1312 int grp; 1313 bool is_eoir0 = ri->crm == 8; 1314 1315 if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) { 1316 icv_eoir_write(env, ri, value); 1317 return; 1318 } 1319 1320 trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1, 1321 gicv3_redist_affid(cs), value); 1322 1323 if ((irq >= cs->gic->num_irq) && 1324 !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) { 1325 /* This handles two cases: 1326 * 1. If software writes the ID of a spurious interrupt [ie 1020-1023] 1327 * to the GICC_EOIR, the GIC ignores that write. 1328 * 2. If software writes the number of a non-existent interrupt 1329 * this must be a subcase of "value written does not match the last 1330 * valid interrupt value read from the Interrupt Acknowledge 1331 * register" and so this is UNPREDICTABLE. We choose to ignore it. 1332 */ 1333 return; 1334 } 1335 1336 grp = icc_highest_active_group(cs); 1337 switch (grp) { 1338 case GICV3_G0: 1339 if (!is_eoir0) { 1340 return; 1341 } 1342 if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) 1343 && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) { 1344 return; 1345 } 1346 break; 1347 case GICV3_G1: 1348 if (is_eoir0) { 1349 return; 1350 } 1351 if (!arm_is_secure(env)) { 1352 return; 1353 } 1354 break; 1355 case GICV3_G1NS: 1356 if (is_eoir0) { 1357 return; 1358 } 1359 if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) { 1360 return; 1361 } 1362 break; 1363 default: 1364 qemu_log_mask(LOG_GUEST_ERROR, 1365 "%s: IRQ %d isn't active\n", __func__, irq); 1366 return; 1367 } 1368 1369 icc_drop_prio(cs, grp); 1370 1371 if (!icc_eoi_split(env, cs)) { 1372 /* Priority drop and deactivate not split: deactivate irq now */ 1373 icc_deactivate_irq(cs, irq); 1374 } 1375 } 1376 1377 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri) 1378 { 1379 GICv3CPUState *cs = icc_cs_from_env(env); 1380 uint64_t value; 1381 1382 if (icv_access(env, HCR_FMO)) { 1383 return icv_hppir_read(env, ri); 1384 } 1385 1386 value = icc_hppir0_value(cs, env); 1387 trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value); 1388 return value; 1389 } 1390 1391 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri) 1392 { 1393 GICv3CPUState *cs = icc_cs_from_env(env); 1394 uint64_t value; 1395 1396 if (icv_access(env, HCR_IMO)) { 1397 return icv_hppir_read(env, ri); 1398 } 1399 1400 value = icc_hppir1_value(cs, env); 1401 trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value); 1402 return value; 1403 } 1404 1405 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1406 { 1407 GICv3CPUState *cs = icc_cs_from_env(env); 1408 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1; 1409 bool satinc = false; 1410 uint64_t bpr; 1411 1412 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1413 return icv_bpr_read(env, ri); 1414 } 1415 1416 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1417 grp = GICV3_G1NS; 1418 } 1419 1420 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) && 1421 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) { 1422 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses 1423 * modify BPR0 1424 */ 1425 grp = GICV3_G0; 1426 } 1427 1428 if (grp == GICV3_G1NS && arm_current_el(env) < 3 && 1429 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) { 1430 /* reads return bpr0 + 1 sat to 7, writes ignored */ 1431 grp = GICV3_G0; 1432 satinc = true; 1433 } 1434 1435 bpr = cs->icc_bpr[grp]; 1436 if (satinc) { 1437 bpr++; 1438 bpr = MIN(bpr, 7); 1439 } 1440 1441 trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr); 1442 1443 return bpr; 1444 } 1445 1446 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1447 uint64_t value) 1448 { 1449 GICv3CPUState *cs = icc_cs_from_env(env); 1450 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1; 1451 uint64_t minval; 1452 1453 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1454 icv_bpr_write(env, ri, value); 1455 return; 1456 } 1457 1458 trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1, 1459 gicv3_redist_affid(cs), value); 1460 1461 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1462 grp = GICV3_G1NS; 1463 } 1464 1465 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) && 1466 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) { 1467 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses 1468 * modify BPR0 1469 */ 1470 grp = GICV3_G0; 1471 } 1472 1473 if (grp == GICV3_G1NS && arm_current_el(env) < 3 && 1474 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) { 1475 /* reads return bpr0 + 1 sat to 7, writes ignored */ 1476 return; 1477 } 1478 1479 minval = (grp == GICV3_G1NS) ? GIC_MIN_BPR_NS : GIC_MIN_BPR; 1480 if (value < minval) { 1481 value = minval; 1482 } 1483 1484 cs->icc_bpr[grp] = value & 7; 1485 gicv3_cpuif_update(cs); 1486 } 1487 1488 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 1489 { 1490 GICv3CPUState *cs = icc_cs_from_env(env); 1491 uint64_t value; 1492 1493 int regno = ri->opc2 & 3; 1494 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0; 1495 1496 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1497 return icv_ap_read(env, ri); 1498 } 1499 1500 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1501 grp = GICV3_G1NS; 1502 } 1503 1504 value = cs->icc_apr[grp][regno]; 1505 1506 trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 1507 return value; 1508 } 1509 1510 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 1511 uint64_t value) 1512 { 1513 GICv3CPUState *cs = icc_cs_from_env(env); 1514 1515 int regno = ri->opc2 & 3; 1516 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0; 1517 1518 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1519 icv_ap_write(env, ri, value); 1520 return; 1521 } 1522 1523 trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 1524 1525 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1526 grp = GICV3_G1NS; 1527 } 1528 1529 /* It's not possible to claim that a Non-secure interrupt is active 1530 * at a priority outside the Non-secure range (128..255), since this 1531 * would otherwise allow malicious NS code to block delivery of S interrupts 1532 * by writing a bad value to these registers. 1533 */ 1534 if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) { 1535 return; 1536 } 1537 1538 cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU; 1539 gicv3_cpuif_update(cs); 1540 } 1541 1542 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri, 1543 uint64_t value) 1544 { 1545 /* Deactivate interrupt */ 1546 GICv3CPUState *cs = icc_cs_from_env(env); 1547 int irq = value & 0xffffff; 1548 bool irq_is_secure, single_sec_state, irq_is_grp0; 1549 bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2; 1550 1551 if (icv_access(env, HCR_FMO | HCR_IMO)) { 1552 icv_dir_write(env, ri, value); 1553 return; 1554 } 1555 1556 trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value); 1557 1558 if (irq >= cs->gic->num_irq) { 1559 /* Also catches special interrupt numbers and LPIs */ 1560 return; 1561 } 1562 1563 if (!icc_eoi_split(env, cs)) { 1564 return; 1565 } 1566 1567 int grp = gicv3_irq_group(cs->gic, cs, irq); 1568 1569 single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS; 1570 irq_is_secure = !single_sec_state && (grp != GICV3_G1NS); 1571 irq_is_grp0 = grp == GICV3_G0; 1572 1573 /* Check whether we're allowed to deactivate this interrupt based 1574 * on its group and the current CPU state. 1575 * These checks are laid out to correspond to the spec's pseudocode. 1576 */ 1577 route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ; 1578 route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ; 1579 /* No need to include !IsSecure in route_*_to_el2 as it's only 1580 * tested in cases where we know !IsSecure is true. 1581 */ 1582 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 1583 route_fiq_to_el2 = hcr_el2 & HCR_FMO; 1584 route_irq_to_el2 = hcr_el2 & HCR_IMO; 1585 1586 switch (arm_current_el(env)) { 1587 case 3: 1588 break; 1589 case 2: 1590 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) { 1591 break; 1592 } 1593 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) { 1594 break; 1595 } 1596 return; 1597 case 1: 1598 if (!arm_is_secure_below_el3(env)) { 1599 if (single_sec_state && irq_is_grp0 && 1600 !route_fiq_to_el3 && !route_fiq_to_el2) { 1601 break; 1602 } 1603 if (!irq_is_secure && !irq_is_grp0 && 1604 !route_irq_to_el3 && !route_irq_to_el2) { 1605 break; 1606 } 1607 } else { 1608 if (irq_is_grp0 && !route_fiq_to_el3) { 1609 break; 1610 } 1611 if (!irq_is_grp0 && 1612 (!irq_is_secure || !single_sec_state) && 1613 !route_irq_to_el3) { 1614 break; 1615 } 1616 } 1617 return; 1618 default: 1619 g_assert_not_reached(); 1620 } 1621 1622 icc_deactivate_irq(cs, irq); 1623 } 1624 1625 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1626 { 1627 GICv3CPUState *cs = icc_cs_from_env(env); 1628 int prio; 1629 1630 if (icv_access(env, HCR_FMO | HCR_IMO)) { 1631 return icv_rpr_read(env, ri); 1632 } 1633 1634 prio = icc_highest_active_prio(cs); 1635 1636 if (arm_feature(env, ARM_FEATURE_EL3) && 1637 !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) { 1638 /* NS GIC access and Group 0 is inaccessible to NS */ 1639 if ((prio & 0x80) == 0) { 1640 /* NS mustn't see priorities in the Secure half of the range */ 1641 prio = 0; 1642 } else if (prio != 0xff) { 1643 /* Non-idle priority: show the Non-secure view of it */ 1644 prio = (prio << 1) & 0xff; 1645 } 1646 } 1647 1648 trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio); 1649 return prio; 1650 } 1651 1652 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs, 1653 uint64_t value, int grp, bool ns) 1654 { 1655 GICv3State *s = cs->gic; 1656 1657 /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */ 1658 uint64_t aff = extract64(value, 48, 8) << 16 | 1659 extract64(value, 32, 8) << 8 | 1660 extract64(value, 16, 8); 1661 uint32_t targetlist = extract64(value, 0, 16); 1662 uint32_t irq = extract64(value, 24, 4); 1663 bool irm = extract64(value, 40, 1); 1664 int i; 1665 1666 if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) { 1667 /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1 1668 * interrupts as Group 0 interrupts and must send Secure Group 0 1669 * interrupts to the target CPUs. 1670 */ 1671 grp = GICV3_G0; 1672 } 1673 1674 trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm, 1675 aff, targetlist); 1676 1677 for (i = 0; i < s->num_cpu; i++) { 1678 GICv3CPUState *ocs = &s->cpu[i]; 1679 1680 if (irm) { 1681 /* IRM == 1 : route to all CPUs except self */ 1682 if (cs == ocs) { 1683 continue; 1684 } 1685 } else { 1686 /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15] 1687 * where the corresponding bit is set in targetlist 1688 */ 1689 int aff0; 1690 1691 if (ocs->gicr_typer >> 40 != aff) { 1692 continue; 1693 } 1694 aff0 = extract64(ocs->gicr_typer, 32, 8); 1695 if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) { 1696 continue; 1697 } 1698 } 1699 1700 /* The redistributor will check against its own GICR_NSACR as needed */ 1701 gicv3_redist_send_sgi(ocs, grp, irq, ns); 1702 } 1703 } 1704 1705 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri, 1706 uint64_t value) 1707 { 1708 /* Generate Secure Group 0 SGI. */ 1709 GICv3CPUState *cs = icc_cs_from_env(env); 1710 bool ns = !arm_is_secure(env); 1711 1712 icc_generate_sgi(env, cs, value, GICV3_G0, ns); 1713 } 1714 1715 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri, 1716 uint64_t value) 1717 { 1718 /* Generate Group 1 SGI for the current Security state */ 1719 GICv3CPUState *cs = icc_cs_from_env(env); 1720 int grp; 1721 bool ns = !arm_is_secure(env); 1722 1723 grp = ns ? GICV3_G1NS : GICV3_G1; 1724 icc_generate_sgi(env, cs, value, grp, ns); 1725 } 1726 1727 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri, 1728 uint64_t value) 1729 { 1730 /* Generate Group 1 SGI for the Security state that is not 1731 * the current state 1732 */ 1733 GICv3CPUState *cs = icc_cs_from_env(env); 1734 int grp; 1735 bool ns = !arm_is_secure(env); 1736 1737 grp = ns ? GICV3_G1 : GICV3_G1NS; 1738 icc_generate_sgi(env, cs, value, grp, ns); 1739 } 1740 1741 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri) 1742 { 1743 GICv3CPUState *cs = icc_cs_from_env(env); 1744 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0; 1745 uint64_t value; 1746 1747 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1748 return icv_igrpen_read(env, ri); 1749 } 1750 1751 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1752 grp = GICV3_G1NS; 1753 } 1754 1755 value = cs->icc_igrpen[grp]; 1756 trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0, 1757 gicv3_redist_affid(cs), value); 1758 return value; 1759 } 1760 1761 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri, 1762 uint64_t value) 1763 { 1764 GICv3CPUState *cs = icc_cs_from_env(env); 1765 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0; 1766 1767 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) { 1768 icv_igrpen_write(env, ri, value); 1769 return; 1770 } 1771 1772 trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0, 1773 gicv3_redist_affid(cs), value); 1774 1775 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) { 1776 grp = GICV3_G1NS; 1777 } 1778 1779 cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE; 1780 gicv3_cpuif_update(cs); 1781 } 1782 1783 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri) 1784 { 1785 GICv3CPUState *cs = icc_cs_from_env(env); 1786 uint64_t value; 1787 1788 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */ 1789 value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1); 1790 trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value); 1791 return value; 1792 } 1793 1794 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri, 1795 uint64_t value) 1796 { 1797 GICv3CPUState *cs = icc_cs_from_env(env); 1798 1799 trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value); 1800 1801 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */ 1802 cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1); 1803 cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1); 1804 gicv3_cpuif_update(cs); 1805 } 1806 1807 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri) 1808 { 1809 GICv3CPUState *cs = icc_cs_from_env(env); 1810 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S; 1811 uint64_t value; 1812 1813 if (icv_access(env, HCR_FMO | HCR_IMO)) { 1814 return icv_ctlr_read(env, ri); 1815 } 1816 1817 value = cs->icc_ctlr_el1[bank]; 1818 trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value); 1819 return value; 1820 } 1821 1822 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, 1823 uint64_t value) 1824 { 1825 GICv3CPUState *cs = icc_cs_from_env(env); 1826 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S; 1827 uint64_t mask; 1828 1829 if (icv_access(env, HCR_FMO | HCR_IMO)) { 1830 icv_ctlr_write(env, ri, value); 1831 return; 1832 } 1833 1834 trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value); 1835 1836 /* Only CBPR and EOIMODE can be RW; 1837 * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or 1838 * the asseciated priority-based routing of them); 1839 * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO. 1840 */ 1841 if (arm_feature(env, ARM_FEATURE_EL3) && 1842 ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) { 1843 mask = ICC_CTLR_EL1_EOIMODE; 1844 } else { 1845 mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE; 1846 } 1847 1848 cs->icc_ctlr_el1[bank] &= ~mask; 1849 cs->icc_ctlr_el1[bank] |= (value & mask); 1850 gicv3_cpuif_update(cs); 1851 } 1852 1853 1854 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri) 1855 { 1856 GICv3CPUState *cs = icc_cs_from_env(env); 1857 uint64_t value; 1858 1859 value = cs->icc_ctlr_el3; 1860 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) { 1861 value |= ICC_CTLR_EL3_EOIMODE_EL1NS; 1862 } 1863 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) { 1864 value |= ICC_CTLR_EL3_CBPR_EL1NS; 1865 } 1866 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) { 1867 value |= ICC_CTLR_EL3_EOIMODE_EL1S; 1868 } 1869 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) { 1870 value |= ICC_CTLR_EL3_CBPR_EL1S; 1871 } 1872 1873 trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value); 1874 return value; 1875 } 1876 1877 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri, 1878 uint64_t value) 1879 { 1880 GICv3CPUState *cs = icc_cs_from_env(env); 1881 uint64_t mask; 1882 1883 trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value); 1884 1885 /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */ 1886 cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE); 1887 if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) { 1888 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE; 1889 } 1890 if (value & ICC_CTLR_EL3_CBPR_EL1NS) { 1891 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR; 1892 } 1893 1894 cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE); 1895 if (value & ICC_CTLR_EL3_EOIMODE_EL1S) { 1896 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE; 1897 } 1898 if (value & ICC_CTLR_EL3_CBPR_EL1S) { 1899 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR; 1900 } 1901 1902 /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */ 1903 mask = ICC_CTLR_EL3_EOIMODE_EL3; 1904 1905 cs->icc_ctlr_el3 &= ~mask; 1906 cs->icc_ctlr_el3 |= (value & mask); 1907 gicv3_cpuif_update(cs); 1908 } 1909 1910 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env, 1911 const ARMCPRegInfo *ri, bool isread) 1912 { 1913 CPAccessResult r = CP_ACCESS_OK; 1914 GICv3CPUState *cs = icc_cs_from_env(env); 1915 int el = arm_current_el(env); 1916 1917 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) && 1918 el == 1 && !arm_is_secure_below_el3(env)) { 1919 /* Takes priority over a possible EL3 trap */ 1920 return CP_ACCESS_TRAP_EL2; 1921 } 1922 1923 if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) { 1924 switch (el) { 1925 case 1: 1926 /* Note that arm_hcr_el2_eff takes secure state into account. */ 1927 if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) { 1928 r = CP_ACCESS_TRAP_EL3; 1929 } 1930 break; 1931 case 2: 1932 r = CP_ACCESS_TRAP_EL3; 1933 break; 1934 case 3: 1935 if (!is_a64(env) && !arm_is_el3_or_mon(env)) { 1936 r = CP_ACCESS_TRAP_EL3; 1937 } 1938 break; 1939 default: 1940 g_assert_not_reached(); 1941 } 1942 } 1943 1944 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) { 1945 r = CP_ACCESS_TRAP; 1946 } 1947 return r; 1948 } 1949 1950 static CPAccessResult gicv3_dir_access(CPUARMState *env, 1951 const ARMCPRegInfo *ri, bool isread) 1952 { 1953 GICv3CPUState *cs = icc_cs_from_env(env); 1954 1955 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) && 1956 arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) { 1957 /* Takes priority over a possible EL3 trap */ 1958 return CP_ACCESS_TRAP_EL2; 1959 } 1960 1961 return gicv3_irqfiq_access(env, ri, isread); 1962 } 1963 1964 static CPAccessResult gicv3_sgi_access(CPUARMState *env, 1965 const ARMCPRegInfo *ri, bool isread) 1966 { 1967 if (arm_current_el(env) == 1 && 1968 (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) { 1969 /* Takes priority over a possible EL3 trap */ 1970 return CP_ACCESS_TRAP_EL2; 1971 } 1972 1973 return gicv3_irqfiq_access(env, ri, isread); 1974 } 1975 1976 static CPAccessResult gicv3_fiq_access(CPUARMState *env, 1977 const ARMCPRegInfo *ri, bool isread) 1978 { 1979 CPAccessResult r = CP_ACCESS_OK; 1980 GICv3CPUState *cs = icc_cs_from_env(env); 1981 int el = arm_current_el(env); 1982 1983 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) && 1984 el == 1 && !arm_is_secure_below_el3(env)) { 1985 /* Takes priority over a possible EL3 trap */ 1986 return CP_ACCESS_TRAP_EL2; 1987 } 1988 1989 if (env->cp15.scr_el3 & SCR_FIQ) { 1990 switch (el) { 1991 case 1: 1992 if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) { 1993 r = CP_ACCESS_TRAP_EL3; 1994 } 1995 break; 1996 case 2: 1997 r = CP_ACCESS_TRAP_EL3; 1998 break; 1999 case 3: 2000 if (!is_a64(env) && !arm_is_el3_or_mon(env)) { 2001 r = CP_ACCESS_TRAP_EL3; 2002 } 2003 break; 2004 default: 2005 g_assert_not_reached(); 2006 } 2007 } 2008 2009 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) { 2010 r = CP_ACCESS_TRAP; 2011 } 2012 return r; 2013 } 2014 2015 static CPAccessResult gicv3_irq_access(CPUARMState *env, 2016 const ARMCPRegInfo *ri, bool isread) 2017 { 2018 CPAccessResult r = CP_ACCESS_OK; 2019 GICv3CPUState *cs = icc_cs_from_env(env); 2020 int el = arm_current_el(env); 2021 2022 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) && 2023 el == 1 && !arm_is_secure_below_el3(env)) { 2024 /* Takes priority over a possible EL3 trap */ 2025 return CP_ACCESS_TRAP_EL2; 2026 } 2027 2028 if (env->cp15.scr_el3 & SCR_IRQ) { 2029 switch (el) { 2030 case 1: 2031 if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) { 2032 r = CP_ACCESS_TRAP_EL3; 2033 } 2034 break; 2035 case 2: 2036 r = CP_ACCESS_TRAP_EL3; 2037 break; 2038 case 3: 2039 if (!is_a64(env) && !arm_is_el3_or_mon(env)) { 2040 r = CP_ACCESS_TRAP_EL3; 2041 } 2042 break; 2043 default: 2044 g_assert_not_reached(); 2045 } 2046 } 2047 2048 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) { 2049 r = CP_ACCESS_TRAP; 2050 } 2051 return r; 2052 } 2053 2054 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2055 { 2056 GICv3CPUState *cs = icc_cs_from_env(env); 2057 2058 cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V | 2059 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) | 2060 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT); 2061 cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V | 2062 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) | 2063 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT); 2064 cs->icc_pmr_el1 = 0; 2065 cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR; 2066 cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR; 2067 cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS; 2068 memset(cs->icc_apr, 0, sizeof(cs->icc_apr)); 2069 memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen)); 2070 cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V | 2071 (1 << ICC_CTLR_EL3_IDBITS_SHIFT) | 2072 (7 << ICC_CTLR_EL3_PRIBITS_SHIFT); 2073 2074 memset(cs->ich_apr, 0, sizeof(cs->ich_apr)); 2075 cs->ich_hcr_el2 = 0; 2076 memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2)); 2077 cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN | 2078 ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) | 2079 (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT); 2080 } 2081 2082 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = { 2083 { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH, 2084 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0, 2085 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2086 .access = PL1_RW, .accessfn = gicv3_irqfiq_access, 2087 .readfn = icc_pmr_read, 2088 .writefn = icc_pmr_write, 2089 /* We hang the whole cpu interface reset routine off here 2090 * rather than parcelling it out into one little function 2091 * per register 2092 */ 2093 .resetfn = icc_reset, 2094 }, 2095 { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH, 2096 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0, 2097 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2098 .access = PL1_R, .accessfn = gicv3_fiq_access, 2099 .readfn = icc_iar0_read, 2100 }, 2101 { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH, 2102 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1, 2103 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2104 .access = PL1_W, .accessfn = gicv3_fiq_access, 2105 .writefn = icc_eoir_write, 2106 }, 2107 { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH, 2108 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2, 2109 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2110 .access = PL1_R, .accessfn = gicv3_fiq_access, 2111 .readfn = icc_hppir0_read, 2112 }, 2113 { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH, 2114 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3, 2115 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2116 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2117 .readfn = icc_bpr_read, 2118 .writefn = icc_bpr_write, 2119 }, 2120 { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH, 2121 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4, 2122 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2123 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2124 .readfn = icc_ap_read, 2125 .writefn = icc_ap_write, 2126 }, 2127 { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH, 2128 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5, 2129 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2130 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2131 .readfn = icc_ap_read, 2132 .writefn = icc_ap_write, 2133 }, 2134 { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH, 2135 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6, 2136 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2137 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2138 .readfn = icc_ap_read, 2139 .writefn = icc_ap_write, 2140 }, 2141 { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH, 2142 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7, 2143 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2144 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2145 .readfn = icc_ap_read, 2146 .writefn = icc_ap_write, 2147 }, 2148 /* All the ICC_AP1R*_EL1 registers are banked */ 2149 { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH, 2150 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0, 2151 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2152 .access = PL1_RW, .accessfn = gicv3_irq_access, 2153 .readfn = icc_ap_read, 2154 .writefn = icc_ap_write, 2155 }, 2156 { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH, 2157 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1, 2158 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2159 .access = PL1_RW, .accessfn = gicv3_irq_access, 2160 .readfn = icc_ap_read, 2161 .writefn = icc_ap_write, 2162 }, 2163 { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH, 2164 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2, 2165 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2166 .access = PL1_RW, .accessfn = gicv3_irq_access, 2167 .readfn = icc_ap_read, 2168 .writefn = icc_ap_write, 2169 }, 2170 { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH, 2171 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3, 2172 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2173 .access = PL1_RW, .accessfn = gicv3_irq_access, 2174 .readfn = icc_ap_read, 2175 .writefn = icc_ap_write, 2176 }, 2177 { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH, 2178 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1, 2179 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2180 .access = PL1_W, .accessfn = gicv3_dir_access, 2181 .writefn = icc_dir_write, 2182 }, 2183 { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH, 2184 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3, 2185 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2186 .access = PL1_R, .accessfn = gicv3_irqfiq_access, 2187 .readfn = icc_rpr_read, 2188 }, 2189 { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64, 2190 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5, 2191 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2192 .access = PL1_W, .accessfn = gicv3_sgi_access, 2193 .writefn = icc_sgi1r_write, 2194 }, 2195 { .name = "ICC_SGI1R", 2196 .cp = 15, .opc1 = 0, .crm = 12, 2197 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW, 2198 .access = PL1_W, .accessfn = gicv3_sgi_access, 2199 .writefn = icc_sgi1r_write, 2200 }, 2201 { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64, 2202 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6, 2203 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2204 .access = PL1_W, .accessfn = gicv3_sgi_access, 2205 .writefn = icc_asgi1r_write, 2206 }, 2207 { .name = "ICC_ASGI1R", 2208 .cp = 15, .opc1 = 1, .crm = 12, 2209 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW, 2210 .access = PL1_W, .accessfn = gicv3_sgi_access, 2211 .writefn = icc_asgi1r_write, 2212 }, 2213 { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64, 2214 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7, 2215 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2216 .access = PL1_W, .accessfn = gicv3_sgi_access, 2217 .writefn = icc_sgi0r_write, 2218 }, 2219 { .name = "ICC_SGI0R", 2220 .cp = 15, .opc1 = 2, .crm = 12, 2221 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW, 2222 .access = PL1_W, .accessfn = gicv3_sgi_access, 2223 .writefn = icc_sgi0r_write, 2224 }, 2225 { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH, 2226 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0, 2227 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2228 .access = PL1_R, .accessfn = gicv3_irq_access, 2229 .readfn = icc_iar1_read, 2230 }, 2231 { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH, 2232 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1, 2233 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2234 .access = PL1_W, .accessfn = gicv3_irq_access, 2235 .writefn = icc_eoir_write, 2236 }, 2237 { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH, 2238 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2, 2239 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2240 .access = PL1_R, .accessfn = gicv3_irq_access, 2241 .readfn = icc_hppir1_read, 2242 }, 2243 /* This register is banked */ 2244 { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH, 2245 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3, 2246 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2247 .access = PL1_RW, .accessfn = gicv3_irq_access, 2248 .readfn = icc_bpr_read, 2249 .writefn = icc_bpr_write, 2250 }, 2251 /* This register is banked */ 2252 { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH, 2253 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4, 2254 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2255 .access = PL1_RW, .accessfn = gicv3_irqfiq_access, 2256 .readfn = icc_ctlr_el1_read, 2257 .writefn = icc_ctlr_el1_write, 2258 }, 2259 { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH, 2260 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5, 2261 .type = ARM_CP_NO_RAW | ARM_CP_CONST, 2262 .access = PL1_RW, 2263 /* We don't support IRQ/FIQ bypass and system registers are 2264 * always enabled, so all our bits are RAZ/WI or RAO/WI. 2265 * This register is banked but since it's constant we don't 2266 * need to do anything special. 2267 */ 2268 .resetvalue = 0x7, 2269 }, 2270 { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH, 2271 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6, 2272 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2273 .access = PL1_RW, .accessfn = gicv3_fiq_access, 2274 .readfn = icc_igrpen_read, 2275 .writefn = icc_igrpen_write, 2276 }, 2277 /* This register is banked */ 2278 { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH, 2279 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7, 2280 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2281 .access = PL1_RW, .accessfn = gicv3_irq_access, 2282 .readfn = icc_igrpen_read, 2283 .writefn = icc_igrpen_write, 2284 }, 2285 { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH, 2286 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5, 2287 .type = ARM_CP_NO_RAW | ARM_CP_CONST, 2288 .access = PL2_RW, 2289 /* We don't support IRQ/FIQ bypass and system registers are 2290 * always enabled, so all our bits are RAZ/WI or RAO/WI. 2291 */ 2292 .resetvalue = 0xf, 2293 }, 2294 { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH, 2295 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4, 2296 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2297 .access = PL3_RW, 2298 .readfn = icc_ctlr_el3_read, 2299 .writefn = icc_ctlr_el3_write, 2300 }, 2301 { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH, 2302 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5, 2303 .type = ARM_CP_NO_RAW | ARM_CP_CONST, 2304 .access = PL3_RW, 2305 /* We don't support IRQ/FIQ bypass and system registers are 2306 * always enabled, so all our bits are RAZ/WI or RAO/WI. 2307 */ 2308 .resetvalue = 0xf, 2309 }, 2310 { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH, 2311 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7, 2312 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2313 .access = PL3_RW, 2314 .readfn = icc_igrpen1_el3_read, 2315 .writefn = icc_igrpen1_el3_write, 2316 }, 2317 REGINFO_SENTINEL 2318 }; 2319 2320 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 2321 { 2322 GICv3CPUState *cs = icc_cs_from_env(env); 2323 int regno = ri->opc2 & 3; 2324 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0; 2325 uint64_t value; 2326 2327 value = cs->ich_apr[grp][regno]; 2328 trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 2329 return value; 2330 } 2331 2332 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 2333 uint64_t value) 2334 { 2335 GICv3CPUState *cs = icc_cs_from_env(env); 2336 int regno = ri->opc2 & 3; 2337 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0; 2338 2339 trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value); 2340 2341 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU; 2342 gicv3_cpuif_virt_update(cs); 2343 } 2344 2345 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2346 { 2347 GICv3CPUState *cs = icc_cs_from_env(env); 2348 uint64_t value = cs->ich_hcr_el2; 2349 2350 trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value); 2351 return value; 2352 } 2353 2354 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2355 uint64_t value) 2356 { 2357 GICv3CPUState *cs = icc_cs_from_env(env); 2358 2359 trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value); 2360 2361 value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE | 2362 ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE | 2363 ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC | 2364 ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI | 2365 ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK; 2366 2367 cs->ich_hcr_el2 = value; 2368 gicv3_cpuif_virt_update(cs); 2369 } 2370 2371 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2372 { 2373 GICv3CPUState *cs = icc_cs_from_env(env); 2374 uint64_t value = cs->ich_vmcr_el2; 2375 2376 trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value); 2377 return value; 2378 } 2379 2380 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2381 uint64_t value) 2382 { 2383 GICv3CPUState *cs = icc_cs_from_env(env); 2384 2385 trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value); 2386 2387 value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR | 2388 ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK | 2389 ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK; 2390 value |= ICH_VMCR_EL2_VFIQEN; 2391 2392 cs->ich_vmcr_el2 = value; 2393 /* Enforce "writing BPRs to less than minimum sets them to the minimum" 2394 * by reading and writing back the fields. 2395 */ 2396 write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0)); 2397 write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1)); 2398 2399 gicv3_cpuif_virt_update(cs); 2400 } 2401 2402 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2403 { 2404 GICv3CPUState *cs = icc_cs_from_env(env); 2405 int regno = ri->opc2 | ((ri->crm & 1) << 3); 2406 uint64_t value; 2407 2408 /* This read function handles all of: 2409 * 64-bit reads of the whole LR 2410 * 32-bit reads of the low half of the LR 2411 * 32-bit reads of the high half of the LR 2412 */ 2413 if (ri->state == ARM_CP_STATE_AA32) { 2414 if (ri->crm >= 14) { 2415 value = extract64(cs->ich_lr_el2[regno], 32, 32); 2416 trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value); 2417 } else { 2418 value = extract64(cs->ich_lr_el2[regno], 0, 32); 2419 trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value); 2420 } 2421 } else { 2422 value = cs->ich_lr_el2[regno]; 2423 trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value); 2424 } 2425 2426 return value; 2427 } 2428 2429 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2430 uint64_t value) 2431 { 2432 GICv3CPUState *cs = icc_cs_from_env(env); 2433 int regno = ri->opc2 | ((ri->crm & 1) << 3); 2434 2435 /* This write function handles all of: 2436 * 64-bit writes to the whole LR 2437 * 32-bit writes to the low half of the LR 2438 * 32-bit writes to the high half of the LR 2439 */ 2440 if (ri->state == ARM_CP_STATE_AA32) { 2441 if (ri->crm >= 14) { 2442 trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value); 2443 value = deposit64(cs->ich_lr_el2[regno], 32, 32, value); 2444 } else { 2445 trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value); 2446 value = deposit64(cs->ich_lr_el2[regno], 0, 32, value); 2447 } 2448 } else { 2449 trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value); 2450 } 2451 2452 /* Enforce RES0 bits in priority field */ 2453 if (cs->vpribits < 8) { 2454 value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT, 2455 8 - cs->vpribits, 0); 2456 } 2457 2458 cs->ich_lr_el2[regno] = value; 2459 gicv3_cpuif_virt_update(cs); 2460 } 2461 2462 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2463 { 2464 GICv3CPUState *cs = icc_cs_from_env(env); 2465 uint64_t value; 2466 2467 value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT) 2468 | ICH_VTR_EL2_TDS | ICH_VTR_EL2_NV4 | ICH_VTR_EL2_A3V 2469 | (1 << ICH_VTR_EL2_IDBITS_SHIFT) 2470 | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT) 2471 | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT); 2472 2473 trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value); 2474 return value; 2475 } 2476 2477 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2478 { 2479 GICv3CPUState *cs = icc_cs_from_env(env); 2480 uint64_t value = maintenance_interrupt_state(cs); 2481 2482 trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value); 2483 return value; 2484 } 2485 2486 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2487 { 2488 GICv3CPUState *cs = icc_cs_from_env(env); 2489 uint64_t value = eoi_maintenance_interrupt_state(cs, NULL); 2490 2491 trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value); 2492 return value; 2493 } 2494 2495 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2496 { 2497 GICv3CPUState *cs = icc_cs_from_env(env); 2498 uint64_t value = 0; 2499 int i; 2500 2501 for (i = 0; i < cs->num_list_regs; i++) { 2502 uint64_t lr = cs->ich_lr_el2[i]; 2503 2504 if ((lr & ICH_LR_EL2_STATE_MASK) == 0 && 2505 ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) { 2506 value |= (1 << i); 2507 } 2508 } 2509 2510 trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value); 2511 return value; 2512 } 2513 2514 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = { 2515 { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH, 2516 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0, 2517 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2518 .access = PL2_RW, 2519 .readfn = ich_ap_read, 2520 .writefn = ich_ap_write, 2521 }, 2522 { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH, 2523 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0, 2524 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2525 .access = PL2_RW, 2526 .readfn = ich_ap_read, 2527 .writefn = ich_ap_write, 2528 }, 2529 { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH, 2530 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0, 2531 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2532 .access = PL2_RW, 2533 .readfn = ich_hcr_read, 2534 .writefn = ich_hcr_write, 2535 }, 2536 { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH, 2537 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1, 2538 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2539 .access = PL2_R, 2540 .readfn = ich_vtr_read, 2541 }, 2542 { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH, 2543 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2, 2544 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2545 .access = PL2_R, 2546 .readfn = ich_misr_read, 2547 }, 2548 { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH, 2549 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3, 2550 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2551 .access = PL2_R, 2552 .readfn = ich_eisr_read, 2553 }, 2554 { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH, 2555 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5, 2556 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2557 .access = PL2_R, 2558 .readfn = ich_elrsr_read, 2559 }, 2560 { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH, 2561 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7, 2562 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2563 .access = PL2_RW, 2564 .readfn = ich_vmcr_read, 2565 .writefn = ich_vmcr_write, 2566 }, 2567 REGINFO_SENTINEL 2568 }; 2569 2570 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = { 2571 { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH, 2572 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1, 2573 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2574 .access = PL2_RW, 2575 .readfn = ich_ap_read, 2576 .writefn = ich_ap_write, 2577 }, 2578 { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH, 2579 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1, 2580 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2581 .access = PL2_RW, 2582 .readfn = ich_ap_read, 2583 .writefn = ich_ap_write, 2584 }, 2585 REGINFO_SENTINEL 2586 }; 2587 2588 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = { 2589 { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH, 2590 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2, 2591 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2592 .access = PL2_RW, 2593 .readfn = ich_ap_read, 2594 .writefn = ich_ap_write, 2595 }, 2596 { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH, 2597 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3, 2598 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2599 .access = PL2_RW, 2600 .readfn = ich_ap_read, 2601 .writefn = ich_ap_write, 2602 }, 2603 { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH, 2604 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2, 2605 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2606 .access = PL2_RW, 2607 .readfn = ich_ap_read, 2608 .writefn = ich_ap_write, 2609 }, 2610 { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH, 2611 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3, 2612 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2613 .access = PL2_RW, 2614 .readfn = ich_ap_read, 2615 .writefn = ich_ap_write, 2616 }, 2617 REGINFO_SENTINEL 2618 }; 2619 2620 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque) 2621 { 2622 GICv3CPUState *cs = opaque; 2623 2624 gicv3_cpuif_update(cs); 2625 } 2626 2627 void gicv3_init_cpuif(GICv3State *s) 2628 { 2629 /* Called from the GICv3 realize function; register our system 2630 * registers with the CPU 2631 */ 2632 int i; 2633 2634 for (i = 0; i < s->num_cpu; i++) { 2635 ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i)); 2636 GICv3CPUState *cs = &s->cpu[i]; 2637 2638 /* Note that we can't just use the GICv3CPUState as an opaque pointer 2639 * in define_arm_cp_regs_with_opaque(), because when we're called back 2640 * it might be with code translated by CPU 0 but run by CPU 1, in 2641 * which case we'd get the wrong value. 2642 * So instead we define the regs with no ri->opaque info, and 2643 * get back to the GICv3CPUState from the CPUARMState. 2644 */ 2645 define_arm_cp_regs(cpu, gicv3_cpuif_reginfo); 2646 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) 2647 && cpu->gic_num_lrs) { 2648 int j; 2649 2650 cs->num_list_regs = cpu->gic_num_lrs; 2651 cs->vpribits = cpu->gic_vpribits; 2652 cs->vprebits = cpu->gic_vprebits; 2653 2654 /* Check against architectural constraints: getting these 2655 * wrong would be a bug in the CPU code defining these, 2656 * and the implementation relies on them holding. 2657 */ 2658 g_assert(cs->vprebits <= cs->vpribits); 2659 g_assert(cs->vprebits >= 5 && cs->vprebits <= 7); 2660 g_assert(cs->vpribits >= 5 && cs->vpribits <= 8); 2661 2662 define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo); 2663 2664 for (j = 0; j < cs->num_list_regs; j++) { 2665 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs 2666 * are split into two cp15 regs, LR (the low part, with the 2667 * same encoding as the AArch64 LR) and LRC (the high part). 2668 */ 2669 ARMCPRegInfo lr_regset[] = { 2670 { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH, 2671 .opc0 = 3, .opc1 = 4, .crn = 12, 2672 .crm = 12 + (j >> 3), .opc2 = j & 7, 2673 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2674 .access = PL2_RW, 2675 .readfn = ich_lr_read, 2676 .writefn = ich_lr_write, 2677 }, 2678 { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32, 2679 .cp = 15, .opc1 = 4, .crn = 12, 2680 .crm = 14 + (j >> 3), .opc2 = j & 7, 2681 .type = ARM_CP_IO | ARM_CP_NO_RAW, 2682 .access = PL2_RW, 2683 .readfn = ich_lr_read, 2684 .writefn = ich_lr_write, 2685 }, 2686 REGINFO_SENTINEL 2687 }; 2688 define_arm_cp_regs(cpu, lr_regset); 2689 } 2690 if (cs->vprebits >= 6) { 2691 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo); 2692 } 2693 if (cs->vprebits == 7) { 2694 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo); 2695 } 2696 } 2697 arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs); 2698 } 2699 } 2700