19b54470aSStafford Horne /*
29b54470aSStafford Horne * Open Multi-Processor Interrupt Controller driver
39b54470aSStafford Horne *
49b54470aSStafford Horne * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
59b54470aSStafford Horne * Copyright (C) 2017 Stafford Horne <shorne@gmail.com>
69b54470aSStafford Horne *
79b54470aSStafford Horne * This file is licensed under the terms of the GNU General Public License
89b54470aSStafford Horne * version 2. This program is licensed "as is" without any warranty of any
99b54470aSStafford Horne * kind, whether express or implied.
109b54470aSStafford Horne *
119b54470aSStafford Horne * The ompic device handles IPI communication between cores in multi-core
129b54470aSStafford Horne * OpenRISC systems.
139b54470aSStafford Horne *
149b54470aSStafford Horne * Registers
159b54470aSStafford Horne *
169b54470aSStafford Horne * For each CPU the ompic has 2 registers. The control register for sending
179b54470aSStafford Horne * and acking IPIs and the status register for receiving IPIs. The register
189b54470aSStafford Horne * layouts are as follows:
199b54470aSStafford Horne *
209b54470aSStafford Horne * Control register
219b54470aSStafford Horne * +---------+---------+----------+---------+
229b54470aSStafford Horne * | 31 | 30 | 29 .. 16 | 15 .. 0 |
239b54470aSStafford Horne * ----------+---------+----------+----------
249b54470aSStafford Horne * | IRQ ACK | IRQ GEN | DST CORE | DATA |
259b54470aSStafford Horne * +---------+---------+----------+---------+
269b54470aSStafford Horne *
279b54470aSStafford Horne * Status register
289b54470aSStafford Horne * +----------+-------------+----------+---------+
299b54470aSStafford Horne * | 31 | 30 | 29 .. 16 | 15 .. 0 |
309b54470aSStafford Horne * -----------+-------------+----------+---------+
319b54470aSStafford Horne * | Reserved | IRQ Pending | SRC CORE | DATA |
329b54470aSStafford Horne * +----------+-------------+----------+---------+
339b54470aSStafford Horne *
349b54470aSStafford Horne * Architecture
359b54470aSStafford Horne *
369b54470aSStafford Horne * - The ompic generates a level interrupt to the CPU PIC when a message is
379b54470aSStafford Horne * ready. Messages are delivered via the memory bus.
389b54470aSStafford Horne * - The ompic does not have any interrupt input lines.
399b54470aSStafford Horne * - The ompic is wired to the same irq line on each core.
409b54470aSStafford Horne * - Devices are wired to the same irq line on each core.
419b54470aSStafford Horne *
429b54470aSStafford Horne * +---------+ +---------+
439b54470aSStafford Horne * | CPU | | CPU |
449b54470aSStafford Horne * | Core 0 |<==\ (memory access) /==>| Core 1 |
459b54470aSStafford Horne * | [ PIC ]| | | | [ PIC ]|
469b54470aSStafford Horne * +----^-^--+ | | +----^-^--+
479b54470aSStafford Horne * | | v v | |
489b54470aSStafford Horne * <====|=|=================================|=|==> (memory bus)
499b54470aSStafford Horne * | | ^ ^ | |
509b54470aSStafford Horne * (ipi | +------|---------+--------|-------|-+ (device irq)
519b54470aSStafford Horne * irq | | | | |
529b54470aSStafford Horne * core0)| +------|---------|--------|-------+ (ipi irq core1)
539b54470aSStafford Horne * | | | | |
549b54470aSStafford Horne * +----o-o-+ | +--------+ |
559b54470aSStafford Horne * | ompic |<===/ | Device |<===/
569b54470aSStafford Horne * | IPI | +--------+
579b54470aSStafford Horne * +--------+*
589b54470aSStafford Horne *
599b54470aSStafford Horne */
609b54470aSStafford Horne
619b54470aSStafford Horne #include <linux/io.h>
629b54470aSStafford Horne #include <linux/ioport.h>
639b54470aSStafford Horne #include <linux/interrupt.h>
649b54470aSStafford Horne #include <linux/smp.h>
659b54470aSStafford Horne #include <linux/of.h>
669b54470aSStafford Horne #include <linux/of_irq.h>
679b54470aSStafford Horne #include <linux/of_address.h>
689b54470aSStafford Horne
699b54470aSStafford Horne #include <linux/irqchip.h>
709b54470aSStafford Horne
719b54470aSStafford Horne #define OMPIC_CPUBYTES 8
729b54470aSStafford Horne #define OMPIC_CTRL(cpu) (0x0 + (cpu * OMPIC_CPUBYTES))
739b54470aSStafford Horne #define OMPIC_STAT(cpu) (0x4 + (cpu * OMPIC_CPUBYTES))
749b54470aSStafford Horne
759b54470aSStafford Horne #define OMPIC_CTRL_IRQ_ACK (1 << 31)
769b54470aSStafford Horne #define OMPIC_CTRL_IRQ_GEN (1 << 30)
779b54470aSStafford Horne #define OMPIC_CTRL_DST(cpu) (((cpu) & 0x3fff) << 16)
789b54470aSStafford Horne
799b54470aSStafford Horne #define OMPIC_STAT_IRQ_PENDING (1 << 30)
809b54470aSStafford Horne
819b54470aSStafford Horne #define OMPIC_DATA(x) ((x) & 0xffff)
829b54470aSStafford Horne
839b54470aSStafford Horne DEFINE_PER_CPU(unsigned long, ops);
849b54470aSStafford Horne
859b54470aSStafford Horne static void __iomem *ompic_base;
869b54470aSStafford Horne
ompic_readreg(void __iomem * base,loff_t offset)879b54470aSStafford Horne static inline u32 ompic_readreg(void __iomem *base, loff_t offset)
889b54470aSStafford Horne {
899b54470aSStafford Horne return ioread32be(base + offset);
909b54470aSStafford Horne }
919b54470aSStafford Horne
ompic_writereg(void __iomem * base,loff_t offset,u32 data)929b54470aSStafford Horne static void ompic_writereg(void __iomem *base, loff_t offset, u32 data)
939b54470aSStafford Horne {
949b54470aSStafford Horne iowrite32be(data, base + offset);
959b54470aSStafford Horne }
969b54470aSStafford Horne
ompic_raise_softirq(const struct cpumask * mask,unsigned int ipi_msg)979b54470aSStafford Horne static void ompic_raise_softirq(const struct cpumask *mask,
989b54470aSStafford Horne unsigned int ipi_msg)
999b54470aSStafford Horne {
1009b54470aSStafford Horne unsigned int dst_cpu;
1019b54470aSStafford Horne unsigned int src_cpu = smp_processor_id();
1029b54470aSStafford Horne
1039b54470aSStafford Horne for_each_cpu(dst_cpu, mask) {
1049b54470aSStafford Horne set_bit(ipi_msg, &per_cpu(ops, dst_cpu));
1059b54470aSStafford Horne
1069b54470aSStafford Horne /*
1079b54470aSStafford Horne * On OpenRISC the atomic set_bit() call implies a memory
1089b54470aSStafford Horne * barrier. Otherwise we would need: smp_wmb(); paired
1099b54470aSStafford Horne * with the read in ompic_ipi_handler.
1109b54470aSStafford Horne */
1119b54470aSStafford Horne
1129b54470aSStafford Horne ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu),
1139b54470aSStafford Horne OMPIC_CTRL_IRQ_GEN |
1149b54470aSStafford Horne OMPIC_CTRL_DST(dst_cpu) |
1159b54470aSStafford Horne OMPIC_DATA(1));
1169b54470aSStafford Horne }
1179b54470aSStafford Horne }
1189b54470aSStafford Horne
ompic_ipi_handler(int irq,void * dev_id)1199b54470aSStafford Horne static irqreturn_t ompic_ipi_handler(int irq, void *dev_id)
1209b54470aSStafford Horne {
1219b54470aSStafford Horne unsigned int cpu = smp_processor_id();
1229b54470aSStafford Horne unsigned long *pending_ops = &per_cpu(ops, cpu);
1239b54470aSStafford Horne unsigned long ops;
1249b54470aSStafford Horne
1259b54470aSStafford Horne ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK);
1269b54470aSStafford Horne while ((ops = xchg(pending_ops, 0)) != 0) {
1279b54470aSStafford Horne
1289b54470aSStafford Horne /*
1299b54470aSStafford Horne * On OpenRISC the atomic xchg() call implies a memory
1309b54470aSStafford Horne * barrier. Otherwise we may need an smp_rmb(); paired
1319b54470aSStafford Horne * with the write in ompic_raise_softirq.
1329b54470aSStafford Horne */
1339b54470aSStafford Horne
1349b54470aSStafford Horne do {
1359b54470aSStafford Horne unsigned long ipi_msg;
1369b54470aSStafford Horne
1379b54470aSStafford Horne ipi_msg = __ffs(ops);
1389b54470aSStafford Horne ops &= ~(1UL << ipi_msg);
1399b54470aSStafford Horne
1409b54470aSStafford Horne handle_IPI(ipi_msg);
1419b54470aSStafford Horne } while (ops);
1429b54470aSStafford Horne }
1439b54470aSStafford Horne
1449b54470aSStafford Horne return IRQ_HANDLED;
1459b54470aSStafford Horne }
1469b54470aSStafford Horne
ompic_of_init(struct device_node * node,struct device_node * parent)1479b54470aSStafford Horne static int __init ompic_of_init(struct device_node *node,
1489b54470aSStafford Horne struct device_node *parent)
1499b54470aSStafford Horne {
1509b54470aSStafford Horne struct resource res;
1519b54470aSStafford Horne int irq;
1529b54470aSStafford Horne int ret;
1539b54470aSStafford Horne
1549b54470aSStafford Horne /* Validate the DT */
1559b54470aSStafford Horne if (ompic_base) {
1569b54470aSStafford Horne pr_err("ompic: duplicate ompic's are not supported");
1579b54470aSStafford Horne return -EEXIST;
1589b54470aSStafford Horne }
1599b54470aSStafford Horne
1609b54470aSStafford Horne if (of_address_to_resource(node, 0, &res)) {
1619b54470aSStafford Horne pr_err("ompic: reg property requires an address and size");
1629b54470aSStafford Horne return -EINVAL;
1639b54470aSStafford Horne }
1649b54470aSStafford Horne
1659b54470aSStafford Horne if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) {
1669b54470aSStafford Horne pr_err("ompic: reg size, currently %d must be at least %d",
1679b54470aSStafford Horne resource_size(&res),
1689b54470aSStafford Horne (num_possible_cpus() * OMPIC_CPUBYTES));
1699b54470aSStafford Horne return -EINVAL;
1709b54470aSStafford Horne }
1719b54470aSStafford Horne
1729b54470aSStafford Horne /* Setup the device */
1739b54470aSStafford Horne ompic_base = ioremap(res.start, resource_size(&res));
174*404e6beaSWei Yongjun if (!ompic_base) {
1759b54470aSStafford Horne pr_err("ompic: unable to map registers");
176*404e6beaSWei Yongjun return -ENOMEM;
1779b54470aSStafford Horne }
1789b54470aSStafford Horne
1799b54470aSStafford Horne irq = irq_of_parse_and_map(node, 0);
1809b54470aSStafford Horne if (irq <= 0) {
1819b54470aSStafford Horne pr_err("ompic: unable to parse device irq");
1829b54470aSStafford Horne ret = -EINVAL;
1839b54470aSStafford Horne goto out_unmap;
1849b54470aSStafford Horne }
1859b54470aSStafford Horne
1869b54470aSStafford Horne ret = request_irq(irq, ompic_ipi_handler, IRQF_PERCPU,
1879b54470aSStafford Horne "ompic_ipi", NULL);
1889b54470aSStafford Horne if (ret)
1899b54470aSStafford Horne goto out_irq_disp;
1909b54470aSStafford Horne
1919b54470aSStafford Horne set_smp_cross_call(ompic_raise_softirq);
1929b54470aSStafford Horne
1939b54470aSStafford Horne return 0;
1949b54470aSStafford Horne
1959b54470aSStafford Horne out_irq_disp:
1969b54470aSStafford Horne irq_dispose_mapping(irq);
1979b54470aSStafford Horne out_unmap:
1989b54470aSStafford Horne iounmap(ompic_base);
1999b54470aSStafford Horne ompic_base = NULL;
2009b54470aSStafford Horne return ret;
2019b54470aSStafford Horne }
2029b54470aSStafford Horne IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init);
203