tests/qtest/npcm7xx_rng-test: count runs properly
[qemu/ar7.git] / hw / intc / xics.c
blob68f9d44feb412db3d9fb0ec78af7e0c9d303a1f2
1 /*
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
24 * THE SOFTWARE.
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "cpu.h"
31 #include "trace.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"
41 #include "hw/irq.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;
49 /* Skip partially initialized vCPUs. This can happen on sPAPR when vCPUs
50 * are hot plugged or unplugged.
52 if (!icp) {
53 return;
56 cpu_index = icp->cs ? icp->cs->cpu_index : -1;
58 if (!icp->output) {
59 return;
62 if (kvm_irqchip_in_kernel()) {
63 icp_synchronize_state(icp);
66 monitor_printf(mon, "CPU %d XIRR=%08x (%p) PP=%02x MFRR=%02x\n",
67 cpu_index, icp->xirr, icp->xirr_owner,
68 icp->pending_priority, icp->mfrr);
71 void ics_pic_print_info(ICSState *ics, Monitor *mon)
73 uint32_t i;
75 monitor_printf(mon, "ICS %4x..%4x %p\n",
76 ics->offset, ics->offset + ics->nr_irqs - 1, ics);
78 if (!ics->irqs) {
79 return;
82 if (kvm_irqchip_in_kernel()) {
83 ics_synchronize_state(ics);
86 for (i = 0; i < ics->nr_irqs; i++) {
87 ICSIRQState *irq = ics->irqs + i;
89 if (!(irq->flags & XICS_FLAGS_IRQ_MASK)) {
90 continue;
92 monitor_printf(mon, " %4x %s %02x %02x\n",
93 ics->offset + i,
94 (irq->flags & XICS_FLAGS_IRQ_LSI) ?
95 "LSI" : "MSI",
96 irq->priority, irq->status);
101 * ICP: Presentation layer
104 #define XISR_MASK 0x00ffffff
105 #define CPPR_MASK 0xff000000
107 #define XISR(icp) (((icp)->xirr) & XISR_MASK)
108 #define CPPR(icp) (((icp)->xirr) >> 24)
110 static void ics_reject(ICSState *ics, uint32_t nr);
111 static void ics_eoi(ICSState *ics, uint32_t nr);
113 static void icp_check_ipi(ICPState *icp)
115 if (XISR(icp) && (icp->pending_priority <= icp->mfrr)) {
116 return;
119 trace_xics_icp_check_ipi(icp->cs->cpu_index, icp->mfrr);
121 if (XISR(icp) && icp->xirr_owner) {
122 ics_reject(icp->xirr_owner, XISR(icp));
125 icp->xirr = (icp->xirr & ~XISR_MASK) | XICS_IPI;
126 icp->pending_priority = icp->mfrr;
127 icp->xirr_owner = NULL;
128 qemu_irq_raise(icp->output);
131 void icp_resend(ICPState *icp)
133 XICSFabric *xi = icp->xics;
134 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
136 if (icp->mfrr < CPPR(icp)) {
137 icp_check_ipi(icp);
140 xic->ics_resend(xi);
143 void icp_set_cppr(ICPState *icp, uint8_t cppr)
145 uint8_t old_cppr;
146 uint32_t old_xisr;
148 old_cppr = CPPR(icp);
149 icp->xirr = (icp->xirr & ~CPPR_MASK) | (cppr << 24);
151 if (cppr < old_cppr) {
152 if (XISR(icp) && (cppr <= icp->pending_priority)) {
153 old_xisr = XISR(icp);
154 icp->xirr &= ~XISR_MASK; /* Clear XISR */
155 icp->pending_priority = 0xff;
156 qemu_irq_lower(icp->output);
157 if (icp->xirr_owner) {
158 ics_reject(icp->xirr_owner, old_xisr);
159 icp->xirr_owner = NULL;
162 } else {
163 if (!XISR(icp)) {
164 icp_resend(icp);
169 void icp_set_mfrr(ICPState *icp, uint8_t mfrr)
171 icp->mfrr = mfrr;
172 if (mfrr < CPPR(icp)) {
173 icp_check_ipi(icp);
177 uint32_t icp_accept(ICPState *icp)
179 uint32_t xirr = icp->xirr;
181 qemu_irq_lower(icp->output);
182 icp->xirr = icp->pending_priority << 24;
183 icp->pending_priority = 0xff;
184 icp->xirr_owner = NULL;
186 trace_xics_icp_accept(xirr, icp->xirr);
188 return xirr;
191 uint32_t icp_ipoll(ICPState *icp, uint32_t *mfrr)
193 if (mfrr) {
194 *mfrr = icp->mfrr;
196 return icp->xirr;
199 void icp_eoi(ICPState *icp, uint32_t xirr)
201 XICSFabric *xi = icp->xics;
202 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
203 ICSState *ics;
204 uint32_t irq;
206 /* Send EOI -> ICS */
207 icp->xirr = (icp->xirr & ~CPPR_MASK) | (xirr & CPPR_MASK);
208 trace_xics_icp_eoi(icp->cs->cpu_index, xirr, icp->xirr);
209 irq = xirr & XISR_MASK;
211 ics = xic->ics_get(xi, irq);
212 if (ics) {
213 ics_eoi(ics, irq);
215 if (!XISR(icp)) {
216 icp_resend(icp);
220 void icp_irq(ICSState *ics, int server, int nr, uint8_t priority)
222 ICPState *icp = xics_icp_get(ics->xics, server);
224 trace_xics_icp_irq(server, nr, priority);
226 if ((priority >= CPPR(icp))
227 || (XISR(icp) && (icp->pending_priority <= priority))) {
228 ics_reject(ics, nr);
229 } else {
230 if (XISR(icp) && icp->xirr_owner) {
231 ics_reject(icp->xirr_owner, XISR(icp));
232 icp->xirr_owner = NULL;
234 icp->xirr = (icp->xirr & ~XISR_MASK) | (nr & XISR_MASK);
235 icp->xirr_owner = ics;
236 icp->pending_priority = priority;
237 trace_xics_icp_raise(icp->xirr, icp->pending_priority);
238 qemu_irq_raise(icp->output);
242 static int icp_pre_save(void *opaque)
244 ICPState *icp = opaque;
246 if (kvm_irqchip_in_kernel()) {
247 icp_get_kvm_state(icp);
250 return 0;
253 static int icp_post_load(void *opaque, int version_id)
255 ICPState *icp = opaque;
257 if (kvm_irqchip_in_kernel()) {
258 Error *local_err = NULL;
259 int ret;
261 ret = icp_set_kvm_state(icp, &local_err);
262 if (ret < 0) {
263 error_report_err(local_err);
264 return ret;
268 return 0;
271 static const VMStateDescription vmstate_icp_server = {
272 .name = "icp/server",
273 .version_id = 1,
274 .minimum_version_id = 1,
275 .pre_save = icp_pre_save,
276 .post_load = icp_post_load,
277 .fields = (VMStateField[]) {
278 /* Sanity check */
279 VMSTATE_UINT32(xirr, ICPState),
280 VMSTATE_UINT8(pending_priority, ICPState),
281 VMSTATE_UINT8(mfrr, ICPState),
282 VMSTATE_END_OF_LIST()
286 void icp_reset(ICPState *icp)
288 icp->xirr = 0;
289 icp->pending_priority = 0xff;
290 icp->mfrr = 0xff;
292 if (kvm_irqchip_in_kernel()) {
293 Error *local_err = NULL;
295 icp_set_kvm_state(icp, &local_err);
296 if (local_err) {
297 error_report_err(local_err);
302 static void icp_realize(DeviceState *dev, Error **errp)
304 ICPState *icp = ICP(dev);
305 CPUPPCState *env;
306 Error *err = NULL;
308 assert(icp->xics);
309 assert(icp->cs);
311 env = &POWERPC_CPU(icp->cs)->env;
312 switch (PPC_INPUT(env)) {
313 case PPC_FLAGS_INPUT_POWER7:
314 icp->output = env->irq_inputs[POWER7_INPUT_INT];
315 break;
316 case PPC_FLAGS_INPUT_POWER9: /* For SPAPR xics emulation */
317 icp->output = env->irq_inputs[POWER9_INPUT_INT];
318 break;
320 case PPC_FLAGS_INPUT_970:
321 icp->output = env->irq_inputs[PPC970_INPUT_INT];
322 break;
324 default:
325 error_setg(errp, "XICS interrupt controller does not support this CPU bus model");
326 return;
329 /* Connect the presenter to the VCPU (required for CPU hotplug) */
330 if (kvm_irqchip_in_kernel()) {
331 icp_kvm_realize(dev, &err);
332 if (err) {
333 error_propagate(errp, err);
334 return;
338 vmstate_register(NULL, icp->cs->cpu_index, &vmstate_icp_server, icp);
341 static void icp_unrealize(DeviceState *dev)
343 ICPState *icp = ICP(dev);
345 vmstate_unregister(NULL, &vmstate_icp_server, icp);
348 static Property icp_properties[] = {
349 DEFINE_PROP_LINK(ICP_PROP_XICS, ICPState, xics, TYPE_XICS_FABRIC,
350 XICSFabric *),
351 DEFINE_PROP_LINK(ICP_PROP_CPU, ICPState, cs, TYPE_CPU, CPUState *),
352 DEFINE_PROP_END_OF_LIST(),
355 static void icp_class_init(ObjectClass *klass, void *data)
357 DeviceClass *dc = DEVICE_CLASS(klass);
359 dc->realize = icp_realize;
360 dc->unrealize = icp_unrealize;
361 device_class_set_props(dc, icp_properties);
363 * Reason: part of XICS interrupt controller, needs to be wired up
364 * by icp_create().
366 dc->user_creatable = false;
369 static const TypeInfo icp_info = {
370 .name = TYPE_ICP,
371 .parent = TYPE_DEVICE,
372 .instance_size = sizeof(ICPState),
373 .class_init = icp_class_init,
374 .class_size = sizeof(ICPStateClass),
377 Object *icp_create(Object *cpu, const char *type, XICSFabric *xi, Error **errp)
379 Object *obj;
381 obj = object_new(type);
382 object_property_add_child(cpu, type, obj);
383 object_unref(obj);
384 object_property_set_link(obj, ICP_PROP_XICS, OBJECT(xi), &error_abort);
385 object_property_set_link(obj, ICP_PROP_CPU, cpu, &error_abort);
386 if (!qdev_realize(DEVICE(obj), NULL, errp)) {
387 object_unparent(obj);
388 obj = NULL;
391 return obj;
394 void icp_destroy(ICPState *icp)
396 Object *obj = OBJECT(icp);
398 object_unparent(obj);
402 * ICS: Source layer
404 static void ics_resend_msi(ICSState *ics, int srcno)
406 ICSIRQState *irq = ics->irqs + srcno;
408 /* FIXME: filter by server#? */
409 if (irq->status & XICS_STATUS_REJECTED) {
410 irq->status &= ~XICS_STATUS_REJECTED;
411 if (irq->priority != 0xff) {
412 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
417 static void ics_resend_lsi(ICSState *ics, int srcno)
419 ICSIRQState *irq = ics->irqs + srcno;
421 if ((irq->priority != 0xff)
422 && (irq->status & XICS_STATUS_ASSERTED)
423 && !(irq->status & XICS_STATUS_SENT)) {
424 irq->status |= XICS_STATUS_SENT;
425 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
429 static void ics_set_irq_msi(ICSState *ics, int srcno, int val)
431 ICSIRQState *irq = ics->irqs + srcno;
433 trace_xics_ics_set_irq_msi(srcno, srcno + ics->offset);
435 if (val) {
436 if (irq->priority == 0xff) {
437 irq->status |= XICS_STATUS_MASKED_PENDING;
438 trace_xics_masked_pending();
439 } else {
440 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
445 static void ics_set_irq_lsi(ICSState *ics, int srcno, int val)
447 ICSIRQState *irq = ics->irqs + srcno;
449 trace_xics_ics_set_irq_lsi(srcno, srcno + ics->offset);
450 if (val) {
451 irq->status |= XICS_STATUS_ASSERTED;
452 } else {
453 irq->status &= ~XICS_STATUS_ASSERTED;
455 ics_resend_lsi(ics, srcno);
458 void ics_set_irq(void *opaque, int srcno, int val)
460 ICSState *ics = (ICSState *)opaque;
462 if (kvm_irqchip_in_kernel()) {
463 ics_kvm_set_irq(ics, srcno, val);
464 return;
467 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
468 ics_set_irq_lsi(ics, srcno, val);
469 } else {
470 ics_set_irq_msi(ics, srcno, val);
474 static void ics_write_xive_msi(ICSState *ics, int srcno)
476 ICSIRQState *irq = ics->irqs + srcno;
478 if (!(irq->status & XICS_STATUS_MASKED_PENDING)
479 || (irq->priority == 0xff)) {
480 return;
483 irq->status &= ~XICS_STATUS_MASKED_PENDING;
484 icp_irq(ics, irq->server, srcno + ics->offset, irq->priority);
487 static void ics_write_xive_lsi(ICSState *ics, int srcno)
489 ics_resend_lsi(ics, srcno);
492 void ics_write_xive(ICSState *ics, int srcno, int server,
493 uint8_t priority, uint8_t saved_priority)
495 ICSIRQState *irq = ics->irqs + srcno;
497 irq->server = server;
498 irq->priority = priority;
499 irq->saved_priority = saved_priority;
501 trace_xics_ics_write_xive(ics->offset + srcno, srcno, server, priority);
503 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
504 ics_write_xive_lsi(ics, srcno);
505 } else {
506 ics_write_xive_msi(ics, srcno);
510 static void ics_reject(ICSState *ics, uint32_t nr)
512 ICSStateClass *isc = ICS_GET_CLASS(ics);
513 ICSIRQState *irq = ics->irqs + nr - ics->offset;
515 if (isc->reject) {
516 isc->reject(ics, nr);
517 return;
520 trace_xics_ics_reject(nr, nr - ics->offset);
521 if (irq->flags & XICS_FLAGS_IRQ_MSI) {
522 irq->status |= XICS_STATUS_REJECTED;
523 } else if (irq->flags & XICS_FLAGS_IRQ_LSI) {
524 irq->status &= ~XICS_STATUS_SENT;
528 void ics_resend(ICSState *ics)
530 ICSStateClass *isc = ICS_GET_CLASS(ics);
531 int i;
533 if (isc->resend) {
534 isc->resend(ics);
535 return;
538 for (i = 0; i < ics->nr_irqs; i++) {
539 /* FIXME: filter by server#? */
540 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
541 ics_resend_lsi(ics, i);
542 } else {
543 ics_resend_msi(ics, i);
548 static void ics_eoi(ICSState *ics, uint32_t nr)
550 int srcno = nr - ics->offset;
551 ICSIRQState *irq = ics->irqs + srcno;
553 trace_xics_ics_eoi(nr);
555 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
556 irq->status &= ~XICS_STATUS_SENT;
560 static void ics_reset_irq(ICSIRQState *irq)
562 irq->priority = 0xff;
563 irq->saved_priority = 0xff;
566 static void ics_reset(DeviceState *dev)
568 ICSState *ics = ICS(dev);
569 int i;
570 uint8_t flags[ics->nr_irqs];
572 for (i = 0; i < ics->nr_irqs; i++) {
573 flags[i] = ics->irqs[i].flags;
576 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
578 for (i = 0; i < ics->nr_irqs; i++) {
579 ics_reset_irq(ics->irqs + i);
580 ics->irqs[i].flags = flags[i];
583 if (kvm_irqchip_in_kernel()) {
584 Error *local_err = NULL;
586 ics_set_kvm_state(ICS(dev), &local_err);
587 if (local_err) {
588 error_report_err(local_err);
593 static void ics_reset_handler(void *dev)
595 ics_reset(dev);
598 static void ics_realize(DeviceState *dev, Error **errp)
600 ICSState *ics = ICS(dev);
602 assert(ics->xics);
604 if (!ics->nr_irqs) {
605 error_setg(errp, "Number of interrupts needs to be greater 0");
606 return;
608 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
610 qemu_register_reset(ics_reset_handler, ics);
613 static void ics_instance_init(Object *obj)
615 ICSState *ics = ICS(obj);
617 ics->offset = XICS_IRQ_BASE;
620 static int ics_pre_save(void *opaque)
622 ICSState *ics = opaque;
624 if (kvm_irqchip_in_kernel()) {
625 ics_get_kvm_state(ics);
628 return 0;
631 static int ics_post_load(void *opaque, int version_id)
633 ICSState *ics = opaque;
635 if (kvm_irqchip_in_kernel()) {
636 Error *local_err = NULL;
637 int ret;
639 ret = ics_set_kvm_state(ics, &local_err);
640 if (ret < 0) {
641 error_report_err(local_err);
642 return ret;
646 return 0;
649 static const VMStateDescription vmstate_ics_irq = {
650 .name = "ics/irq",
651 .version_id = 2,
652 .minimum_version_id = 1,
653 .fields = (VMStateField[]) {
654 VMSTATE_UINT32(server, ICSIRQState),
655 VMSTATE_UINT8(priority, ICSIRQState),
656 VMSTATE_UINT8(saved_priority, ICSIRQState),
657 VMSTATE_UINT8(status, ICSIRQState),
658 VMSTATE_UINT8(flags, ICSIRQState),
659 VMSTATE_END_OF_LIST()
663 static const VMStateDescription vmstate_ics = {
664 .name = "ics",
665 .version_id = 1,
666 .minimum_version_id = 1,
667 .pre_save = ics_pre_save,
668 .post_load = ics_post_load,
669 .fields = (VMStateField[]) {
670 /* Sanity check */
671 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState, NULL),
673 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
674 vmstate_ics_irq,
675 ICSIRQState),
676 VMSTATE_END_OF_LIST()
680 static Property ics_properties[] = {
681 DEFINE_PROP_UINT32("nr-irqs", ICSState, nr_irqs, 0),
682 DEFINE_PROP_LINK(ICS_PROP_XICS, ICSState, xics, TYPE_XICS_FABRIC,
683 XICSFabric *),
684 DEFINE_PROP_END_OF_LIST(),
687 static void ics_class_init(ObjectClass *klass, void *data)
689 DeviceClass *dc = DEVICE_CLASS(klass);
691 dc->realize = ics_realize;
692 device_class_set_props(dc, ics_properties);
693 dc->reset = ics_reset;
694 dc->vmsd = &vmstate_ics;
696 * Reason: part of XICS interrupt controller, needs to be wired up,
697 * e.g. by spapr_irq_init().
699 dc->user_creatable = false;
702 static const TypeInfo ics_info = {
703 .name = TYPE_ICS,
704 .parent = TYPE_DEVICE,
705 .instance_size = sizeof(ICSState),
706 .instance_init = ics_instance_init,
707 .class_init = ics_class_init,
708 .class_size = sizeof(ICSStateClass),
711 static const TypeInfo xics_fabric_info = {
712 .name = TYPE_XICS_FABRIC,
713 .parent = TYPE_INTERFACE,
714 .class_size = sizeof(XICSFabricClass),
718 * Exported functions
720 ICPState *xics_icp_get(XICSFabric *xi, int server)
722 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
724 return xic->icp_get(xi, server);
727 void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
729 assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
731 ics->irqs[srcno].flags |=
732 lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
734 if (kvm_irqchip_in_kernel()) {
735 Error *local_err = NULL;
737 ics_reset_irq(ics->irqs + srcno);
738 ics_set_kvm_state_one(ics, srcno, &local_err);
739 if (local_err) {
740 error_report_err(local_err);
745 static void xics_register_types(void)
747 type_register_static(&ics_info);
748 type_register_static(&icp_info);
749 type_register_static(&xics_fabric_info);
752 type_init(xics_register_types)