xref: /qemu/hw/ppc/spapr_irq.c (revision 605994e5b7d17dcc275465b4f89816d29105b238)
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 static const TypeInfo spapr_intc_info = {
27     .name = TYPE_SPAPR_INTC,
28     .parent = TYPE_INTERFACE,
29     .class_size = sizeof(SpaprInterruptControllerClass),
30 };
31 
32 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis)
33 {
34     spapr->irq_map_nr = nr_msis;
35     spapr->irq_map = bitmap_new(spapr->irq_map_nr);
36 }
37 
38 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align,
39                         Error **errp)
40 {
41     int irq;
42 
43     /*
44      * The 'align_mask' parameter of bitmap_find_next_zero_area()
45      * should be one less than a power of 2; 0 means no
46      * alignment. Adapt the 'align' value of the former allocator
47      * to fit the requirements of bitmap_find_next_zero_area()
48      */
49     align -= 1;
50 
51     irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num,
52                                      align);
53     if (irq == spapr->irq_map_nr) {
54         error_setg(errp, "can't find a free %d-IRQ block", num);
55         return -1;
56     }
57 
58     bitmap_set(spapr->irq_map, irq, num);
59 
60     return irq + SPAPR_IRQ_MSI;
61 }
62 
63 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num)
64 {
65     bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
66 }
67 
68 int spapr_irq_init_kvm(int (*fn)(SpaprInterruptController *, Error **),
69                        SpaprInterruptController *intc,
70                        Error **errp)
71 {
72     MachineState *machine = MACHINE(qdev_get_machine());
73     Error *local_err = NULL;
74 
75     if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
76         if (fn(intc, &local_err) < 0) {
77             if (machine_kernel_irqchip_required(machine)) {
78                 error_prepend(&local_err,
79                               "kernel_irqchip requested but unavailable: ");
80                 error_propagate(errp, local_err);
81                 return -1;
82             }
83 
84             /*
85              * We failed to initialize the KVM device, fallback to
86              * emulated mode
87              */
88             error_prepend(&local_err,
89                           "kernel_irqchip allowed but unavailable: ");
90             error_append_hint(&local_err,
91                               "Falling back to kernel-irqchip=off\n");
92             warn_report_err(local_err);
93         }
94     }
95 
96     return 0;
97 }
98 
99 /*
100  * XICS IRQ backend.
101  */
102 
103 SpaprIrq spapr_irq_xics = {
104     .nr_xirqs    = SPAPR_NR_XIRQS,
105     .nr_msis     = SPAPR_NR_MSIS,
106     .xics        = true,
107     .xive        = false,
108 };
109 
110 /*
111  * XIVE IRQ backend.
112  */
113 
114 SpaprIrq spapr_irq_xive = {
115     .nr_xirqs    = SPAPR_NR_XIRQS,
116     .nr_msis     = SPAPR_NR_MSIS,
117     .xics        = false,
118     .xive        = true,
119 };
120 
121 /*
122  * Dual XIVE and XICS IRQ backend.
123  *
124  * Both interrupt mode, XIVE and XICS, objects are created but the
125  * machine starts in legacy interrupt mode (XICS). It can be changed
126  * by the CAS negotiation process and, in that case, the new mode is
127  * activated after an extra machine reset.
128  */
129 
130 /*
131  * Define values in sync with the XIVE and XICS backend
132  */
133 SpaprIrq spapr_irq_dual = {
134     .nr_xirqs    = SPAPR_NR_XIRQS,
135     .nr_msis     = SPAPR_NR_MSIS,
136     .xics        = true,
137     .xive        = true,
138 };
139 
140 
141 static int spapr_irq_check(SpaprMachineState *spapr, Error **errp)
142 {
143     MachineState *machine = MACHINE(spapr);
144 
145     /*
146      * Sanity checks on non-P9 machines. On these, XIVE is not
147      * advertised, see spapr_dt_ov5_platform_support()
148      */
149     if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00,
150                                0, spapr->max_compat_pvr)) {
151         /*
152          * If the 'dual' interrupt mode is selected, force XICS as CAS
153          * negotiation is useless.
154          */
155         if (spapr->irq == &spapr_irq_dual) {
156             spapr->irq = &spapr_irq_xics;
157             return 0;
158         }
159 
160         /*
161          * Non-P9 machines using only XIVE is a bogus setup. We have two
162          * scenarios to take into account because of the compat mode:
163          *
164          * 1. POWER7/8 machines should fail to init later on when creating
165          *    the XIVE interrupt presenters because a POWER9 exception
166          *    model is required.
167 
168          * 2. POWER9 machines using the POWER8 compat mode won't fail and
169          *    will let the OS boot with a partial XIVE setup : DT
170          *    properties but no hcalls.
171          *
172          * To cover both and not confuse the OS, add an early failure in
173          * QEMU.
174          */
175         if (spapr->irq == &spapr_irq_xive) {
176             error_setg(errp, "XIVE-only machines require a POWER9 CPU");
177             return -1;
178         }
179     }
180 
181     /*
182      * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
183      * re-created. Detect that early to avoid QEMU to exit later when the
184      * guest reboots.
185      */
186     if (kvm_enabled() &&
187         spapr->irq == &spapr_irq_dual &&
188         machine_kernel_irqchip_required(machine) &&
189         xics_kvm_has_broken_disconnect(spapr)) {
190         error_setg(errp, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
191         return -1;
192     }
193 
194     return 0;
195 }
196 
197 /*
198  * sPAPR IRQ frontend routines for devices
199  */
200 #define ALL_INTCS(spapr_) \
201     { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), }
202 
203 int spapr_irq_cpu_intc_create(SpaprMachineState *spapr,
204                               PowerPCCPU *cpu, Error **errp)
205 {
206     SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
207     int i;
208     int rc;
209 
210     for (i = 0; i < ARRAY_SIZE(intcs); i++) {
211         SpaprInterruptController *intc = intcs[i];
212         if (intc) {
213             SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
214             rc = sicc->cpu_intc_create(intc, cpu, errp);
215             if (rc < 0) {
216                 return rc;
217             }
218         }
219     }
220 
221     return 0;
222 }
223 
224 static void spapr_set_irq(void *opaque, int irq, int level)
225 {
226     SpaprMachineState *spapr = SPAPR_MACHINE(opaque);
227     SpaprInterruptControllerClass *sicc
228         = SPAPR_INTC_GET_CLASS(spapr->active_intc);
229 
230     sicc->set_irq(spapr->active_intc, irq, level);
231 }
232 
233 void spapr_irq_print_info(SpaprMachineState *spapr, Monitor *mon)
234 {
235     SpaprInterruptControllerClass *sicc
236         = SPAPR_INTC_GET_CLASS(spapr->active_intc);
237 
238     sicc->print_info(spapr->active_intc, mon);
239 }
240 
241 void spapr_irq_dt(SpaprMachineState *spapr, uint32_t nr_servers,
242                   void *fdt, uint32_t phandle)
243 {
244     SpaprInterruptControllerClass *sicc
245         = SPAPR_INTC_GET_CLASS(spapr->active_intc);
246 
247     sicc->dt(spapr->active_intc, nr_servers, fdt, phandle);
248 }
249 
250 void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
251 {
252     MachineState *machine = MACHINE(spapr);
253 
254     if (machine_kernel_irqchip_split(machine)) {
255         error_setg(errp, "kernel_irqchip split mode not supported on pseries");
256         return;
257     }
258 
259     if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
260         error_setg(errp,
261                    "kernel_irqchip requested but only available with KVM");
262         return;
263     }
264 
265     if (spapr_irq_check(spapr, errp) < 0) {
266         return;
267     }
268 
269     /* Initialize the MSI IRQ allocator. */
270     if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
271         spapr_irq_msi_init(spapr, spapr->irq->nr_msis);
272     }
273 
274     if (spapr->irq->xics) {
275         Error *local_err = NULL;
276         Object *obj;
277 
278         obj = object_new(TYPE_ICS_SPAPR);
279         object_property_add_child(OBJECT(spapr), "ics", obj, &local_err);
280         if (local_err) {
281             error_propagate(errp, local_err);
282             return;
283         }
284 
285         object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
286                                        &local_err);
287         if (local_err) {
288             error_propagate(errp, local_err);
289             return;
290         }
291 
292         object_property_set_int(obj, spapr->irq->nr_xirqs, "nr-irqs",
293                                 &local_err);
294         if (local_err) {
295             error_propagate(errp, local_err);
296             return;
297         }
298 
299         object_property_set_bool(obj, true, "realized", &local_err);
300         if (local_err) {
301             error_propagate(errp, local_err);
302             return;
303         }
304 
305         spapr->ics = ICS_SPAPR(obj);
306     }
307 
308     if (spapr->irq->xive) {
309         uint32_t nr_servers = spapr_max_server_number(spapr);
310         DeviceState *dev;
311         int i;
312 
313         dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
314         qdev_prop_set_uint32(dev, "nr-irqs",
315                              spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE);
316         /*
317          * 8 XIVE END structures per CPU. One for each available
318          * priority
319          */
320         qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
321         qdev_init_nofail(dev);
322 
323         spapr->xive = SPAPR_XIVE(dev);
324 
325         /* Enable the CPU IPIs */
326         for (i = 0; i < nr_servers; ++i) {
327             SpaprInterruptControllerClass *sicc
328                 = SPAPR_INTC_GET_CLASS(spapr->xive);
329 
330             if (sicc->claim_irq(SPAPR_INTC(spapr->xive), SPAPR_IRQ_IPI + i,
331                                 false, errp) < 0) {
332                 return;
333             }
334         }
335 
336         spapr_xive_hcall_init(spapr);
337     }
338 
339     spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
340                                       spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE);
341 }
342 
343 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
344 {
345     SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
346     int i;
347     int rc;
348 
349     assert(irq >= SPAPR_XIRQ_BASE);
350     assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
351 
352     for (i = 0; i < ARRAY_SIZE(intcs); i++) {
353         SpaprInterruptController *intc = intcs[i];
354         if (intc) {
355             SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
356             rc = sicc->claim_irq(intc, irq, lsi, errp);
357             if (rc < 0) {
358                 return rc;
359             }
360         }
361     }
362 
363     return 0;
364 }
365 
366 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
367 {
368     SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
369     int i, j;
370 
371     assert(irq >= SPAPR_XIRQ_BASE);
372     assert((irq + num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
373 
374     for (i = irq; i < (irq + num); i++) {
375         for (j = 0; j < ARRAY_SIZE(intcs); j++) {
376             SpaprInterruptController *intc = intcs[j];
377 
378             if (intc) {
379                 SpaprInterruptControllerClass *sicc
380                     = SPAPR_INTC_GET_CLASS(intc);
381                 sicc->free_irq(intc, i);
382             }
383         }
384     }
385 }
386 
387 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq)
388 {
389     /*
390      * This interface is basically for VIO and PHB devices to find the
391      * right qemu_irq to manipulate, so we only allow access to the
392      * external irqs for now.  Currently anything which needs to
393      * access the IPIs most naturally gets there via the guest side
394      * interfaces, we can change this if we need to in future.
395      */
396     assert(irq >= SPAPR_XIRQ_BASE);
397     assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
398 
399     if (spapr->ics) {
400         assert(ics_valid_irq(spapr->ics, irq));
401     }
402     if (spapr->xive) {
403         assert(irq < spapr->xive->nr_irqs);
404         assert(xive_eas_is_valid(&spapr->xive->eat[irq]));
405     }
406 
407     return spapr->qirqs[irq];
408 }
409 
410 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id)
411 {
412     SpaprInterruptControllerClass *sicc;
413 
414     spapr_irq_update_active_intc(spapr);
415     sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc);
416     return sicc->post_load(spapr->active_intc, version_id);
417 }
418 
419 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp)
420 {
421     assert(!spapr->irq_map || bitmap_empty(spapr->irq_map, spapr->irq_map_nr));
422 
423     spapr_irq_update_active_intc(spapr);
424 }
425 
426 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp)
427 {
428     const char *nodename = "interrupt-controller";
429     int offset, phandle;
430 
431     offset = fdt_subnode_offset(fdt, 0, nodename);
432     if (offset < 0) {
433         error_setg(errp, "Can't find node \"%s\": %s",
434                    nodename, fdt_strerror(offset));
435         return -1;
436     }
437 
438     phandle = fdt_get_phandle(fdt, offset);
439     if (!phandle) {
440         error_setg(errp, "Can't get phandle of node \"%s\"", nodename);
441         return -1;
442     }
443 
444     return phandle;
445 }
446 
447 static void set_active_intc(SpaprMachineState *spapr,
448                             SpaprInterruptController *new_intc)
449 {
450     SpaprInterruptControllerClass *sicc;
451 
452     assert(new_intc);
453 
454     if (new_intc == spapr->active_intc) {
455         /* Nothing to do */
456         return;
457     }
458 
459     if (spapr->active_intc) {
460         sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc);
461         if (sicc->deactivate) {
462             sicc->deactivate(spapr->active_intc);
463         }
464     }
465 
466     sicc = SPAPR_INTC_GET_CLASS(new_intc);
467     if (sicc->activate) {
468         sicc->activate(new_intc, &error_fatal);
469     }
470 
471     spapr->active_intc = new_intc;
472 }
473 
474 void spapr_irq_update_active_intc(SpaprMachineState *spapr)
475 {
476     SpaprInterruptController *new_intc;
477 
478     if (!spapr->ics) {
479         /*
480          * XXX before we run CAS, ov5_cas is initialized empty, which
481          * indicates XICS, even if we have ic-mode=xive.  TODO: clean
482          * up the CAS path so that we have a clearer way of handling
483          * this.
484          */
485         new_intc = SPAPR_INTC(spapr->xive);
486     } else if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
487         new_intc = SPAPR_INTC(spapr->xive);
488     } else {
489         new_intc = SPAPR_INTC(spapr->ics);
490     }
491 
492     set_active_intc(spapr, new_intc);
493 }
494 
495 /*
496  * XICS legacy routines - to deprecate one day
497  */
498 
499 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
500 {
501     int first, i;
502 
503     for (first = 0; first < ics->nr_irqs; first += alignnum) {
504         if (num > (ics->nr_irqs - first)) {
505             return -1;
506         }
507         for (i = first; i < first + num; ++i) {
508             if (!ics_irq_free(ics, i)) {
509                 break;
510             }
511         }
512         if (i == (first + num)) {
513             return first;
514         }
515     }
516 
517     return -1;
518 }
519 
520 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp)
521 {
522     ICSState *ics = spapr->ics;
523     int first = -1;
524 
525     assert(ics);
526 
527     /*
528      * MSIMesage::data is used for storing VIRQ so
529      * it has to be aligned to num to support multiple
530      * MSI vectors. MSI-X is not affected by this.
531      * The hint is used for the first IRQ, the rest should
532      * be allocated continuously.
533      */
534     if (align) {
535         assert((num == 1) || (num == 2) || (num == 4) ||
536                (num == 8) || (num == 16) || (num == 32));
537         first = ics_find_free_block(ics, num, num);
538     } else {
539         first = ics_find_free_block(ics, num, 1);
540     }
541 
542     if (first < 0) {
543         error_setg(errp, "can't find a free %d-IRQ block", num);
544         return -1;
545     }
546 
547     return first + ics->offset;
548 }
549 
550 #define SPAPR_IRQ_XICS_LEGACY_NR_XIRQS     0x400
551 
552 SpaprIrq spapr_irq_xics_legacy = {
553     .nr_xirqs    = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS,
554     .nr_msis     = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS,
555     .xics        = true,
556     .xive        = false,
557 };
558 
559 static void spapr_irq_register_types(void)
560 {
561     type_register_static(&spapr_intc_info);
562 }
563 
564 type_init(spapr_irq_register_types)
565