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
28 #include "qemu/osdep.h"
32 #include "qemu/timer.h"
33 #include "hw/ppc/spapr.h"
34 #include "hw/ppc/xics.h"
35 #include "hw/ppc/fdt.h"
36 #include "qapi/visitor.h"
37 #include "qapi/error.h"
43 static target_ulong
h_cppr(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
44 target_ulong opcode
, target_ulong
*args
)
46 CPUState
*cs
= CPU(cpu
);
47 ICPState
*icp
= &spapr
->xics
->ss
[cs
->cpu_index
];
48 target_ulong cppr
= args
[0];
50 icp_set_cppr(icp
, cppr
);
54 static target_ulong
h_ipi(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
55 target_ulong opcode
, target_ulong
*args
)
57 target_ulong server
= xics_get_cpu_index_by_dt_id(args
[0]);
58 target_ulong mfrr
= args
[1];
60 if (server
>= spapr
->xics
->nr_servers
) {
64 icp_set_mfrr(spapr
->xics
->ss
+ server
, mfrr
);
68 static target_ulong
h_xirr(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
69 target_ulong opcode
, target_ulong
*args
)
71 CPUState
*cs
= CPU(cpu
);
72 ICPState
*icp
= &spapr
->xics
->ss
[cs
->cpu_index
];
73 uint32_t xirr
= icp_accept(icp
);
79 static target_ulong
h_xirr_x(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
80 target_ulong opcode
, target_ulong
*args
)
82 CPUState
*cs
= CPU(cpu
);
83 ICPState
*icp
= &spapr
->xics
->ss
[cs
->cpu_index
];
84 uint32_t xirr
= icp_accept(icp
);
87 args
[1] = cpu_get_host_ticks();
91 static target_ulong
h_eoi(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
92 target_ulong opcode
, target_ulong
*args
)
94 CPUState
*cs
= CPU(cpu
);
95 ICPState
*icp
= &spapr
->xics
->ss
[cs
->cpu_index
];
96 target_ulong xirr
= args
[0];
102 static target_ulong
h_ipoll(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
103 target_ulong opcode
, target_ulong
*args
)
105 CPUState
*cs
= CPU(cpu
);
106 ICPState
*icp
= &spapr
->xics
->ss
[cs
->cpu_index
];
108 uint32_t xirr
= icp_ipoll(icp
, &mfrr
);
116 static void rtas_set_xive(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
118 uint32_t nargs
, target_ulong args
,
119 uint32_t nret
, target_ulong rets
)
121 ICSState
*ics
= QLIST_FIRST(&spapr
->xics
->ics
);
122 uint32_t nr
, srcno
, server
, priority
;
124 if ((nargs
!= 3) || (nret
!= 1)) {
125 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
129 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
133 nr
= rtas_ld(args
, 0);
134 server
= xics_get_cpu_index_by_dt_id(rtas_ld(args
, 1));
135 priority
= rtas_ld(args
, 2);
137 if (!ics_valid_irq(ics
, nr
) || (server
>= ics
->xics
->nr_servers
)
138 || (priority
> 0xff)) {
139 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
143 srcno
= nr
- ics
->offset
;
144 ics_simple_write_xive(ics
, srcno
, server
, priority
, priority
);
146 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
149 static void rtas_get_xive(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
151 uint32_t nargs
, target_ulong args
,
152 uint32_t nret
, target_ulong rets
)
154 ICSState
*ics
= QLIST_FIRST(&spapr
->xics
->ics
);
157 if ((nargs
!= 1) || (nret
!= 3)) {
158 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
162 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
166 nr
= rtas_ld(args
, 0);
168 if (!ics_valid_irq(ics
, nr
)) {
169 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
173 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
174 srcno
= nr
- ics
->offset
;
175 rtas_st(rets
, 1, ics
->irqs
[srcno
].server
);
176 rtas_st(rets
, 2, ics
->irqs
[srcno
].priority
);
179 static void rtas_int_off(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
181 uint32_t nargs
, target_ulong args
,
182 uint32_t nret
, target_ulong rets
)
184 ICSState
*ics
= QLIST_FIRST(&spapr
->xics
->ics
);
187 if ((nargs
!= 1) || (nret
!= 1)) {
188 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
192 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
196 nr
= rtas_ld(args
, 0);
198 if (!ics_valid_irq(ics
, nr
)) {
199 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
203 srcno
= nr
- ics
->offset
;
204 ics_simple_write_xive(ics
, srcno
, ics
->irqs
[srcno
].server
, 0xff,
205 ics
->irqs
[srcno
].priority
);
207 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
210 static void rtas_int_on(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
212 uint32_t nargs
, target_ulong args
,
213 uint32_t nret
, target_ulong rets
)
215 ICSState
*ics
= QLIST_FIRST(&spapr
->xics
->ics
);
218 if ((nargs
!= 1) || (nret
!= 1)) {
219 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
223 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
227 nr
= rtas_ld(args
, 0);
229 if (!ics_valid_irq(ics
, nr
)) {
230 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
234 srcno
= nr
- ics
->offset
;
235 ics_simple_write_xive(ics
, srcno
, ics
->irqs
[srcno
].server
,
236 ics
->irqs
[srcno
].saved_priority
,
237 ics
->irqs
[srcno
].saved_priority
);
239 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
242 static void xics_spapr_set_nr_irqs(XICSState
*xics
, uint32_t nr_irqs
,
245 ICSState
*ics
= QLIST_FIRST(&xics
->ics
);
247 /* This needs to be deprecated ... */
248 xics
->nr_irqs
= nr_irqs
;
250 ics
->nr_irqs
= nr_irqs
;
254 static void xics_spapr_set_nr_servers(XICSState
*xics
, uint32_t nr_servers
,
257 xics_set_nr_servers(xics
, nr_servers
, TYPE_ICP
, errp
);
260 static void xics_spapr_realize(DeviceState
*dev
, Error
**errp
)
262 XICSState
*xics
= XICS_SPAPR(dev
);
267 if (!xics
->nr_servers
) {
268 error_setg(errp
, "Number of servers needs to be greater 0");
272 /* Registration of global state belongs into realize */
273 spapr_rtas_register(RTAS_IBM_SET_XIVE
, "ibm,set-xive", rtas_set_xive
);
274 spapr_rtas_register(RTAS_IBM_GET_XIVE
, "ibm,get-xive", rtas_get_xive
);
275 spapr_rtas_register(RTAS_IBM_INT_OFF
, "ibm,int-off", rtas_int_off
);
276 spapr_rtas_register(RTAS_IBM_INT_ON
, "ibm,int-on", rtas_int_on
);
278 spapr_register_hypercall(H_CPPR
, h_cppr
);
279 spapr_register_hypercall(H_IPI
, h_ipi
);
280 spapr_register_hypercall(H_XIRR
, h_xirr
);
281 spapr_register_hypercall(H_XIRR_X
, h_xirr_x
);
282 spapr_register_hypercall(H_EOI
, h_eoi
);
283 spapr_register_hypercall(H_IPOLL
, h_ipoll
);
285 QLIST_FOREACH(ics
, &xics
->ics
, list
) {
286 object_property_set_bool(OBJECT(ics
), true, "realized", &error
);
288 error_propagate(errp
, error
);
293 for (i
= 0; i
< xics
->nr_servers
; i
++) {
294 object_property_set_bool(OBJECT(&xics
->ss
[i
]), true, "realized",
297 error_propagate(errp
, error
);
303 static void xics_spapr_initfn(Object
*obj
)
305 XICSState
*xics
= XICS_SPAPR(obj
);
308 ics
= ICS_SIMPLE(object_new(TYPE_ICS_SIMPLE
));
309 object_property_add_child(obj
, "ics", OBJECT(ics
), NULL
);
311 QLIST_INSERT_HEAD(&xics
->ics
, ics
, list
);
314 static void xics_spapr_class_init(ObjectClass
*oc
, void *data
)
316 DeviceClass
*dc
= DEVICE_CLASS(oc
);
317 XICSStateClass
*xsc
= XICS_SPAPR_CLASS(oc
);
319 dc
->realize
= xics_spapr_realize
;
320 xsc
->set_nr_irqs
= xics_spapr_set_nr_irqs
;
321 xsc
->set_nr_servers
= xics_spapr_set_nr_servers
;
324 static const TypeInfo xics_spapr_info
= {
325 .name
= TYPE_XICS_SPAPR
,
326 .parent
= TYPE_XICS_COMMON
,
327 .instance_size
= sizeof(XICSState
),
328 .class_size
= sizeof(XICSStateClass
),
329 .class_init
= xics_spapr_class_init
,
330 .instance_init
= xics_spapr_initfn
,
333 #define ICS_IRQ_FREE(ics, srcno) \
334 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
336 static int ics_find_free_block(ICSState
*ics
, int num
, int alignnum
)
340 for (first
= 0; first
< ics
->nr_irqs
; first
+= alignnum
) {
341 if (num
> (ics
->nr_irqs
- first
)) {
344 for (i
= first
; i
< first
+ num
; ++i
) {
345 if (!ICS_IRQ_FREE(ics
, i
)) {
349 if (i
== (first
+ num
)) {
357 int xics_spapr_alloc(XICSState
*xics
, int irq_hint
, bool lsi
, Error
**errp
)
359 ICSState
*ics
= QLIST_FIRST(&xics
->ics
);
366 if (!ICS_IRQ_FREE(ics
, irq_hint
- ics
->offset
)) {
367 error_setg(errp
, "can't allocate IRQ %d: already in use", irq_hint
);
372 irq
= ics_find_free_block(ics
, 1, 1);
374 error_setg(errp
, "can't allocate IRQ: no IRQ left");
380 ics_set_irq_type(ics
, irq
- ics
->offset
, lsi
);
381 trace_xics_alloc(irq
);
387 * Allocate block of consecutive IRQs, and return the number of the first IRQ in
388 * the block. If align==true, aligns the first IRQ number to num.
390 int xics_spapr_alloc_block(XICSState
*xics
, int num
, bool lsi
, bool align
,
393 ICSState
*ics
= QLIST_FIRST(&xics
->ics
);
401 * MSIMesage::data is used for storing VIRQ so
402 * it has to be aligned to num to support multiple
403 * MSI vectors. MSI-X is not affected by this.
404 * The hint is used for the first IRQ, the rest should
405 * be allocated continuously.
408 assert((num
== 1) || (num
== 2) || (num
== 4) ||
409 (num
== 8) || (num
== 16) || (num
== 32));
410 first
= ics_find_free_block(ics
, num
, num
);
412 first
= ics_find_free_block(ics
, num
, 1);
415 error_setg(errp
, "can't find a free %d-IRQ block", num
);
420 for (i
= first
; i
< first
+ num
; ++i
) {
421 ics_set_irq_type(ics
, i
, lsi
);
424 first
+= ics
->offset
;
426 trace_xics_alloc_block(first
, num
, lsi
, align
);
431 static void ics_free(ICSState
*ics
, int srcno
, int num
)
435 for (i
= srcno
; i
< srcno
+ num
; ++i
) {
436 if (ICS_IRQ_FREE(ics
, i
)) {
437 trace_xics_ics_free_warn(0, i
+ ics
->offset
);
439 memset(&ics
->irqs
[i
], 0, sizeof(ICSIRQState
));
443 void xics_spapr_free(XICSState
*xics
, int irq
, int num
)
445 ICSState
*ics
= xics_find_source(xics
, irq
);
448 trace_xics_ics_free(0, irq
, num
);
449 ics_free(ics
, irq
- ics
->offset
, num
);
453 void spapr_dt_xics(XICSState
*xics
, void *fdt
, uint32_t phandle
)
455 uint32_t interrupt_server_ranges_prop
[] = {
456 0, cpu_to_be32(xics
->nr_servers
),
460 _FDT(node
= fdt_add_subnode(fdt
, 0, "interrupt-controller"));
462 _FDT(fdt_setprop_string(fdt
, node
, "device_type",
463 "PowerPC-External-Interrupt-Presentation"));
464 _FDT(fdt_setprop_string(fdt
, node
, "compatible", "IBM,ppc-xicp"));
465 _FDT(fdt_setprop(fdt
, node
, "interrupt-controller", NULL
, 0));
466 _FDT(fdt_setprop(fdt
, node
, "ibm,interrupt-server-ranges",
467 interrupt_server_ranges_prop
,
468 sizeof(interrupt_server_ranges_prop
)));
469 _FDT(fdt_setprop_cell(fdt
, node
, "#interrupt-cells", 2));
470 _FDT(fdt_setprop_cell(fdt
, node
, "linux,phandle", phandle
));
471 _FDT(fdt_setprop_cell(fdt
, node
, "phandle", phandle
));
474 static void xics_spapr_register_types(void)
476 type_register_static(&xics_spapr_info
);
479 type_init(xics_spapr_register_types
)