1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Support for the interrupt controllers found on Power Macintosh, 4 * currently Apple's "Grand Central" interrupt controller in all 5 * it's incarnations. OpenPIC support used on newer machines is 6 * in a separate file 7 * 8 * Copyright (C) 1997 Paul Mackerras (paulus@samba.org) 9 * Copyright (C) 2005 Benjamin Herrenschmidt (benh@kernel.crashing.org) 10 * IBM, Corp. 11 */ 12 13 #include <linux/stddef.h> 14 #include <linux/init.h> 15 #include <linux/sched.h> 16 #include <linux/signal.h> 17 #include <linux/pci.h> 18 #include <linux/interrupt.h> 19 #include <linux/syscore_ops.h> 20 #include <linux/adb.h> 21 #include <linux/minmax.h> 22 #include <linux/pmu.h> 23 24 #include <asm/sections.h> 25 #include <asm/io.h> 26 #include <asm/smp.h> 27 #include <asm/prom.h> 28 #include <asm/pci-bridge.h> 29 #include <asm/time.h> 30 #include <asm/pmac_feature.h> 31 #include <asm/mpic.h> 32 #include <asm/xmon.h> 33 34 #include "pmac.h" 35 36 #ifdef CONFIG_PPC32 37 struct pmac_irq_hw { 38 unsigned int event; 39 unsigned int enable; 40 unsigned int ack; 41 unsigned int level; 42 }; 43 44 /* Workaround flags for 32bit powermac machines */ 45 unsigned int of_irq_workarounds; 46 struct device_node *of_irq_dflt_pic; 47 48 /* Default addresses */ 49 static volatile struct pmac_irq_hw __iomem *pmac_irq_hw[4]; 50 51 static int max_irqs; 52 static int max_real_irqs; 53 54 static DEFINE_RAW_SPINLOCK(pmac_pic_lock); 55 56 /* The max irq number this driver deals with is 128; see max_irqs */ 57 static DECLARE_BITMAP(ppc_lost_interrupts, 128); 58 static DECLARE_BITMAP(ppc_cached_irq_mask, 128); 59 static int pmac_irq_cascade = -1; 60 static struct irq_domain *pmac_pic_host; 61 62 static void __pmac_retrigger(unsigned int irq_nr) 63 { 64 if (irq_nr >= max_real_irqs && pmac_irq_cascade > 0) { 65 __set_bit(irq_nr, ppc_lost_interrupts); 66 irq_nr = pmac_irq_cascade; 67 mb(); 68 } 69 if (!__test_and_set_bit(irq_nr, ppc_lost_interrupts)) { 70 atomic_inc(&ppc_n_lost_interrupts); 71 set_dec(1); 72 } 73 } 74 75 static void pmac_mask_and_ack_irq(struct irq_data *d) 76 { 77 unsigned int src = irqd_to_hwirq(d); 78 unsigned long bit = 1UL << (src & 0x1f); 79 int i = src >> 5; 80 unsigned long flags; 81 82 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 83 __clear_bit(src, ppc_cached_irq_mask); 84 if (__test_and_clear_bit(src, ppc_lost_interrupts)) 85 atomic_dec(&ppc_n_lost_interrupts); 86 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]); 87 out_le32(&pmac_irq_hw[i]->ack, bit); 88 do { 89 /* make sure ack gets to controller before we enable 90 interrupts */ 91 mb(); 92 } while((in_le32(&pmac_irq_hw[i]->enable) & bit) 93 != (ppc_cached_irq_mask[i] & bit)); 94 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 95 } 96 97 static void pmac_ack_irq(struct irq_data *d) 98 { 99 unsigned int src = irqd_to_hwirq(d); 100 unsigned long bit = 1UL << (src & 0x1f); 101 int i = src >> 5; 102 unsigned long flags; 103 104 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 105 if (__test_and_clear_bit(src, ppc_lost_interrupts)) 106 atomic_dec(&ppc_n_lost_interrupts); 107 out_le32(&pmac_irq_hw[i]->ack, bit); 108 (void)in_le32(&pmac_irq_hw[i]->ack); 109 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 110 } 111 112 static void __pmac_set_irq_mask(unsigned int irq_nr, int nokicklost) 113 { 114 unsigned long bit = 1UL << (irq_nr & 0x1f); 115 int i = irq_nr >> 5; 116 117 if ((unsigned)irq_nr >= max_irqs) 118 return; 119 120 /* enable unmasked interrupts */ 121 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]); 122 123 do { 124 /* make sure mask gets to controller before we 125 return to user */ 126 mb(); 127 } while((in_le32(&pmac_irq_hw[i]->enable) & bit) 128 != (ppc_cached_irq_mask[i] & bit)); 129 130 /* 131 * Unfortunately, setting the bit in the enable register 132 * when the device interrupt is already on *doesn't* set 133 * the bit in the flag register or request another interrupt. 134 */ 135 if (bit & ppc_cached_irq_mask[i] & in_le32(&pmac_irq_hw[i]->level)) 136 __pmac_retrigger(irq_nr); 137 } 138 139 /* When an irq gets requested for the first client, if it's an 140 * edge interrupt, we clear any previous one on the controller 141 */ 142 static unsigned int pmac_startup_irq(struct irq_data *d) 143 { 144 unsigned long flags; 145 unsigned int src = irqd_to_hwirq(d); 146 unsigned long bit = 1UL << (src & 0x1f); 147 int i = src >> 5; 148 149 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 150 if (!irqd_is_level_type(d)) 151 out_le32(&pmac_irq_hw[i]->ack, bit); 152 __set_bit(src, ppc_cached_irq_mask); 153 __pmac_set_irq_mask(src, 0); 154 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 155 156 return 0; 157 } 158 159 static void pmac_mask_irq(struct irq_data *d) 160 { 161 unsigned long flags; 162 unsigned int src = irqd_to_hwirq(d); 163 164 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 165 __clear_bit(src, ppc_cached_irq_mask); 166 __pmac_set_irq_mask(src, 1); 167 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 168 } 169 170 static void pmac_unmask_irq(struct irq_data *d) 171 { 172 unsigned long flags; 173 unsigned int src = irqd_to_hwirq(d); 174 175 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 176 __set_bit(src, ppc_cached_irq_mask); 177 __pmac_set_irq_mask(src, 0); 178 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 179 } 180 181 static int pmac_retrigger(struct irq_data *d) 182 { 183 unsigned long flags; 184 185 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 186 __pmac_retrigger(irqd_to_hwirq(d)); 187 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 188 return 1; 189 } 190 191 static struct irq_chip pmac_pic = { 192 .name = "PMAC-PIC", 193 .irq_startup = pmac_startup_irq, 194 .irq_mask = pmac_mask_irq, 195 .irq_ack = pmac_ack_irq, 196 .irq_mask_ack = pmac_mask_and_ack_irq, 197 .irq_unmask = pmac_unmask_irq, 198 .irq_retrigger = pmac_retrigger, 199 }; 200 201 static irqreturn_t gatwick_action(int cpl, void *dev_id) 202 { 203 unsigned long flags; 204 int irq, bits; 205 int rc = IRQ_NONE; 206 207 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 208 for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) { 209 int i = irq >> 5; 210 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i]; 211 bits |= in_le32(&pmac_irq_hw[i]->level); 212 bits &= ppc_cached_irq_mask[i]; 213 if (bits == 0) 214 continue; 215 irq += __ilog2(bits); 216 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 217 generic_handle_irq(irq); 218 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 219 rc = IRQ_HANDLED; 220 } 221 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 222 return rc; 223 } 224 225 static unsigned int pmac_pic_get_irq(void) 226 { 227 int irq; 228 unsigned long bits = 0; 229 unsigned long flags; 230 231 #ifdef CONFIG_PPC_PMAC32_PSURGE 232 /* IPI's are a hack on the powersurge -- Cort */ 233 if (smp_processor_id() != 0) { 234 return psurge_secondary_virq; 235 } 236 #endif /* CONFIG_PPC_PMAC32_PSURGE */ 237 raw_spin_lock_irqsave(&pmac_pic_lock, flags); 238 for (irq = max_real_irqs; (irq -= 32) >= 0; ) { 239 int i = irq >> 5; 240 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i]; 241 bits |= in_le32(&pmac_irq_hw[i]->level); 242 bits &= ppc_cached_irq_mask[i]; 243 if (bits == 0) 244 continue; 245 irq += __ilog2(bits); 246 break; 247 } 248 raw_spin_unlock_irqrestore(&pmac_pic_lock, flags); 249 if (unlikely(irq < 0)) 250 return 0; 251 return irq_linear_revmap(pmac_pic_host, irq); 252 } 253 254 static int pmac_pic_host_match(struct irq_domain *h, struct device_node *node, 255 enum irq_domain_bus_token bus_token) 256 { 257 /* We match all, we don't always have a node anyway */ 258 return 1; 259 } 260 261 static int pmac_pic_host_map(struct irq_domain *h, unsigned int virq, 262 irq_hw_number_t hw) 263 { 264 if (hw >= max_irqs) 265 return -EINVAL; 266 267 /* Mark level interrupts, set delayed disable for edge ones and set 268 * handlers 269 */ 270 irq_set_status_flags(virq, IRQ_LEVEL); 271 irq_set_chip_and_handler(virq, &pmac_pic, handle_level_irq); 272 return 0; 273 } 274 275 static const struct irq_domain_ops pmac_pic_host_ops = { 276 .match = pmac_pic_host_match, 277 .map = pmac_pic_host_map, 278 .xlate = irq_domain_xlate_onecell, 279 }; 280 281 static void __init pmac_pic_probe_oldstyle(void) 282 { 283 int i; 284 struct device_node *master = NULL; 285 struct device_node *slave = NULL; 286 u8 __iomem *addr; 287 struct resource r; 288 289 /* Set our get_irq function */ 290 ppc_md.get_irq = pmac_pic_get_irq; 291 292 /* 293 * Find the interrupt controller type & node 294 */ 295 296 if ((master = of_find_node_by_name(NULL, "gc")) != NULL) { 297 max_irqs = max_real_irqs = 32; 298 } else if ((master = of_find_node_by_name(NULL, "ohare")) != NULL) { 299 max_irqs = max_real_irqs = 32; 300 /* We might have a second cascaded ohare */ 301 slave = of_find_node_by_name(NULL, "pci106b,7"); 302 if (slave) 303 max_irqs = 64; 304 } else if ((master = of_find_node_by_name(NULL, "mac-io")) != NULL) { 305 max_irqs = max_real_irqs = 64; 306 307 /* We might have a second cascaded heathrow */ 308 309 /* Compensate for of_node_put() in of_find_node_by_name() */ 310 of_node_get(master); 311 slave = of_find_node_by_name(master, "mac-io"); 312 313 /* Check ordering of master & slave */ 314 if (of_device_is_compatible(master, "gatwick")) { 315 BUG_ON(slave == NULL); 316 swap(master, slave); 317 } 318 319 /* We found a slave */ 320 if (slave) 321 max_irqs = 128; 322 } 323 BUG_ON(master == NULL); 324 325 /* 326 * Allocate an irq host 327 */ 328 pmac_pic_host = irq_domain_add_linear(master, max_irqs, 329 &pmac_pic_host_ops, NULL); 330 BUG_ON(pmac_pic_host == NULL); 331 irq_set_default_host(pmac_pic_host); 332 333 /* Get addresses of first controller if we have a node for it */ 334 BUG_ON(of_address_to_resource(master, 0, &r)); 335 336 /* Map interrupts of primary controller */ 337 addr = (u8 __iomem *) ioremap(r.start, 0x40); 338 i = 0; 339 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *) 340 (addr + 0x20); 341 if (max_real_irqs > 32) 342 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *) 343 (addr + 0x10); 344 of_node_put(master); 345 346 printk(KERN_INFO "irq: Found primary Apple PIC %pOF for %d irqs\n", 347 master, max_real_irqs); 348 349 /* Map interrupts of cascaded controller */ 350 if (slave && !of_address_to_resource(slave, 0, &r)) { 351 addr = (u8 __iomem *)ioremap(r.start, 0x40); 352 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *) 353 (addr + 0x20); 354 if (max_irqs > 64) 355 pmac_irq_hw[i++] = 356 (volatile struct pmac_irq_hw __iomem *) 357 (addr + 0x10); 358 pmac_irq_cascade = irq_of_parse_and_map(slave, 0); 359 360 printk(KERN_INFO "irq: Found slave Apple PIC %pOF for %d irqs" 361 " cascade: %d\n", slave, 362 max_irqs - max_real_irqs, pmac_irq_cascade); 363 } 364 of_node_put(slave); 365 366 /* Disable all interrupts in all controllers */ 367 for (i = 0; i * 32 < max_irqs; ++i) 368 out_le32(&pmac_irq_hw[i]->enable, 0); 369 370 /* Hookup cascade irq */ 371 if (slave && pmac_irq_cascade) { 372 if (request_irq(pmac_irq_cascade, gatwick_action, 373 IRQF_NO_THREAD, "cascade", NULL)) 374 pr_err("Failed to register cascade interrupt\n"); 375 } 376 377 printk(KERN_INFO "irq: System has %d possible interrupts\n", max_irqs); 378 #ifdef CONFIG_XMON 379 i = irq_create_mapping(NULL, 20); 380 if (request_irq(i, xmon_irq, IRQF_NO_THREAD, "NMI - XMON", NULL)) 381 pr_err("Failed to register NMI-XMON interrupt\n"); 382 #endif 383 } 384 385 int of_irq_parse_oldworld(struct device_node *device, int index, 386 struct of_phandle_args *out_irq) 387 { 388 const u32 *ints = NULL; 389 int intlen; 390 391 /* 392 * Old machines just have a list of interrupt numbers 393 * and no interrupt-controller nodes. We also have dodgy 394 * cases where the APPL,interrupts property is completely 395 * missing behind pci-pci bridges and we have to get it 396 * from the parent (the bridge itself, as apple just wired 397 * everything together on these) 398 */ 399 while (device) { 400 ints = of_get_property(device, "AAPL,interrupts", &intlen); 401 if (ints != NULL) 402 break; 403 device = device->parent; 404 if (!of_node_is_type(device, "pci")) 405 break; 406 } 407 if (ints == NULL) 408 return -EINVAL; 409 intlen /= sizeof(u32); 410 411 if (index >= intlen) 412 return -EINVAL; 413 414 out_irq->np = NULL; 415 out_irq->args[0] = ints[index]; 416 out_irq->args_count = 1; 417 418 return 0; 419 } 420 #endif /* CONFIG_PPC32 */ 421 422 static void __init pmac_pic_setup_mpic_nmi(struct mpic *mpic) 423 { 424 #if defined(CONFIG_XMON) && defined(CONFIG_PPC32) 425 struct device_node* pswitch; 426 int nmi_irq; 427 428 pswitch = of_find_node_by_name(NULL, "programmer-switch"); 429 if (pswitch) { 430 nmi_irq = irq_of_parse_and_map(pswitch, 0); 431 if (nmi_irq) { 432 mpic_irq_set_priority(nmi_irq, 9); 433 if (request_irq(nmi_irq, xmon_irq, IRQF_NO_THREAD, 434 "NMI - XMON", NULL)) 435 pr_err("Failed to register NMI-XMON interrupt\n"); 436 } 437 of_node_put(pswitch); 438 } 439 #endif /* defined(CONFIG_XMON) && defined(CONFIG_PPC32) */ 440 } 441 442 static struct mpic * __init pmac_setup_one_mpic(struct device_node *np, 443 int master) 444 { 445 const char *name = master ? " MPIC 1 " : " MPIC 2 "; 446 struct mpic *mpic; 447 unsigned int flags = master ? 0 : MPIC_SECONDARY; 448 449 pmac_call_feature(PMAC_FTR_ENABLE_MPIC, np, 0, 0); 450 451 if (of_get_property(np, "big-endian", NULL)) 452 flags |= MPIC_BIG_ENDIAN; 453 454 /* Primary Big Endian means HT interrupts. This is quite dodgy 455 * but works until I find a better way 456 */ 457 if (master && (flags & MPIC_BIG_ENDIAN)) 458 flags |= MPIC_U3_HT_IRQS; 459 460 mpic = mpic_alloc(np, 0, flags, 0, 0, name); 461 if (mpic == NULL) 462 return NULL; 463 464 mpic_init(mpic); 465 466 return mpic; 467 } 468 469 static int __init pmac_pic_probe_mpic(void) 470 { 471 struct mpic *mpic1, *mpic2; 472 struct device_node *np, *master = NULL, *slave = NULL; 473 474 /* We can have up to 2 MPICs cascaded */ 475 for_each_node_by_type(np, "open-pic") { 476 if (master == NULL && 477 of_get_property(np, "interrupts", NULL) == NULL) 478 master = of_node_get(np); 479 else if (slave == NULL) 480 slave = of_node_get(np); 481 if (master && slave) { 482 of_node_put(np); 483 break; 484 } 485 } 486 487 /* Check for bogus setups */ 488 if (master == NULL && slave != NULL) { 489 master = slave; 490 slave = NULL; 491 } 492 493 /* Not found, default to good old pmac pic */ 494 if (master == NULL) 495 return -ENODEV; 496 497 /* Set master handler */ 498 ppc_md.get_irq = mpic_get_irq; 499 500 /* Setup master */ 501 mpic1 = pmac_setup_one_mpic(master, 1); 502 BUG_ON(mpic1 == NULL); 503 504 /* Install NMI if any */ 505 pmac_pic_setup_mpic_nmi(mpic1); 506 507 of_node_put(master); 508 509 /* Set up a cascaded controller, if present */ 510 if (slave) { 511 mpic2 = pmac_setup_one_mpic(slave, 0); 512 if (mpic2 == NULL) 513 printk(KERN_ERR "Failed to setup slave MPIC\n"); 514 of_node_put(slave); 515 } 516 517 return 0; 518 } 519 520 521 void __init pmac_pic_init(void) 522 { 523 /* We configure the OF parsing based on our oldworld vs. newworld 524 * platform type and whether we were booted by BootX. 525 */ 526 #ifdef CONFIG_PPC32 527 if (!pmac_newworld) 528 of_irq_workarounds |= OF_IMAP_OLDWORLD_MAC; 529 if (of_get_property(of_chosen, "linux,bootx", NULL) != NULL) 530 of_irq_workarounds |= OF_IMAP_NO_PHANDLE; 531 532 /* If we don't have phandles on a newworld, then try to locate a 533 * default interrupt controller (happens when booting with BootX). 534 * We do a first match here, hopefully, that only ever happens on 535 * machines with one controller. 536 */ 537 if (pmac_newworld && (of_irq_workarounds & OF_IMAP_NO_PHANDLE)) { 538 struct device_node *np; 539 540 for_each_node_with_property(np, "interrupt-controller") { 541 /* Skip /chosen/interrupt-controller */ 542 if (of_node_name_eq(np, "chosen")) 543 continue; 544 /* It seems like at least one person wants 545 * to use BootX on a machine with an AppleKiwi 546 * controller which happens to pretend to be an 547 * interrupt controller too. */ 548 if (of_node_name_eq(np, "AppleKiwi")) 549 continue; 550 /* I think we found one ! */ 551 of_irq_dflt_pic = np; 552 break; 553 } 554 } 555 #endif /* CONFIG_PPC32 */ 556 557 /* We first try to detect Apple's new Core99 chipset, since mac-io 558 * is quite different on those machines and contains an IBM MPIC2. 559 */ 560 if (pmac_pic_probe_mpic() == 0) 561 return; 562 563 #ifdef CONFIG_PPC32 564 pmac_pic_probe_oldstyle(); 565 #endif 566 } 567 568 #if defined(CONFIG_PM) && defined(CONFIG_PPC32) 569 /* 570 * These procedures are used in implementing sleep on the powerbooks. 571 * sleep_save_intrs() saves the states of all interrupt enables 572 * and disables all interrupts except for the nominated one. 573 * sleep_restore_intrs() restores the states of all interrupt enables. 574 */ 575 unsigned long sleep_save_mask[2]; 576 577 /* This used to be passed by the PMU driver but that link got 578 * broken with the new driver model. We use this tweak for now... 579 * We really want to do things differently though... 580 */ 581 static int pmacpic_find_viaint(void) 582 { 583 int viaint = -1; 584 585 #ifdef CONFIG_ADB_PMU 586 struct device_node *np; 587 588 if (pmu_get_model() != PMU_OHARE_BASED) 589 goto not_found; 590 np = of_find_node_by_name(NULL, "via-pmu"); 591 if (np == NULL) 592 goto not_found; 593 viaint = irq_of_parse_and_map(np, 0); 594 of_node_put(np); 595 596 not_found: 597 #endif /* CONFIG_ADB_PMU */ 598 return viaint; 599 } 600 601 static int pmacpic_suspend(void) 602 { 603 int viaint = pmacpic_find_viaint(); 604 605 sleep_save_mask[0] = ppc_cached_irq_mask[0]; 606 sleep_save_mask[1] = ppc_cached_irq_mask[1]; 607 ppc_cached_irq_mask[0] = 0; 608 ppc_cached_irq_mask[1] = 0; 609 if (viaint > 0) 610 set_bit(viaint, ppc_cached_irq_mask); 611 out_le32(&pmac_irq_hw[0]->enable, ppc_cached_irq_mask[0]); 612 if (max_real_irqs > 32) 613 out_le32(&pmac_irq_hw[1]->enable, ppc_cached_irq_mask[1]); 614 (void)in_le32(&pmac_irq_hw[0]->event); 615 /* make sure mask gets to controller before we return to caller */ 616 mb(); 617 (void)in_le32(&pmac_irq_hw[0]->enable); 618 619 return 0; 620 } 621 622 static void pmacpic_resume(void) 623 { 624 int i; 625 626 out_le32(&pmac_irq_hw[0]->enable, 0); 627 if (max_real_irqs > 32) 628 out_le32(&pmac_irq_hw[1]->enable, 0); 629 mb(); 630 for (i = 0; i < max_real_irqs; ++i) 631 if (test_bit(i, sleep_save_mask)) 632 pmac_unmask_irq(irq_get_irq_data(i)); 633 } 634 635 static struct syscore_ops pmacpic_syscore_ops = { 636 .suspend = pmacpic_suspend, 637 .resume = pmacpic_resume, 638 }; 639 640 static int __init init_pmacpic_syscore(void) 641 { 642 if (pmac_irq_hw[0]) 643 register_syscore_ops(&pmacpic_syscore_ops); 644 return 0; 645 } 646 647 machine_subsys_initcall(powermac, init_pmacpic_syscore); 648 649 #endif /* CONFIG_PM && CONFIG_PPC32 */ 650