KVM: PPC: Book 3S: XICS: Fix potential issue with duplicate IRQ resends
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_xics.c
CommitLineData
bc5ad3f3
BH
1/*
2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/kvm_host.h>
12#include <linux/err.h>
13#include <linux/gfp.h>
5975a2e0 14#include <linux/anon_inodes.h>
433c5c20 15#include <linux/spinlock.h>
bc5ad3f3 16
7c0f6ba6 17#include <linux/uaccess.h>
bc5ad3f3
BH
18#include <asm/kvm_book3s.h>
19#include <asm/kvm_ppc.h>
20#include <asm/hvcall.h>
21#include <asm/xics.h>
22#include <asm/debug.h>
7bfa9ad5 23#include <asm/time.h>
bc5ad3f3
BH
24
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27
28#include "book3s_xics.h"
29
30#if 1
31#define XICS_DBG(fmt...) do { } while (0)
32#else
33#define XICS_DBG(fmt...) trace_printk(fmt)
34#endif
35
e7d26f28
BH
36#define ENABLE_REALMODE true
37#define DEBUG_REALMODE false
38
bc5ad3f3
BH
39/*
40 * LOCKING
41 * =======
42 *
34cb7954 43 * Each ICS has a spin lock protecting the information about the IRQ
4e33d1f0 44 * sources and avoiding simultaneous deliveries of the same interrupt.
bc5ad3f3
BH
45 *
46 * ICP operations are done via a single compare & swap transaction
47 * (most ICP state fits in the union kvmppc_icp_state)
48 */
49
50/*
51 * TODO
52 * ====
53 *
54 * - To speed up resends, keep a bitmap of "resend" set bits in the
55 * ICS
56 *
57 * - Speed up server# -> ICP lookup (array ? hash table ?)
58 *
59 * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
60 * locks array to improve scalability
bc5ad3f3
BH
61 */
62
63/* -- ICS routines -- */
64
65static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
66 u32 new_irq);
67
25a2150b
PM
68/*
69 * Return value ideally indicates how the interrupt was handled, but no
70 * callers look at it (given that we don't implement KVM_IRQ_LINE_STATUS),
71 * so just return 0.
72 */
73static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
bc5ad3f3
BH
74{
75 struct ics_irq_state *state;
76 struct kvmppc_ics *ics;
77 u16 src;
78
79 XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
80
81 ics = kvmppc_xics_find_ics(xics, irq, &src);
82 if (!ics) {
83 XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
84 return -EINVAL;
85 }
86 state = &ics->irq_state[src];
87 if (!state->exists)
88 return -EINVAL;
89
90 /*
91 * We set state->asserted locklessly. This should be fine as
92 * we are the only setter, thus concurrent access is undefined
93 * to begin with.
94 */
b1a4286b 95 if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
bc5ad3f3 96 state->asserted = 1;
25a2150b 97 else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
bc5ad3f3
BH
98 state->asserted = 0;
99 return 0;
100 }
101
5d375199
PM
102 /* Record which CPU this arrived on for passed-through interrupts */
103 if (state->host_irq)
104 state->intr_cpu = raw_smp_processor_id();
105
bc5ad3f3
BH
106 /* Attempt delivery */
107 icp_deliver_irq(xics, NULL, irq);
108
25a2150b 109 return 0;
bc5ad3f3
BH
110}
111
112static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
113 struct kvmppc_icp *icp)
114{
115 int i;
116
34cb7954
SW
117 unsigned long flags;
118
119 local_irq_save(flags);
120 arch_spin_lock(&ics->lock);
bc5ad3f3
BH
121
122 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
123 struct ics_irq_state *state = &ics->irq_state[i];
124
125 if (!state->resend)
126 continue;
127
bf5a71d5
LZ
128 state->resend = 0;
129
bc5ad3f3
BH
130 XICS_DBG("resend %#x prio %#x\n", state->number,
131 state->priority);
132
34cb7954
SW
133 arch_spin_unlock(&ics->lock);
134 local_irq_restore(flags);
bc5ad3f3 135 icp_deliver_irq(xics, icp, state->number);
34cb7954
SW
136 local_irq_save(flags);
137 arch_spin_lock(&ics->lock);
bc5ad3f3
BH
138 }
139
34cb7954
SW
140 arch_spin_unlock(&ics->lock);
141 local_irq_restore(flags);
bc5ad3f3
BH
142}
143
d19bd862
PM
144static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
145 struct ics_irq_state *state,
146 u32 server, u32 priority, u32 saved_priority)
147{
148 bool deliver;
34cb7954 149 unsigned long flags;
d19bd862 150
34cb7954
SW
151 local_irq_save(flags);
152 arch_spin_lock(&ics->lock);
d19bd862
PM
153
154 state->server = server;
155 state->priority = priority;
156 state->saved_priority = saved_priority;
157 deliver = false;
158 if ((state->masked_pending || state->resend) && priority != MASKED) {
159 state->masked_pending = 0;
bf5a71d5 160 state->resend = 0;
d19bd862
PM
161 deliver = true;
162 }
163
34cb7954
SW
164 arch_spin_unlock(&ics->lock);
165 local_irq_restore(flags);
d19bd862
PM
166
167 return deliver;
168}
169
bc5ad3f3
BH
170int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
171{
172 struct kvmppc_xics *xics = kvm->arch.xics;
173 struct kvmppc_icp *icp;
174 struct kvmppc_ics *ics;
175 struct ics_irq_state *state;
176 u16 src;
bc5ad3f3
BH
177
178 if (!xics)
179 return -ENODEV;
180
181 ics = kvmppc_xics_find_ics(xics, irq, &src);
182 if (!ics)
183 return -EINVAL;
184 state = &ics->irq_state[src];
185
186 icp = kvmppc_xics_find_server(kvm, server);
187 if (!icp)
188 return -EINVAL;
189
bc5ad3f3
BH
190 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
191 irq, server, priority,
192 state->masked_pending, state->resend);
193
d19bd862 194 if (write_xive(xics, ics, state, server, priority, priority))
bc5ad3f3
BH
195 icp_deliver_irq(xics, icp, irq);
196
197 return 0;
198}
199
200int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
201{
202 struct kvmppc_xics *xics = kvm->arch.xics;
203 struct kvmppc_ics *ics;
204 struct ics_irq_state *state;
205 u16 src;
34cb7954 206 unsigned long flags;
bc5ad3f3
BH
207
208 if (!xics)
209 return -ENODEV;
210
211 ics = kvmppc_xics_find_ics(xics, irq, &src);
212 if (!ics)
213 return -EINVAL;
214 state = &ics->irq_state[src];
215
34cb7954
SW
216 local_irq_save(flags);
217 arch_spin_lock(&ics->lock);
bc5ad3f3
BH
218 *server = state->server;
219 *priority = state->priority;
34cb7954
SW
220 arch_spin_unlock(&ics->lock);
221 local_irq_restore(flags);
bc5ad3f3
BH
222
223 return 0;
224}
225
d19bd862
PM
226int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
227{
228 struct kvmppc_xics *xics = kvm->arch.xics;
229 struct kvmppc_icp *icp;
230 struct kvmppc_ics *ics;
231 struct ics_irq_state *state;
232 u16 src;
233
234 if (!xics)
235 return -ENODEV;
236
237 ics = kvmppc_xics_find_ics(xics, irq, &src);
238 if (!ics)
239 return -EINVAL;
240 state = &ics->irq_state[src];
241
242 icp = kvmppc_xics_find_server(kvm, state->server);
243 if (!icp)
244 return -EINVAL;
245
246 if (write_xive(xics, ics, state, state->server, state->saved_priority,
247 state->saved_priority))
248 icp_deliver_irq(xics, icp, irq);
249
250 return 0;
251}
252
253int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
254{
255 struct kvmppc_xics *xics = kvm->arch.xics;
256 struct kvmppc_ics *ics;
257 struct ics_irq_state *state;
258 u16 src;
259
260 if (!xics)
261 return -ENODEV;
262
263 ics = kvmppc_xics_find_ics(xics, irq, &src);
264 if (!ics)
265 return -EINVAL;
266 state = &ics->irq_state[src];
267
268 write_xive(xics, ics, state, state->server, MASKED, state->priority);
269
270 return 0;
271}
272
bc5ad3f3
BH
273/* -- ICP routines, including hcalls -- */
274
275static inline bool icp_try_update(struct kvmppc_icp *icp,
276 union kvmppc_icp_state old,
277 union kvmppc_icp_state new,
278 bool change_self)
279{
280 bool success;
281
282 /* Calculate new output value */
283 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
284
285 /* Attempt atomic update */
286 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
287 if (!success)
288 goto bail;
289
ade3ac66 290 XICS_DBG("UPD [%04lx] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
bc5ad3f3
BH
291 icp->server_num,
292 old.cppr, old.mfrr, old.pending_pri, old.xisr,
293 old.need_resend, old.out_ee);
294 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
295 new.cppr, new.mfrr, new.pending_pri, new.xisr,
296 new.need_resend, new.out_ee);
297 /*
298 * Check for output state update
299 *
300 * Note that this is racy since another processor could be updating
301 * the state already. This is why we never clear the interrupt output
302 * here, we only ever set it. The clear only happens prior to doing
303 * an update and only by the processor itself. Currently we do it
304 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
305 *
306 * We also do not try to figure out whether the EE state has changed,
e7d26f28
BH
307 * we unconditionally set it if the new state calls for it. The reason
308 * for that is that we opportunistically remove the pending interrupt
309 * flag when raising CPPR, so we need to set it back here if an
310 * interrupt is still pending.
bc5ad3f3
BH
311 */
312 if (new.out_ee) {
313 kvmppc_book3s_queue_irqprio(icp->vcpu,
314 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
315 if (!change_self)
54695c30 316 kvmppc_fast_vcpu_kick(icp->vcpu);
bc5ad3f3
BH
317 }
318 bail:
319 return success;
320}
321
322static void icp_check_resend(struct kvmppc_xics *xics,
323 struct kvmppc_icp *icp)
324{
325 u32 icsid;
326
327 /* Order this load with the test for need_resend in the caller */
328 smp_rmb();
329 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
330 struct kvmppc_ics *ics = xics->ics[icsid];
331
332 if (!test_and_clear_bit(icsid, icp->resend_map))
333 continue;
334 if (!ics)
335 continue;
336 ics_check_resend(xics, ics, icp);
337 }
338}
339
340static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
341 u32 *reject)
342{
343 union kvmppc_icp_state old_state, new_state;
344 bool success;
345
ade3ac66 346 XICS_DBG("try deliver %#x(P:%#x) to server %#lx\n", irq, priority,
bc5ad3f3
BH
347 icp->server_num);
348
349 do {
5ee07612 350 old_state = new_state = READ_ONCE(icp->state);
bc5ad3f3
BH
351
352 *reject = 0;
353
354 /* See if we can deliver */
355 success = new_state.cppr > priority &&
356 new_state.mfrr > priority &&
357 new_state.pending_pri > priority;
358
359 /*
360 * If we can, check for a rejection and perform the
361 * delivery
362 */
363 if (success) {
364 *reject = new_state.xisr;
365 new_state.xisr = irq;
366 new_state.pending_pri = priority;
367 } else {
368 /*
369 * If we failed to deliver we set need_resend
370 * so a subsequent CPPR state change causes us
371 * to try a new delivery.
372 */
373 new_state.need_resend = true;
374 }
375
376 } while (!icp_try_update(icp, old_state, new_state, false));
377
378 return success;
379}
380
381static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
382 u32 new_irq)
383{
384 struct ics_irq_state *state;
385 struct kvmppc_ics *ics;
386 u32 reject;
387 u16 src;
34cb7954 388 unsigned long flags;
bc5ad3f3
BH
389
390 /*
391 * This is used both for initial delivery of an interrupt and
392 * for subsequent rejection.
393 *
394 * Rejection can be racy vs. resends. We have evaluated the
395 * rejection in an atomic ICP transaction which is now complete,
396 * so potentially the ICP can already accept the interrupt again.
397 *
398 * So we need to retry the delivery. Essentially the reject path
399 * boils down to a failed delivery. Always.
400 *
401 * Now the interrupt could also have moved to a different target,
402 * thus we may need to re-do the ICP lookup as well
403 */
404
405 again:
406 /* Get the ICS state and lock it */
407 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
408 if (!ics) {
409 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
410 return;
411 }
412 state = &ics->irq_state[src];
413
414 /* Get a lock on the ICS */
34cb7954
SW
415 local_irq_save(flags);
416 arch_spin_lock(&ics->lock);
bc5ad3f3
BH
417
418 /* Get our server */
419 if (!icp || state->server != icp->server_num) {
420 icp = kvmppc_xics_find_server(xics->kvm, state->server);
421 if (!icp) {
422 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
423 new_irq, state->server);
424 goto out;
425 }
426 }
427
428 /* Clear the resend bit of that interrupt */
429 state->resend = 0;
430
431 /*
432 * If masked, bail out
433 *
434 * Note: PAPR doesn't mention anything about masked pending
435 * when doing a resend, only when doing a delivery.
436 *
437 * However that would have the effect of losing a masked
438 * interrupt that was rejected and isn't consistent with
439 * the whole masked_pending business which is about not
440 * losing interrupts that occur while masked.
441 *
446957ba 442 * I don't differentiate normal deliveries and resends, this
bc5ad3f3
BH
443 * implementation will differ from PAPR and not lose such
444 * interrupts.
445 */
446 if (state->priority == MASKED) {
447 XICS_DBG("irq %#x masked pending\n", new_irq);
448 state->masked_pending = 1;
449 goto out;
450 }
451
452 /*
453 * Try the delivery, this will set the need_resend flag
454 * in the ICP as part of the atomic transaction if the
455 * delivery is not possible.
456 *
457 * Note that if successful, the new delivery might have itself
458 * rejected an interrupt that was "delivered" before we took the
34cb7954 459 * ics spin lock.
bc5ad3f3
BH
460 *
461 * In this case we do the whole sequence all over again for the
462 * new guy. We cannot assume that the rejected interrupt is less
463 * favored than the new one, and thus doesn't need to be delivered,
464 * because by the time we exit icp_try_to_deliver() the target
465 * processor may well have alrady consumed & completed it, and thus
466 * the rejected interrupt might actually be already acceptable.
467 */
468 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
469 /*
470 * Delivery was successful, did we reject somebody else ?
471 */
472 if (reject && reject != XICS_IPI) {
34cb7954
SW
473 arch_spin_unlock(&ics->lock);
474 local_irq_restore(flags);
bc5ad3f3
BH
475 new_irq = reject;
476 goto again;
477 }
478 } else {
479 /*
480 * We failed to deliver the interrupt we need to set the
481 * resend map bit and mark the ICS state as needing a resend
482 */
483 set_bit(ics->icsid, icp->resend_map);
484 state->resend = 1;
485
486 /*
487 * If the need_resend flag got cleared in the ICP some time
488 * between icp_try_to_deliver() atomic update and now, then
489 * we know it might have missed the resend_map bit. So we
490 * retry
491 */
492 smp_mb();
493 if (!icp->state.need_resend) {
bf5a71d5 494 state->resend = 0;
34cb7954
SW
495 arch_spin_unlock(&ics->lock);
496 local_irq_restore(flags);
bc5ad3f3
BH
497 goto again;
498 }
499 }
500 out:
34cb7954
SW
501 arch_spin_unlock(&ics->lock);
502 local_irq_restore(flags);
bc5ad3f3
BH
503}
504
505static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
506 u8 new_cppr)
507{
508 union kvmppc_icp_state old_state, new_state;
509 bool resend;
510
511 /*
512 * This handles several related states in one operation:
513 *
514 * ICP State: Down_CPPR
515 *
516 * Load CPPR with new value and if the XISR is 0
517 * then check for resends:
518 *
519 * ICP State: Resend
520 *
521 * If MFRR is more favored than CPPR, check for IPIs
522 * and notify ICS of a potential resend. This is done
523 * asynchronously (when used in real mode, we will have
524 * to exit here).
525 *
526 * We do not handle the complete Check_IPI as documented
527 * here. In the PAPR, this state will be used for both
528 * Set_MFRR and Down_CPPR. However, we know that we aren't
529 * changing the MFRR state here so we don't need to handle
530 * the case of an MFRR causing a reject of a pending irq,
531 * this will have been handled when the MFRR was set in the
532 * first place.
533 *
534 * Thus we don't have to handle rejects, only resends.
535 *
536 * When implementing real mode for HV KVM, resend will lead to
537 * a H_TOO_HARD return and the whole transaction will be handled
538 * in virtual mode.
539 */
540 do {
5ee07612 541 old_state = new_state = READ_ONCE(icp->state);
bc5ad3f3
BH
542
543 /* Down_CPPR */
544 new_state.cppr = new_cppr;
545
546 /*
547 * Cut down Resend / Check_IPI / IPI
548 *
549 * The logic is that we cannot have a pending interrupt
550 * trumped by an IPI at this point (see above), so we
551 * know that either the pending interrupt is already an
552 * IPI (in which case we don't care to override it) or
553 * it's either more favored than us or non existent
554 */
555 if (new_state.mfrr < new_cppr &&
556 new_state.mfrr <= new_state.pending_pri) {
557 WARN_ON(new_state.xisr != XICS_IPI &&
558 new_state.xisr != 0);
559 new_state.pending_pri = new_state.mfrr;
560 new_state.xisr = XICS_IPI;
561 }
562
563 /* Latch/clear resend bit */
564 resend = new_state.need_resend;
565 new_state.need_resend = 0;
566
567 } while (!icp_try_update(icp, old_state, new_state, true));
568
569 /*
570 * Now handle resend checks. Those are asynchronous to the ICP
571 * state update in HW (ie bus transactions) so we can handle them
572 * separately here too
573 */
574 if (resend)
575 icp_check_resend(xics, icp);
576}
577
e7d26f28 578static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
bc5ad3f3
BH
579{
580 union kvmppc_icp_state old_state, new_state;
581 struct kvmppc_icp *icp = vcpu->arch.icp;
582 u32 xirr;
583
584 /* First, remove EE from the processor */
585 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
586 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
587
588 /*
589 * ICP State: Accept_Interrupt
590 *
591 * Return the pending interrupt (if any) along with the
592 * current CPPR, then clear the XISR & set CPPR to the
593 * pending priority
594 */
595 do {
5ee07612 596 old_state = new_state = READ_ONCE(icp->state);
bc5ad3f3
BH
597
598 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
599 if (!old_state.xisr)
600 break;
601 new_state.cppr = new_state.pending_pri;
602 new_state.pending_pri = 0xff;
603 new_state.xisr = 0;
604
605 } while (!icp_try_update(icp, old_state, new_state, true));
606
607 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
608
609 return xirr;
610}
611
e7d26f28
BH
612static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
613 unsigned long mfrr)
bc5ad3f3
BH
614{
615 union kvmppc_icp_state old_state, new_state;
616 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
617 struct kvmppc_icp *icp;
618 u32 reject;
619 bool resend;
620 bool local;
621
622 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
623 vcpu->vcpu_id, server, mfrr);
624
625 icp = vcpu->arch.icp;
626 local = icp->server_num == server;
627 if (!local) {
628 icp = kvmppc_xics_find_server(vcpu->kvm, server);
629 if (!icp)
630 return H_PARAMETER;
631 }
632
633 /*
634 * ICP state: Set_MFRR
635 *
636 * If the CPPR is more favored than the new MFRR, then
637 * nothing needs to be rejected as there can be no XISR to
638 * reject. If the MFRR is being made less favored then
639 * there might be a previously-rejected interrupt needing
640 * to be resent.
641 *
5b88cda6
SW
642 * ICP state: Check_IPI
643 *
bc5ad3f3 644 * If the CPPR is less favored, then we might be replacing
5b88cda6 645 * an interrupt, and thus need to possibly reject it.
bc5ad3f3 646 *
5b88cda6
SW
647 * ICP State: IPI
648 *
649 * Besides rejecting any pending interrupts, we also
650 * update XISR and pending_pri to mark IPI as pending.
651 *
652 * PAPR does not describe this state, but if the MFRR is being
653 * made less favored than its earlier value, there might be
654 * a previously-rejected interrupt needing to be resent.
655 * Ideally, we would want to resend only if
656 * prio(pending_interrupt) < mfrr &&
657 * prio(pending_interrupt) < cppr
658 * where pending interrupt is the one that was rejected. But
659 * we don't have that state, so we simply trigger a resend
660 * whenever the MFRR is made less favored.
bc5ad3f3
BH
661 */
662 do {
5ee07612 663 old_state = new_state = READ_ONCE(icp->state);
bc5ad3f3
BH
664
665 /* Set_MFRR */
666 new_state.mfrr = mfrr;
667
668 /* Check_IPI */
669 reject = 0;
670 resend = false;
671 if (mfrr < new_state.cppr) {
672 /* Reject a pending interrupt if not an IPI */
5b88cda6 673 if (mfrr <= new_state.pending_pri) {
bc5ad3f3 674 reject = new_state.xisr;
5b88cda6
SW
675 new_state.pending_pri = mfrr;
676 new_state.xisr = XICS_IPI;
677 }
bc5ad3f3
BH
678 }
679
5b88cda6 680 if (mfrr > old_state.mfrr) {
bc5ad3f3
BH
681 resend = new_state.need_resend;
682 new_state.need_resend = 0;
683 }
684 } while (!icp_try_update(icp, old_state, new_state, local));
685
686 /* Handle reject */
687 if (reject && reject != XICS_IPI)
688 icp_deliver_irq(xics, icp, reject);
689
690 /* Handle resend */
691 if (resend)
692 icp_check_resend(xics, icp);
693
694 return H_SUCCESS;
695}
696
8e44ddc3
PM
697static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
698{
699 union kvmppc_icp_state state;
700 struct kvmppc_icp *icp;
701
702 icp = vcpu->arch.icp;
703 if (icp->server_num != server) {
704 icp = kvmppc_xics_find_server(vcpu->kvm, server);
705 if (!icp)
706 return H_PARAMETER;
707 }
5ee07612 708 state = READ_ONCE(icp->state);
8e44ddc3
PM
709 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
710 kvmppc_set_gpr(vcpu, 5, state.mfrr);
711 return H_SUCCESS;
712}
713
e7d26f28 714static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
bc5ad3f3
BH
715{
716 union kvmppc_icp_state old_state, new_state;
717 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
718 struct kvmppc_icp *icp = vcpu->arch.icp;
719 u32 reject;
720
721 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
722
723 /*
724 * ICP State: Set_CPPR
725 *
726 * We can safely compare the new value with the current
727 * value outside of the transaction as the CPPR is only
728 * ever changed by the processor on itself
729 */
730 if (cppr > icp->state.cppr)
731 icp_down_cppr(xics, icp, cppr);
732 else if (cppr == icp->state.cppr)
733 return;
734
735 /*
736 * ICP State: Up_CPPR
737 *
738 * The processor is raising its priority, this can result
739 * in a rejection of a pending interrupt:
740 *
741 * ICP State: Reject_Current
742 *
743 * We can remove EE from the current processor, the update
744 * transaction will set it again if needed
745 */
746 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
747 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
748
749 do {
5ee07612 750 old_state = new_state = READ_ONCE(icp->state);
bc5ad3f3
BH
751
752 reject = 0;
753 new_state.cppr = cppr;
754
755 if (cppr <= new_state.pending_pri) {
756 reject = new_state.xisr;
757 new_state.xisr = 0;
758 new_state.pending_pri = 0xff;
759 }
760
761 } while (!icp_try_update(icp, old_state, new_state, true));
762
763 /*
764 * Check for rejects. They are handled by doing a new delivery
765 * attempt (see comments in icp_deliver_irq).
766 */
767 if (reject && reject != XICS_IPI)
768 icp_deliver_irq(xics, icp, reject);
769}
770
e7d26f28 771static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
bc5ad3f3
BH
772{
773 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
774 struct kvmppc_icp *icp = vcpu->arch.icp;
775 struct kvmppc_ics *ics;
776 struct ics_irq_state *state;
777 u32 irq = xirr & 0x00ffffff;
778 u16 src;
779
780 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
781
782 /*
783 * ICP State: EOI
784 *
785 * Note: If EOI is incorrectly used by SW to lower the CPPR
786 * value (ie more favored), we do not check for rejection of
787 * a pending interrupt, this is a SW error and PAPR sepcifies
788 * that we don't have to deal with it.
789 *
790 * The sending of an EOI to the ICS is handled after the
791 * CPPR update
792 *
793 * ICP State: Down_CPPR which we handle
794 * in a separate function as it's shared with H_CPPR.
795 */
796 icp_down_cppr(xics, icp, xirr >> 24);
797
798 /* IPIs have no EOI */
799 if (irq == XICS_IPI)
800 return H_SUCCESS;
801 /*
802 * EOI handling: If the interrupt is still asserted, we need to
803 * resend it. We can take a lockless "peek" at the ICS state here.
804 *
805 * "Message" interrupts will never have "asserted" set
806 */
807 ics = kvmppc_xics_find_ics(xics, irq, &src);
808 if (!ics) {
809 XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
810 return H_PARAMETER;
811 }
812 state = &ics->irq_state[src];
813
814 /* Still asserted, resend it */
815 if (state->asserted)
816 icp_deliver_irq(xics, icp, irq);
817
25a2150b
PM
818 kvm_notify_acked_irq(vcpu->kvm, 0, irq);
819
bc5ad3f3
BH
820 return H_SUCCESS;
821}
822
f7af5209 823int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
e7d26f28
BH
824{
825 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
826 struct kvmppc_icp *icp = vcpu->arch.icp;
827
828 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
829 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
830
878610fe
SW
831 if (icp->rm_action & XICS_RM_KICK_VCPU) {
832 icp->n_rm_kick_vcpu++;
e7d26f28 833 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
878610fe
SW
834 }
835 if (icp->rm_action & XICS_RM_CHECK_RESEND) {
836 icp->n_rm_check_resend++;
5b88cda6 837 icp_check_resend(xics, icp->rm_resend_icp);
878610fe 838 }
878610fe
SW
839 if (icp->rm_action & XICS_RM_NOTIFY_EOI) {
840 icp->n_rm_notify_eoi++;
25a2150b 841 kvm_notify_acked_irq(vcpu->kvm, 0, icp->rm_eoied_irq);
878610fe 842 }
e7d26f28
BH
843
844 icp->rm_action = 0;
845
846 return H_SUCCESS;
847}
f7af5209 848EXPORT_SYMBOL_GPL(kvmppc_xics_rm_complete);
e7d26f28 849
bc5ad3f3
BH
850int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
851{
e7d26f28 852 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
bc5ad3f3
BH
853 unsigned long res;
854 int rc = H_SUCCESS;
855
856 /* Check if we have an ICP */
e7d26f28 857 if (!xics || !vcpu->arch.icp)
bc5ad3f3
BH
858 return H_HARDWARE;
859
8e44ddc3
PM
860 /* These requests don't have real-mode implementations at present */
861 switch (req) {
862 case H_XIRR_X:
863 res = kvmppc_h_xirr(vcpu);
864 kvmppc_set_gpr(vcpu, 4, res);
865 kvmppc_set_gpr(vcpu, 5, get_tb());
866 return rc;
867 case H_IPOLL:
868 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
869 return rc;
870 }
871
e7d26f28 872 /* Check for real mode returning too hard */
a78b55d1 873 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
e7d26f28
BH
874 return kvmppc_xics_rm_complete(vcpu, req);
875
bc5ad3f3
BH
876 switch (req) {
877 case H_XIRR:
e7d26f28 878 res = kvmppc_h_xirr(vcpu);
bc5ad3f3
BH
879 kvmppc_set_gpr(vcpu, 4, res);
880 break;
881 case H_CPPR:
e7d26f28 882 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
bc5ad3f3
BH
883 break;
884 case H_EOI:
e7d26f28 885 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
bc5ad3f3
BH
886 break;
887 case H_IPI:
e7d26f28
BH
888 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
889 kvmppc_get_gpr(vcpu, 5));
bc5ad3f3
BH
890 break;
891 }
892
893 return rc;
894}
2ba9f0d8 895EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
bc5ad3f3
BH
896
897
898/* -- Initialisation code etc. -- */
899
af893c7d
SW
900static void xics_debugfs_irqmap(struct seq_file *m,
901 struct kvmppc_passthru_irqmap *pimap)
902{
903 int i;
904
905 if (!pimap)
906 return;
907 seq_printf(m, "========\nPIRQ mappings: %d maps\n===========\n",
908 pimap->n_mapped);
909 for (i = 0; i < pimap->n_mapped; i++) {
910 seq_printf(m, "r_hwirq=%x, v_hwirq=%x\n",
911 pimap->mapped[i].r_hwirq, pimap->mapped[i].v_hwirq);
912 }
913}
914
bc5ad3f3
BH
915static int xics_debug_show(struct seq_file *m, void *private)
916{
917 struct kvmppc_xics *xics = m->private;
918 struct kvm *kvm = xics->kvm;
919 struct kvm_vcpu *vcpu;
920 int icsid, i;
34cb7954 921 unsigned long flags;
878610fe 922 unsigned long t_rm_kick_vcpu, t_rm_check_resend;
5efa6605 923 unsigned long t_rm_notify_eoi;
6e0365b7 924 unsigned long t_reject, t_check_resend;
bc5ad3f3
BH
925
926 if (!kvm)
927 return 0;
928
878610fe
SW
929 t_rm_kick_vcpu = 0;
930 t_rm_notify_eoi = 0;
931 t_rm_check_resend = 0;
6e0365b7
SW
932 t_check_resend = 0;
933 t_reject = 0;
878610fe 934
af893c7d
SW
935 xics_debugfs_irqmap(m, kvm->arch.pimap);
936
bc5ad3f3
BH
937 seq_printf(m, "=========\nICP state\n=========\n");
938
939 kvm_for_each_vcpu(i, vcpu, kvm) {
940 struct kvmppc_icp *icp = vcpu->arch.icp;
941 union kvmppc_icp_state state;
942
943 if (!icp)
944 continue;
945
5ee07612 946 state.raw = READ_ONCE(icp->state.raw);
bc5ad3f3
BH
947 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
948 icp->server_num, state.xisr,
949 state.pending_pri, state.cppr, state.mfrr,
950 state.out_ee, state.need_resend);
878610fe
SW
951 t_rm_kick_vcpu += icp->n_rm_kick_vcpu;
952 t_rm_notify_eoi += icp->n_rm_notify_eoi;
953 t_rm_check_resend += icp->n_rm_check_resend;
6e0365b7
SW
954 t_check_resend += icp->n_check_resend;
955 t_reject += icp->n_reject;
bc5ad3f3
BH
956 }
957
5efa6605 958 seq_printf(m, "ICP Guest->Host totals: kick_vcpu=%lu check_resend=%lu notify_eoi=%lu\n",
878610fe 959 t_rm_kick_vcpu, t_rm_check_resend,
5efa6605 960 t_rm_notify_eoi);
6e0365b7
SW
961 seq_printf(m, "ICP Real Mode totals: check_resend=%lu resend=%lu\n",
962 t_check_resend, t_reject);
bc5ad3f3
BH
963 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
964 struct kvmppc_ics *ics = xics->ics[icsid];
965
966 if (!ics)
967 continue;
968
969 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
970 icsid);
971
34cb7954
SW
972 local_irq_save(flags);
973 arch_spin_lock(&ics->lock);
bc5ad3f3
BH
974
975 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
976 struct ics_irq_state *irq = &ics->irq_state[i];
977
978 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
979 irq->number, irq->server, irq->priority,
980 irq->saved_priority, irq->asserted,
981 irq->resend, irq->masked_pending);
982
983 }
34cb7954
SW
984 arch_spin_unlock(&ics->lock);
985 local_irq_restore(flags);
bc5ad3f3
BH
986 }
987 return 0;
988}
989
990static int xics_debug_open(struct inode *inode, struct file *file)
991{
992 return single_open(file, xics_debug_show, inode->i_private);
993}
994
995static const struct file_operations xics_debug_fops = {
996 .open = xics_debug_open,
997 .read = seq_read,
998 .llseek = seq_lseek,
999 .release = single_release,
1000};
1001
1002static void xics_debugfs_init(struct kvmppc_xics *xics)
1003{
1004 char *name;
1005
1006 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
1007 if (!name) {
1008 pr_err("%s: no memory for name\n", __func__);
1009 return;
1010 }
1011
1012 xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
1013 xics, &xics_debug_fops);
1014
1015 pr_debug("%s: created %s\n", __func__, name);
1016 kfree(name);
1017}
1018
5975a2e0
PM
1019static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
1020 struct kvmppc_xics *xics, int irq)
bc5ad3f3
BH
1021{
1022 struct kvmppc_ics *ics;
1023 int i, icsid;
1024
1025 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
1026
1027 mutex_lock(&kvm->lock);
1028
1029 /* ICS already exists - somebody else got here first */
1030 if (xics->ics[icsid])
1031 goto out;
1032
1033 /* Create the ICS */
1034 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
1035 if (!ics)
1036 goto out;
1037
bc5ad3f3
BH
1038 ics->icsid = icsid;
1039
1040 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1041 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
1042 ics->irq_state[i].priority = MASKED;
1043 ics->irq_state[i].saved_priority = MASKED;
1044 }
1045 smp_wmb();
1046 xics->ics[icsid] = ics;
1047
1048 if (icsid > xics->max_icsid)
1049 xics->max_icsid = icsid;
1050
1051 out:
1052 mutex_unlock(&kvm->lock);
1053 return xics->ics[icsid];
1054}
1055
1056int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
1057{
1058 struct kvmppc_icp *icp;
1059
1060 if (!vcpu->kvm->arch.xics)
1061 return -ENODEV;
1062
1063 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
1064 return -EEXIST;
1065
1066 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
1067 if (!icp)
1068 return -ENOMEM;
1069
1070 icp->vcpu = vcpu;
1071 icp->server_num = server_num;
1072 icp->state.mfrr = MASKED;
1073 icp->state.pending_pri = MASKED;
1074 vcpu->arch.icp = icp;
1075
1076 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
1077
1078 return 0;
1079}
1080
8b78645c
PM
1081u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
1082{
1083 struct kvmppc_icp *icp = vcpu->arch.icp;
1084 union kvmppc_icp_state state;
1085
1086 if (!icp)
1087 return 0;
1088 state = icp->state;
1089 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1090 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1091 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1092 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1093}
1094
1095int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1096{
1097 struct kvmppc_icp *icp = vcpu->arch.icp;
1098 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1099 union kvmppc_icp_state old_state, new_state;
1100 struct kvmppc_ics *ics;
1101 u8 cppr, mfrr, pending_pri;
1102 u32 xisr;
1103 u16 src;
1104 bool resend;
1105
1106 if (!icp || !xics)
1107 return -ENOENT;
1108
1109 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1110 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1111 KVM_REG_PPC_ICP_XISR_MASK;
1112 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1113 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1114
1115 /* Require the new state to be internally consistent */
1116 if (xisr == 0) {
1117 if (pending_pri != 0xff)
1118 return -EINVAL;
1119 } else if (xisr == XICS_IPI) {
1120 if (pending_pri != mfrr || pending_pri >= cppr)
1121 return -EINVAL;
1122 } else {
1123 if (pending_pri >= mfrr || pending_pri >= cppr)
1124 return -EINVAL;
1125 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1126 if (!ics)
1127 return -EINVAL;
1128 }
1129
1130 new_state.raw = 0;
1131 new_state.cppr = cppr;
1132 new_state.xisr = xisr;
1133 new_state.mfrr = mfrr;
1134 new_state.pending_pri = pending_pri;
1135
1136 /*
1137 * Deassert the CPU interrupt request.
1138 * icp_try_update will reassert it if necessary.
1139 */
1140 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1141 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1142
1143 /*
1144 * Note that if we displace an interrupt from old_state.xisr,
1145 * we don't mark it as rejected. We expect userspace to set
1146 * the state of the interrupt sources to be consistent with
1147 * the ICP states (either before or afterwards, which doesn't
1148 * matter). We do handle resends due to CPPR becoming less
1149 * favoured because that is necessary to end up with a
1150 * consistent state in the situation where userspace restores
1151 * the ICS states before the ICP states.
1152 */
1153 do {
5ee07612 1154 old_state = READ_ONCE(icp->state);
8b78645c
PM
1155
1156 if (new_state.mfrr <= old_state.mfrr) {
1157 resend = false;
1158 new_state.need_resend = old_state.need_resend;
1159 } else {
1160 resend = old_state.need_resend;
1161 new_state.need_resend = 0;
1162 }
1163 } while (!icp_try_update(icp, old_state, new_state, false));
1164
1165 if (resend)
1166 icp_check_resend(xics, icp);
1167
1168 return 0;
1169}
1170
5975a2e0
PM
1171static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
1172{
1173 int ret;
1174 struct kvmppc_ics *ics;
1175 struct ics_irq_state *irqp;
1176 u64 __user *ubufp = (u64 __user *) addr;
1177 u16 idx;
1178 u64 val, prio;
34cb7954 1179 unsigned long flags;
5975a2e0
PM
1180
1181 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1182 if (!ics)
1183 return -ENOENT;
bc5ad3f3 1184
5975a2e0 1185 irqp = &ics->irq_state[idx];
34cb7954
SW
1186 local_irq_save(flags);
1187 arch_spin_lock(&ics->lock);
5975a2e0
PM
1188 ret = -ENOENT;
1189 if (irqp->exists) {
1190 val = irqp->server;
1191 prio = irqp->priority;
1192 if (prio == MASKED) {
1193 val |= KVM_XICS_MASKED;
1194 prio = irqp->saved_priority;
1195 }
1196 val |= prio << KVM_XICS_PRIORITY_SHIFT;
b1a4286b
PM
1197 if (irqp->lsi) {
1198 val |= KVM_XICS_LEVEL_SENSITIVE;
1199 if (irqp->asserted)
1200 val |= KVM_XICS_PENDING;
1201 } else if (irqp->masked_pending || irqp->resend)
5975a2e0
PM
1202 val |= KVM_XICS_PENDING;
1203 ret = 0;
1204 }
34cb7954
SW
1205 arch_spin_unlock(&ics->lock);
1206 local_irq_restore(flags);
5975a2e0
PM
1207
1208 if (!ret && put_user(val, ubufp))
1209 ret = -EFAULT;
1210
1211 return ret;
1212}
1213
1214static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
bc5ad3f3 1215{
5975a2e0
PM
1216 struct kvmppc_ics *ics;
1217 struct ics_irq_state *irqp;
1218 u64 __user *ubufp = (u64 __user *) addr;
1219 u16 idx;
1220 u64 val;
1221 u8 prio;
1222 u32 server;
34cb7954 1223 unsigned long flags;
5975a2e0
PM
1224
1225 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1226 return -ENOENT;
1227
1228 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1229 if (!ics) {
1230 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1231 if (!ics)
1232 return -ENOMEM;
1233 }
1234 irqp = &ics->irq_state[idx];
1235 if (get_user(val, ubufp))
1236 return -EFAULT;
1237
1238 server = val & KVM_XICS_DESTINATION_MASK;
1239 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1240 if (prio != MASKED &&
1241 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1242 return -EINVAL;
bc5ad3f3 1243
34cb7954
SW
1244 local_irq_save(flags);
1245 arch_spin_lock(&ics->lock);
5975a2e0
PM
1246 irqp->server = server;
1247 irqp->saved_priority = prio;
1248 if (val & KVM_XICS_MASKED)
1249 prio = MASKED;
1250 irqp->priority = prio;
1251 irqp->resend = 0;
1252 irqp->masked_pending = 0;
b1a4286b 1253 irqp->lsi = 0;
5975a2e0 1254 irqp->asserted = 0;
b1a4286b
PM
1255 if (val & KVM_XICS_LEVEL_SENSITIVE) {
1256 irqp->lsi = 1;
1257 if (val & KVM_XICS_PENDING)
1258 irqp->asserted = 1;
1259 }
5975a2e0 1260 irqp->exists = 1;
34cb7954
SW
1261 arch_spin_unlock(&ics->lock);
1262 local_irq_restore(flags);
bc5ad3f3 1263
5975a2e0
PM
1264 if (val & KVM_XICS_PENDING)
1265 icp_deliver_irq(xics, NULL, irqp->number);
bc5ad3f3 1266
5975a2e0
PM
1267 return 0;
1268}
1269
1270int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1271 bool line_status)
1272{
1273 struct kvmppc_xics *xics = kvm->arch.xics;
1274
e48ba1cb
PM
1275 if (!xics)
1276 return -ENODEV;
25a2150b
PM
1277 return ics_deliver_irq(xics, irq, level);
1278}
1279
b1a4286b
PM
1280int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *irq_entry,
1281 struct kvm *kvm, int irq_source_id,
1282 int level, bool line_status)
25a2150b 1283{
25a2150b
PM
1284 return kvm_set_irq(kvm, irq_source_id, irq_entry->gsi,
1285 level, line_status);
5975a2e0
PM
1286}
1287
1288static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1289{
1290 struct kvmppc_xics *xics = dev->private;
1291
1292 switch (attr->group) {
1293 case KVM_DEV_XICS_GRP_SOURCES:
1294 return xics_set_source(xics, attr->attr, attr->addr);
bc5ad3f3 1295 }
5975a2e0
PM
1296 return -ENXIO;
1297}
bc5ad3f3 1298
5975a2e0
PM
1299static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1300{
1301 struct kvmppc_xics *xics = dev->private;
1302
1303 switch (attr->group) {
1304 case KVM_DEV_XICS_GRP_SOURCES:
1305 return xics_get_source(xics, attr->attr, attr->addr);
1306 }
1307 return -ENXIO;
bc5ad3f3
BH
1308}
1309
5975a2e0 1310static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
bc5ad3f3 1311{
5975a2e0
PM
1312 switch (attr->group) {
1313 case KVM_DEV_XICS_GRP_SOURCES:
1314 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1315 attr->attr < KVMPPC_XICS_NR_IRQS)
1316 return 0;
1317 break;
1318 }
1319 return -ENXIO;
1320}
1321
1322static void kvmppc_xics_free(struct kvm_device *dev)
1323{
1324 struct kvmppc_xics *xics = dev->private;
bc5ad3f3
BH
1325 int i;
1326 struct kvm *kvm = xics->kvm;
1327
1328 debugfs_remove(xics->dentry);
1329
1330 if (kvm)
1331 kvm->arch.xics = NULL;
1332
1333 for (i = 0; i <= xics->max_icsid; i++)
1334 kfree(xics->ics[i]);
1335 kfree(xics);
5975a2e0 1336 kfree(dev);
bc5ad3f3
BH
1337}
1338
5975a2e0 1339static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
bc5ad3f3
BH
1340{
1341 struct kvmppc_xics *xics;
5975a2e0 1342 struct kvm *kvm = dev->kvm;
bc5ad3f3
BH
1343 int ret = 0;
1344
1345 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1346 if (!xics)
1347 return -ENOMEM;
1348
5975a2e0
PM
1349 dev->private = xics;
1350 xics->dev = dev;
bc5ad3f3
BH
1351 xics->kvm = kvm;
1352
1353 /* Already there ? */
bc5ad3f3
BH
1354 if (kvm->arch.xics)
1355 ret = -EEXIST;
1356 else
1357 kvm->arch.xics = xics;
bc5ad3f3 1358
458ff3c0
GN
1359 if (ret) {
1360 kfree(xics);
bc5ad3f3 1361 return ret;
458ff3c0 1362 }
bc5ad3f3 1363
3a167bea 1364#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
e7d26f28
BH
1365 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1366 /* Enable real mode support */
1367 xics->real_mode = ENABLE_REALMODE;
1368 xics->real_mode_dbg = DEBUG_REALMODE;
1369 }
3a167bea 1370#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
e7d26f28 1371
bc5ad3f3
BH
1372 return 0;
1373}
1374
023e9fdd
CD
1375static void kvmppc_xics_init(struct kvm_device *dev)
1376{
1377 struct kvmppc_xics *xics = (struct kvmppc_xics *)dev->private;
1378
1379 xics_debugfs_init(xics);
1380}
1381
5975a2e0
PM
1382struct kvm_device_ops kvm_xics_ops = {
1383 .name = "kvm-xics",
1384 .create = kvmppc_xics_create,
023e9fdd 1385 .init = kvmppc_xics_init,
5975a2e0
PM
1386 .destroy = kvmppc_xics_free,
1387 .set_attr = xics_set_attr,
1388 .get_attr = xics_get_attr,
1389 .has_attr = xics_has_attr,
1390};
1391
1392int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1393 u32 xcpu)
1394{
1395 struct kvmppc_xics *xics = dev->private;
1396 int r = -EBUSY;
1397
1398 if (dev->ops != &kvm_xics_ops)
1399 return -EPERM;
1400 if (xics->kvm != vcpu->kvm)
1401 return -EPERM;
1402 if (vcpu->arch.irq_type)
1403 return -EBUSY;
1404
1405 r = kvmppc_xics_create_icp(vcpu, xcpu);
1406 if (!r)
1407 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1408
1409 return r;
1410}
1411
bc5ad3f3
BH
1412void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1413{
1414 if (!vcpu->arch.icp)
1415 return;
1416 kfree(vcpu->arch.icp);
1417 vcpu->arch.icp = NULL;
1418 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1419}
25a2150b
PM
1420
1421static int xics_set_irq(struct kvm_kernel_irq_routing_entry *e,
1422 struct kvm *kvm, int irq_source_id, int level,
1423 bool line_status)
1424{
1425 return kvm_set_irq(kvm, irq_source_id, e->gsi, level, line_status);
1426}
1427
1428int kvm_irq_map_gsi(struct kvm *kvm,
1429 struct kvm_kernel_irq_routing_entry *entries, int gsi)
1430{
1431 entries->gsi = gsi;
1432 entries->type = KVM_IRQ_ROUTING_IRQCHIP;
1433 entries->set = xics_set_irq;
1434 entries->irqchip.irqchip = 0;
1435 entries->irqchip.pin = gsi;
1436 return 1;
1437}
1438
1439int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
1440{
1441 return pin;
1442}
5d375199
PM
1443
1444void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long irq,
1445 unsigned long host_irq)
1446{
1447 struct kvmppc_xics *xics = kvm->arch.xics;
1448 struct kvmppc_ics *ics;
1449 u16 idx;
1450
1451 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1452 if (!ics)
1453 return;
1454
1455 ics->irq_state[idx].host_irq = host_irq;
1456 ics->irq_state[idx].intr_cpu = -1;
1457}
1458EXPORT_SYMBOL_GPL(kvmppc_xics_set_mapped);
1459
1460void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long irq,
1461 unsigned long host_irq)
1462{
1463 struct kvmppc_xics *xics = kvm->arch.xics;
1464 struct kvmppc_ics *ics;
1465 u16 idx;
1466
1467 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1468 if (!ics)
1469 return;
1470
1471 ics->irq_state[idx].host_irq = 0;
1472}
1473EXPORT_SYMBOL_GPL(kvmppc_xics_clr_mapped);