kvm: powerpc: book3s: drop is_hv_enabled
[linux-2.6/btrfs-unstable.git] / arch / powerpc / kvm / book3s_xics.c
blob02a17dcf16107adbe1309185bf464d0fda4b8772
1 /*
2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
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 */
10 #include <linux/kernel.h>
11 #include <linux/kvm_host.h>
12 #include <linux/err.h>
13 #include <linux/gfp.h>
14 #include <linux/anon_inodes.h>
16 #include <asm/uaccess.h>
17 #include <asm/kvm_book3s.h>
18 #include <asm/kvm_ppc.h>
19 #include <asm/hvcall.h>
20 #include <asm/xics.h>
21 #include <asm/debug.h>
22 #include <asm/time.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
27 #include "book3s_xics.h"
29 #if 1
30 #define XICS_DBG(fmt...) do { } while (0)
31 #else
32 #define XICS_DBG(fmt...) trace_printk(fmt)
33 #endif
35 #define ENABLE_REALMODE true
36 #define DEBUG_REALMODE false
39 * LOCKING
40 * =======
42 * Each ICS has a mutex protecting the information about the IRQ
43 * sources and avoiding simultaneous deliveries if the same interrupt.
45 * ICP operations are done via a single compare & swap transaction
46 * (most ICP state fits in the union kvmppc_icp_state)
50 * TODO
51 * ====
53 * - To speed up resends, keep a bitmap of "resend" set bits in the
54 * ICS
56 * - Speed up server# -> ICP lookup (array ? hash table ?)
58 * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
59 * locks array to improve scalability
62 /* -- ICS routines -- */
64 static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
65 u32 new_irq);
67 static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level,
68 bool report_status)
70 struct ics_irq_state *state;
71 struct kvmppc_ics *ics;
72 u16 src;
74 XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
76 ics = kvmppc_xics_find_ics(xics, irq, &src);
77 if (!ics) {
78 XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
79 return -EINVAL;
81 state = &ics->irq_state[src];
82 if (!state->exists)
83 return -EINVAL;
85 if (report_status)
86 return state->asserted;
89 * We set state->asserted locklessly. This should be fine as
90 * we are the only setter, thus concurrent access is undefined
91 * to begin with.
93 if (level == KVM_INTERRUPT_SET_LEVEL)
94 state->asserted = 1;
95 else if (level == KVM_INTERRUPT_UNSET) {
96 state->asserted = 0;
97 return 0;
100 /* Attempt delivery */
101 icp_deliver_irq(xics, NULL, irq);
103 return state->asserted;
106 static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
107 struct kvmppc_icp *icp)
109 int i;
111 mutex_lock(&ics->lock);
113 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
114 struct ics_irq_state *state = &ics->irq_state[i];
116 if (!state->resend)
117 continue;
119 XICS_DBG("resend %#x prio %#x\n", state->number,
120 state->priority);
122 mutex_unlock(&ics->lock);
123 icp_deliver_irq(xics, icp, state->number);
124 mutex_lock(&ics->lock);
127 mutex_unlock(&ics->lock);
130 static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
131 struct ics_irq_state *state,
132 u32 server, u32 priority, u32 saved_priority)
134 bool deliver;
136 mutex_lock(&ics->lock);
138 state->server = server;
139 state->priority = priority;
140 state->saved_priority = saved_priority;
141 deliver = false;
142 if ((state->masked_pending || state->resend) && priority != MASKED) {
143 state->masked_pending = 0;
144 deliver = true;
147 mutex_unlock(&ics->lock);
149 return deliver;
152 int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
154 struct kvmppc_xics *xics = kvm->arch.xics;
155 struct kvmppc_icp *icp;
156 struct kvmppc_ics *ics;
157 struct ics_irq_state *state;
158 u16 src;
160 if (!xics)
161 return -ENODEV;
163 ics = kvmppc_xics_find_ics(xics, irq, &src);
164 if (!ics)
165 return -EINVAL;
166 state = &ics->irq_state[src];
168 icp = kvmppc_xics_find_server(kvm, server);
169 if (!icp)
170 return -EINVAL;
172 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
173 irq, server, priority,
174 state->masked_pending, state->resend);
176 if (write_xive(xics, ics, state, server, priority, priority))
177 icp_deliver_irq(xics, icp, irq);
179 return 0;
182 int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
184 struct kvmppc_xics *xics = kvm->arch.xics;
185 struct kvmppc_ics *ics;
186 struct ics_irq_state *state;
187 u16 src;
189 if (!xics)
190 return -ENODEV;
192 ics = kvmppc_xics_find_ics(xics, irq, &src);
193 if (!ics)
194 return -EINVAL;
195 state = &ics->irq_state[src];
197 mutex_lock(&ics->lock);
198 *server = state->server;
199 *priority = state->priority;
200 mutex_unlock(&ics->lock);
202 return 0;
205 int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
207 struct kvmppc_xics *xics = kvm->arch.xics;
208 struct kvmppc_icp *icp;
209 struct kvmppc_ics *ics;
210 struct ics_irq_state *state;
211 u16 src;
213 if (!xics)
214 return -ENODEV;
216 ics = kvmppc_xics_find_ics(xics, irq, &src);
217 if (!ics)
218 return -EINVAL;
219 state = &ics->irq_state[src];
221 icp = kvmppc_xics_find_server(kvm, state->server);
222 if (!icp)
223 return -EINVAL;
225 if (write_xive(xics, ics, state, state->server, state->saved_priority,
226 state->saved_priority))
227 icp_deliver_irq(xics, icp, irq);
229 return 0;
232 int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
234 struct kvmppc_xics *xics = kvm->arch.xics;
235 struct kvmppc_ics *ics;
236 struct ics_irq_state *state;
237 u16 src;
239 if (!xics)
240 return -ENODEV;
242 ics = kvmppc_xics_find_ics(xics, irq, &src);
243 if (!ics)
244 return -EINVAL;
245 state = &ics->irq_state[src];
247 write_xive(xics, ics, state, state->server, MASKED, state->priority);
249 return 0;
252 /* -- ICP routines, including hcalls -- */
254 static inline bool icp_try_update(struct kvmppc_icp *icp,
255 union kvmppc_icp_state old,
256 union kvmppc_icp_state new,
257 bool change_self)
259 bool success;
261 /* Calculate new output value */
262 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
264 /* Attempt atomic update */
265 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
266 if (!success)
267 goto bail;
269 XICS_DBG("UPD [%04x] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
270 icp->server_num,
271 old.cppr, old.mfrr, old.pending_pri, old.xisr,
272 old.need_resend, old.out_ee);
273 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
274 new.cppr, new.mfrr, new.pending_pri, new.xisr,
275 new.need_resend, new.out_ee);
277 * Check for output state update
279 * Note that this is racy since another processor could be updating
280 * the state already. This is why we never clear the interrupt output
281 * here, we only ever set it. The clear only happens prior to doing
282 * an update and only by the processor itself. Currently we do it
283 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
285 * We also do not try to figure out whether the EE state has changed,
286 * we unconditionally set it if the new state calls for it. The reason
287 * for that is that we opportunistically remove the pending interrupt
288 * flag when raising CPPR, so we need to set it back here if an
289 * interrupt is still pending.
291 if (new.out_ee) {
292 kvmppc_book3s_queue_irqprio(icp->vcpu,
293 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
294 if (!change_self)
295 kvmppc_fast_vcpu_kick(icp->vcpu);
297 bail:
298 return success;
301 static void icp_check_resend(struct kvmppc_xics *xics,
302 struct kvmppc_icp *icp)
304 u32 icsid;
306 /* Order this load with the test for need_resend in the caller */
307 smp_rmb();
308 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
309 struct kvmppc_ics *ics = xics->ics[icsid];
311 if (!test_and_clear_bit(icsid, icp->resend_map))
312 continue;
313 if (!ics)
314 continue;
315 ics_check_resend(xics, ics, icp);
319 static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
320 u32 *reject)
322 union kvmppc_icp_state old_state, new_state;
323 bool success;
325 XICS_DBG("try deliver %#x(P:%#x) to server %#x\n", irq, priority,
326 icp->server_num);
328 do {
329 old_state = new_state = ACCESS_ONCE(icp->state);
331 *reject = 0;
333 /* See if we can deliver */
334 success = new_state.cppr > priority &&
335 new_state.mfrr > priority &&
336 new_state.pending_pri > priority;
339 * If we can, check for a rejection and perform the
340 * delivery
342 if (success) {
343 *reject = new_state.xisr;
344 new_state.xisr = irq;
345 new_state.pending_pri = priority;
346 } else {
348 * If we failed to deliver we set need_resend
349 * so a subsequent CPPR state change causes us
350 * to try a new delivery.
352 new_state.need_resend = true;
355 } while (!icp_try_update(icp, old_state, new_state, false));
357 return success;
360 static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
361 u32 new_irq)
363 struct ics_irq_state *state;
364 struct kvmppc_ics *ics;
365 u32 reject;
366 u16 src;
369 * This is used both for initial delivery of an interrupt and
370 * for subsequent rejection.
372 * Rejection can be racy vs. resends. We have evaluated the
373 * rejection in an atomic ICP transaction which is now complete,
374 * so potentially the ICP can already accept the interrupt again.
376 * So we need to retry the delivery. Essentially the reject path
377 * boils down to a failed delivery. Always.
379 * Now the interrupt could also have moved to a different target,
380 * thus we may need to re-do the ICP lookup as well
383 again:
384 /* Get the ICS state and lock it */
385 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
386 if (!ics) {
387 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
388 return;
390 state = &ics->irq_state[src];
392 /* Get a lock on the ICS */
393 mutex_lock(&ics->lock);
395 /* Get our server */
396 if (!icp || state->server != icp->server_num) {
397 icp = kvmppc_xics_find_server(xics->kvm, state->server);
398 if (!icp) {
399 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
400 new_irq, state->server);
401 goto out;
405 /* Clear the resend bit of that interrupt */
406 state->resend = 0;
409 * If masked, bail out
411 * Note: PAPR doesn't mention anything about masked pending
412 * when doing a resend, only when doing a delivery.
414 * However that would have the effect of losing a masked
415 * interrupt that was rejected and isn't consistent with
416 * the whole masked_pending business which is about not
417 * losing interrupts that occur while masked.
419 * I don't differenciate normal deliveries and resends, this
420 * implementation will differ from PAPR and not lose such
421 * interrupts.
423 if (state->priority == MASKED) {
424 XICS_DBG("irq %#x masked pending\n", new_irq);
425 state->masked_pending = 1;
426 goto out;
430 * Try the delivery, this will set the need_resend flag
431 * in the ICP as part of the atomic transaction if the
432 * delivery is not possible.
434 * Note that if successful, the new delivery might have itself
435 * rejected an interrupt that was "delivered" before we took the
436 * icp mutex.
438 * In this case we do the whole sequence all over again for the
439 * new guy. We cannot assume that the rejected interrupt is less
440 * favored than the new one, and thus doesn't need to be delivered,
441 * because by the time we exit icp_try_to_deliver() the target
442 * processor may well have alrady consumed & completed it, and thus
443 * the rejected interrupt might actually be already acceptable.
445 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
447 * Delivery was successful, did we reject somebody else ?
449 if (reject && reject != XICS_IPI) {
450 mutex_unlock(&ics->lock);
451 new_irq = reject;
452 goto again;
454 } else {
456 * We failed to deliver the interrupt we need to set the
457 * resend map bit and mark the ICS state as needing a resend
459 set_bit(ics->icsid, icp->resend_map);
460 state->resend = 1;
463 * If the need_resend flag got cleared in the ICP some time
464 * between icp_try_to_deliver() atomic update and now, then
465 * we know it might have missed the resend_map bit. So we
466 * retry
468 smp_mb();
469 if (!icp->state.need_resend) {
470 mutex_unlock(&ics->lock);
471 goto again;
474 out:
475 mutex_unlock(&ics->lock);
478 static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
479 u8 new_cppr)
481 union kvmppc_icp_state old_state, new_state;
482 bool resend;
485 * This handles several related states in one operation:
487 * ICP State: Down_CPPR
489 * Load CPPR with new value and if the XISR is 0
490 * then check for resends:
492 * ICP State: Resend
494 * If MFRR is more favored than CPPR, check for IPIs
495 * and notify ICS of a potential resend. This is done
496 * asynchronously (when used in real mode, we will have
497 * to exit here).
499 * We do not handle the complete Check_IPI as documented
500 * here. In the PAPR, this state will be used for both
501 * Set_MFRR and Down_CPPR. However, we know that we aren't
502 * changing the MFRR state here so we don't need to handle
503 * the case of an MFRR causing a reject of a pending irq,
504 * this will have been handled when the MFRR was set in the
505 * first place.
507 * Thus we don't have to handle rejects, only resends.
509 * When implementing real mode for HV KVM, resend will lead to
510 * a H_TOO_HARD return and the whole transaction will be handled
511 * in virtual mode.
513 do {
514 old_state = new_state = ACCESS_ONCE(icp->state);
516 /* Down_CPPR */
517 new_state.cppr = new_cppr;
520 * Cut down Resend / Check_IPI / IPI
522 * The logic is that we cannot have a pending interrupt
523 * trumped by an IPI at this point (see above), so we
524 * know that either the pending interrupt is already an
525 * IPI (in which case we don't care to override it) or
526 * it's either more favored than us or non existent
528 if (new_state.mfrr < new_cppr &&
529 new_state.mfrr <= new_state.pending_pri) {
530 WARN_ON(new_state.xisr != XICS_IPI &&
531 new_state.xisr != 0);
532 new_state.pending_pri = new_state.mfrr;
533 new_state.xisr = XICS_IPI;
536 /* Latch/clear resend bit */
537 resend = new_state.need_resend;
538 new_state.need_resend = 0;
540 } while (!icp_try_update(icp, old_state, new_state, true));
543 * Now handle resend checks. Those are asynchronous to the ICP
544 * state update in HW (ie bus transactions) so we can handle them
545 * separately here too
547 if (resend)
548 icp_check_resend(xics, icp);
551 static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
553 union kvmppc_icp_state old_state, new_state;
554 struct kvmppc_icp *icp = vcpu->arch.icp;
555 u32 xirr;
557 /* First, remove EE from the processor */
558 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
559 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
562 * ICP State: Accept_Interrupt
564 * Return the pending interrupt (if any) along with the
565 * current CPPR, then clear the XISR & set CPPR to the
566 * pending priority
568 do {
569 old_state = new_state = ACCESS_ONCE(icp->state);
571 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
572 if (!old_state.xisr)
573 break;
574 new_state.cppr = new_state.pending_pri;
575 new_state.pending_pri = 0xff;
576 new_state.xisr = 0;
578 } while (!icp_try_update(icp, old_state, new_state, true));
580 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
582 return xirr;
585 static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
586 unsigned long mfrr)
588 union kvmppc_icp_state old_state, new_state;
589 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
590 struct kvmppc_icp *icp;
591 u32 reject;
592 bool resend;
593 bool local;
595 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
596 vcpu->vcpu_id, server, mfrr);
598 icp = vcpu->arch.icp;
599 local = icp->server_num == server;
600 if (!local) {
601 icp = kvmppc_xics_find_server(vcpu->kvm, server);
602 if (!icp)
603 return H_PARAMETER;
607 * ICP state: Set_MFRR
609 * If the CPPR is more favored than the new MFRR, then
610 * nothing needs to be rejected as there can be no XISR to
611 * reject. If the MFRR is being made less favored then
612 * there might be a previously-rejected interrupt needing
613 * to be resent.
615 * If the CPPR is less favored, then we might be replacing
616 * an interrupt, and thus need to possibly reject it as in
618 * ICP state: Check_IPI
620 do {
621 old_state = new_state = ACCESS_ONCE(icp->state);
623 /* Set_MFRR */
624 new_state.mfrr = mfrr;
626 /* Check_IPI */
627 reject = 0;
628 resend = false;
629 if (mfrr < new_state.cppr) {
630 /* Reject a pending interrupt if not an IPI */
631 if (mfrr <= new_state.pending_pri)
632 reject = new_state.xisr;
633 new_state.pending_pri = mfrr;
634 new_state.xisr = XICS_IPI;
637 if (mfrr > old_state.mfrr && mfrr > new_state.cppr) {
638 resend = new_state.need_resend;
639 new_state.need_resend = 0;
641 } while (!icp_try_update(icp, old_state, new_state, local));
643 /* Handle reject */
644 if (reject && reject != XICS_IPI)
645 icp_deliver_irq(xics, icp, reject);
647 /* Handle resend */
648 if (resend)
649 icp_check_resend(xics, icp);
651 return H_SUCCESS;
654 static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
656 union kvmppc_icp_state state;
657 struct kvmppc_icp *icp;
659 icp = vcpu->arch.icp;
660 if (icp->server_num != server) {
661 icp = kvmppc_xics_find_server(vcpu->kvm, server);
662 if (!icp)
663 return H_PARAMETER;
665 state = ACCESS_ONCE(icp->state);
666 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
667 kvmppc_set_gpr(vcpu, 5, state.mfrr);
668 return H_SUCCESS;
671 static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
673 union kvmppc_icp_state old_state, new_state;
674 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
675 struct kvmppc_icp *icp = vcpu->arch.icp;
676 u32 reject;
678 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
681 * ICP State: Set_CPPR
683 * We can safely compare the new value with the current
684 * value outside of the transaction as the CPPR is only
685 * ever changed by the processor on itself
687 if (cppr > icp->state.cppr)
688 icp_down_cppr(xics, icp, cppr);
689 else if (cppr == icp->state.cppr)
690 return;
693 * ICP State: Up_CPPR
695 * The processor is raising its priority, this can result
696 * in a rejection of a pending interrupt:
698 * ICP State: Reject_Current
700 * We can remove EE from the current processor, the update
701 * transaction will set it again if needed
703 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
704 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
706 do {
707 old_state = new_state = ACCESS_ONCE(icp->state);
709 reject = 0;
710 new_state.cppr = cppr;
712 if (cppr <= new_state.pending_pri) {
713 reject = new_state.xisr;
714 new_state.xisr = 0;
715 new_state.pending_pri = 0xff;
718 } while (!icp_try_update(icp, old_state, new_state, true));
721 * Check for rejects. They are handled by doing a new delivery
722 * attempt (see comments in icp_deliver_irq).
724 if (reject && reject != XICS_IPI)
725 icp_deliver_irq(xics, icp, reject);
728 static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
730 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
731 struct kvmppc_icp *icp = vcpu->arch.icp;
732 struct kvmppc_ics *ics;
733 struct ics_irq_state *state;
734 u32 irq = xirr & 0x00ffffff;
735 u16 src;
737 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
740 * ICP State: EOI
742 * Note: If EOI is incorrectly used by SW to lower the CPPR
743 * value (ie more favored), we do not check for rejection of
744 * a pending interrupt, this is a SW error and PAPR sepcifies
745 * that we don't have to deal with it.
747 * The sending of an EOI to the ICS is handled after the
748 * CPPR update
750 * ICP State: Down_CPPR which we handle
751 * in a separate function as it's shared with H_CPPR.
753 icp_down_cppr(xics, icp, xirr >> 24);
755 /* IPIs have no EOI */
756 if (irq == XICS_IPI)
757 return H_SUCCESS;
759 * EOI handling: If the interrupt is still asserted, we need to
760 * resend it. We can take a lockless "peek" at the ICS state here.
762 * "Message" interrupts will never have "asserted" set
764 ics = kvmppc_xics_find_ics(xics, irq, &src);
765 if (!ics) {
766 XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
767 return H_PARAMETER;
769 state = &ics->irq_state[src];
771 /* Still asserted, resend it */
772 if (state->asserted)
773 icp_deliver_irq(xics, icp, irq);
775 return H_SUCCESS;
778 static noinline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
780 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
781 struct kvmppc_icp *icp = vcpu->arch.icp;
783 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
784 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
786 if (icp->rm_action & XICS_RM_KICK_VCPU)
787 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
788 if (icp->rm_action & XICS_RM_CHECK_RESEND)
789 icp_check_resend(xics, icp);
790 if (icp->rm_action & XICS_RM_REJECT)
791 icp_deliver_irq(xics, icp, icp->rm_reject);
793 icp->rm_action = 0;
795 return H_SUCCESS;
798 int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
800 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
801 unsigned long res;
802 int rc = H_SUCCESS;
804 /* Check if we have an ICP */
805 if (!xics || !vcpu->arch.icp)
806 return H_HARDWARE;
808 /* These requests don't have real-mode implementations at present */
809 switch (req) {
810 case H_XIRR_X:
811 res = kvmppc_h_xirr(vcpu);
812 kvmppc_set_gpr(vcpu, 4, res);
813 kvmppc_set_gpr(vcpu, 5, get_tb());
814 return rc;
815 case H_IPOLL:
816 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
817 return rc;
820 /* Check for real mode returning too hard */
821 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
822 return kvmppc_xics_rm_complete(vcpu, req);
824 switch (req) {
825 case H_XIRR:
826 res = kvmppc_h_xirr(vcpu);
827 kvmppc_set_gpr(vcpu, 4, res);
828 break;
829 case H_CPPR:
830 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
831 break;
832 case H_EOI:
833 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
834 break;
835 case H_IPI:
836 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
837 kvmppc_get_gpr(vcpu, 5));
838 break;
841 return rc;
843 EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
846 /* -- Initialisation code etc. -- */
848 static int xics_debug_show(struct seq_file *m, void *private)
850 struct kvmppc_xics *xics = m->private;
851 struct kvm *kvm = xics->kvm;
852 struct kvm_vcpu *vcpu;
853 int icsid, i;
855 if (!kvm)
856 return 0;
858 seq_printf(m, "=========\nICP state\n=========\n");
860 kvm_for_each_vcpu(i, vcpu, kvm) {
861 struct kvmppc_icp *icp = vcpu->arch.icp;
862 union kvmppc_icp_state state;
864 if (!icp)
865 continue;
867 state.raw = ACCESS_ONCE(icp->state.raw);
868 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
869 icp->server_num, state.xisr,
870 state.pending_pri, state.cppr, state.mfrr,
871 state.out_ee, state.need_resend);
874 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
875 struct kvmppc_ics *ics = xics->ics[icsid];
877 if (!ics)
878 continue;
880 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
881 icsid);
883 mutex_lock(&ics->lock);
885 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
886 struct ics_irq_state *irq = &ics->irq_state[i];
888 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
889 irq->number, irq->server, irq->priority,
890 irq->saved_priority, irq->asserted,
891 irq->resend, irq->masked_pending);
894 mutex_unlock(&ics->lock);
896 return 0;
899 static int xics_debug_open(struct inode *inode, struct file *file)
901 return single_open(file, xics_debug_show, inode->i_private);
904 static const struct file_operations xics_debug_fops = {
905 .open = xics_debug_open,
906 .read = seq_read,
907 .llseek = seq_lseek,
908 .release = single_release,
911 static void xics_debugfs_init(struct kvmppc_xics *xics)
913 char *name;
915 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
916 if (!name) {
917 pr_err("%s: no memory for name\n", __func__);
918 return;
921 xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
922 xics, &xics_debug_fops);
924 pr_debug("%s: created %s\n", __func__, name);
925 kfree(name);
928 static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
929 struct kvmppc_xics *xics, int irq)
931 struct kvmppc_ics *ics;
932 int i, icsid;
934 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
936 mutex_lock(&kvm->lock);
938 /* ICS already exists - somebody else got here first */
939 if (xics->ics[icsid])
940 goto out;
942 /* Create the ICS */
943 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
944 if (!ics)
945 goto out;
947 mutex_init(&ics->lock);
948 ics->icsid = icsid;
950 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
951 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
952 ics->irq_state[i].priority = MASKED;
953 ics->irq_state[i].saved_priority = MASKED;
955 smp_wmb();
956 xics->ics[icsid] = ics;
958 if (icsid > xics->max_icsid)
959 xics->max_icsid = icsid;
961 out:
962 mutex_unlock(&kvm->lock);
963 return xics->ics[icsid];
966 int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
968 struct kvmppc_icp *icp;
970 if (!vcpu->kvm->arch.xics)
971 return -ENODEV;
973 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
974 return -EEXIST;
976 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
977 if (!icp)
978 return -ENOMEM;
980 icp->vcpu = vcpu;
981 icp->server_num = server_num;
982 icp->state.mfrr = MASKED;
983 icp->state.pending_pri = MASKED;
984 vcpu->arch.icp = icp;
986 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
988 return 0;
991 u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
993 struct kvmppc_icp *icp = vcpu->arch.icp;
994 union kvmppc_icp_state state;
996 if (!icp)
997 return 0;
998 state = icp->state;
999 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1000 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1001 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1002 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1005 int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1007 struct kvmppc_icp *icp = vcpu->arch.icp;
1008 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1009 union kvmppc_icp_state old_state, new_state;
1010 struct kvmppc_ics *ics;
1011 u8 cppr, mfrr, pending_pri;
1012 u32 xisr;
1013 u16 src;
1014 bool resend;
1016 if (!icp || !xics)
1017 return -ENOENT;
1019 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1020 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1021 KVM_REG_PPC_ICP_XISR_MASK;
1022 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1023 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1025 /* Require the new state to be internally consistent */
1026 if (xisr == 0) {
1027 if (pending_pri != 0xff)
1028 return -EINVAL;
1029 } else if (xisr == XICS_IPI) {
1030 if (pending_pri != mfrr || pending_pri >= cppr)
1031 return -EINVAL;
1032 } else {
1033 if (pending_pri >= mfrr || pending_pri >= cppr)
1034 return -EINVAL;
1035 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1036 if (!ics)
1037 return -EINVAL;
1040 new_state.raw = 0;
1041 new_state.cppr = cppr;
1042 new_state.xisr = xisr;
1043 new_state.mfrr = mfrr;
1044 new_state.pending_pri = pending_pri;
1047 * Deassert the CPU interrupt request.
1048 * icp_try_update will reassert it if necessary.
1050 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1051 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1054 * Note that if we displace an interrupt from old_state.xisr,
1055 * we don't mark it as rejected. We expect userspace to set
1056 * the state of the interrupt sources to be consistent with
1057 * the ICP states (either before or afterwards, which doesn't
1058 * matter). We do handle resends due to CPPR becoming less
1059 * favoured because that is necessary to end up with a
1060 * consistent state in the situation where userspace restores
1061 * the ICS states before the ICP states.
1063 do {
1064 old_state = ACCESS_ONCE(icp->state);
1066 if (new_state.mfrr <= old_state.mfrr) {
1067 resend = false;
1068 new_state.need_resend = old_state.need_resend;
1069 } else {
1070 resend = old_state.need_resend;
1071 new_state.need_resend = 0;
1073 } while (!icp_try_update(icp, old_state, new_state, false));
1075 if (resend)
1076 icp_check_resend(xics, icp);
1078 return 0;
1081 static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
1083 int ret;
1084 struct kvmppc_ics *ics;
1085 struct ics_irq_state *irqp;
1086 u64 __user *ubufp = (u64 __user *) addr;
1087 u16 idx;
1088 u64 val, prio;
1090 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1091 if (!ics)
1092 return -ENOENT;
1094 irqp = &ics->irq_state[idx];
1095 mutex_lock(&ics->lock);
1096 ret = -ENOENT;
1097 if (irqp->exists) {
1098 val = irqp->server;
1099 prio = irqp->priority;
1100 if (prio == MASKED) {
1101 val |= KVM_XICS_MASKED;
1102 prio = irqp->saved_priority;
1104 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1105 if (irqp->asserted)
1106 val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
1107 else if (irqp->masked_pending || irqp->resend)
1108 val |= KVM_XICS_PENDING;
1109 ret = 0;
1111 mutex_unlock(&ics->lock);
1113 if (!ret && put_user(val, ubufp))
1114 ret = -EFAULT;
1116 return ret;
1119 static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
1121 struct kvmppc_ics *ics;
1122 struct ics_irq_state *irqp;
1123 u64 __user *ubufp = (u64 __user *) addr;
1124 u16 idx;
1125 u64 val;
1126 u8 prio;
1127 u32 server;
1129 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1130 return -ENOENT;
1132 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1133 if (!ics) {
1134 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1135 if (!ics)
1136 return -ENOMEM;
1138 irqp = &ics->irq_state[idx];
1139 if (get_user(val, ubufp))
1140 return -EFAULT;
1142 server = val & KVM_XICS_DESTINATION_MASK;
1143 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1144 if (prio != MASKED &&
1145 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1146 return -EINVAL;
1148 mutex_lock(&ics->lock);
1149 irqp->server = server;
1150 irqp->saved_priority = prio;
1151 if (val & KVM_XICS_MASKED)
1152 prio = MASKED;
1153 irqp->priority = prio;
1154 irqp->resend = 0;
1155 irqp->masked_pending = 0;
1156 irqp->asserted = 0;
1157 if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
1158 irqp->asserted = 1;
1159 irqp->exists = 1;
1160 mutex_unlock(&ics->lock);
1162 if (val & KVM_XICS_PENDING)
1163 icp_deliver_irq(xics, NULL, irqp->number);
1165 return 0;
1168 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1169 bool line_status)
1171 struct kvmppc_xics *xics = kvm->arch.xics;
1173 return ics_deliver_irq(xics, irq, level, line_status);
1176 static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1178 struct kvmppc_xics *xics = dev->private;
1180 switch (attr->group) {
1181 case KVM_DEV_XICS_GRP_SOURCES:
1182 return xics_set_source(xics, attr->attr, attr->addr);
1184 return -ENXIO;
1187 static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1189 struct kvmppc_xics *xics = dev->private;
1191 switch (attr->group) {
1192 case KVM_DEV_XICS_GRP_SOURCES:
1193 return xics_get_source(xics, attr->attr, attr->addr);
1195 return -ENXIO;
1198 static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1200 switch (attr->group) {
1201 case KVM_DEV_XICS_GRP_SOURCES:
1202 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1203 attr->attr < KVMPPC_XICS_NR_IRQS)
1204 return 0;
1205 break;
1207 return -ENXIO;
1210 static void kvmppc_xics_free(struct kvm_device *dev)
1212 struct kvmppc_xics *xics = dev->private;
1213 int i;
1214 struct kvm *kvm = xics->kvm;
1216 debugfs_remove(xics->dentry);
1218 if (kvm)
1219 kvm->arch.xics = NULL;
1221 for (i = 0; i <= xics->max_icsid; i++)
1222 kfree(xics->ics[i]);
1223 kfree(xics);
1224 kfree(dev);
1227 static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
1229 struct kvmppc_xics *xics;
1230 struct kvm *kvm = dev->kvm;
1231 int ret = 0;
1233 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1234 if (!xics)
1235 return -ENOMEM;
1237 dev->private = xics;
1238 xics->dev = dev;
1239 xics->kvm = kvm;
1241 /* Already there ? */
1242 mutex_lock(&kvm->lock);
1243 if (kvm->arch.xics)
1244 ret = -EEXIST;
1245 else
1246 kvm->arch.xics = xics;
1247 mutex_unlock(&kvm->lock);
1249 if (ret)
1250 return ret;
1252 xics_debugfs_init(xics);
1254 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1255 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1256 /* Enable real mode support */
1257 xics->real_mode = ENABLE_REALMODE;
1258 xics->real_mode_dbg = DEBUG_REALMODE;
1260 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1262 return 0;
1265 struct kvm_device_ops kvm_xics_ops = {
1266 .name = "kvm-xics",
1267 .create = kvmppc_xics_create,
1268 .destroy = kvmppc_xics_free,
1269 .set_attr = xics_set_attr,
1270 .get_attr = xics_get_attr,
1271 .has_attr = xics_has_attr,
1274 int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1275 u32 xcpu)
1277 struct kvmppc_xics *xics = dev->private;
1278 int r = -EBUSY;
1280 if (dev->ops != &kvm_xics_ops)
1281 return -EPERM;
1282 if (xics->kvm != vcpu->kvm)
1283 return -EPERM;
1284 if (vcpu->arch.irq_type)
1285 return -EBUSY;
1287 r = kvmppc_xics_create_icp(vcpu, xcpu);
1288 if (!r)
1289 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1291 return r;
1294 void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1296 if (!vcpu->arch.icp)
1297 return;
1298 kfree(vcpu->arch.icp);
1299 vcpu->arch.icp = NULL;
1300 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;