]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_irq.c
spapr/irq: add an 'nr_irq' parameter to initialize the backend.
[mirror_qemu.git] / hw / ppc / spapr_irq.c
CommitLineData
82cffa2e
CLG
1/*
2 * QEMU PowerPC sPAPR IRQ interface
3 *
4 * Copyright (c) 2018, IBM Corporation.
5 *
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
9
10#include "qemu/osdep.h"
11#include "qemu/log.h"
12#include "qemu/error-report.h"
13#include "qapi/error.h"
14#include "hw/ppc/spapr.h"
a28b9a5a 15#include "hw/ppc/spapr_cpu_core.h"
dcc345b6 16#include "hw/ppc/spapr_xive.h"
82cffa2e 17#include "hw/ppc/xics.h"
a51d5afc 18#include "hw/ppc/xics_spapr.h"
ef01ed9d
CLG
19#include "sysemu/kvm.h"
20
21#include "trace.h"
82cffa2e
CLG
22
23void spapr_irq_msi_init(sPAPRMachineState *spapr, uint32_t nr_msis)
24{
25 spapr->irq_map_nr = nr_msis;
26 spapr->irq_map = bitmap_new(spapr->irq_map_nr);
27}
28
29int spapr_irq_msi_alloc(sPAPRMachineState *spapr, uint32_t num, bool align,
30 Error **errp)
31{
32 int irq;
33
34 /*
35 * The 'align_mask' parameter of bitmap_find_next_zero_area()
36 * should be one less than a power of 2; 0 means no
37 * alignment. Adapt the 'align' value of the former allocator
38 * to fit the requirements of bitmap_find_next_zero_area()
39 */
40 align -= 1;
41
42 irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num,
43 align);
44 if (irq == spapr->irq_map_nr) {
45 error_setg(errp, "can't find a free %d-IRQ block", num);
46 return -1;
47 }
48
49 bitmap_set(spapr->irq_map, irq, num);
50
51 return irq + SPAPR_IRQ_MSI;
52}
53
54void spapr_irq_msi_free(sPAPRMachineState *spapr, int irq, uint32_t num)
55{
56 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
57}
58
59void spapr_irq_msi_reset(sPAPRMachineState *spapr)
60{
61 bitmap_clear(spapr->irq_map, 0, spapr->irq_map_nr);
62}
ef01ed9d
CLG
63
64
65/*
66 * XICS IRQ backend.
67 */
68
69static ICSState *spapr_ics_create(sPAPRMachineState *spapr,
70 const char *type_ics,
71 int nr_irqs, Error **errp)
72{
73 Error *local_err = NULL;
74 Object *obj;
75
76 obj = object_new(type_ics);
77 object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort);
78 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
79 &error_abort);
80 object_property_set_int(obj, nr_irqs, "nr-irqs", &local_err);
81 if (local_err) {
82 goto error;
83 }
84 object_property_set_bool(obj, true, "realized", &local_err);
85 if (local_err) {
86 goto error;
87 }
88
89 return ICS_BASE(obj);
90
91error:
92 error_propagate(errp, local_err);
93 return NULL;
94}
95
2e66cdb7
CLG
96static void spapr_irq_init_xics(sPAPRMachineState *spapr, int nr_irqs,
97 Error **errp)
ef01ed9d
CLG
98{
99 MachineState *machine = MACHINE(spapr);
ef01ed9d
CLG
100 Error *local_err = NULL;
101
ef01ed9d
CLG
102 if (kvm_enabled()) {
103 if (machine_kernel_irqchip_allowed(machine) &&
104 !xics_kvm_init(spapr, &local_err)) {
105 spapr->icp_type = TYPE_KVM_ICP;
106 spapr->ics = spapr_ics_create(spapr, TYPE_ICS_KVM, nr_irqs,
107 &local_err);
108 }
109 if (machine_kernel_irqchip_required(machine) && !spapr->ics) {
110 error_prepend(&local_err,
111 "kernel_irqchip requested but unavailable: ");
112 goto error;
113 }
114 error_free(local_err);
115 local_err = NULL;
116 }
117
118 if (!spapr->ics) {
119 xics_spapr_init(spapr);
120 spapr->icp_type = TYPE_ICP;
121 spapr->ics = spapr_ics_create(spapr, TYPE_ICS_SIMPLE, nr_irqs,
122 &local_err);
123 }
124
125error:
126 error_propagate(errp, local_err);
127}
128
129#define ICS_IRQ_FREE(ics, srcno) \
130 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
131
132static int spapr_irq_claim_xics(sPAPRMachineState *spapr, int irq, bool lsi,
133 Error **errp)
134{
135 ICSState *ics = spapr->ics;
136
137 assert(ics);
138
139 if (!ics_valid_irq(ics, irq)) {
140 error_setg(errp, "IRQ %d is invalid", irq);
141 return -1;
142 }
143
144 if (!ICS_IRQ_FREE(ics, irq - ics->offset)) {
145 error_setg(errp, "IRQ %d is not free", irq);
146 return -1;
147 }
148
149 ics_set_irq_type(ics, irq - ics->offset, lsi);
150 return 0;
151}
152
153static void spapr_irq_free_xics(sPAPRMachineState *spapr, int irq, int num)
154{
155 ICSState *ics = spapr->ics;
156 uint32_t srcno = irq - ics->offset;
157 int i;
158
159 if (ics_valid_irq(ics, irq)) {
160 trace_spapr_irq_free(0, irq, num);
161 for (i = srcno; i < srcno + num; ++i) {
162 if (ICS_IRQ_FREE(ics, i)) {
163 trace_spapr_irq_free_warn(0, i);
164 }
165 memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
166 }
167 }
168}
169
170static qemu_irq spapr_qirq_xics(sPAPRMachineState *spapr, int irq)
171{
172 ICSState *ics = spapr->ics;
173 uint32_t srcno = irq - ics->offset;
174
175 if (ics_valid_irq(ics, irq)) {
872ff3de 176 return spapr->qirqs[srcno];
ef01ed9d
CLG
177 }
178
179 return NULL;
180}
181
182static void spapr_irq_print_info_xics(sPAPRMachineState *spapr, Monitor *mon)
183{
184 CPUState *cs;
185
186 CPU_FOREACH(cs) {
187 PowerPCCPU *cpu = POWERPC_CPU(cs);
188
a28b9a5a 189 icp_pic_print_info(spapr_cpu_state(cpu)->icp, mon);
ef01ed9d
CLG
190 }
191
192 ics_pic_print_info(spapr->ics, mon);
193}
194
8fa1f4ef
CLG
195static void spapr_irq_cpu_intc_create_xics(sPAPRMachineState *spapr,
196 PowerPCCPU *cpu, Error **errp)
1a937ad7 197{
8fa1f4ef
CLG
198 Error *local_err = NULL;
199 Object *obj;
a28b9a5a 200 sPAPRCPUState *spapr_cpu = spapr_cpu_state(cpu);
8fa1f4ef
CLG
201
202 obj = icp_create(OBJECT(cpu), spapr->icp_type, XICS_FABRIC(spapr),
203 &local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 return;
207 }
208
a28b9a5a 209 spapr_cpu->icp = ICP(obj);
1a937ad7
CLG
210}
211
1c53b06c
CLG
212static int spapr_irq_post_load_xics(sPAPRMachineState *spapr, int version_id)
213{
214 if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
215 CPUState *cs;
216 CPU_FOREACH(cs) {
217 PowerPCCPU *cpu = POWERPC_CPU(cs);
a28b9a5a 218 icp_resend(spapr_cpu_state(cpu)->icp);
1c53b06c
CLG
219 }
220 }
221 return 0;
222}
223
872ff3de
CLG
224static void spapr_irq_set_irq_xics(void *opaque, int srcno, int val)
225{
226 sPAPRMachineState *spapr = opaque;
227 MachineState *machine = MACHINE(opaque);
228
229 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
230 ics_kvm_set_irq(spapr->ics, srcno, val);
231 } else {
232 ics_simple_set_irq(spapr->ics, srcno, val);
233 }
234}
235
13db0cd9
CLG
236static void spapr_irq_reset_xics(sPAPRMachineState *spapr, Error **errp)
237{
238 /* TODO: create the KVM XICS device */
239}
240
ae837402 241#define SPAPR_IRQ_XICS_NR_IRQS 0x1000
e39de895
CLG
242#define SPAPR_IRQ_XICS_NR_MSIS \
243 (XICS_IRQ_BASE + SPAPR_IRQ_XICS_NR_IRQS - SPAPR_IRQ_MSI)
244
ef01ed9d 245sPAPRIrq spapr_irq_xics = {
e39de895
CLG
246 .nr_irqs = SPAPR_IRQ_XICS_NR_IRQS,
247 .nr_msis = SPAPR_IRQ_XICS_NR_MSIS,
db592b5b 248 .ov5 = SPAPR_OV5_XIVE_LEGACY,
ef01ed9d
CLG
249
250 .init = spapr_irq_init_xics,
251 .claim = spapr_irq_claim_xics,
252 .free = spapr_irq_free_xics,
253 .qirq = spapr_qirq_xics,
254 .print_info = spapr_irq_print_info_xics,
6e21de4a 255 .dt_populate = spapr_dt_xics,
1a937ad7 256 .cpu_intc_create = spapr_irq_cpu_intc_create_xics,
1c53b06c 257 .post_load = spapr_irq_post_load_xics,
13db0cd9 258 .reset = spapr_irq_reset_xics,
872ff3de 259 .set_irq = spapr_irq_set_irq_xics,
ef01ed9d
CLG
260};
261
dcc345b6
CLG
262/*
263 * XIVE IRQ backend.
264 */
2e66cdb7
CLG
265static void spapr_irq_init_xive(sPAPRMachineState *spapr, int nr_irqs,
266 Error **errp)
dcc345b6
CLG
267{
268 MachineState *machine = MACHINE(spapr);
dcc345b6
CLG
269 uint32_t nr_servers = spapr_max_server_number(spapr);
270 DeviceState *dev;
271 int i;
272
273 /* KVM XIVE device not yet available */
274 if (kvm_enabled()) {
275 if (machine_kernel_irqchip_required(machine)) {
276 error_setg(errp, "kernel_irqchip requested. no KVM XIVE support");
277 return;
278 }
279 }
280
281 dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
2e66cdb7 282 qdev_prop_set_uint32(dev, "nr-irqs", nr_irqs);
dcc345b6
CLG
283 /*
284 * 8 XIVE END structures per CPU. One for each available priority
285 */
286 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
287 qdev_init_nofail(dev);
288
289 spapr->xive = SPAPR_XIVE(dev);
290
291 /* Enable the CPU IPIs */
292 for (i = 0; i < nr_servers; ++i) {
293 spapr_xive_irq_claim(spapr->xive, SPAPR_IRQ_IPI + i, false);
294 }
23bcd5eb
CLG
295
296 spapr_xive_hcall_init(spapr);
dcc345b6
CLG
297}
298
299static int spapr_irq_claim_xive(sPAPRMachineState *spapr, int irq, bool lsi,
300 Error **errp)
301{
302 if (!spapr_xive_irq_claim(spapr->xive, irq, lsi)) {
303 error_setg(errp, "IRQ %d is invalid", irq);
304 return -1;
305 }
306 return 0;
307}
308
309static void spapr_irq_free_xive(sPAPRMachineState *spapr, int irq, int num)
310{
311 int i;
312
313 for (i = irq; i < irq + num; ++i) {
314 spapr_xive_irq_free(spapr->xive, i);
315 }
316}
317
318static qemu_irq spapr_qirq_xive(sPAPRMachineState *spapr, int irq)
319{
a0c493ae 320 sPAPRXive *xive = spapr->xive;
a0c493ae
CLG
321
322 if (irq >= xive->nr_irqs) {
323 return NULL;
324 }
325
326 /* The sPAPR machine/device should have claimed the IRQ before */
327 assert(xive_eas_is_valid(&xive->eat[irq]));
328
872ff3de 329 return spapr->qirqs[irq];
dcc345b6
CLG
330}
331
332static void spapr_irq_print_info_xive(sPAPRMachineState *spapr,
333 Monitor *mon)
334{
335 CPUState *cs;
336
337 CPU_FOREACH(cs) {
338 PowerPCCPU *cpu = POWERPC_CPU(cs);
339
a28b9a5a 340 xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
dcc345b6
CLG
341 }
342
343 spapr_xive_pic_print_info(spapr->xive, mon);
344}
345
8fa1f4ef
CLG
346static void spapr_irq_cpu_intc_create_xive(sPAPRMachineState *spapr,
347 PowerPCCPU *cpu, Error **errp)
1a937ad7 348{
8fa1f4ef
CLG
349 Error *local_err = NULL;
350 Object *obj;
a28b9a5a 351 sPAPRCPUState *spapr_cpu = spapr_cpu_state(cpu);
8fa1f4ef
CLG
352
353 obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(spapr->xive), &local_err);
354 if (local_err) {
355 error_propagate(errp, local_err);
356 return;
357 }
358
a28b9a5a 359 spapr_cpu->tctx = XIVE_TCTX(obj);
b2e22477
CLG
360
361 /*
362 * (TCG) Early setting the OS CAM line for hotplugged CPUs as they
8fa1f4ef 363 * don't beneficiate from the reset of the XIVE IRQ backend
b2e22477 364 */
a28b9a5a 365 spapr_xive_set_tctx_os_cam(spapr_cpu->tctx);
1a937ad7
CLG
366}
367
1c53b06c
CLG
368static int spapr_irq_post_load_xive(sPAPRMachineState *spapr, int version_id)
369{
370 return 0;
371}
372
b2e22477
CLG
373static void spapr_irq_reset_xive(sPAPRMachineState *spapr, Error **errp)
374{
375 CPUState *cs;
376
377 CPU_FOREACH(cs) {
378 PowerPCCPU *cpu = POWERPC_CPU(cs);
379
380 /* (TCG) Set the OS CAM line of the thread interrupt context. */
a28b9a5a 381 spapr_xive_set_tctx_os_cam(spapr_cpu_state(cpu)->tctx);
b2e22477 382 }
3a8eb78e
CLG
383
384 /* Activate the XIVE MMIOs */
385 spapr_xive_mmio_set_enabled(spapr->xive, true);
b2e22477
CLG
386}
387
872ff3de
CLG
388static void spapr_irq_set_irq_xive(void *opaque, int srcno, int val)
389{
390 sPAPRMachineState *spapr = opaque;
391
392 xive_source_set_irq(&spapr->xive->source, srcno, val);
393}
394
dcc345b6
CLG
395/*
396 * XIVE uses the full IRQ number space. Set it to 8K to be compatible
397 * with XICS.
398 */
399
400#define SPAPR_IRQ_XIVE_NR_IRQS 0x2000
401#define SPAPR_IRQ_XIVE_NR_MSIS (SPAPR_IRQ_XIVE_NR_IRQS - SPAPR_IRQ_MSI)
402
403sPAPRIrq spapr_irq_xive = {
404 .nr_irqs = SPAPR_IRQ_XIVE_NR_IRQS,
405 .nr_msis = SPAPR_IRQ_XIVE_NR_MSIS,
db592b5b 406 .ov5 = SPAPR_OV5_XIVE_EXPLOIT,
dcc345b6
CLG
407
408 .init = spapr_irq_init_xive,
409 .claim = spapr_irq_claim_xive,
410 .free = spapr_irq_free_xive,
411 .qirq = spapr_qirq_xive,
412 .print_info = spapr_irq_print_info_xive,
6e21de4a 413 .dt_populate = spapr_dt_xive,
1a937ad7 414 .cpu_intc_create = spapr_irq_cpu_intc_create_xive,
1c53b06c 415 .post_load = spapr_irq_post_load_xive,
b2e22477 416 .reset = spapr_irq_reset_xive,
872ff3de 417 .set_irq = spapr_irq_set_irq_xive,
dcc345b6
CLG
418};
419
13db0cd9
CLG
420/*
421 * Dual XIVE and XICS IRQ backend.
422 *
423 * Both interrupt mode, XIVE and XICS, objects are created but the
424 * machine starts in legacy interrupt mode (XICS). It can be changed
425 * by the CAS negotiation process and, in that case, the new mode is
426 * activated after an extra machine reset.
427 */
428
429/*
430 * Returns the sPAPR IRQ backend negotiated by CAS. XICS is the
431 * default.
432 */
433static sPAPRIrq *spapr_irq_current(sPAPRMachineState *spapr)
434{
435 return spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT) ?
436 &spapr_irq_xive : &spapr_irq_xics;
437}
438
2e66cdb7
CLG
439static void spapr_irq_init_dual(sPAPRMachineState *spapr, int nr_irqs,
440 Error **errp)
13db0cd9
CLG
441{
442 MachineState *machine = MACHINE(spapr);
443 Error *local_err = NULL;
444
445 if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
446 error_setg(errp, "No KVM support for the 'dual' machine");
447 return;
448 }
449
2e66cdb7 450 spapr_irq_xics.init(spapr, spapr_irq_xics.nr_irqs, &local_err);
13db0cd9
CLG
451 if (local_err) {
452 error_propagate(errp, local_err);
453 return;
454 }
455
456 /*
457 * Align the XICS and the XIVE IRQ number space under QEMU.
458 *
459 * However, the XICS KVM device still considers that the IRQ
460 * numbers should start at XICS_IRQ_BASE (0x1000). Either we
461 * should introduce a KVM device ioctl to set the offset or ignore
462 * the lower 4K numbers when using the get/set ioctl of the XICS
463 * KVM device. The second option seems the least intrusive.
464 */
465 spapr->ics->offset = 0;
466
2e66cdb7 467 spapr_irq_xive.init(spapr, spapr_irq_xive.nr_irqs, &local_err);
13db0cd9
CLG
468 if (local_err) {
469 error_propagate(errp, local_err);
470 return;
471 }
472}
473
474static int spapr_irq_claim_dual(sPAPRMachineState *spapr, int irq, bool lsi,
475 Error **errp)
476{
477 Error *local_err = NULL;
478 int ret;
479
480 ret = spapr_irq_xics.claim(spapr, irq, lsi, &local_err);
481 if (local_err) {
482 error_propagate(errp, local_err);
483 return ret;
484 }
485
486 ret = spapr_irq_xive.claim(spapr, irq, lsi, &local_err);
487 if (local_err) {
488 error_propagate(errp, local_err);
489 return ret;
490 }
491
492 return ret;
493}
494
495static void spapr_irq_free_dual(sPAPRMachineState *spapr, int irq, int num)
496{
497 spapr_irq_xics.free(spapr, irq, num);
498 spapr_irq_xive.free(spapr, irq, num);
499}
500
501static qemu_irq spapr_qirq_dual(sPAPRMachineState *spapr, int irq)
502{
503 sPAPRXive *xive = spapr->xive;
504 ICSState *ics = spapr->ics;
505
506 if (irq >= spapr->irq->nr_irqs) {
507 return NULL;
508 }
509
510 /*
511 * The IRQ number should have been claimed under both interrupt
512 * controllers.
513 */
514 assert(!ICS_IRQ_FREE(ics, irq - ics->offset));
515 assert(xive_eas_is_valid(&xive->eat[irq]));
516
517 return spapr->qirqs[irq];
518}
519
520static void spapr_irq_print_info_dual(sPAPRMachineState *spapr, Monitor *mon)
521{
522 spapr_irq_current(spapr)->print_info(spapr, mon);
523}
524
525static void spapr_irq_dt_populate_dual(sPAPRMachineState *spapr,
526 uint32_t nr_servers, void *fdt,
527 uint32_t phandle)
528{
529 spapr_irq_current(spapr)->dt_populate(spapr, nr_servers, fdt, phandle);
530}
531
532static void spapr_irq_cpu_intc_create_dual(sPAPRMachineState *spapr,
533 PowerPCCPU *cpu, Error **errp)
534{
535 Error *local_err = NULL;
536
537 spapr_irq_xive.cpu_intc_create(spapr, cpu, &local_err);
538 if (local_err) {
539 error_propagate(errp, local_err);
540 return;
541 }
542
543 spapr_irq_xics.cpu_intc_create(spapr, cpu, errp);
544}
545
546static int spapr_irq_post_load_dual(sPAPRMachineState *spapr, int version_id)
547{
548 /*
549 * Force a reset of the XIVE backend after migration. The machine
550 * defaults to XICS at startup.
551 */
552 if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
553 spapr_irq_xive.reset(spapr, &error_fatal);
554 }
555
556 return spapr_irq_current(spapr)->post_load(spapr, version_id);
557}
558
559static void spapr_irq_reset_dual(sPAPRMachineState *spapr, Error **errp)
560{
3a8eb78e
CLG
561 /*
562 * Deactivate the XIVE MMIOs. The XIVE backend will reenable them
563 * if selected.
564 */
565 spapr_xive_mmio_set_enabled(spapr->xive, false);
566
13db0cd9
CLG
567 spapr_irq_current(spapr)->reset(spapr, errp);
568}
569
570static void spapr_irq_set_irq_dual(void *opaque, int srcno, int val)
571{
572 sPAPRMachineState *spapr = opaque;
573
574 spapr_irq_current(spapr)->set_irq(spapr, srcno, val);
575}
576
577/*
578 * Define values in sync with the XIVE and XICS backend
579 */
580#define SPAPR_IRQ_DUAL_NR_IRQS 0x2000
581#define SPAPR_IRQ_DUAL_NR_MSIS (SPAPR_IRQ_DUAL_NR_IRQS - SPAPR_IRQ_MSI)
582
583sPAPRIrq spapr_irq_dual = {
584 .nr_irqs = SPAPR_IRQ_DUAL_NR_IRQS,
585 .nr_msis = SPAPR_IRQ_DUAL_NR_MSIS,
586 .ov5 = SPAPR_OV5_XIVE_BOTH,
587
588 .init = spapr_irq_init_dual,
589 .claim = spapr_irq_claim_dual,
590 .free = spapr_irq_free_dual,
591 .qirq = spapr_qirq_dual,
592 .print_info = spapr_irq_print_info_dual,
593 .dt_populate = spapr_irq_dt_populate_dual,
594 .cpu_intc_create = spapr_irq_cpu_intc_create_dual,
595 .post_load = spapr_irq_post_load_dual,
596 .reset = spapr_irq_reset_dual,
597 .set_irq = spapr_irq_set_irq_dual
598};
599
ef01ed9d
CLG
600/*
601 * sPAPR IRQ frontend routines for devices
602 */
fab397d8
CLG
603void spapr_irq_init(sPAPRMachineState *spapr, Error **errp)
604{
1a511340
GK
605 MachineState *machine = MACHINE(spapr);
606
607 if (machine_kernel_irqchip_split(machine)) {
608 error_setg(errp, "kernel_irqchip split mode not supported on pseries");
609 return;
610 }
611
612 if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
613 error_setg(errp,
614 "kernel_irqchip requested but only available with KVM");
615 return;
616 }
617
fab397d8
CLG
618 /* Initialize the MSI IRQ allocator. */
619 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
3ba3d0bc 620 spapr_irq_msi_init(spapr, spapr->irq->nr_msis);
fab397d8
CLG
621 }
622
2e66cdb7 623 spapr->irq->init(spapr, spapr->irq->nr_irqs, errp);
872ff3de
CLG
624
625 spapr->qirqs = qemu_allocate_irqs(spapr->irq->set_irq, spapr,
626 spapr->irq->nr_irqs);
fab397d8 627}
ef01ed9d
CLG
628
629int spapr_irq_claim(sPAPRMachineState *spapr, int irq, bool lsi, Error **errp)
630{
3ba3d0bc 631 return spapr->irq->claim(spapr, irq, lsi, errp);
ef01ed9d
CLG
632}
633
634void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num)
635{
3ba3d0bc 636 spapr->irq->free(spapr, irq, num);
ef01ed9d
CLG
637}
638
639qemu_irq spapr_qirq(sPAPRMachineState *spapr, int irq)
640{
3ba3d0bc 641 return spapr->irq->qirq(spapr, irq);
ef01ed9d
CLG
642}
643
1c53b06c
CLG
644int spapr_irq_post_load(sPAPRMachineState *spapr, int version_id)
645{
3ba3d0bc 646 return spapr->irq->post_load(spapr, version_id);
1c53b06c
CLG
647}
648
b2e22477
CLG
649void spapr_irq_reset(sPAPRMachineState *spapr, Error **errp)
650{
3ba3d0bc
CLG
651 if (spapr->irq->reset) {
652 spapr->irq->reset(spapr, errp);
b2e22477
CLG
653 }
654}
655
ef01ed9d
CLG
656/*
657 * XICS legacy routines - to deprecate one day
658 */
659
660static int ics_find_free_block(ICSState *ics, int num, int alignnum)
661{
662 int first, i;
663
664 for (first = 0; first < ics->nr_irqs; first += alignnum) {
665 if (num > (ics->nr_irqs - first)) {
666 return -1;
667 }
668 for (i = first; i < first + num; ++i) {
669 if (!ICS_IRQ_FREE(ics, i)) {
670 break;
671 }
672 }
673 if (i == (first + num)) {
674 return first;
675 }
676 }
677
678 return -1;
679}
680
681int spapr_irq_find(sPAPRMachineState *spapr, int num, bool align, Error **errp)
682{
683 ICSState *ics = spapr->ics;
684 int first = -1;
685
686 assert(ics);
687
688 /*
689 * MSIMesage::data is used for storing VIRQ so
690 * it has to be aligned to num to support multiple
691 * MSI vectors. MSI-X is not affected by this.
692 * The hint is used for the first IRQ, the rest should
693 * be allocated continuously.
694 */
695 if (align) {
696 assert((num == 1) || (num == 2) || (num == 4) ||
697 (num == 8) || (num == 16) || (num == 32));
698 first = ics_find_free_block(ics, num, num);
699 } else {
700 first = ics_find_free_block(ics, num, 1);
701 }
702
703 if (first < 0) {
704 error_setg(errp, "can't find a free %d-IRQ block", num);
705 return -1;
706 }
707
708 return first + ics->offset;
709}
ae837402
CLG
710
711#define SPAPR_IRQ_XICS_LEGACY_NR_IRQS 0x400
712
713sPAPRIrq spapr_irq_xics_legacy = {
714 .nr_irqs = SPAPR_IRQ_XICS_LEGACY_NR_IRQS,
715 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_IRQS,
db592b5b 716 .ov5 = SPAPR_OV5_XIVE_LEGACY,
ae837402
CLG
717
718 .init = spapr_irq_init_xics,
719 .claim = spapr_irq_claim_xics,
720 .free = spapr_irq_free_xics,
721 .qirq = spapr_qirq_xics,
722 .print_info = spapr_irq_print_info_xics,
6e21de4a 723 .dt_populate = spapr_dt_xics,
1a937ad7 724 .cpu_intc_create = spapr_irq_cpu_intc_create_xics,
1c53b06c 725 .post_load = spapr_irq_post_load_xics,
872ff3de 726 .set_irq = spapr_irq_set_irq_xics,
ae837402 727};