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