nvdimm acpi: emulate dsm method
[qemu/ar7.git] / hw / intc / xics.c
blob213a3709254c77bea17f1dc572d43c94922d168d
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 "hw/hw.h"
30 #include "trace.h"
31 #include "qemu/timer.h"
32 #include "hw/ppc/spapr.h"
33 #include "hw/ppc/xics.h"
34 #include "qemu/error-report.h"
35 #include "qapi/visitor.h"
37 static int get_cpu_index_by_dt_id(int cpu_dt_id)
39 PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
41 if (cpu) {
42 return cpu->parent_obj.cpu_index;
45 return -1;
48 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu)
50 CPUState *cs = CPU(cpu);
51 CPUPPCState *env = &cpu->env;
52 ICPState *ss = &icp->ss[cs->cpu_index];
53 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
55 assert(cs->cpu_index < icp->nr_servers);
57 if (info->cpu_setup) {
58 info->cpu_setup(icp, cpu);
61 switch (PPC_INPUT(env)) {
62 case PPC_FLAGS_INPUT_POWER7:
63 ss->output = env->irq_inputs[POWER7_INPUT_INT];
64 break;
66 case PPC_FLAGS_INPUT_970:
67 ss->output = env->irq_inputs[PPC970_INPUT_INT];
68 break;
70 default:
71 error_report("XICS interrupt controller does not support this CPU "
72 "bus model");
73 abort();
78 * XICS Common class - parent for emulated XICS and KVM-XICS
80 static void xics_common_reset(DeviceState *d)
82 XICSState *icp = XICS_COMMON(d);
83 int i;
85 for (i = 0; i < icp->nr_servers; i++) {
86 device_reset(DEVICE(&icp->ss[i]));
89 device_reset(DEVICE(icp->ics));
92 static void xics_prop_get_nr_irqs(Object *obj, Visitor *v, const char *name,
93 void *opaque, Error **errp)
95 XICSState *icp = XICS_COMMON(obj);
96 int64_t value = icp->nr_irqs;
98 visit_type_int(v, name, &value, errp);
101 static void xics_prop_set_nr_irqs(Object *obj, Visitor *v, const char *name,
102 void *opaque, Error **errp)
104 XICSState *icp = XICS_COMMON(obj);
105 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
106 Error *error = NULL;
107 int64_t value;
109 visit_type_int(v, name, &value, &error);
110 if (error) {
111 error_propagate(errp, error);
112 return;
114 if (icp->nr_irqs) {
115 error_setg(errp, "Number of interrupts is already set to %u",
116 icp->nr_irqs);
117 return;
120 assert(info->set_nr_irqs);
121 assert(icp->ics);
122 info->set_nr_irqs(icp, value, errp);
125 static void xics_prop_get_nr_servers(Object *obj, Visitor *v,
126 const char *name, void *opaque,
127 Error **errp)
129 XICSState *icp = XICS_COMMON(obj);
130 int64_t value = icp->nr_servers;
132 visit_type_int(v, name, &value, errp);
135 static void xics_prop_set_nr_servers(Object *obj, Visitor *v,
136 const char *name, void *opaque,
137 Error **errp)
139 XICSState *icp = XICS_COMMON(obj);
140 XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
141 Error *error = NULL;
142 int64_t value;
144 visit_type_int(v, name, &value, &error);
145 if (error) {
146 error_propagate(errp, error);
147 return;
149 if (icp->nr_servers) {
150 error_setg(errp, "Number of servers is already set to %u",
151 icp->nr_servers);
152 return;
155 assert(info->set_nr_servers);
156 info->set_nr_servers(icp, value, errp);
159 static void xics_common_initfn(Object *obj)
161 object_property_add(obj, "nr_irqs", "int",
162 xics_prop_get_nr_irqs, xics_prop_set_nr_irqs,
163 NULL, NULL, NULL);
164 object_property_add(obj, "nr_servers", "int",
165 xics_prop_get_nr_servers, xics_prop_set_nr_servers,
166 NULL, NULL, NULL);
169 static void xics_common_class_init(ObjectClass *oc, void *data)
171 DeviceClass *dc = DEVICE_CLASS(oc);
173 dc->reset = xics_common_reset;
176 static const TypeInfo xics_common_info = {
177 .name = TYPE_XICS_COMMON,
178 .parent = TYPE_SYS_BUS_DEVICE,
179 .instance_size = sizeof(XICSState),
180 .class_size = sizeof(XICSStateClass),
181 .instance_init = xics_common_initfn,
182 .class_init = xics_common_class_init,
186 * ICP: Presentation layer
189 #define XISR_MASK 0x00ffffff
190 #define CPPR_MASK 0xff000000
192 #define XISR(ss) (((ss)->xirr) & XISR_MASK)
193 #define CPPR(ss) (((ss)->xirr) >> 24)
195 static void ics_reject(ICSState *ics, int nr);
196 static void ics_resend(ICSState *ics);
197 static void ics_eoi(ICSState *ics, int nr);
199 static void icp_check_ipi(XICSState *icp, int server)
201 ICPState *ss = icp->ss + server;
203 if (XISR(ss) && (ss->pending_priority <= ss->mfrr)) {
204 return;
207 trace_xics_icp_check_ipi(server, ss->mfrr);
209 if (XISR(ss)) {
210 ics_reject(icp->ics, XISR(ss));
213 ss->xirr = (ss->xirr & ~XISR_MASK) | XICS_IPI;
214 ss->pending_priority = ss->mfrr;
215 qemu_irq_raise(ss->output);
218 static void icp_resend(XICSState *icp, int server)
220 ICPState *ss = icp->ss + server;
222 if (ss->mfrr < CPPR(ss)) {
223 icp_check_ipi(icp, server);
225 ics_resend(icp->ics);
228 static void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
230 ICPState *ss = icp->ss + server;
231 uint8_t old_cppr;
232 uint32_t old_xisr;
234 old_cppr = CPPR(ss);
235 ss->xirr = (ss->xirr & ~CPPR_MASK) | (cppr << 24);
237 if (cppr < old_cppr) {
238 if (XISR(ss) && (cppr <= ss->pending_priority)) {
239 old_xisr = XISR(ss);
240 ss->xirr &= ~XISR_MASK; /* Clear XISR */
241 ss->pending_priority = 0xff;
242 qemu_irq_lower(ss->output);
243 ics_reject(icp->ics, old_xisr);
245 } else {
246 if (!XISR(ss)) {
247 icp_resend(icp, server);
252 static void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
254 ICPState *ss = icp->ss + server;
256 ss->mfrr = mfrr;
257 if (mfrr < CPPR(ss)) {
258 icp_check_ipi(icp, server);
262 static uint32_t icp_accept(ICPState *ss)
264 uint32_t xirr = ss->xirr;
266 qemu_irq_lower(ss->output);
267 ss->xirr = ss->pending_priority << 24;
268 ss->pending_priority = 0xff;
270 trace_xics_icp_accept(xirr, ss->xirr);
272 return xirr;
275 static void icp_eoi(XICSState *icp, int server, uint32_t xirr)
277 ICPState *ss = icp->ss + server;
279 /* Send EOI -> ICS */
280 ss->xirr = (ss->xirr & ~CPPR_MASK) | (xirr & CPPR_MASK);
281 trace_xics_icp_eoi(server, xirr, ss->xirr);
282 ics_eoi(icp->ics, xirr & XISR_MASK);
283 if (!XISR(ss)) {
284 icp_resend(icp, server);
288 static void icp_irq(XICSState *icp, int server, int nr, uint8_t priority)
290 ICPState *ss = icp->ss + server;
292 trace_xics_icp_irq(server, nr, priority);
294 if ((priority >= CPPR(ss))
295 || (XISR(ss) && (ss->pending_priority <= priority))) {
296 ics_reject(icp->ics, nr);
297 } else {
298 if (XISR(ss)) {
299 ics_reject(icp->ics, XISR(ss));
301 ss->xirr = (ss->xirr & ~XISR_MASK) | (nr & XISR_MASK);
302 ss->pending_priority = priority;
303 trace_xics_icp_raise(ss->xirr, ss->pending_priority);
304 qemu_irq_raise(ss->output);
308 static void icp_dispatch_pre_save(void *opaque)
310 ICPState *ss = opaque;
311 ICPStateClass *info = ICP_GET_CLASS(ss);
313 if (info->pre_save) {
314 info->pre_save(ss);
318 static int icp_dispatch_post_load(void *opaque, int version_id)
320 ICPState *ss = opaque;
321 ICPStateClass *info = ICP_GET_CLASS(ss);
323 if (info->post_load) {
324 return info->post_load(ss, version_id);
327 return 0;
330 static const VMStateDescription vmstate_icp_server = {
331 .name = "icp/server",
332 .version_id = 1,
333 .minimum_version_id = 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->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
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->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
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->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
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->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
516 irq->status &= ~XICS_STATUS_SENT;
520 static void ics_reset(DeviceState *dev)
522 ICSState *ics = ICS(dev);
523 int i;
524 uint8_t flags[ics->nr_irqs];
526 for (i = 0; i < ics->nr_irqs; i++) {
527 flags[i] = ics->irqs[i].flags;
530 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
532 for (i = 0; i < ics->nr_irqs; i++) {
533 ics->irqs[i].priority = 0xff;
534 ics->irqs[i].saved_priority = 0xff;
535 ics->irqs[i].flags = flags[i];
539 static int ics_post_load(ICSState *ics, int version_id)
541 int i;
543 for (i = 0; i < ics->icp->nr_servers; i++) {
544 icp_resend(ics->icp, i);
547 return 0;
550 static void ics_dispatch_pre_save(void *opaque)
552 ICSState *ics = opaque;
553 ICSStateClass *info = ICS_GET_CLASS(ics);
555 if (info->pre_save) {
556 info->pre_save(ics);
560 static int ics_dispatch_post_load(void *opaque, int version_id)
562 ICSState *ics = opaque;
563 ICSStateClass *info = ICS_GET_CLASS(ics);
565 if (info->post_load) {
566 return info->post_load(ics, version_id);
569 return 0;
572 static const VMStateDescription vmstate_ics_irq = {
573 .name = "ics/irq",
574 .version_id = 2,
575 .minimum_version_id = 1,
576 .fields = (VMStateField[]) {
577 VMSTATE_UINT32(server, ICSIRQState),
578 VMSTATE_UINT8(priority, ICSIRQState),
579 VMSTATE_UINT8(saved_priority, ICSIRQState),
580 VMSTATE_UINT8(status, ICSIRQState),
581 VMSTATE_UINT8(flags, ICSIRQState),
582 VMSTATE_END_OF_LIST()
586 static const VMStateDescription vmstate_ics = {
587 .name = "ics",
588 .version_id = 1,
589 .minimum_version_id = 1,
590 .pre_save = ics_dispatch_pre_save,
591 .post_load = ics_dispatch_post_load,
592 .fields = (VMStateField[]) {
593 /* Sanity check */
594 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState),
596 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
597 vmstate_ics_irq, ICSIRQState),
598 VMSTATE_END_OF_LIST()
602 static void ics_initfn(Object *obj)
604 ICSState *ics = ICS(obj);
606 ics->offset = XICS_IRQ_BASE;
609 static void ics_realize(DeviceState *dev, Error **errp)
611 ICSState *ics = ICS(dev);
613 if (!ics->nr_irqs) {
614 error_setg(errp, "Number of interrupts needs to be greater 0");
615 return;
617 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
618 ics->qirqs = qemu_allocate_irqs(ics_set_irq, ics, ics->nr_irqs);
621 static void ics_class_init(ObjectClass *klass, void *data)
623 DeviceClass *dc = DEVICE_CLASS(klass);
624 ICSStateClass *isc = ICS_CLASS(klass);
626 dc->realize = ics_realize;
627 dc->vmsd = &vmstate_ics;
628 dc->reset = ics_reset;
629 isc->post_load = ics_post_load;
632 static const TypeInfo ics_info = {
633 .name = TYPE_ICS,
634 .parent = TYPE_DEVICE,
635 .instance_size = sizeof(ICSState),
636 .class_init = ics_class_init,
637 .class_size = sizeof(ICSStateClass),
638 .instance_init = ics_initfn,
642 * Exported functions
644 static int xics_find_source(XICSState *icp, int irq)
646 int sources = 1;
647 int src;
649 /* FIXME: implement multiple sources */
650 for (src = 0; src < sources; ++src) {
651 ICSState *ics = &icp->ics[src];
652 if (ics_valid_irq(ics, irq)) {
653 return src;
657 return -1;
660 qemu_irq xics_get_qirq(XICSState *icp, int irq)
662 int src = xics_find_source(icp, irq);
664 if (src >= 0) {
665 ICSState *ics = &icp->ics[src];
666 return ics->qirqs[irq - ics->offset];
669 return NULL;
672 static void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
674 assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
676 ics->irqs[srcno].flags |=
677 lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
680 void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
682 int src = xics_find_source(icp, irq);
683 ICSState *ics;
685 assert(src >= 0);
687 ics = &icp->ics[src];
688 ics_set_irq_type(ics, irq - ics->offset, lsi);
691 #define ICS_IRQ_FREE(ics, srcno) \
692 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
694 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
696 int first, i;
698 for (first = 0; first < ics->nr_irqs; first += alignnum) {
699 if (num > (ics->nr_irqs - first)) {
700 return -1;
702 for (i = first; i < first + num; ++i) {
703 if (!ICS_IRQ_FREE(ics, i)) {
704 break;
707 if (i == (first + num)) {
708 return first;
712 return -1;
715 int xics_alloc(XICSState *icp, int src, int irq_hint, bool lsi, Error **errp)
717 ICSState *ics = &icp->ics[src];
718 int irq;
720 if (irq_hint) {
721 assert(src == xics_find_source(icp, irq_hint));
722 if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
723 error_setg(errp, "can't allocate IRQ %d: already in use", irq_hint);
724 return -1;
726 irq = irq_hint;
727 } else {
728 irq = ics_find_free_block(ics, 1, 1);
729 if (irq < 0) {
730 error_setg(errp, "can't allocate IRQ: no IRQ left");
731 return -1;
733 irq += ics->offset;
736 ics_set_irq_type(ics, irq - ics->offset, lsi);
737 trace_xics_alloc(src, irq);
739 return irq;
743 * Allocate block of consecutive IRQs, and return the number of the first IRQ in the block.
744 * If align==true, aligns the first IRQ number to num.
746 int xics_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align,
747 Error **errp)
749 int i, first = -1;
750 ICSState *ics = &icp->ics[src];
752 assert(src == 0);
754 * MSIMesage::data is used for storing VIRQ so
755 * it has to be aligned to num to support multiple
756 * MSI vectors. MSI-X is not affected by this.
757 * The hint is used for the first IRQ, the rest should
758 * be allocated continuously.
760 if (align) {
761 assert((num == 1) || (num == 2) || (num == 4) ||
762 (num == 8) || (num == 16) || (num == 32));
763 first = ics_find_free_block(ics, num, num);
764 } else {
765 first = ics_find_free_block(ics, num, 1);
767 if (first < 0) {
768 error_setg(errp, "can't find a free %d-IRQ block", num);
769 return -1;
772 if (first >= 0) {
773 for (i = first; i < first + num; ++i) {
774 ics_set_irq_type(ics, i, lsi);
777 first += ics->offset;
779 trace_xics_alloc_block(src, first, num, lsi, align);
781 return first;
784 static void ics_free(ICSState *ics, int srcno, int num)
786 int i;
788 for (i = srcno; i < srcno + num; ++i) {
789 if (ICS_IRQ_FREE(ics, i)) {
790 trace_xics_ics_free_warn(ics - ics->icp->ics, i + ics->offset);
792 memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
796 void xics_free(XICSState *icp, int irq, int num)
798 int src = xics_find_source(icp, irq);
800 if (src >= 0) {
801 ICSState *ics = &icp->ics[src];
803 /* FIXME: implement multiple sources */
804 assert(src == 0);
806 trace_xics_ics_free(ics - icp->ics, irq, num);
807 ics_free(ics, irq - ics->offset, num);
812 * Guest interfaces
815 static target_ulong h_cppr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
816 target_ulong opcode, target_ulong *args)
818 CPUState *cs = CPU(cpu);
819 target_ulong cppr = args[0];
821 icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
822 return H_SUCCESS;
825 static target_ulong h_ipi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
826 target_ulong opcode, target_ulong *args)
828 target_ulong server = get_cpu_index_by_dt_id(args[0]);
829 target_ulong mfrr = args[1];
831 if (server >= spapr->icp->nr_servers) {
832 return H_PARAMETER;
835 icp_set_mfrr(spapr->icp, server, mfrr);
836 return H_SUCCESS;
839 static target_ulong h_xirr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
840 target_ulong opcode, target_ulong *args)
842 CPUState *cs = CPU(cpu);
843 uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
845 args[0] = xirr;
846 return H_SUCCESS;
849 static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPRMachineState *spapr,
850 target_ulong opcode, target_ulong *args)
852 CPUState *cs = CPU(cpu);
853 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
854 uint32_t xirr = icp_accept(ss);
856 args[0] = xirr;
857 args[1] = cpu_get_host_ticks();
858 return H_SUCCESS;
861 static target_ulong h_eoi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
862 target_ulong opcode, target_ulong *args)
864 CPUState *cs = CPU(cpu);
865 target_ulong xirr = args[0];
867 icp_eoi(spapr->icp, cs->cpu_index, xirr);
868 return H_SUCCESS;
871 static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPRMachineState *spapr,
872 target_ulong opcode, target_ulong *args)
874 CPUState *cs = CPU(cpu);
875 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
877 args[0] = ss->xirr;
878 args[1] = ss->mfrr;
880 return H_SUCCESS;
883 static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
884 uint32_t token,
885 uint32_t nargs, target_ulong args,
886 uint32_t nret, target_ulong rets)
888 ICSState *ics = spapr->icp->ics;
889 uint32_t nr, server, priority;
891 if ((nargs != 3) || (nret != 1)) {
892 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
893 return;
896 nr = rtas_ld(args, 0);
897 server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
898 priority = rtas_ld(args, 2);
900 if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
901 || (priority > 0xff)) {
902 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
903 return;
906 ics_write_xive(ics, nr, server, priority, priority);
908 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
911 static void rtas_get_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
912 uint32_t token,
913 uint32_t nargs, target_ulong args,
914 uint32_t nret, target_ulong rets)
916 ICSState *ics = spapr->icp->ics;
917 uint32_t nr;
919 if ((nargs != 1) || (nret != 3)) {
920 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
921 return;
924 nr = rtas_ld(args, 0);
926 if (!ics_valid_irq(ics, nr)) {
927 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
928 return;
931 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
932 rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
933 rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
936 static void rtas_int_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
937 uint32_t token,
938 uint32_t nargs, target_ulong args,
939 uint32_t nret, target_ulong rets)
941 ICSState *ics = spapr->icp->ics;
942 uint32_t nr;
944 if ((nargs != 1) || (nret != 1)) {
945 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
946 return;
949 nr = rtas_ld(args, 0);
951 if (!ics_valid_irq(ics, nr)) {
952 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
953 return;
956 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
957 ics->irqs[nr - ics->offset].priority);
959 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
962 static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
963 uint32_t token,
964 uint32_t nargs, target_ulong args,
965 uint32_t nret, target_ulong rets)
967 ICSState *ics = spapr->icp->ics;
968 uint32_t nr;
970 if ((nargs != 1) || (nret != 1)) {
971 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
972 return;
975 nr = rtas_ld(args, 0);
977 if (!ics_valid_irq(ics, nr)) {
978 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
979 return;
982 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
983 ics->irqs[nr - ics->offset].saved_priority,
984 ics->irqs[nr - ics->offset].saved_priority);
986 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
990 * XICS
993 static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
995 icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
998 static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
999 Error **errp)
1001 int i;
1003 icp->nr_servers = nr_servers;
1005 icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
1006 for (i = 0; i < icp->nr_servers; i++) {
1007 char buffer[32];
1008 object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_ICP);
1009 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
1010 object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
1011 errp);
1015 static void xics_realize(DeviceState *dev, Error **errp)
1017 XICSState *icp = XICS(dev);
1018 Error *error = NULL;
1019 int i;
1021 if (!icp->nr_servers) {
1022 error_setg(errp, "Number of servers needs to be greater 0");
1023 return;
1026 /* Registration of global state belongs into realize */
1027 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_set_xive);
1028 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_get_xive);
1029 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_int_off);
1030 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_int_on);
1032 spapr_register_hypercall(H_CPPR, h_cppr);
1033 spapr_register_hypercall(H_IPI, h_ipi);
1034 spapr_register_hypercall(H_XIRR, h_xirr);
1035 spapr_register_hypercall(H_XIRR_X, h_xirr_x);
1036 spapr_register_hypercall(H_EOI, h_eoi);
1037 spapr_register_hypercall(H_IPOLL, h_ipoll);
1039 object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
1040 if (error) {
1041 error_propagate(errp, error);
1042 return;
1045 for (i = 0; i < icp->nr_servers; i++) {
1046 object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
1047 if (error) {
1048 error_propagate(errp, error);
1049 return;
1054 static void xics_initfn(Object *obj)
1056 XICSState *xics = XICS(obj);
1058 xics->ics = ICS(object_new(TYPE_ICS));
1059 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
1060 xics->ics->icp = xics;
1063 static void xics_class_init(ObjectClass *oc, void *data)
1065 DeviceClass *dc = DEVICE_CLASS(oc);
1066 XICSStateClass *xsc = XICS_CLASS(oc);
1068 dc->realize = xics_realize;
1069 xsc->set_nr_irqs = xics_set_nr_irqs;
1070 xsc->set_nr_servers = xics_set_nr_servers;
1073 static const TypeInfo xics_info = {
1074 .name = TYPE_XICS,
1075 .parent = TYPE_XICS_COMMON,
1076 .instance_size = sizeof(XICSState),
1077 .class_size = sizeof(XICSStateClass),
1078 .class_init = xics_class_init,
1079 .instance_init = xics_initfn,
1082 static void xics_register_types(void)
1084 type_register_static(&xics_common_info);
1085 type_register_static(&xics_info);
1086 type_register_static(&ics_info);
1087 type_register_static(&icp_info);
1090 type_init(xics_register_types)