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
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
);
41 return cpu
->parent_obj
.cpu_index
;
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
];
65 case PPC_FLAGS_INPUT_970
:
66 ss
->output
= env
->irq_inputs
[PPC970_INPUT_INT
];
70 error_report("XICS interrupt controller does not support this CPU "
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
);
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
);
108 visit_type_int(v
, &value
, name
, &error
);
110 error_propagate(errp
, error
);
114 error_setg(errp
, "Number of interrupts is already set to %u",
119 assert(info
->set_nr_irqs
);
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
,
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
,
138 XICSState
*icp
= XICS_COMMON(obj
);
139 XICSStateClass
*info
= XICS_COMMON_GET_CLASS(icp
);
143 visit_type_int(v
, &value
, name
, &error
);
145 error_propagate(errp
, error
);
148 if (icp
->nr_servers
) {
149 error_setg(errp
, "Number of servers is already set to %u",
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
,
163 object_property_add(obj
, "nr_servers", "int",
164 xics_prop_get_nr_servers
, xics_prop_set_nr_servers
,
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
)) {
206 trace_xics_icp_check_ipi(server
, ss
->mfrr
);
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
;
234 ss
->xirr
= (ss
->xirr
& ~CPPR_MASK
) | (cppr
<< 24);
236 if (cppr
< old_cppr
) {
237 if (XISR(ss
) && (cppr
<= ss
->pending_priority
)) {
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
);
246 icp_resend(icp
, server
);
251 static void icp_set_mfrr(XICSState
*icp
, int server
, uint8_t mfrr
)
253 ICPState
*ss
= icp
->ss
+ server
;
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
);
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
);
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
);
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
) {
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
);
329 static const VMStateDescription vmstate_icp_server
= {
330 .name
= "icp/server",
332 .minimum_version_id
= 1,
333 .pre_save
= icp_dispatch_pre_save
,
334 .post_load
= icp_dispatch_post_load
,
335 .fields
= (VMStateField
[]) {
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
);
349 icp
->pending_priority
= 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
= {
366 .parent
= TYPE_DEVICE
,
367 .instance_size
= sizeof(ICPState
),
368 .class_init
= icp_class_init
,
369 .class_size
= sizeof(ICPStateClass
),
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
,
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
);
414 if (irq
->priority
== 0xff) {
415 irq
->status
|= XICS_STATUS_MASKED_PENDING
;
416 trace_xics_masked_pending();
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
);
429 irq
->status
|= XICS_STATUS_ASSERTED
;
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
);
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)) {
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
);
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
)
497 for (i
= 0; i
< ics
->nr_irqs
; i
++) {
498 /* FIXME: filter by server#? */
499 if (ics
->irqs
[i
].flags
& XICS_FLAGS_IRQ_LSI
) {
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
);
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
)
542 for (i
= 0; i
< ics
->icp
->nr_servers
; i
++) {
543 icp_resend(ics
->icp
, i
);
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
) {
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
);
571 static const VMStateDescription vmstate_ics_irq
= {
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
= {
588 .minimum_version_id
= 1,
589 .pre_save
= ics_dispatch_pre_save
,
590 .post_load
= ics_dispatch_post_load
,
591 .fields
= (VMStateField
[]) {
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
);
613 error_setg(errp
, "Number of interrupts needs to be greater 0");
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
= {
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
,
643 static int xics_find_source(XICSState
*icp
, int irq
)
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
)) {
659 qemu_irq
xics_get_qirq(XICSState
*icp
, int irq
)
661 int src
= xics_find_source(icp
, irq
);
664 ICSState
*ics
= &icp
->ics
[src
];
665 return ics
->qirqs
[irq
- ics
->offset
];
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
);
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
)
697 for (first
= 0; first
< ics
->nr_irqs
; first
+= alignnum
) {
698 if (num
> (ics
->nr_irqs
- first
)) {
701 for (i
= first
; i
< first
+ num
; ++i
) {
702 if (!ICS_IRQ_FREE(ics
, i
)) {
706 if (i
== (first
+ num
)) {
714 int xics_alloc(XICSState
*icp
, int src
, int irq_hint
, bool lsi
)
716 ICSState
*ics
= &icp
->ics
[src
];
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
);
727 irq
= ics_find_free_block(ics
, 1, 1);
729 trace_xics_alloc_failed_no_left(src
);
735 ics_set_irq_type(ics
, irq
- ics
->offset
, lsi
);
736 trace_xics_alloc(src
, 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
)
748 ICSState
*ics
= &icp
->ics
[src
];
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.
759 assert((num
== 1) || (num
== 2) || (num
== 4) ||
760 (num
== 8) || (num
== 16) || (num
== 32));
761 first
= ics_find_free_block(ics
, num
, num
);
763 first
= ics_find_free_block(ics
, num
, 1);
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
);
778 static void ics_free(ICSState
*ics
, int srcno
, int num
)
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
);
795 ICSState
*ics
= &icp
->ics
[src
];
797 /* FIXME: implement multiple sources */
800 trace_xics_ics_free(ics
- icp
->ics
, irq
, num
);
801 ics_free(ics
, irq
- ics
->offset
, num
);
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
);
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
) {
829 icp_set_mfrr(spapr
->icp
, server
, mfrr
);
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
);
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
);
851 args
[1] = cpu_get_real_ticks();
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
);
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
];
877 static void rtas_set_xive(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
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
);
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
);
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
,
907 uint32_t nargs
, target_ulong args
,
908 uint32_t nret
, target_ulong rets
)
910 ICSState
*ics
= spapr
->icp
->ics
;
913 if ((nargs
!= 1) || (nret
!= 3)) {
914 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
918 nr
= rtas_ld(args
, 0);
920 if (!ics_valid_irq(ics
, nr
)) {
921 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
,
932 uint32_t nargs
, target_ulong args
,
933 uint32_t nret
, target_ulong rets
)
935 ICSState
*ics
= spapr
->icp
->ics
;
938 if ((nargs
!= 1) || (nret
!= 1)) {
939 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
943 nr
= rtas_ld(args
, 0);
945 if (!ics_valid_irq(ics
, nr
)) {
946 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
,
958 uint32_t nargs
, target_ulong args
,
959 uint32_t nret
, target_ulong rets
)
961 ICSState
*ics
= spapr
->icp
->ics
;
964 if ((nargs
!= 1) || (nret
!= 1)) {
965 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
969 nr
= rtas_ld(args
, 0);
971 if (!ics_valid_irq(ics
, nr
)) {
972 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
);
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
,
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
++) {
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
]),
1009 static void xics_realize(DeviceState
*dev
, Error
**errp
)
1011 XICSState
*icp
= XICS(dev
);
1012 Error
*error
= NULL
;
1015 if (!icp
->nr_servers
) {
1016 error_setg(errp
, "Number of servers needs to be greater 0");
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
);
1035 error_propagate(errp
, error
);
1039 for (i
= 0; i
< icp
->nr_servers
; i
++) {
1040 object_property_set_bool(OBJECT(&icp
->ss
[i
]), true, "realized", &error
);
1042 error_propagate(errp
, error
);
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
= {
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
)