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