xref: /openbmc/qemu/hw/intc/xics.c (revision 3aff6c2f)
1 /*
2  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3  *
4  * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics
5  *
6  * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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.
25  *
26  */
27 
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"
35 
36 static int get_cpu_index_by_dt_id(int cpu_dt_id)
37 {
38     PowerPCCPU *cpu = ppc_get_vcpu_by_dt_id(cpu_dt_id);
39 
40     if (cpu) {
41         return cpu->parent_obj.cpu_index;
42     }
43 
44     return -1;
45 }
46 
47 void xics_cpu_setup(XICSState *icp, PowerPCCPU *cpu)
48 {
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);
53 
54     assert(cs->cpu_index < icp->nr_servers);
55 
56     if (info->cpu_setup) {
57         info->cpu_setup(icp, cpu);
58     }
59 
60     switch (PPC_INPUT(env)) {
61     case PPC_FLAGS_INPUT_POWER7:
62         ss->output = env->irq_inputs[POWER7_INPUT_INT];
63         break;
64 
65     case PPC_FLAGS_INPUT_970:
66         ss->output = env->irq_inputs[PPC970_INPUT_INT];
67         break;
68 
69     default:
70         error_report("XICS interrupt controller does not support this CPU "
71                      "bus model");
72         abort();
73     }
74 }
75 
76 /*
77  * XICS Common class - parent for emulated XICS and KVM-XICS
78  */
79 static void xics_common_reset(DeviceState *d)
80 {
81     XICSState *icp = XICS_COMMON(d);
82     int i;
83 
84     for (i = 0; i < icp->nr_servers; i++) {
85         device_reset(DEVICE(&icp->ss[i]));
86     }
87 
88     device_reset(DEVICE(icp->ics));
89 }
90 
91 static void xics_prop_get_nr_irqs(Object *obj, Visitor *v,
92                                   void *opaque, const char *name, Error **errp)
93 {
94     XICSState *icp = XICS_COMMON(obj);
95     int64_t value = icp->nr_irqs;
96 
97     visit_type_int(v, &value, name, errp);
98 }
99 
100 static void xics_prop_set_nr_irqs(Object *obj, Visitor *v,
101                                   void *opaque, const char *name, Error **errp)
102 {
103     XICSState *icp = XICS_COMMON(obj);
104     XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
105     Error *error = NULL;
106     int64_t value;
107 
108     visit_type_int(v, &value, name, &error);
109     if (error) {
110         error_propagate(errp, error);
111         return;
112     }
113     if (icp->nr_irqs) {
114         error_setg(errp, "Number of interrupts is already set to %u",
115                    icp->nr_irqs);
116         return;
117     }
118 
119     assert(info->set_nr_irqs);
120     assert(icp->ics);
121     info->set_nr_irqs(icp, value, errp);
122 }
123 
124 static void xics_prop_get_nr_servers(Object *obj, Visitor *v,
125                                      void *opaque, const char *name,
126                                      Error **errp)
127 {
128     XICSState *icp = XICS_COMMON(obj);
129     int64_t value = icp->nr_servers;
130 
131     visit_type_int(v, &value, name, errp);
132 }
133 
134 static void xics_prop_set_nr_servers(Object *obj, Visitor *v,
135                                      void *opaque, const char *name,
136                                      Error **errp)
137 {
138     XICSState *icp = XICS_COMMON(obj);
139     XICSStateClass *info = XICS_COMMON_GET_CLASS(icp);
140     Error *error = NULL;
141     int64_t value;
142 
143     visit_type_int(v, &value, name, &error);
144     if (error) {
145         error_propagate(errp, error);
146         return;
147     }
148     if (icp->nr_servers) {
149         error_setg(errp, "Number of servers is already set to %u",
150                    icp->nr_servers);
151         return;
152     }
153 
154     assert(info->set_nr_servers);
155     info->set_nr_servers(icp, value, errp);
156 }
157 
158 static void xics_common_initfn(Object *obj)
159 {
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);
166 }
167 
168 static void xics_common_class_init(ObjectClass *oc, void *data)
169 {
170     DeviceClass *dc = DEVICE_CLASS(oc);
171 
172     dc->reset = xics_common_reset;
173 }
174 
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,
182 };
183 
184 /*
185  * ICP: Presentation layer
186  */
187 
188 #define XISR_MASK  0x00ffffff
189 #define CPPR_MASK  0xff000000
190 
191 #define XISR(ss)   (((ss)->xirr) & XISR_MASK)
192 #define CPPR(ss)   (((ss)->xirr) >> 24)
193 
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);
197 
198 static void icp_check_ipi(XICSState *icp, int server)
199 {
200     ICPState *ss = icp->ss + server;
201 
202     if (XISR(ss) && (ss->pending_priority <= ss->mfrr)) {
203         return;
204     }
205 
206     trace_xics_icp_check_ipi(server, ss->mfrr);
207 
208     if (XISR(ss)) {
209         ics_reject(icp->ics, XISR(ss));
210     }
211 
212     ss->xirr = (ss->xirr & ~XISR_MASK) | XICS_IPI;
213     ss->pending_priority = ss->mfrr;
214     qemu_irq_raise(ss->output);
215 }
216 
217 static void icp_resend(XICSState *icp, int server)
218 {
219     ICPState *ss = icp->ss + server;
220 
221     if (ss->mfrr < CPPR(ss)) {
222         icp_check_ipi(icp, server);
223     }
224     ics_resend(icp->ics);
225 }
226 
227 static void icp_set_cppr(XICSState *icp, int server, uint8_t cppr)
228 {
229     ICPState *ss = icp->ss + server;
230     uint8_t old_cppr;
231     uint32_t old_xisr;
232 
233     old_cppr = CPPR(ss);
234     ss->xirr = (ss->xirr & ~CPPR_MASK) | (cppr << 24);
235 
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);
243         }
244     } else {
245         if (!XISR(ss)) {
246             icp_resend(icp, server);
247         }
248     }
249 }
250 
251 static void icp_set_mfrr(XICSState *icp, int server, uint8_t mfrr)
252 {
253     ICPState *ss = icp->ss + server;
254 
255     ss->mfrr = mfrr;
256     if (mfrr < CPPR(ss)) {
257         icp_check_ipi(icp, server);
258     }
259 }
260 
261 static uint32_t icp_accept(ICPState *ss)
262 {
263     uint32_t xirr = ss->xirr;
264 
265     qemu_irq_lower(ss->output);
266     ss->xirr = ss->pending_priority << 24;
267     ss->pending_priority = 0xff;
268 
269     trace_xics_icp_accept(xirr, ss->xirr);
270 
271     return xirr;
272 }
273 
274 static void icp_eoi(XICSState *icp, int server, uint32_t xirr)
275 {
276     ICPState *ss = icp->ss + server;
277 
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);
284     }
285 }
286 
287 static void icp_irq(XICSState *icp, int server, int nr, uint8_t priority)
288 {
289     ICPState *ss = icp->ss + server;
290 
291     trace_xics_icp_irq(server, nr, priority);
292 
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));
299         }
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);
304     }
305 }
306 
307 static void icp_dispatch_pre_save(void *opaque)
308 {
309     ICPState *ss = opaque;
310     ICPStateClass *info = ICP_GET_CLASS(ss);
311 
312     if (info->pre_save) {
313         info->pre_save(ss);
314     }
315 }
316 
317 static int icp_dispatch_post_load(void *opaque, int version_id)
318 {
319     ICPState *ss = opaque;
320     ICPStateClass *info = ICP_GET_CLASS(ss);
321 
322     if (info->post_load) {
323         return info->post_load(ss, version_id);
324     }
325 
326     return 0;
327 }
328 
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()
341     },
342 };
343 
344 static void icp_reset(DeviceState *dev)
345 {
346     ICPState *icp = ICP(dev);
347 
348     icp->xirr = 0;
349     icp->pending_priority = 0xff;
350     icp->mfrr = 0xff;
351 
352     /* Make all outputs are deasserted */
353     qemu_set_irq(icp->output, 0);
354 }
355 
356 static void icp_class_init(ObjectClass *klass, void *data)
357 {
358     DeviceClass *dc = DEVICE_CLASS(klass);
359 
360     dc->reset = icp_reset;
361     dc->vmsd = &vmstate_icp_server;
362 }
363 
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),
370 };
371 
372 /*
373  * ICS: Source layer
374  */
375 static int ics_valid_irq(ICSState *ics, uint32_t nr)
376 {
377     return (nr >= ics->offset)
378         && (nr < (ics->offset + ics->nr_irqs));
379 }
380 
381 static void resend_msi(ICSState *ics, int srcno)
382 {
383     ICSIRQState *irq = ics->irqs + srcno;
384 
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);
391         }
392     }
393 }
394 
395 static void resend_lsi(ICSState *ics, int srcno)
396 {
397     ICSIRQState *irq = ics->irqs + srcno;
398 
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);
404     }
405 }
406 
407 static void set_irq_msi(ICSState *ics, int srcno, int val)
408 {
409     ICSIRQState *irq = ics->irqs + srcno;
410 
411     trace_xics_set_irq_msi(srcno, srcno + ics->offset);
412 
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);
419         }
420     }
421 }
422 
423 static void set_irq_lsi(ICSState *ics, int srcno, int val)
424 {
425     ICSIRQState *irq = ics->irqs + srcno;
426 
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;
432     }
433     resend_lsi(ics, srcno);
434 }
435 
436 static void ics_set_irq(void *opaque, int srcno, int val)
437 {
438     ICSState *ics = (ICSState *)opaque;
439 
440     if (ics->islsi[srcno]) {
441         set_irq_lsi(ics, srcno, val);
442     } else {
443         set_irq_msi(ics, srcno, val);
444     }
445 }
446 
447 static void write_xive_msi(ICSState *ics, int srcno)
448 {
449     ICSIRQState *irq = ics->irqs + srcno;
450 
451     if (!(irq->status & XICS_STATUS_MASKED_PENDING)
452         || (irq->priority == 0xff)) {
453         return;
454     }
455 
456     irq->status &= ~XICS_STATUS_MASKED_PENDING;
457     icp_irq(ics->icp, irq->server, srcno + ics->offset, irq->priority);
458 }
459 
460 static void write_xive_lsi(ICSState *ics, int srcno)
461 {
462     resend_lsi(ics, srcno);
463 }
464 
465 static void ics_write_xive(ICSState *ics, int nr, int server,
466                            uint8_t priority, uint8_t saved_priority)
467 {
468     int srcno = nr - ics->offset;
469     ICSIRQState *irq = ics->irqs + srcno;
470 
471     irq->server = server;
472     irq->priority = priority;
473     irq->saved_priority = saved_priority;
474 
475     trace_xics_ics_write_xive(nr, srcno, server, priority);
476 
477     if (ics->islsi[srcno]) {
478         write_xive_lsi(ics, srcno);
479     } else {
480         write_xive_msi(ics, srcno);
481     }
482 }
483 
484 static void ics_reject(ICSState *ics, int nr)
485 {
486     ICSIRQState *irq = ics->irqs + nr - ics->offset;
487 
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 */
491 }
492 
493 static void ics_resend(ICSState *ics)
494 {
495     int i;
496 
497     for (i = 0; i < ics->nr_irqs; i++) {
498         /* FIXME: filter by server#? */
499         if (ics->islsi[i]) {
500             resend_lsi(ics, i);
501         } else {
502             resend_msi(ics, i);
503         }
504     }
505 }
506 
507 static void ics_eoi(ICSState *ics, int nr)
508 {
509     int srcno = nr - ics->offset;
510     ICSIRQState *irq = ics->irqs + srcno;
511 
512     trace_xics_ics_eoi(nr);
513 
514     if (ics->islsi[srcno]) {
515         irq->status &= ~XICS_STATUS_SENT;
516     }
517 }
518 
519 static void ics_reset(DeviceState *dev)
520 {
521     ICSState *ics = ICS(dev);
522     int i;
523 
524     memset(ics->irqs, 0, sizeof(ICSIRQState) * ics->nr_irqs);
525     for (i = 0; i < ics->nr_irqs; i++) {
526         ics->irqs[i].priority = 0xff;
527         ics->irqs[i].saved_priority = 0xff;
528     }
529 }
530 
531 static int ics_post_load(ICSState *ics, int version_id)
532 {
533     int i;
534 
535     for (i = 0; i < ics->icp->nr_servers; i++) {
536         icp_resend(ics->icp, i);
537     }
538 
539     return 0;
540 }
541 
542 static void ics_dispatch_pre_save(void *opaque)
543 {
544     ICSState *ics = opaque;
545     ICSStateClass *info = ICS_GET_CLASS(ics);
546 
547     if (info->pre_save) {
548         info->pre_save(ics);
549     }
550 }
551 
552 static int ics_dispatch_post_load(void *opaque, int version_id)
553 {
554     ICSState *ics = opaque;
555     ICSStateClass *info = ICS_GET_CLASS(ics);
556 
557     if (info->post_load) {
558         return info->post_load(ics, version_id);
559     }
560 
561     return 0;
562 }
563 
564 static const VMStateDescription vmstate_ics_irq = {
565     .name = "ics/irq",
566     .version_id = 1,
567     .minimum_version_id = 1,
568     .fields = (VMStateField[]) {
569         VMSTATE_UINT32(server, ICSIRQState),
570         VMSTATE_UINT8(priority, ICSIRQState),
571         VMSTATE_UINT8(saved_priority, ICSIRQState),
572         VMSTATE_UINT8(status, ICSIRQState),
573         VMSTATE_END_OF_LIST()
574     },
575 };
576 
577 static const VMStateDescription vmstate_ics = {
578     .name = "ics",
579     .version_id = 1,
580     .minimum_version_id = 1,
581     .pre_save = ics_dispatch_pre_save,
582     .post_load = ics_dispatch_post_load,
583     .fields = (VMStateField[]) {
584         /* Sanity check */
585         VMSTATE_UINT32_EQUAL(nr_irqs, ICSState),
586 
587         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(irqs, ICSState, nr_irqs,
588                                              vmstate_ics_irq, ICSIRQState),
589         VMSTATE_END_OF_LIST()
590     },
591 };
592 
593 static void ics_initfn(Object *obj)
594 {
595     ICSState *ics = ICS(obj);
596 
597     ics->offset = XICS_IRQ_BASE;
598 }
599 
600 static void ics_realize(DeviceState *dev, Error **errp)
601 {
602     ICSState *ics = ICS(dev);
603 
604     if (!ics->nr_irqs) {
605         error_setg(errp, "Number of interrupts needs to be greater 0");
606         return;
607     }
608     ics->irqs = g_malloc0(ics->nr_irqs * sizeof(ICSIRQState));
609     ics->islsi = g_malloc0(ics->nr_irqs * sizeof(bool));
610     ics->qirqs = qemu_allocate_irqs(ics_set_irq, ics, ics->nr_irqs);
611 }
612 
613 static void ics_class_init(ObjectClass *klass, void *data)
614 {
615     DeviceClass *dc = DEVICE_CLASS(klass);
616     ICSStateClass *isc = ICS_CLASS(klass);
617 
618     dc->realize = ics_realize;
619     dc->vmsd = &vmstate_ics;
620     dc->reset = ics_reset;
621     isc->post_load = ics_post_load;
622 }
623 
624 static const TypeInfo ics_info = {
625     .name = TYPE_ICS,
626     .parent = TYPE_DEVICE,
627     .instance_size = sizeof(ICSState),
628     .class_init = ics_class_init,
629     .class_size = sizeof(ICSStateClass),
630     .instance_init = ics_initfn,
631 };
632 
633 /*
634  * Exported functions
635  */
636 
637 qemu_irq xics_get_qirq(XICSState *icp, int irq)
638 {
639     if (!ics_valid_irq(icp->ics, irq)) {
640         return NULL;
641     }
642 
643     return icp->ics->qirqs[irq - icp->ics->offset];
644 }
645 
646 void xics_set_irq_type(XICSState *icp, int irq, bool lsi)
647 {
648     assert(ics_valid_irq(icp->ics, irq));
649 
650     icp->ics->islsi[irq - icp->ics->offset] = lsi;
651 }
652 
653 /*
654  * Guest interfaces
655  */
656 
657 static target_ulong h_cppr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
658                            target_ulong opcode, target_ulong *args)
659 {
660     CPUState *cs = CPU(cpu);
661     target_ulong cppr = args[0];
662 
663     icp_set_cppr(spapr->icp, cs->cpu_index, cppr);
664     return H_SUCCESS;
665 }
666 
667 static target_ulong h_ipi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
668                           target_ulong opcode, target_ulong *args)
669 {
670     target_ulong server = get_cpu_index_by_dt_id(args[0]);
671     target_ulong mfrr = args[1];
672 
673     if (server >= spapr->icp->nr_servers) {
674         return H_PARAMETER;
675     }
676 
677     icp_set_mfrr(spapr->icp, server, mfrr);
678     return H_SUCCESS;
679 }
680 
681 static target_ulong h_xirr(PowerPCCPU *cpu, sPAPREnvironment *spapr,
682                            target_ulong opcode, target_ulong *args)
683 {
684     CPUState *cs = CPU(cpu);
685     uint32_t xirr = icp_accept(spapr->icp->ss + cs->cpu_index);
686 
687     args[0] = xirr;
688     return H_SUCCESS;
689 }
690 
691 static target_ulong h_xirr_x(PowerPCCPU *cpu, sPAPREnvironment *spapr,
692                              target_ulong opcode, target_ulong *args)
693 {
694     CPUState *cs = CPU(cpu);
695     ICPState *ss = &spapr->icp->ss[cs->cpu_index];
696     uint32_t xirr = icp_accept(ss);
697 
698     args[0] = xirr;
699     args[1] = cpu_get_real_ticks();
700     return H_SUCCESS;
701 }
702 
703 static target_ulong h_eoi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
704                           target_ulong opcode, target_ulong *args)
705 {
706     CPUState *cs = CPU(cpu);
707     target_ulong xirr = args[0];
708 
709     icp_eoi(spapr->icp, cs->cpu_index, xirr);
710     return H_SUCCESS;
711 }
712 
713 static target_ulong h_ipoll(PowerPCCPU *cpu, sPAPREnvironment *spapr,
714                             target_ulong opcode, target_ulong *args)
715 {
716     CPUState *cs = CPU(cpu);
717     ICPState *ss = &spapr->icp->ss[cs->cpu_index];
718 
719     args[0] = ss->xirr;
720     args[1] = ss->mfrr;
721 
722     return H_SUCCESS;
723 }
724 
725 static void rtas_set_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
726                           uint32_t token,
727                           uint32_t nargs, target_ulong args,
728                           uint32_t nret, target_ulong rets)
729 {
730     ICSState *ics = spapr->icp->ics;
731     uint32_t nr, server, priority;
732 
733     if ((nargs != 3) || (nret != 1)) {
734         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
735         return;
736     }
737 
738     nr = rtas_ld(args, 0);
739     server = get_cpu_index_by_dt_id(rtas_ld(args, 1));
740     priority = rtas_ld(args, 2);
741 
742     if (!ics_valid_irq(ics, nr) || (server >= ics->icp->nr_servers)
743         || (priority > 0xff)) {
744         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
745         return;
746     }
747 
748     ics_write_xive(ics, nr, server, priority, priority);
749 
750     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
751 }
752 
753 static void rtas_get_xive(PowerPCCPU *cpu, sPAPREnvironment *spapr,
754                           uint32_t token,
755                           uint32_t nargs, target_ulong args,
756                           uint32_t nret, target_ulong rets)
757 {
758     ICSState *ics = spapr->icp->ics;
759     uint32_t nr;
760 
761     if ((nargs != 1) || (nret != 3)) {
762         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
763         return;
764     }
765 
766     nr = rtas_ld(args, 0);
767 
768     if (!ics_valid_irq(ics, nr)) {
769         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
770         return;
771     }
772 
773     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
774     rtas_st(rets, 1, ics->irqs[nr - ics->offset].server);
775     rtas_st(rets, 2, ics->irqs[nr - ics->offset].priority);
776 }
777 
778 static void rtas_int_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
779                          uint32_t token,
780                          uint32_t nargs, target_ulong args,
781                          uint32_t nret, target_ulong rets)
782 {
783     ICSState *ics = spapr->icp->ics;
784     uint32_t nr;
785 
786     if ((nargs != 1) || (nret != 1)) {
787         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
788         return;
789     }
790 
791     nr = rtas_ld(args, 0);
792 
793     if (!ics_valid_irq(ics, nr)) {
794         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
795         return;
796     }
797 
798     ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server, 0xff,
799                    ics->irqs[nr - ics->offset].priority);
800 
801     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
802 }
803 
804 static void rtas_int_on(PowerPCCPU *cpu, sPAPREnvironment *spapr,
805                         uint32_t token,
806                         uint32_t nargs, target_ulong args,
807                         uint32_t nret, target_ulong rets)
808 {
809     ICSState *ics = spapr->icp->ics;
810     uint32_t nr;
811 
812     if ((nargs != 1) || (nret != 1)) {
813         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
814         return;
815     }
816 
817     nr = rtas_ld(args, 0);
818 
819     if (!ics_valid_irq(ics, nr)) {
820         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
821         return;
822     }
823 
824     ics_write_xive(ics, nr, ics->irqs[nr - ics->offset].server,
825                    ics->irqs[nr - ics->offset].saved_priority,
826                    ics->irqs[nr - ics->offset].saved_priority);
827 
828     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
829 }
830 
831 /*
832  * XICS
833  */
834 
835 static void xics_set_nr_irqs(XICSState *icp, uint32_t nr_irqs, Error **errp)
836 {
837     icp->nr_irqs = icp->ics->nr_irqs = nr_irqs;
838 }
839 
840 static void xics_set_nr_servers(XICSState *icp, uint32_t nr_servers,
841                                 Error **errp)
842 {
843     int i;
844 
845     icp->nr_servers = nr_servers;
846 
847     icp->ss = g_malloc0(icp->nr_servers*sizeof(ICPState));
848     for (i = 0; i < icp->nr_servers; i++) {
849         char buffer[32];
850         object_initialize(&icp->ss[i], sizeof(icp->ss[i]), TYPE_ICP);
851         snprintf(buffer, sizeof(buffer), "icp[%d]", i);
852         object_property_add_child(OBJECT(icp), buffer, OBJECT(&icp->ss[i]),
853                                   errp);
854     }
855 }
856 
857 static void xics_realize(DeviceState *dev, Error **errp)
858 {
859     XICSState *icp = XICS(dev);
860     Error *error = NULL;
861     int i;
862 
863     if (!icp->nr_servers) {
864         error_setg(errp, "Number of servers needs to be greater 0");
865         return;
866     }
867 
868     /* Registration of global state belongs into realize */
869     spapr_rtas_register("ibm,set-xive", rtas_set_xive);
870     spapr_rtas_register("ibm,get-xive", rtas_get_xive);
871     spapr_rtas_register("ibm,int-off", rtas_int_off);
872     spapr_rtas_register("ibm,int-on", rtas_int_on);
873 
874     spapr_register_hypercall(H_CPPR, h_cppr);
875     spapr_register_hypercall(H_IPI, h_ipi);
876     spapr_register_hypercall(H_XIRR, h_xirr);
877     spapr_register_hypercall(H_XIRR_X, h_xirr_x);
878     spapr_register_hypercall(H_EOI, h_eoi);
879     spapr_register_hypercall(H_IPOLL, h_ipoll);
880 
881     object_property_set_bool(OBJECT(icp->ics), true, "realized", &error);
882     if (error) {
883         error_propagate(errp, error);
884         return;
885     }
886 
887     for (i = 0; i < icp->nr_servers; i++) {
888         object_property_set_bool(OBJECT(&icp->ss[i]), true, "realized", &error);
889         if (error) {
890             error_propagate(errp, error);
891             return;
892         }
893     }
894 }
895 
896 static void xics_initfn(Object *obj)
897 {
898     XICSState *xics = XICS(obj);
899 
900     xics->ics = ICS(object_new(TYPE_ICS));
901     object_property_add_child(obj, "ics", OBJECT(xics->ics), NULL);
902     xics->ics->icp = xics;
903 }
904 
905 static void xics_class_init(ObjectClass *oc, void *data)
906 {
907     DeviceClass *dc = DEVICE_CLASS(oc);
908     XICSStateClass *xsc = XICS_CLASS(oc);
909 
910     dc->realize = xics_realize;
911     xsc->set_nr_irqs = xics_set_nr_irqs;
912     xsc->set_nr_servers = xics_set_nr_servers;
913 }
914 
915 static const TypeInfo xics_info = {
916     .name          = TYPE_XICS,
917     .parent        = TYPE_XICS_COMMON,
918     .instance_size = sizeof(XICSState),
919     .class_size = sizeof(XICSStateClass),
920     .class_init    = xics_class_init,
921     .instance_init = xics_initfn,
922 };
923 
924 static void xics_register_types(void)
925 {
926     type_register_static(&xics_common_info);
927     type_register_static(&xics_info);
928     type_register_static(&ics_info);
929     type_register_static(&icp_info);
930 }
931 
932 type_init(xics_register_types)
933