1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) "xive: " fmt
11
12#include <linux/types.h>
13#include <linux/threads.h>
14#include <linux/kernel.h>
15#include <linux/irq.h>
16#include <linux/debugfs.h>
17#include <linux/smp.h>
18#include <linux/interrupt.h>
19#include <linux/seq_file.h>
20#include <linux/init.h>
21#include <linux/cpu.h>
22#include <linux/of.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25#include <linux/msi.h>
26
27#include <asm/prom.h>
28#include <asm/io.h>
29#include <asm/smp.h>
30#include <asm/machdep.h>
31#include <asm/irq.h>
32#include <asm/errno.h>
33#include <asm/xive.h>
34#include <asm/xive-regs.h>
35#include <asm/xmon.h>
36
37#include "xive-internal.h"
38
39#undef DEBUG_FLUSH
40#undef DEBUG_ALL
41
42#ifdef DEBUG_ALL
43#define DBG_VERBOSE(fmt, ...) pr_devel("cpu %d - " fmt, \
44 smp_processor_id(), ## __VA_ARGS__)
45#else
46#define DBG_VERBOSE(fmt...) do { } while(0)
47#endif
48
49bool __xive_enabled;
50EXPORT_SYMBOL_GPL(__xive_enabled);
51bool xive_cmdline_disabled;
52
53
54static u8 xive_irq_priority;
55
56
57void __iomem *xive_tima;
58EXPORT_SYMBOL_GPL(xive_tima);
59u32 xive_tima_offset;
60
61
62static const struct xive_ops *xive_ops;
63
64
65static struct irq_domain *xive_irq_domain;
66
67#ifdef CONFIG_SMP
68
69static u32 xive_ipi_irq;
70#endif
71
72
73static DEFINE_PER_CPU(struct xive_cpu *, xive_cpu);
74
75
76
77
78
79#define XIVE_BAD_IRQ 0x7fffffff
80#define XIVE_MAX_IRQ (XIVE_BAD_IRQ - 1)
81
82
83#define XIVE_INVALID_TARGET (-1)
84
85
86
87
88
89
90
91static u32 xive_read_eq(struct xive_q *q, bool just_peek)
92{
93 u32 cur;
94
95 if (!q->qpage)
96 return 0;
97 cur = be32_to_cpup(q->qpage + q->idx);
98
99
100 if ((cur >> 31) == q->toggle)
101 return 0;
102
103
104 if (!just_peek) {
105
106 q->idx = (q->idx + 1) & q->msk;
107
108
109 if (q->idx == 0)
110 q->toggle ^= 1;
111 }
112
113 return cur & 0x7fffffff;
114}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
140{
141 u32 irq = 0;
142 u8 prio;
143
144
145 while (xc->pending_prio != 0) {
146 struct xive_q *q;
147
148 prio = ffs(xc->pending_prio) - 1;
149 DBG_VERBOSE("scan_irq: trying prio %d\n", prio);
150
151
152 irq = xive_read_eq(&xc->queue[prio], just_peek);
153
154
155 if (irq)
156 break;
157
158
159 xc->pending_prio &= ~(1 << prio);
160
161
162
163
164
165
166 q = &xc->queue[prio];
167 if (atomic_read(&q->pending_count)) {
168 int p = atomic_xchg(&q->pending_count, 0);
169 if (p) {
170 WARN_ON(p > atomic_read(&q->count));
171 atomic_sub(p, &q->count);
172 }
173 }
174 }
175
176
177 if (irq == 0)
178 prio = 0xff;
179
180
181 if (prio != xc->cppr) {
182 DBG_VERBOSE("scan_irq: adjusting CPPR to %d\n", prio);
183 xc->cppr = prio;
184 out_8(xive_tima + xive_tima_offset + TM_CPPR, prio);
185 }
186
187 return irq;
188}
189
190
191
192
193
194static notrace u8 xive_esb_read(struct xive_irq_data *xd, u32 offset)
195{
196 u64 val;
197
198
199 if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
200 offset |= offset << 4;
201
202 if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
203 val = xive_ops->esb_rw(xd->hw_irq, offset, 0, 0);
204 else
205 val = in_be64(xd->eoi_mmio + offset);
206
207 return (u8)val;
208}
209
210static void xive_esb_write(struct xive_irq_data *xd, u32 offset, u64 data)
211{
212
213 if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
214 offset |= offset << 4;
215
216 if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
217 xive_ops->esb_rw(xd->hw_irq, offset, data, 1);
218 else
219 out_be64(xd->eoi_mmio + offset, data);
220}
221
222#ifdef CONFIG_XMON
223static notrace void xive_dump_eq(const char *name, struct xive_q *q)
224{
225 u32 i0, i1, idx;
226
227 if (!q->qpage)
228 return;
229 idx = q->idx;
230 i0 = be32_to_cpup(q->qpage + idx);
231 idx = (idx + 1) & q->msk;
232 i1 = be32_to_cpup(q->qpage + idx);
233 xmon_printf(" %s Q T=%d %08x %08x ...\n", name,
234 q->toggle, i0, i1);
235}
236
237notrace void xmon_xive_do_dump(int cpu)
238{
239 struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
240
241 xmon_printf("XIVE state for CPU %d:\n", cpu);
242 xmon_printf(" pp=%02x cppr=%02x\n", xc->pending_prio, xc->cppr);
243 xive_dump_eq("IRQ", &xc->queue[xive_irq_priority]);
244#ifdef CONFIG_SMP
245 {
246 u64 val = xive_esb_read(&xc->ipi_data, XIVE_ESB_GET);
247 xmon_printf(" IPI state: %x:%c%c\n", xc->hw_ipi,
248 val & XIVE_ESB_VAL_P ? 'P' : 'p',
249 val & XIVE_ESB_VAL_P ? 'Q' : 'q');
250 }
251#endif
252}
253#endif
254
255static unsigned int xive_get_irq(void)
256{
257 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
258 u32 irq;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 xive_ops->update_pending(xc);
275
276 DBG_VERBOSE("get_irq: pending=%02x\n", xc->pending_prio);
277
278
279 irq = xive_scan_interrupts(xc, false);
280
281 DBG_VERBOSE("get_irq: got irq 0x%x, new pending=0x%02x\n",
282 irq, xc->pending_prio);
283
284
285 if (irq == XIVE_BAD_IRQ)
286 return 0;
287 return irq;
288}
289
290
291
292
293
294
295
296
297
298
299
300static void xive_do_queue_eoi(struct xive_cpu *xc)
301{
302 if (xive_scan_interrupts(xc, true) != 0) {
303 DBG_VERBOSE("eoi: pending=0x%02x\n", xc->pending_prio);
304 force_external_irq_replay();
305 }
306}
307
308
309
310
311
312void xive_do_source_eoi(u32 hw_irq, struct xive_irq_data *xd)
313{
314
315 if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
316 xive_esb_write(xd, XIVE_ESB_STORE_EOI, 0);
317 else if (hw_irq && xd->flags & XIVE_IRQ_FLAG_EOI_FW) {
318
319
320
321
322
323
324
325 if (WARN_ON_ONCE(!xive_ops->eoi))
326 return;
327 xive_ops->eoi(hw_irq);
328 } else {
329 u8 eoi_val;
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 if (xd->flags & XIVE_IRQ_FLAG_LSI)
345 xive_esb_read(xd, XIVE_ESB_LOAD_EOI);
346 else {
347 eoi_val = xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
348 DBG_VERBOSE("eoi_val=%x\n", eoi_val);
349
350
351 if ((eoi_val & XIVE_ESB_VAL_Q) && xd->trig_mmio)
352 out_be64(xd->trig_mmio, 0);
353 }
354 }
355}
356
357
358static void xive_irq_eoi(struct irq_data *d)
359{
360 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
361 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
362
363 DBG_VERBOSE("eoi_irq: irq=%d [0x%lx] pending=%02x\n",
364 d->irq, irqd_to_hwirq(d), xc->pending_prio);
365
366
367
368
369
370 if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d))
371 xive_do_source_eoi(irqd_to_hwirq(d), xd);
372
373
374
375
376
377 xd->saved_p = false;
378
379
380 xive_do_queue_eoi(xc);
381}
382
383
384
385
386
387
388static void xive_do_source_set_mask(struct xive_irq_data *xd,
389 bool mask)
390{
391 u64 val;
392
393
394
395
396
397
398
399
400
401 if (mask) {
402 val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
403 xd->saved_p = !!(val & XIVE_ESB_VAL_P);
404 } else if (xd->saved_p)
405 xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
406 else
407 xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
408}
409
410
411
412
413
414
415static bool xive_try_pick_target(int cpu)
416{
417 struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
418 struct xive_q *q = &xc->queue[xive_irq_priority];
419 int max;
420
421
422
423
424
425
426 max = (q->msk + 1) - 1;
427 return !!atomic_add_unless(&q->count, 1, max);
428}
429
430
431
432
433
434
435
436
437
438
439static void xive_dec_target_count(int cpu)
440{
441 struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
442 struct xive_q *q = &xc->queue[xive_irq_priority];
443
444 if (unlikely(WARN_ON(cpu < 0 || !xc))) {
445 pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
446 return;
447 }
448
449
450
451
452
453
454
455
456 atomic_inc(&q->pending_count);
457}
458
459
460static int xive_find_target_in_mask(const struct cpumask *mask,
461 unsigned int fuzz)
462{
463 int cpu, first, num, i;
464
465
466 num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
467 first = fuzz % num;
468
469
470 cpu = cpumask_first(mask);
471 for (i = 0; i < first && cpu < nr_cpu_ids; i++)
472 cpu = cpumask_next(cpu, mask);
473
474
475 if (WARN_ON(cpu >= nr_cpu_ids))
476 cpu = cpumask_first(cpu_online_mask);
477
478
479 first = cpu;
480
481
482
483
484
485 for (;;) {
486
487
488
489
490 if (cpu_online(cpu) && xive_try_pick_target(cpu))
491 return cpu;
492 cpu = cpumask_next(cpu, mask);
493 if (cpu == first)
494 break;
495
496 if (cpu >= nr_cpu_ids)
497 cpu = cpumask_first(mask);
498 }
499 return -1;
500}
501
502
503
504
505
506
507static int xive_pick_irq_target(struct irq_data *d,
508 const struct cpumask *affinity)
509{
510 static unsigned int fuzz;
511 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
512 cpumask_var_t mask;
513 int cpu = -1;
514
515
516
517
518
519 if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
520 zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
521
522 for_each_cpu_and(cpu, affinity, cpu_online_mask) {
523 struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
524 if (xc->chip_id == xd->src_chip)
525 cpumask_set_cpu(cpu, mask);
526 }
527
528 if (cpumask_empty(mask))
529 cpu = -1;
530 else
531 cpu = xive_find_target_in_mask(mask, fuzz++);
532 free_cpumask_var(mask);
533 if (cpu >= 0)
534 return cpu;
535 fuzz--;
536 }
537
538
539 return xive_find_target_in_mask(affinity, fuzz++);
540}
541
542static unsigned int xive_irq_startup(struct irq_data *d)
543{
544 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
545 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
546 int target, rc;
547
548 pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
549 d->irq, hw_irq, d);
550
551#ifdef CONFIG_PCI_MSI
552
553
554
555
556
557 if (irq_data_get_msi_desc(d))
558 pci_msi_unmask_irq(d);
559#endif
560
561
562 target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
563 if (target == XIVE_INVALID_TARGET) {
564
565 target = xive_pick_irq_target(d, cpu_online_mask);
566 if (target == XIVE_INVALID_TARGET)
567 return -ENXIO;
568 pr_warn("irq %d started with broken affinity\n", d->irq);
569 }
570
571
572 if (WARN_ON(target == XIVE_INVALID_TARGET ||
573 target >= nr_cpu_ids))
574 target = smp_processor_id();
575
576 xd->target = target;
577
578
579
580
581
582 rc = xive_ops->configure_irq(hw_irq,
583 get_hard_smp_processor_id(target),
584 xive_irq_priority, d->irq);
585 if (rc)
586 return rc;
587
588
589 xive_do_source_set_mask(xd, false);
590
591 return 0;
592}
593
594static void xive_irq_shutdown(struct irq_data *d)
595{
596 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
597 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
598
599 pr_devel("xive_irq_shutdown: irq %d [0x%x] data @%p\n",
600 d->irq, hw_irq, d);
601
602 if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
603 return;
604
605
606 xive_do_source_set_mask(xd, true);
607
608
609
610
611
612
613
614
615
616 xd->saved_p = false;
617
618
619
620
621
622 xive_ops->configure_irq(hw_irq,
623 get_hard_smp_processor_id(xd->target),
624 0xff, XIVE_BAD_IRQ);
625
626 xive_dec_target_count(xd->target);
627 xd->target = XIVE_INVALID_TARGET;
628}
629
630static void xive_irq_unmask(struct irq_data *d)
631{
632 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
633
634 pr_devel("xive_irq_unmask: irq %d data @%p\n", d->irq, xd);
635
636
637
638
639
640
641
642 if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
643 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
644 xive_ops->configure_irq(hw_irq,
645 get_hard_smp_processor_id(xd->target),
646 xive_irq_priority, d->irq);
647 return;
648 }
649
650 xive_do_source_set_mask(xd, false);
651}
652
653static void xive_irq_mask(struct irq_data *d)
654{
655 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
656
657 pr_devel("xive_irq_mask: irq %d data @%p\n", d->irq, xd);
658
659
660
661
662
663
664
665 if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
666 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
667 xive_ops->configure_irq(hw_irq,
668 get_hard_smp_processor_id(xd->target),
669 0xff, d->irq);
670 return;
671 }
672
673 xive_do_source_set_mask(xd, true);
674}
675
676static int xive_irq_set_affinity(struct irq_data *d,
677 const struct cpumask *cpumask,
678 bool force)
679{
680 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
681 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
682 u32 target, old_target;
683 int rc = 0;
684
685 pr_devel("xive_irq_set_affinity: irq %d\n", d->irq);
686
687
688 if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
689 return -EINVAL;
690
691
692 if (!irqd_is_started(d))
693 return IRQ_SET_MASK_OK;
694
695
696
697
698
699 if (xd->target != XIVE_INVALID_TARGET &&
700 cpu_online(xd->target) &&
701 cpumask_test_cpu(xd->target, cpumask))
702 return IRQ_SET_MASK_OK;
703
704
705 target = xive_pick_irq_target(d, cpumask);
706
707
708 if (target == XIVE_INVALID_TARGET)
709 return -ENXIO;
710
711
712 if (WARN_ON(target >= nr_cpu_ids))
713 target = smp_processor_id();
714
715 old_target = xd->target;
716
717
718
719
720
721 if (!irqd_is_forwarded_to_vcpu(d))
722 rc = xive_ops->configure_irq(hw_irq,
723 get_hard_smp_processor_id(target),
724 xive_irq_priority, d->irq);
725 if (rc < 0) {
726 pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
727 return rc;
728 }
729
730 pr_devel(" target: 0x%x\n", target);
731 xd->target = target;
732
733
734 if (old_target != XIVE_INVALID_TARGET)
735 xive_dec_target_count(old_target);
736
737 return IRQ_SET_MASK_OK;
738}
739
740static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
741{
742 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
743
744
745
746
747
748
749
750
751 if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
752 flow_type = IRQ_TYPE_EDGE_RISING;
753
754 if (flow_type != IRQ_TYPE_EDGE_RISING &&
755 flow_type != IRQ_TYPE_LEVEL_LOW)
756 return -EINVAL;
757
758 irqd_set_trigger_type(d, flow_type);
759
760
761
762
763
764
765
766
767
768 if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
769 !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
770 pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
771 d->irq, (u32)irqd_to_hwirq(d),
772 (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
773 (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
774 }
775
776 return IRQ_SET_MASK_OK_NOCOPY;
777}
778
779static int xive_irq_retrigger(struct irq_data *d)
780{
781 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
782
783
784 if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
785 return 0;
786
787
788
789
790
791 xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
792
793
794
795
796
797
798
799 xive_do_source_eoi(0, xd);
800
801 return 1;
802}
803
804static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
805{
806 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
807 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
808 int rc;
809 u8 pq;
810
811
812
813
814
815 if (xd->flags & XIVE_IRQ_FLAG_MASK_FW)
816 return -EIO;
817
818
819
820
821
822 if (state) {
823 irqd_set_forwarded_to_vcpu(d);
824
825
826 pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
827
828
829 if (xd->target == XIVE_INVALID_TARGET) {
830
831
832
833
834 WARN_ON(pq & 2);
835
836 return 0;
837 }
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854 if (pq & 2) {
855 pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
856 xd->saved_p = true;
857
858
859
860
861
862
863
864
865
866
867 if (xive_ops->sync_source)
868 xive_ops->sync_source(hw_irq);
869 } else
870 xd->saved_p = false;
871 } else {
872 irqd_clr_forwarded_to_vcpu(d);
873
874
875 if (xd->target == XIVE_INVALID_TARGET) {
876 xive_do_source_set_mask(xd, true);
877 return 0;
878 }
879
880
881
882
883
884
885 if (xive_ops->sync_source)
886 xive_ops->sync_source(hw_irq);
887
888
889
890
891
892
893
894
895
896 rc = xive_ops->configure_irq(hw_irq,
897 get_hard_smp_processor_id(xd->target),
898 xive_irq_priority, d->irq);
899 if (rc)
900 return rc;
901
902
903
904
905
906
907
908
909
910
911
912
913
914 if (!xd->saved_p)
915 xive_do_source_eoi(hw_irq, xd);
916
917 }
918 return 0;
919}
920
921static struct irq_chip xive_irq_chip = {
922 .name = "XIVE-IRQ",
923 .irq_startup = xive_irq_startup,
924 .irq_shutdown = xive_irq_shutdown,
925 .irq_eoi = xive_irq_eoi,
926 .irq_mask = xive_irq_mask,
927 .irq_unmask = xive_irq_unmask,
928 .irq_set_affinity = xive_irq_set_affinity,
929 .irq_set_type = xive_irq_set_type,
930 .irq_retrigger = xive_irq_retrigger,
931 .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
932};
933
934bool is_xive_irq(struct irq_chip *chip)
935{
936 return chip == &xive_irq_chip;
937}
938EXPORT_SYMBOL_GPL(is_xive_irq);
939
940void xive_cleanup_irq_data(struct xive_irq_data *xd)
941{
942 if (xd->eoi_mmio) {
943 iounmap(xd->eoi_mmio);
944 if (xd->eoi_mmio == xd->trig_mmio)
945 xd->trig_mmio = NULL;
946 xd->eoi_mmio = NULL;
947 }
948 if (xd->trig_mmio) {
949 iounmap(xd->trig_mmio);
950 xd->trig_mmio = NULL;
951 }
952}
953EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
954
955static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
956{
957 struct xive_irq_data *xd;
958 int rc;
959
960 xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
961 if (!xd)
962 return -ENOMEM;
963 rc = xive_ops->populate_irq_data(hw, xd);
964 if (rc) {
965 kfree(xd);
966 return rc;
967 }
968 xd->target = XIVE_INVALID_TARGET;
969 irq_set_handler_data(virq, xd);
970
971 return 0;
972}
973
974static void xive_irq_free_data(unsigned int virq)
975{
976 struct xive_irq_data *xd = irq_get_handler_data(virq);
977
978 if (!xd)
979 return;
980 irq_set_handler_data(virq, NULL);
981 xive_cleanup_irq_data(xd);
982 kfree(xd);
983}
984
985#ifdef CONFIG_SMP
986
987static void xive_cause_ipi(int cpu)
988{
989 struct xive_cpu *xc;
990 struct xive_irq_data *xd;
991
992 xc = per_cpu(xive_cpu, cpu);
993
994 DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
995 smp_processor_id(), cpu, xc->hw_ipi);
996
997 xd = &xc->ipi_data;
998 if (WARN_ON(!xd->trig_mmio))
999 return;
1000 out_be64(xd->trig_mmio, 0);
1001}
1002
1003static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
1004{
1005 return smp_ipi_demux();
1006}
1007
1008static void xive_ipi_eoi(struct irq_data *d)
1009{
1010 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1011
1012 DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
1013 d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
1014
1015
1016 if (!xc)
1017 return;
1018 xive_do_source_eoi(xc->hw_ipi, &xc->ipi_data);
1019 xive_do_queue_eoi(xc);
1020}
1021
1022static void xive_ipi_do_nothing(struct irq_data *d)
1023{
1024
1025
1026
1027
1028}
1029
1030static struct irq_chip xive_ipi_chip = {
1031 .name = "XIVE-IPI",
1032 .irq_eoi = xive_ipi_eoi,
1033 .irq_mask = xive_ipi_do_nothing,
1034 .irq_unmask = xive_ipi_do_nothing,
1035};
1036
1037static void __init xive_request_ipi(void)
1038{
1039 unsigned int virq;
1040
1041
1042
1043
1044
1045
1046 if (!xive_irq_domain)
1047 return;
1048
1049
1050 virq = irq_create_mapping(xive_irq_domain, 0);
1051 xive_ipi_irq = virq;
1052
1053 WARN_ON(request_irq(virq, xive_muxed_ipi_action,
1054 IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
1055}
1056
1057static int xive_setup_cpu_ipi(unsigned int cpu)
1058{
1059 struct xive_cpu *xc;
1060 int rc;
1061
1062 pr_debug("Setting up IPI for CPU %d\n", cpu);
1063
1064 xc = per_cpu(xive_cpu, cpu);
1065
1066
1067 if (xc->hw_ipi != 0)
1068 return 0;
1069
1070
1071 if (xive_ops->get_ipi(cpu, xc))
1072 return -EIO;
1073
1074
1075
1076
1077
1078 rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
1079 if (rc) {
1080 pr_err("Failed to populate IPI data on CPU %d\n", cpu);
1081 return -EIO;
1082 }
1083 rc = xive_ops->configure_irq(xc->hw_ipi,
1084 get_hard_smp_processor_id(cpu),
1085 xive_irq_priority, xive_ipi_irq);
1086 if (rc) {
1087 pr_err("Failed to map IPI CPU %d\n", cpu);
1088 return -EIO;
1089 }
1090 pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
1091 xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
1092
1093
1094 xive_do_source_set_mask(&xc->ipi_data, false);
1095
1096 return 0;
1097}
1098
1099static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
1100{
1101
1102
1103
1104 if (xc->hw_ipi == 0)
1105 return;
1106
1107
1108 xive_do_source_set_mask(&xc->ipi_data, true);
1109
1110
1111
1112
1113
1114
1115
1116
1117 xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
1118 0xff, xive_ipi_irq);
1119
1120
1121 xive_ops->put_ipi(cpu, xc);
1122}
1123
1124void __init xive_smp_probe(void)
1125{
1126 smp_ops->cause_ipi = xive_cause_ipi;
1127
1128
1129 xive_request_ipi();
1130
1131
1132 xive_setup_cpu_ipi(smp_processor_id());
1133}
1134
1135#endif
1136
1137static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
1138 irq_hw_number_t hw)
1139{
1140 int rc;
1141
1142
1143
1144
1145
1146 irq_clear_status_flags(virq, IRQ_LEVEL);
1147
1148#ifdef CONFIG_SMP
1149
1150 if (hw == 0) {
1151
1152
1153
1154
1155 irq_set_chip_and_handler(virq, &xive_ipi_chip,
1156 handle_percpu_irq);
1157 return 0;
1158 }
1159#endif
1160
1161 rc = xive_irq_alloc_data(virq, hw);
1162 if (rc)
1163 return rc;
1164
1165 irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
1166
1167 return 0;
1168}
1169
1170static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
1171{
1172 struct irq_data *data = irq_get_irq_data(virq);
1173 unsigned int hw_irq;
1174
1175
1176 if (!data)
1177 return;
1178 hw_irq = (unsigned int)irqd_to_hwirq(data);
1179 if (hw_irq)
1180 xive_irq_free_data(virq);
1181}
1182
1183static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
1184 const u32 *intspec, unsigned int intsize,
1185 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
1186
1187{
1188 *out_hwirq = intspec[0];
1189
1190
1191
1192
1193
1194 if (intsize > 1) {
1195 if (intspec[1] & 1)
1196 *out_flags = IRQ_TYPE_LEVEL_LOW;
1197 else
1198 *out_flags = IRQ_TYPE_EDGE_RISING;
1199 } else
1200 *out_flags = IRQ_TYPE_LEVEL_LOW;
1201
1202 return 0;
1203}
1204
1205static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
1206 enum irq_domain_bus_token bus_token)
1207{
1208 return xive_ops->match(node);
1209}
1210
1211static const struct irq_domain_ops xive_irq_domain_ops = {
1212 .match = xive_irq_domain_match,
1213 .map = xive_irq_domain_map,
1214 .unmap = xive_irq_domain_unmap,
1215 .xlate = xive_irq_domain_xlate,
1216};
1217
1218static void __init xive_init_host(void)
1219{
1220 xive_irq_domain = irq_domain_add_nomap(NULL, XIVE_MAX_IRQ,
1221 &xive_irq_domain_ops, NULL);
1222 if (WARN_ON(xive_irq_domain == NULL))
1223 return;
1224 irq_set_default_host(xive_irq_domain);
1225}
1226
1227static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1228{
1229 if (xc->queue[xive_irq_priority].qpage)
1230 xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
1231}
1232
1233static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1234{
1235 int rc = 0;
1236
1237
1238 if (!xc->queue[xive_irq_priority].qpage)
1239 rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
1240
1241 return rc;
1242}
1243
1244static int xive_prepare_cpu(unsigned int cpu)
1245{
1246 struct xive_cpu *xc;
1247
1248 xc = per_cpu(xive_cpu, cpu);
1249 if (!xc) {
1250 struct device_node *np;
1251
1252 xc = kzalloc_node(sizeof(struct xive_cpu),
1253 GFP_KERNEL, cpu_to_node(cpu));
1254 if (!xc)
1255 return -ENOMEM;
1256 np = of_get_cpu_node(cpu, NULL);
1257 if (np)
1258 xc->chip_id = of_get_ibm_chip_id(np);
1259 of_node_put(np);
1260
1261 per_cpu(xive_cpu, cpu) = xc;
1262 }
1263
1264
1265 return xive_setup_cpu_queues(cpu, xc);
1266}
1267
1268static void xive_setup_cpu(void)
1269{
1270 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1271
1272
1273 pr_devel("CPU %d [HW 0x%02x] VT=%02x\n",
1274 smp_processor_id(), hard_smp_processor_id(),
1275 in_8(xive_tima + xive_tima_offset + TM_WORD2));
1276
1277
1278 if (xive_ops->setup_cpu)
1279 xive_ops->setup_cpu(smp_processor_id(), xc);
1280
1281
1282 xc->cppr = 0xff;
1283 out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1284}
1285
1286#ifdef CONFIG_SMP
1287void xive_smp_setup_cpu(void)
1288{
1289 pr_devel("SMP setup CPU %d\n", smp_processor_id());
1290
1291
1292 if (smp_processor_id() != boot_cpuid)
1293 xive_setup_cpu();
1294
1295}
1296
1297int xive_smp_prepare_cpu(unsigned int cpu)
1298{
1299 int rc;
1300
1301
1302 rc = xive_prepare_cpu(cpu);
1303 if (rc)
1304 return rc;
1305
1306
1307 return xive_setup_cpu_ipi(cpu);
1308}
1309
1310#ifdef CONFIG_HOTPLUG_CPU
1311static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
1312{
1313 u32 irq;
1314
1315
1316 WARN_ON(!irqs_disabled());
1317
1318
1319 while ((irq = xive_scan_interrupts(xc, false)) != 0) {
1320
1321
1322
1323
1324 struct irq_desc *desc = irq_to_desc(irq);
1325 struct irq_data *d = irq_desc_get_irq_data(desc);
1326 struct xive_irq_data *xd;
1327 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1328
1329
1330
1331
1332
1333 if (d->domain != xive_irq_domain || hw_irq == 0)
1334 continue;
1335
1336
1337
1338
1339
1340
1341#ifdef DEBUG_FLUSH
1342 pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
1343 cpu, irq);
1344#endif
1345 raw_spin_lock(&desc->lock);
1346 xd = irq_desc_get_handler_data(desc);
1347
1348
1349
1350
1351
1352 if (xd->flags & XIVE_IRQ_FLAG_LSI)
1353 xive_do_source_eoi(irqd_to_hwirq(d), xd);
1354 else
1355 xive_irq_retrigger(d);
1356
1357 raw_spin_unlock(&desc->lock);
1358 }
1359}
1360
1361void xive_smp_disable_cpu(void)
1362{
1363 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1364 unsigned int cpu = smp_processor_id();
1365
1366
1367 irq_migrate_all_off_this_cpu();
1368
1369
1370 xc->cppr = 0;
1371 out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1372
1373
1374 xive_flush_cpu_queue(cpu, xc);
1375
1376
1377 xc->cppr = 0xff;
1378 out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1379}
1380
1381void xive_flush_interrupt(void)
1382{
1383 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1384 unsigned int cpu = smp_processor_id();
1385
1386
1387 xive_flush_cpu_queue(cpu, xc);
1388}
1389
1390#endif
1391
1392#endif
1393
1394void xive_teardown_cpu(void)
1395{
1396 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1397 unsigned int cpu = smp_processor_id();
1398
1399
1400 xc->cppr = 0;
1401 out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1402
1403 if (xive_ops->teardown_cpu)
1404 xive_ops->teardown_cpu(cpu, xc);
1405
1406#ifdef CONFIG_SMP
1407
1408 xive_cleanup_cpu_ipi(cpu, xc);
1409#endif
1410
1411
1412 xive_cleanup_cpu_queues(cpu, xc);
1413}
1414
1415void xive_kexec_teardown_cpu(int secondary)
1416{
1417 struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1418 unsigned int cpu = smp_processor_id();
1419
1420
1421 xc->cppr = 0;
1422 out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1423
1424
1425 if (xive_ops->teardown_cpu)
1426 xive_ops->teardown_cpu(cpu, xc);
1427
1428#ifdef CONFIG_SMP
1429
1430 xive_cleanup_cpu_ipi(cpu, xc);
1431#endif
1432
1433
1434 xive_cleanup_cpu_queues(cpu, xc);
1435}
1436
1437void xive_shutdown(void)
1438{
1439 xive_ops->shutdown();
1440}
1441
1442bool __init xive_core_init(const struct xive_ops *ops, void __iomem *area, u32 offset,
1443 u8 max_prio)
1444{
1445 xive_tima = area;
1446 xive_tima_offset = offset;
1447 xive_ops = ops;
1448 xive_irq_priority = max_prio;
1449
1450 ppc_md.get_irq = xive_get_irq;
1451 __xive_enabled = true;
1452
1453 pr_devel("Initializing host..\n");
1454 xive_init_host();
1455
1456 pr_devel("Initializing boot CPU..\n");
1457
1458
1459 xive_prepare_cpu(smp_processor_id());
1460
1461
1462 xive_setup_cpu();
1463
1464 pr_info("Interrupt handling initialized with %s backend\n",
1465 xive_ops->name);
1466 pr_info("Using priority %d for all interrupts\n", max_prio);
1467
1468 return true;
1469}
1470
1471__be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
1472{
1473 unsigned int alloc_order;
1474 struct page *pages;
1475 __be32 *qpage;
1476
1477 alloc_order = xive_alloc_order(queue_shift);
1478 pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
1479 if (!pages)
1480 return ERR_PTR(-ENOMEM);
1481 qpage = (__be32 *)page_address(pages);
1482 memset(qpage, 0, 1 << queue_shift);
1483
1484 return qpage;
1485}
1486
1487static int __init xive_off(char *arg)
1488{
1489 xive_cmdline_disabled = true;
1490 return 0;
1491}
1492__setup("xive=off", xive_off);
1493