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