18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/powerpc/sysdev/uic.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * IBM PowerPC 4xx Universal Interrupt Controller 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright 2007 David Gibson <dwg@au1.ibm.com>, IBM Corporation. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/errno.h> 128c2ecf20Sopenharmony_ci#include <linux/reboot.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/stddef.h> 158c2ecf20Sopenharmony_ci#include <linux/sched.h> 168c2ecf20Sopenharmony_ci#include <linux/signal.h> 178c2ecf20Sopenharmony_ci#include <linux/device.h> 188c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 198c2ecf20Sopenharmony_ci#include <linux/irq.h> 208c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 218c2ecf20Sopenharmony_ci#include <linux/kernel_stat.h> 228c2ecf20Sopenharmony_ci#include <asm/irq.h> 238c2ecf20Sopenharmony_ci#include <asm/io.h> 248c2ecf20Sopenharmony_ci#include <asm/prom.h> 258c2ecf20Sopenharmony_ci#include <asm/dcr.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define NR_UIC_INTS 32 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define UIC_SR 0x0 308c2ecf20Sopenharmony_ci#define UIC_ER 0x2 318c2ecf20Sopenharmony_ci#define UIC_CR 0x3 328c2ecf20Sopenharmony_ci#define UIC_PR 0x4 338c2ecf20Sopenharmony_ci#define UIC_TR 0x5 348c2ecf20Sopenharmony_ci#define UIC_MSR 0x6 358c2ecf20Sopenharmony_ci#define UIC_VR 0x7 368c2ecf20Sopenharmony_ci#define UIC_VCR 0x8 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistruct uic *primary_uic; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct uic { 418c2ecf20Sopenharmony_ci int index; 428c2ecf20Sopenharmony_ci int dcrbase; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci raw_spinlock_t lock; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci /* The remapper for this UIC */ 478c2ecf20Sopenharmony_ci struct irq_domain *irqhost; 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void uic_unmask_irq(struct irq_data *d) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct uic *uic = irq_data_get_irq_chip_data(d); 538c2ecf20Sopenharmony_ci unsigned int src = irqd_to_hwirq(d); 548c2ecf20Sopenharmony_ci unsigned long flags; 558c2ecf20Sopenharmony_ci u32 er, sr; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci sr = 1 << (31-src); 588c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&uic->lock, flags); 598c2ecf20Sopenharmony_ci /* ack level-triggered interrupts here */ 608c2ecf20Sopenharmony_ci if (irqd_is_level_type(d)) 618c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_SR, sr); 628c2ecf20Sopenharmony_ci er = mfdcr(uic->dcrbase + UIC_ER); 638c2ecf20Sopenharmony_ci er |= sr; 648c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_ER, er); 658c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&uic->lock, flags); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic void uic_mask_irq(struct irq_data *d) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct uic *uic = irq_data_get_irq_chip_data(d); 718c2ecf20Sopenharmony_ci unsigned int src = irqd_to_hwirq(d); 728c2ecf20Sopenharmony_ci unsigned long flags; 738c2ecf20Sopenharmony_ci u32 er; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&uic->lock, flags); 768c2ecf20Sopenharmony_ci er = mfdcr(uic->dcrbase + UIC_ER); 778c2ecf20Sopenharmony_ci er &= ~(1 << (31 - src)); 788c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_ER, er); 798c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&uic->lock, flags); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic void uic_ack_irq(struct irq_data *d) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci struct uic *uic = irq_data_get_irq_chip_data(d); 858c2ecf20Sopenharmony_ci unsigned int src = irqd_to_hwirq(d); 868c2ecf20Sopenharmony_ci unsigned long flags; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&uic->lock, flags); 898c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src)); 908c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&uic->lock, flags); 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic void uic_mask_ack_irq(struct irq_data *d) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct uic *uic = irq_data_get_irq_chip_data(d); 968c2ecf20Sopenharmony_ci unsigned int src = irqd_to_hwirq(d); 978c2ecf20Sopenharmony_ci unsigned long flags; 988c2ecf20Sopenharmony_ci u32 er, sr; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci sr = 1 << (31-src); 1018c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&uic->lock, flags); 1028c2ecf20Sopenharmony_ci er = mfdcr(uic->dcrbase + UIC_ER); 1038c2ecf20Sopenharmony_ci er &= ~sr; 1048c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_ER, er); 1058c2ecf20Sopenharmony_ci /* On the UIC, acking (i.e. clearing the SR bit) 1068c2ecf20Sopenharmony_ci * a level irq will have no effect if the interrupt 1078c2ecf20Sopenharmony_ci * is still asserted by the device, even if 1088c2ecf20Sopenharmony_ci * the interrupt is already masked. Therefore 1098c2ecf20Sopenharmony_ci * we only ack the egde interrupts here, while 1108c2ecf20Sopenharmony_ci * level interrupts are ack'ed after the actual 1118c2ecf20Sopenharmony_ci * isr call in the uic_unmask_irq() 1128c2ecf20Sopenharmony_ci */ 1138c2ecf20Sopenharmony_ci if (!irqd_is_level_type(d)) 1148c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_SR, sr); 1158c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&uic->lock, flags); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic int uic_set_irq_type(struct irq_data *d, unsigned int flow_type) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct uic *uic = irq_data_get_irq_chip_data(d); 1218c2ecf20Sopenharmony_ci unsigned int src = irqd_to_hwirq(d); 1228c2ecf20Sopenharmony_ci unsigned long flags; 1238c2ecf20Sopenharmony_ci int trigger, polarity; 1248c2ecf20Sopenharmony_ci u32 tr, pr, mask; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci switch (flow_type & IRQ_TYPE_SENSE_MASK) { 1278c2ecf20Sopenharmony_ci case IRQ_TYPE_NONE: 1288c2ecf20Sopenharmony_ci uic_mask_irq(d); 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci case IRQ_TYPE_EDGE_RISING: 1328c2ecf20Sopenharmony_ci trigger = 1; polarity = 1; 1338c2ecf20Sopenharmony_ci break; 1348c2ecf20Sopenharmony_ci case IRQ_TYPE_EDGE_FALLING: 1358c2ecf20Sopenharmony_ci trigger = 1; polarity = 0; 1368c2ecf20Sopenharmony_ci break; 1378c2ecf20Sopenharmony_ci case IRQ_TYPE_LEVEL_HIGH: 1388c2ecf20Sopenharmony_ci trigger = 0; polarity = 1; 1398c2ecf20Sopenharmony_ci break; 1408c2ecf20Sopenharmony_ci case IRQ_TYPE_LEVEL_LOW: 1418c2ecf20Sopenharmony_ci trigger = 0; polarity = 0; 1428c2ecf20Sopenharmony_ci break; 1438c2ecf20Sopenharmony_ci default: 1448c2ecf20Sopenharmony_ci return -EINVAL; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci mask = ~(1 << (31 - src)); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&uic->lock, flags); 1508c2ecf20Sopenharmony_ci tr = mfdcr(uic->dcrbase + UIC_TR); 1518c2ecf20Sopenharmony_ci pr = mfdcr(uic->dcrbase + UIC_PR); 1528c2ecf20Sopenharmony_ci tr = (tr & mask) | (trigger << (31-src)); 1538c2ecf20Sopenharmony_ci pr = (pr & mask) | (polarity << (31-src)); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_PR, pr); 1568c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_TR, tr); 1578c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_SR, ~mask); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&uic->lock, flags); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci return 0; 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic struct irq_chip uic_irq_chip = { 1658c2ecf20Sopenharmony_ci .name = "UIC", 1668c2ecf20Sopenharmony_ci .irq_unmask = uic_unmask_irq, 1678c2ecf20Sopenharmony_ci .irq_mask = uic_mask_irq, 1688c2ecf20Sopenharmony_ci .irq_mask_ack = uic_mask_ack_irq, 1698c2ecf20Sopenharmony_ci .irq_ack = uic_ack_irq, 1708c2ecf20Sopenharmony_ci .irq_set_type = uic_set_irq_type, 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic int uic_host_map(struct irq_domain *h, unsigned int virq, 1748c2ecf20Sopenharmony_ci irq_hw_number_t hw) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci struct uic *uic = h->host_data; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci irq_set_chip_data(virq, uic); 1798c2ecf20Sopenharmony_ci /* Despite the name, handle_level_irq() works for both level 1808c2ecf20Sopenharmony_ci * and edge irqs on UIC. FIXME: check this is correct */ 1818c2ecf20Sopenharmony_ci irq_set_chip_and_handler(virq, &uic_irq_chip, handle_level_irq); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* Set default irq type */ 1848c2ecf20Sopenharmony_ci irq_set_irq_type(virq, IRQ_TYPE_NONE); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic const struct irq_domain_ops uic_host_ops = { 1908c2ecf20Sopenharmony_ci .map = uic_host_map, 1918c2ecf20Sopenharmony_ci .xlate = irq_domain_xlate_twocell, 1928c2ecf20Sopenharmony_ci}; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic void uic_irq_cascade(struct irq_desc *desc) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci struct irq_chip *chip = irq_desc_get_chip(desc); 1978c2ecf20Sopenharmony_ci struct irq_data *idata = irq_desc_get_irq_data(desc); 1988c2ecf20Sopenharmony_ci struct uic *uic = irq_desc_get_handler_data(desc); 1998c2ecf20Sopenharmony_ci u32 msr; 2008c2ecf20Sopenharmony_ci int src; 2018c2ecf20Sopenharmony_ci int subvirq; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci raw_spin_lock(&desc->lock); 2048c2ecf20Sopenharmony_ci if (irqd_is_level_type(idata)) 2058c2ecf20Sopenharmony_ci chip->irq_mask(idata); 2068c2ecf20Sopenharmony_ci else 2078c2ecf20Sopenharmony_ci chip->irq_mask_ack(idata); 2088c2ecf20Sopenharmony_ci raw_spin_unlock(&desc->lock); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci msr = mfdcr(uic->dcrbase + UIC_MSR); 2118c2ecf20Sopenharmony_ci if (!msr) /* spurious interrupt */ 2128c2ecf20Sopenharmony_ci goto uic_irq_ret; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci src = 32 - ffs(msr); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci subvirq = irq_linear_revmap(uic->irqhost, src); 2178c2ecf20Sopenharmony_ci generic_handle_irq(subvirq); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciuic_irq_ret: 2208c2ecf20Sopenharmony_ci raw_spin_lock(&desc->lock); 2218c2ecf20Sopenharmony_ci if (irqd_is_level_type(idata)) 2228c2ecf20Sopenharmony_ci chip->irq_ack(idata); 2238c2ecf20Sopenharmony_ci if (!irqd_irq_disabled(idata) && chip->irq_unmask) 2248c2ecf20Sopenharmony_ci chip->irq_unmask(idata); 2258c2ecf20Sopenharmony_ci raw_spin_unlock(&desc->lock); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic struct uic * __init uic_init_one(struct device_node *node) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci struct uic *uic; 2318c2ecf20Sopenharmony_ci const u32 *indexp, *dcrreg; 2328c2ecf20Sopenharmony_ci int len; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci BUG_ON(! of_device_is_compatible(node, "ibm,uic")); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci uic = kzalloc(sizeof(*uic), GFP_KERNEL); 2378c2ecf20Sopenharmony_ci if (! uic) 2388c2ecf20Sopenharmony_ci return NULL; /* FIXME: panic? */ 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci raw_spin_lock_init(&uic->lock); 2418c2ecf20Sopenharmony_ci indexp = of_get_property(node, "cell-index", &len); 2428c2ecf20Sopenharmony_ci if (!indexp || (len != sizeof(u32))) { 2438c2ecf20Sopenharmony_ci printk(KERN_ERR "uic: Device node %pOF has missing or invalid " 2448c2ecf20Sopenharmony_ci "cell-index property\n", node); 2458c2ecf20Sopenharmony_ci return NULL; 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci uic->index = *indexp; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci dcrreg = of_get_property(node, "dcr-reg", &len); 2508c2ecf20Sopenharmony_ci if (!dcrreg || (len != 2*sizeof(u32))) { 2518c2ecf20Sopenharmony_ci printk(KERN_ERR "uic: Device node %pOF has missing or invalid " 2528c2ecf20Sopenharmony_ci "dcr-reg property\n", node); 2538c2ecf20Sopenharmony_ci return NULL; 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci uic->dcrbase = *dcrreg; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci uic->irqhost = irq_domain_add_linear(node, NR_UIC_INTS, &uic_host_ops, 2588c2ecf20Sopenharmony_ci uic); 2598c2ecf20Sopenharmony_ci if (! uic->irqhost) 2608c2ecf20Sopenharmony_ci return NULL; /* FIXME: panic? */ 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci /* Start with all interrupts disabled, level and non-critical */ 2638c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_ER, 0); 2648c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_CR, 0); 2658c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_TR, 0); 2668c2ecf20Sopenharmony_ci /* Clear any pending interrupts, in case the firmware left some */ 2678c2ecf20Sopenharmony_ci mtdcr(uic->dcrbase + UIC_SR, 0xffffffff); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index, 2708c2ecf20Sopenharmony_ci NR_UIC_INTS, uic->dcrbase); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci return uic; 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_civoid __init uic_init_tree(void) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci struct device_node *np; 2788c2ecf20Sopenharmony_ci struct uic *uic; 2798c2ecf20Sopenharmony_ci const u32 *interrupts; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* First locate and initialize the top-level UIC */ 2828c2ecf20Sopenharmony_ci for_each_compatible_node(np, NULL, "ibm,uic") { 2838c2ecf20Sopenharmony_ci interrupts = of_get_property(np, "interrupts", NULL); 2848c2ecf20Sopenharmony_ci if (!interrupts) 2858c2ecf20Sopenharmony_ci break; 2868c2ecf20Sopenharmony_ci } 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the 2898c2ecf20Sopenharmony_ci * top-level interrupt controller */ 2908c2ecf20Sopenharmony_ci primary_uic = uic_init_one(np); 2918c2ecf20Sopenharmony_ci if (!primary_uic) 2928c2ecf20Sopenharmony_ci panic("Unable to initialize primary UIC %pOF\n", np); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci irq_set_default_host(primary_uic->irqhost); 2958c2ecf20Sopenharmony_ci of_node_put(np); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci /* The scan again for cascaded UICs */ 2988c2ecf20Sopenharmony_ci for_each_compatible_node(np, NULL, "ibm,uic") { 2998c2ecf20Sopenharmony_ci interrupts = of_get_property(np, "interrupts", NULL); 3008c2ecf20Sopenharmony_ci if (interrupts) { 3018c2ecf20Sopenharmony_ci /* Secondary UIC */ 3028c2ecf20Sopenharmony_ci int cascade_virq; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci uic = uic_init_one(np); 3058c2ecf20Sopenharmony_ci if (! uic) 3068c2ecf20Sopenharmony_ci panic("Unable to initialize a secondary UIC %pOF\n", 3078c2ecf20Sopenharmony_ci np); 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci cascade_virq = irq_of_parse_and_map(np, 0); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci irq_set_handler_data(cascade_virq, uic); 3128c2ecf20Sopenharmony_ci irq_set_chained_handler(cascade_virq, uic_irq_cascade); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci /* FIXME: setup critical cascade?? */ 3158c2ecf20Sopenharmony_ci } 3168c2ecf20Sopenharmony_ci } 3178c2ecf20Sopenharmony_ci} 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci/* Return an interrupt vector or 0 if no interrupt is pending. */ 3208c2ecf20Sopenharmony_ciunsigned int uic_get_irq(void) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci u32 msr; 3238c2ecf20Sopenharmony_ci int src; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci BUG_ON(! primary_uic); 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci msr = mfdcr(primary_uic->dcrbase + UIC_MSR); 3288c2ecf20Sopenharmony_ci src = 32 - ffs(msr); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return irq_linear_revmap(primary_uic->irqhost, src); 3318c2ecf20Sopenharmony_ci} 332