s390x/tcg: Fix VECTOR SUBTRACT WITH BORROW COMPUTE BORROW INDICATION
[qemu/ar7.git] / hw / intc / xics.c
blobdfe7dbd254ab46bd318922df3f4cc4e599934c38
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 = icp->cs ? icp->cs->cpu_index : -1;
49 if (!icp->output) {
50 return;
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)
64 uint32_t i;
66 monitor_printf(mon, "ICS %4x..%4x %p\n",
67 ics->offset, ics->offset + ics->nr_irqs - 1, ics);
69 if (!ics->irqs) {
70 return;
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)) {
81 continue;
83 monitor_printf(mon, " %4x %s %02x %02x\n",
84 ics->offset + i,
85 (irq->flags & XICS_FLAGS_IRQ_LSI) ?
86 "LSI" : "MSI",
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)) {
107 return;
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)) {
128 icp_check_ipi(icp);
131 xic->ics_resend(xi);
134 void icp_set_cppr(ICPState *icp, uint8_t cppr)
136 uint8_t old_cppr;
137 uint32_t old_xisr;
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;
153 } else {
154 if (!XISR(icp)) {
155 icp_resend(icp);
160 void icp_set_mfrr(ICPState *icp, uint8_t mfrr)
162 icp->mfrr = mfrr;
163 if (mfrr < CPPR(icp)) {
164 icp_check_ipi(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);
179 return xirr;
182 uint32_t icp_ipoll(ICPState *icp, uint32_t *mfrr)
184 if (mfrr) {
185 *mfrr = icp->mfrr;
187 return icp->xirr;
190 void icp_eoi(ICPState *icp, uint32_t xirr)
192 XICSFabric *xi = icp->xics;
193 XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
194 ICSState *ics;
195 uint32_t irq;
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);
203 if (ics) {
204 ics_eoi(ics, irq);
206 if (!XISR(icp)) {
207 icp_resend(icp);
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))) {
219 ics_reject(ics, nr);
220 } else {
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);
241 return 0;
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;
250 int ret;
252 ret = icp_set_kvm_state(icp, &local_err);
253 if (ret < 0) {
254 error_report_err(local_err);
255 return ret;
259 return 0;
262 static const VMStateDescription vmstate_icp_server = {
263 .name = "icp/server",
264 .version_id = 1,
265 .minimum_version_id = 1,
266 .pre_save = icp_pre_save,
267 .post_load = icp_post_load,
268 .fields = (VMStateField[]) {
269 /* Sanity check */
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);
281 icp->xirr = 0;
282 icp->pending_priority = 0xff;
283 icp->mfrr = 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);
292 if (local_err) {
293 error_report_err(local_err);
298 static void icp_realize(DeviceState *dev, Error **errp)
300 ICPState *icp = ICP(dev);
301 PowerPCCPU *cpu;
302 CPUPPCState *env;
303 Object *obj;
304 Error *err = NULL;
306 obj = object_property_get_link(OBJECT(dev), ICP_PROP_XICS, &err);
307 if (!obj) {
308 error_propagate_prepend(errp, err,
309 "required link '" ICP_PROP_XICS
310 "' not found: ");
311 return;
314 icp->xics = XICS_FABRIC(obj);
316 obj = object_property_get_link(OBJECT(dev), ICP_PROP_CPU, &err);
317 if (!obj) {
318 error_propagate_prepend(errp, err,
319 "required link '" ICP_PROP_CPU
320 "' not found: ");
321 return;
324 cpu = POWERPC_CPU(obj);
325 icp->cs = CPU(obj);
327 env = &cpu->env;
328 switch (PPC_INPUT(env)) {
329 case PPC_FLAGS_INPUT_POWER7:
330 icp->output = env->irq_inputs[POWER7_INPUT_INT];
331 break;
332 case PPC_FLAGS_INPUT_POWER9: /* For SPAPR xics emulation */
333 icp->output = env->irq_inputs[POWER9_INPUT_INT];
334 break;
336 case PPC_FLAGS_INPUT_970:
337 icp->output = env->irq_inputs[PPC970_INPUT_INT];
338 break;
340 default:
341 error_setg(errp, "XICS interrupt controller does not support this CPU bus model");
342 return;
345 /* Connect the presenter to the VCPU (required for CPU hotplug) */
346 if (kvm_irqchip_in_kernel()) {
347 icp_kvm_realize(dev, &err);
348 if (err) {
349 error_propagate(errp, err);
350 return;
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 = {
375 .name = TYPE_ICP,
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;
385 Object *obj;
387 obj = object_new(type);
388 object_property_add_child(cpu, type, obj, &error_abort);
389 object_unref(obj);
390 object_property_add_const_link(obj, ICP_PROP_XICS, OBJECT(xi),
391 &error_abort);
392 object_property_add_const_link(obj, ICP_PROP_CPU, cpu, &error_abort);
393 object_property_set_bool(obj, true, "realized", &local_err);
394 if (local_err) {
395 object_unparent(obj);
396 error_propagate(errp, local_err);
397 obj = NULL;
400 return obj;
404 * ICS: Source layer
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);
437 if (val) {
438 if (irq->priority == 0xff) {
439 irq->status |= XICS_STATUS_MASKED_PENDING;
440 trace_xics_masked_pending();
441 } else {
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);
452 if (val) {
453 irq->status |= XICS_STATUS_ASSERTED;
454 } else {
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);
466 return;
469 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
470 ics_set_irq_lsi(ics, srcno, val);
471 } else {
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)) {
482 return;
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);
507 } else {
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)
526 int i;
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);
532 } else {
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);
559 int i;
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);
577 if (local_err) {
578 error_report_err(local_err);
583 static void ics_reset_handler(void *dev)
585 ics_reset(dev);
588 static void ics_realize(DeviceState *dev, Error **errp)
590 ICSState *ics = ICS(dev);
591 Error *local_err = NULL;
592 Object *obj;
594 obj = object_property_get_link(OBJECT(dev), ICS_PROP_XICS, &local_err);
595 if (!obj) {
596 error_propagate_prepend(errp, local_err,
597 "required link '" ICS_PROP_XICS
598 "' not found: ");
599 return;
601 ics->xics = XICS_FABRIC(obj);
603 if (!ics->nr_irqs) {
604 error_setg(errp, "Number of interrupts needs to be greater 0");
605 return;
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);
627 return 0;
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;
636 int ret;
638 ret = ics_set_kvm_state(ics, &local_err);
639 if (ret < 0) {
640 error_report_err(local_err);
641 return ret;
645 return 0;
648 static const VMStateDescription vmstate_ics_irq = {
649 .name = "ics/irq",
650 .version_id = 2,
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 = {
663 .name = "ics",
664 .version_id = 1,
665 .minimum_version_id = 1,
666 .pre_save = ics_pre_save,
667 .post_load = ics_post_load,
668 .fields = (VMStateField[]) {
669 /* Sanity check */
670 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState, NULL),
672 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
673 vmstate_ics_irq,
674 ICSIRQState),
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 = {
695 .name = TYPE_ICS,
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),
710 * Exported functions
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);
731 if (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)