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