make check-block: Use default cache modes
[qemu/ar7.git] / hw / intc / xics.c
blob0fd2a84c7bc4c4955cc522297a51277487f4c47a
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 .pre_save = icp_dispatch_pre_save,
334 .post_load = icp_dispatch_post_load,
335 .fields = (VMStateField[]) {
336 /* Sanity check */
337 VMSTATE_UINT32(xirr, ICPState),
338 VMSTATE_UINT8(pending_priority, ICPState),
339 VMSTATE_UINT8(mfrr, ICPState),
340 VMSTATE_END_OF_LIST()
344 static void icp_reset(DeviceState *dev)
346 ICPState *icp = ICP(dev);
348 icp->xirr = 0;
349 icp->pending_priority = 0xff;
350 icp->mfrr = 0xff;
352 /* Make all outputs are deasserted */
353 qemu_set_irq(icp->output, 0);
356 static void icp_class_init(ObjectClass *klass, void *data)
358 DeviceClass *dc = DEVICE_CLASS(klass);
360 dc->reset = icp_reset;
361 dc->vmsd = &vmstate_icp_server;
364 static const TypeInfo icp_info = {
365 .name = TYPE_ICP,
366 .parent = TYPE_DEVICE,
367 .instance_size = sizeof(ICPState),
368 .class_init = icp_class_init,
369 .class_size = sizeof(ICPStateClass),
373 * ICS: Source layer
375 static int ics_valid_irq(ICSState *ics, uint32_t nr)
377 return (nr >= ics->offset)
378 && (nr < (ics->offset + ics->nr_irqs));
381 static void resend_msi(ICSState *ics, int srcno)
383 ICSIRQState *irq = ics->irqs + srcno;
385 /* FIXME: filter by server#? */
386 if (irq->status & XICS_STATUS_REJECTED) {
387 irq->status &= ~XICS_STATUS_REJECTED;
388 if (irq->priority != 0xff) {
389 icp_irq(ics->icp, irq->server, srcno + ics->offset,
390 irq->priority);
395 static void resend_lsi(ICSState *ics, int srcno)
397 ICSIRQState *irq = ics->irqs + srcno;
399 if ((irq->priority != 0xff)
400 && (irq->status & XICS_STATUS_ASSERTED)
401 && !(irq->status & XICS_STATUS_SENT)) {
402 irq->status |= XICS_STATUS_SENT;
403 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
407 static void set_irq_msi(ICSState *ics, int srcno, int val)
409 ICSIRQState *irq = ics->irqs + srcno;
411 trace_xics_set_irq_msi(srcno, srcno + ics->offset);
413 if (val) {
414 if (irq->priority == 0xff) {
415 irq->status |= XICS_STATUS_MASKED_PENDING;
416 trace_xics_masked_pending();
417 } else {
418 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
423 static void set_irq_lsi(ICSState *ics, int srcno, int val)
425 ICSIRQState *irq = ics->irqs + srcno;
427 trace_xics_set_irq_lsi(srcno, srcno + ics->offset);
428 if (val) {
429 irq->status |= XICS_STATUS_ASSERTED;
430 } else {
431 irq->status &= ~XICS_STATUS_ASSERTED;
433 resend_lsi(ics, srcno);
436 static void ics_set_irq(void *opaque, int srcno, int val)
438 ICSState *ics = (ICSState *)opaque;
440 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
441 set_irq_lsi(ics, srcno, val);
442 } else {
443 set_irq_msi(ics, srcno, val);
447 static void write_xive_msi(ICSState *ics, int srcno)
449 ICSIRQState *irq = ics->irqs + srcno;
451 if (!(irq->status & XICS_STATUS_MASKED_PENDING)
452 || (irq->priority == 0xff)) {
453 return;
456 irq->status &= ~XICS_STATUS_MASKED_PENDING;
457 icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
460 static void write_xive_lsi(ICSState *ics, int srcno)
462 resend_lsi(ics, srcno);
465 static void ics_write_xive(ICSState *ics, int nr, int server,
466 uint8_t priority, uint8_t saved_priority)
468 int srcno = nr - ics->offset;
469 ICSIRQState *irq = ics->irqs + srcno;
471 irq->server = server;
472 irq->priority = priority;
473 irq->saved_priority = saved_priority;
475 trace_xics_ics_write_xive(nr, srcno, server, priority);
477 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
478 write_xive_lsi(ics, srcno);
479 } else {
480 write_xive_msi(ics, srcno);
484 static void ics_reject(ICSState *ics, int nr)
486 ICSIRQState *irq = ics->irqs + nr - ics->offset;
488 trace_xics_ics_reject(nr, nr - ics->offset);
489 irq->status |= XICS_STATUS_REJECTED; /* Irrelevant but harmless for LSI */
490 irq->status &= ~XICS_STATUS_SENT; /* Irrelevant but harmless for MSI */
493 static void ics_resend(ICSState *ics)
495 int i;
497 for (i = 0; i < ics->nr_irqs; i++) {
498 /* FIXME: filter by server#? */
499 if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
500 resend_lsi(ics, i);
501 } else {
502 resend_msi(ics, i);
507 static void ics_eoi(ICSState *ics, int nr)
509 int srcno = nr - ics->offset;
510 ICSIRQState *irq = ics->irqs + srcno;
512 trace_xics_ics_eoi(nr);
514 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
515 irq->status &= ~XICS_STATUS_SENT;
519 static void ics_reset(DeviceState *dev)
521 ICSState *ics = ICS(dev);
522 int i;
523 uint8_t flags[ics->nr_irqs];
525 for (i = 0; i < ics->nr_irqs; i++) {
526 flags[i] = ics->irqs[i].flags;
529 memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
531 for (i = 0; i < ics->nr_irqs; i++) {
532 ics->irqs[i].priority = 0xff;
533 ics->irqs[i].saved_priority = 0xff;
534 ics->irqs[i].flags = flags[i];
538 static int ics_post_load(ICSState *ics, int version_id)
540 int i;
542 for (i = 0; i < ics->icp->nr_servers; i++) {
543 icp_resend(ics->icp, i);
546 return 0;
549 static void ics_dispatch_pre_save(void *opaque)
551 ICSState *ics = opaque;
552 ICSStateClass *info = ICS_GET_CLASS(ics);
554 if (info->pre_save) {
555 info->pre_save(ics);
559 static int ics_dispatch_post_load(void *opaque, int version_id)
561 ICSState *ics = opaque;
562 ICSStateClass *info = ICS_GET_CLASS(ics);
564 if (info->post_load) {
565 return info->post_load(ics, version_id);
568 return 0;
571 static const VMStateDescription vmstate_ics_irq = {
572 .name = "ics/irq",
573 .version_id = 2,
574 .minimum_version_id = 1,
575 .fields = (VMStateField[]) {
576 VMSTATE_UINT32(server, ICSIRQState),
577 VMSTATE_UINT8(priority, ICSIRQState),
578 VMSTATE_UINT8(saved_priority, ICSIRQState),
579 VMSTATE_UINT8(status, ICSIRQState),
580 VMSTATE_UINT8(flags, ICSIRQState),
581 VMSTATE_END_OF_LIST()
585 static const VMStateDescription vmstate_ics = {
586 .name = "ics",
587 .version_id = 1,
588 .minimum_version_id = 1,
589 .pre_save = ics_dispatch_pre_save,
590 .post_load = ics_dispatch_post_load,
591 .fields = (VMStateField[]) {
592 /* Sanity check */
593 VMSTATE_UINT32_EQUAL(nr_irqs, ICSState),
595 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
596 vmstate_ics_irq, ICSIRQState),
597 VMSTATE_END_OF_LIST()
601 static void ics_initfn(Object *obj)
603 ICSState *ics = ICS(obj);
605 ics->offset = XICS_IRQ_BASE;
608 static void ics_realize(DeviceState *dev, Error **errp)
610 ICSState *ics = ICS(dev);
612 if (!ics->nr_irqs) {
613 error_setg(errp, "Number of interrupts needs to be greater 0");
614 return;
616 ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
617 ics->qirqs = qemu_allocate_irqs(ics_set_irq, ics, ics->nr_irqs);
620 static void ics_class_init(ObjectClass *klass, void *data)
622 DeviceClass *dc = DEVICE_CLASS(klass);
623 ICSStateClass *isc = ICS_CLASS(klass);
625 dc->realize = ics_realize;
626 dc->vmsd = &vmstate_ics;
627 dc->reset = ics_reset;
628 isc->post_load = ics_post_load;
631 static const TypeInfo ics_info = {
632 .name = TYPE_ICS,
633 .parent = TYPE_DEVICE,
634 .instance_size = sizeof(ICSState),
635 .class_init = ics_class_init,
636 .class_size = sizeof(ICSStateClass),
637 .instance_init = ics_initfn,
641 * Exported functions
643 static int xics_find_source(XICSState *icp, int irq)
645 int sources = 1;
646 int src;
648 /* FIXME: implement multiple sources */
649 for (src = 0; src < sources; ++src) {
650 ICSState *ics = &icp->ics[src];
651 if (ics_valid_irq(ics, irq)) {
652 return src;
656 return -1;
659 qemu_irq xics_get_qirq(XICSState *icp, int irq)
661 int src = xics_find_source(icp, irq);
663 if (src >= 0) {
664 ICSState *ics = &icp->ics[src];
665 return ics->qirqs[irq - ics->offset];
668 return NULL;
671 static void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
673 assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
675 ics->irqs[srcno].flags |=
676 lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
679 void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
681 int src = xics_find_source(icp, irq);
682 ICSState *ics;
684 assert(src >= 0);
686 ics = &icp->ics[src];
687 ics_set_irq_type(ics, irq - ics->offset, lsi);
690 #define ICS_IRQ_FREE(ics, srcno) \
691 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
693 static int ics_find_free_block(ICSState *ics, int num, int alignnum)
695 int first, i;
697 for (first = 0; first < ics->nr_irqs; first += alignnum) {
698 if (num > (ics->nr_irqs - first)) {
699 return -1;
701 for (i = first; i < first + num; ++i) {
702 if (!ICS_IRQ_FREE(ics, i)) {
703 break;
706 if (i == (first + num)) {
707 return first;
711 return -1;
714 int xics_alloc(XICSState *icp, int src, int irq_hint, bool lsi)
716 ICSState *ics = &icp->ics[src];
717 int irq;
719 if (irq_hint) {
720 assert(src == xics_find_source(icp, irq_hint));
721 if (!ICS_IRQ_FREE(ics, irq_hint - ics->offset)) {
722 trace_xics_alloc_failed_hint(src, irq_hint);
723 return -1;
725 irq = irq_hint;
726 } else {
727 irq = ics_find_free_block(ics, 1, 1);
728 if (irq < 0) {
729 trace_xics_alloc_failed_no_left(src);
730 return -1;
732 irq += ics->offset;
735 ics_set_irq_type(ics, irq - ics->offset, lsi);
736 trace_xics_alloc(src, irq);
738 return irq;
742 * Allocate block of consequtive IRQs, returns a number of the first.
743 * If align==true, aligns the first IRQ number to num.
745 int xics_alloc_block(XICSState *icp, int src, int num, bool lsi, bool align)
747 int i, first = -1;
748 ICSState *ics = &icp->ics[src];
750 assert(src == 0);
752 * MSIMesage::data is used for storing VIRQ so
753 * it has to be aligned to num to support multiple
754 * MSI vectors. MSI-X is not affected by this.
755 * The hint is used for the first IRQ, the rest should
756 * be allocated continuously.
758 if (align) {
759 assert((num == 1) || (num == 2) || (num == 4) ||
760 (num == 8) || (num == 16) || (num == 32));
761 first = ics_find_free_block(ics, num, num);
762 } else {
763 first = ics_find_free_block(ics, num, 1);
766 if (first >= 0) {
767 for (i = first; i < first + num; ++i) {
768 ics_set_irq_type(ics, i, lsi);
771 first += ics->offset;
773 trace_xics_alloc_block(src, first, num, lsi, align);
775 return first;
778 static void ics_free(ICSState *ics, int srcno, int num)
780 int i;
782 for (i = srcno; i < srcno + num; ++i) {
783 if (ICS_IRQ_FREE(ics, i)) {
784 trace_xics_ics_free_warn(ics - ics->icp->ics, i + ics->offset);
786 memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
790 void xics_free(XICSState *icp, int irq, int num)
792 int src = xics_find_source(icp, irq);
794 if (src >= 0) {
795 ICSState *ics = &icp->ics[src];
797 /* FIXME: implement multiple sources */
798 assert(src == 0);
800 trace_xics_ics_free(ics - icp->ics, irq, num);
801 ics_free(ics, irq - ics->offset, num);
806 * Guest interfaces
809 static target_ulong h_cppr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
810 target_ulong opcode, target_ulong *args)
812 CPUState *cs = CPU(cpu);
813 target_ulong cppr = args[0];
815 icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
816 return H_SUCCESS;
819 static target_ulong h_ipi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
820 target_ulong opcode, target_ulong *args)
822 target_ulong server = get_cpu_index_by_dt_id(args[0]);
823 target_ulong mfrr = args[1];
825 if (server >= spapr->icp->nr_servers) {
826 return H_PARAMETER;
829 icp_set_mfrr(spapr->icp, server, mfrr);
830 return H_SUCCESS;
833 static target_ulong h_xirr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
834 target_ulong opcode, target_ulong *args)
836 CPUState *cs = CPU(cpu);
837 uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
839 args[0] = xirr;
840 return H_SUCCESS;
843 static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPREnvironment *spapr,
844 target_ulong opcode, target_ulong *args)
846 CPUState *cs = CPU(cpu);
847 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
848 uint32_t xirr = icp_accept(ss);
850 args[0] = xirr;
851 args[1] = cpu_get_real_ticks();
852 return H_SUCCESS;
855 static target_ulong h_eoi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
856 target_ulong opcode, target_ulong *args)
858 CPUState *cs = CPU(cpu);
859 target_ulong xirr = args[0];
861 icp_eoi(spapr->icp, cs->cpu_index, xirr);
862 return H_SUCCESS;
865 static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPREnvironment *spapr,
866 target_ulong opcode, target_ulong *args)
868 CPUState *cs = CPU(cpu);
869 ICPState *ss = &spapr->icp->ss[cs->cpu_index];
871 args[0] = ss->xirr;
872 args[1] = ss->mfrr;
874 return H_SUCCESS;
877 static void rtas_set_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
878 uint32_t token,
879 uint32_t nargs, target_ulong args,
880 uint32_t nret, target_ulong rets)
882 ICSState *ics = spapr->icp->ics;
883 uint32_t nr, server, priority;
885 if ((nargs != 3) || (nret != 1)) {
886 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
887 return;
890 nr = rtas_ld(args, 0);
891 server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
892 priority = rtas_ld(args, 2);
894 if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
895 || (priority > 0xff)) {
896 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
897 return;
900 ics_write_xive(ics, nr, server, priority, priority);
902 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
905 static void rtas_get_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
906 uint32_t token,
907 uint32_t nargs, target_ulong args,
908 uint32_t nret, target_ulong rets)
910 ICSState *ics = spapr->icp->ics;
911 uint32_t nr;
913 if ((nargs != 1) || (nret != 3)) {
914 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
915 return;
918 nr = rtas_ld(args, 0);
920 if (!ics_valid_irq(ics, nr)) {
921 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
922 return;
925 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
926 rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
927 rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
930 static void rtas_int_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
931 uint32_t token,
932 uint32_t nargs, target_ulong args,
933 uint32_t nret, target_ulong rets)
935 ICSState *ics = spapr->icp->ics;
936 uint32_t nr;
938 if ((nargs != 1) || (nret != 1)) {
939 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
940 return;
943 nr = rtas_ld(args, 0);
945 if (!ics_valid_irq(ics, nr)) {
946 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
947 return;
950 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
951 ics->irqs[nr - ics->offset].priority);
953 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
956 static void rtas_int_on(PowerPCCPU *cpu, sPAPREnvironment *spapr,
957 uint32_t token,
958 uint32_t nargs, target_ulong args,
959 uint32_t nret, target_ulong rets)
961 ICSState *ics = spapr->icp->ics;
962 uint32_t nr;
964 if ((nargs != 1) || (nret != 1)) {
965 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
966 return;
969 nr = rtas_ld(args, 0);
971 if (!ics_valid_irq(ics, nr)) {
972 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
973 return;
976 ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
977 ics->irqs[nr - ics->offset].saved_priority,
978 ics->irqs[nr - ics->offset].saved_priority);
980 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
984 * XICS
987 static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
989 icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
992 static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
993 Error **errp)
995 int i;
997 icp->nr_servers = nr_servers;
999 icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
1000 for (i = 0; i < icp->nr_servers; i++) {
1001 char buffer[32];
1002 object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_ICP);
1003 snprintf(buffer, sizeof(buffer), "icp[%d]", i);
1004 object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
1005 errp);
1009 static void xics_realize(DeviceState *dev, Error **errp)
1011 XICSState *icp = XICS(dev);
1012 Error *error = NULL;
1013 int i;
1015 if (!icp->nr_servers) {
1016 error_setg(errp, "Number of servers needs to be greater 0");
1017 return;
1020 /* Registration of global state belongs into realize */
1021 spapr_rtas_register(RTAS_IBM_SET_XIVE, "ibm,set-xive", rtas_set_xive);
1022 spapr_rtas_register(RTAS_IBM_GET_XIVE, "ibm,get-xive", rtas_get_xive);
1023 spapr_rtas_register(RTAS_IBM_INT_OFF, "ibm,int-off", rtas_int_off);
1024 spapr_rtas_register(RTAS_IBM_INT_ON, "ibm,int-on", rtas_int_on);
1026 spapr_register_hypercall(H_CPPR, h_cppr);
1027 spapr_register_hypercall(H_IPI, h_ipi);
1028 spapr_register_hypercall(H_XIRR, h_xirr);
1029 spapr_register_hypercall(H_XIRR_X, h_xirr_x);
1030 spapr_register_hypercall(H_EOI, h_eoi);
1031 spapr_register_hypercall(H_IPOLL, h_ipoll);
1033 object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
1034 if (error) {
1035 error_propagate(errp, error);
1036 return;
1039 for (i = 0; i < icp->nr_servers; i++) {
1040 object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
1041 if (error) {
1042 error_propagate(errp, error);
1043 return;
1048 static void xics_initfn(Object *obj)
1050 XICSState *xics = XICS(obj);
1052 xics->ics = ICS(object_new(TYPE_ICS));
1053 object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
1054 xics->ics->icp = xics;
1057 static void xics_class_init(ObjectClass *oc, void *data)
1059 DeviceClass *dc = DEVICE_CLASS(oc);
1060 XICSStateClass *xsc = XICS_CLASS(oc);
1062 dc->realize = xics_realize;
1063 xsc->set_nr_irqs = xics_set_nr_irqs;
1064 xsc->set_nr_servers = xics_set_nr_servers;
1067 static const TypeInfo xics_info = {
1068 .name = TYPE_XICS,
1069 .parent = TYPE_XICS_COMMON,
1070 .instance_size = sizeof(XICSState),
1071 .class_size = sizeof(XICSStateClass),
1072 .class_init = xics_class_init,
1073 .instance_init = xics_initfn,
1076 static void xics_register_types(void)
1078 type_register_static(&xics_common_info);
1079 type_register_static(&xics_info);
1080 type_register_static(&ics_info);
1081 type_register_static(&icp_info);
1084 type_init(xics_register_types)