xref: /openbmc/qemu/hw/ppc/spapr_irq.c (revision 6a0acfff)
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 "cpu-models.h"
21 #include "sysemu/kvm.h"
22 
23 #include "trace.h"
24 
25 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis)
26 {
27     spapr->irq_map_nr = nr_msis;
28     spapr->irq_map = bitmap_new(spapr->irq_map_nr);
29 }
30 
31 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align,
32                         Error **errp)
33 {
34     int irq;
35 
36     /*
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()
41      */
42     align -= 1;
43 
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;
49     }
50 
51     bitmap_set(spapr->irq_map, irq, num);
52 
53     return irq + SPAPR_IRQ_MSI;
54 }
55 
56 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num)
57 {
58     bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
59 }
60 
61 void spapr_irq_msi_reset(SpaprMachineState *spapr)
62 {
63     bitmap_clear(spapr->irq_map, 0, spapr->irq_map_nr);
64 }
65 
66 static void spapr_irq_init_kvm(SpaprMachineState *spapr,
67                                   SpaprIrq *irq, Error **errp)
68 {
69     MachineState *machine = MACHINE(spapr);
70     Error *local_err = NULL;
71 
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;
79         }
80 
81         if (!local_err) {
82             return;
83         }
84 
85         /*
86          * We failed to initialize the KVM device, fallback to
87          * emulated mode
88          */
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);
92     }
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     xics_spapr_init(spapr);
119 }
120 
121 #define ICS_IRQ_FREE(ics, srcno)   \
122     (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
123 
124 static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
125                                 Error **errp)
126 {
127     ICSState *ics = spapr->ics;
128 
129     assert(ics);
130 
131     if (!ics_valid_irq(ics, irq)) {
132         error_setg(errp, "IRQ %d is invalid", irq);
133         return -1;
134     }
135 
136     if (!ICS_IRQ_FREE(ics, irq - ics->offset)) {
137         error_setg(errp, "IRQ %d is not free", irq);
138         return -1;
139     }
140 
141     ics_set_irq_type(ics, irq - ics->offset, lsi);
142     return 0;
143 }
144 
145 static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq, int num)
146 {
147     ICSState *ics = spapr->ics;
148     uint32_t srcno = irq - ics->offset;
149     int i;
150 
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);
156             }
157             memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
158         }
159     }
160 }
161 
162 static qemu_irq spapr_qirq_xics(SpaprMachineState *spapr, int irq)
163 {
164     ICSState *ics = spapr->ics;
165     uint32_t srcno = irq - ics->offset;
166 
167     if (ics_valid_irq(ics, irq)) {
168         return spapr->qirqs[srcno];
169     }
170 
171     return NULL;
172 }
173 
174 static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
175 {
176     CPUState *cs;
177 
178     CPU_FOREACH(cs) {
179         PowerPCCPU *cpu = POWERPC_CPU(cs);
180 
181         icp_pic_print_info(spapr_cpu_state(cpu)->icp, mon);
182     }
183 
184     ics_pic_print_info(spapr->ics, mon);
185 }
186 
187 static void spapr_irq_cpu_intc_create_xics(SpaprMachineState *spapr,
188                                            PowerPCCPU *cpu, Error **errp)
189 {
190     Error *local_err = NULL;
191     Object *obj;
192     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
193 
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;
199     }
200 
201     spapr_cpu->icp = ICP(obj);
202 }
203 
204 static int spapr_irq_post_load_xics(SpaprMachineState *spapr, int version_id)
205 {
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);
211         }
212     }
213     return 0;
214 }
215 
216 static void spapr_irq_set_irq_xics(void *opaque, int srcno, int val)
217 {
218     SpaprMachineState *spapr = opaque;
219 
220     ics_simple_set_irq(spapr->ics, srcno, val);
221 }
222 
223 static void spapr_irq_reset_xics(SpaprMachineState *spapr, Error **errp)
224 {
225     Error *local_err = NULL;
226 
227     spapr_irq_init_kvm(spapr, &spapr_irq_xics, &local_err);
228     if (local_err) {
229         error_propagate(errp, local_err);
230         return;
231     }
232 }
233 
234 static const char *spapr_irq_get_nodename_xics(SpaprMachineState *spapr)
235 {
236     return XICS_NODENAME;
237 }
238 
239 static void spapr_irq_init_kvm_xics(SpaprMachineState *spapr, Error **errp)
240 {
241     if (kvm_enabled()) {
242         xics_kvm_connect(spapr, errp);
243     }
244 }
245 
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)
249 
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,
254 
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,
267 };
268 
269 /*
270  * XIVE IRQ backend.
271  */
272 static void spapr_irq_init_xive(SpaprMachineState *spapr, int nr_irqs,
273                                 Error **errp)
274 {
275     uint32_t nr_servers = spapr_max_server_number(spapr);
276     DeviceState *dev;
277     int i;
278 
279     dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
280     qdev_prop_set_uint32(dev, "nr-irqs", nr_irqs);
281     /*
282      * 8 XIVE END structures per CPU. One for each available priority
283      */
284     qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
285     qdev_init_nofail(dev);
286 
287     spapr->xive = SPAPR_XIVE(dev);
288 
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);
292     }
293 
294     spapr_xive_hcall_init(spapr);
295 }
296 
297 static int spapr_irq_claim_xive(SpaprMachineState *spapr, int irq, bool lsi,
298                                 Error **errp)
299 {
300     if (!spapr_xive_irq_claim(spapr->xive, irq, lsi)) {
301         error_setg(errp, "IRQ %d is invalid", irq);
302         return -1;
303     }
304     return 0;
305 }
306 
307 static void spapr_irq_free_xive(SpaprMachineState *spapr, int irq, int num)
308 {
309     int i;
310 
311     for (i = irq; i < irq + num; ++i) {
312         spapr_xive_irq_free(spapr->xive, i);
313     }
314 }
315 
316 static qemu_irq spapr_qirq_xive(SpaprMachineState *spapr, int irq)
317 {
318     SpaprXive *xive = spapr->xive;
319 
320     if (irq >= xive->nr_irqs) {
321         return NULL;
322     }
323 
324     /* The sPAPR machine/device should have claimed the IRQ before */
325     assert(xive_eas_is_valid(&xive->eat[irq]));
326 
327     return spapr->qirqs[irq];
328 }
329 
330 static void spapr_irq_print_info_xive(SpaprMachineState *spapr,
331                                       Monitor *mon)
332 {
333     CPUState *cs;
334 
335     CPU_FOREACH(cs) {
336         PowerPCCPU *cpu = POWERPC_CPU(cs);
337 
338         xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
339     }
340 
341     spapr_xive_pic_print_info(spapr->xive, mon);
342 }
343 
344 static void spapr_irq_cpu_intc_create_xive(SpaprMachineState *spapr,
345                                            PowerPCCPU *cpu, Error **errp)
346 {
347     Error *local_err = NULL;
348     Object *obj;
349     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
350 
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;
355     }
356 
357     spapr_cpu->tctx = XIVE_TCTX(obj);
358 
359     /*
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
362      */
363     spapr_xive_set_tctx_os_cam(spapr_cpu->tctx);
364 }
365 
366 static int spapr_irq_post_load_xive(SpaprMachineState *spapr, int version_id)
367 {
368     return spapr_xive_post_load(spapr->xive, version_id);
369 }
370 
371 static void spapr_irq_reset_xive(SpaprMachineState *spapr, Error **errp)
372 {
373     CPUState *cs;
374     Error *local_err = NULL;
375 
376     CPU_FOREACH(cs) {
377         PowerPCCPU *cpu = POWERPC_CPU(cs);
378 
379         /* (TCG) Set the OS CAM line of the thread interrupt context. */
380         spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx);
381     }
382 
383     spapr_irq_init_kvm(spapr, &spapr_irq_xive, &local_err);
384     if (local_err) {
385         error_propagate(errp, local_err);
386         return;
387     }
388 
389     /* Activate the XIVE MMIOs */
390     spapr_xive_mmio_set_enabled(spapr->xive, true);
391 }
392 
393 static void spapr_irq_set_irq_xive(void *opaque, int srcno, int val)
394 {
395     SpaprMachineState *spapr = opaque;
396 
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);
401     }
402 }
403 
404 static const char *spapr_irq_get_nodename_xive(SpaprMachineState *spapr)
405 {
406     return spapr->xive->nodename;
407 }
408 
409 static void spapr_irq_init_kvm_xive(SpaprMachineState *spapr, Error **errp)
410 {
411     if (kvm_enabled()) {
412         kvmppc_xive_connect(spapr->xive, errp);
413     }
414 }
415 
416 /*
417  * XIVE uses the full IRQ number space. Set it to 8K to be compatible
418  * with XICS.
419  */
420 
421 #define SPAPR_IRQ_XIVE_NR_IRQS     0x2000
422 #define SPAPR_IRQ_XIVE_NR_MSIS     (SPAPR_IRQ_XIVE_NR_IRQS - SPAPR_IRQ_MSI)
423 
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,
428 
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,
441 };
442 
443 /*
444  * Dual XIVE and XICS IRQ backend.
445  *
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.
450  */
451 
452 /*
453  * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the
454  * default.
455  */
456 static SpaprIrq *spapr_irq_current(SpaprMachineState *spapr)
457 {
458     return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ?
459         &spapr_irq_xive : &spapr_irq_xics;
460 }
461 
462 static void spapr_irq_init_dual(SpaprMachineState *spapr, int nr_irqs,
463                                 Error **errp)
464 {
465     Error *local_err = NULL;
466 
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;
471     }
472 
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;
477     }
478 }
479 
480 static int spapr_irq_claim_dual(SpaprMachineState *spapr, int irq, bool lsi,
481                                 Error **errp)
482 {
483     Error *local_err = NULL;
484     int ret;
485 
486     ret = spapr_irq_xics.claim(spapr, irq, lsi, &local_err);
487     if (local_err) {
488         error_propagate(errp, local_err);
489         return ret;
490     }
491 
492     ret = spapr_irq_xive.claim(spapr, irq, lsi, &local_err);
493     if (local_err) {
494         error_propagate(errp, local_err);
495         return ret;
496     }
497 
498     return ret;
499 }
500 
501 static void spapr_irq_free_dual(SpaprMachineState *spapr, int irq, int num)
502 {
503     spapr_irq_xics.free(spapr, irq, num);
504     spapr_irq_xive.free(spapr, irq, num);
505 }
506 
507 static qemu_irq spapr_qirq_dual(SpaprMachineState *spapr, int irq)
508 {
509     return spapr_irq_current(spapr)->qirq(spapr, irq);
510 }
511 
512 static void spapr_irq_print_info_dual(SpaprMachineState *spapr, Monitor *mon)
513 {
514     spapr_irq_current(spapr)->print_info(spapr, mon);
515 }
516 
517 static void spapr_irq_dt_populate_dual(SpaprMachineState *spapr,
518                                        uint32_t nr_servers, void *fdt,
519                                        uint32_t phandle)
520 {
521     spapr_irq_current(spapr)->dt_populate(spapr, nr_servers, fdt, phandle);
522 }
523 
524 static void spapr_irq_cpu_intc_create_dual(SpaprMachineState *spapr,
525                                            PowerPCCPU *cpu, Error **errp)
526 {
527     Error *local_err = NULL;
528 
529     spapr_irq_xive.cpu_intc_create(spapr, cpu, &local_err);
530     if (local_err) {
531         error_propagate(errp, local_err);
532         return;
533     }
534 
535     spapr_irq_xics.cpu_intc_create(spapr, cpu, errp);
536 }
537 
538 static int spapr_irq_post_load_dual(SpaprMachineState *spapr, int version_id)
539 {
540     /*
541      * Force a reset of the XIVE backend after migration. The machine
542      * defaults to XICS at startup.
543      */
544     if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
545         if (kvm_irqchip_in_kernel()) {
546             xics_kvm_disconnect(spapr, &error_fatal);
547         }
548         spapr_irq_xive.reset(spapr, &error_fatal);
549     }
550 
551     return spapr_irq_current(spapr)->post_load(spapr, version_id);
552 }
553 
554 static void spapr_irq_reset_dual(SpaprMachineState *spapr, Error **errp)
555 {
556     Error *local_err = NULL;
557 
558     /*
559      * Deactivate the XIVE MMIOs. The XIVE backend will reenable them
560      * if selected.
561      */
562     spapr_xive_mmio_set_enabled(spapr->xive, false);
563 
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;
571         }
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;
577         }
578     }
579 
580     spapr_irq_current(spapr)->reset(spapr, errp);
581 }
582 
583 static void spapr_irq_set_irq_dual(void *opaque, int srcno, int val)
584 {
585     SpaprMachineState *spapr = opaque;
586 
587     spapr_irq_current(spapr)->set_irq(spapr, srcno, val);
588 }
589 
590 static const char *spapr_irq_get_nodename_dual(SpaprMachineState *spapr)
591 {
592     return spapr_irq_current(spapr)->get_nodename(spapr);
593 }
594 
595 /*
596  * Define values in sync with the XIVE and XICS backend
597  */
598 #define SPAPR_IRQ_DUAL_NR_IRQS     0x2000
599 #define SPAPR_IRQ_DUAL_NR_MSIS     (SPAPR_IRQ_DUAL_NR_IRQS - SPAPR_IRQ_MSI)
600 
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,
605 
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 */
618 };
619 
620 
621 static void spapr_irq_check(SpaprMachineState *spapr, Error **errp)
622 {
623     MachineState *machine = MACHINE(spapr);
624 
625     /*
626      * Sanity checks on non-P9 machines. On these, XIVE is not
627      * advertised, see spapr_dt_ov5_platform_support()
628      */
629     if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00,
630                                0, spapr->max_compat_pvr)) {
631         /*
632          * If the 'dual' interrupt mode is selected, force XICS as CAS
633          * negotiation is useless.
634          */
635         if (spapr->irq == &spapr_irq_dual) {
636             spapr->irq = &spapr_irq_xics;
637             return;
638         }
639 
640         /*
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:
643          *
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.
647 
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.
651          *
652          * To cover both and not confuse the OS, add an early failure in
653          * QEMU.
654          */
655         if (spapr->irq == &spapr_irq_xive) {
656             error_setg(errp, "XIVE-only machines require a POWER9 CPU");
657             return;
658         }
659     }
660 
661     /*
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.
665      */
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;
672     }
673 }
674 
675 /*
676  * sPAPR IRQ frontend routines for devices
677  */
678 void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
679 {
680     MachineState *machine = MACHINE(spapr);
681     Error *local_err = NULL;
682 
683     if (machine_kernel_irqchip_split(machine)) {
684         error_setg(errp, "kernel_irqchip split mode not supported on pseries");
685         return;
686     }
687 
688     if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
689         error_setg(errp,
690                    "kernel_irqchip requested but only available with KVM");
691         return;
692     }
693 
694     spapr_irq_check(spapr, &local_err);
695     if (local_err) {
696         error_propagate(errp, local_err);
697         return;
698     }
699 
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);
703     }
704 
705     spapr->irq->init(spapr, spapr->irq->nr_irqs, errp);
706 
707     spapr->qirqs = qemu_allocate_irqs(spapr->irq->set_irq, spapr,
708                                       spapr->irq->nr_irqs);
709 }
710 
711 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
712 {
713     return spapr->irq->claim(spapr, irq, lsi, errp);
714 }
715 
716 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
717 {
718     spapr->irq->free(spapr, irq, num);
719 }
720 
721 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq)
722 {
723     return spapr->irq->qirq(spapr, irq);
724 }
725 
726 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id)
727 {
728     return spapr->irq->post_load(spapr, version_id);
729 }
730 
731 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp)
732 {
733     if (spapr->irq->reset) {
734         spapr->irq->reset(spapr, errp);
735     }
736 }
737 
738 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp)
739 {
740     const char *nodename = spapr->irq->get_nodename(spapr);
741     int offset, phandle;
742 
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;
748     }
749 
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;
754     }
755 
756     return phandle;
757 }
758 
759 /*
760  * XICS legacy routines - to deprecate one day
761  */
762 
763 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
764 {
765     int first, i;
766 
767     for (first = 0; first < ics->nr_irqs; first += alignnum) {
768         if (num > (ics->nr_irqs - first)) {
769             return -1;
770         }
771         for (i = first; i < first + num; ++i) {
772             if (!ICS_IRQ_FREE(ics, i)) {
773                 break;
774             }
775         }
776         if (i == (first + num)) {
777             return first;
778         }
779     }
780 
781     return -1;
782 }
783 
784 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp)
785 {
786     ICSState *ics = spapr->ics;
787     int first = -1;
788 
789     assert(ics);
790 
791     /*
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.
797      */
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);
804     }
805 
806     if (first < 0) {
807         error_setg(errp, "can't find a free %d-IRQ block", num);
808         return -1;
809     }
810 
811     return first + ics->offset;
812 }
813 
814 #define SPAPR_IRQ_XICS_LEGACY_NR_IRQS     0x400
815 
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,
820 
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,
833 };
834