Merge tag 'asoc-fix-v5.14-rc4' of https://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / arch / powerpc / sysdev / xive / common.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2016,2017 IBM Corporation.
4  */
5
6 #define pr_fmt(fmt) "xive: " fmt
7
8 #include <linux/types.h>
9 #include <linux/threads.h>
10 #include <linux/kernel.h>
11 #include <linux/irq.h>
12 #include <linux/debugfs.h>
13 #include <linux/smp.h>
14 #include <linux/interrupt.h>
15 #include <linux/seq_file.h>
16 #include <linux/init.h>
17 #include <linux/cpu.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/msi.h>
22 #include <linux/vmalloc.h>
23
24 #include <asm/debugfs.h>
25 #include <asm/prom.h>
26 #include <asm/io.h>
27 #include <asm/smp.h>
28 #include <asm/machdep.h>
29 #include <asm/irq.h>
30 #include <asm/errno.h>
31 #include <asm/xive.h>
32 #include <asm/xive-regs.h>
33 #include <asm/xmon.h>
34
35 #include "xive-internal.h"
36
37 #undef DEBUG_FLUSH
38 #undef DEBUG_ALL
39
40 #ifdef DEBUG_ALL
41 #define DBG_VERBOSE(fmt, ...)   pr_devel("cpu %d - " fmt, \
42                                          smp_processor_id(), ## __VA_ARGS__)
43 #else
44 #define DBG_VERBOSE(fmt...)     do { } while(0)
45 #endif
46
47 bool __xive_enabled;
48 EXPORT_SYMBOL_GPL(__xive_enabled);
49 bool xive_cmdline_disabled;
50
51 /* We use only one priority for now */
52 static u8 xive_irq_priority;
53
54 /* TIMA exported to KVM */
55 void __iomem *xive_tima;
56 EXPORT_SYMBOL_GPL(xive_tima);
57 u32 xive_tima_offset;
58
59 /* Backend ops */
60 static const struct xive_ops *xive_ops;
61
62 /* Our global interrupt domain */
63 static struct irq_domain *xive_irq_domain;
64
65 #ifdef CONFIG_SMP
66 /* The IPIs use the same logical irq number when on the same chip */
67 static struct xive_ipi_desc {
68         unsigned int irq;
69         char name[16];
70 } *xive_ipis;
71
72 /*
73  * Use early_cpu_to_node() for hot-plugged CPUs
74  */
75 static unsigned int xive_ipi_cpu_to_irq(unsigned int cpu)
76 {
77         return xive_ipis[early_cpu_to_node(cpu)].irq;
78 }
79 #endif
80
81 /* Xive state for each CPU */
82 static DEFINE_PER_CPU(struct xive_cpu *, xive_cpu);
83
84 /* An invalid CPU target */
85 #define XIVE_INVALID_TARGET     (-1)
86
87 /*
88  * Read the next entry in a queue, return its content if it's valid
89  * or 0 if there is no new entry.
90  *
91  * The queue pointer is moved forward unless "just_peek" is set
92  */
93 static u32 xive_read_eq(struct xive_q *q, bool just_peek)
94 {
95         u32 cur;
96
97         if (!q->qpage)
98                 return 0;
99         cur = be32_to_cpup(q->qpage + q->idx);
100
101         /* Check valid bit (31) vs current toggle polarity */
102         if ((cur >> 31) == q->toggle)
103                 return 0;
104
105         /* If consuming from the queue ... */
106         if (!just_peek) {
107                 /* Next entry */
108                 q->idx = (q->idx + 1) & q->msk;
109
110                 /* Wrap around: flip valid toggle */
111                 if (q->idx == 0)
112                         q->toggle ^= 1;
113         }
114         /* Mask out the valid bit (31) */
115         return cur & 0x7fffffff;
116 }
117
118 /*
119  * Scans all the queue that may have interrupts in them
120  * (based on "pending_prio") in priority order until an
121  * interrupt is found or all the queues are empty.
122  *
123  * Then updates the CPPR (Current Processor Priority
124  * Register) based on the most favored interrupt found
125  * (0xff if none) and return what was found (0 if none).
126  *
127  * If just_peek is set, return the most favored pending
128  * interrupt if any but don't update the queue pointers.
129  *
130  * Note: This function can operate generically on any number
131  * of queues (up to 8). The current implementation of the XIVE
132  * driver only uses a single queue however.
133  *
134  * Note2: This will also "flush" "the pending_count" of a queue
135  * into the "count" when that queue is observed to be empty.
136  * This is used to keep track of the amount of interrupts
137  * targetting a queue. When an interrupt is moved away from
138  * a queue, we only decrement that queue count once the queue
139  * has been observed empty to avoid races.
140  */
141 static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
142 {
143         u32 irq = 0;
144         u8 prio = 0;
145
146         /* Find highest pending priority */
147         while (xc->pending_prio != 0) {
148                 struct xive_q *q;
149
150                 prio = ffs(xc->pending_prio) - 1;
151                 DBG_VERBOSE("scan_irq: trying prio %d\n", prio);
152
153                 /* Try to fetch */
154                 irq = xive_read_eq(&xc->queue[prio], just_peek);
155
156                 /* Found something ? That's it */
157                 if (irq) {
158                         if (just_peek || irq_to_desc(irq))
159                                 break;
160                         /*
161                          * We should never get here; if we do then we must
162                          * have failed to synchronize the interrupt properly
163                          * when shutting it down.
164                          */
165                         pr_crit("xive: got interrupt %d without descriptor, dropping\n",
166                                 irq);
167                         WARN_ON(1);
168                         continue;
169                 }
170
171                 /* Clear pending bits */
172                 xc->pending_prio &= ~(1 << prio);
173
174                 /*
175                  * Check if the queue count needs adjusting due to
176                  * interrupts being moved away. See description of
177                  * xive_dec_target_count()
178                  */
179                 q = &xc->queue[prio];
180                 if (atomic_read(&q->pending_count)) {
181                         int p = atomic_xchg(&q->pending_count, 0);
182                         if (p) {
183                                 WARN_ON(p > atomic_read(&q->count));
184                                 atomic_sub(p, &q->count);
185                         }
186                 }
187         }
188
189         /* If nothing was found, set CPPR to 0xff */
190         if (irq == 0)
191                 prio = 0xff;
192
193         /* Update HW CPPR to match if necessary */
194         if (prio != xc->cppr) {
195                 DBG_VERBOSE("scan_irq: adjusting CPPR to %d\n", prio);
196                 xc->cppr = prio;
197                 out_8(xive_tima + xive_tima_offset + TM_CPPR, prio);
198         }
199
200         return irq;
201 }
202
203 /*
204  * This is used to perform the magic loads from an ESB
205  * described in xive-regs.h
206  */
207 static notrace u8 xive_esb_read(struct xive_irq_data *xd, u32 offset)
208 {
209         u64 val;
210
211         if (offset == XIVE_ESB_SET_PQ_10 && xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
212                 offset |= XIVE_ESB_LD_ST_MO;
213
214         if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
215                 val = xive_ops->esb_rw(xd->hw_irq, offset, 0, 0);
216         else
217                 val = in_be64(xd->eoi_mmio + offset);
218
219         return (u8)val;
220 }
221
222 static void xive_esb_write(struct xive_irq_data *xd, u32 offset, u64 data)
223 {
224         if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
225                 xive_ops->esb_rw(xd->hw_irq, offset, data, 1);
226         else
227                 out_be64(xd->eoi_mmio + offset, data);
228 }
229
230 #ifdef CONFIG_XMON
231 static notrace void xive_dump_eq(const char *name, struct xive_q *q)
232 {
233         u32 i0, i1, idx;
234
235         if (!q->qpage)
236                 return;
237         idx = q->idx;
238         i0 = be32_to_cpup(q->qpage + idx);
239         idx = (idx + 1) & q->msk;
240         i1 = be32_to_cpup(q->qpage + idx);
241         xmon_printf("%s idx=%d T=%d %08x %08x ...", name,
242                      q->idx, q->toggle, i0, i1);
243 }
244
245 notrace void xmon_xive_do_dump(int cpu)
246 {
247         struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
248
249         xmon_printf("CPU %d:", cpu);
250         if (xc) {
251                 xmon_printf("pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
252
253 #ifdef CONFIG_SMP
254                 {
255                         u64 val = xive_esb_read(&xc->ipi_data, XIVE_ESB_GET);
256
257                         xmon_printf("IPI=0x%08x PQ=%c%c ", xc->hw_ipi,
258                                     val & XIVE_ESB_VAL_P ? 'P' : '-',
259                                     val & XIVE_ESB_VAL_Q ? 'Q' : '-');
260                 }
261 #endif
262                 xive_dump_eq("EQ", &xc->queue[xive_irq_priority]);
263         }
264         xmon_printf("\n");
265 }
266
267 static struct irq_data *xive_get_irq_data(u32 hw_irq)
268 {
269         unsigned int irq = irq_find_mapping(xive_irq_domain, hw_irq);
270
271         return irq ? irq_get_irq_data(irq) : NULL;
272 }
273
274 int xmon_xive_get_irq_config(u32 hw_irq, struct irq_data *d)
275 {
276         int rc;
277         u32 target;
278         u8 prio;
279         u32 lirq;
280
281         rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
282         if (rc) {
283                 xmon_printf("IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
284                 return rc;
285         }
286
287         xmon_printf("IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
288                     hw_irq, target, prio, lirq);
289
290         if (!d)
291                 d = xive_get_irq_data(hw_irq);
292
293         if (d) {
294                 struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
295                 u64 val = xive_esb_read(xd, XIVE_ESB_GET);
296
297                 xmon_printf("flags=%c%c%c PQ=%c%c",
298                             xd->flags & XIVE_IRQ_FLAG_STORE_EOI ? 'S' : ' ',
299                             xd->flags & XIVE_IRQ_FLAG_LSI ? 'L' : ' ',
300                             xd->flags & XIVE_IRQ_FLAG_H_INT_ESB ? 'H' : ' ',
301                             val & XIVE_ESB_VAL_P ? 'P' : '-',
302                             val & XIVE_ESB_VAL_Q ? 'Q' : '-');
303         }
304
305         xmon_printf("\n");
306         return 0;
307 }
308
309 void xmon_xive_get_irq_all(void)
310 {
311         unsigned int i;
312         struct irq_desc *desc;
313
314         for_each_irq_desc(i, desc) {
315                 struct irq_data *d = irq_desc_get_irq_data(desc);
316                 unsigned int hwirq = (unsigned int)irqd_to_hwirq(d);
317
318                 if (d->domain == xive_irq_domain)
319                         xmon_xive_get_irq_config(hwirq, d);
320         }
321 }
322
323 #endif /* CONFIG_XMON */
324
325 static unsigned int xive_get_irq(void)
326 {
327         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
328         u32 irq;
329
330         /*
331          * This can be called either as a result of a HW interrupt or
332          * as a "replay" because EOI decided there was still something
333          * in one of the queues.
334          *
335          * First we perform an ACK cycle in order to update our mask
336          * of pending priorities. This will also have the effect of
337          * updating the CPPR to the most favored pending interrupts.
338          *
339          * In the future, if we have a way to differentiate a first
340          * entry (on HW interrupt) from a replay triggered by EOI,
341          * we could skip this on replays unless we soft-mask tells us
342          * that a new HW interrupt occurred.
343          */
344         xive_ops->update_pending(xc);
345
346         DBG_VERBOSE("get_irq: pending=%02x\n", xc->pending_prio);
347
348         /* Scan our queue(s) for interrupts */
349         irq = xive_scan_interrupts(xc, false);
350
351         DBG_VERBOSE("get_irq: got irq 0x%x, new pending=0x%02x\n",
352             irq, xc->pending_prio);
353
354         /* Return pending interrupt if any */
355         if (irq == XIVE_BAD_IRQ)
356                 return 0;
357         return irq;
358 }
359
360 /*
361  * After EOI'ing an interrupt, we need to re-check the queue
362  * to see if another interrupt is pending since multiple
363  * interrupts can coalesce into a single notification to the
364  * CPU.
365  *
366  * If we find that there is indeed more in there, we call
367  * force_external_irq_replay() to make Linux synthetize an
368  * external interrupt on the next call to local_irq_restore().
369  */
370 static void xive_do_queue_eoi(struct xive_cpu *xc)
371 {
372         if (xive_scan_interrupts(xc, true) != 0) {
373                 DBG_VERBOSE("eoi: pending=0x%02x\n", xc->pending_prio);
374                 force_external_irq_replay();
375         }
376 }
377
378 /*
379  * EOI an interrupt at the source. There are several methods
380  * to do this depending on the HW version and source type
381  */
382 static void xive_do_source_eoi(struct xive_irq_data *xd)
383 {
384         u8 eoi_val;
385
386         xd->stale_p = false;
387
388         /* If the XIVE supports the new "store EOI facility, use it */
389         if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI) {
390                 xive_esb_write(xd, XIVE_ESB_STORE_EOI, 0);
391                 return;
392         }
393
394         /*
395          * For LSIs, we use the "EOI cycle" special load rather than
396          * PQ bits, as they are automatically re-triggered in HW when
397          * still pending.
398          */
399         if (xd->flags & XIVE_IRQ_FLAG_LSI) {
400                 xive_esb_read(xd, XIVE_ESB_LOAD_EOI);
401                 return;
402         }
403
404         /*
405          * Otherwise, we use the special MMIO that does a clear of
406          * both P and Q and returns the old Q. This allows us to then
407          * do a re-trigger if Q was set rather than synthesizing an
408          * interrupt in software
409          */
410         eoi_val = xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
411         DBG_VERBOSE("eoi_val=%x\n", eoi_val);
412
413         /* Re-trigger if needed */
414         if ((eoi_val & XIVE_ESB_VAL_Q) && xd->trig_mmio)
415                 out_be64(xd->trig_mmio, 0);
416 }
417
418 /* irq_chip eoi callback, called with irq descriptor lock held */
419 static void xive_irq_eoi(struct irq_data *d)
420 {
421         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
422         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
423
424         DBG_VERBOSE("eoi_irq: irq=%d [0x%lx] pending=%02x\n",
425                     d->irq, irqd_to_hwirq(d), xc->pending_prio);
426
427         /*
428          * EOI the source if it hasn't been disabled and hasn't
429          * been passed-through to a KVM guest
430          */
431         if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d) &&
432             !(xd->flags & XIVE_IRQ_FLAG_NO_EOI))
433                 xive_do_source_eoi(xd);
434         else
435                 xd->stale_p = true;
436
437         /*
438          * Clear saved_p to indicate that it's no longer occupying
439          * a queue slot on the target queue
440          */
441         xd->saved_p = false;
442
443         /* Check for more work in the queue */
444         xive_do_queue_eoi(xc);
445 }
446
447 /*
448  * Helper used to mask and unmask an interrupt source.
449  */
450 static void xive_do_source_set_mask(struct xive_irq_data *xd,
451                                     bool mask)
452 {
453         u64 val;
454
455         /*
456          * If the interrupt had P set, it may be in a queue.
457          *
458          * We need to make sure we don't re-enable it until it
459          * has been fetched from that queue and EOId. We keep
460          * a copy of that P state and use it to restore the
461          * ESB accordingly on unmask.
462          */
463         if (mask) {
464                 val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
465                 if (!xd->stale_p && !!(val & XIVE_ESB_VAL_P))
466                         xd->saved_p = true;
467                 xd->stale_p = false;
468         } else if (xd->saved_p) {
469                 xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
470                 xd->saved_p = false;
471         } else {
472                 xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
473                 xd->stale_p = false;
474         }
475 }
476
477 /*
478  * Try to chose "cpu" as a new interrupt target. Increments
479  * the queue accounting for that target if it's not already
480  * full.
481  */
482 static bool xive_try_pick_target(int cpu)
483 {
484         struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
485         struct xive_q *q = &xc->queue[xive_irq_priority];
486         int max;
487
488         /*
489          * Calculate max number of interrupts in that queue.
490          *
491          * We leave a gap of 1 just in case...
492          */
493         max = (q->msk + 1) - 1;
494         return !!atomic_add_unless(&q->count, 1, max);
495 }
496
497 /*
498  * Un-account an interrupt for a target CPU. We don't directly
499  * decrement q->count since the interrupt might still be present
500  * in the queue.
501  *
502  * Instead increment a separate counter "pending_count" which
503  * will be substracted from "count" later when that CPU observes
504  * the queue to be empty.
505  */
506 static void xive_dec_target_count(int cpu)
507 {
508         struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
509         struct xive_q *q = &xc->queue[xive_irq_priority];
510
511         if (WARN_ON(cpu < 0 || !xc)) {
512                 pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
513                 return;
514         }
515
516         /*
517          * We increment the "pending count" which will be used
518          * to decrement the target queue count whenever it's next
519          * processed and found empty. This ensure that we don't
520          * decrement while we still have the interrupt there
521          * occupying a slot.
522          */
523         atomic_inc(&q->pending_count);
524 }
525
526 /* Find a tentative CPU target in a CPU mask */
527 static int xive_find_target_in_mask(const struct cpumask *mask,
528                                     unsigned int fuzz)
529 {
530         int cpu, first, num, i;
531
532         /* Pick up a starting point CPU in the mask based on  fuzz */
533         num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
534         first = fuzz % num;
535
536         /* Locate it */
537         cpu = cpumask_first(mask);
538         for (i = 0; i < first && cpu < nr_cpu_ids; i++)
539                 cpu = cpumask_next(cpu, mask);
540
541         /* Sanity check */
542         if (WARN_ON(cpu >= nr_cpu_ids))
543                 cpu = cpumask_first(cpu_online_mask);
544
545         /* Remember first one to handle wrap-around */
546         first = cpu;
547
548         /*
549          * Now go through the entire mask until we find a valid
550          * target.
551          */
552         do {
553                 /*
554                  * We re-check online as the fallback case passes us
555                  * an untested affinity mask
556                  */
557                 if (cpu_online(cpu) && xive_try_pick_target(cpu))
558                         return cpu;
559                 cpu = cpumask_next(cpu, mask);
560                 /* Wrap around */
561                 if (cpu >= nr_cpu_ids)
562                         cpu = cpumask_first(mask);
563         } while (cpu != first);
564
565         return -1;
566 }
567
568 /*
569  * Pick a target CPU for an interrupt. This is done at
570  * startup or if the affinity is changed in a way that
571  * invalidates the current target.
572  */
573 static int xive_pick_irq_target(struct irq_data *d,
574                                 const struct cpumask *affinity)
575 {
576         static unsigned int fuzz;
577         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
578         cpumask_var_t mask;
579         int cpu = -1;
580
581         /*
582          * If we have chip IDs, first we try to build a mask of
583          * CPUs matching the CPU and find a target in there
584          */
585         if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
586                 zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
587                 /* Build a mask of matching chip IDs */
588                 for_each_cpu_and(cpu, affinity, cpu_online_mask) {
589                         struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
590                         if (xc->chip_id == xd->src_chip)
591                                 cpumask_set_cpu(cpu, mask);
592                 }
593                 /* Try to find a target */
594                 if (cpumask_empty(mask))
595                         cpu = -1;
596                 else
597                         cpu = xive_find_target_in_mask(mask, fuzz++);
598                 free_cpumask_var(mask);
599                 if (cpu >= 0)
600                         return cpu;
601                 fuzz--;
602         }
603
604         /* No chip IDs, fallback to using the affinity mask */
605         return xive_find_target_in_mask(affinity, fuzz++);
606 }
607
608 static unsigned int xive_irq_startup(struct irq_data *d)
609 {
610         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
611         unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
612         int target, rc;
613
614         xd->saved_p = false;
615         xd->stale_p = false;
616         pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
617                  d->irq, hw_irq, d);
618
619 #ifdef CONFIG_PCI_MSI
620         /*
621          * The generic MSI code returns with the interrupt disabled on the
622          * card, using the MSI mask bits. Firmware doesn't appear to unmask
623          * at that level, so we do it here by hand.
624          */
625         if (irq_data_get_msi_desc(d))
626                 pci_msi_unmask_irq(d);
627 #endif
628
629         /* Pick a target */
630         target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
631         if (target == XIVE_INVALID_TARGET) {
632                 /* Try again breaking affinity */
633                 target = xive_pick_irq_target(d, cpu_online_mask);
634                 if (target == XIVE_INVALID_TARGET)
635                         return -ENXIO;
636                 pr_warn("irq %d started with broken affinity\n", d->irq);
637         }
638
639         /* Sanity check */
640         if (WARN_ON(target == XIVE_INVALID_TARGET ||
641                     target >= nr_cpu_ids))
642                 target = smp_processor_id();
643
644         xd->target = target;
645
646         /*
647          * Configure the logical number to be the Linux IRQ number
648          * and set the target queue
649          */
650         rc = xive_ops->configure_irq(hw_irq,
651                                      get_hard_smp_processor_id(target),
652                                      xive_irq_priority, d->irq);
653         if (rc)
654                 return rc;
655
656         /* Unmask the ESB */
657         xive_do_source_set_mask(xd, false);
658
659         return 0;
660 }
661
662 /* called with irq descriptor lock held */
663 static void xive_irq_shutdown(struct irq_data *d)
664 {
665         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
666         unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
667
668         pr_devel("xive_irq_shutdown: irq %d [0x%x] data @%p\n",
669                  d->irq, hw_irq, d);
670
671         if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
672                 return;
673
674         /* Mask the interrupt at the source */
675         xive_do_source_set_mask(xd, true);
676
677         /*
678          * Mask the interrupt in HW in the IVT/EAS and set the number
679          * to be the "bad" IRQ number
680          */
681         xive_ops->configure_irq(hw_irq,
682                                 get_hard_smp_processor_id(xd->target),
683                                 0xff, XIVE_BAD_IRQ);
684
685         xive_dec_target_count(xd->target);
686         xd->target = XIVE_INVALID_TARGET;
687 }
688
689 static void xive_irq_unmask(struct irq_data *d)
690 {
691         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
692
693         pr_devel("xive_irq_unmask: irq %d data @%p\n", d->irq, xd);
694
695         xive_do_source_set_mask(xd, false);
696 }
697
698 static void xive_irq_mask(struct irq_data *d)
699 {
700         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
701
702         pr_devel("xive_irq_mask: irq %d data @%p\n", d->irq, xd);
703
704         xive_do_source_set_mask(xd, true);
705 }
706
707 static int xive_irq_set_affinity(struct irq_data *d,
708                                  const struct cpumask *cpumask,
709                                  bool force)
710 {
711         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
712         unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
713         u32 target, old_target;
714         int rc = 0;
715
716         pr_devel("xive_irq_set_affinity: irq %d\n", d->irq);
717
718         /* Is this valid ? */
719         if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
720                 return -EINVAL;
721
722         /* Don't do anything if the interrupt isn't started */
723         if (!irqd_is_started(d))
724                 return IRQ_SET_MASK_OK;
725
726         /*
727          * If existing target is already in the new mask, and is
728          * online then do nothing.
729          */
730         if (xd->target != XIVE_INVALID_TARGET &&
731             cpu_online(xd->target) &&
732             cpumask_test_cpu(xd->target, cpumask))
733                 return IRQ_SET_MASK_OK;
734
735         /* Pick a new target */
736         target = xive_pick_irq_target(d, cpumask);
737
738         /* No target found */
739         if (target == XIVE_INVALID_TARGET)
740                 return -ENXIO;
741
742         /* Sanity check */
743         if (WARN_ON(target >= nr_cpu_ids))
744                 target = smp_processor_id();
745
746         old_target = xd->target;
747
748         /*
749          * Only configure the irq if it's not currently passed-through to
750          * a KVM guest
751          */
752         if (!irqd_is_forwarded_to_vcpu(d))
753                 rc = xive_ops->configure_irq(hw_irq,
754                                              get_hard_smp_processor_id(target),
755                                              xive_irq_priority, d->irq);
756         if (rc < 0) {
757                 pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
758                 return rc;
759         }
760
761         pr_devel("  target: 0x%x\n", target);
762         xd->target = target;
763
764         /* Give up previous target */
765         if (old_target != XIVE_INVALID_TARGET)
766             xive_dec_target_count(old_target);
767
768         return IRQ_SET_MASK_OK;
769 }
770
771 static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
772 {
773         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
774
775         /*
776          * We only support these. This has really no effect other than setting
777          * the corresponding descriptor bits mind you but those will in turn
778          * affect the resend function when re-enabling an edge interrupt.
779          *
780          * Set set the default to edge as explained in map().
781          */
782         if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
783                 flow_type = IRQ_TYPE_EDGE_RISING;
784
785         if (flow_type != IRQ_TYPE_EDGE_RISING &&
786             flow_type != IRQ_TYPE_LEVEL_LOW)
787                 return -EINVAL;
788
789         irqd_set_trigger_type(d, flow_type);
790
791         /*
792          * Double check it matches what the FW thinks
793          *
794          * NOTE: We don't know yet if the PAPR interface will provide
795          * the LSI vs MSI information apart from the device-tree so
796          * this check might have to move into an optional backend call
797          * that is specific to the native backend
798          */
799         if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
800             !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
801                 pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
802                         d->irq, (u32)irqd_to_hwirq(d),
803                         (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
804                         (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
805         }
806
807         return IRQ_SET_MASK_OK_NOCOPY;
808 }
809
810 static int xive_irq_retrigger(struct irq_data *d)
811 {
812         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
813
814         /* This should be only for MSIs */
815         if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
816                 return 0;
817
818         /*
819          * To perform a retrigger, we first set the PQ bits to
820          * 11, then perform an EOI.
821          */
822         xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
823         xive_do_source_eoi(xd);
824
825         return 1;
826 }
827
828 /*
829  * Caller holds the irq descriptor lock, so this won't be called
830  * concurrently with xive_get_irqchip_state on the same interrupt.
831  */
832 static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
833 {
834         struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
835         unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
836         int rc;
837         u8 pq;
838
839         /*
840          * This is called by KVM with state non-NULL for enabling
841          * pass-through or NULL for disabling it
842          */
843         if (state) {
844                 irqd_set_forwarded_to_vcpu(d);
845
846                 /* Set it to PQ=10 state to prevent further sends */
847                 pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
848                 if (!xd->stale_p) {
849                         xd->saved_p = !!(pq & XIVE_ESB_VAL_P);
850                         xd->stale_p = !xd->saved_p;
851                 }
852
853                 /* No target ? nothing to do */
854                 if (xd->target == XIVE_INVALID_TARGET) {
855                         /*
856                          * An untargetted interrupt should have been
857                          * also masked at the source
858                          */
859                         WARN_ON(xd->saved_p);
860
861                         return 0;
862                 }
863
864                 /*
865                  * If P was set, adjust state to PQ=11 to indicate
866                  * that a resend is needed for the interrupt to reach
867                  * the guest. Also remember the value of P.
868                  *
869                  * This also tells us that it's in flight to a host queue
870                  * or has already been fetched but hasn't been EOIed yet
871                  * by the host. This it's potentially using up a host
872                  * queue slot. This is important to know because as long
873                  * as this is the case, we must not hard-unmask it when
874                  * "returning" that interrupt to the host.
875                  *
876                  * This saved_p is cleared by the host EOI, when we know
877                  * for sure the queue slot is no longer in use.
878                  */
879                 if (xd->saved_p) {
880                         xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
881
882                         /*
883                          * Sync the XIVE source HW to ensure the interrupt
884                          * has gone through the EAS before we change its
885                          * target to the guest. That should guarantee us
886                          * that we *will* eventually get an EOI for it on
887                          * the host. Otherwise there would be a small window
888                          * for P to be seen here but the interrupt going
889                          * to the guest queue.
890                          */
891                         if (xive_ops->sync_source)
892                                 xive_ops->sync_source(hw_irq);
893                 }
894         } else {
895                 irqd_clr_forwarded_to_vcpu(d);
896
897                 /* No host target ? hard mask and return */
898                 if (xd->target == XIVE_INVALID_TARGET) {
899                         xive_do_source_set_mask(xd, true);
900                         return 0;
901                 }
902
903                 /*
904                  * Sync the XIVE source HW to ensure the interrupt
905                  * has gone through the EAS before we change its
906                  * target to the host.
907                  */
908                 if (xive_ops->sync_source)
909                         xive_ops->sync_source(hw_irq);
910
911                 /*
912                  * By convention we are called with the interrupt in
913                  * a PQ=10 or PQ=11 state, ie, it won't fire and will
914                  * have latched in Q whether there's a pending HW
915                  * interrupt or not.
916                  *
917                  * First reconfigure the target.
918                  */
919                 rc = xive_ops->configure_irq(hw_irq,
920                                              get_hard_smp_processor_id(xd->target),
921                                              xive_irq_priority, d->irq);
922                 if (rc)
923                         return rc;
924
925                 /*
926                  * Then if saved_p is not set, effectively re-enable the
927                  * interrupt with an EOI. If it is set, we know there is
928                  * still a message in a host queue somewhere that will be
929                  * EOId eventually.
930                  *
931                  * Note: We don't check irqd_irq_disabled(). Effectively,
932                  * we *will* let the irq get through even if masked if the
933                  * HW is still firing it in order to deal with the whole
934                  * saved_p business properly. If the interrupt triggers
935                  * while masked, the generic code will re-mask it anyway.
936                  */
937                 if (!xd->saved_p)
938                         xive_do_source_eoi(xd);
939
940         }
941         return 0;
942 }
943
944 /* Called with irq descriptor lock held. */
945 static int xive_get_irqchip_state(struct irq_data *data,
946                                   enum irqchip_irq_state which, bool *state)
947 {
948         struct xive_irq_data *xd = irq_data_get_irq_handler_data(data);
949         u8 pq;
950
951         switch (which) {
952         case IRQCHIP_STATE_ACTIVE:
953                 pq = xive_esb_read(xd, XIVE_ESB_GET);
954
955                 /*
956                  * The esb value being all 1's means we couldn't get
957                  * the PQ state of the interrupt through mmio. It may
958                  * happen, for example when querying a PHB interrupt
959                  * while the PHB is in an error state. We consider the
960                  * interrupt to be inactive in that case.
961                  */
962                 *state = (pq != XIVE_ESB_INVALID) && !xd->stale_p &&
963                         (xd->saved_p || !!(pq & XIVE_ESB_VAL_P));
964                 return 0;
965         default:
966                 return -EINVAL;
967         }
968 }
969
970 static struct irq_chip xive_irq_chip = {
971         .name = "XIVE-IRQ",
972         .irq_startup = xive_irq_startup,
973         .irq_shutdown = xive_irq_shutdown,
974         .irq_eoi = xive_irq_eoi,
975         .irq_mask = xive_irq_mask,
976         .irq_unmask = xive_irq_unmask,
977         .irq_set_affinity = xive_irq_set_affinity,
978         .irq_set_type = xive_irq_set_type,
979         .irq_retrigger = xive_irq_retrigger,
980         .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
981         .irq_get_irqchip_state = xive_get_irqchip_state,
982 };
983
984 bool is_xive_irq(struct irq_chip *chip)
985 {
986         return chip == &xive_irq_chip;
987 }
988 EXPORT_SYMBOL_GPL(is_xive_irq);
989
990 void xive_cleanup_irq_data(struct xive_irq_data *xd)
991 {
992         if (xd->eoi_mmio) {
993                 iounmap(xd->eoi_mmio);
994                 if (xd->eoi_mmio == xd->trig_mmio)
995                         xd->trig_mmio = NULL;
996                 xd->eoi_mmio = NULL;
997         }
998         if (xd->trig_mmio) {
999                 iounmap(xd->trig_mmio);
1000                 xd->trig_mmio = NULL;
1001         }
1002 }
1003 EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
1004
1005 static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
1006 {
1007         struct xive_irq_data *xd;
1008         int rc;
1009
1010         xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
1011         if (!xd)
1012                 return -ENOMEM;
1013         rc = xive_ops->populate_irq_data(hw, xd);
1014         if (rc) {
1015                 kfree(xd);
1016                 return rc;
1017         }
1018         xd->target = XIVE_INVALID_TARGET;
1019         irq_set_handler_data(virq, xd);
1020
1021         /*
1022          * Turn OFF by default the interrupt being mapped. A side
1023          * effect of this check is the mapping the ESB page of the
1024          * interrupt in the Linux address space. This prevents page
1025          * fault issues in the crash handler which masks all
1026          * interrupts.
1027          */
1028         xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
1029
1030         return 0;
1031 }
1032
1033 static void xive_irq_free_data(unsigned int virq)
1034 {
1035         struct xive_irq_data *xd = irq_get_handler_data(virq);
1036
1037         if (!xd)
1038                 return;
1039         irq_set_handler_data(virq, NULL);
1040         xive_cleanup_irq_data(xd);
1041         kfree(xd);
1042 }
1043
1044 #ifdef CONFIG_SMP
1045
1046 static void xive_cause_ipi(int cpu)
1047 {
1048         struct xive_cpu *xc;
1049         struct xive_irq_data *xd;
1050
1051         xc = per_cpu(xive_cpu, cpu);
1052
1053         DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
1054                     smp_processor_id(), cpu, xc->hw_ipi);
1055
1056         xd = &xc->ipi_data;
1057         if (WARN_ON(!xd->trig_mmio))
1058                 return;
1059         out_be64(xd->trig_mmio, 0);
1060 }
1061
1062 static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
1063 {
1064         return smp_ipi_demux();
1065 }
1066
1067 static void xive_ipi_eoi(struct irq_data *d)
1068 {
1069         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1070
1071         /* Handle possible race with unplug and drop stale IPIs */
1072         if (!xc)
1073                 return;
1074
1075         DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
1076                     d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
1077
1078         xive_do_source_eoi(&xc->ipi_data);
1079         xive_do_queue_eoi(xc);
1080 }
1081
1082 static void xive_ipi_do_nothing(struct irq_data *d)
1083 {
1084         /*
1085          * Nothing to do, we never mask/unmask IPIs, but the callback
1086          * has to exist for the struct irq_chip.
1087          */
1088 }
1089
1090 static struct irq_chip xive_ipi_chip = {
1091         .name = "XIVE-IPI",
1092         .irq_eoi = xive_ipi_eoi,
1093         .irq_mask = xive_ipi_do_nothing,
1094         .irq_unmask = xive_ipi_do_nothing,
1095 };
1096
1097 /*
1098  * IPIs are marked per-cpu. We use separate HW interrupts under the
1099  * hood but associated with the same "linux" interrupt
1100  */
1101 struct xive_ipi_alloc_info {
1102         irq_hw_number_t hwirq;
1103 };
1104
1105 static int xive_ipi_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1106                                      unsigned int nr_irqs, void *arg)
1107 {
1108         struct xive_ipi_alloc_info *info = arg;
1109         int i;
1110
1111         for (i = 0; i < nr_irqs; i++) {
1112                 irq_domain_set_info(domain, virq + i, info->hwirq + i, &xive_ipi_chip,
1113                                     domain->host_data, handle_percpu_irq,
1114                                     NULL, NULL);
1115         }
1116         return 0;
1117 }
1118
1119 static const struct irq_domain_ops xive_ipi_irq_domain_ops = {
1120         .alloc  = xive_ipi_irq_domain_alloc,
1121 };
1122
1123 static int __init xive_request_ipi(void)
1124 {
1125         struct fwnode_handle *fwnode;
1126         struct irq_domain *ipi_domain;
1127         unsigned int node;
1128         int ret = -ENOMEM;
1129
1130         fwnode = irq_domain_alloc_named_fwnode("XIVE-IPI");
1131         if (!fwnode)
1132                 goto out;
1133
1134         ipi_domain = irq_domain_create_linear(fwnode, nr_node_ids,
1135                                               &xive_ipi_irq_domain_ops, NULL);
1136         if (!ipi_domain)
1137                 goto out_free_fwnode;
1138
1139         xive_ipis = kcalloc(nr_node_ids, sizeof(*xive_ipis), GFP_KERNEL | __GFP_NOFAIL);
1140         if (!xive_ipis)
1141                 goto out_free_domain;
1142
1143         for_each_node(node) {
1144                 struct xive_ipi_desc *xid = &xive_ipis[node];
1145                 struct xive_ipi_alloc_info info = { node };
1146
1147                 /* Skip nodes without CPUs */
1148                 if (cpumask_empty(cpumask_of_node(node)))
1149                         continue;
1150
1151                 /*
1152                  * Map one IPI interrupt per node for all cpus of that node.
1153                  * Since the HW interrupt number doesn't have any meaning,
1154                  * simply use the node number.
1155                  */
1156                 ret = irq_domain_alloc_irqs(ipi_domain, 1, node, &info);
1157                 if (ret < 0)
1158                         goto out_free_xive_ipis;
1159                 xid->irq = ret;
1160
1161                 snprintf(xid->name, sizeof(xid->name), "IPI-%d", node);
1162
1163                 ret = request_irq(xid->irq, xive_muxed_ipi_action,
1164                                   IRQF_PERCPU | IRQF_NO_THREAD, xid->name, NULL);
1165
1166                 WARN(ret < 0, "Failed to request IPI %d: %d\n", xid->irq, ret);
1167         }
1168
1169         return ret;
1170
1171 out_free_xive_ipis:
1172         kfree(xive_ipis);
1173 out_free_domain:
1174         irq_domain_remove(ipi_domain);
1175 out_free_fwnode:
1176         irq_domain_free_fwnode(fwnode);
1177 out:
1178         return ret;
1179 }
1180
1181 static int xive_setup_cpu_ipi(unsigned int cpu)
1182 {
1183         unsigned int xive_ipi_irq = xive_ipi_cpu_to_irq(cpu);
1184         struct xive_cpu *xc;
1185         int rc;
1186
1187         pr_debug("Setting up IPI for CPU %d\n", cpu);
1188
1189         xc = per_cpu(xive_cpu, cpu);
1190
1191         /* Check if we are already setup */
1192         if (xc->hw_ipi != XIVE_BAD_IRQ)
1193                 return 0;
1194
1195         /* Grab an IPI from the backend, this will populate xc->hw_ipi */
1196         if (xive_ops->get_ipi(cpu, xc))
1197                 return -EIO;
1198
1199         /*
1200          * Populate the IRQ data in the xive_cpu structure and
1201          * configure the HW / enable the IPIs.
1202          */
1203         rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
1204         if (rc) {
1205                 pr_err("Failed to populate IPI data on CPU %d\n", cpu);
1206                 return -EIO;
1207         }
1208         rc = xive_ops->configure_irq(xc->hw_ipi,
1209                                      get_hard_smp_processor_id(cpu),
1210                                      xive_irq_priority, xive_ipi_irq);
1211         if (rc) {
1212                 pr_err("Failed to map IPI CPU %d\n", cpu);
1213                 return -EIO;
1214         }
1215         pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
1216             xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
1217
1218         /* Unmask it */
1219         xive_do_source_set_mask(&xc->ipi_data, false);
1220
1221         return 0;
1222 }
1223
1224 static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
1225 {
1226         unsigned int xive_ipi_irq = xive_ipi_cpu_to_irq(cpu);
1227
1228         /* Disable the IPI and free the IRQ data */
1229
1230         /* Already cleaned up ? */
1231         if (xc->hw_ipi == XIVE_BAD_IRQ)
1232                 return;
1233
1234         /* Mask the IPI */
1235         xive_do_source_set_mask(&xc->ipi_data, true);
1236
1237         /*
1238          * Note: We don't call xive_cleanup_irq_data() to free
1239          * the mappings as this is called from an IPI on kexec
1240          * which is not a safe environment to call iounmap()
1241          */
1242
1243         /* Deconfigure/mask in the backend */
1244         xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
1245                                 0xff, xive_ipi_irq);
1246
1247         /* Free the IPIs in the backend */
1248         xive_ops->put_ipi(cpu, xc);
1249 }
1250
1251 void __init xive_smp_probe(void)
1252 {
1253         smp_ops->cause_ipi = xive_cause_ipi;
1254
1255         /* Register the IPI */
1256         xive_request_ipi();
1257
1258         /* Allocate and setup IPI for the boot CPU */
1259         xive_setup_cpu_ipi(smp_processor_id());
1260 }
1261
1262 #endif /* CONFIG_SMP */
1263
1264 static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
1265                                irq_hw_number_t hw)
1266 {
1267         int rc;
1268
1269         /*
1270          * Mark interrupts as edge sensitive by default so that resend
1271          * actually works. Will fix that up below if needed.
1272          */
1273         irq_clear_status_flags(virq, IRQ_LEVEL);
1274
1275         rc = xive_irq_alloc_data(virq, hw);
1276         if (rc)
1277                 return rc;
1278
1279         irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
1280
1281         return 0;
1282 }
1283
1284 static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
1285 {
1286         xive_irq_free_data(virq);
1287 }
1288
1289 static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
1290                                  const u32 *intspec, unsigned int intsize,
1291                                  irq_hw_number_t *out_hwirq, unsigned int *out_flags)
1292
1293 {
1294         *out_hwirq = intspec[0];
1295
1296         /*
1297          * If intsize is at least 2, we look for the type in the second cell,
1298          * we assume the LSB indicates a level interrupt.
1299          */
1300         if (intsize > 1) {
1301                 if (intspec[1] & 1)
1302                         *out_flags = IRQ_TYPE_LEVEL_LOW;
1303                 else
1304                         *out_flags = IRQ_TYPE_EDGE_RISING;
1305         } else
1306                 *out_flags = IRQ_TYPE_LEVEL_LOW;
1307
1308         return 0;
1309 }
1310
1311 static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
1312                                  enum irq_domain_bus_token bus_token)
1313 {
1314         return xive_ops->match(node);
1315 }
1316
1317 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1318 static const char * const esb_names[] = { "RESET", "OFF", "PENDING", "QUEUED" };
1319
1320 static const struct {
1321         u64  mask;
1322         char *name;
1323 } xive_irq_flags[] = {
1324         { XIVE_IRQ_FLAG_STORE_EOI, "STORE_EOI" },
1325         { XIVE_IRQ_FLAG_LSI,       "LSI"       },
1326         { XIVE_IRQ_FLAG_H_INT_ESB, "H_INT_ESB" },
1327         { XIVE_IRQ_FLAG_NO_EOI,    "NO_EOI"    },
1328 };
1329
1330 static void xive_irq_domain_debug_show(struct seq_file *m, struct irq_domain *d,
1331                                        struct irq_data *irqd, int ind)
1332 {
1333         struct xive_irq_data *xd;
1334         u64 val;
1335         int i;
1336
1337         /* No IRQ domain level information. To be done */
1338         if (!irqd)
1339                 return;
1340
1341         if (!is_xive_irq(irq_data_get_irq_chip(irqd)))
1342                 return;
1343
1344         seq_printf(m, "%*sXIVE:\n", ind, "");
1345         ind++;
1346
1347         xd = irq_data_get_irq_handler_data(irqd);
1348         if (!xd) {
1349                 seq_printf(m, "%*snot assigned\n", ind, "");
1350                 return;
1351         }
1352
1353         val = xive_esb_read(xd, XIVE_ESB_GET);
1354         seq_printf(m, "%*sESB:      %s\n", ind, "", esb_names[val & 0x3]);
1355         seq_printf(m, "%*sPstate:   %s %s\n", ind, "", xd->stale_p ? "stale" : "",
1356                    xd->saved_p ? "saved" : "");
1357         seq_printf(m, "%*sTarget:   %d\n", ind, "", xd->target);
1358         seq_printf(m, "%*sChip:     %d\n", ind, "", xd->src_chip);
1359         seq_printf(m, "%*sTrigger:  0x%016llx\n", ind, "", xd->trig_page);
1360         seq_printf(m, "%*sEOI:      0x%016llx\n", ind, "", xd->eoi_page);
1361         seq_printf(m, "%*sFlags:    0x%llx\n", ind, "", xd->flags);
1362         for (i = 0; i < ARRAY_SIZE(xive_irq_flags); i++) {
1363                 if (xd->flags & xive_irq_flags[i].mask)
1364                         seq_printf(m, "%*s%s\n", ind + 12, "", xive_irq_flags[i].name);
1365         }
1366 }
1367 #endif
1368
1369 static const struct irq_domain_ops xive_irq_domain_ops = {
1370         .match = xive_irq_domain_match,
1371         .map = xive_irq_domain_map,
1372         .unmap = xive_irq_domain_unmap,
1373         .xlate = xive_irq_domain_xlate,
1374 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1375         .debug_show = xive_irq_domain_debug_show,
1376 #endif
1377 };
1378
1379 static void __init xive_init_host(struct device_node *np)
1380 {
1381         xive_irq_domain = irq_domain_add_nomap(np, XIVE_MAX_IRQ,
1382                                                &xive_irq_domain_ops, NULL);
1383         if (WARN_ON(xive_irq_domain == NULL))
1384                 return;
1385         irq_set_default_host(xive_irq_domain);
1386 }
1387
1388 static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1389 {
1390         if (xc->queue[xive_irq_priority].qpage)
1391                 xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
1392 }
1393
1394 static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
1395 {
1396         int rc = 0;
1397
1398         /* We setup 1 queues for now with a 64k page */
1399         if (!xc->queue[xive_irq_priority].qpage)
1400                 rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
1401
1402         return rc;
1403 }
1404
1405 static int xive_prepare_cpu(unsigned int cpu)
1406 {
1407         struct xive_cpu *xc;
1408
1409         xc = per_cpu(xive_cpu, cpu);
1410         if (!xc) {
1411                 xc = kzalloc_node(sizeof(struct xive_cpu),
1412                                   GFP_KERNEL, cpu_to_node(cpu));
1413                 if (!xc)
1414                         return -ENOMEM;
1415                 xc->hw_ipi = XIVE_BAD_IRQ;
1416                 xc->chip_id = XIVE_INVALID_CHIP_ID;
1417                 if (xive_ops->prepare_cpu)
1418                         xive_ops->prepare_cpu(cpu, xc);
1419
1420                 per_cpu(xive_cpu, cpu) = xc;
1421         }
1422
1423         /* Setup EQs if not already */
1424         return xive_setup_cpu_queues(cpu, xc);
1425 }
1426
1427 static void xive_setup_cpu(void)
1428 {
1429         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1430
1431         /* The backend might have additional things to do */
1432         if (xive_ops->setup_cpu)
1433                 xive_ops->setup_cpu(smp_processor_id(), xc);
1434
1435         /* Set CPPR to 0xff to enable flow of interrupts */
1436         xc->cppr = 0xff;
1437         out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1438 }
1439
1440 #ifdef CONFIG_SMP
1441 void xive_smp_setup_cpu(void)
1442 {
1443         pr_devel("SMP setup CPU %d\n", smp_processor_id());
1444
1445         /* This will have already been done on the boot CPU */
1446         if (smp_processor_id() != boot_cpuid)
1447                 xive_setup_cpu();
1448
1449 }
1450
1451 int xive_smp_prepare_cpu(unsigned int cpu)
1452 {
1453         int rc;
1454
1455         /* Allocate per-CPU data and queues */
1456         rc = xive_prepare_cpu(cpu);
1457         if (rc)
1458                 return rc;
1459
1460         /* Allocate and setup IPI for the new CPU */
1461         return xive_setup_cpu_ipi(cpu);
1462 }
1463
1464 #ifdef CONFIG_HOTPLUG_CPU
1465 static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
1466 {
1467         u32 irq;
1468
1469         /* We assume local irqs are disabled */
1470         WARN_ON(!irqs_disabled());
1471
1472         /* Check what's already in the CPU queue */
1473         while ((irq = xive_scan_interrupts(xc, false)) != 0) {
1474                 /*
1475                  * We need to re-route that interrupt to its new destination.
1476                  * First get and lock the descriptor
1477                  */
1478                 struct irq_desc *desc = irq_to_desc(irq);
1479                 struct irq_data *d = irq_desc_get_irq_data(desc);
1480                 struct xive_irq_data *xd;
1481
1482                 /*
1483                  * Ignore anything that isn't a XIVE irq and ignore
1484                  * IPIs, so can just be dropped.
1485                  */
1486                 if (d->domain != xive_irq_domain)
1487                         continue;
1488
1489                 /*
1490                  * The IRQ should have already been re-routed, it's just a
1491                  * stale in the old queue, so re-trigger it in order to make
1492                  * it reach is new destination.
1493                  */
1494 #ifdef DEBUG_FLUSH
1495                 pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
1496                         cpu, irq);
1497 #endif
1498                 raw_spin_lock(&desc->lock);
1499                 xd = irq_desc_get_handler_data(desc);
1500
1501                 /*
1502                  * Clear saved_p to indicate that it's no longer pending
1503                  */
1504                 xd->saved_p = false;
1505
1506                 /*
1507                  * For LSIs, we EOI, this will cause a resend if it's
1508                  * still asserted. Otherwise do an MSI retrigger.
1509                  */
1510                 if (xd->flags & XIVE_IRQ_FLAG_LSI)
1511                         xive_do_source_eoi(xd);
1512                 else
1513                         xive_irq_retrigger(d);
1514
1515                 raw_spin_unlock(&desc->lock);
1516         }
1517 }
1518
1519 void xive_smp_disable_cpu(void)
1520 {
1521         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1522         unsigned int cpu = smp_processor_id();
1523
1524         /* Migrate interrupts away from the CPU */
1525         irq_migrate_all_off_this_cpu();
1526
1527         /* Set CPPR to 0 to disable flow of interrupts */
1528         xc->cppr = 0;
1529         out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1530
1531         /* Flush everything still in the queue */
1532         xive_flush_cpu_queue(cpu, xc);
1533
1534         /* Re-enable CPPR  */
1535         xc->cppr = 0xff;
1536         out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
1537 }
1538
1539 void xive_flush_interrupt(void)
1540 {
1541         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1542         unsigned int cpu = smp_processor_id();
1543
1544         /* Called if an interrupt occurs while the CPU is hot unplugged */
1545         xive_flush_cpu_queue(cpu, xc);
1546 }
1547
1548 #endif /* CONFIG_HOTPLUG_CPU */
1549
1550 #endif /* CONFIG_SMP */
1551
1552 void xive_teardown_cpu(void)
1553 {
1554         struct xive_cpu *xc = __this_cpu_read(xive_cpu);
1555         unsigned int cpu = smp_processor_id();
1556
1557         /* Set CPPR to 0 to disable flow of interrupts */
1558         xc->cppr = 0;
1559         out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
1560
1561         if (xive_ops->teardown_cpu)
1562                 xive_ops->teardown_cpu(cpu, xc);
1563
1564 #ifdef CONFIG_SMP
1565         /* Get rid of IPI */
1566         xive_cleanup_cpu_ipi(cpu, xc);
1567 #endif
1568
1569         /* Disable and free the queues */
1570         xive_cleanup_cpu_queues(cpu, xc);
1571 }
1572
1573 void xive_shutdown(void)
1574 {
1575         xive_ops->shutdown();
1576 }
1577
1578 bool __init xive_core_init(struct device_node *np, const struct xive_ops *ops,
1579                            void __iomem *area, u32 offset, u8 max_prio)
1580 {
1581         xive_tima = area;
1582         xive_tima_offset = offset;
1583         xive_ops = ops;
1584         xive_irq_priority = max_prio;
1585
1586         ppc_md.get_irq = xive_get_irq;
1587         __xive_enabled = true;
1588
1589         pr_devel("Initializing host..\n");
1590         xive_init_host(np);
1591
1592         pr_devel("Initializing boot CPU..\n");
1593
1594         /* Allocate per-CPU data and queues */
1595         xive_prepare_cpu(smp_processor_id());
1596
1597         /* Get ready for interrupts */
1598         xive_setup_cpu();
1599
1600         pr_info("Interrupt handling initialized with %s backend\n",
1601                 xive_ops->name);
1602         pr_info("Using priority %d for all interrupts\n", max_prio);
1603
1604         return true;
1605 }
1606
1607 __be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
1608 {
1609         unsigned int alloc_order;
1610         struct page *pages;
1611         __be32 *qpage;
1612
1613         alloc_order = xive_alloc_order(queue_shift);
1614         pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
1615         if (!pages)
1616                 return ERR_PTR(-ENOMEM);
1617         qpage = (__be32 *)page_address(pages);
1618         memset(qpage, 0, 1 << queue_shift);
1619
1620         return qpage;
1621 }
1622
1623 static int __init xive_off(char *arg)
1624 {
1625         xive_cmdline_disabled = true;
1626         return 0;
1627 }
1628 __setup("xive=off", xive_off);
1629
1630 static void xive_debug_show_cpu(struct seq_file *m, int cpu)
1631 {
1632         struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
1633
1634         seq_printf(m, "CPU %d:", cpu);
1635         if (xc) {
1636                 seq_printf(m, "pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
1637
1638 #ifdef CONFIG_SMP
1639                 {
1640                         u64 val = xive_esb_read(&xc->ipi_data, XIVE_ESB_GET);
1641
1642                         seq_printf(m, "IPI=0x%08x PQ=%c%c ", xc->hw_ipi,
1643                                    val & XIVE_ESB_VAL_P ? 'P' : '-',
1644                                    val & XIVE_ESB_VAL_Q ? 'Q' : '-');
1645                 }
1646 #endif
1647                 {
1648                         struct xive_q *q = &xc->queue[xive_irq_priority];
1649                         u32 i0, i1, idx;
1650
1651                         if (q->qpage) {
1652                                 idx = q->idx;
1653                                 i0 = be32_to_cpup(q->qpage + idx);
1654                                 idx = (idx + 1) & q->msk;
1655                                 i1 = be32_to_cpup(q->qpage + idx);
1656                                 seq_printf(m, "EQ idx=%d T=%d %08x %08x ...",
1657                                            q->idx, q->toggle, i0, i1);
1658                         }
1659                 }
1660         }
1661         seq_puts(m, "\n");
1662 }
1663
1664 static void xive_debug_show_irq(struct seq_file *m, struct irq_data *d)
1665 {
1666         unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1667         int rc;
1668         u32 target;
1669         u8 prio;
1670         u32 lirq;
1671         struct xive_irq_data *xd;
1672         u64 val;
1673
1674         rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
1675         if (rc) {
1676                 seq_printf(m, "IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
1677                 return;
1678         }
1679
1680         seq_printf(m, "IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
1681                    hw_irq, target, prio, lirq);
1682
1683         xd = irq_data_get_irq_handler_data(d);
1684         val = xive_esb_read(xd, XIVE_ESB_GET);
1685         seq_printf(m, "flags=%c%c%c PQ=%c%c",
1686                    xd->flags & XIVE_IRQ_FLAG_STORE_EOI ? 'S' : ' ',
1687                    xd->flags & XIVE_IRQ_FLAG_LSI ? 'L' : ' ',
1688                    xd->flags & XIVE_IRQ_FLAG_H_INT_ESB ? 'H' : ' ',
1689                    val & XIVE_ESB_VAL_P ? 'P' : '-',
1690                    val & XIVE_ESB_VAL_Q ? 'Q' : '-');
1691         seq_puts(m, "\n");
1692 }
1693
1694 static int xive_core_debug_show(struct seq_file *m, void *private)
1695 {
1696         unsigned int i;
1697         struct irq_desc *desc;
1698         int cpu;
1699
1700         if (xive_ops->debug_show)
1701                 xive_ops->debug_show(m, private);
1702
1703         for_each_possible_cpu(cpu)
1704                 xive_debug_show_cpu(m, cpu);
1705
1706         for_each_irq_desc(i, desc) {
1707                 struct irq_data *d = irq_desc_get_irq_data(desc);
1708
1709                 if (d->domain == xive_irq_domain)
1710                         xive_debug_show_irq(m, d);
1711         }
1712         return 0;
1713 }
1714 DEFINE_SHOW_ATTRIBUTE(xive_core_debug);
1715
1716 int xive_core_debug_init(void)
1717 {
1718         if (xive_enabled())
1719                 debugfs_create_file("xive", 0400, powerpc_debugfs_root,
1720                                     NULL, &xive_core_debug_fops);
1721         return 0;
1722 }