Include hw/irq.h a lot less
[qemu/ar7.git] / hw / ppc / spapr_irq.c
blob66705962e733f44dabfd1f41f2f1c879162e7c61
1 /*
2 * QEMU PowerPC sPAPR IRQ interface
4 * Copyright (c) 2018, IBM Corporation.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
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 "cpu-models.h"
21 #include "sysemu/kvm.h"
23 #include "trace.h"
25 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis)
27 spapr->irq_map_nr = nr_msis;
28 spapr->irq_map = bitmap_new(spapr->irq_map_nr);
31 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align,
32 Error **errp)
34 int irq;
37 * The 'align_mask' parameter of bitmap_find_next_zero_area()
38 * should be one less than a power of 2; 0 means no
39 * alignment. Adapt the 'align' value of the former allocator
40 * to fit the requirements of bitmap_find_next_zero_area()
42 align -= 1;
44 irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num,
45 align);
46 if (irq == spapr->irq_map_nr) {
47 error_setg(errp, "can't find a free %d-IRQ block", num);
48 return -1;
51 bitmap_set(spapr->irq_map, irq, num);
53 return irq + SPAPR_IRQ_MSI;
56 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num)
58 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
61 void spapr_irq_msi_reset(SpaprMachineState *spapr)
63 bitmap_clear(spapr->irq_map, 0, spapr->irq_map_nr);
66 static void spapr_irq_init_kvm(SpaprMachineState *spapr,
67 SpaprIrq *irq, Error **errp)
69 MachineState *machine = MACHINE(spapr);
70 Error *local_err = NULL;
72 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
73 irq->init_kvm(spapr, &local_err);
74 if (local_err && machine_kernel_irqchip_required(machine)) {
75 error_prepend(&local_err,
76 "kernel_irqchip requested but unavailable: ");
77 error_propagate(errp, local_err);
78 return;
81 if (!local_err) {
82 return;
86 * We failed to initialize the KVM device, fallback to
87 * emulated mode
89 error_prepend(&local_err, "kernel_irqchip allowed but unavailable: ");
90 error_append_hint(&local_err, "Falling back to kernel-irqchip=off\n");
91 warn_report_err(local_err);
96 * XICS IRQ backend.
99 static void spapr_irq_init_xics(SpaprMachineState *spapr, int nr_irqs,
100 Error **errp)
102 Object *obj;
103 Error *local_err = NULL;
105 obj = object_new(TYPE_ICS_SIMPLE);
106 object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort);
107 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
108 &error_fatal);
109 object_property_set_int(obj, nr_irqs, "nr-irqs", &error_fatal);
110 object_property_set_bool(obj, true, "realized", &local_err);
111 if (local_err) {
112 error_propagate(errp, local_err);
113 return;
116 spapr->ics = ICS_BASE(obj);
118 xics_spapr_init(spapr);
121 #define ICS_IRQ_FREE(ics, srcno) \
122 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
124 static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
125 Error **errp)
127 ICSState *ics = spapr->ics;
129 assert(ics);
131 if (!ics_valid_irq(ics, irq)) {
132 error_setg(errp, "IRQ %d is invalid", irq);
133 return -1;
136 if (!ICS_IRQ_FREE(ics, irq - ics->offset)) {
137 error_setg(errp, "IRQ %d is not free", irq);
138 return -1;
141 ics_set_irq_type(ics, irq - ics->offset, lsi);
142 return 0;
145 static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq, int num)
147 ICSState *ics = spapr->ics;
148 uint32_t srcno = irq - ics->offset;
149 int i;
151 if (ics_valid_irq(ics, irq)) {
152 trace_spapr_irq_free(0, irq, num);
153 for (i = srcno; i < srcno + num; ++i) {
154 if (ICS_IRQ_FREE(ics, i)) {
155 trace_spapr_irq_free_warn(0, i);
157 memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
162 static qemu_irq spapr_qirq_xics(SpaprMachineState *spapr, int irq)
164 ICSState *ics = spapr->ics;
165 uint32_t srcno = irq - ics->offset;
167 if (ics_valid_irq(ics, irq)) {
168 return spapr->qirqs[srcno];
171 return NULL;
174 static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
176 CPUState *cs;
178 CPU_FOREACH(cs) {
179 PowerPCCPU *cpu = POWERPC_CPU(cs);
181 icp_pic_print_info(spapr_cpu_state(cpu)->icp, mon);
184 ics_pic_print_info(spapr->ics, mon);
187 static void spapr_irq_cpu_intc_create_xics(SpaprMachineState *spapr,
188 PowerPCCPU *cpu, Error **errp)
190 Error *local_err = NULL;
191 Object *obj;
192 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
194 obj = icp_create(OBJECT(cpu), TYPE_ICP, XICS_FABRIC(spapr),
195 &local_err);
196 if (local_err) {
197 error_propagate(errp, local_err);
198 return;
201 spapr_cpu->icp = ICP(obj);
204 static int spapr_irq_post_load_xics(SpaprMachineState *spapr, int version_id)
206 if (!kvm_irqchip_in_kernel()) {
207 CPUState *cs;
208 CPU_FOREACH(cs) {
209 PowerPCCPU *cpu = POWERPC_CPU(cs);
210 icp_resend(spapr_cpu_state(cpu)->icp);
213 return 0;
216 static void spapr_irq_set_irq_xics(void *opaque, int srcno, int val)
218 SpaprMachineState *spapr = opaque;
220 ics_simple_set_irq(spapr->ics, srcno, val);
223 static void spapr_irq_reset_xics(SpaprMachineState *spapr, Error **errp)
225 Error *local_err = NULL;
227 spapr_irq_init_kvm(spapr, &spapr_irq_xics, &local_err);
228 if (local_err) {
229 error_propagate(errp, local_err);
230 return;
234 static const char *spapr_irq_get_nodename_xics(SpaprMachineState *spapr)
236 return XICS_NODENAME;
239 static void spapr_irq_init_kvm_xics(SpaprMachineState *spapr, Error **errp)
241 if (kvm_enabled()) {
242 xics_kvm_connect(spapr, errp);
246 #define SPAPR_IRQ_XICS_NR_IRQS 0x1000
247 #define SPAPR_IRQ_XICS_NR_MSIS \
248 (XICS_IRQ_BASE + SPAPR_IRQ_XICS_NR_IRQS - SPAPR_IRQ_MSI)
250 SpaprIrq spapr_irq_xics = {
251 .nr_irqs = SPAPR_IRQ_XICS_NR_IRQS,
252 .nr_msis = SPAPR_IRQ_XICS_NR_MSIS,
253 .ov5 = SPAPR_OV5_XIVE_LEGACY,
255 .init = spapr_irq_init_xics,
256 .claim = spapr_irq_claim_xics,
257 .free = spapr_irq_free_xics,
258 .qirq = spapr_qirq_xics,
259 .print_info = spapr_irq_print_info_xics,
260 .dt_populate = spapr_dt_xics,
261 .cpu_intc_create = spapr_irq_cpu_intc_create_xics,
262 .post_load = spapr_irq_post_load_xics,
263 .reset = spapr_irq_reset_xics,
264 .set_irq = spapr_irq_set_irq_xics,
265 .get_nodename = spapr_irq_get_nodename_xics,
266 .init_kvm = spapr_irq_init_kvm_xics,
270 * XIVE IRQ backend.
272 static void spapr_irq_init_xive(SpaprMachineState *spapr, int nr_irqs,
273 Error **errp)
275 uint32_t nr_servers = spapr_max_server_number(spapr);
276 DeviceState *dev;
277 int i;
279 dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
280 qdev_prop_set_uint32(dev, "nr-irqs", nr_irqs);
282 * 8 XIVE END structures per CPU. One for each available priority
284 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
285 qdev_init_nofail(dev);
287 spapr->xive = SPAPR_XIVE(dev);
289 /* Enable the CPU IPIs */
290 for (i = 0; i < nr_servers; ++i) {
291 spapr_xive_irq_claim(spapr->xive, SPAPR_IRQ_IPI + i, false);
294 spapr_xive_hcall_init(spapr);
297 static int spapr_irq_claim_xive(SpaprMachineState *spapr, int irq, bool lsi,
298 Error **errp)
300 if (!spapr_xive_irq_claim(spapr->xive, irq, lsi)) {
301 error_setg(errp, "IRQ %d is invalid", irq);
302 return -1;
304 return 0;
307 static void spapr_irq_free_xive(SpaprMachineState *spapr, int irq, int num)
309 int i;
311 for (i = irq; i < irq + num; ++i) {
312 spapr_xive_irq_free(spapr->xive, i);
316 static qemu_irq spapr_qirq_xive(SpaprMachineState *spapr, int irq)
318 SpaprXive *xive = spapr->xive;
320 if (irq >= xive->nr_irqs) {
321 return NULL;
324 /* The sPAPR machine/device should have claimed the IRQ before */
325 assert(xive_eas_is_valid(&xive->eat[irq]));
327 return spapr->qirqs[irq];
330 static void spapr_irq_print_info_xive(SpaprMachineState *spapr,
331 Monitor *mon)
333 CPUState *cs;
335 CPU_FOREACH(cs) {
336 PowerPCCPU *cpu = POWERPC_CPU(cs);
338 xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
341 spapr_xive_pic_print_info(spapr->xive, mon);
344 static void spapr_irq_cpu_intc_create_xive(SpaprMachineState *spapr,
345 PowerPCCPU *cpu, Error **errp)
347 Error *local_err = NULL;
348 Object *obj;
349 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
351 obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(spapr->xive), &local_err);
352 if (local_err) {
353 error_propagate(errp, local_err);
354 return;
357 spapr_cpu->tctx = XIVE_TCTX(obj);
360 * (TCG) Early setting the OS CAM line for hotplugged CPUs as they
361 * don't beneficiate from the reset of the XIVE IRQ backend
363 spapr_xive_set_tctx_os_cam(spapr_cpu->tctx);
366 static int spapr_irq_post_load_xive(SpaprMachineState *spapr, int version_id)
368 return spapr_xive_post_load(spapr->xive, version_id);
371 static void spapr_irq_reset_xive(SpaprMachineState *spapr, Error **errp)
373 CPUState *cs;
374 Error *local_err = NULL;
376 CPU_FOREACH(cs) {
377 PowerPCCPU *cpu = POWERPC_CPU(cs);
379 /* (TCG) Set the OS CAM line of the thread interrupt context. */
380 spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx);
383 spapr_irq_init_kvm(spapr, &spapr_irq_xive, &local_err);
384 if (local_err) {
385 error_propagate(errp, local_err);
386 return;
389 /* Activate the XIVE MMIOs */
390 spapr_xive_mmio_set_enabled(spapr->xive, true);
393 static void spapr_irq_set_irq_xive(void *opaque, int srcno, int val)
395 SpaprMachineState *spapr = opaque;
397 if (kvm_irqchip_in_kernel()) {
398 kvmppc_xive_source_set_irq(&spapr->xive->source, srcno, val);
399 } else {
400 xive_source_set_irq(&spapr->xive->source, srcno, val);
404 static const char *spapr_irq_get_nodename_xive(SpaprMachineState *spapr)
406 return spapr->xive->nodename;
409 static void spapr_irq_init_kvm_xive(SpaprMachineState *spapr, Error **errp)
411 if (kvm_enabled()) {
412 kvmppc_xive_connect(spapr->xive, errp);
417 * XIVE uses the full IRQ number space. Set it to 8K to be compatible
418 * with XICS.
421 #define SPAPR_IRQ_XIVE_NR_IRQS 0x2000
422 #define SPAPR_IRQ_XIVE_NR_MSIS (SPAPR_IRQ_XIVE_NR_IRQS - SPAPR_IRQ_MSI)
424 SpaprIrq spapr_irq_xive = {
425 .nr_irqs = SPAPR_IRQ_XIVE_NR_IRQS,
426 .nr_msis = SPAPR_IRQ_XIVE_NR_MSIS,
427 .ov5 = SPAPR_OV5_XIVE_EXPLOIT,
429 .init = spapr_irq_init_xive,
430 .claim = spapr_irq_claim_xive,
431 .free = spapr_irq_free_xive,
432 .qirq = spapr_qirq_xive,
433 .print_info = spapr_irq_print_info_xive,
434 .dt_populate = spapr_dt_xive,
435 .cpu_intc_create = spapr_irq_cpu_intc_create_xive,
436 .post_load = spapr_irq_post_load_xive,
437 .reset = spapr_irq_reset_xive,
438 .set_irq = spapr_irq_set_irq_xive,
439 .get_nodename = spapr_irq_get_nodename_xive,
440 .init_kvm = spapr_irq_init_kvm_xive,
444 * Dual XIVE and XICS IRQ backend.
446 * Both interrupt mode, XIVE and XICS, objects are created but the
447 * machine starts in legacy interrupt mode (XICS). It can be changed
448 * by the CAS negotiation process and, in that case, the new mode is
449 * activated after an extra machine reset.
453 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the
454 * default.
456 static SpaprIrq *spapr_irq_current(SpaprMachineState *spapr)
458 return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ?
459 &spapr_irq_xive : &spapr_irq_xics;
462 static void spapr_irq_init_dual(SpaprMachineState *spapr, int nr_irqs,
463 Error **errp)
465 Error *local_err = NULL;
467 spapr_irq_xics.init(spapr, spapr_irq_xics.nr_irqs, &local_err);
468 if (local_err) {
469 error_propagate(errp, local_err);
470 return;
473 spapr_irq_xive.init(spapr, spapr_irq_xive.nr_irqs, &local_err);
474 if (local_err) {
475 error_propagate(errp, local_err);
476 return;
480 static int spapr_irq_claim_dual(SpaprMachineState *spapr, int irq, bool lsi,
481 Error **errp)
483 Error *local_err = NULL;
484 int ret;
486 ret = spapr_irq_xics.claim(spapr, irq, lsi, &local_err);
487 if (local_err) {
488 error_propagate(errp, local_err);
489 return ret;
492 ret = spapr_irq_xive.claim(spapr, irq, lsi, &local_err);
493 if (local_err) {
494 error_propagate(errp, local_err);
495 return ret;
498 return ret;
501 static void spapr_irq_free_dual(SpaprMachineState *spapr, int irq, int num)
503 spapr_irq_xics.free(spapr, irq, num);
504 spapr_irq_xive.free(spapr, irq, num);
507 static qemu_irq spapr_qirq_dual(SpaprMachineState *spapr, int irq)
509 return spapr_irq_current(spapr)->qirq(spapr, irq);
512 static void spapr_irq_print_info_dual(SpaprMachineState *spapr, Monitor *mon)
514 spapr_irq_current(spapr)->print_info(spapr, mon);
517 static void spapr_irq_dt_populate_dual(SpaprMachineState *spapr,
518 uint32_t nr_servers, void *fdt,
519 uint32_t phandle)
521 spapr_irq_current(spapr)->dt_populate(spapr, nr_servers, fdt, phandle);
524 static void spapr_irq_cpu_intc_create_dual(SpaprMachineState *spapr,
525 PowerPCCPU *cpu, Error **errp)
527 Error *local_err = NULL;
529 spapr_irq_xive.cpu_intc_create(spapr, cpu, &local_err);
530 if (local_err) {
531 error_propagate(errp, local_err);
532 return;
535 spapr_irq_xics.cpu_intc_create(spapr, cpu, errp);
538 static int spapr_irq_post_load_dual(SpaprMachineState *spapr, int version_id)
541 * Force a reset of the XIVE backend after migration. The machine
542 * defaults to XICS at startup.
544 if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
545 if (kvm_irqchip_in_kernel()) {
546 xics_kvm_disconnect(spapr, &error_fatal);
548 spapr_irq_xive.reset(spapr, &error_fatal);
551 return spapr_irq_current(spapr)->post_load(spapr, version_id);
554 static void spapr_irq_reset_dual(SpaprMachineState *spapr, Error **errp)
556 Error *local_err = NULL;
559 * Deactivate the XIVE MMIOs. The XIVE backend will reenable them
560 * if selected.
562 spapr_xive_mmio_set_enabled(spapr->xive, false);
564 /* Destroy all KVM devices */
565 if (kvm_irqchip_in_kernel()) {
566 xics_kvm_disconnect(spapr, &local_err);
567 if (local_err) {
568 error_propagate(errp, local_err);
569 error_prepend(errp, "KVM XICS disconnect failed: ");
570 return;
572 kvmppc_xive_disconnect(spapr->xive, &local_err);
573 if (local_err) {
574 error_propagate(errp, local_err);
575 error_prepend(errp, "KVM XIVE disconnect failed: ");
576 return;
580 spapr_irq_current(spapr)->reset(spapr, errp);
583 static void spapr_irq_set_irq_dual(void *opaque, int srcno, int val)
585 SpaprMachineState *spapr = opaque;
587 spapr_irq_current(spapr)->set_irq(spapr, srcno, val);
590 static const char *spapr_irq_get_nodename_dual(SpaprMachineState *spapr)
592 return spapr_irq_current(spapr)->get_nodename(spapr);
596 * Define values in sync with the XIVE and XICS backend
598 #define SPAPR_IRQ_DUAL_NR_IRQS 0x2000
599 #define SPAPR_IRQ_DUAL_NR_MSIS (SPAPR_IRQ_DUAL_NR_IRQS - SPAPR_IRQ_MSI)
601 SpaprIrq spapr_irq_dual = {
602 .nr_irqs = SPAPR_IRQ_DUAL_NR_IRQS,
603 .nr_msis = SPAPR_IRQ_DUAL_NR_MSIS,
604 .ov5 = SPAPR_OV5_XIVE_BOTH,
606 .init = spapr_irq_init_dual,
607 .claim = spapr_irq_claim_dual,
608 .free = spapr_irq_free_dual,
609 .qirq = spapr_qirq_dual,
610 .print_info = spapr_irq_print_info_dual,
611 .dt_populate = spapr_irq_dt_populate_dual,
612 .cpu_intc_create = spapr_irq_cpu_intc_create_dual,
613 .post_load = spapr_irq_post_load_dual,
614 .reset = spapr_irq_reset_dual,
615 .set_irq = spapr_irq_set_irq_dual,
616 .get_nodename = spapr_irq_get_nodename_dual,
617 .init_kvm = NULL, /* should not be used */
621 static void spapr_irq_check(SpaprMachineState *spapr, Error **errp)
623 MachineState *machine = MACHINE(spapr);
626 * Sanity checks on non-P9 machines. On these, XIVE is not
627 * advertised, see spapr_dt_ov5_platform_support()
629 if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00,
630 0, spapr->max_compat_pvr)) {
632 * If the 'dual' interrupt mode is selected, force XICS as CAS
633 * negotiation is useless.
635 if (spapr->irq == &spapr_irq_dual) {
636 spapr->irq = &spapr_irq_xics;
637 return;
641 * Non-P9 machines using only XIVE is a bogus setup. We have two
642 * scenarios to take into account because of the compat mode:
644 * 1. POWER7/8 machines should fail to init later on when creating
645 * the XIVE interrupt presenters because a POWER9 exception
646 * model is required.
648 * 2. POWER9 machines using the POWER8 compat mode won't fail and
649 * will let the OS boot with a partial XIVE setup : DT
650 * properties but no hcalls.
652 * To cover both and not confuse the OS, add an early failure in
653 * QEMU.
655 if (spapr->irq == &spapr_irq_xive) {
656 error_setg(errp, "XIVE-only machines require a POWER9 CPU");
657 return;
662 * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
663 * re-created. Detect that early to avoid QEMU to exit later when the
664 * guest reboots.
666 if (kvm_enabled() &&
667 spapr->irq == &spapr_irq_dual &&
668 machine_kernel_irqchip_required(machine) &&
669 xics_kvm_has_broken_disconnect(spapr)) {
670 error_setg(errp, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
671 return;
676 * sPAPR IRQ frontend routines for devices
678 void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
680 MachineState *machine = MACHINE(spapr);
681 Error *local_err = NULL;
683 if (machine_kernel_irqchip_split(machine)) {
684 error_setg(errp, "kernel_irqchip split mode not supported on pseries");
685 return;
688 if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
689 error_setg(errp,
690 "kernel_irqchip requested but only available with KVM");
691 return;
694 spapr_irq_check(spapr, &local_err);
695 if (local_err) {
696 error_propagate(errp, local_err);
697 return;
700 /* Initialize the MSI IRQ allocator. */
701 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
702 spapr_irq_msi_init(spapr, spapr->irq->nr_msis);
705 spapr->irq->init(spapr, spapr->irq->nr_irqs, errp);
707 spapr->qirqs = qemu_allocate_irqs(spapr->irq->set_irq, spapr,
708 spapr->irq->nr_irqs);
711 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
713 return spapr->irq->claim(spapr, irq, lsi, errp);
716 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
718 spapr->irq->free(spapr, irq, num);
721 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq)
723 return spapr->irq->qirq(spapr, irq);
726 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id)
728 return spapr->irq->post_load(spapr, version_id);
731 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp)
733 if (spapr->irq->reset) {
734 spapr->irq->reset(spapr, errp);
738 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp)
740 const char *nodename = spapr->irq->get_nodename(spapr);
741 int offset, phandle;
743 offset = fdt_subnode_offset(fdt, 0, nodename);
744 if (offset < 0) {
745 error_setg(errp, "Can't find node \"%s\": %s", nodename,
746 fdt_strerror(offset));
747 return -1;
750 phandle = fdt_get_phandle(fdt, offset);
751 if (!phandle) {
752 error_setg(errp, "Can't get phandle of node \"%s\"", nodename);
753 return -1;
756 return phandle;
760 * XICS legacy routines - to deprecate one day
763 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
765 int first, i;
767 for (first = 0; first < ics->nr_irqs; first += alignnum) {
768 if (num > (ics->nr_irqs - first)) {
769 return -1;
771 for (i = first; i < first + num; ++i) {
772 if (!ICS_IRQ_FREE(ics, i)) {
773 break;
776 if (i == (first + num)) {
777 return first;
781 return -1;
784 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp)
786 ICSState *ics = spapr->ics;
787 int first = -1;
789 assert(ics);
792 * MSIMesage::data is used for storing VIRQ so
793 * it has to be aligned to num to support multiple
794 * MSI vectors. MSI-X is not affected by this.
795 * The hint is used for the first IRQ, the rest should
796 * be allocated continuously.
798 if (align) {
799 assert((num == 1) || (num == 2) || (num == 4) ||
800 (num == 8) || (num == 16) || (num == 32));
801 first = ics_find_free_block(ics, num, num);
802 } else {
803 first = ics_find_free_block(ics, num, 1);
806 if (first < 0) {
807 error_setg(errp, "can't find a free %d-IRQ block", num);
808 return -1;
811 return first + ics->offset;
814 #define SPAPR_IRQ_XICS_LEGACY_NR_IRQS 0x400
816 SpaprIrq spapr_irq_xics_legacy = {
817 .nr_irqs = SPAPR_IRQ_XICS_LEGACY_NR_IRQS,
818 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_IRQS,
819 .ov5 = SPAPR_OV5_XIVE_LEGACY,
821 .init = spapr_irq_init_xics,
822 .claim = spapr_irq_claim_xics,
823 .free = spapr_irq_free_xics,
824 .qirq = spapr_qirq_xics,
825 .print_info = spapr_irq_print_info_xics,
826 .dt_populate = spapr_dt_xics,
827 .cpu_intc_create = spapr_irq_cpu_intc_create_xics,
828 .post_load = spapr_irq_post_load_xics,
829 .reset = spapr_irq_reset_xics,
830 .set_irq = spapr_irq_set_irq_xics,
831 .get_nodename = spapr_irq_get_nodename_xics,
832 .init_kvm = spapr_irq_init_kvm_xics,