0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 #include <linux/io.h>
0062 #include <linux/ioport.h>
0063 #include <linux/interrupt.h>
0064 #include <linux/smp.h>
0065 #include <linux/of.h>
0066 #include <linux/of_irq.h>
0067 #include <linux/of_address.h>
0068
0069 #include <linux/irqchip.h>
0070
0071 #define OMPIC_CPUBYTES 8
0072 #define OMPIC_CTRL(cpu) (0x0 + (cpu * OMPIC_CPUBYTES))
0073 #define OMPIC_STAT(cpu) (0x4 + (cpu * OMPIC_CPUBYTES))
0074
0075 #define OMPIC_CTRL_IRQ_ACK (1 << 31)
0076 #define OMPIC_CTRL_IRQ_GEN (1 << 30)
0077 #define OMPIC_CTRL_DST(cpu) (((cpu) & 0x3fff) << 16)
0078
0079 #define OMPIC_STAT_IRQ_PENDING (1 << 30)
0080
0081 #define OMPIC_DATA(x) ((x) & 0xffff)
0082
0083 DEFINE_PER_CPU(unsigned long, ops);
0084
0085 static void __iomem *ompic_base;
0086
0087 static inline u32 ompic_readreg(void __iomem *base, loff_t offset)
0088 {
0089 return ioread32be(base + offset);
0090 }
0091
0092 static void ompic_writereg(void __iomem *base, loff_t offset, u32 data)
0093 {
0094 iowrite32be(data, base + offset);
0095 }
0096
0097 static void ompic_raise_softirq(const struct cpumask *mask,
0098 unsigned int ipi_msg)
0099 {
0100 unsigned int dst_cpu;
0101 unsigned int src_cpu = smp_processor_id();
0102
0103 for_each_cpu(dst_cpu, mask) {
0104 set_bit(ipi_msg, &per_cpu(ops, dst_cpu));
0105
0106
0107
0108
0109
0110
0111
0112 ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu),
0113 OMPIC_CTRL_IRQ_GEN |
0114 OMPIC_CTRL_DST(dst_cpu) |
0115 OMPIC_DATA(1));
0116 }
0117 }
0118
0119 static irqreturn_t ompic_ipi_handler(int irq, void *dev_id)
0120 {
0121 unsigned int cpu = smp_processor_id();
0122 unsigned long *pending_ops = &per_cpu(ops, cpu);
0123 unsigned long ops;
0124
0125 ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK);
0126 while ((ops = xchg(pending_ops, 0)) != 0) {
0127
0128
0129
0130
0131
0132
0133
0134 do {
0135 unsigned long ipi_msg;
0136
0137 ipi_msg = __ffs(ops);
0138 ops &= ~(1UL << ipi_msg);
0139
0140 handle_IPI(ipi_msg);
0141 } while (ops);
0142 }
0143
0144 return IRQ_HANDLED;
0145 }
0146
0147 static int __init ompic_of_init(struct device_node *node,
0148 struct device_node *parent)
0149 {
0150 struct resource res;
0151 int irq;
0152 int ret;
0153
0154
0155 if (ompic_base) {
0156 pr_err("ompic: duplicate ompic's are not supported");
0157 return -EEXIST;
0158 }
0159
0160 if (of_address_to_resource(node, 0, &res)) {
0161 pr_err("ompic: reg property requires an address and size");
0162 return -EINVAL;
0163 }
0164
0165 if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) {
0166 pr_err("ompic: reg size, currently %d must be at least %d",
0167 resource_size(&res),
0168 (num_possible_cpus() * OMPIC_CPUBYTES));
0169 return -EINVAL;
0170 }
0171
0172
0173 ompic_base = ioremap(res.start, resource_size(&res));
0174 if (!ompic_base) {
0175 pr_err("ompic: unable to map registers");
0176 return -ENOMEM;
0177 }
0178
0179 irq = irq_of_parse_and_map(node, 0);
0180 if (irq <= 0) {
0181 pr_err("ompic: unable to parse device irq");
0182 ret = -EINVAL;
0183 goto out_unmap;
0184 }
0185
0186 ret = request_irq(irq, ompic_ipi_handler, IRQF_PERCPU,
0187 "ompic_ipi", NULL);
0188 if (ret)
0189 goto out_irq_disp;
0190
0191 set_smp_cross_call(ompic_raise_softirq);
0192
0193 return 0;
0194
0195 out_irq_disp:
0196 irq_dispose_mapping(irq);
0197 out_unmap:
0198 iounmap(ompic_base);
0199 ompic_base = NULL;
0200 return ret;
0201 }
0202 IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init);