Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2011 IBM Corporation.
0004  */
0005 
0006 #include <linux/types.h>
0007 #include <linux/kernel.h>
0008 #include <linux/irq.h>
0009 #include <linux/irqdomain.h>
0010 #include <linux/smp.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/init.h>
0013 #include <linux/cpu.h>
0014 #include <linux/of.h>
0015 #include <linux/of_address.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/module.h>
0018 
0019 #include <asm/io.h>
0020 #include <asm/smp.h>
0021 #include <asm/irq.h>
0022 #include <asm/errno.h>
0023 #include <asm/xics.h>
0024 #include <asm/kvm_ppc.h>
0025 #include <asm/dbell.h>
0026 
0027 struct icp_ipl {
0028     union {
0029         u32 word;
0030         u8 bytes[4];
0031     } xirr_poll;
0032     union {
0033         u32 word;
0034         u8 bytes[4];
0035     } xirr;
0036     u32 dummy;
0037     union {
0038         u32 word;
0039         u8 bytes[4];
0040     } qirr;
0041     u32 link_a;
0042     u32 link_b;
0043     u32 link_c;
0044 };
0045 
0046 static struct icp_ipl __iomem *icp_native_regs[NR_CPUS];
0047 
0048 static inline unsigned int icp_native_get_xirr(void)
0049 {
0050     int cpu = smp_processor_id();
0051     unsigned int xirr;
0052 
0053     /* Handled an interrupt latched by KVM */
0054     xirr = kvmppc_get_xics_latch();
0055     if (xirr)
0056         return xirr;
0057 
0058     return in_be32(&icp_native_regs[cpu]->xirr.word);
0059 }
0060 
0061 static inline void icp_native_set_xirr(unsigned int value)
0062 {
0063     int cpu = smp_processor_id();
0064 
0065     out_be32(&icp_native_regs[cpu]->xirr.word, value);
0066 }
0067 
0068 static inline void icp_native_set_cppr(u8 value)
0069 {
0070     int cpu = smp_processor_id();
0071 
0072     out_8(&icp_native_regs[cpu]->xirr.bytes[0], value);
0073 }
0074 
0075 static inline void icp_native_set_qirr(int n_cpu, u8 value)
0076 {
0077     out_8(&icp_native_regs[n_cpu]->qirr.bytes[0], value);
0078 }
0079 
0080 static void icp_native_set_cpu_priority(unsigned char cppr)
0081 {
0082     xics_set_base_cppr(cppr);
0083     icp_native_set_cppr(cppr);
0084     iosync();
0085 }
0086 
0087 void icp_native_eoi(struct irq_data *d)
0088 {
0089     unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
0090 
0091     iosync();
0092     icp_native_set_xirr((xics_pop_cppr() << 24) | hw_irq);
0093 }
0094 
0095 static void icp_native_teardown_cpu(void)
0096 {
0097     int cpu = smp_processor_id();
0098 
0099     /* Clear any pending IPI */
0100     icp_native_set_qirr(cpu, 0xff);
0101 }
0102 
0103 static void icp_native_flush_ipi(void)
0104 {
0105     /* We take the ipi irq but and never return so we
0106      * need to EOI the IPI, but want to leave our priority 0
0107      *
0108      * should we check all the other interrupts too?
0109      * should we be flagging idle loop instead?
0110      * or creating some task to be scheduled?
0111      */
0112 
0113     icp_native_set_xirr((0x00 << 24) | XICS_IPI);
0114 }
0115 
0116 static unsigned int icp_native_get_irq(void)
0117 {
0118     unsigned int xirr = icp_native_get_xirr();
0119     unsigned int vec = xirr & 0x00ffffff;
0120     unsigned int irq;
0121 
0122     if (vec == XICS_IRQ_SPURIOUS)
0123         return 0;
0124 
0125     irq = irq_find_mapping(xics_host, vec);
0126     if (likely(irq)) {
0127         xics_push_cppr(vec);
0128         return irq;
0129     }
0130 
0131     /* We don't have a linux mapping, so have rtas mask it. */
0132     xics_mask_unknown_vec(vec);
0133 
0134     /* We might learn about it later, so EOI it */
0135     icp_native_set_xirr(xirr);
0136 
0137     return 0;
0138 }
0139 
0140 #ifdef CONFIG_SMP
0141 
0142 static void icp_native_cause_ipi(int cpu)
0143 {
0144     kvmppc_set_host_ipi(cpu);
0145     icp_native_set_qirr(cpu, IPI_PRIORITY);
0146 }
0147 
0148 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
0149 void icp_native_cause_ipi_rm(int cpu)
0150 {
0151     /*
0152      * Currently not used to send IPIs to another CPU
0153      * on the same core. Only caller is KVM real mode.
0154      * Need the physical address of the XICS to be
0155      * previously saved in kvm_hstate in the paca.
0156      */
0157     void __iomem *xics_phys;
0158 
0159     /*
0160      * Just like the cause_ipi functions, it is required to
0161      * include a full barrier before causing the IPI.
0162      */
0163     xics_phys = paca_ptrs[cpu]->kvm_hstate.xics_phys;
0164     mb();
0165     __raw_rm_writeb(IPI_PRIORITY, xics_phys + XICS_MFRR);
0166 }
0167 #endif
0168 
0169 /*
0170  * Called when an interrupt is received on an off-line CPU to
0171  * clear the interrupt, so that the CPU can go back to nap mode.
0172  */
0173 void icp_native_flush_interrupt(void)
0174 {
0175     unsigned int xirr = icp_native_get_xirr();
0176     unsigned int vec = xirr & 0x00ffffff;
0177 
0178     if (vec == XICS_IRQ_SPURIOUS)
0179         return;
0180     if (vec == XICS_IPI) {
0181         /* Clear pending IPI */
0182         int cpu = smp_processor_id();
0183         kvmppc_clear_host_ipi(cpu);
0184         icp_native_set_qirr(cpu, 0xff);
0185     } else {
0186         pr_err("XICS: hw interrupt 0x%x to offline cpu, disabling\n",
0187                vec);
0188         xics_mask_unknown_vec(vec);
0189     }
0190     /* EOI the interrupt */
0191     icp_native_set_xirr(xirr);
0192 }
0193 
0194 void xics_wake_cpu(int cpu)
0195 {
0196     icp_native_set_qirr(cpu, IPI_PRIORITY);
0197 }
0198 EXPORT_SYMBOL_GPL(xics_wake_cpu);
0199 
0200 static irqreturn_t icp_native_ipi_action(int irq, void *dev_id)
0201 {
0202     int cpu = smp_processor_id();
0203 
0204     kvmppc_clear_host_ipi(cpu);
0205     icp_native_set_qirr(cpu, 0xff);
0206 
0207     return smp_ipi_demux();
0208 }
0209 
0210 #endif /* CONFIG_SMP */
0211 
0212 static int __init icp_native_map_one_cpu(int hw_id, unsigned long addr,
0213                      unsigned long size)
0214 {
0215     char *rname;
0216     int i, cpu = -1;
0217 
0218     /* This may look gross but it's good enough for now, we don't quite
0219      * have a hard -> linux processor id matching.
0220      */
0221     for_each_possible_cpu(i) {
0222         if (!cpu_present(i))
0223             continue;
0224         if (hw_id == get_hard_smp_processor_id(i)) {
0225             cpu = i;
0226             break;
0227         }
0228     }
0229 
0230     /* Fail, skip that CPU. Don't print, it's normal, some XICS come up
0231      * with way more entries in there than you have CPUs
0232      */
0233     if (cpu == -1)
0234         return 0;
0235 
0236     rname = kasprintf(GFP_KERNEL, "CPU %d [0x%x] Interrupt Presentation",
0237               cpu, hw_id);
0238 
0239     if (!request_mem_region(addr, size, rname)) {
0240         pr_warn("icp_native: Could not reserve ICP MMIO for CPU %d, interrupt server #0x%x\n",
0241             cpu, hw_id);
0242         return -EBUSY;
0243     }
0244 
0245     icp_native_regs[cpu] = ioremap(addr, size);
0246     kvmppc_set_xics_phys(cpu, addr);
0247     if (!icp_native_regs[cpu]) {
0248         pr_warn("icp_native: Failed ioremap for CPU %d, interrupt server #0x%x, addr %#lx\n",
0249             cpu, hw_id, addr);
0250         release_mem_region(addr, size);
0251         return -ENOMEM;
0252     }
0253     return 0;
0254 }
0255 
0256 static int __init icp_native_init_one_node(struct device_node *np,
0257                        unsigned int *indx)
0258 {
0259     unsigned int ilen;
0260     const __be32 *ireg;
0261     int i;
0262     int reg_tuple_size;
0263     int num_servers = 0;
0264 
0265     /* This code does the theorically broken assumption that the interrupt
0266      * server numbers are the same as the hard CPU numbers.
0267      * This happens to be the case so far but we are playing with fire...
0268      * should be fixed one of these days. -BenH.
0269      */
0270     ireg = of_get_property(np, "ibm,interrupt-server-ranges", &ilen);
0271 
0272     /* Do that ever happen ? we'll know soon enough... but even good'old
0273      * f80 does have that property ..
0274      */
0275     WARN_ON((ireg == NULL) || (ilen != 2*sizeof(u32)));
0276 
0277     if (ireg) {
0278         *indx = of_read_number(ireg, 1);
0279         if (ilen >= 2*sizeof(u32))
0280             num_servers = of_read_number(ireg + 1, 1);
0281     }
0282 
0283     ireg = of_get_property(np, "reg", &ilen);
0284     if (!ireg) {
0285         pr_err("icp_native: Can't find interrupt reg property");
0286         return -1;
0287     }
0288 
0289     reg_tuple_size = (of_n_addr_cells(np) + of_n_size_cells(np)) * 4;
0290     if (((ilen % reg_tuple_size) != 0)
0291         || (num_servers && (num_servers != (ilen / reg_tuple_size)))) {
0292         pr_err("icp_native: ICP reg len (%d) != num servers (%d)",
0293                ilen / reg_tuple_size, num_servers);
0294         return -1;
0295     }
0296 
0297     for (i = 0; i < (ilen / reg_tuple_size); i++) {
0298         struct resource r;
0299         int err;
0300 
0301         err = of_address_to_resource(np, i, &r);
0302         if (err) {
0303             pr_err("icp_native: Could not translate ICP MMIO"
0304                    " for interrupt server 0x%x (%d)\n", *indx, err);
0305             return -1;
0306         }
0307 
0308         if (icp_native_map_one_cpu(*indx, r.start, resource_size(&r)))
0309             return -1;
0310 
0311         (*indx)++;
0312     }
0313     return 0;
0314 }
0315 
0316 static const struct icp_ops icp_native_ops = {
0317     .get_irq    = icp_native_get_irq,
0318     .eoi        = icp_native_eoi,
0319     .set_priority   = icp_native_set_cpu_priority,
0320     .teardown_cpu   = icp_native_teardown_cpu,
0321     .flush_ipi  = icp_native_flush_ipi,
0322 #ifdef CONFIG_SMP
0323     .ipi_action = icp_native_ipi_action,
0324     .cause_ipi  = icp_native_cause_ipi,
0325 #endif
0326 };
0327 
0328 int __init icp_native_init(void)
0329 {
0330     struct device_node *np;
0331     u32 indx = 0;
0332     int found = 0;
0333 
0334     for_each_compatible_node(np, NULL, "ibm,ppc-xicp")
0335         if (icp_native_init_one_node(np, &indx) == 0)
0336             found = 1;
0337     if (!found) {
0338         for_each_node_by_type(np,
0339             "PowerPC-External-Interrupt-Presentation") {
0340                 if (icp_native_init_one_node(np, &indx) == 0)
0341                     found = 1;
0342         }
0343     }
0344 
0345     if (found == 0)
0346         return -ENODEV;
0347 
0348     icp_ops = &icp_native_ops;
0349 
0350     return 0;
0351 }