1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/kvm_host.h>
12#include <linux/err.h>
13#include <linux/kernel_stat.h>
14
15#include <asm/kvm_book3s.h>
16#include <asm/kvm_ppc.h>
17#include <asm/hvcall.h>
18#include <asm/xics.h>
19#include <asm/synch.h>
20#include <asm/cputhreads.h>
21#include <asm/pgtable.h>
22#include <asm/ppc-opcode.h>
23#include <asm/pnv-pci.h>
24#include <asm/opal.h>
25#include <asm/smp.h>
26
27#include "book3s_xics.h"
28
29#define DEBUG_PASSUP
30
31int h_ipi_redirect = 1;
32EXPORT_SYMBOL(h_ipi_redirect);
33int kvm_irq_bypass = 1;
34EXPORT_SYMBOL(kvm_irq_bypass);
35
36static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
37 u32 new_irq, bool check_resend);
38static int xics_opal_set_server(unsigned int hw_irq, int server_cpu);
39
40
41static void ics_rm_check_resend(struct kvmppc_xics *xics,
42 struct kvmppc_ics *ics, struct kvmppc_icp *icp)
43{
44 int i;
45
46 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
47 struct ics_irq_state *state = &ics->irq_state[i];
48 if (state->resend)
49 icp_rm_deliver_irq(xics, icp, state->number, true);
50 }
51
52}
53
54
55
56#ifdef CONFIG_SMP
57static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu)
58{
59 int hcpu;
60
61 hcpu = hcore << threads_shift;
62 kvmppc_host_rm_ops_hv->rm_core[hcore].rm_data = vcpu;
63 smp_muxed_ipi_set_message(hcpu, PPC_MSG_RM_HOST_ACTION);
64 kvmppc_set_host_ipi(hcpu);
65 smp_mb();
66 kvmhv_rm_send_ipi(hcpu);
67}
68#else
69static inline void icp_send_hcore_msg(int hcore, struct kvm_vcpu *vcpu) { }
70#endif
71
72
73
74
75
76
77
78
79
80
81
82
83
84static inline int grab_next_hostcore(int start,
85 struct kvmppc_host_rm_core *rm_core, int max, int action)
86{
87 bool success;
88 int core;
89 union kvmppc_rm_state old, new;
90
91 for (core = start + 1; core < max; core++) {
92 old = new = READ_ONCE(rm_core[core].rm_state);
93
94 if (!old.in_host || old.rm_action)
95 continue;
96
97
98 new.rm_action = action;
99
100 success = cmpxchg64(&rm_core[core].rm_state.raw,
101 old.raw, new.raw) == old.raw;
102 if (success) {
103
104
105
106
107
108
109 smp_wmb();
110 return core;
111 }
112 }
113
114 return -1;
115}
116
117static inline int find_available_hostcore(int action)
118{
119 int core;
120 int my_core = smp_processor_id() >> threads_shift;
121 struct kvmppc_host_rm_core *rm_core = kvmppc_host_rm_ops_hv->rm_core;
122
123 core = grab_next_hostcore(my_core, rm_core, cpu_nr_cores(), action);
124 if (core == -1)
125 core = grab_next_hostcore(core, rm_core, my_core, action);
126
127 return core;
128}
129
130static void icp_rm_set_vcpu_irq(struct kvm_vcpu *vcpu,
131 struct kvm_vcpu *this_vcpu)
132{
133 struct kvmppc_icp *this_icp = this_vcpu->arch.icp;
134 int cpu;
135 int hcore;
136
137
138 vcpu->stat.queue_intr++;
139 set_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
140
141
142 if (vcpu == this_vcpu) {
143 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_MER);
144 return;
145 }
146
147 if (xive_enabled() && kvmhv_on_pseries()) {
148
149 this_icp->rm_action |= XICS_RM_KICK_VCPU;
150 this_icp->rm_kick_target = vcpu;
151 return;
152 }
153
154
155
156
157
158
159 cpu = vcpu->arch.thread_cpu;
160 if (cpu < 0 || cpu >= nr_cpu_ids) {
161 hcore = -1;
162 if (kvmppc_host_rm_ops_hv && h_ipi_redirect)
163 hcore = find_available_hostcore(XICS_RM_KICK_VCPU);
164 if (hcore != -1) {
165 icp_send_hcore_msg(hcore, vcpu);
166 } else {
167 this_icp->rm_action |= XICS_RM_KICK_VCPU;
168 this_icp->rm_kick_target = vcpu;
169 }
170 return;
171 }
172
173 smp_mb();
174 kvmhv_rm_send_ipi(cpu);
175}
176
177static void icp_rm_clr_vcpu_irq(struct kvm_vcpu *vcpu)
178{
179
180 clear_bit(BOOK3S_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
181 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_MER);
182}
183
184static inline bool icp_rm_try_update(struct kvmppc_icp *icp,
185 union kvmppc_icp_state old,
186 union kvmppc_icp_state new)
187{
188 struct kvm_vcpu *this_vcpu = local_paca->kvm_hstate.kvm_vcpu;
189 bool success;
190
191
192 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
193
194
195 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
196 if (!success)
197 goto bail;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 if (new.out_ee)
215 icp_rm_set_vcpu_irq(icp->vcpu, this_vcpu);
216
217
218 this_vcpu->arch.icp->rm_dbgstate = new;
219 this_vcpu->arch.icp->rm_dbgtgt = icp->vcpu;
220
221 bail:
222 return success;
223}
224
225static inline int check_too_hard(struct kvmppc_xics *xics,
226 struct kvmppc_icp *icp)
227{
228 return (xics->real_mode_dbg || icp->rm_action) ? H_TOO_HARD : H_SUCCESS;
229}
230
231static void icp_rm_check_resend(struct kvmppc_xics *xics,
232 struct kvmppc_icp *icp)
233{
234 u32 icsid;
235
236
237 smp_rmb();
238 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
239 struct kvmppc_ics *ics = xics->ics[icsid];
240
241 if (!test_and_clear_bit(icsid, icp->resend_map))
242 continue;
243 if (!ics)
244 continue;
245 ics_rm_check_resend(xics, ics, icp);
246 }
247}
248
249static bool icp_rm_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
250 u32 *reject)
251{
252 union kvmppc_icp_state old_state, new_state;
253 bool success;
254
255 do {
256 old_state = new_state = READ_ONCE(icp->state);
257
258 *reject = 0;
259
260
261 success = new_state.cppr > priority &&
262 new_state.mfrr > priority &&
263 new_state.pending_pri > priority;
264
265
266
267
268
269 if (success) {
270 *reject = new_state.xisr;
271 new_state.xisr = irq;
272 new_state.pending_pri = priority;
273 } else {
274
275
276
277
278
279 new_state.need_resend = true;
280 }
281
282 } while (!icp_rm_try_update(icp, old_state, new_state));
283
284 return success;
285}
286
287static void icp_rm_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
288 u32 new_irq, bool check_resend)
289{
290 struct ics_irq_state *state;
291 struct kvmppc_ics *ics;
292 u32 reject;
293 u16 src;
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 again:
311
312 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
313 if (!ics) {
314
315 xics->err_noics++;
316 return;
317 }
318 state = &ics->irq_state[src];
319
320
321 arch_spin_lock(&ics->lock);
322
323
324 if (!icp || state->server != icp->server_num) {
325 icp = kvmppc_xics_find_server(xics->kvm, state->server);
326 if (!icp) {
327
328 xics->err_noicp++;
329 goto out;
330 }
331 }
332
333 if (check_resend)
334 if (!state->resend)
335 goto out;
336
337
338 state->resend = 0;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 if (state->priority == MASKED) {
356 state->masked_pending = 1;
357 goto out;
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 if (icp_rm_try_to_deliver(icp, new_irq, state->priority, &reject)) {
377
378
379
380 if (reject && reject != XICS_IPI) {
381 arch_spin_unlock(&ics->lock);
382 icp->n_reject++;
383 new_irq = reject;
384 check_resend = 0;
385 goto again;
386 }
387 } else {
388
389
390
391
392 state->resend = 1;
393
394
395
396
397
398 smp_wmb();
399 set_bit(ics->icsid, icp->resend_map);
400
401
402
403
404
405
406
407 smp_mb();
408 if (!icp->state.need_resend) {
409 state->resend = 0;
410 arch_spin_unlock(&ics->lock);
411 check_resend = 0;
412 goto again;
413 }
414 }
415 out:
416 arch_spin_unlock(&ics->lock);
417}
418
419static void icp_rm_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
420 u8 new_cppr)
421{
422 union kvmppc_icp_state old_state, new_state;
423 bool resend;
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 do {
455 old_state = new_state = READ_ONCE(icp->state);
456
457
458 new_state.cppr = new_cppr;
459
460
461
462
463
464
465
466
467
468
469 if (new_state.mfrr < new_cppr &&
470 new_state.mfrr <= new_state.pending_pri) {
471 new_state.pending_pri = new_state.mfrr;
472 new_state.xisr = XICS_IPI;
473 }
474
475
476 resend = new_state.need_resend;
477 new_state.need_resend = 0;
478
479 } while (!icp_rm_try_update(icp, old_state, new_state));
480
481
482
483
484
485
486 if (resend) {
487 icp->n_check_resend++;
488 icp_rm_check_resend(xics, icp);
489 }
490}
491
492
493unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu)
494{
495 union kvmppc_icp_state old_state, new_state;
496 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
497 struct kvmppc_icp *icp = vcpu->arch.icp;
498 u32 xirr;
499
500 if (!xics || !xics->real_mode)
501 return H_TOO_HARD;
502
503
504 icp_rm_clr_vcpu_irq(icp->vcpu);
505
506
507
508
509
510
511
512
513 do {
514 old_state = new_state = READ_ONCE(icp->state);
515
516 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
517 if (!old_state.xisr)
518 break;
519 new_state.cppr = new_state.pending_pri;
520 new_state.pending_pri = 0xff;
521 new_state.xisr = 0;
522
523 } while (!icp_rm_try_update(icp, old_state, new_state));
524
525
526 vcpu->arch.regs.gpr[4] = xirr;
527
528 return check_too_hard(xics, icp);
529}
530
531int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
532 unsigned long mfrr)
533{
534 union kvmppc_icp_state old_state, new_state;
535 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
536 struct kvmppc_icp *icp, *this_icp = vcpu->arch.icp;
537 u32 reject;
538 bool resend;
539 bool local;
540
541 if (!xics || !xics->real_mode)
542 return H_TOO_HARD;
543
544 local = this_icp->server_num == server;
545 if (local)
546 icp = this_icp;
547 else
548 icp = kvmppc_xics_find_server(vcpu->kvm, server);
549 if (!icp)
550 return H_PARAMETER;
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 do {
580 old_state = new_state = READ_ONCE(icp->state);
581
582
583 new_state.mfrr = mfrr;
584
585
586 reject = 0;
587 resend = false;
588 if (mfrr < new_state.cppr) {
589
590 if (mfrr <= new_state.pending_pri) {
591 reject = new_state.xisr;
592 new_state.pending_pri = mfrr;
593 new_state.xisr = XICS_IPI;
594 }
595 }
596
597 if (mfrr > old_state.mfrr) {
598 resend = new_state.need_resend;
599 new_state.need_resend = 0;
600 }
601 } while (!icp_rm_try_update(icp, old_state, new_state));
602
603
604 if (reject && reject != XICS_IPI) {
605 this_icp->n_reject++;
606 icp_rm_deliver_irq(xics, icp, reject, false);
607 }
608
609
610 if (resend) {
611 this_icp->n_check_resend++;
612 icp_rm_check_resend(xics, icp);
613 }
614
615 return check_too_hard(xics, this_icp);
616}
617
618int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
619{
620 union kvmppc_icp_state old_state, new_state;
621 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
622 struct kvmppc_icp *icp = vcpu->arch.icp;
623 u32 reject;
624
625 if (!xics || !xics->real_mode)
626 return H_TOO_HARD;
627
628
629
630
631
632
633
634
635 if (cppr > icp->state.cppr) {
636 icp_rm_down_cppr(xics, icp, cppr);
637 goto bail;
638 } else if (cppr == icp->state.cppr)
639 return H_SUCCESS;
640
641
642
643
644
645
646
647
648
649
650
651
652 icp_rm_clr_vcpu_irq(icp->vcpu);
653
654 do {
655 old_state = new_state = READ_ONCE(icp->state);
656
657 reject = 0;
658 new_state.cppr = cppr;
659
660 if (cppr <= new_state.pending_pri) {
661 reject = new_state.xisr;
662 new_state.xisr = 0;
663 new_state.pending_pri = 0xff;
664 }
665
666 } while (!icp_rm_try_update(icp, old_state, new_state));
667
668
669
670
671
672 if (reject && reject != XICS_IPI) {
673 icp->n_reject++;
674 icp_rm_deliver_irq(xics, icp, reject, false);
675 }
676 bail:
677 return check_too_hard(xics, icp);
678}
679
680static int ics_rm_eoi(struct kvm_vcpu *vcpu, u32 irq)
681{
682 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
683 struct kvmppc_icp *icp = vcpu->arch.icp;
684 struct kvmppc_ics *ics;
685 struct ics_irq_state *state;
686 u16 src;
687 u32 pq_old, pq_new;
688
689
690
691
692
693
694
695
696
697 ics = kvmppc_xics_find_ics(xics, irq, &src);
698 if (!ics)
699 goto bail;
700
701 state = &ics->irq_state[src];
702
703 if (state->lsi)
704 pq_new = state->pq_state;
705 else
706 do {
707 pq_old = state->pq_state;
708 pq_new = pq_old >> 1;
709 } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
710
711 if (pq_new & PQ_PRESENTED)
712 icp_rm_deliver_irq(xics, NULL, irq, false);
713
714 if (!hlist_empty(&vcpu->kvm->irq_ack_notifier_list)) {
715 icp->rm_action |= XICS_RM_NOTIFY_EOI;
716 icp->rm_eoied_irq = irq;
717 }
718
719 if (state->host_irq) {
720 ++vcpu->stat.pthru_all;
721 if (state->intr_cpu != -1) {
722 int pcpu = raw_smp_processor_id();
723
724 pcpu = cpu_first_thread_sibling(pcpu);
725 ++vcpu->stat.pthru_host;
726 if (state->intr_cpu != pcpu) {
727 ++vcpu->stat.pthru_bad_aff;
728 xics_opal_set_server(state->host_irq, pcpu);
729 }
730 state->intr_cpu = -1;
731 }
732 }
733
734 bail:
735 return check_too_hard(xics, icp);
736}
737
738int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
739{
740 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
741 struct kvmppc_icp *icp = vcpu->arch.icp;
742 u32 irq = xirr & 0x00ffffff;
743
744 if (!xics || !xics->real_mode)
745 return H_TOO_HARD;
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761 icp_rm_down_cppr(xics, icp, xirr >> 24);
762
763
764 if (irq == XICS_IPI)
765 return check_too_hard(xics, icp);
766
767 return ics_rm_eoi(vcpu, irq);
768}
769
770static unsigned long eoi_rc;
771
772static void icp_eoi(struct irq_chip *c, u32 hwirq, __be32 xirr, bool *again)
773{
774 void __iomem *xics_phys;
775 int64_t rc;
776
777 if (kvmhv_on_pseries()) {
778 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
779
780 iosync();
781 plpar_hcall_raw(H_EOI, retbuf, hwirq);
782 return;
783 }
784
785 rc = pnv_opal_pci_msi_eoi(c, hwirq);
786
787 if (rc)
788 eoi_rc = rc;
789
790 iosync();
791
792
793 xics_phys = local_paca->kvm_hstate.xics_phys;
794 if (xics_phys) {
795 __raw_rm_writel(xirr, xics_phys + XICS_XIRR);
796 } else {
797 rc = opal_int_eoi(be32_to_cpu(xirr));
798 *again = rc > 0;
799 }
800}
801
802static int xics_opal_set_server(unsigned int hw_irq, int server_cpu)
803{
804 unsigned int mangle_cpu = get_hard_smp_processor_id(server_cpu) << 2;
805
806 return opal_set_xive(hw_irq, mangle_cpu, DEFAULT_PRIORITY);
807}
808
809
810
811
812
813
814
815
816static inline void this_cpu_inc_rm(unsigned int __percpu *addr)
817{
818 unsigned long l;
819 unsigned int *raddr;
820 int cpu = smp_processor_id();
821
822 raddr = per_cpu_ptr(addr, cpu);
823 l = (unsigned long)raddr;
824
825 if (REGION_ID(l) == VMALLOC_REGION_ID) {
826 l = vmalloc_to_phys(raddr);
827 raddr = (unsigned int *)l;
828 }
829 ++*raddr;
830}
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850static void kvmppc_rm_handle_irq_desc(struct irq_desc *desc)
851{
852 this_cpu_inc_rm(desc->kstat_irqs);
853 __this_cpu_inc(kstat.irqs_sum);
854}
855
856long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu,
857 __be32 xirr,
858 struct kvmppc_irq_map *irq_map,
859 struct kvmppc_passthru_irqmap *pimap,
860 bool *again)
861{
862 struct kvmppc_xics *xics;
863 struct kvmppc_icp *icp;
864 struct kvmppc_ics *ics;
865 struct ics_irq_state *state;
866 u32 irq;
867 u16 src;
868 u32 pq_old, pq_new;
869
870 irq = irq_map->v_hwirq;
871 xics = vcpu->kvm->arch.xics;
872 icp = vcpu->arch.icp;
873
874 kvmppc_rm_handle_irq_desc(irq_map->desc);
875
876 ics = kvmppc_xics_find_ics(xics, irq, &src);
877 if (!ics)
878 return 2;
879
880 state = &ics->irq_state[src];
881
882
883 do {
884 pq_old = state->pq_state;
885 pq_new = ((pq_old << 1) & 3) | PQ_PRESENTED;
886 } while (cmpxchg(&state->pq_state, pq_old, pq_new) != pq_old);
887
888
889 if (pq_new == PQ_PRESENTED)
890 icp_rm_deliver_irq(xics, icp, irq, false);
891
892
893 icp_eoi(irq_desc_get_chip(irq_map->desc), irq_map->r_hwirq, xirr,
894 again);
895
896 if (check_too_hard(xics, icp) == H_TOO_HARD)
897 return 2;
898 else
899 return -2;
900}
901
902
903
904
905
906
907static void rm_host_ipi_action(int action, void *data)
908{
909 switch (action) {
910 case XICS_RM_KICK_VCPU:
911 kvmppc_host_rm_ops_hv->vcpu_kick(data);
912 break;
913 default:
914 WARN(1, "Unexpected rm_action=%d data=%p\n", action, data);
915 break;
916 }
917
918}
919
920void kvmppc_xics_ipi_action(void)
921{
922 int core;
923 unsigned int cpu = smp_processor_id();
924 struct kvmppc_host_rm_core *rm_corep;
925
926 core = cpu >> threads_shift;
927 rm_corep = &kvmppc_host_rm_ops_hv->rm_core[core];
928
929 if (rm_corep->rm_data) {
930 rm_host_ipi_action(rm_corep->rm_state.rm_action,
931 rm_corep->rm_data);
932
933 rm_corep->rm_data = NULL;
934 smp_wmb();
935 rm_corep->rm_state.rm_action = 0;
936 }
937}
938