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