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