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