1 /* 2 * QEMU PowerPC sPAPR IRQ interface 3 * 4 * Copyright (c) 2018, IBM Corporation. 5 * 6 * This code is licensed under the GPL version 2 or later. See the 7 * COPYING file in the top-level directory. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "qemu/log.h" 12 #include "qemu/error-report.h" 13 #include "qapi/error.h" 14 #include "hw/irq.h" 15 #include "hw/ppc/spapr.h" 16 #include "hw/ppc/spapr_cpu_core.h" 17 #include "hw/ppc/spapr_xive.h" 18 #include "hw/ppc/xics.h" 19 #include "hw/ppc/xics_spapr.h" 20 #include "hw/qdev-properties.h" 21 #include "cpu-models.h" 22 #include "sysemu/kvm.h" 23 24 #include "trace.h" 25 26 static const TypeInfo spapr_intc_info = { 27 .name = TYPE_SPAPR_INTC, 28 .parent = TYPE_INTERFACE, 29 .class_size = sizeof(SpaprInterruptControllerClass), 30 }; 31 32 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis) 33 { 34 spapr->irq_map_nr = nr_msis; 35 spapr->irq_map = bitmap_new(spapr->irq_map_nr); 36 } 37 38 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align, 39 Error **errp) 40 { 41 int irq; 42 43 /* 44 * The 'align_mask' parameter of bitmap_find_next_zero_area() 45 * should be one less than a power of 2; 0 means no 46 * alignment. Adapt the 'align' value of the former allocator 47 * to fit the requirements of bitmap_find_next_zero_area() 48 */ 49 align -= 1; 50 51 irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num, 52 align); 53 if (irq == spapr->irq_map_nr) { 54 error_setg(errp, "can't find a free %d-IRQ block", num); 55 return -1; 56 } 57 58 bitmap_set(spapr->irq_map, irq, num); 59 60 return irq + SPAPR_IRQ_MSI; 61 } 62 63 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num) 64 { 65 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num); 66 } 67 68 static void spapr_irq_init_kvm(SpaprMachineState *spapr, 69 SpaprIrq *irq, Error **errp) 70 { 71 MachineState *machine = MACHINE(spapr); 72 Error *local_err = NULL; 73 74 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) { 75 irq->init_kvm(spapr, &local_err); 76 if (local_err && machine_kernel_irqchip_required(machine)) { 77 error_prepend(&local_err, 78 "kernel_irqchip requested but unavailable: "); 79 error_propagate(errp, local_err); 80 return; 81 } 82 83 if (!local_err) { 84 return; 85 } 86 87 /* 88 * We failed to initialize the KVM device, fallback to 89 * emulated mode 90 */ 91 error_prepend(&local_err, "kernel_irqchip allowed but unavailable: "); 92 error_append_hint(&local_err, "Falling back to kernel-irqchip=off\n"); 93 warn_report_err(local_err); 94 } 95 } 96 97 /* 98 * XICS IRQ backend. 99 */ 100 101 static int spapr_irq_post_load_xics(SpaprMachineState *spapr, int version_id) 102 { 103 if (!kvm_irqchip_in_kernel()) { 104 CPUState *cs; 105 CPU_FOREACH(cs) { 106 PowerPCCPU *cpu = POWERPC_CPU(cs); 107 icp_resend(spapr_cpu_state(cpu)->icp); 108 } 109 } 110 return 0; 111 } 112 113 static void spapr_irq_reset_xics(SpaprMachineState *spapr, Error **errp) 114 { 115 Error *local_err = NULL; 116 117 spapr_irq_init_kvm(spapr, &spapr_irq_xics, &local_err); 118 if (local_err) { 119 error_propagate(errp, local_err); 120 return; 121 } 122 } 123 124 static void spapr_irq_init_kvm_xics(SpaprMachineState *spapr, Error **errp) 125 { 126 if (kvm_enabled()) { 127 xics_kvm_connect(spapr, errp); 128 } 129 } 130 131 SpaprIrq spapr_irq_xics = { 132 .nr_xirqs = SPAPR_NR_XIRQS, 133 .nr_msis = SPAPR_NR_MSIS, 134 .xics = true, 135 .xive = false, 136 137 .dt_populate = spapr_dt_xics, 138 .post_load = spapr_irq_post_load_xics, 139 .reset = spapr_irq_reset_xics, 140 .init_kvm = spapr_irq_init_kvm_xics, 141 }; 142 143 /* 144 * XIVE IRQ backend. 145 */ 146 147 static int spapr_irq_post_load_xive(SpaprMachineState *spapr, int version_id) 148 { 149 return spapr_xive_post_load(spapr->xive, version_id); 150 } 151 152 static void spapr_irq_reset_xive(SpaprMachineState *spapr, Error **errp) 153 { 154 CPUState *cs; 155 Error *local_err = NULL; 156 157 CPU_FOREACH(cs) { 158 PowerPCCPU *cpu = POWERPC_CPU(cs); 159 160 /* (TCG) Set the OS CAM line of the thread interrupt context. */ 161 spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx); 162 } 163 164 spapr_irq_init_kvm(spapr, &spapr_irq_xive, &local_err); 165 if (local_err) { 166 error_propagate(errp, local_err); 167 return; 168 } 169 170 /* Activate the XIVE MMIOs */ 171 spapr_xive_mmio_set_enabled(spapr->xive, true); 172 } 173 174 static void spapr_irq_init_kvm_xive(SpaprMachineState *spapr, Error **errp) 175 { 176 if (kvm_enabled()) { 177 kvmppc_xive_connect(spapr->xive, errp); 178 } 179 } 180 181 SpaprIrq spapr_irq_xive = { 182 .nr_xirqs = SPAPR_NR_XIRQS, 183 .nr_msis = SPAPR_NR_MSIS, 184 .xics = false, 185 .xive = true, 186 187 .dt_populate = spapr_dt_xive, 188 .post_load = spapr_irq_post_load_xive, 189 .reset = spapr_irq_reset_xive, 190 .init_kvm = spapr_irq_init_kvm_xive, 191 }; 192 193 /* 194 * Dual XIVE and XICS IRQ backend. 195 * 196 * Both interrupt mode, XIVE and XICS, objects are created but the 197 * machine starts in legacy interrupt mode (XICS). It can be changed 198 * by the CAS negotiation process and, in that case, the new mode is 199 * activated after an extra machine reset. 200 */ 201 202 /* 203 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the 204 * default. 205 */ 206 static SpaprIrq *spapr_irq_current(SpaprMachineState *spapr) 207 { 208 return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ? 209 &spapr_irq_xive : &spapr_irq_xics; 210 } 211 212 static void spapr_irq_dt_populate_dual(SpaprMachineState *spapr, 213 uint32_t nr_servers, void *fdt, 214 uint32_t phandle) 215 { 216 spapr_irq_current(spapr)->dt_populate(spapr, nr_servers, fdt, phandle); 217 } 218 219 static int spapr_irq_post_load_dual(SpaprMachineState *spapr, int version_id) 220 { 221 /* 222 * Force a reset of the XIVE backend after migration. The machine 223 * defaults to XICS at startup. 224 */ 225 if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 226 if (kvm_irqchip_in_kernel()) { 227 xics_kvm_disconnect(spapr, &error_fatal); 228 } 229 spapr_irq_xive.reset(spapr, &error_fatal); 230 } 231 232 return spapr_irq_current(spapr)->post_load(spapr, version_id); 233 } 234 235 static void spapr_irq_reset_dual(SpaprMachineState *spapr, Error **errp) 236 { 237 Error *local_err = NULL; 238 239 /* 240 * Deactivate the XIVE MMIOs. The XIVE backend will reenable them 241 * if selected. 242 */ 243 spapr_xive_mmio_set_enabled(spapr->xive, false); 244 245 /* Destroy all KVM devices */ 246 if (kvm_irqchip_in_kernel()) { 247 xics_kvm_disconnect(spapr, &local_err); 248 if (local_err) { 249 error_propagate(errp, local_err); 250 error_prepend(errp, "KVM XICS disconnect failed: "); 251 return; 252 } 253 kvmppc_xive_disconnect(spapr->xive, &local_err); 254 if (local_err) { 255 error_propagate(errp, local_err); 256 error_prepend(errp, "KVM XIVE disconnect failed: "); 257 return; 258 } 259 } 260 261 spapr_irq_current(spapr)->reset(spapr, errp); 262 } 263 264 /* 265 * Define values in sync with the XIVE and XICS backend 266 */ 267 SpaprIrq spapr_irq_dual = { 268 .nr_xirqs = SPAPR_NR_XIRQS, 269 .nr_msis = SPAPR_NR_MSIS, 270 .xics = true, 271 .xive = true, 272 273 .dt_populate = spapr_irq_dt_populate_dual, 274 .post_load = spapr_irq_post_load_dual, 275 .reset = spapr_irq_reset_dual, 276 .init_kvm = NULL, /* should not be used */ 277 }; 278 279 280 static int spapr_irq_check(SpaprMachineState *spapr, Error **errp) 281 { 282 MachineState *machine = MACHINE(spapr); 283 284 /* 285 * Sanity checks on non-P9 machines. On these, XIVE is not 286 * advertised, see spapr_dt_ov5_platform_support() 287 */ 288 if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 289 0, spapr->max_compat_pvr)) { 290 /* 291 * If the 'dual' interrupt mode is selected, force XICS as CAS 292 * negotiation is useless. 293 */ 294 if (spapr->irq == &spapr_irq_dual) { 295 spapr->irq = &spapr_irq_xics; 296 return 0; 297 } 298 299 /* 300 * Non-P9 machines using only XIVE is a bogus setup. We have two 301 * scenarios to take into account because of the compat mode: 302 * 303 * 1. POWER7/8 machines should fail to init later on when creating 304 * the XIVE interrupt presenters because a POWER9 exception 305 * model is required. 306 307 * 2. POWER9 machines using the POWER8 compat mode won't fail and 308 * will let the OS boot with a partial XIVE setup : DT 309 * properties but no hcalls. 310 * 311 * To cover both and not confuse the OS, add an early failure in 312 * QEMU. 313 */ 314 if (spapr->irq == &spapr_irq_xive) { 315 error_setg(errp, "XIVE-only machines require a POWER9 CPU"); 316 return -1; 317 } 318 } 319 320 /* 321 * On a POWER9 host, some older KVM XICS devices cannot be destroyed and 322 * re-created. Detect that early to avoid QEMU to exit later when the 323 * guest reboots. 324 */ 325 if (kvm_enabled() && 326 spapr->irq == &spapr_irq_dual && 327 machine_kernel_irqchip_required(machine) && 328 xics_kvm_has_broken_disconnect(spapr)) { 329 error_setg(errp, "KVM is too old to support ic-mode=dual,kernel-irqchip=on"); 330 return -1; 331 } 332 333 return 0; 334 } 335 336 /* 337 * sPAPR IRQ frontend routines for devices 338 */ 339 #define ALL_INTCS(spapr_) \ 340 { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), } 341 342 int spapr_irq_cpu_intc_create(SpaprMachineState *spapr, 343 PowerPCCPU *cpu, Error **errp) 344 { 345 SpaprInterruptController *intcs[] = ALL_INTCS(spapr); 346 int i; 347 int rc; 348 349 for (i = 0; i < ARRAY_SIZE(intcs); i++) { 350 SpaprInterruptController *intc = intcs[i]; 351 if (intc) { 352 SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc); 353 rc = sicc->cpu_intc_create(intc, cpu, errp); 354 if (rc < 0) { 355 return rc; 356 } 357 } 358 } 359 360 return 0; 361 } 362 363 static void spapr_set_irq(void *opaque, int irq, int level) 364 { 365 SpaprMachineState *spapr = SPAPR_MACHINE(opaque); 366 SpaprInterruptControllerClass *sicc 367 = SPAPR_INTC_GET_CLASS(spapr->active_intc); 368 369 sicc->set_irq(spapr->active_intc, irq, level); 370 } 371 372 void spapr_irq_print_info(SpaprMachineState *spapr, Monitor *mon) 373 { 374 SpaprInterruptControllerClass *sicc 375 = SPAPR_INTC_GET_CLASS(spapr->active_intc); 376 377 sicc->print_info(spapr->active_intc, mon); 378 } 379 380 void spapr_irq_init(SpaprMachineState *spapr, Error **errp) 381 { 382 MachineState *machine = MACHINE(spapr); 383 384 if (machine_kernel_irqchip_split(machine)) { 385 error_setg(errp, "kernel_irqchip split mode not supported on pseries"); 386 return; 387 } 388 389 if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) { 390 error_setg(errp, 391 "kernel_irqchip requested but only available with KVM"); 392 return; 393 } 394 395 if (spapr_irq_check(spapr, errp) < 0) { 396 return; 397 } 398 399 /* Initialize the MSI IRQ allocator. */ 400 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) { 401 spapr_irq_msi_init(spapr, spapr->irq->nr_msis); 402 } 403 404 if (spapr->irq->xics) { 405 Error *local_err = NULL; 406 Object *obj; 407 408 obj = object_new(TYPE_ICS_SPAPR); 409 object_property_add_child(OBJECT(spapr), "ics", obj, &local_err); 410 if (local_err) { 411 error_propagate(errp, local_err); 412 return; 413 } 414 415 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr), 416 &local_err); 417 if (local_err) { 418 error_propagate(errp, local_err); 419 return; 420 } 421 422 object_property_set_int(obj, spapr->irq->nr_xirqs, "nr-irqs", 423 &local_err); 424 if (local_err) { 425 error_propagate(errp, local_err); 426 return; 427 } 428 429 object_property_set_bool(obj, true, "realized", &local_err); 430 if (local_err) { 431 error_propagate(errp, local_err); 432 return; 433 } 434 435 spapr->ics = ICS_SPAPR(obj); 436 } 437 438 if (spapr->irq->xive) { 439 uint32_t nr_servers = spapr_max_server_number(spapr); 440 DeviceState *dev; 441 int i; 442 443 dev = qdev_create(NULL, TYPE_SPAPR_XIVE); 444 qdev_prop_set_uint32(dev, "nr-irqs", 445 spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE); 446 /* 447 * 8 XIVE END structures per CPU. One for each available 448 * priority 449 */ 450 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3); 451 qdev_init_nofail(dev); 452 453 spapr->xive = SPAPR_XIVE(dev); 454 455 /* Enable the CPU IPIs */ 456 for (i = 0; i < nr_servers; ++i) { 457 SpaprInterruptControllerClass *sicc 458 = SPAPR_INTC_GET_CLASS(spapr->xive); 459 460 if (sicc->claim_irq(SPAPR_INTC(spapr->xive), SPAPR_IRQ_IPI + i, 461 false, errp) < 0) { 462 return; 463 } 464 } 465 466 spapr_xive_hcall_init(spapr); 467 } 468 469 spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr, 470 spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE); 471 } 472 473 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp) 474 { 475 SpaprInterruptController *intcs[] = ALL_INTCS(spapr); 476 int i; 477 int rc; 478 479 assert(irq >= SPAPR_XIRQ_BASE); 480 assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE)); 481 482 for (i = 0; i < ARRAY_SIZE(intcs); i++) { 483 SpaprInterruptController *intc = intcs[i]; 484 if (intc) { 485 SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc); 486 rc = sicc->claim_irq(intc, irq, lsi, errp); 487 if (rc < 0) { 488 return rc; 489 } 490 } 491 } 492 493 return 0; 494 } 495 496 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num) 497 { 498 SpaprInterruptController *intcs[] = ALL_INTCS(spapr); 499 int i, j; 500 501 assert(irq >= SPAPR_XIRQ_BASE); 502 assert((irq + num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE)); 503 504 for (i = irq; i < (irq + num); i++) { 505 for (j = 0; j < ARRAY_SIZE(intcs); j++) { 506 SpaprInterruptController *intc = intcs[j]; 507 508 if (intc) { 509 SpaprInterruptControllerClass *sicc 510 = SPAPR_INTC_GET_CLASS(intc); 511 sicc->free_irq(intc, i); 512 } 513 } 514 } 515 } 516 517 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq) 518 { 519 /* 520 * This interface is basically for VIO and PHB devices to find the 521 * right qemu_irq to manipulate, so we only allow access to the 522 * external irqs for now. Currently anything which needs to 523 * access the IPIs most naturally gets there via the guest side 524 * interfaces, we can change this if we need to in future. 525 */ 526 assert(irq >= SPAPR_XIRQ_BASE); 527 assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE)); 528 529 if (spapr->ics) { 530 assert(ics_valid_irq(spapr->ics, irq)); 531 } 532 if (spapr->xive) { 533 assert(irq < spapr->xive->nr_irqs); 534 assert(xive_eas_is_valid(&spapr->xive->eat[irq])); 535 } 536 537 return spapr->qirqs[irq]; 538 } 539 540 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id) 541 { 542 spapr_irq_update_active_intc(spapr); 543 return spapr->irq->post_load(spapr, version_id); 544 } 545 546 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp) 547 { 548 assert(!spapr->irq_map || bitmap_empty(spapr->irq_map, spapr->irq_map_nr)); 549 550 spapr_irq_update_active_intc(spapr); 551 552 if (spapr->irq->reset) { 553 spapr->irq->reset(spapr, errp); 554 } 555 } 556 557 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp) 558 { 559 const char *nodename = "interrupt-controller"; 560 int offset, phandle; 561 562 offset = fdt_subnode_offset(fdt, 0, nodename); 563 if (offset < 0) { 564 error_setg(errp, "Can't find node \"%s\": %s", 565 nodename, fdt_strerror(offset)); 566 return -1; 567 } 568 569 phandle = fdt_get_phandle(fdt, offset); 570 if (!phandle) { 571 error_setg(errp, "Can't get phandle of node \"%s\"", nodename); 572 return -1; 573 } 574 575 return phandle; 576 } 577 578 static void set_active_intc(SpaprMachineState *spapr, 579 SpaprInterruptController *new_intc) 580 { 581 SpaprInterruptControllerClass *sicc; 582 583 assert(new_intc); 584 585 if (new_intc == spapr->active_intc) { 586 /* Nothing to do */ 587 return; 588 } 589 590 if (spapr->active_intc) { 591 sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc); 592 if (sicc->deactivate) { 593 sicc->deactivate(spapr->active_intc); 594 } 595 } 596 597 sicc = SPAPR_INTC_GET_CLASS(new_intc); 598 if (sicc->activate) { 599 sicc->activate(new_intc, &error_fatal); 600 } 601 602 spapr->active_intc = new_intc; 603 } 604 605 void spapr_irq_update_active_intc(SpaprMachineState *spapr) 606 { 607 SpaprInterruptController *new_intc; 608 609 if (!spapr->ics) { 610 /* 611 * XXX before we run CAS, ov5_cas is initialized empty, which 612 * indicates XICS, even if we have ic-mode=xive. TODO: clean 613 * up the CAS path so that we have a clearer way of handling 614 * this. 615 */ 616 new_intc = SPAPR_INTC(spapr->xive); 617 } else if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 618 new_intc = SPAPR_INTC(spapr->xive); 619 } else { 620 new_intc = SPAPR_INTC(spapr->ics); 621 } 622 623 set_active_intc(spapr, new_intc); 624 } 625 626 /* 627 * XICS legacy routines - to deprecate one day 628 */ 629 630 static int ics_find_free_block(ICSState *ics, int num, int alignnum) 631 { 632 int first, i; 633 634 for (first = 0; first < ics->nr_irqs; first += alignnum) { 635 if (num > (ics->nr_irqs - first)) { 636 return -1; 637 } 638 for (i = first; i < first + num; ++i) { 639 if (!ics_irq_free(ics, i)) { 640 break; 641 } 642 } 643 if (i == (first + num)) { 644 return first; 645 } 646 } 647 648 return -1; 649 } 650 651 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp) 652 { 653 ICSState *ics = spapr->ics; 654 int first = -1; 655 656 assert(ics); 657 658 /* 659 * MSIMesage::data is used for storing VIRQ so 660 * it has to be aligned to num to support multiple 661 * MSI vectors. MSI-X is not affected by this. 662 * The hint is used for the first IRQ, the rest should 663 * be allocated continuously. 664 */ 665 if (align) { 666 assert((num == 1) || (num == 2) || (num == 4) || 667 (num == 8) || (num == 16) || (num == 32)); 668 first = ics_find_free_block(ics, num, num); 669 } else { 670 first = ics_find_free_block(ics, num, 1); 671 } 672 673 if (first < 0) { 674 error_setg(errp, "can't find a free %d-IRQ block", num); 675 return -1; 676 } 677 678 return first + ics->offset; 679 } 680 681 #define SPAPR_IRQ_XICS_LEGACY_NR_XIRQS 0x400 682 683 SpaprIrq spapr_irq_xics_legacy = { 684 .nr_xirqs = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS, 685 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS, 686 .xics = true, 687 .xive = false, 688 689 .dt_populate = spapr_dt_xics, 690 .post_load = spapr_irq_post_load_xics, 691 .reset = spapr_irq_reset_xics, 692 .init_kvm = spapr_irq_init_kvm_xics, 693 }; 694 695 static void spapr_irq_register_types(void) 696 { 697 type_register_static(&spapr_intc_info); 698 } 699 700 type_init(spapr_irq_register_types) 701