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