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