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.
10 #include "qemu/osdep.h"
12 #include "qemu/error-report.h"
13 #include "qapi/error.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"
26 static const TypeInfo spapr_intc_info
= {
27 .name
= TYPE_SPAPR_INTC
,
28 .parent
= TYPE_INTERFACE
,
29 .class_size
= sizeof(SpaprInterruptControllerClass
),
32 static void spapr_irq_msi_init(SpaprMachineState
*spapr
)
34 if (SPAPR_MACHINE_GET_CLASS(spapr
)->legacy_irq_allocation
) {
35 /* Legacy mode doesn't use this allocator */
39 spapr
->irq_map_nr
= spapr_irq_nr_msis(spapr
);
40 spapr
->irq_map
= bitmap_new(spapr
->irq_map_nr
);
43 int spapr_irq_msi_alloc(SpaprMachineState
*spapr
, uint32_t num
, bool align
,
49 * The 'align_mask' parameter of bitmap_find_next_zero_area()
50 * should be one less than a power of 2; 0 means no
51 * alignment. Adapt the 'align' value of the former allocator
52 * to fit the requirements of bitmap_find_next_zero_area()
56 irq
= bitmap_find_next_zero_area(spapr
->irq_map
, spapr
->irq_map_nr
, 0, num
,
58 if (irq
== spapr
->irq_map_nr
) {
59 error_setg(errp
, "can't find a free %d-IRQ block", num
);
63 bitmap_set(spapr
->irq_map
, irq
, num
);
65 return irq
+ SPAPR_IRQ_MSI
;
68 void spapr_irq_msi_free(SpaprMachineState
*spapr
, int irq
, uint32_t num
)
70 bitmap_clear(spapr
->irq_map
, irq
- SPAPR_IRQ_MSI
, num
);
73 int spapr_irq_init_kvm(SpaprInterruptControllerInitKvm fn
,
74 SpaprInterruptController
*intc
,
78 Error
*local_err
= NULL
;
80 if (kvm_enabled() && kvm_kernel_irqchip_allowed()) {
81 if (fn(intc
, nr_servers
, &local_err
) < 0) {
82 if (kvm_kernel_irqchip_required()) {
83 error_prepend(&local_err
,
84 "kernel_irqchip requested but unavailable: ");
85 error_propagate(errp
, local_err
);
90 * We failed to initialize the KVM device, fallback to
93 error_prepend(&local_err
,
94 "kernel_irqchip allowed but unavailable: ");
95 error_append_hint(&local_err
,
96 "Falling back to kernel-irqchip=off\n");
97 warn_report_err(local_err
);
108 SpaprIrq spapr_irq_xics
= {
117 SpaprIrq spapr_irq_xive
= {
123 * Dual XIVE and XICS IRQ backend.
125 * Both interrupt mode, XIVE and XICS, objects are created but the
126 * machine starts in legacy interrupt mode (XICS). It can be changed
127 * by the CAS negotiation process and, in that case, the new mode is
128 * activated after an extra machine reset.
132 * Define values in sync with the XIVE and XICS backend
134 SpaprIrq spapr_irq_dual
= {
140 static int spapr_irq_check(SpaprMachineState
*spapr
, Error
**errp
)
142 MachineState
*machine
= MACHINE(spapr
);
145 * Sanity checks on non-P9 machines. On these, XIVE is not
146 * advertised, see spapr_dt_ov5_platform_support()
148 if (!ppc_type_check_compat(machine
->cpu_type
, CPU_POWERPC_LOGICAL_3_00
,
149 0, spapr
->max_compat_pvr
)) {
151 * If the 'dual' interrupt mode is selected, force XICS as CAS
152 * negotiation is useless.
154 if (spapr
->irq
== &spapr_irq_dual
) {
155 spapr
->irq
= &spapr_irq_xics
;
160 * Non-P9 machines using only XIVE is a bogus setup. We have two
161 * scenarios to take into account because of the compat mode:
163 * 1. POWER7/8 machines should fail to init later on when creating
164 * the XIVE interrupt presenters because a POWER9 exception
167 * 2. POWER9 machines using the POWER8 compat mode won't fail and
168 * will let the OS boot with a partial XIVE setup : DT
169 * properties but no hcalls.
171 * To cover both and not confuse the OS, add an early failure in
174 if (spapr
->irq
== &spapr_irq_xive
) {
175 error_setg(errp
, "XIVE-only machines require a POWER9 CPU");
181 * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
182 * re-created. Detect that early to avoid QEMU to exit later when the
186 spapr
->irq
== &spapr_irq_dual
&&
187 kvm_kernel_irqchip_required() &&
188 xics_kvm_has_broken_disconnect(spapr
)) {
189 error_setg(errp
, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
197 * sPAPR IRQ frontend routines for devices
199 #define ALL_INTCS(spapr_) \
200 { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), }
202 int spapr_irq_cpu_intc_create(SpaprMachineState
*spapr
,
203 PowerPCCPU
*cpu
, Error
**errp
)
205 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
209 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
210 SpaprInterruptController
*intc
= intcs
[i
];
212 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
213 rc
= sicc
->cpu_intc_create(intc
, cpu
, errp
);
223 void spapr_irq_cpu_intc_reset(SpaprMachineState
*spapr
, PowerPCCPU
*cpu
)
225 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
228 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
229 SpaprInterruptController
*intc
= intcs
[i
];
231 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
232 sicc
->cpu_intc_reset(intc
, cpu
);
237 void spapr_irq_cpu_intc_destroy(SpaprMachineState
*spapr
, PowerPCCPU
*cpu
)
239 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
242 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
243 SpaprInterruptController
*intc
= intcs
[i
];
245 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
246 sicc
->cpu_intc_destroy(intc
, cpu
);
251 static void spapr_set_irq(void *opaque
, int irq
, int level
)
253 SpaprMachineState
*spapr
= SPAPR_MACHINE(opaque
);
254 SpaprInterruptControllerClass
*sicc
255 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
257 sicc
->set_irq(spapr
->active_intc
, irq
, level
);
260 void spapr_irq_print_info(SpaprMachineState
*spapr
, Monitor
*mon
)
262 SpaprInterruptControllerClass
*sicc
263 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
265 sicc
->print_info(spapr
->active_intc
, mon
);
268 void spapr_irq_dt(SpaprMachineState
*spapr
, uint32_t nr_servers
,
269 void *fdt
, uint32_t phandle
)
271 SpaprInterruptControllerClass
*sicc
272 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
274 sicc
->dt(spapr
->active_intc
, nr_servers
, fdt
, phandle
);
277 uint32_t spapr_irq_nr_msis(SpaprMachineState
*spapr
)
279 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
281 if (smc
->legacy_irq_allocation
) {
282 return smc
->nr_xirqs
;
284 return SPAPR_XIRQ_BASE
+ smc
->nr_xirqs
- SPAPR_IRQ_MSI
;
288 void spapr_irq_init(SpaprMachineState
*spapr
, Error
**errp
)
290 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
292 if (kvm_enabled() && kvm_kernel_irqchip_split()) {
293 error_setg(errp
, "kernel_irqchip split mode not supported on pseries");
297 if (spapr_irq_check(spapr
, errp
) < 0) {
301 /* Initialize the MSI IRQ allocator. */
302 spapr_irq_msi_init(spapr
);
304 if (spapr
->irq
->xics
) {
305 Error
*local_err
= NULL
;
308 obj
= object_new(TYPE_ICS_SPAPR
);
310 object_property_add_child(OBJECT(spapr
), "ics", obj
);
311 object_property_set_link(obj
, OBJECT(spapr
), ICS_PROP_XICS
,
313 object_property_set_int(obj
, smc
->nr_xirqs
, "nr-irqs", &error_abort
);
314 qdev_realize(DEVICE(obj
), NULL
, &local_err
);
316 error_propagate(errp
, local_err
);
320 spapr
->ics
= ICS_SPAPR(obj
);
323 if (spapr
->irq
->xive
) {
324 uint32_t nr_servers
= spapr_max_server_number(spapr
);
328 dev
= qdev_new(TYPE_SPAPR_XIVE
);
329 qdev_prop_set_uint32(dev
, "nr-irqs", smc
->nr_xirqs
+ SPAPR_XIRQ_BASE
);
331 * 8 XIVE END structures per CPU. One for each available
334 qdev_prop_set_uint32(dev
, "nr-ends", nr_servers
<< 3);
335 object_property_set_link(OBJECT(dev
), OBJECT(spapr
), "xive-fabric",
337 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
339 spapr
->xive
= SPAPR_XIVE(dev
);
341 /* Enable the CPU IPIs */
342 for (i
= 0; i
< nr_servers
; ++i
) {
343 SpaprInterruptControllerClass
*sicc
344 = SPAPR_INTC_GET_CLASS(spapr
->xive
);
346 if (sicc
->claim_irq(SPAPR_INTC(spapr
->xive
), SPAPR_IRQ_IPI
+ i
,
352 spapr_xive_hcall_init(spapr
);
355 spapr
->qirqs
= qemu_allocate_irqs(spapr_set_irq
, spapr
,
356 smc
->nr_xirqs
+ SPAPR_XIRQ_BASE
);
359 * Mostly we don't actually need this until reset, except that not
360 * having this set up can cause VFIO devices to issue a
361 * false-positive warning during realize(), because they don't yet
362 * have an in-kernel irq chip.
364 spapr_irq_update_active_intc(spapr
);
367 int spapr_irq_claim(SpaprMachineState
*spapr
, int irq
, bool lsi
, Error
**errp
)
369 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
371 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
374 assert(irq
>= SPAPR_XIRQ_BASE
);
375 assert(irq
< (smc
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
377 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
378 SpaprInterruptController
*intc
= intcs
[i
];
380 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
381 rc
= sicc
->claim_irq(intc
, irq
, lsi
, errp
);
391 void spapr_irq_free(SpaprMachineState
*spapr
, int irq
, int num
)
393 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
395 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
397 assert(irq
>= SPAPR_XIRQ_BASE
);
398 assert((irq
+ num
) <= (smc
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
400 for (i
= irq
; i
< (irq
+ num
); i
++) {
401 for (j
= 0; j
< ARRAY_SIZE(intcs
); j
++) {
402 SpaprInterruptController
*intc
= intcs
[j
];
405 SpaprInterruptControllerClass
*sicc
406 = SPAPR_INTC_GET_CLASS(intc
);
407 sicc
->free_irq(intc
, i
);
413 qemu_irq
spapr_qirq(SpaprMachineState
*spapr
, int irq
)
415 SpaprMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
418 * This interface is basically for VIO and PHB devices to find the
419 * right qemu_irq to manipulate, so we only allow access to the
420 * external irqs for now. Currently anything which needs to
421 * access the IPIs most naturally gets there via the guest side
422 * interfaces, we can change this if we need to in future.
424 assert(irq
>= SPAPR_XIRQ_BASE
);
425 assert(irq
< (smc
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
428 assert(ics_valid_irq(spapr
->ics
, irq
));
431 assert(irq
< spapr
->xive
->nr_irqs
);
432 assert(xive_eas_is_valid(&spapr
->xive
->eat
[irq
]));
435 return spapr
->qirqs
[irq
];
438 int spapr_irq_post_load(SpaprMachineState
*spapr
, int version_id
)
440 SpaprInterruptControllerClass
*sicc
;
442 spapr_irq_update_active_intc(spapr
);
443 sicc
= SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
444 return sicc
->post_load(spapr
->active_intc
, version_id
);
447 void spapr_irq_reset(SpaprMachineState
*spapr
, Error
**errp
)
449 assert(!spapr
->irq_map
|| bitmap_empty(spapr
->irq_map
, spapr
->irq_map_nr
));
451 spapr_irq_update_active_intc(spapr
);
454 int spapr_irq_get_phandle(SpaprMachineState
*spapr
, void *fdt
, Error
**errp
)
456 const char *nodename
= "interrupt-controller";
459 offset
= fdt_subnode_offset(fdt
, 0, nodename
);
461 error_setg(errp
, "Can't find node \"%s\": %s",
462 nodename
, fdt_strerror(offset
));
466 phandle
= fdt_get_phandle(fdt
, offset
);
468 error_setg(errp
, "Can't get phandle of node \"%s\"", nodename
);
475 static void set_active_intc(SpaprMachineState
*spapr
,
476 SpaprInterruptController
*new_intc
)
478 SpaprInterruptControllerClass
*sicc
;
479 uint32_t nr_servers
= spapr_max_server_number(spapr
);
483 if (new_intc
== spapr
->active_intc
) {
488 if (spapr
->active_intc
) {
489 sicc
= SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
490 if (sicc
->deactivate
) {
491 sicc
->deactivate(spapr
->active_intc
);
495 sicc
= SPAPR_INTC_GET_CLASS(new_intc
);
496 if (sicc
->activate
) {
497 sicc
->activate(new_intc
, nr_servers
, &error_fatal
);
500 spapr
->active_intc
= new_intc
;
503 * We've changed the kernel irqchip, let VFIO devices know they
506 kvm_irqchip_change_notify();
509 void spapr_irq_update_active_intc(SpaprMachineState
*spapr
)
511 SpaprInterruptController
*new_intc
;
515 * XXX before we run CAS, ov5_cas is initialized empty, which
516 * indicates XICS, even if we have ic-mode=xive. TODO: clean
517 * up the CAS path so that we have a clearer way of handling
520 new_intc
= SPAPR_INTC(spapr
->xive
);
521 } else if (spapr
->ov5_cas
522 && spapr_ovec_test(spapr
->ov5_cas
, OV5_XIVE_EXPLOIT
)) {
523 new_intc
= SPAPR_INTC(spapr
->xive
);
525 new_intc
= SPAPR_INTC(spapr
->ics
);
528 set_active_intc(spapr
, new_intc
);
532 * XICS legacy routines - to deprecate one day
535 static int ics_find_free_block(ICSState
*ics
, int num
, int alignnum
)
539 for (first
= 0; first
< ics
->nr_irqs
; first
+= alignnum
) {
540 if (num
> (ics
->nr_irqs
- first
)) {
543 for (i
= first
; i
< first
+ num
; ++i
) {
544 if (!ics_irq_free(ics
, i
)) {
548 if (i
== (first
+ num
)) {
556 int spapr_irq_find(SpaprMachineState
*spapr
, int num
, bool align
, Error
**errp
)
558 ICSState
*ics
= spapr
->ics
;
564 * MSIMesage::data is used for storing VIRQ so
565 * it has to be aligned to num to support multiple
566 * MSI vectors. MSI-X is not affected by this.
567 * The hint is used for the first IRQ, the rest should
568 * be allocated continuously.
571 assert((num
== 1) || (num
== 2) || (num
== 4) ||
572 (num
== 8) || (num
== 16) || (num
== 32));
573 first
= ics_find_free_block(ics
, num
, num
);
575 first
= ics_find_free_block(ics
, num
, 1);
579 error_setg(errp
, "can't find a free %d-IRQ block", num
);
583 return first
+ ics
->offset
;
586 SpaprIrq spapr_irq_xics_legacy
= {
591 static void spapr_irq_register_types(void)
593 type_register_static(&spapr_intc_info
);
596 type_init(spapr_irq_register_types
)