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