2 * Open Multi-Processor Interrupt Controller driver
4 * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
5 * Copyright (C) 2017 Stafford Horne <shorne@gmail.com>
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
11 * The ompic device handles IPI communication between cores in multi-core
16 * For each CPU the ompic has 2 registers. The control register for sending
17 * and acking IPIs and the status register for receiving IPIs. The register
18 * layouts are as follows:
21 * +---------+---------+----------+---------+
22 * | 31 | 30 | 29 .. 16 | 15 .. 0 |
23 * ----------+---------+----------+----------
24 * | IRQ ACK | IRQ GEN | DST CORE | DATA |
25 * +---------+---------+----------+---------+
28 * +----------+-------------+----------+---------+
29 * | 31 | 30 | 29 .. 16 | 15 .. 0 |
30 * -----------+-------------+----------+---------+
31 * | Reserved | IRQ Pending | SRC CORE | DATA |
32 * +----------+-------------+----------+---------+
36 * - The ompic generates a level interrupt to the CPU PIC when a message is
37 * ready. Messages are delivered via the memory bus.
38 * - The ompic does not have any interrupt input lines.
39 * - The ompic is wired to the same irq line on each core.
40 * - Devices are wired to the same irq line on each core.
42 * +---------+ +---------+
44 * | Core 0 |<==\ (memory access) /==>| Core 1 |
45 * | [ PIC ]| | | | [ PIC ]|
46 * +----^-^--+ | | +----^-^--+
48 * <====|=|=================================|=|==> (memory bus)
50 * (ipi | +------|---------+--------|-------|-+ (device irq)
52 * core0)| +------|---------|--------|-------+ (ipi irq core1)
54 * +----o-o-+ | +--------+ |
55 * | ompic |<===/ | Device |<===/
62 #include <linux/ioport.h>
63 #include <linux/interrupt.h>
64 #include <linux/smp.h>
66 #include <linux/of_irq.h>
67 #include <linux/of_address.h>
69 #include <linux/irqchip.h>
71 #define OMPIC_CPUBYTES 8
72 #define OMPIC_CTRL(cpu) (0x0 + (cpu * OMPIC_CPUBYTES))
73 #define OMPIC_STAT(cpu) (0x4 + (cpu * OMPIC_CPUBYTES))
75 #define OMPIC_CTRL_IRQ_ACK (1 << 31)
76 #define OMPIC_CTRL_IRQ_GEN (1 << 30)
77 #define OMPIC_CTRL_DST(cpu) (((cpu) & 0x3fff) << 16)
79 #define OMPIC_STAT_IRQ_PENDING (1 << 30)
81 #define OMPIC_DATA(x) ((x) & 0xffff)
83 DEFINE_PER_CPU(unsigned long, ops);
85 static void __iomem *ompic_base;
87 static inline u32 ompic_readreg(void __iomem *base, loff_t offset)
89 return ioread32be(base + offset);
92 static void ompic_writereg(void __iomem *base, loff_t offset, u32 data)
94 iowrite32be(data, base + offset);
97 static void ompic_raise_softirq(const struct cpumask *mask,
100 unsigned int dst_cpu;
101 unsigned int src_cpu = smp_processor_id();
103 for_each_cpu(dst_cpu, mask) {
104 set_bit(ipi_msg, &per_cpu(ops, dst_cpu));
107 * On OpenRISC the atomic set_bit() call implies a memory
108 * barrier. Otherwise we would need: smp_wmb(); paired
109 * with the read in ompic_ipi_handler.
112 ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu),
114 OMPIC_CTRL_DST(dst_cpu) |
119 static irqreturn_t ompic_ipi_handler(int irq, void *dev_id)
121 unsigned int cpu = smp_processor_id();
122 unsigned long *pending_ops = &per_cpu(ops, cpu);
125 ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK);
126 while ((ops = xchg(pending_ops, 0)) != 0) {
129 * On OpenRISC the atomic xchg() call implies a memory
130 * barrier. Otherwise we may need an smp_rmb(); paired
131 * with the write in ompic_raise_softirq.
135 unsigned long ipi_msg;
137 ipi_msg = __ffs(ops);
138 ops &= ~(1UL << ipi_msg);
147 static int __init ompic_of_init(struct device_node *node,
148 struct device_node *parent)
154 /* Validate the DT */
156 pr_err("ompic: duplicate ompic's are not supported");
160 if (of_address_to_resource(node, 0, &res)) {
161 pr_err("ompic: reg property requires an address and size");
165 if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) {
166 pr_err("ompic: reg size, currently %d must be at least %d",
168 (num_possible_cpus() * OMPIC_CPUBYTES));
172 /* Setup the device */
173 ompic_base = ioremap(res.start, resource_size(&res));
175 pr_err("ompic: unable to map registers");
179 irq = irq_of_parse_and_map(node, 0);
181 pr_err("ompic: unable to parse device irq");
186 ret = request_irq(irq, ompic_ipi_handler, IRQF_PERCPU,
191 set_smp_cross_call(ompic_raise_softirq);
196 irq_dispose_mapping(irq);
202 IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init);