1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/types.h> 3 #include <linux/kernel.h> 4 #include <linux/irq.h> 5 #include <linux/smp.h> 6 #include <linux/interrupt.h> 7 #include <linux/init.h> 8 #include <linux/cpu.h> 9 #include <linux/of.h> 10 #include <linux/spinlock.h> 11 #include <linux/msi.h> 12 13 #include <asm/prom.h> 14 #include <asm/smp.h> 15 #include <asm/machdep.h> 16 #include <asm/irq.h> 17 #include <asm/errno.h> 18 #include <asm/xics.h> 19 #include <asm/rtas.h> 20 21 /* RTAS service tokens */ 22 static int ibm_get_xive; 23 static int ibm_set_xive; 24 static int ibm_int_on; 25 static int ibm_int_off; 26 27 static void ics_rtas_unmask_irq(struct irq_data *d) 28 { 29 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); 30 int call_status; 31 int server; 32 33 pr_devel("xics: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq); 34 35 if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS) 36 return; 37 38 server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0); 39 40 call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq, 41 server, DEFAULT_PRIORITY); 42 if (call_status != 0) { 43 printk(KERN_ERR 44 "%s: ibm_set_xive irq %u server %x returned %d\n", 45 __func__, hw_irq, server, call_status); 46 return; 47 } 48 49 /* Now unmask the interrupt (often a no-op) */ 50 call_status = rtas_call_reentrant(ibm_int_on, 1, 1, NULL, hw_irq); 51 if (call_status != 0) { 52 printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n", 53 __func__, hw_irq, call_status); 54 return; 55 } 56 } 57 58 static unsigned int ics_rtas_startup(struct irq_data *d) 59 { 60 #ifdef CONFIG_PCI_MSI 61 /* 62 * The generic MSI code returns with the interrupt disabled on the 63 * card, using the MSI mask bits. Firmware doesn't appear to unmask 64 * at that level, so we do it here by hand. 65 */ 66 if (irq_data_get_msi_desc(d)) 67 pci_msi_unmask_irq(d); 68 #endif 69 /* unmask it */ 70 ics_rtas_unmask_irq(d); 71 return 0; 72 } 73 74 static void ics_rtas_mask_real_irq(unsigned int hw_irq) 75 { 76 int call_status; 77 78 if (hw_irq == XICS_IPI) 79 return; 80 81 call_status = rtas_call_reentrant(ibm_int_off, 1, 1, NULL, hw_irq); 82 if (call_status != 0) { 83 printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n", 84 __func__, hw_irq, call_status); 85 return; 86 } 87 88 /* Have to set XIVE to 0xff to be able to remove a slot */ 89 call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq, 90 xics_default_server, 0xff); 91 if (call_status != 0) { 92 printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n", 93 __func__, hw_irq, call_status); 94 return; 95 } 96 } 97 98 static void ics_rtas_mask_irq(struct irq_data *d) 99 { 100 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); 101 102 pr_devel("xics: mask virq %d [hw 0x%x]\n", d->irq, hw_irq); 103 104 if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS) 105 return; 106 ics_rtas_mask_real_irq(hw_irq); 107 } 108 109 static int ics_rtas_set_affinity(struct irq_data *d, 110 const struct cpumask *cpumask, 111 bool force) 112 { 113 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); 114 int status; 115 int xics_status[2]; 116 int irq_server; 117 118 if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS) 119 return -1; 120 121 status = rtas_call_reentrant(ibm_get_xive, 1, 3, xics_status, hw_irq); 122 123 if (status) { 124 printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n", 125 __func__, hw_irq, status); 126 return -1; 127 } 128 129 irq_server = xics_get_irq_server(d->irq, cpumask, 1); 130 if (irq_server == -1) { 131 pr_warn("%s: No online cpus in the mask %*pb for irq %d\n", 132 __func__, cpumask_pr_args(cpumask), d->irq); 133 return -1; 134 } 135 136 pr_debug("%s: irq %d [hw 0x%x] server: 0x%x\n", __func__, d->irq, 137 hw_irq, irq_server); 138 139 status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, 140 hw_irq, irq_server, xics_status[1]); 141 142 if (status) { 143 printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n", 144 __func__, hw_irq, status); 145 return -1; 146 } 147 148 return IRQ_SET_MASK_OK; 149 } 150 151 static struct irq_chip ics_rtas_irq_chip = { 152 .name = "XICS", 153 .irq_startup = ics_rtas_startup, 154 .irq_mask = ics_rtas_mask_irq, 155 .irq_unmask = ics_rtas_unmask_irq, 156 .irq_eoi = NULL, /* Patched at init time */ 157 .irq_set_affinity = ics_rtas_set_affinity, 158 .irq_set_type = xics_set_irq_type, 159 .irq_retrigger = xics_retrigger, 160 }; 161 162 static int ics_rtas_check(struct ics *ics, unsigned int hw_irq) 163 { 164 int status[2]; 165 int rc; 166 167 if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)) 168 return -EINVAL; 169 170 /* Check if RTAS knows about this interrupt */ 171 rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, hw_irq); 172 if (rc) 173 return -ENXIO; 174 175 return 0; 176 } 177 178 static void ics_rtas_mask_unknown(struct ics *ics, unsigned long vec) 179 { 180 ics_rtas_mask_real_irq(vec); 181 } 182 183 static long ics_rtas_get_server(struct ics *ics, unsigned long vec) 184 { 185 int rc, status[2]; 186 187 rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, vec); 188 if (rc) 189 return -1; 190 return status[0]; 191 } 192 193 static int ics_rtas_host_match(struct ics *ics, struct device_node *node) 194 { 195 /* IBM machines have interrupt parents of various funky types for things 196 * like vdevices, events, etc... The trick we use here is to match 197 * everything here except the legacy 8259 which is compatible "chrp,iic" 198 */ 199 return !of_device_is_compatible(node, "chrp,iic"); 200 } 201 202 /* Only one global & state struct ics */ 203 static struct ics ics_rtas = { 204 .check = ics_rtas_check, 205 .mask_unknown = ics_rtas_mask_unknown, 206 .get_server = ics_rtas_get_server, 207 .host_match = ics_rtas_host_match, 208 .chip = &ics_rtas_irq_chip, 209 }; 210 211 __init int ics_rtas_init(void) 212 { 213 ibm_get_xive = rtas_token("ibm,get-xive"); 214 ibm_set_xive = rtas_token("ibm,set-xive"); 215 ibm_int_on = rtas_token("ibm,int-on"); 216 ibm_int_off = rtas_token("ibm,int-off"); 217 218 /* We enable the RTAS "ICS" if RTAS is present with the 219 * appropriate tokens 220 */ 221 if (ibm_get_xive == RTAS_UNKNOWN_SERVICE || 222 ibm_set_xive == RTAS_UNKNOWN_SERVICE) 223 return -ENODEV; 224 225 /* We need to patch our irq chip's EOI to point to the 226 * right ICP 227 */ 228 ics_rtas_irq_chip.irq_eoi = icp_ops->eoi; 229 230 /* Register ourselves */ 231 xics_register_ics(&ics_rtas); 232 233 return 0; 234 } 235 236