nbd: enable use of TLS with nbd-server-start command
[qemu.git] / hw / intc / xics.c
blobe66ae328819ac2519dfc8b6317aa8058eddd550c
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)
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 trace_xics_alloc_failed_hint(src, 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 trace_xics_alloc_failed_no_left(src);
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)
748 int i, first = -1;
749 ICSState *ics = &icp->ics[src];
751 assert(src == 0);
753 * MSIMesage::data is used for storing VIRQ so
754 * it has to be aligned to num to support multiple
755 * MSI vectors. MSI-X is not affected by this.
756 * The hint is used for the first IRQ, the rest should
757 * be allocated continuously.
759 if (align) {
760 assert((num == 1) || (num == 2) || (num == 4) ||
761 (num == 8) || (num == 16) || (num == 32));
762 first = ics_find_free_block(ics, num, num);
763 } else {
764 first = ics_find_free_block(ics, num, 1);
767 if (first >= 0) {
768 for (i = first; i < first + num; ++i) {
769 ics_set_irq_type(ics, i, lsi);
772 first += ics->offset;
774 trace_xics_alloc_block(src, first, num, lsi, align);
776 return first;
779 static void ics_free(ICSState *ics, int srcno, int num)
781 int i;
783 for (i = srcno; i < srcno + num; ++i) {
784 if (ICS_IRQ_FREE(ics, i)) {
785 trace_xics_ics_free_warn(ics - ics->icp->ics, i + ics->offset);
787 memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
791 void xics_free(XICSState *icp, int irq, int num)
793 int src = xics_find_source(icp, irq);
795 if (src >= 0) {
796 ICSState *ics = &icp->ics[src];
798 /* FIXME: implement multiple sources */
799 assert(src == 0);
801 trace_xics_ics_free(ics - icp->ics, irq, num);
802 ics_free(ics, irq - ics->offset, num);
807 * Guest interfaces
810 static target_ulong h_cppr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
811 target_ulong opcode, target_ulong *args)
813 CPUState *cs = CPU(cpu);
814 target_ulong cppr = args[0];
816 icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
817 return H_SUCCESS;
820 static target_ulong h_ipi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
821 target_ulong opcode, target_ulong *args)
823 target_ulong server = get_cpu_index_by_dt_id(args[0]);
824 target_ulong mfrr = args[1];
826 if (server >= spapr->icp->nr_servers) {
827 return H_PARAMETER;
830 icp_set_mfrr(spapr->icp, server, mfrr);
831 return H_SUCCESS;
834 static target_ulong h_xirr(PowerPCCPU *cpu, sPAPRMachineState *spapr,
835 target_ulong opcode, target_ulong *args)
837 CPUState *cs = CPU(cpu);
838 uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
840 args[0] = xirr;
841 return H_SUCCESS;
844 static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPRMachineState *spapr,
845 target_ulong opcode, target_ulong *args)
847 CPUState *cs = CPU(cpu);
848 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
849 uint32_t xirr = icp_accept(ss);
851 args[0] = xirr;
852 args[1] = cpu_get_host_ticks();
853 return H_SUCCESS;
856 static target_ulong h_eoi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
857 target_ulong opcode, target_ulong *args)
859 CPUState *cs = CPU(cpu);
860 target_ulong xirr = args[0];
862 icp_eoi(spapr->icp, cs->cpu_index, xirr);
863 return H_SUCCESS;
866 static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPRMachineState *spapr,
867 target_ulong opcode, target_ulong *args)
869 CPUState *cs = CPU(cpu);
870 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
872 args[0] = ss->xirr;
873 args[1] = ss->mfrr;
875 return H_SUCCESS;
878 static void rtas_set_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
879 uint32_t token,
880 uint32_t nargs, target_ulong args,
881 uint32_t nret, target_ulong rets)
883 ICSState *ics = spapr->icp->ics;
884 uint32_t nr, server, priority;
886 if ((nargs != 3) || (nret != 1)) {
887 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
888 return;
891 nr = rtas_ld(args, 0);
892 server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
893 priority = rtas_ld(args, 2);
895 if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
896 || (priority > 0xff)) {
897 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
898 return;
901 ics_write_xive(ics, nr, server, priority, priority);
903 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
906 static void rtas_get_xive(PowerPCCPU *cpu, sPAPRMachineState *spapr,
907 uint32_t token,
908 uint32_t nargs, target_ulong args,
909 uint32_t nret, target_ulong rets)
911 ICSState *ics = spapr->icp->ics;
912 uint32_t nr;
914 if ((nargs != 1) || (nret != 3)) {
915 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
916 return;
919 nr = rtas_ld(args, 0);
921 if (!ics_valid_irq(ics, nr)) {
922 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
923 return;
926 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
927 rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
928 rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
931 static void rtas_int_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
932 uint32_t token,
933 uint32_t nargs, target_ulong args,
934 uint32_t nret, target_ulong rets)
936 ICSState *ics = spapr->icp->ics;
937 uint32_t nr;
939 if ((nargs != 1) || (nret != 1)) {
940 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
941 return;
944 nr = rtas_ld(args, 0);
946 if (!ics_valid_irq(ics, nr)) {
947 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
948 return;
951 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
952 ics->irqs[nr - ics->offset].priority);
954 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
957 static void rtas_int_on(PowerPCCPU *cpu, sPAPRMachineState *spapr,
958 uint32_t token,
959 uint32_t nargs, target_ulong args,
960 uint32_t nret, target_ulong rets)
962 ICSState *ics = spapr->icp->ics;
963 uint32_t nr;
965 if ((nargs != 1) || (nret != 1)) {
966 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
967 return;
970 nr = rtas_ld(args, 0);
972 if (!ics_valid_irq(ics, nr)) {
973 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
974 return;
977 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
978 ics->irqs[nr - ics->offset].saved_priority,
979 ics->irqs[nr - ics->offset].saved_priority);
981 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
985 * XICS
988 static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
990 icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
993 static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
994 Error **errp)
996 int i;
998 icp->nr_servers = nr_servers;
1000 icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
1001 for (i = 0; i < icp->nr_servers; i++) {
1002 char buffer[32];
1003 object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_ICP);
1004 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
1005 object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
1006 errp);
1010 static void xics_realize(DeviceState *dev, Error **errp)
1012 XICSState *icp = XICS(dev);
1013 Error *error = NULL;
1014 int i;
1016 if (!icp->nr_servers) {
1017 error_setg(errp, "Number of servers needs to be greater 0");
1018 return;
1021 /* Registration of global state belongs into realize */
1022 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_set_xive);
1023 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_get_xive);
1024 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_int_off);
1025 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_int_on);
1027 spapr_register_hypercall(H_CPPR, h_cppr);
1028 spapr_register_hypercall(H_IPI, h_ipi);
1029 spapr_register_hypercall(H_XIRR, h_xirr);
1030 spapr_register_hypercall(H_XIRR_X, h_xirr_x);
1031 spapr_register_hypercall(H_EOI, h_eoi);
1032 spapr_register_hypercall(H_IPOLL, h_ipoll);
1034 object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
1035 if (error) {
1036 error_propagate(errp, error);
1037 return;
1040 for (i = 0; i < icp->nr_servers; i++) {
1041 object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
1042 if (error) {
1043 error_propagate(errp, error);
1044 return;
1049 static void xics_initfn(Object *obj)
1051 XICSState *xics = XICS(obj);
1053 xics->ics = ICS(object_new(TYPE_ICS));
1054 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
1055 xics->ics->icp = xics;
1058 static void xics_class_init(ObjectClass *oc, void *data)
1060 DeviceClass *dc = DEVICE_CLASS(oc);
1061 XICSStateClass *xsc = XICS_CLASS(oc);
1063 dc->realize = xics_realize;
1064 xsc->set_nr_irqs = xics_set_nr_irqs;
1065 xsc->set_nr_servers = xics_set_nr_servers;
1068 static const TypeInfo xics_info = {
1069 .name = TYPE_XICS,
1070 .parent = TYPE_XICS_COMMON,
1071 .instance_size = sizeof(XICSState),
1072 .class_size = sizeof(XICSStateClass),
1073 .class_init = xics_class_init,
1074 .instance_init = xics_initfn,
1077 static void xics_register_types(void)
1079 type_register_static(&xics_common_info);
1080 type_register_static(&xics_info);
1081 type_register_static(&ics_info);
1082 type_register_static(&icp_info);
1085 type_init(xics_register_types)