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