spapr, xics, xive: Match signatures for XICS and XIVE KVM connect routines
[qemu/ar7.git] / hw / ppc / spapr_irq.c
blob5c8ffb27dab470c1cb5bd06d4bbb7170aaaba45f
1 /*
2 * QEMU PowerPC sPAPR IRQ interface
4 * Copyright (c) 2018, IBM Corporation.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/log.h"
12 #include "qemu/error-report.h"
13 #include "qapi/error.h"
14 #include "hw/irq.h"
15 #include "hw/ppc/spapr.h"
16 #include "hw/ppc/spapr_cpu_core.h"
17 #include "hw/ppc/spapr_xive.h"
18 #include "hw/ppc/xics.h"
19 #include "hw/ppc/xics_spapr.h"
20 #include "hw/qdev-properties.h"
21 #include "cpu-models.h"
22 #include "sysemu/kvm.h"
24 #include "trace.h"
26 static const TypeInfo spapr_intc_info = {
27 .name = TYPE_SPAPR_INTC,
28 .parent = TYPE_INTERFACE,
29 .class_size = sizeof(SpaprInterruptControllerClass),
32 void spapr_irq_msi_init(SpaprMachineState *spapr, uint32_t nr_msis)
34 spapr->irq_map_nr = nr_msis;
35 spapr->irq_map = bitmap_new(spapr->irq_map_nr);
38 int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align,
39 Error **errp)
41 int irq;
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()
49 align -= 1;
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;
58 bitmap_set(spapr->irq_map, irq, num);
60 return irq + SPAPR_IRQ_MSI;
63 void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num)
65 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
68 static void spapr_irq_init_kvm(SpaprMachineState *spapr,
69 SpaprIrq *irq, Error **errp)
71 MachineState *machine = MACHINE(spapr);
72 Error *local_err = NULL;
74 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
75 irq->init_kvm(spapr, &local_err);
76 if (local_err && machine_kernel_irqchip_required(machine)) {
77 error_prepend(&local_err,
78 "kernel_irqchip requested but unavailable: ");
79 error_propagate(errp, local_err);
80 return;
83 if (!local_err) {
84 return;
88 * We failed to initialize the KVM device, fallback to
89 * emulated mode
91 error_prepend(&local_err, "kernel_irqchip allowed but unavailable: ");
92 error_append_hint(&local_err, "Falling back to kernel-irqchip=off\n");
93 warn_report_err(local_err);
98 * XICS IRQ backend.
101 static int spapr_irq_post_load_xics(SpaprMachineState *spapr, int version_id)
103 if (!kvm_irqchip_in_kernel()) {
104 CPUState *cs;
105 CPU_FOREACH(cs) {
106 PowerPCCPU *cpu = POWERPC_CPU(cs);
107 icp_resend(spapr_cpu_state(cpu)->icp);
110 return 0;
113 static void spapr_irq_reset_xics(SpaprMachineState *spapr, Error **errp)
115 Error *local_err = NULL;
117 spapr_irq_init_kvm(spapr, &spapr_irq_xics, &local_err);
118 if (local_err) {
119 error_propagate(errp, local_err);
120 return;
124 static void spapr_irq_init_kvm_xics(SpaprMachineState *spapr, Error **errp)
126 if (kvm_enabled()) {
127 xics_kvm_connect(SPAPR_INTC(spapr->ics), errp);
131 SpaprIrq spapr_irq_xics = {
132 .nr_xirqs = SPAPR_NR_XIRQS,
133 .nr_msis = SPAPR_NR_MSIS,
134 .xics = true,
135 .xive = false,
137 .post_load = spapr_irq_post_load_xics,
138 .reset = spapr_irq_reset_xics,
139 .init_kvm = spapr_irq_init_kvm_xics,
143 * XIVE IRQ backend.
146 static int spapr_irq_post_load_xive(SpaprMachineState *spapr, int version_id)
148 return spapr_xive_post_load(spapr->xive, version_id);
151 static void spapr_irq_reset_xive(SpaprMachineState *spapr, Error **errp)
153 CPUState *cs;
154 Error *local_err = NULL;
156 CPU_FOREACH(cs) {
157 PowerPCCPU *cpu = POWERPC_CPU(cs);
159 /* (TCG) Set the OS CAM line of the thread interrupt context. */
160 spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx);
163 spapr_irq_init_kvm(spapr, &spapr_irq_xive, &local_err);
164 if (local_err) {
165 error_propagate(errp, local_err);
166 return;
169 /* Activate the XIVE MMIOs */
170 spapr_xive_mmio_set_enabled(spapr->xive, true);
173 static void spapr_irq_init_kvm_xive(SpaprMachineState *spapr, Error **errp)
175 if (kvm_enabled()) {
176 kvmppc_xive_connect(SPAPR_INTC(spapr->xive), errp);
180 SpaprIrq spapr_irq_xive = {
181 .nr_xirqs = SPAPR_NR_XIRQS,
182 .nr_msis = SPAPR_NR_MSIS,
183 .xics = false,
184 .xive = true,
186 .post_load = spapr_irq_post_load_xive,
187 .reset = spapr_irq_reset_xive,
188 .init_kvm = spapr_irq_init_kvm_xive,
192 * Dual XIVE and XICS IRQ backend.
194 * Both interrupt mode, XIVE and XICS, objects are created but the
195 * machine starts in legacy interrupt mode (XICS). It can be changed
196 * by the CAS negotiation process and, in that case, the new mode is
197 * activated after an extra machine reset.
201 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the
202 * default.
204 static SpaprIrq *spapr_irq_current(SpaprMachineState *spapr)
206 return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ?
207 &spapr_irq_xive : &spapr_irq_xics;
210 static int spapr_irq_post_load_dual(SpaprMachineState *spapr, int version_id)
213 * Force a reset of the XIVE backend after migration. The machine
214 * defaults to XICS at startup.
216 if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
217 if (kvm_irqchip_in_kernel()) {
218 xics_kvm_disconnect(SPAPR_INTC(spapr->ics));
220 spapr_irq_xive.reset(spapr, &error_fatal);
223 return spapr_irq_current(spapr)->post_load(spapr, version_id);
226 static void spapr_irq_reset_dual(SpaprMachineState *spapr, Error **errp)
229 * Deactivate the XIVE MMIOs. The XIVE backend will reenable them
230 * if selected.
232 spapr_xive_mmio_set_enabled(spapr->xive, false);
234 /* Destroy all KVM devices */
235 if (kvm_irqchip_in_kernel()) {
236 xics_kvm_disconnect(SPAPR_INTC(spapr->ics));
237 kvmppc_xive_disconnect(SPAPR_INTC(spapr->xive));
240 spapr_irq_current(spapr)->reset(spapr, errp);
244 * Define values in sync with the XIVE and XICS backend
246 SpaprIrq spapr_irq_dual = {
247 .nr_xirqs = SPAPR_NR_XIRQS,
248 .nr_msis = SPAPR_NR_MSIS,
249 .xics = true,
250 .xive = true,
252 .post_load = spapr_irq_post_load_dual,
253 .reset = spapr_irq_reset_dual,
254 .init_kvm = NULL, /* should not be used */
258 static int spapr_irq_check(SpaprMachineState *spapr, Error **errp)
260 MachineState *machine = MACHINE(spapr);
263 * Sanity checks on non-P9 machines. On these, XIVE is not
264 * advertised, see spapr_dt_ov5_platform_support()
266 if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00,
267 0, spapr->max_compat_pvr)) {
269 * If the 'dual' interrupt mode is selected, force XICS as CAS
270 * negotiation is useless.
272 if (spapr->irq == &spapr_irq_dual) {
273 spapr->irq = &spapr_irq_xics;
274 return 0;
278 * Non-P9 machines using only XIVE is a bogus setup. We have two
279 * scenarios to take into account because of the compat mode:
281 * 1. POWER7/8 machines should fail to init later on when creating
282 * the XIVE interrupt presenters because a POWER9 exception
283 * model is required.
285 * 2. POWER9 machines using the POWER8 compat mode won't fail and
286 * will let the OS boot with a partial XIVE setup : DT
287 * properties but no hcalls.
289 * To cover both and not confuse the OS, add an early failure in
290 * QEMU.
292 if (spapr->irq == &spapr_irq_xive) {
293 error_setg(errp, "XIVE-only machines require a POWER9 CPU");
294 return -1;
299 * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
300 * re-created. Detect that early to avoid QEMU to exit later when the
301 * guest reboots.
303 if (kvm_enabled() &&
304 spapr->irq == &spapr_irq_dual &&
305 machine_kernel_irqchip_required(machine) &&
306 xics_kvm_has_broken_disconnect(spapr)) {
307 error_setg(errp, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
308 return -1;
311 return 0;
315 * sPAPR IRQ frontend routines for devices
317 #define ALL_INTCS(spapr_) \
318 { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), }
320 int spapr_irq_cpu_intc_create(SpaprMachineState *spapr,
321 PowerPCCPU *cpu, Error **errp)
323 SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
324 int i;
325 int rc;
327 for (i = 0; i < ARRAY_SIZE(intcs); i++) {
328 SpaprInterruptController *intc = intcs[i];
329 if (intc) {
330 SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
331 rc = sicc->cpu_intc_create(intc, cpu, errp);
332 if (rc < 0) {
333 return rc;
338 return 0;
341 static void spapr_set_irq(void *opaque, int irq, int level)
343 SpaprMachineState *spapr = SPAPR_MACHINE(opaque);
344 SpaprInterruptControllerClass *sicc
345 = SPAPR_INTC_GET_CLASS(spapr->active_intc);
347 sicc->set_irq(spapr->active_intc, irq, level);
350 void spapr_irq_print_info(SpaprMachineState *spapr, Monitor *mon)
352 SpaprInterruptControllerClass *sicc
353 = SPAPR_INTC_GET_CLASS(spapr->active_intc);
355 sicc->print_info(spapr->active_intc, mon);
358 void spapr_irq_dt(SpaprMachineState *spapr, uint32_t nr_servers,
359 void *fdt, uint32_t phandle)
361 SpaprInterruptControllerClass *sicc
362 = SPAPR_INTC_GET_CLASS(spapr->active_intc);
364 sicc->dt(spapr->active_intc, nr_servers, fdt, phandle);
367 void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
369 MachineState *machine = MACHINE(spapr);
371 if (machine_kernel_irqchip_split(machine)) {
372 error_setg(errp, "kernel_irqchip split mode not supported on pseries");
373 return;
376 if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
377 error_setg(errp,
378 "kernel_irqchip requested but only available with KVM");
379 return;
382 if (spapr_irq_check(spapr, errp) < 0) {
383 return;
386 /* Initialize the MSI IRQ allocator. */
387 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
388 spapr_irq_msi_init(spapr, spapr->irq->nr_msis);
391 if (spapr->irq->xics) {
392 Error *local_err = NULL;
393 Object *obj;
395 obj = object_new(TYPE_ICS_SPAPR);
396 object_property_add_child(OBJECT(spapr), "ics", obj, &local_err);
397 if (local_err) {
398 error_propagate(errp, local_err);
399 return;
402 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
403 &local_err);
404 if (local_err) {
405 error_propagate(errp, local_err);
406 return;
409 object_property_set_int(obj, spapr->irq->nr_xirqs, "nr-irqs",
410 &local_err);
411 if (local_err) {
412 error_propagate(errp, local_err);
413 return;
416 object_property_set_bool(obj, true, "realized", &local_err);
417 if (local_err) {
418 error_propagate(errp, local_err);
419 return;
422 spapr->ics = ICS_SPAPR(obj);
425 if (spapr->irq->xive) {
426 uint32_t nr_servers = spapr_max_server_number(spapr);
427 DeviceState *dev;
428 int i;
430 dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
431 qdev_prop_set_uint32(dev, "nr-irqs",
432 spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE);
434 * 8 XIVE END structures per CPU. One for each available
435 * priority
437 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
438 qdev_init_nofail(dev);
440 spapr->xive = SPAPR_XIVE(dev);
442 /* Enable the CPU IPIs */
443 for (i = 0; i < nr_servers; ++i) {
444 SpaprInterruptControllerClass *sicc
445 = SPAPR_INTC_GET_CLASS(spapr->xive);
447 if (sicc->claim_irq(SPAPR_INTC(spapr->xive), SPAPR_IRQ_IPI + i,
448 false, errp) < 0) {
449 return;
453 spapr_xive_hcall_init(spapr);
456 spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
457 spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE);
460 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
462 SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
463 int i;
464 int rc;
466 assert(irq >= SPAPR_XIRQ_BASE);
467 assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
469 for (i = 0; i < ARRAY_SIZE(intcs); i++) {
470 SpaprInterruptController *intc = intcs[i];
471 if (intc) {
472 SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
473 rc = sicc->claim_irq(intc, irq, lsi, errp);
474 if (rc < 0) {
475 return rc;
480 return 0;
483 void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
485 SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
486 int i, j;
488 assert(irq >= SPAPR_XIRQ_BASE);
489 assert((irq + num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
491 for (i = irq; i < (irq + num); i++) {
492 for (j = 0; j < ARRAY_SIZE(intcs); j++) {
493 SpaprInterruptController *intc = intcs[j];
495 if (intc) {
496 SpaprInterruptControllerClass *sicc
497 = SPAPR_INTC_GET_CLASS(intc);
498 sicc->free_irq(intc, i);
504 qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq)
507 * This interface is basically for VIO and PHB devices to find the
508 * right qemu_irq to manipulate, so we only allow access to the
509 * external irqs for now. Currently anything which needs to
510 * access the IPIs most naturally gets there via the guest side
511 * interfaces, we can change this if we need to in future.
513 assert(irq >= SPAPR_XIRQ_BASE);
514 assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
516 if (spapr->ics) {
517 assert(ics_valid_irq(spapr->ics, irq));
519 if (spapr->xive) {
520 assert(irq < spapr->xive->nr_irqs);
521 assert(xive_eas_is_valid(&spapr->xive->eat[irq]));
524 return spapr->qirqs[irq];
527 int spapr_irq_post_load(SpaprMachineState *spapr, int version_id)
529 spapr_irq_update_active_intc(spapr);
530 return spapr->irq->post_load(spapr, version_id);
533 void spapr_irq_reset(SpaprMachineState *spapr, Error **errp)
535 assert(!spapr->irq_map || bitmap_empty(spapr->irq_map, spapr->irq_map_nr));
537 spapr_irq_update_active_intc(spapr);
539 if (spapr->irq->reset) {
540 spapr->irq->reset(spapr, errp);
544 int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp)
546 const char *nodename = "interrupt-controller";
547 int offset, phandle;
549 offset = fdt_subnode_offset(fdt, 0, nodename);
550 if (offset < 0) {
551 error_setg(errp, "Can't find node \"%s\": %s",
552 nodename, fdt_strerror(offset));
553 return -1;
556 phandle = fdt_get_phandle(fdt, offset);
557 if (!phandle) {
558 error_setg(errp, "Can't get phandle of node \"%s\"", nodename);
559 return -1;
562 return phandle;
565 static void set_active_intc(SpaprMachineState *spapr,
566 SpaprInterruptController *new_intc)
568 SpaprInterruptControllerClass *sicc;
570 assert(new_intc);
572 if (new_intc == spapr->active_intc) {
573 /* Nothing to do */
574 return;
577 if (spapr->active_intc) {
578 sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc);
579 if (sicc->deactivate) {
580 sicc->deactivate(spapr->active_intc);
584 sicc = SPAPR_INTC_GET_CLASS(new_intc);
585 if (sicc->activate) {
586 sicc->activate(new_intc, &error_fatal);
589 spapr->active_intc = new_intc;
592 void spapr_irq_update_active_intc(SpaprMachineState *spapr)
594 SpaprInterruptController *new_intc;
596 if (!spapr->ics) {
598 * XXX before we run CAS, ov5_cas is initialized empty, which
599 * indicates XICS, even if we have ic-mode=xive. TODO: clean
600 * up the CAS path so that we have a clearer way of handling
601 * this.
603 new_intc = SPAPR_INTC(spapr->xive);
604 } else if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
605 new_intc = SPAPR_INTC(spapr->xive);
606 } else {
607 new_intc = SPAPR_INTC(spapr->ics);
610 set_active_intc(spapr, new_intc);
614 * XICS legacy routines - to deprecate one day
617 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
619 int first, i;
621 for (first = 0; first < ics->nr_irqs; first += alignnum) {
622 if (num > (ics->nr_irqs - first)) {
623 return -1;
625 for (i = first; i < first + num; ++i) {
626 if (!ics_irq_free(ics, i)) {
627 break;
630 if (i == (first + num)) {
631 return first;
635 return -1;
638 int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp)
640 ICSState *ics = spapr->ics;
641 int first = -1;
643 assert(ics);
646 * MSIMesage::data is used for storing VIRQ so
647 * it has to be aligned to num to support multiple
648 * MSI vectors. MSI-X is not affected by this.
649 * The hint is used for the first IRQ, the rest should
650 * be allocated continuously.
652 if (align) {
653 assert((num == 1) || (num == 2) || (num == 4) ||
654 (num == 8) || (num == 16) || (num == 32));
655 first = ics_find_free_block(ics, num, num);
656 } else {
657 first = ics_find_free_block(ics, num, 1);
660 if (first < 0) {
661 error_setg(errp, "can't find a free %d-IRQ block", num);
662 return -1;
665 return first + ics->offset;
668 #define SPAPR_IRQ_XICS_LEGACY_NR_XIRQS 0x400
670 SpaprIrq spapr_irq_xics_legacy = {
671 .nr_xirqs = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS,
672 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_XIRQS,
673 .xics = true,
674 .xive = false,
676 .post_load = spapr_irq_post_load_xics,
677 .reset = spapr_irq_reset_xics,
678 .init_kvm = spapr_irq_init_kvm_xics,
681 static void spapr_irq_register_types(void)
683 type_register_static(&spapr_intc_info);
686 type_init(spapr_irq_register_types)