2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics
6 * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
32 #include "qemu/timer.h"
33 #include "hw/ppc/xics.h"
34 #include "hw/qdev-properties.h"
35 #include "qemu/error-report.h"
36 #include "qemu/module.h"
37 #include "qapi/visitor.h"
38 #include "migration/vmstate.h"
39 #include "monitor/monitor.h"
40 #include "hw/intc/intc.h"
42 #include "sysemu/kvm.h"
43 #include "sysemu/reset.h"
45 void icp_pic_print_info(ICPState
*icp
, Monitor
*mon
)
47 int cpu_index
= icp
->cs
? icp
->cs
->cpu_index
: -1;
53 if (kvm_irqchip_in_kernel()) {
54 icp_synchronize_state(icp
);
57 monitor_printf(mon
, "CPU %d XIRR=%08x (%p) PP=%02x MFRR=%02x\n",
58 cpu_index
, icp
->xirr
, icp
->xirr_owner
,
59 icp
->pending_priority
, icp
->mfrr
);
62 void ics_pic_print_info(ICSState
*ics
, Monitor
*mon
)
66 monitor_printf(mon
, "ICS %4x..%4x %p\n",
67 ics
->offset
, ics
->offset
+ ics
->nr_irqs
- 1, ics
);
73 if (kvm_irqchip_in_kernel()) {
74 ics_synchronize_state(ics
);
77 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
78 ICSIRQState
*irq
= ics
->irqs
+ i
;
80 if (!(irq
->flags
& XICS_FLAGS_IRQ_MASK
)) {
83 monitor_printf(mon
, " %4x %s %02x %02x\n",
85 (irq
->flags
& XICS_FLAGS_IRQ_LSI
) ?
87 irq
->priority
, irq
->status
);
92 * ICP: Presentation layer
95 #define XISR_MASK 0x00ffffff
96 #define CPPR_MASK 0xff000000
98 #define XISR(icp) (((icp)->xirr) & XISR_MASK)
99 #define CPPR(icp) (((icp)->xirr) >> 24)
101 static void ics_reject(ICSState
*ics
, uint32_t nr
);
102 static void ics_eoi(ICSState
*ics
, uint32_t nr
);
104 static void icp_check_ipi(ICPState
*icp
)
106 if (XISR(icp
) && (icp
->pending_priority
<= icp
->mfrr
)) {
110 trace_xics_icp_check_ipi(icp
->cs
->cpu_index
, icp
->mfrr
);
112 if (XISR(icp
) && icp
->xirr_owner
) {
113 ics_reject(icp
->xirr_owner
, XISR(icp
));
116 icp
->xirr
= (icp
->xirr
& ~XISR_MASK
) | XICS_IPI
;
117 icp
->pending_priority
= icp
->mfrr
;
118 icp
->xirr_owner
= NULL
;
119 qemu_irq_raise(icp
->output
);
122 void icp_resend(ICPState
*icp
)
124 XICSFabric
*xi
= icp
->xics
;
125 XICSFabricClass
*xic
= XICS_FABRIC_GET_CLASS(xi
);
127 if (icp
->mfrr
< CPPR(icp
)) {
134 void icp_set_cppr(ICPState
*icp
, uint8_t cppr
)
139 old_cppr
= CPPR(icp
);
140 icp
->xirr
= (icp
->xirr
& ~CPPR_MASK
) | (cppr
<< 24);
142 if (cppr
< old_cppr
) {
143 if (XISR(icp
) && (cppr
<= icp
->pending_priority
)) {
144 old_xisr
= XISR(icp
);
145 icp
->xirr
&= ~XISR_MASK
; /* Clear XISR */
146 icp
->pending_priority
= 0xff;
147 qemu_irq_lower(icp
->output
);
148 if (icp
->xirr_owner
) {
149 ics_reject(icp
->xirr_owner
, old_xisr
);
150 icp
->xirr_owner
= NULL
;
160 void icp_set_mfrr(ICPState
*icp
, uint8_t mfrr
)
163 if (mfrr
< CPPR(icp
)) {
168 uint32_t icp_accept(ICPState
*icp
)
170 uint32_t xirr
= icp
->xirr
;
172 qemu_irq_lower(icp
->output
);
173 icp
->xirr
= icp
->pending_priority
<< 24;
174 icp
->pending_priority
= 0xff;
175 icp
->xirr_owner
= NULL
;
177 trace_xics_icp_accept(xirr
, icp
->xirr
);
182 uint32_t icp_ipoll(ICPState
*icp
, uint32_t *mfrr
)
190 void icp_eoi(ICPState
*icp
, uint32_t xirr
)
192 XICSFabric
*xi
= icp
->xics
;
193 XICSFabricClass
*xic
= XICS_FABRIC_GET_CLASS(xi
);
197 /* Send EOI -> ICS */
198 icp
->xirr
= (icp
->xirr
& ~CPPR_MASK
) | (xirr
& CPPR_MASK
);
199 trace_xics_icp_eoi(icp
->cs
->cpu_index
, xirr
, icp
->xirr
);
200 irq
= xirr
& XISR_MASK
;
202 ics
= xic
->ics_get(xi
, irq
);
211 static void icp_irq(ICSState
*ics
, int server
, int nr
, uint8_t priority
)
213 ICPState
*icp
= xics_icp_get(ics
->xics
, server
);
215 trace_xics_icp_irq(server
, nr
, priority
);
217 if ((priority
>= CPPR(icp
))
218 || (XISR(icp
) && (icp
->pending_priority
<= priority
))) {
221 if (XISR(icp
) && icp
->xirr_owner
) {
222 ics_reject(icp
->xirr_owner
, XISR(icp
));
223 icp
->xirr_owner
= NULL
;
225 icp
->xirr
= (icp
->xirr
& ~XISR_MASK
) | (nr
& XISR_MASK
);
226 icp
->xirr_owner
= ics
;
227 icp
->pending_priority
= priority
;
228 trace_xics_icp_raise(icp
->xirr
, icp
->pending_priority
);
229 qemu_irq_raise(icp
->output
);
233 static int icp_pre_save(void *opaque
)
235 ICPState
*icp
= opaque
;
237 if (kvm_irqchip_in_kernel()) {
238 icp_get_kvm_state(icp
);
244 static int icp_post_load(void *opaque
, int version_id
)
246 ICPState
*icp
= opaque
;
248 if (kvm_irqchip_in_kernel()) {
249 Error
*local_err
= NULL
;
252 ret
= icp_set_kvm_state(icp
, &local_err
);
254 error_report_err(local_err
);
262 static const VMStateDescription vmstate_icp_server
= {
263 .name
= "icp/server",
265 .minimum_version_id
= 1,
266 .pre_save
= icp_pre_save
,
267 .post_load
= icp_post_load
,
268 .fields
= (VMStateField
[]) {
270 VMSTATE_UINT32(xirr
, ICPState
),
271 VMSTATE_UINT8(pending_priority
, ICPState
),
272 VMSTATE_UINT8(mfrr
, ICPState
),
273 VMSTATE_END_OF_LIST()
277 static void icp_reset_handler(void *dev
)
279 ICPState
*icp
= ICP(dev
);
282 icp
->pending_priority
= 0xff;
285 /* Make all outputs are deasserted */
286 qemu_set_irq(icp
->output
, 0);
288 if (kvm_irqchip_in_kernel()) {
289 Error
*local_err
= NULL
;
291 icp_set_kvm_state(ICP(dev
), &local_err
);
293 error_report_err(local_err
);
298 static void icp_realize(DeviceState
*dev
, Error
**errp
)
300 ICPState
*icp
= ICP(dev
);
306 obj
= object_property_get_link(OBJECT(dev
), ICP_PROP_XICS
, &err
);
308 error_propagate_prepend(errp
, err
,
309 "required link '" ICP_PROP_XICS
314 icp
->xics
= XICS_FABRIC(obj
);
316 obj
= object_property_get_link(OBJECT(dev
), ICP_PROP_CPU
, &err
);
318 error_propagate_prepend(errp
, err
,
319 "required link '" ICP_PROP_CPU
324 cpu
= POWERPC_CPU(obj
);
328 switch (PPC_INPUT(env
)) {
329 case PPC_FLAGS_INPUT_POWER7
:
330 icp
->output
= env
->irq_inputs
[POWER7_INPUT_INT
];
332 case PPC_FLAGS_INPUT_POWER9
: /* For SPAPR xics emulation */
333 icp
->output
= env
->irq_inputs
[POWER9_INPUT_INT
];
336 case PPC_FLAGS_INPUT_970
:
337 icp
->output
= env
->irq_inputs
[PPC970_INPUT_INT
];
341 error_setg(errp
, "XICS interrupt controller does not support this CPU bus model");
345 /* Connect the presenter to the VCPU (required for CPU hotplug) */
346 if (kvm_irqchip_in_kernel()) {
347 icp_kvm_realize(dev
, &err
);
349 error_propagate(errp
, err
);
354 qemu_register_reset(icp_reset_handler
, dev
);
355 vmstate_register(NULL
, icp
->cs
->cpu_index
, &vmstate_icp_server
, icp
);
358 static void icp_unrealize(DeviceState
*dev
, Error
**errp
)
360 ICPState
*icp
= ICP(dev
);
362 vmstate_unregister(NULL
, &vmstate_icp_server
, icp
);
363 qemu_unregister_reset(icp_reset_handler
, dev
);
366 static void icp_class_init(ObjectClass
*klass
, void *data
)
368 DeviceClass
*dc
= DEVICE_CLASS(klass
);
370 dc
->realize
= icp_realize
;
371 dc
->unrealize
= icp_unrealize
;
374 static const TypeInfo icp_info
= {
376 .parent
= TYPE_DEVICE
,
377 .instance_size
= sizeof(ICPState
),
378 .class_init
= icp_class_init
,
379 .class_size
= sizeof(ICPStateClass
),
382 Object
*icp_create(Object
*cpu
, const char *type
, XICSFabric
*xi
, Error
**errp
)
384 Error
*local_err
= NULL
;
387 obj
= object_new(type
);
388 object_property_add_child(cpu
, type
, obj
, &error_abort
);
390 object_property_add_const_link(obj
, ICP_PROP_XICS
, OBJECT(xi
),
392 object_property_add_const_link(obj
, ICP_PROP_CPU
, cpu
, &error_abort
);
393 object_property_set_bool(obj
, true, "realized", &local_err
);
395 object_unparent(obj
);
396 error_propagate(errp
, local_err
);
406 static void ics_resend_msi(ICSState
*ics
, int srcno
)
408 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
410 /* FIXME: filter by server#? */
411 if (irq
->status
& XICS_STATUS_REJECTED
) {
412 irq
->status
&= ~XICS_STATUS_REJECTED
;
413 if (irq
->priority
!= 0xff) {
414 icp_irq(ics
, irq
->server
, srcno
+ ics
->offset
, irq
->priority
);
419 static void ics_resend_lsi(ICSState
*ics
, int srcno
)
421 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
423 if ((irq
->priority
!= 0xff)
424 && (irq
->status
& XICS_STATUS_ASSERTED
)
425 && !(irq
->status
& XICS_STATUS_SENT
)) {
426 irq
->status
|= XICS_STATUS_SENT
;
427 icp_irq(ics
, irq
->server
, srcno
+ ics
->offset
, irq
->priority
);
431 static void ics_set_irq_msi(ICSState
*ics
, int srcno
, int val
)
433 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
435 trace_xics_ics_set_irq_msi(srcno
, srcno
+ ics
->offset
);
438 if (irq
->priority
== 0xff) {
439 irq
->status
|= XICS_STATUS_MASKED_PENDING
;
440 trace_xics_masked_pending();
442 icp_irq(ics
, irq
->server
, srcno
+ ics
->offset
, irq
->priority
);
447 static void ics_set_irq_lsi(ICSState
*ics
, int srcno
, int val
)
449 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
451 trace_xics_ics_set_irq_lsi(srcno
, srcno
+ ics
->offset
);
453 irq
->status
|= XICS_STATUS_ASSERTED
;
455 irq
->status
&= ~XICS_STATUS_ASSERTED
;
457 ics_resend_lsi(ics
, srcno
);
460 void ics_set_irq(void *opaque
, int srcno
, int val
)
462 ICSState
*ics
= (ICSState
*)opaque
;
464 if (kvm_irqchip_in_kernel()) {
465 ics_kvm_set_irq(ics
, srcno
, val
);
469 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_LSI
) {
470 ics_set_irq_lsi(ics
, srcno
, val
);
472 ics_set_irq_msi(ics
, srcno
, val
);
476 static void ics_write_xive_msi(ICSState
*ics
, int srcno
)
478 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
480 if (!(irq
->status
& XICS_STATUS_MASKED_PENDING
)
481 || (irq
->priority
== 0xff)) {
485 irq
->status
&= ~XICS_STATUS_MASKED_PENDING
;
486 icp_irq(ics
, irq
->server
, srcno
+ ics
->offset
, irq
->priority
);
489 static void ics_write_xive_lsi(ICSState
*ics
, int srcno
)
491 ics_resend_lsi(ics
, srcno
);
494 void ics_write_xive(ICSState
*ics
, int srcno
, int server
,
495 uint8_t priority
, uint8_t saved_priority
)
497 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
499 irq
->server
= server
;
500 irq
->priority
= priority
;
501 irq
->saved_priority
= saved_priority
;
503 trace_xics_ics_write_xive(ics
->offset
+ srcno
, srcno
, server
, priority
);
505 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_LSI
) {
506 ics_write_xive_lsi(ics
, srcno
);
508 ics_write_xive_msi(ics
, srcno
);
512 static void ics_reject(ICSState
*ics
, uint32_t nr
)
514 ICSIRQState
*irq
= ics
->irqs
+ nr
- ics
->offset
;
516 trace_xics_ics_reject(nr
, nr
- ics
->offset
);
517 if (irq
->flags
& XICS_FLAGS_IRQ_MSI
) {
518 irq
->status
|= XICS_STATUS_REJECTED
;
519 } else if (irq
->flags
& XICS_FLAGS_IRQ_LSI
) {
520 irq
->status
&= ~XICS_STATUS_SENT
;
524 void ics_resend(ICSState
*ics
)
528 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
529 /* FIXME: filter by server#? */
530 if (ics
->irqs
[i
].flags
& XICS_FLAGS_IRQ_LSI
) {
531 ics_resend_lsi(ics
, i
);
533 ics_resend_msi(ics
, i
);
538 static void ics_eoi(ICSState
*ics
, uint32_t nr
)
540 int srcno
= nr
- ics
->offset
;
541 ICSIRQState
*irq
= ics
->irqs
+ srcno
;
543 trace_xics_ics_eoi(nr
);
545 if (ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_LSI
) {
546 irq
->status
&= ~XICS_STATUS_SENT
;
550 static void ics_reset_irq(ICSIRQState
*irq
)
552 irq
->priority
= 0xff;
553 irq
->saved_priority
= 0xff;
556 static void ics_reset(DeviceState
*dev
)
558 ICSState
*ics
= ICS(dev
);
560 uint8_t flags
[ics
->nr_irqs
];
562 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
563 flags
[i
] = ics
->irqs
[i
].flags
;
566 memset(ics
->irqs
, 0, sizeof(ICSIRQState
) * ics
->nr_irqs
);
568 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
569 ics_reset_irq(ics
->irqs
+ i
);
570 ics
->irqs
[i
].flags
= flags
[i
];
573 if (kvm_irqchip_in_kernel()) {
574 Error
*local_err
= NULL
;
576 ics_set_kvm_state(ICS(dev
), &local_err
);
578 error_report_err(local_err
);
583 static void ics_reset_handler(void *dev
)
588 static void ics_realize(DeviceState
*dev
, Error
**errp
)
590 ICSState
*ics
= ICS(dev
);
591 Error
*local_err
= NULL
;
594 obj
= object_property_get_link(OBJECT(dev
), ICS_PROP_XICS
, &local_err
);
596 error_propagate_prepend(errp
, local_err
,
597 "required link '" ICS_PROP_XICS
601 ics
->xics
= XICS_FABRIC(obj
);
604 error_setg(errp
, "Number of interrupts needs to be greater 0");
607 ics
->irqs
= g_malloc0(ics
->nr_irqs
* sizeof(ICSIRQState
));
609 qemu_register_reset(ics_reset_handler
, ics
);
612 static void ics_instance_init(Object
*obj
)
614 ICSState
*ics
= ICS(obj
);
616 ics
->offset
= XICS_IRQ_BASE
;
619 static int ics_pre_save(void *opaque
)
621 ICSState
*ics
= opaque
;
623 if (kvm_irqchip_in_kernel()) {
624 ics_get_kvm_state(ics
);
630 static int ics_post_load(void *opaque
, int version_id
)
632 ICSState
*ics
= opaque
;
634 if (kvm_irqchip_in_kernel()) {
635 Error
*local_err
= NULL
;
638 ret
= ics_set_kvm_state(ics
, &local_err
);
640 error_report_err(local_err
);
648 static const VMStateDescription vmstate_ics_irq
= {
651 .minimum_version_id
= 1,
652 .fields
= (VMStateField
[]) {
653 VMSTATE_UINT32(server
, ICSIRQState
),
654 VMSTATE_UINT8(priority
, ICSIRQState
),
655 VMSTATE_UINT8(saved_priority
, ICSIRQState
),
656 VMSTATE_UINT8(status
, ICSIRQState
),
657 VMSTATE_UINT8(flags
, ICSIRQState
),
658 VMSTATE_END_OF_LIST()
662 static const VMStateDescription vmstate_ics
= {
665 .minimum_version_id
= 1,
666 .pre_save
= ics_pre_save
,
667 .post_load
= ics_post_load
,
668 .fields
= (VMStateField
[]) {
670 VMSTATE_UINT32_EQUAL(nr_irqs
, ICSState
, NULL
),
672 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs
, ICSState
, nr_irqs
,
675 VMSTATE_END_OF_LIST()
679 static Property ics_properties
[] = {
680 DEFINE_PROP_UINT32("nr-irqs", ICSState
, nr_irqs
, 0),
681 DEFINE_PROP_END_OF_LIST(),
684 static void ics_class_init(ObjectClass
*klass
, void *data
)
686 DeviceClass
*dc
= DEVICE_CLASS(klass
);
688 dc
->realize
= ics_realize
;
689 dc
->props
= ics_properties
;
690 dc
->reset
= ics_reset
;
691 dc
->vmsd
= &vmstate_ics
;
694 static const TypeInfo ics_info
= {
696 .parent
= TYPE_DEVICE
,
697 .instance_size
= sizeof(ICSState
),
698 .instance_init
= ics_instance_init
,
699 .class_init
= ics_class_init
,
700 .class_size
= sizeof(ICSStateClass
),
703 static const TypeInfo xics_fabric_info
= {
704 .name
= TYPE_XICS_FABRIC
,
705 .parent
= TYPE_INTERFACE
,
706 .class_size
= sizeof(XICSFabricClass
),
712 ICPState
*xics_icp_get(XICSFabric
*xi
, int server
)
714 XICSFabricClass
*xic
= XICS_FABRIC_GET_CLASS(xi
);
716 return xic
->icp_get(xi
, server
);
719 void ics_set_irq_type(ICSState
*ics
, int srcno
, bool lsi
)
721 assert(!(ics
->irqs
[srcno
].flags
& XICS_FLAGS_IRQ_MASK
));
723 ics
->irqs
[srcno
].flags
|=
724 lsi
? XICS_FLAGS_IRQ_LSI
: XICS_FLAGS_IRQ_MSI
;
726 if (kvm_irqchip_in_kernel()) {
727 Error
*local_err
= NULL
;
729 ics_reset_irq(ics
->irqs
+ srcno
);
730 ics_set_kvm_state_one(ics
, srcno
, &local_err
);
732 error_report_err(local_err
);
737 static void xics_register_types(void)
739 type_register_static(&ics_info
);
740 type_register_static(&icp_info
);
741 type_register_static(&xics_fabric_info
);
744 type_init(xics_register_types
)