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/ppc/spapr.h" 15 #include "hw/ppc/spapr_cpu_core.h" 16 #include "hw/ppc/spapr_xive.h" 17 #include "hw/ppc/xics.h" 18 #include "hw/ppc/xics_spapr.h" 19 #include "cpu-models.h" 20 #include "sysemu/kvm.h" 21 22 #include "trace.h" 23 24 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis) 25 { 26 spapr->irq_map_nr = nr_msis; 27 spapr->irq_map = bitmap_new(spapr->irq_map_nr); 28 } 29 30 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align, 31 Error **errp) 32 { 33 int irq; 34 35 /* 36 * The 'align_mask' parameter of bitmap_find_next_zero_area() 37 * should be one less than a power of 2; 0 means no 38 * alignment. Adapt the 'align' value of the former allocator 39 * to fit the requirements of bitmap_find_next_zero_area() 40 */ 41 align -= 1; 42 43 irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num, 44 align); 45 if (irq == spapr->irq_map_nr) { 46 error_setg(errp, "can't find a free %d-IRQ block", num); 47 return -1; 48 } 49 50 bitmap_set(spapr->irq_map, irq, num); 51 52 return irq + SPAPR_IRQ_MSI; 53 } 54 55 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num) 56 { 57 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num); 58 } 59 60 void spapr_irq_msi_reset(SpaprMachineState *spapr) 61 { 62 bitmap_clear(spapr->irq_map, 0, spapr->irq_map_nr); 63 } 64 65 66 /* 67 * XICS IRQ backend. 68 */ 69 70 static void spapr_irq_init_xics(SpaprMachineState *spapr, int nr_irqs, 71 Error **errp) 72 { 73 MachineState *machine = MACHINE(spapr); 74 Object *obj; 75 Error *local_err = NULL; 76 bool xics_kvm = false; 77 78 if (kvm_enabled()) { 79 if (machine_kernel_irqchip_allowed(machine) && 80 !xics_kvm_init(spapr, &local_err)) { 81 xics_kvm = true; 82 } 83 if (machine_kernel_irqchip_required(machine) && !xics_kvm) { 84 error_prepend(&local_err, 85 "kernel_irqchip requested but unavailable: "); 86 error_propagate(errp, local_err); 87 return; 88 } 89 error_free(local_err); 90 local_err = NULL; 91 } 92 93 if (!xics_kvm) { 94 xics_spapr_init(spapr); 95 } 96 97 obj = object_new(TYPE_ICS_SIMPLE); 98 object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort); 99 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr), 100 &error_fatal); 101 object_property_set_int(obj, nr_irqs, "nr-irqs", &error_fatal); 102 object_property_set_bool(obj, true, "realized", &local_err); 103 if (local_err) { 104 error_propagate(errp, local_err); 105 return; 106 } 107 108 spapr->ics = ICS_BASE(obj); 109 } 110 111 #define ICS_IRQ_FREE(ics, srcno) \ 112 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK))) 113 114 static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi, 115 Error **errp) 116 { 117 ICSState *ics = spapr->ics; 118 119 assert(ics); 120 121 if (!ics_valid_irq(ics, irq)) { 122 error_setg(errp, "IRQ %d is invalid", irq); 123 return -1; 124 } 125 126 if (!ICS_IRQ_FREE(ics, irq - ics->offset)) { 127 error_setg(errp, "IRQ %d is not free", irq); 128 return -1; 129 } 130 131 ics_set_irq_type(ics, irq - ics->offset, lsi); 132 return 0; 133 } 134 135 static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq, int num) 136 { 137 ICSState *ics = spapr->ics; 138 uint32_t srcno = irq - ics->offset; 139 int i; 140 141 if (ics_valid_irq(ics, irq)) { 142 trace_spapr_irq_free(0, irq, num); 143 for (i = srcno; i < srcno + num; ++i) { 144 if (ICS_IRQ_FREE(ics, i)) { 145 trace_spapr_irq_free_warn(0, i); 146 } 147 memset(&ics->irqs[i], 0, sizeof(ICSIRQState)); 148 } 149 } 150 } 151 152 static qemu_irq spapr_qirq_xics(SpaprMachineState *spapr, int irq) 153 { 154 ICSState *ics = spapr->ics; 155 uint32_t srcno = irq - ics->offset; 156 157 if (ics_valid_irq(ics, irq)) { 158 return spapr->qirqs[srcno]; 159 } 160 161 return NULL; 162 } 163 164 static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon) 165 { 166 CPUState *cs; 167 168 CPU_FOREACH(cs) { 169 PowerPCCPU *cpu = POWERPC_CPU(cs); 170 171 icp_pic_print_info(spapr_cpu_state(cpu)->icp, mon); 172 } 173 174 ics_pic_print_info(spapr->ics, mon); 175 } 176 177 static void spapr_irq_cpu_intc_create_xics(SpaprMachineState *spapr, 178 PowerPCCPU *cpu, Error **errp) 179 { 180 Error *local_err = NULL; 181 Object *obj; 182 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 183 184 obj = icp_create(OBJECT(cpu), TYPE_ICP, XICS_FABRIC(spapr), 185 &local_err); 186 if (local_err) { 187 error_propagate(errp, local_err); 188 return; 189 } 190 191 spapr_cpu->icp = ICP(obj); 192 } 193 194 static int spapr_irq_post_load_xics(SpaprMachineState *spapr, int version_id) 195 { 196 if (!kvm_irqchip_in_kernel()) { 197 CPUState *cs; 198 CPU_FOREACH(cs) { 199 PowerPCCPU *cpu = POWERPC_CPU(cs); 200 icp_resend(spapr_cpu_state(cpu)->icp); 201 } 202 } 203 return 0; 204 } 205 206 static void spapr_irq_set_irq_xics(void *opaque, int srcno, int val) 207 { 208 SpaprMachineState *spapr = opaque; 209 210 ics_simple_set_irq(spapr->ics, srcno, val); 211 } 212 213 static void spapr_irq_reset_xics(SpaprMachineState *spapr, Error **errp) 214 { 215 /* TODO: create the KVM XICS device */ 216 } 217 218 static const char *spapr_irq_get_nodename_xics(SpaprMachineState *spapr) 219 { 220 return XICS_NODENAME; 221 } 222 223 #define SPAPR_IRQ_XICS_NR_IRQS 0x1000 224 #define SPAPR_IRQ_XICS_NR_MSIS \ 225 (XICS_IRQ_BASE + SPAPR_IRQ_XICS_NR_IRQS - SPAPR_IRQ_MSI) 226 227 SpaprIrq spapr_irq_xics = { 228 .nr_irqs = SPAPR_IRQ_XICS_NR_IRQS, 229 .nr_msis = SPAPR_IRQ_XICS_NR_MSIS, 230 .ov5 = SPAPR_OV5_XIVE_LEGACY, 231 232 .init = spapr_irq_init_xics, 233 .claim = spapr_irq_claim_xics, 234 .free = spapr_irq_free_xics, 235 .qirq = spapr_qirq_xics, 236 .print_info = spapr_irq_print_info_xics, 237 .dt_populate = spapr_dt_xics, 238 .cpu_intc_create = spapr_irq_cpu_intc_create_xics, 239 .post_load = spapr_irq_post_load_xics, 240 .reset = spapr_irq_reset_xics, 241 .set_irq = spapr_irq_set_irq_xics, 242 .get_nodename = spapr_irq_get_nodename_xics, 243 }; 244 245 /* 246 * XIVE IRQ backend. 247 */ 248 static void spapr_irq_init_xive(SpaprMachineState *spapr, int nr_irqs, 249 Error **errp) 250 { 251 uint32_t nr_servers = spapr_max_server_number(spapr); 252 DeviceState *dev; 253 int i; 254 255 dev = qdev_create(NULL, TYPE_SPAPR_XIVE); 256 qdev_prop_set_uint32(dev, "nr-irqs", nr_irqs); 257 /* 258 * 8 XIVE END structures per CPU. One for each available priority 259 */ 260 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3); 261 qdev_init_nofail(dev); 262 263 spapr->xive = SPAPR_XIVE(dev); 264 265 /* Enable the CPU IPIs */ 266 for (i = 0; i < nr_servers; ++i) { 267 spapr_xive_irq_claim(spapr->xive, SPAPR_IRQ_IPI + i, false); 268 } 269 270 spapr_xive_hcall_init(spapr); 271 } 272 273 static int spapr_irq_claim_xive(SpaprMachineState *spapr, int irq, bool lsi, 274 Error **errp) 275 { 276 if (!spapr_xive_irq_claim(spapr->xive, irq, lsi)) { 277 error_setg(errp, "IRQ %d is invalid", irq); 278 return -1; 279 } 280 return 0; 281 } 282 283 static void spapr_irq_free_xive(SpaprMachineState *spapr, int irq, int num) 284 { 285 int i; 286 287 for (i = irq; i < irq + num; ++i) { 288 spapr_xive_irq_free(spapr->xive, i); 289 } 290 } 291 292 static qemu_irq spapr_qirq_xive(SpaprMachineState *spapr, int irq) 293 { 294 SpaprXive *xive = spapr->xive; 295 296 if (irq >= xive->nr_irqs) { 297 return NULL; 298 } 299 300 /* The sPAPR machine/device should have claimed the IRQ before */ 301 assert(xive_eas_is_valid(&xive->eat[irq])); 302 303 return spapr->qirqs[irq]; 304 } 305 306 static void spapr_irq_print_info_xive(SpaprMachineState *spapr, 307 Monitor *mon) 308 { 309 CPUState *cs; 310 311 CPU_FOREACH(cs) { 312 PowerPCCPU *cpu = POWERPC_CPU(cs); 313 314 xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon); 315 } 316 317 spapr_xive_pic_print_info(spapr->xive, mon); 318 } 319 320 static void spapr_irq_cpu_intc_create_xive(SpaprMachineState *spapr, 321 PowerPCCPU *cpu, Error **errp) 322 { 323 Error *local_err = NULL; 324 Object *obj; 325 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 326 327 obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(spapr->xive), &local_err); 328 if (local_err) { 329 error_propagate(errp, local_err); 330 return; 331 } 332 333 spapr_cpu->tctx = XIVE_TCTX(obj); 334 335 /* 336 * (TCG) Early setting the OS CAM line for hotplugged CPUs as they 337 * don't beneficiate from the reset of the XIVE IRQ backend 338 */ 339 spapr_xive_set_tctx_os_cam(spapr_cpu->tctx); 340 } 341 342 static int spapr_irq_post_load_xive(SpaprMachineState *spapr, int version_id) 343 { 344 return spapr_xive_post_load(spapr->xive, version_id); 345 } 346 347 static void spapr_irq_reset_xive(SpaprMachineState *spapr, Error **errp) 348 { 349 CPUState *cs; 350 351 CPU_FOREACH(cs) { 352 PowerPCCPU *cpu = POWERPC_CPU(cs); 353 354 /* (TCG) Set the OS CAM line of the thread interrupt context. */ 355 spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx); 356 } 357 358 /* Activate the XIVE MMIOs */ 359 spapr_xive_mmio_set_enabled(spapr->xive, true); 360 } 361 362 static void spapr_irq_set_irq_xive(void *opaque, int srcno, int val) 363 { 364 SpaprMachineState *spapr = opaque; 365 366 if (kvm_irqchip_in_kernel()) { 367 kvmppc_xive_source_set_irq(&spapr->xive->source, srcno, val); 368 } else { 369 xive_source_set_irq(&spapr->xive->source, srcno, val); 370 } 371 } 372 373 static const char *spapr_irq_get_nodename_xive(SpaprMachineState *spapr) 374 { 375 return spapr->xive->nodename; 376 } 377 378 /* 379 * XIVE uses the full IRQ number space. Set it to 8K to be compatible 380 * with XICS. 381 */ 382 383 #define SPAPR_IRQ_XIVE_NR_IRQS 0x2000 384 #define SPAPR_IRQ_XIVE_NR_MSIS (SPAPR_IRQ_XIVE_NR_IRQS - SPAPR_IRQ_MSI) 385 386 SpaprIrq spapr_irq_xive = { 387 .nr_irqs = SPAPR_IRQ_XIVE_NR_IRQS, 388 .nr_msis = SPAPR_IRQ_XIVE_NR_MSIS, 389 .ov5 = SPAPR_OV5_XIVE_EXPLOIT, 390 391 .init = spapr_irq_init_xive, 392 .claim = spapr_irq_claim_xive, 393 .free = spapr_irq_free_xive, 394 .qirq = spapr_qirq_xive, 395 .print_info = spapr_irq_print_info_xive, 396 .dt_populate = spapr_dt_xive, 397 .cpu_intc_create = spapr_irq_cpu_intc_create_xive, 398 .post_load = spapr_irq_post_load_xive, 399 .reset = spapr_irq_reset_xive, 400 .set_irq = spapr_irq_set_irq_xive, 401 .get_nodename = spapr_irq_get_nodename_xive, 402 }; 403 404 /* 405 * Dual XIVE and XICS IRQ backend. 406 * 407 * Both interrupt mode, XIVE and XICS, objects are created but the 408 * machine starts in legacy interrupt mode (XICS). It can be changed 409 * by the CAS negotiation process and, in that case, the new mode is 410 * activated after an extra machine reset. 411 */ 412 413 /* 414 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the 415 * default. 416 */ 417 static SpaprIrq *spapr_irq_current(SpaprMachineState *spapr) 418 { 419 return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ? 420 &spapr_irq_xive : &spapr_irq_xics; 421 } 422 423 static void spapr_irq_init_dual(SpaprMachineState *spapr, int nr_irqs, 424 Error **errp) 425 { 426 MachineState *machine = MACHINE(spapr); 427 Error *local_err = NULL; 428 429 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) { 430 error_setg(errp, "No KVM support for the 'dual' machine"); 431 return; 432 } 433 434 spapr_irq_xics.init(spapr, spapr_irq_xics.nr_irqs, &local_err); 435 if (local_err) { 436 error_propagate(errp, local_err); 437 return; 438 } 439 440 spapr_irq_xive.init(spapr, spapr_irq_xive.nr_irqs, &local_err); 441 if (local_err) { 442 error_propagate(errp, local_err); 443 return; 444 } 445 } 446 447 static int spapr_irq_claim_dual(SpaprMachineState *spapr, int irq, bool lsi, 448 Error **errp) 449 { 450 Error *local_err = NULL; 451 int ret; 452 453 ret = spapr_irq_xics.claim(spapr, irq, lsi, &local_err); 454 if (local_err) { 455 error_propagate(errp, local_err); 456 return ret; 457 } 458 459 ret = spapr_irq_xive.claim(spapr, irq, lsi, &local_err); 460 if (local_err) { 461 error_propagate(errp, local_err); 462 return ret; 463 } 464 465 return ret; 466 } 467 468 static void spapr_irq_free_dual(SpaprMachineState *spapr, int irq, int num) 469 { 470 spapr_irq_xics.free(spapr, irq, num); 471 spapr_irq_xive.free(spapr, irq, num); 472 } 473 474 static qemu_irq spapr_qirq_dual(SpaprMachineState *spapr, int irq) 475 { 476 return spapr_irq_current(spapr)->qirq(spapr, irq); 477 } 478 479 static void spapr_irq_print_info_dual(SpaprMachineState *spapr, Monitor *mon) 480 { 481 spapr_irq_current(spapr)->print_info(spapr, mon); 482 } 483 484 static void spapr_irq_dt_populate_dual(SpaprMachineState *spapr, 485 uint32_t nr_servers, void *fdt, 486 uint32_t phandle) 487 { 488 spapr_irq_current(spapr)->dt_populate(spapr, nr_servers, fdt, phandle); 489 } 490 491 static void spapr_irq_cpu_intc_create_dual(SpaprMachineState *spapr, 492 PowerPCCPU *cpu, Error **errp) 493 { 494 Error *local_err = NULL; 495 496 spapr_irq_xive.cpu_intc_create(spapr, cpu, &local_err); 497 if (local_err) { 498 error_propagate(errp, local_err); 499 return; 500 } 501 502 spapr_irq_xics.cpu_intc_create(spapr, cpu, errp); 503 } 504 505 static int spapr_irq_post_load_dual(SpaprMachineState *spapr, int version_id) 506 { 507 /* 508 * Force a reset of the XIVE backend after migration. The machine 509 * defaults to XICS at startup. 510 */ 511 if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 512 spapr_irq_xive.reset(spapr, &error_fatal); 513 } 514 515 return spapr_irq_current(spapr)->post_load(spapr, version_id); 516 } 517 518 static void spapr_irq_reset_dual(SpaprMachineState *spapr, Error **errp) 519 { 520 /* 521 * Deactivate the XIVE MMIOs. The XIVE backend will reenable them 522 * if selected. 523 */ 524 spapr_xive_mmio_set_enabled(spapr->xive, false); 525 526 spapr_irq_current(spapr)->reset(spapr, errp); 527 } 528 529 static void spapr_irq_set_irq_dual(void *opaque, int srcno, int val) 530 { 531 SpaprMachineState *spapr = opaque; 532 533 spapr_irq_current(spapr)->set_irq(spapr, srcno, val); 534 } 535 536 static const char *spapr_irq_get_nodename_dual(SpaprMachineState *spapr) 537 { 538 return spapr_irq_current(spapr)->get_nodename(spapr); 539 } 540 541 /* 542 * Define values in sync with the XIVE and XICS backend 543 */ 544 #define SPAPR_IRQ_DUAL_NR_IRQS 0x2000 545 #define SPAPR_IRQ_DUAL_NR_MSIS (SPAPR_IRQ_DUAL_NR_IRQS - SPAPR_IRQ_MSI) 546 547 SpaprIrq spapr_irq_dual = { 548 .nr_irqs = SPAPR_IRQ_DUAL_NR_IRQS, 549 .nr_msis = SPAPR_IRQ_DUAL_NR_MSIS, 550 .ov5 = SPAPR_OV5_XIVE_BOTH, 551 552 .init = spapr_irq_init_dual, 553 .claim = spapr_irq_claim_dual, 554 .free = spapr_irq_free_dual, 555 .qirq = spapr_qirq_dual, 556 .print_info = spapr_irq_print_info_dual, 557 .dt_populate = spapr_irq_dt_populate_dual, 558 .cpu_intc_create = spapr_irq_cpu_intc_create_dual, 559 .post_load = spapr_irq_post_load_dual, 560 .reset = spapr_irq_reset_dual, 561 .set_irq = spapr_irq_set_irq_dual, 562 .get_nodename = spapr_irq_get_nodename_dual, 563 }; 564 565 566 static void spapr_irq_check(SpaprMachineState *spapr, Error **errp) 567 { 568 MachineState *machine = MACHINE(spapr); 569 570 /* 571 * Sanity checks on non-P9 machines. On these, XIVE is not 572 * advertised, see spapr_dt_ov5_platform_support() 573 */ 574 if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 575 0, spapr->max_compat_pvr)) { 576 /* 577 * If the 'dual' interrupt mode is selected, force XICS as CAS 578 * negotiation is useless. 579 */ 580 if (spapr->irq == &spapr_irq_dual) { 581 spapr->irq = &spapr_irq_xics; 582 return; 583 } 584 585 /* 586 * Non-P9 machines using only XIVE is a bogus setup. We have two 587 * scenarios to take into account because of the compat mode: 588 * 589 * 1. POWER7/8 machines should fail to init later on when creating 590 * the XIVE interrupt presenters because a POWER9 exception 591 * model is required. 592 593 * 2. POWER9 machines using the POWER8 compat mode won't fail and 594 * will let the OS boot with a partial XIVE setup : DT 595 * properties but no hcalls. 596 * 597 * To cover both and not confuse the OS, add an early failure in 598 * QEMU. 599 */ 600 if (spapr->irq == &spapr_irq_xive) { 601 error_setg(errp, "XIVE-only machines require a POWER9 CPU"); 602 return; 603 } 604 } 605 } 606 607 /* 608 * sPAPR IRQ frontend routines for devices 609 */ 610 void spapr_irq_init(SpaprMachineState *spapr, Error **errp) 611 { 612 MachineState *machine = MACHINE(spapr); 613 Error *local_err = NULL; 614 615 if (machine_kernel_irqchip_split(machine)) { 616 error_setg(errp, "kernel_irqchip split mode not supported on pseries"); 617 return; 618 } 619 620 if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) { 621 error_setg(errp, 622 "kernel_irqchip requested but only available with KVM"); 623 return; 624 } 625 626 spapr_irq_check(spapr, &local_err); 627 if (local_err) { 628 error_propagate(errp, local_err); 629 return; 630 } 631 632 /* Initialize the MSI IRQ allocator. */ 633 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) { 634 spapr_irq_msi_init(spapr, spapr->irq->nr_msis); 635 } 636 637 spapr->irq->init(spapr, spapr->irq->nr_irqs, errp); 638 639 spapr->qirqs = qemu_allocate_irqs(spapr->irq->set_irq, spapr, 640 spapr->irq->nr_irqs); 641 } 642 643 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp) 644 { 645 return spapr->irq->claim(spapr, irq, lsi, errp); 646 } 647 648 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num) 649 { 650 spapr->irq->free(spapr, irq, num); 651 } 652 653 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq) 654 { 655 return spapr->irq->qirq(spapr, irq); 656 } 657 658 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id) 659 { 660 return spapr->irq->post_load(spapr, version_id); 661 } 662 663 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp) 664 { 665 if (spapr->irq->reset) { 666 spapr->irq->reset(spapr, errp); 667 } 668 } 669 670 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp) 671 { 672 const char *nodename = spapr->irq->get_nodename(spapr); 673 int offset, phandle; 674 675 offset = fdt_subnode_offset(fdt, 0, nodename); 676 if (offset < 0) { 677 error_setg(errp, "Can't find node \"%s\": %s", nodename, 678 fdt_strerror(offset)); 679 return -1; 680 } 681 682 phandle = fdt_get_phandle(fdt, offset); 683 if (!phandle) { 684 error_setg(errp, "Can't get phandle of node \"%s\"", nodename); 685 return -1; 686 } 687 688 return phandle; 689 } 690 691 /* 692 * XICS legacy routines - to deprecate one day 693 */ 694 695 static int ics_find_free_block(ICSState *ics, int num, int alignnum) 696 { 697 int first, i; 698 699 for (first = 0; first < ics->nr_irqs; first += alignnum) { 700 if (num > (ics->nr_irqs - first)) { 701 return -1; 702 } 703 for (i = first; i < first + num; ++i) { 704 if (!ICS_IRQ_FREE(ics, i)) { 705 break; 706 } 707 } 708 if (i == (first + num)) { 709 return first; 710 } 711 } 712 713 return -1; 714 } 715 716 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp) 717 { 718 ICSState *ics = spapr->ics; 719 int first = -1; 720 721 assert(ics); 722 723 /* 724 * MSIMesage::data is used for storing VIRQ so 725 * it has to be aligned to num to support multiple 726 * MSI vectors. MSI-X is not affected by this. 727 * The hint is used for the first IRQ, the rest should 728 * be allocated continuously. 729 */ 730 if (align) { 731 assert((num == 1) || (num == 2) || (num == 4) || 732 (num == 8) || (num == 16) || (num == 32)); 733 first = ics_find_free_block(ics, num, num); 734 } else { 735 first = ics_find_free_block(ics, num, 1); 736 } 737 738 if (first < 0) { 739 error_setg(errp, "can't find a free %d-IRQ block", num); 740 return -1; 741 } 742 743 return first + ics->offset; 744 } 745 746 #define SPAPR_IRQ_XICS_LEGACY_NR_IRQS 0x400 747 748 SpaprIrq spapr_irq_xics_legacy = { 749 .nr_irqs = SPAPR_IRQ_XICS_LEGACY_NR_IRQS, 750 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_IRQS, 751 .ov5 = SPAPR_OV5_XIVE_LEGACY, 752 753 .init = spapr_irq_init_xics, 754 .claim = spapr_irq_claim_xics, 755 .free = spapr_irq_free_xics, 756 .qirq = spapr_qirq_xics, 757 .print_info = spapr_irq_print_info_xics, 758 .dt_populate = spapr_dt_xics, 759 .cpu_intc_create = spapr_irq_cpu_intc_create_xics, 760 .post_load = spapr_irq_post_load_xics, 761 .set_irq = spapr_irq_set_irq_xics, 762 .get_nodename = spapr_irq_get_nodename_xics, 763 }; 764