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 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
,
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()
51 irq
= bitmap_find_next_zero_area(spapr
->irq_map
, spapr
->irq_map_nr
, 0, num
,
53 if (irq
== spapr
->irq_map_nr
) {
54 error_setg(errp
, "can't find a free %d-IRQ block", num
);
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 int spapr_irq_init_kvm(int (*fn
)(SpaprInterruptController
*, Error
**),
69 SpaprInterruptController
*intc
,
72 MachineState
*machine
= MACHINE(qdev_get_machine());
73 Error
*local_err
= NULL
;
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
);
85 * We failed to initialize the KVM device, fallback to
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
);
103 static int spapr_irq_post_load_xics(SpaprMachineState
*spapr
, int version_id
)
105 if (!kvm_irqchip_in_kernel()) {
108 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
109 icp_resend(spapr_cpu_state(cpu
)->icp
);
115 SpaprIrq spapr_irq_xics
= {
116 .nr_xirqs
= SPAPR_NR_XIRQS
,
117 .nr_msis
= SPAPR_NR_MSIS
,
121 .post_load
= spapr_irq_post_load_xics
,
128 static int spapr_irq_post_load_xive(SpaprMachineState
*spapr
, int version_id
)
130 return spapr_xive_post_load(spapr
->xive
, version_id
);
133 SpaprIrq spapr_irq_xive
= {
134 .nr_xirqs
= SPAPR_NR_XIRQS
,
135 .nr_msis
= SPAPR_NR_MSIS
,
139 .post_load
= spapr_irq_post_load_xive
,
143 * Dual XIVE and XICS IRQ backend.
145 * Both interrupt mode, XIVE and XICS, objects are created but the
146 * machine starts in legacy interrupt mode (XICS). It can be changed
147 * by the CAS negotiation process and, in that case, the new mode is
148 * activated after an extra machine reset.
152 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the
155 static SpaprIrq
*spapr_irq_current(SpaprMachineState
*spapr
)
157 return spapr_ovec_test(spapr
->ov5_cas
, OV5_XIVE_EXPLOIT
) ?
158 &spapr_irq_xive
: &spapr_irq_xics
;
161 static int spapr_irq_post_load_dual(SpaprMachineState
*spapr
, int version_id
)
163 return spapr_irq_current(spapr
)->post_load(spapr
, version_id
);
167 * Define values in sync with the XIVE and XICS backend
169 SpaprIrq spapr_irq_dual
= {
170 .nr_xirqs
= SPAPR_NR_XIRQS
,
171 .nr_msis
= SPAPR_NR_MSIS
,
175 .post_load
= spapr_irq_post_load_dual
,
179 static int spapr_irq_check(SpaprMachineState
*spapr
, Error
**errp
)
181 MachineState
*machine
= MACHINE(spapr
);
184 * Sanity checks on non-P9 machines. On these, XIVE is not
185 * advertised, see spapr_dt_ov5_platform_support()
187 if (!ppc_type_check_compat(machine
->cpu_type
, CPU_POWERPC_LOGICAL_3_00
,
188 0, spapr
->max_compat_pvr
)) {
190 * If the 'dual' interrupt mode is selected, force XICS as CAS
191 * negotiation is useless.
193 if (spapr
->irq
== &spapr_irq_dual
) {
194 spapr
->irq
= &spapr_irq_xics
;
199 * Non-P9 machines using only XIVE is a bogus setup. We have two
200 * scenarios to take into account because of the compat mode:
202 * 1. POWER7/8 machines should fail to init later on when creating
203 * the XIVE interrupt presenters because a POWER9 exception
206 * 2. POWER9 machines using the POWER8 compat mode won't fail and
207 * will let the OS boot with a partial XIVE setup : DT
208 * properties but no hcalls.
210 * To cover both and not confuse the OS, add an early failure in
213 if (spapr
->irq
== &spapr_irq_xive
) {
214 error_setg(errp
, "XIVE-only machines require a POWER9 CPU");
220 * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
221 * re-created. Detect that early to avoid QEMU to exit later when the
225 spapr
->irq
== &spapr_irq_dual
&&
226 machine_kernel_irqchip_required(machine
) &&
227 xics_kvm_has_broken_disconnect(spapr
)) {
228 error_setg(errp
, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
236 * sPAPR IRQ frontend routines for devices
238 #define ALL_INTCS(spapr_) \
239 { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), }
241 int spapr_irq_cpu_intc_create(SpaprMachineState
*spapr
,
242 PowerPCCPU
*cpu
, Error
**errp
)
244 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
248 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
249 SpaprInterruptController
*intc
= intcs
[i
];
251 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
252 rc
= sicc
->cpu_intc_create(intc
, cpu
, errp
);
262 static void spapr_set_irq(void *opaque
, int irq
, int level
)
264 SpaprMachineState
*spapr
= SPAPR_MACHINE(opaque
);
265 SpaprInterruptControllerClass
*sicc
266 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
268 sicc
->set_irq(spapr
->active_intc
, irq
, level
);
271 void spapr_irq_print_info(SpaprMachineState
*spapr
, Monitor
*mon
)
273 SpaprInterruptControllerClass
*sicc
274 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
276 sicc
->print_info(spapr
->active_intc
, mon
);
279 void spapr_irq_dt(SpaprMachineState
*spapr
, uint32_t nr_servers
,
280 void *fdt
, uint32_t phandle
)
282 SpaprInterruptControllerClass
*sicc
283 = SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
285 sicc
->dt(spapr
->active_intc
, nr_servers
, fdt
, phandle
);
288 void spapr_irq_init(SpaprMachineState
*spapr
, Error
**errp
)
290 MachineState
*machine
= MACHINE(spapr
);
292 if (machine_kernel_irqchip_split(machine
)) {
293 error_setg(errp
, "kernel_irqchip split mode not supported on pseries");
297 if (!kvm_enabled() && machine_kernel_irqchip_required(machine
)) {
299 "kernel_irqchip requested but only available with KVM");
303 if (spapr_irq_check(spapr
, errp
) < 0) {
307 /* Initialize the MSI IRQ allocator. */
308 if (!SPAPR_MACHINE_GET_CLASS(spapr
)->legacy_irq_allocation
) {
309 spapr_irq_msi_init(spapr
, spapr
->irq
->nr_msis
);
312 if (spapr
->irq
->xics
) {
313 Error
*local_err
= NULL
;
316 obj
= object_new(TYPE_ICS_SPAPR
);
317 object_property_add_child(OBJECT(spapr
), "ics", obj
, &local_err
);
319 error_propagate(errp
, local_err
);
323 object_property_add_const_link(obj
, ICS_PROP_XICS
, OBJECT(spapr
),
326 error_propagate(errp
, local_err
);
330 object_property_set_int(obj
, spapr
->irq
->nr_xirqs
, "nr-irqs",
333 error_propagate(errp
, local_err
);
337 object_property_set_bool(obj
, true, "realized", &local_err
);
339 error_propagate(errp
, local_err
);
343 spapr
->ics
= ICS_SPAPR(obj
);
346 if (spapr
->irq
->xive
) {
347 uint32_t nr_servers
= spapr_max_server_number(spapr
);
351 dev
= qdev_create(NULL
, TYPE_SPAPR_XIVE
);
352 qdev_prop_set_uint32(dev
, "nr-irqs",
353 spapr
->irq
->nr_xirqs
+ SPAPR_XIRQ_BASE
);
355 * 8 XIVE END structures per CPU. One for each available
358 qdev_prop_set_uint32(dev
, "nr-ends", nr_servers
<< 3);
359 qdev_init_nofail(dev
);
361 spapr
->xive
= SPAPR_XIVE(dev
);
363 /* Enable the CPU IPIs */
364 for (i
= 0; i
< nr_servers
; ++i
) {
365 SpaprInterruptControllerClass
*sicc
366 = SPAPR_INTC_GET_CLASS(spapr
->xive
);
368 if (sicc
->claim_irq(SPAPR_INTC(spapr
->xive
), SPAPR_IRQ_IPI
+ i
,
374 spapr_xive_hcall_init(spapr
);
377 spapr
->qirqs
= qemu_allocate_irqs(spapr_set_irq
, spapr
,
378 spapr
->irq
->nr_xirqs
+ SPAPR_XIRQ_BASE
);
381 int spapr_irq_claim(SpaprMachineState
*spapr
, int irq
, bool lsi
, Error
**errp
)
383 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
387 assert(irq
>= SPAPR_XIRQ_BASE
);
388 assert(irq
< (spapr
->irq
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
390 for (i
= 0; i
< ARRAY_SIZE(intcs
); i
++) {
391 SpaprInterruptController
*intc
= intcs
[i
];
393 SpaprInterruptControllerClass
*sicc
= SPAPR_INTC_GET_CLASS(intc
);
394 rc
= sicc
->claim_irq(intc
, irq
, lsi
, errp
);
404 void spapr_irq_free(SpaprMachineState
*spapr
, int irq
, int num
)
406 SpaprInterruptController
*intcs
[] = ALL_INTCS(spapr
);
409 assert(irq
>= SPAPR_XIRQ_BASE
);
410 assert((irq
+ num
) <= (spapr
->irq
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
412 for (i
= irq
; i
< (irq
+ num
); i
++) {
413 for (j
= 0; j
< ARRAY_SIZE(intcs
); j
++) {
414 SpaprInterruptController
*intc
= intcs
[j
];
417 SpaprInterruptControllerClass
*sicc
418 = SPAPR_INTC_GET_CLASS(intc
);
419 sicc
->free_irq(intc
, i
);
425 qemu_irq
spapr_qirq(SpaprMachineState
*spapr
, int irq
)
428 * This interface is basically for VIO and PHB devices to find the
429 * right qemu_irq to manipulate, so we only allow access to the
430 * external irqs for now. Currently anything which needs to
431 * access the IPIs most naturally gets there via the guest side
432 * interfaces, we can change this if we need to in future.
434 assert(irq
>= SPAPR_XIRQ_BASE
);
435 assert(irq
< (spapr
->irq
->nr_xirqs
+ SPAPR_XIRQ_BASE
));
438 assert(ics_valid_irq(spapr
->ics
, irq
));
441 assert(irq
< spapr
->xive
->nr_irqs
);
442 assert(xive_eas_is_valid(&spapr
->xive
->eat
[irq
]));
445 return spapr
->qirqs
[irq
];
448 int spapr_irq_post_load(SpaprMachineState
*spapr
, int version_id
)
450 spapr_irq_update_active_intc(spapr
);
451 return spapr
->irq
->post_load(spapr
, version_id
);
454 void spapr_irq_reset(SpaprMachineState
*spapr
, Error
**errp
)
456 assert(!spapr
->irq_map
|| bitmap_empty(spapr
->irq_map
, spapr
->irq_map_nr
));
458 spapr_irq_update_active_intc(spapr
);
461 int spapr_irq_get_phandle(SpaprMachineState
*spapr
, void *fdt
, Error
**errp
)
463 const char *nodename
= "interrupt-controller";
466 offset
= fdt_subnode_offset(fdt
, 0, nodename
);
468 error_setg(errp
, "Can't find node \"%s\": %s",
469 nodename
, fdt_strerror(offset
));
473 phandle
= fdt_get_phandle(fdt
, offset
);
475 error_setg(errp
, "Can't get phandle of node \"%s\"", nodename
);
482 static void set_active_intc(SpaprMachineState
*spapr
,
483 SpaprInterruptController
*new_intc
)
485 SpaprInterruptControllerClass
*sicc
;
489 if (new_intc
== spapr
->active_intc
) {
494 if (spapr
->active_intc
) {
495 sicc
= SPAPR_INTC_GET_CLASS(spapr
->active_intc
);
496 if (sicc
->deactivate
) {
497 sicc
->deactivate(spapr
->active_intc
);
501 sicc
= SPAPR_INTC_GET_CLASS(new_intc
);
502 if (sicc
->activate
) {
503 sicc
->activate(new_intc
, &error_fatal
);
506 spapr
->active_intc
= new_intc
;
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_ovec_test(spapr
->ov5_cas
, OV5_XIVE_EXPLOIT
)) {
522 new_intc
= SPAPR_INTC(spapr
->xive
);
524 new_intc
= SPAPR_INTC(spapr
->ics
);
527 set_active_intc(spapr
, new_intc
);
531 * XICS legacy routines - to deprecate one day
534 static int ics_find_free_block(ICSState
*ics
, int num
, int alignnum
)
538 for (first
= 0; first
< ics
->nr_irqs
; first
+= alignnum
) {
539 if (num
> (ics
->nr_irqs
- first
)) {
542 for (i
= first
; i
< first
+ num
; ++i
) {
543 if (!ics_irq_free(ics
, i
)) {
547 if (i
== (first
+ num
)) {
555 int spapr_irq_find(SpaprMachineState
*spapr
, int num
, bool align
, Error
**errp
)
557 ICSState
*ics
= spapr
->ics
;
563 * MSIMesage::data is used for storing VIRQ so
564 * it has to be aligned to num to support multiple
565 * MSI vectors. MSI-X is not affected by this.
566 * The hint is used for the first IRQ, the rest should
567 * be allocated continuously.
570 assert((num
== 1) || (num
== 2) || (num
== 4) ||
571 (num
== 8) || (num
== 16) || (num
== 32));
572 first
= ics_find_free_block(ics
, num
, num
);
574 first
= ics_find_free_block(ics
, num
, 1);
578 error_setg(errp
, "can't find a free %d-IRQ block", num
);
582 return first
+ ics
->offset
;
585 #define SPAPR_IRQ_XICS_LEGACY_NR_XIRQS 0x400
587 SpaprIrq spapr_irq_xics_legacy
= {
588 .nr_xirqs
= SPAPR_IRQ_XICS_LEGACY_NR_XIRQS
,
589 .nr_msis
= SPAPR_IRQ_XICS_LEGACY_NR_XIRQS
,
593 .post_load
= spapr_irq_post_load_xics
,
596 static void spapr_irq_register_types(void)
598 type_register_static(&spapr_intc_info
);
601 type_init(spapr_irq_register_types
)