sdl2: Support mouse wheel
[qemu.git] / hw / intc / xics.c
blob64aabe753d2ec7efeaeb1e17edc1050f96d66114
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 "hw/hw.h"
29 #include "trace.h"
30 #include "qemu/timer.h"
31 #include "hw/ppc/spapr.h"
32 #include "hw/ppc/xics.h"
33 #include "qemu/error-report.h"
34 #include "qapi/visitor.h"
36 static int get_cpu_index_by_dt_id(int cpu_dt_id)
38 PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
40 if (cpu) {
41 return cpu->parent_obj.cpu_index;
44 return -1;
47 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu)
49 CPUState *cs = CPU(cpu);
50 CPUPPCState *env = &cpu->env;
51 ICPState *ss = &icp->ss[cs->cpu_index];
52 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
54 assert(cs->cpu_index < icp->nr_servers);
56 if (info->cpu_setup) {
57 info->cpu_setup(icp, cpu);
60 switch (PPC_INPUT(env)) {
61 case PPC_FLAGS_INPUT_POWER7:
62 ss->output = env->irq_inputs[POWER7_INPUT_INT];
63 break;
65 case PPC_FLAGS_INPUT_970:
66 ss->output = env->irq_inputs[PPC970_INPUT_INT];
67 break;
69 default:
70 error_report("XICS interrupt controller does not support this CPU "
71 "bus model");
72 abort();
77 * XICS Common class - parent for emulated XICS and KVM-XICS
79 static void xics_common_reset(DeviceState *d)
81 XICSState *icp = XICS_COMMON(d);
82 int i;
84 for (i = 0; i < icp->nr_servers; i++) {
85 device_reset(DEVICE(&icp->ss[i]));
88 device_reset(DEVICE(icp->ics));
91 static void xics_prop_get_nr_irqs(Object *obj, Visitor *v,
92 void *opaque, const char *name, Error **errp)
94 XICSState *icp = XICS_COMMON(obj);
95 int64_t value = icp->nr_irqs;
97 visit_type_int(v, &value, name, errp);
100 static void xics_prop_set_nr_irqs(Object *obj, Visitor *v,
101 void *opaque, const char *name, Error **errp)
103 XICSState *icp = XICS_COMMON(obj);
104 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
105 Error *error = NULL;
106 int64_t value;
108 visit_type_int(v, &value, name, &error);
109 if (error) {
110 error_propagate(errp, error);
111 return;
113 if (icp->nr_irqs) {
114 error_setg(errp, "Number of interrupts is already set to %u",
115 icp->nr_irqs);
116 return;
119 assert(info->set_nr_irqs);
120 assert(icp->ics);
121 info->set_nr_irqs(icp, value, errp);
124 static void xics_prop_get_nr_servers(Object *obj, Visitor *v,
125 void *opaque, const char *name,
126 Error **errp)
128 XICSState *icp = XICS_COMMON(obj);
129 int64_t value = icp->nr_servers;
131 visit_type_int(v, &value, name, errp);
134 static void xics_prop_set_nr_servers(Object *obj, Visitor *v,
135 void *opaque, const char *name,
136 Error **errp)
138 XICSState *icp = XICS_COMMON(obj);
139 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
140 Error *error = NULL;
141 int64_t value;
143 visit_type_int(v, &value, name, &error);
144 if (error) {
145 error_propagate(errp, error);
146 return;
148 if (icp->nr_servers) {
149 error_setg(errp, "Number of servers is already set to %u",
150 icp->nr_servers);
151 return;
154 assert(info->set_nr_servers);
155 info->set_nr_servers(icp, value, errp);
158 static void xics_common_initfn(Object *obj)
160 object_property_add(obj, "nr_irqs", "int",
161 xics_prop_get_nr_irqs, xics_prop_set_nr_irqs,
162 NULL, NULL, NULL);
163 object_property_add(obj, "nr_servers", "int",
164 xics_prop_get_nr_servers, xics_prop_set_nr_servers,
165 NULL, NULL, NULL);
168 static void xics_common_class_init(ObjectClass *oc, void *data)
170 DeviceClass *dc = DEVICE_CLASS(oc);
172 dc->reset = xics_common_reset;
175 static const TypeInfo xics_common_info = {
176 .name = TYPE_XICS_COMMON,
177 .parent = TYPE_SYS_BUS_DEVICE,
178 .instance_size = sizeof(XICSState),
179 .class_size = sizeof(XICSStateClass),
180 .instance_init = xics_common_initfn,
181 .class_init = xics_common_class_init,
185 * ICP: Presentation layer
188 #define XISR_MASK 0x00ffffff
189 #define CPPR_MASK 0xff000000
191 #define XISR(ss) (((ss)->xirr) & XISR_MASK)
192 #define CPPR(ss) (((ss)->xirr) >> 24)
194 static void ics_reject(ICSState *ics, int nr);
195 static void ics_resend(ICSState *ics);
196 static void ics_eoi(ICSState *ics, int nr);
198 static void icp_check_ipi(XICSState *icp, int server)
200 ICPState *ss = icp->ss + server;
202 if (XISR(ss) && (ss->pending_priority <= ss->mfrr)) {
203 return;
206 trace_xics_icp_check_ipi(server, ss->mfrr);
208 if (XISR(ss)) {
209 ics_reject(icp->ics, XISR(ss));
212 ss->xirr = (ss->xirr & ~XISR_MASK) | XICS_IPI;
213 ss->pending_priority = ss->mfrr;
214 qemu_irq_raise(ss->output);
217 static void icp_resend(XICSState *icp, int server)
219 ICPState *ss = icp->ss + server;
221 if (ss->mfrr < CPPR(ss)) {
222 icp_check_ipi(icp, server);
224 ics_resend(icp->ics);
227 static void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
229 ICPState *ss = icp->ss + server;
230 uint8_t old_cppr;
231 uint32_t old_xisr;
233 old_cppr = CPPR(ss);
234 ss->xirr = (ss->xirr & ~CPPR_MASK) | (cppr << 24);
236 if (cppr < old_cppr) {
237 if (XISR(ss) && (cppr <= ss->pending_priority)) {
238 old_xisr = XISR(ss);
239 ss->xirr &= ~XISR_MASK; /* Clear XISR */
240 ss->pending_priority = 0xff;
241 qemu_irq_lower(ss->output);
242 ics_reject(icp->ics, old_xisr);
244 } else {
245 if (!XISR(ss)) {
246 icp_resend(icp, server);
251 static void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
253 ICPState *ss = icp->ss + server;
255 ss->mfrr = mfrr;
256 if (mfrr < CPPR(ss)) {
257 icp_check_ipi(icp, server);
261 static uint32_t icp_accept(ICPState *ss)
263 uint32_t xirr = ss->xirr;
265 qemu_irq_lower(ss->output);
266 ss->xirr = ss->pending_priority << 24;
267 ss->pending_priority = 0xff;
269 trace_xics_icp_accept(xirr, ss->xirr);
271 return xirr;
274 static void icp_eoi(XICSState *icp, int server, uint32_t xirr)
276 ICPState *ss = icp->ss + server;
278 /* Send EOI -> ICS */
279 ss->xirr = (ss->xirr & ~CPPR_MASK) | (xirr & CPPR_MASK);
280 trace_xics_icp_eoi(server, xirr, ss->xirr);
281 ics_eoi(icp->ics, xirr & XISR_MASK);
282 if (!XISR(ss)) {
283 icp_resend(icp, server);
287 static void icp_irq(XICSState *icp, int server, int nr, uint8_t priority)
289 ICPState *ss = icp->ss + server;
291 trace_xics_icp_irq(server, nr, priority);
293 if ((priority >= CPPR(ss))
294 || (XISR(ss) && (ss->pending_priority <= priority))) {
295 ics_reject(icp->ics, nr);
296 } else {
297 if (XISR(ss)) {
298 ics_reject(icp->ics, XISR(ss));
300 ss->xirr = (ss->xirr & ~XISR_MASK) | (nr & XISR_MASK);
301 ss->pending_priority = priority;
302 trace_xics_icp_raise(ss->xirr, ss->pending_priority);
303 qemu_irq_raise(ss->output);
307 static void icp_dispatch_pre_save(void *opaque)
309 ICPState *ss = opaque;
310 ICPStateClass *info = ICP_GET_CLASS(ss);
312 if (info->pre_save) {
313 info->pre_save(ss);
317 static int icp_dispatch_post_load(void *opaque, int version_id)
319 ICPState *ss = opaque;
320 ICPStateClass *info = ICP_GET_CLASS(ss);
322 if (info->post_load) {
323 return info->post_load(ss, version_id);
326 return 0;
329 static const VMStateDescription vmstate_icp_server = {
330 .name = "icp/server",
331 .version_id = 1,
332 .minimum_version_id = 1,
333 .minimum_version_id_old = 1,
334 .pre_save = icp_dispatch_pre_save,
335 .post_load = icp_dispatch_post_load,
336 .fields = (VMStateField []) {
337 /* Sanity check */
338 VMSTATE_UINT32(xirr, ICPState),
339 VMSTATE_UINT8(pending_priority, ICPState),
340 VMSTATE_UINT8(mfrr, ICPState),
341 VMSTATE_END_OF_LIST()
345 static void icp_reset(DeviceState *dev)
347 ICPState *icp = ICP(dev);
349 icp->xirr = 0;
350 icp->pending_priority = 0xff;
351 icp->mfrr = 0xff;
353 /* Make all outputs are deasserted */
354 qemu_set_irq(icp->output, 0);
357 static void icp_class_init(ObjectClass *klass, void *data)
359 DeviceClass *dc = DEVICE_CLASS(klass);
361 dc->reset = icp_reset;
362 dc->vmsd = &vmstate_icp_server;
365 static const TypeInfo icp_info = {
366 .name = TYPE_ICP,
367 .parent = TYPE_DEVICE,
368 .instance_size = sizeof(ICPState),
369 .class_init = icp_class_init,
370 .class_size = sizeof(ICPStateClass),
374 * ICS: Source layer
376 static int ics_valid_irq(ICSState *ics, uint32_t nr)
378 return (nr >= ics->offset)
379 && (nr < (ics->offset + ics->nr_irqs));
382 static void resend_msi(ICSState *ics, int srcno)
384 ICSIRQState *irq = ics->irqs + srcno;
386 /* FIXME: filter by server#? */
387 if (irq->status & XICS_STATUS_REJECTED) {
388 irq->status &= ~XICS_STATUS_REJECTED;
389 if (irq->priority != 0xff) {
390 icp_irq(ics->icp, irq->server, srcno + ics->offset,
391 irq->priority);
396 static void resend_lsi(ICSState *ics, int srcno)
398 ICSIRQState *irq = ics->irqs + srcno;
400 if ((irq->priority != 0xff)
401 && (irq->status & XICS_STATUS_ASSERTED)
402 && !(irq->status & XICS_STATUS_SENT)) {
403 irq->status |= XICS_STATUS_SENT;
404 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
408 static void set_irq_msi(ICSState *ics, int srcno, int val)
410 ICSIRQState *irq = ics->irqs + srcno;
412 trace_xics_set_irq_msi(srcno, srcno + ics->offset);
414 if (val) {
415 if (irq->priority == 0xff) {
416 irq->status |= XICS_STATUS_MASKED_PENDING;
417 trace_xics_masked_pending();
418 } else {
419 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
424 static void set_irq_lsi(ICSState *ics, int srcno, int val)
426 ICSIRQState *irq = ics->irqs + srcno;
428 trace_xics_set_irq_lsi(srcno, srcno + ics->offset);
429 if (val) {
430 irq->status |= XICS_STATUS_ASSERTED;
431 } else {
432 irq->status &= ~XICS_STATUS_ASSERTED;
434 resend_lsi(ics, srcno);
437 static void ics_set_irq(void *opaque, int srcno, int val)
439 ICSState *ics = (ICSState *)opaque;
441 if (ics->islsi[srcno]) {
442 set_irq_lsi(ics, srcno, val);
443 } else {
444 set_irq_msi(ics, srcno, val);
448 static void write_xive_msi(ICSState *ics, int srcno)
450 ICSIRQState *irq = ics->irqs + srcno;
452 if (!(irq->status & XICS_STATUS_MASKED_PENDING)
453 || (irq->priority == 0xff)) {
454 return;
457 irq->status &= ~XICS_STATUS_MASKED_PENDING;
458 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
461 static void write_xive_lsi(ICSState *ics, int srcno)
463 resend_lsi(ics, srcno);
466 static void ics_write_xive(ICSState *ics, int nr, int server,
467 uint8_t priority, uint8_t saved_priority)
469 int srcno = nr - ics->offset;
470 ICSIRQState *irq = ics->irqs + srcno;
472 irq->server = server;
473 irq->priority = priority;
474 irq->saved_priority = saved_priority;
476 trace_xics_ics_write_xive(nr, srcno, server, priority);
478 if (ics->islsi[srcno]) {
479 write_xive_lsi(ics, srcno);
480 } else {
481 write_xive_msi(ics, srcno);
485 static void ics_reject(ICSState *ics, int nr)
487 ICSIRQState *irq = ics->irqs + nr - ics->offset;
489 trace_xics_ics_reject(nr, nr - ics->offset);
490 irq->status |= XICS_STATUS_REJECTED; /* Irrelevant but harmless for LSI */
491 irq->status &= ~XICS_STATUS_SENT; /* Irrelevant but harmless for MSI */
494 static void ics_resend(ICSState *ics)
496 int i;
498 for (i = 0; i < ics->nr_irqs; i++) {
499 /* FIXME: filter by server#? */
500 if (ics->islsi[i]) {
501 resend_lsi(ics, i);
502 } else {
503 resend_msi(ics, i);
508 static void ics_eoi(ICSState *ics, int nr)
510 int srcno = nr - ics->offset;
511 ICSIRQState *irq = ics->irqs + srcno;
513 trace_xics_ics_eoi(nr);
515 if (ics->islsi[srcno]) {
516 irq->status &= ~XICS_STATUS_SENT;
520 static void ics_reset(DeviceState *dev)
522 ICSState *ics = ICS(dev);
523 int i;
525 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
526 for (i = 0; i < ics->nr_irqs; i++) {
527 ics->irqs[i].priority = 0xff;
528 ics->irqs[i].saved_priority = 0xff;
532 static int ics_post_load(ICSState *ics, int version_id)
534 int i;
536 for (i = 0; i < ics->icp->nr_servers; i++) {
537 icp_resend(ics->icp, i);
540 return 0;
543 static void ics_dispatch_pre_save(void *opaque)
545 ICSState *ics = opaque;
546 ICSStateClass *info = ICS_GET_CLASS(ics);
548 if (info->pre_save) {
549 info->pre_save(ics);
553 static int ics_dispatch_post_load(void *opaque, int version_id)
555 ICSState *ics = opaque;
556 ICSStateClass *info = ICS_GET_CLASS(ics);
558 if (info->post_load) {
559 return info->post_load(ics, version_id);
562 return 0;
565 static const VMStateDescription vmstate_ics_irq = {
566 .name = "ics/irq",
567 .version_id = 1,
568 .minimum_version_id = 1,
569 .minimum_version_id_old = 1,
570 .fields = (VMStateField []) {
571 VMSTATE_UINT32(server, ICSIRQState),
572 VMSTATE_UINT8(priority, ICSIRQState),
573 VMSTATE_UINT8(saved_priority, ICSIRQState),
574 VMSTATE_UINT8(status, ICSIRQState),
575 VMSTATE_END_OF_LIST()
579 static const VMStateDescription vmstate_ics = {
580 .name = "ics",
581 .version_id = 1,
582 .minimum_version_id = 1,
583 .minimum_version_id_old = 1,
584 .pre_save = ics_dispatch_pre_save,
585 .post_load = ics_dispatch_post_load,
586 .fields = (VMStateField []) {
587 /* Sanity check */
588 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState),
590 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
591 vmstate_ics_irq, ICSIRQState),
592 VMSTATE_END_OF_LIST()
596 static void ics_initfn(Object *obj)
598 ICSState *ics = ICS(obj);
600 ics->offset = XICS_IRQ_BASE;
603 static void ics_realize(DeviceState *dev, Error **errp)
605 ICSState *ics = ICS(dev);
607 if (!ics->nr_irqs) {
608 error_setg(errp, "Number of interrupts needs to be greater 0");
609 return;
611 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
612 ics->islsi = g_malloc0(ics->nr_irqs * sizeof(bool));
613 ics->qirqs = qemu_allocate_irqs(ics_set_irq, ics, ics->nr_irqs);
616 static void ics_class_init(ObjectClass *klass, void *data)
618 DeviceClass *dc = DEVICE_CLASS(klass);
619 ICSStateClass *isc = ICS_CLASS(klass);
621 dc->realize = ics_realize;
622 dc->vmsd = &vmstate_ics;
623 dc->reset = ics_reset;
624 isc->post_load = ics_post_load;
627 static const TypeInfo ics_info = {
628 .name = TYPE_ICS,
629 .parent = TYPE_DEVICE,
630 .instance_size = sizeof(ICSState),
631 .class_init = ics_class_init,
632 .class_size = sizeof(ICSStateClass),
633 .instance_init = ics_initfn,
637 * Exported functions
640 qemu_irq xics_get_qirq(XICSState *icp, int irq)
642 if (!ics_valid_irq(icp->ics, irq)) {
643 return NULL;
646 return icp->ics->qirqs[irq - icp->ics->offset];
649 void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
651 assert(ics_valid_irq(icp->ics, irq));
653 icp->ics->islsi[irq - icp->ics->offset] = lsi;
657 * Guest interfaces
660 static target_ulong h_cppr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
661 target_ulong opcode, target_ulong *args)
663 CPUState *cs = CPU(cpu);
664 target_ulong cppr = args[0];
666 icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
667 return H_SUCCESS;
670 static target_ulong h_ipi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
671 target_ulong opcode, target_ulong *args)
673 target_ulong server = get_cpu_index_by_dt_id(args[0]);
674 target_ulong mfrr = args[1];
676 if (server >= spapr->icp->nr_servers) {
677 return H_PARAMETER;
680 icp_set_mfrr(spapr->icp, server, mfrr);
681 return H_SUCCESS;
684 static target_ulong h_xirr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
685 target_ulong opcode, target_ulong *args)
687 CPUState *cs = CPU(cpu);
688 uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
690 args[0] = xirr;
691 return H_SUCCESS;
694 static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPREnvironment *spapr,
695 target_ulong opcode, target_ulong *args)
697 CPUState *cs = CPU(cpu);
698 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
699 uint32_t xirr = icp_accept(ss);
701 args[0] = xirr;
702 args[1] = cpu_get_real_ticks();
703 return H_SUCCESS;
706 static target_ulong h_eoi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
707 target_ulong opcode, target_ulong *args)
709 CPUState *cs = CPU(cpu);
710 target_ulong xirr = args[0];
712 icp_eoi(spapr->icp, cs->cpu_index, xirr);
713 return H_SUCCESS;
716 static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPREnvironment *spapr,
717 target_ulong opcode, target_ulong *args)
719 CPUState *cs = CPU(cpu);
720 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
722 args[0] = ss->xirr;
723 args[1] = ss->mfrr;
725 return H_SUCCESS;
728 static void rtas_set_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
729 uint32_t token,
730 uint32_t nargs, target_ulong args,
731 uint32_t nret, target_ulong rets)
733 ICSState *ics = spapr->icp->ics;
734 uint32_t nr, server, priority;
736 if ((nargs != 3) || (nret != 1)) {
737 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
738 return;
741 nr = rtas_ld(args, 0);
742 server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
743 priority = rtas_ld(args, 2);
745 if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
746 || (priority > 0xff)) {
747 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
748 return;
751 ics_write_xive(ics, nr, server, priority, priority);
753 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
756 static void rtas_get_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
757 uint32_t token,
758 uint32_t nargs, target_ulong args,
759 uint32_t nret, target_ulong rets)
761 ICSState *ics = spapr->icp->ics;
762 uint32_t nr;
764 if ((nargs != 1) || (nret != 3)) {
765 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
766 return;
769 nr = rtas_ld(args, 0);
771 if (!ics_valid_irq(ics, nr)) {
772 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
773 return;
776 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
777 rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
778 rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
781 static void rtas_int_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
782 uint32_t token,
783 uint32_t nargs, target_ulong args,
784 uint32_t nret, target_ulong rets)
786 ICSState *ics = spapr->icp->ics;
787 uint32_t nr;
789 if ((nargs != 1) || (nret != 1)) {
790 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
791 return;
794 nr = rtas_ld(args, 0);
796 if (!ics_valid_irq(ics, nr)) {
797 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
798 return;
801 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
802 ics->irqs[nr - ics->offset].priority);
804 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
807 static void rtas_int_on(PowerPCCPU *cpu, sPAPREnvironment *spapr,
808 uint32_t token,
809 uint32_t nargs, target_ulong args,
810 uint32_t nret, target_ulong rets)
812 ICSState *ics = spapr->icp->ics;
813 uint32_t nr;
815 if ((nargs != 1) || (nret != 1)) {
816 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
817 return;
820 nr = rtas_ld(args, 0);
822 if (!ics_valid_irq(ics, nr)) {
823 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
824 return;
827 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
828 ics->irqs[nr - ics->offset].saved_priority,
829 ics->irqs[nr - ics->offset].saved_priority);
831 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
835 * XICS
838 static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
840 icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
843 static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
844 Error **errp)
846 int i;
848 icp->nr_servers = nr_servers;
850 icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
851 for (i = 0; i < icp->nr_servers; i++) {
852 char buffer[32];
853 object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_ICP);
854 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
855 object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
856 errp);
860 static void xics_realize(DeviceState *dev, Error **errp)
862 XICSState *icp = XICS(dev);
863 Error *error = NULL;
864 int i;
866 if (!icp->nr_servers) {
867 error_setg(errp, "Number of servers needs to be greater 0");
868 return;
871 /* Registration of global state belongs into realize */
872 spapr_rtas_register("ibm,set-xive", rtas_set_xive);
873 spapr_rtas_register("ibm,get-xive", rtas_get_xive);
874 spapr_rtas_register("ibm,int-off", rtas_int_off);
875 spapr_rtas_register("ibm,int-on", rtas_int_on);
877 spapr_register_hypercall(H_CPPR, h_cppr);
878 spapr_register_hypercall(H_IPI, h_ipi);
879 spapr_register_hypercall(H_XIRR, h_xirr);
880 spapr_register_hypercall(H_XIRR_X, h_xirr_x);
881 spapr_register_hypercall(H_EOI, h_eoi);
882 spapr_register_hypercall(H_IPOLL, h_ipoll);
884 object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
885 if (error) {
886 error_propagate(errp, error);
887 return;
890 for (i = 0; i < icp->nr_servers; i++) {
891 object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
892 if (error) {
893 error_propagate(errp, error);
894 return;
899 static void xics_initfn(Object *obj)
901 XICSState *xics = XICS(obj);
903 xics->ics = ICS(object_new(TYPE_ICS));
904 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
905 xics->ics->icp = xics;
908 static void xics_class_init(ObjectClass *oc, void *data)
910 DeviceClass *dc = DEVICE_CLASS(oc);
911 XICSStateClass *xsc = XICS_CLASS(oc);
913 dc->realize = xics_realize;
914 xsc->set_nr_irqs = xics_set_nr_irqs;
915 xsc->set_nr_servers = xics_set_nr_servers;
918 static const TypeInfo xics_info = {
919 .name = TYPE_XICS,
920 .parent = TYPE_XICS_COMMON,
921 .instance_size = sizeof(XICSState),
922 .class_size = sizeof(XICSStateClass),
923 .class_init = xics_class_init,
924 .instance_init = xics_initfn,
927 static void xics_register_types(void)
929 type_register_static(&xics_common_info);
930 type_register_static(&xics_info);
931 type_register_static(&ics_info);
932 type_register_static(&icp_info);
935 type_init(xics_register_types)