1 /* 2 * ARM GICv3 support - common bits of emulated and KVM kernel model 3 * 4 * Copyright (c) 2012 Linaro Limited 5 * Copyright (c) 2015 Huawei. 6 * Copyright (c) 2015 Samsung Electronics Co., Ltd. 7 * Written by Peter Maydell 8 * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License along 21 * with this program; if not, see <http://www.gnu.org/licenses/>. 22 */ 23 24 #include "qemu/osdep.h" 25 #include "qapi/error.h" 26 #include "qom/cpu.h" 27 #include "hw/intc/arm_gicv3_common.h" 28 #include "gicv3_internal.h" 29 #include "hw/arm/linux-boot-if.h" 30 #include "sysemu/kvm.h" 31 32 static int gicv3_pre_save(void *opaque) 33 { 34 GICv3State *s = (GICv3State *)opaque; 35 ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 36 37 if (c->pre_save) { 38 c->pre_save(s); 39 } 40 41 return 0; 42 } 43 44 static int gicv3_post_load(void *opaque, int version_id) 45 { 46 GICv3State *s = (GICv3State *)opaque; 47 ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s); 48 49 if (c->post_load) { 50 c->post_load(s); 51 } 52 return 0; 53 } 54 55 static bool virt_state_needed(void *opaque) 56 { 57 GICv3CPUState *cs = opaque; 58 59 return cs->num_list_regs != 0; 60 } 61 62 static const VMStateDescription vmstate_gicv3_cpu_virt = { 63 .name = "arm_gicv3_cpu/virt", 64 .version_id = 1, 65 .minimum_version_id = 1, 66 .needed = virt_state_needed, 67 .fields = (VMStateField[]) { 68 VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4), 69 VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState), 70 VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX), 71 VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState), 72 VMSTATE_END_OF_LIST() 73 } 74 }; 75 76 static int icc_sre_el1_reg_pre_load(void *opaque) 77 { 78 GICv3CPUState *cs = opaque; 79 80 /* 81 * If the sre_el1 subsection is not transferred this 82 * means SRE_EL1 is 0x7 (which might not be the same as 83 * our reset value). 84 */ 85 cs->icc_sre_el1 = 0x7; 86 return 0; 87 } 88 89 static bool icc_sre_el1_reg_needed(void *opaque) 90 { 91 GICv3CPUState *cs = opaque; 92 93 return cs->icc_sre_el1 != 7; 94 } 95 96 const VMStateDescription vmstate_gicv3_cpu_sre_el1 = { 97 .name = "arm_gicv3_cpu/sre_el1", 98 .version_id = 1, 99 .minimum_version_id = 1, 100 .pre_load = icc_sre_el1_reg_pre_load, 101 .needed = icc_sre_el1_reg_needed, 102 .fields = (VMStateField[]) { 103 VMSTATE_UINT64(icc_sre_el1, GICv3CPUState), 104 VMSTATE_END_OF_LIST() 105 } 106 }; 107 108 static const VMStateDescription vmstate_gicv3_cpu = { 109 .name = "arm_gicv3_cpu", 110 .version_id = 1, 111 .minimum_version_id = 1, 112 .fields = (VMStateField[]) { 113 VMSTATE_UINT32(level, GICv3CPUState), 114 VMSTATE_UINT32(gicr_ctlr, GICv3CPUState), 115 VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2), 116 VMSTATE_UINT32(gicr_waker, GICv3CPUState), 117 VMSTATE_UINT64(gicr_propbaser, GICv3CPUState), 118 VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState), 119 VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState), 120 VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState), 121 VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState), 122 VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState), 123 VMSTATE_UINT32(edge_trigger, GICv3CPUState), 124 VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState), 125 VMSTATE_UINT32(gicr_nsacr, GICv3CPUState), 126 VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL), 127 VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2), 128 VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState), 129 VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3), 130 VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4), 131 VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3), 132 VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState), 133 VMSTATE_END_OF_LIST() 134 }, 135 .subsections = (const VMStateDescription * []) { 136 &vmstate_gicv3_cpu_virt, 137 NULL 138 }, 139 .subsections = (const VMStateDescription * []) { 140 &vmstate_gicv3_cpu_sre_el1, 141 NULL 142 } 143 }; 144 145 static int gicv3_gicd_no_migration_shift_bug_pre_load(void *opaque) 146 { 147 GICv3State *cs = opaque; 148 149 /* 150 * The gicd_no_migration_shift_bug flag is used for migration compatibility 151 * for old version QEMU which may have the GICD bmp shift bug under KVM mode. 152 * Strictly, what we want to know is whether the migration source is using 153 * KVM. Since we don't have any way to determine that, we look at whether the 154 * destination is using KVM; this is close enough because for the older QEMU 155 * versions with this bug KVM -> TCG migration didn't work anyway. If the 156 * source is a newer QEMU without this bug it will transmit the migration 157 * subsection which sets the flag to true; otherwise it will remain set to 158 * the value we select here. 159 */ 160 if (kvm_enabled()) { 161 cs->gicd_no_migration_shift_bug = false; 162 } 163 164 return 0; 165 } 166 167 static int gicv3_gicd_no_migration_shift_bug_post_load(void *opaque, 168 int version_id) 169 { 170 GICv3State *cs = opaque; 171 172 if (cs->gicd_no_migration_shift_bug) { 173 return 0; 174 } 175 176 /* Older versions of QEMU had a bug in the handling of state save/restore 177 * to the KVM GICv3: they got the offset in the bitmap arrays wrong, 178 * so that instead of the data for external interrupts 32 and up 179 * starting at bit position 32 in the bitmap, it started at bit 180 * position 64. If we're receiving data from a QEMU with that bug, 181 * we must move the data down into the right place. 182 */ 183 memmove(cs->group, (uint8_t *)cs->group + GIC_INTERNAL / 8, 184 sizeof(cs->group) - GIC_INTERNAL / 8); 185 memmove(cs->grpmod, (uint8_t *)cs->grpmod + GIC_INTERNAL / 8, 186 sizeof(cs->grpmod) - GIC_INTERNAL / 8); 187 memmove(cs->enabled, (uint8_t *)cs->enabled + GIC_INTERNAL / 8, 188 sizeof(cs->enabled) - GIC_INTERNAL / 8); 189 memmove(cs->pending, (uint8_t *)cs->pending + GIC_INTERNAL / 8, 190 sizeof(cs->pending) - GIC_INTERNAL / 8); 191 memmove(cs->active, (uint8_t *)cs->active + GIC_INTERNAL / 8, 192 sizeof(cs->active) - GIC_INTERNAL / 8); 193 memmove(cs->edge_trigger, (uint8_t *)cs->edge_trigger + GIC_INTERNAL / 8, 194 sizeof(cs->edge_trigger) - GIC_INTERNAL / 8); 195 196 /* 197 * While this new version QEMU doesn't have this kind of bug as we fix it, 198 * so it needs to set the flag to true to indicate that and it's necessary 199 * for next migration to work from this new version QEMU. 200 */ 201 cs->gicd_no_migration_shift_bug = true; 202 203 return 0; 204 } 205 206 static bool needed_always(void *opaque) 207 { 208 return true; 209 } 210 211 const VMStateDescription vmstate_gicv3_gicd_no_migration_shift_bug = { 212 .name = "arm_gicv3/gicd_no_migration_shift_bug", 213 .version_id = 1, 214 .minimum_version_id = 1, 215 .needed = needed_always, 216 .pre_load = gicv3_gicd_no_migration_shift_bug_pre_load, 217 .post_load = gicv3_gicd_no_migration_shift_bug_post_load, 218 .fields = (VMStateField[]) { 219 VMSTATE_BOOL(gicd_no_migration_shift_bug, GICv3State), 220 VMSTATE_END_OF_LIST() 221 } 222 }; 223 224 static const VMStateDescription vmstate_gicv3 = { 225 .name = "arm_gicv3", 226 .version_id = 1, 227 .minimum_version_id = 1, 228 .pre_save = gicv3_pre_save, 229 .post_load = gicv3_post_load, 230 .priority = MIG_PRI_GICV3, 231 .fields = (VMStateField[]) { 232 VMSTATE_UINT32(gicd_ctlr, GICv3State), 233 VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2), 234 VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE), 235 VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE), 236 VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE), 237 VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE), 238 VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE), 239 VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE), 240 VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE), 241 VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ), 242 VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ), 243 VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State, 244 DIV_ROUND_UP(GICV3_MAXIRQ, 16)), 245 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu, 246 vmstate_gicv3_cpu, GICv3CPUState), 247 VMSTATE_END_OF_LIST() 248 }, 249 .subsections = (const VMStateDescription * []) { 250 &vmstate_gicv3_gicd_no_migration_shift_bug, 251 NULL 252 } 253 }; 254 255 void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler, 256 const MemoryRegionOps *ops, Error **errp) 257 { 258 SysBusDevice *sbd = SYS_BUS_DEVICE(s); 259 int rdist_capacity = 0; 260 int i; 261 262 for (i = 0; i < s->nb_redist_regions; i++) { 263 rdist_capacity += s->redist_region_count[i]; 264 } 265 if (rdist_capacity < s->num_cpu) { 266 error_setg(errp, "Capacity of the redist regions(%d) " 267 "is less than number of vcpus(%d)", 268 rdist_capacity, s->num_cpu); 269 return; 270 } 271 272 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU. 273 * GPIO array layout is thus: 274 * [0..N-1] spi 275 * [N..N+31] PPIs for CPU 0 276 * [N+32..N+63] PPIs for CPU 1 277 * ... 278 */ 279 i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu; 280 qdev_init_gpio_in(DEVICE(s), handler, i); 281 282 for (i = 0; i < s->num_cpu; i++) { 283 sysbus_init_irq(sbd, &s->cpu[i].parent_irq); 284 } 285 for (i = 0; i < s->num_cpu; i++) { 286 sysbus_init_irq(sbd, &s->cpu[i].parent_fiq); 287 } 288 for (i = 0; i < s->num_cpu; i++) { 289 sysbus_init_irq(sbd, &s->cpu[i].parent_virq); 290 } 291 for (i = 0; i < s->num_cpu; i++) { 292 sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq); 293 } 294 295 memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s, 296 "gicv3_dist", 0x10000); 297 sysbus_init_mmio(sbd, &s->iomem_dist); 298 299 s->iomem_redist = g_new0(MemoryRegion, s->nb_redist_regions); 300 for (i = 0; i < s->nb_redist_regions; i++) { 301 char *name = g_strdup_printf("gicv3_redist_region[%d]", i); 302 303 memory_region_init_io(&s->iomem_redist[i], OBJECT(s), 304 ops ? &ops[1] : NULL, s, name, 305 s->redist_region_count[i] * GICV3_REDIST_SIZE); 306 sysbus_init_mmio(sbd, &s->iomem_redist[i]); 307 g_free(name); 308 } 309 } 310 311 static void arm_gicv3_common_realize(DeviceState *dev, Error **errp) 312 { 313 GICv3State *s = ARM_GICV3_COMMON(dev); 314 int i; 315 316 /* revision property is actually reserved and currently used only in order 317 * to keep the interface compatible with GICv2 code, avoiding extra 318 * conditions. However, in future it could be used, for example, if we 319 * implement GICv4. 320 */ 321 if (s->revision != 3) { 322 error_setg(errp, "unsupported GIC revision %d", s->revision); 323 return; 324 } 325 326 if (s->num_irq > GICV3_MAXIRQ) { 327 error_setg(errp, 328 "requested %u interrupt lines exceeds GIC maximum %d", 329 s->num_irq, GICV3_MAXIRQ); 330 return; 331 } 332 if (s->num_irq < GIC_INTERNAL) { 333 error_setg(errp, 334 "requested %u interrupt lines is below GIC minimum %d", 335 s->num_irq, GIC_INTERNAL); 336 return; 337 } 338 339 /* ITLinesNumber is represented as (N / 32) - 1, so this is an 340 * implementation imposed restriction, not an architectural one, 341 * so we don't have to deal with bitfields where only some of the 342 * bits in a 32-bit word should be valid. 343 */ 344 if (s->num_irq % 32) { 345 error_setg(errp, 346 "%d interrupt lines unsupported: not divisible by 32", 347 s->num_irq); 348 return; 349 } 350 351 s->cpu = g_new0(GICv3CPUState, s->num_cpu); 352 353 for (i = 0; i < s->num_cpu; i++) { 354 CPUState *cpu = qemu_get_cpu(i); 355 uint64_t cpu_affid; 356 int last; 357 358 s->cpu[i].cpu = cpu; 359 s->cpu[i].gic = s; 360 /* Store GICv3CPUState in CPUARMState gicv3state pointer */ 361 gicv3_set_gicv3state(cpu, &s->cpu[i]); 362 363 /* Pre-construct the GICR_TYPER: 364 * For our implementation: 365 * Top 32 bits are the affinity value of the associated CPU 366 * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table) 367 * Processor_Number == CPU index starting from 0 368 * DPGS == 0 (GICR_CTLR.DPG* not supported) 369 * Last == 1 if this is the last redistributor in a series of 370 * contiguous redistributor pages 371 * DirectLPI == 0 (direct injection of LPIs not supported) 372 * VLPIS == 0 (virtual LPIs not supported) 373 * PLPIS == 0 (physical LPIs not supported) 374 */ 375 cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL); 376 last = (i == s->num_cpu - 1); 377 378 /* The CPU mp-affinity property is in MPIDR register format; squash 379 * the affinity bytes into 32 bits as the GICR_TYPER has them. 380 */ 381 cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) | 382 (cpu_affid & 0xFFFFFF); 383 s->cpu[i].gicr_typer = (cpu_affid << 32) | 384 (1 << 24) | 385 (i << 8) | 386 (last << 4); 387 } 388 } 389 390 static void arm_gicv3_finalize(Object *obj) 391 { 392 GICv3State *s = ARM_GICV3_COMMON(obj); 393 394 g_free(s->redist_region_count); 395 } 396 397 static void arm_gicv3_common_reset(DeviceState *dev) 398 { 399 GICv3State *s = ARM_GICV3_COMMON(dev); 400 int i; 401 402 for (i = 0; i < s->num_cpu; i++) { 403 GICv3CPUState *cs = &s->cpu[i]; 404 405 cs->level = 0; 406 cs->gicr_ctlr = 0; 407 cs->gicr_statusr[GICV3_S] = 0; 408 cs->gicr_statusr[GICV3_NS] = 0; 409 cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep; 410 cs->gicr_propbaser = 0; 411 cs->gicr_pendbaser = 0; 412 /* If we're resetting a TZ-aware GIC as if secure firmware 413 * had set it up ready to start a kernel in non-secure, we 414 * need to set interrupts to group 1 so the kernel can use them. 415 * Otherwise they reset to group 0 like the hardware. 416 */ 417 if (s->irq_reset_nonsecure) { 418 cs->gicr_igroupr0 = 0xffffffff; 419 } else { 420 cs->gicr_igroupr0 = 0; 421 } 422 423 cs->gicr_ienabler0 = 0; 424 cs->gicr_ipendr0 = 0; 425 cs->gicr_iactiver0 = 0; 426 cs->edge_trigger = 0xffff; 427 cs->gicr_igrpmodr0 = 0; 428 cs->gicr_nsacr = 0; 429 memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr)); 430 431 cs->hppi.prio = 0xff; 432 433 /* State in the CPU interface must *not* be reset here, because it 434 * is part of the CPU's reset domain, not the GIC device's. 435 */ 436 } 437 438 /* For our implementation affinity routing is always enabled */ 439 if (s->security_extn) { 440 s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS; 441 } else { 442 s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE; 443 } 444 445 s->gicd_statusr[GICV3_S] = 0; 446 s->gicd_statusr[GICV3_NS] = 0; 447 448 memset(s->group, 0, sizeof(s->group)); 449 memset(s->grpmod, 0, sizeof(s->grpmod)); 450 memset(s->enabled, 0, sizeof(s->enabled)); 451 memset(s->pending, 0, sizeof(s->pending)); 452 memset(s->active, 0, sizeof(s->active)); 453 memset(s->level, 0, sizeof(s->level)); 454 memset(s->edge_trigger, 0, sizeof(s->edge_trigger)); 455 memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority)); 456 memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter)); 457 memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr)); 458 /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must 459 * write these to get sane behaviour and we need not populate the 460 * pointer cache here; however having the cache be different for 461 * "happened to be 0 from reset" and "guest wrote 0" would be 462 * too confusing. 463 */ 464 gicv3_cache_all_target_cpustates(s); 465 466 if (s->irq_reset_nonsecure) { 467 /* If we're resetting a TZ-aware GIC as if secure firmware 468 * had set it up ready to start a kernel in non-secure, we 469 * need to set interrupts to group 1 so the kernel can use them. 470 * Otherwise they reset to group 0 like the hardware. 471 */ 472 for (i = GIC_INTERNAL; i < s->num_irq; i++) { 473 gicv3_gicd_group_set(s, i); 474 } 475 } 476 s->gicd_no_migration_shift_bug = true; 477 } 478 479 static void arm_gic_common_linux_init(ARMLinuxBootIf *obj, 480 bool secure_boot) 481 { 482 GICv3State *s = ARM_GICV3_COMMON(obj); 483 484 if (s->security_extn && !secure_boot) { 485 /* We're directly booting a kernel into NonSecure. If this GIC 486 * implements the security extensions then we must configure it 487 * to have all the interrupts be NonSecure (this is a job that 488 * is done by the Secure boot firmware in real hardware, and in 489 * this mode QEMU is acting as a minimalist firmware-and-bootloader 490 * equivalent). 491 */ 492 s->irq_reset_nonsecure = true; 493 } 494 } 495 496 static Property arm_gicv3_common_properties[] = { 497 DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1), 498 DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32), 499 DEFINE_PROP_UINT32("revision", GICv3State, revision, 3), 500 DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0), 501 DEFINE_PROP_ARRAY("redist-region-count", GICv3State, nb_redist_regions, 502 redist_region_count, qdev_prop_uint32, uint32_t), 503 DEFINE_PROP_END_OF_LIST(), 504 }; 505 506 static void arm_gicv3_common_class_init(ObjectClass *klass, void *data) 507 { 508 DeviceClass *dc = DEVICE_CLASS(klass); 509 ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass); 510 511 dc->reset = arm_gicv3_common_reset; 512 dc->realize = arm_gicv3_common_realize; 513 dc->props = arm_gicv3_common_properties; 514 dc->vmsd = &vmstate_gicv3; 515 albifc->arm_linux_init = arm_gic_common_linux_init; 516 } 517 518 static const TypeInfo arm_gicv3_common_type = { 519 .name = TYPE_ARM_GICV3_COMMON, 520 .parent = TYPE_SYS_BUS_DEVICE, 521 .instance_size = sizeof(GICv3State), 522 .class_size = sizeof(ARMGICv3CommonClass), 523 .class_init = arm_gicv3_common_class_init, 524 .instance_finalize = arm_gicv3_finalize, 525 .abstract = true, 526 .interfaces = (InterfaceInfo []) { 527 { TYPE_ARM_LINUX_BOOT_IF }, 528 { }, 529 }, 530 }; 531 532 static void register_types(void) 533 { 534 type_register_static(&arm_gicv3_common_type); 535 } 536 537 type_init(register_types) 538