GNU Linux-libre 4.4.285-gnu1
[releases.git] / arch / powerpc / kvm / book3s_hv_rm_xics.c
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
14 #include <asm/kvm_book3s.h>
15 #include <asm/kvm_ppc.h>
16 #include <asm/hvcall.h>
17 #include <asm/xics.h>
18 #include <asm/debug.h>
19 #include <asm/synch.h>
20 #include <asm/ppc-opcode.h>
21
22 #include "book3s_xics.h"
23
24 #define DEBUG_PASSUP
25
26 static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
27                             u32 new_irq);
28
29 /* -- ICS routines -- */
30 static void ics_rm_check_resend(struct kvmppc_xics *xics,
31                                 struct kvmppc_ics *ics, struct kvmppc_icp *icp)
32 {
33         int i;
34
35         arch_spin_lock(&ics->lock);
36
37         for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
38                 struct ics_irq_state *state = &ics->irq_state[i];
39
40                 if (!state->resend)
41                         continue;
42
43                 arch_spin_unlock(&ics->lock);
44                 icp_rm_deliver_irq(xics, icp, state->number);
45                 arch_spin_lock(&ics->lock);
46         }
47
48         arch_spin_unlock(&ics->lock);
49 }
50
51 /* -- ICP routines -- */
52
53 static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
54                                 struct kvm_vcpu *this_vcpu)
55 {
56         struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
57         int cpu;
58
59         /* Mark the target VCPU as having an interrupt pending */
60         vcpu->stat.queue_intr++;
61         set_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
62
63         /* Kick self ? Just set MER and return */
64         if (vcpu == this_vcpu) {
65                 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
66                 return;
67         }
68
69         /* Check if the core is loaded, if not, too hard */
70         cpu = vcpu->arch.thread_cpu;
71         if (cpu < 0 || cpu >= nr_cpu_ids) {
72                 this_icp->rm_action |= XICS_RM_KICK_VCPU;
73                 this_icp->rm_kick_target = vcpu;
74                 return;
75         }
76
77         smp_mb();
78         kvmhv_rm_send_ipi(cpu);
79 }
80
81 static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
82 {
83         /* Note: Only called on self ! */
84         clear_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL,
85                   &vcpu->arch.pending_exceptions);
86         mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
87 }
88
89 static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
90                                      union kvmppc_icp_state old,
91                                      union kvmppc_icp_state new)
92 {
93         struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
94         bool success;
95
96         /* Calculate new output value */
97         new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
98
99         /* Attempt atomic update */
100         success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
101         if (!success)
102                 goto bail;
103
104         /*
105          * Check for output state update
106          *
107          * Note that this is racy since another processor could be updating
108          * the state already. This is why we never clear the interrupt output
109          * here, we only ever set it. The clear only happens prior to doing
110          * an update and only by the processor itself. Currently we do it
111          * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
112          *
113          * We also do not try to figure out whether the EE state has changed,
114          * we unconditionally set it if the new state calls for it. The reason
115          * for that is that we opportunistically remove the pending interrupt
116          * flag when raising CPPR, so we need to set it back here if an
117          * interrupt is still pending.
118          */
119         if (new.out_ee)
120                 icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
121
122         /* Expose the state change for debug purposes */
123         this_vcpu->arch.icp->rm_dbgstate = new;
124         this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
125
126  bail:
127         return success;
128 }
129
130 static inline int check_too_hard(struct kvmppc_xics *xics,
131                                  struct kvmppc_icp *icp)
132 {
133         return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
134 }
135
136 static void icp_rm_check_resend(struct kvmppc_xics *xics,
137                              struct kvmppc_icp *icp)
138 {
139         u32 icsid;
140
141         /* Order this load with the test for need_resend in the caller */
142         smp_rmb();
143         for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
144                 struct kvmppc_ics *ics = xics->ics[icsid];
145
146                 if (!test_and_clear_bit(icsid, icp->resend_map))
147                         continue;
148                 if (!ics)
149                         continue;
150                 ics_rm_check_resend(xics, ics, icp);
151         }
152 }
153
154 static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
155                                u32 *reject)
156 {
157         union kvmppc_icp_state old_state, new_state;
158         bool success;
159
160         do {
161                 old_state = new_state = READ_ONCE(icp->state);
162
163                 *reject = 0;
164
165                 /* See if we can deliver */
166                 success = new_state.cppr > priority &&
167                         new_state.mfrr > priority &&
168                         new_state.pending_pri > priority;
169
170                 /*
171                  * If we can, check for a rejection and perform the
172                  * delivery
173                  */
174                 if (success) {
175                         *reject = new_state.xisr;
176                         new_state.xisr = irq;
177                         new_state.pending_pri = priority;
178                 } else {
179                         /*
180                          * If we failed to deliver we set need_resend
181                          * so a subsequent CPPR state change causes us
182                          * to try a new delivery.
183                          */
184                         new_state.need_resend = true;
185                 }
186
187         } while (!icp_rm_try_update(icp, old_state, new_state));
188
189         return success;
190 }
191
192 static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
193                             u32 new_irq)
194 {
195         struct ics_irq_state *state;
196         struct kvmppc_ics *ics;
197         u32 reject;
198         u16 src;
199
200         /*
201          * This is used both for initial delivery of an interrupt and
202          * for subsequent rejection.
203          *
204          * Rejection can be racy vs. resends. We have evaluated the
205          * rejection in an atomic ICP transaction which is now complete,
206          * so potentially the ICP can already accept the interrupt again.
207          *
208          * So we need to retry the delivery. Essentially the reject path
209          * boils down to a failed delivery. Always.
210          *
211          * Now the interrupt could also have moved to a different target,
212          * thus we may need to re-do the ICP lookup as well
213          */
214
215  again:
216         /* Get the ICS state and lock it */
217         ics = kvmppc_xics_find_ics(xics, new_irq, &src);
218         if (!ics) {
219                 /* Unsafe increment, but this does not need to be accurate */
220                 xics->err_noics++;
221                 return;
222         }
223         state = &ics->irq_state[src];
224
225         /* Get a lock on the ICS */
226         arch_spin_lock(&ics->lock);
227
228         /* Get our server */
229         if (!icp || state->server != icp->server_num) {
230                 icp = kvmppc_xics_find_server(xics->kvm, state->server);
231                 if (!icp) {
232                         /* Unsafe increment again*/
233                         xics->err_noicp++;
234                         goto out;
235                 }
236         }
237
238         /* Clear the resend bit of that interrupt */
239         state->resend = 0;
240
241         /*
242          * If masked, bail out
243          *
244          * Note: PAPR doesn't mention anything about masked pending
245          * when doing a resend, only when doing a delivery.
246          *
247          * However that would have the effect of losing a masked
248          * interrupt that was rejected and isn't consistent with
249          * the whole masked_pending business which is about not
250          * losing interrupts that occur while masked.
251          *
252          * I don't differentiate normal deliveries and resends, this
253          * implementation will differ from PAPR and not lose such
254          * interrupts.
255          */
256         if (state->priority == MASKED) {
257                 state->masked_pending = 1;
258                 goto out;
259         }
260
261         /*
262          * Try the delivery, this will set the need_resend flag
263          * in the ICP as part of the atomic transaction if the
264          * delivery is not possible.
265          *
266          * Note that if successful, the new delivery might have itself
267          * rejected an interrupt that was "delivered" before we took the
268          * ics spin lock.
269          *
270          * In this case we do the whole sequence all over again for the
271          * new guy. We cannot assume that the rejected interrupt is less
272          * favored than the new one, and thus doesn't need to be delivered,
273          * because by the time we exit icp_rm_try_to_deliver() the target
274          * processor may well have already consumed & completed it, and thus
275          * the rejected interrupt might actually be already acceptable.
276          */
277         if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
278                 /*
279                  * Delivery was successful, did we reject somebody else ?
280                  */
281                 if (reject && reject != XICS_IPI) {
282                         arch_spin_unlock(&ics->lock);
283                         icp->n_reject++;
284                         new_irq = reject;
285                         goto again;
286                 }
287         } else {
288                 /*
289                  * We failed to deliver the interrupt we need to set the
290                  * resend map bit and mark the ICS state as needing a resend
291                  */
292                 set_bit(ics->icsid, icp->resend_map);
293                 state->resend = 1;
294
295                 /*
296                  * If the need_resend flag got cleared in the ICP some time
297                  * between icp_rm_try_to_deliver() atomic update and now, then
298                  * we know it might have missed the resend_map bit. So we
299                  * retry
300                  */
301                 smp_mb();
302                 if (!icp->state.need_resend) {
303                         arch_spin_unlock(&ics->lock);
304                         goto again;
305                 }
306         }
307  out:
308         arch_spin_unlock(&ics->lock);
309 }
310
311 static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
312                              u8 new_cppr)
313 {
314         union kvmppc_icp_state old_state, new_state;
315         bool resend;
316
317         /*
318          * This handles several related states in one operation:
319          *
320          * ICP State: Down_CPPR
321          *
322          * Load CPPR with new value and if the XISR is 0
323          * then check for resends:
324          *
325          * ICP State: Resend
326          *
327          * If MFRR is more favored than CPPR, check for IPIs
328          * and notify ICS of a potential resend. This is done
329          * asynchronously (when used in real mode, we will have
330          * to exit here).
331          *
332          * We do not handle the complete Check_IPI as documented
333          * here. In the PAPR, this state will be used for both
334          * Set_MFRR and Down_CPPR. However, we know that we aren't
335          * changing the MFRR state here so we don't need to handle
336          * the case of an MFRR causing a reject of a pending irq,
337          * this will have been handled when the MFRR was set in the
338          * first place.
339          *
340          * Thus we don't have to handle rejects, only resends.
341          *
342          * When implementing real mode for HV KVM, resend will lead to
343          * a H_TOO_HARD return and the whole transaction will be handled
344          * in virtual mode.
345          */
346         do {
347                 old_state = new_state = READ_ONCE(icp->state);
348
349                 /* Down_CPPR */
350                 new_state.cppr = new_cppr;
351
352                 /*
353                  * Cut down Resend / Check_IPI / IPI
354                  *
355                  * The logic is that we cannot have a pending interrupt
356                  * trumped by an IPI at this point (see above), so we
357                  * know that either the pending interrupt is already an
358                  * IPI (in which case we don't care to override it) or
359                  * it's either more favored than us or non existent
360                  */
361                 if (new_state.mfrr < new_cppr &&
362                     new_state.mfrr <= new_state.pending_pri) {
363                         new_state.pending_pri = new_state.mfrr;
364                         new_state.xisr = XICS_IPI;
365                 }
366
367                 /* Latch/clear resend bit */
368                 resend = new_state.need_resend;
369                 new_state.need_resend = 0;
370
371         } while (!icp_rm_try_update(icp, old_state, new_state));
372
373         /*
374          * Now handle resend checks. Those are asynchronous to the ICP
375          * state update in HW (ie bus transactions) so we can handle them
376          * separately here as well.
377          */
378         if (resend) {
379                 icp->n_check_resend++;
380                 icp_rm_check_resend(xics, icp);
381         }
382 }
383
384
385 unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
386 {
387         union kvmppc_icp_state old_state, new_state;
388         struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
389         struct kvmppc_icp *icp = vcpu->arch.icp;
390         u32 xirr;
391
392         if (!xics || !xics->real_mode)
393                 return H_TOO_HARD;
394
395         /* First clear the interrupt */
396         icp_rm_clr_vcpu_irq(icp->vcpu);
397
398         /*
399          * ICP State: Accept_Interrupt
400          *
401          * Return the pending interrupt (if any) along with the
402          * current CPPR, then clear the XISR & set CPPR to the
403          * pending priority
404          */
405         do {
406                 old_state = new_state = READ_ONCE(icp->state);
407
408                 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
409                 if (!old_state.xisr)
410                         break;
411                 new_state.cppr = new_state.pending_pri;
412                 new_state.pending_pri = 0xff;
413                 new_state.xisr = 0;
414
415         } while (!icp_rm_try_update(icp, old_state, new_state));
416
417         /* Return the result in GPR4 */
418         vcpu->arch.gpr[4] = xirr;
419
420         return check_too_hard(xics, icp);
421 }
422
423 int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
424                     unsigned long mfrr)
425 {
426         union kvmppc_icp_state old_state, new_state;
427         struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
428         struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
429         u32 reject;
430         bool resend;
431         bool local;
432
433         if (!xics || !xics->real_mode)
434                 return H_TOO_HARD;
435
436         local = this_icp->server_num == server;
437         if (local)
438                 icp = this_icp;
439         else
440                 icp = kvmppc_xics_find_server(vcpu->kvm, server);
441         if (!icp)
442                 return H_PARAMETER;
443
444         /*
445          * ICP state: Set_MFRR
446          *
447          * If the CPPR is more favored than the new MFRR, then
448          * nothing needs to be done as there can be no XISR to
449          * reject.
450          *
451          * ICP state: Check_IPI
452          *
453          * If the CPPR is less favored, then we might be replacing
454          * an interrupt, and thus need to possibly reject it.
455          *
456          * ICP State: IPI
457          *
458          * Besides rejecting any pending interrupts, we also
459          * update XISR and pending_pri to mark IPI as pending.
460          *
461          * PAPR does not describe this state, but if the MFRR is being
462          * made less favored than its earlier value, there might be
463          * a previously-rejected interrupt needing to be resent.
464          * Ideally, we would want to resend only if
465          *      prio(pending_interrupt) < mfrr &&
466          *      prio(pending_interrupt) < cppr
467          * where pending interrupt is the one that was rejected. But
468          * we don't have that state, so we simply trigger a resend
469          * whenever the MFRR is made less favored.
470          */
471         do {
472                 old_state = new_state = READ_ONCE(icp->state);
473
474                 /* Set_MFRR */
475                 new_state.mfrr = mfrr;
476
477                 /* Check_IPI */
478                 reject = 0;
479                 resend = false;
480                 if (mfrr < new_state.cppr) {
481                         /* Reject a pending interrupt if not an IPI */
482                         if (mfrr <= new_state.pending_pri) {
483                                 reject = new_state.xisr;
484                                 new_state.pending_pri = mfrr;
485                                 new_state.xisr = XICS_IPI;
486                         }
487                 }
488
489                 if (mfrr > old_state.mfrr) {
490                         resend = new_state.need_resend;
491                         new_state.need_resend = 0;
492                 }
493         } while (!icp_rm_try_update(icp, old_state, new_state));
494
495         /* Handle reject in real mode */
496         if (reject && reject != XICS_IPI) {
497                 this_icp->n_reject++;
498                 icp_rm_deliver_irq(xics, icp, reject);
499         }
500
501         /* Handle resends in real mode */
502         if (resend) {
503                 this_icp->n_check_resend++;
504                 icp_rm_check_resend(xics, icp);
505         }
506
507         return check_too_hard(xics, this_icp);
508 }
509
510 int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
511 {
512         union kvmppc_icp_state old_state, new_state;
513         struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
514         struct kvmppc_icp *icp = vcpu->arch.icp;
515         u32 reject;
516
517         if (!xics || !xics->real_mode)
518                 return H_TOO_HARD;
519
520         /*
521          * ICP State: Set_CPPR
522          *
523          * We can safely compare the new value with the current
524          * value outside of the transaction as the CPPR is only
525          * ever changed by the processor on itself
526          */
527         if (cppr > icp->state.cppr) {
528                 icp_rm_down_cppr(xics, icp, cppr);
529                 goto bail;
530         } else if (cppr == icp->state.cppr)
531                 return H_SUCCESS;
532
533         /*
534          * ICP State: Up_CPPR
535          *
536          * The processor is raising its priority, this can result
537          * in a rejection of a pending interrupt:
538          *
539          * ICP State: Reject_Current
540          *
541          * We can remove EE from the current processor, the update
542          * transaction will set it again if needed
543          */
544         icp_rm_clr_vcpu_irq(icp->vcpu);
545
546         do {
547                 old_state = new_state = READ_ONCE(icp->state);
548
549                 reject = 0;
550                 new_state.cppr = cppr;
551
552                 if (cppr <= new_state.pending_pri) {
553                         reject = new_state.xisr;
554                         new_state.xisr = 0;
555                         new_state.pending_pri = 0xff;
556                 }
557
558         } while (!icp_rm_try_update(icp, old_state, new_state));
559
560         /*
561          * Check for rejects. They are handled by doing a new delivery
562          * attempt (see comments in icp_rm_deliver_irq).
563          */
564         if (reject && reject != XICS_IPI) {
565                 icp->n_reject++;
566                 icp_rm_deliver_irq(xics, icp, reject);
567         }
568  bail:
569         return check_too_hard(xics, icp);
570 }
571
572 int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
573 {
574         struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
575         struct kvmppc_icp *icp = vcpu->arch.icp;
576         struct kvmppc_ics *ics;
577         struct ics_irq_state *state;
578         u32 irq = xirr & 0x00ffffff;
579         u16 src;
580
581         if (!xics || !xics->real_mode)
582                 return H_TOO_HARD;
583
584         /*
585          * ICP State: EOI
586          *
587          * Note: If EOI is incorrectly used by SW to lower the CPPR
588          * value (ie more favored), we do not check for rejection of
589          * a pending interrupt, this is a SW error and PAPR sepcifies
590          * that we don't have to deal with it.
591          *
592          * The sending of an EOI to the ICS is handled after the
593          * CPPR update
594          *
595          * ICP State: Down_CPPR which we handle
596          * in a separate function as it's shared with H_CPPR.
597          */
598         icp_rm_down_cppr(xics, icp, xirr >> 24);
599
600         /* IPIs have no EOI */
601         if (irq == XICS_IPI)
602                 goto bail;
603         /*
604          * EOI handling: If the interrupt is still asserted, we need to
605          * resend it. We can take a lockless "peek" at the ICS state here.
606          *
607          * "Message" interrupts will never have "asserted" set
608          */
609         ics = kvmppc_xics_find_ics(xics, irq, &src);
610         if (!ics)
611                 goto bail;
612         state = &ics->irq_state[src];
613
614         /* Still asserted, resend it */
615         if (state->asserted)
616                 icp_rm_deliver_irq(xics, icp, irq);
617
618         if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
619                 icp->rm_action |= XICS_RM_NOTIFY_EOI;
620                 icp->rm_eoied_irq = irq;
621         }
622  bail:
623         return check_too_hard(xics, icp);
624 }