| 1 |
/* |
| 2 |
* Open Multi-Processor Interrupt Controller driver |
| 3 |
* |
| 4 |
* Copyright (C) 2014 Stefan Kristiansson <[email protected]> |
| 5 |
* Copyright (C) 2017 Stafford Horne <[email protected]> |
| 6 |
* |
| 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. |
| 10 |
* |
| 11 |
* The ompic device handles IPI communication between cores in multi-core |
| 12 |
* OpenRISC systems. |
| 13 |
* |
| 14 |
* Registers |
| 15 |
* |
| 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: |
| 19 |
* |
| 20 |
* Control register |
| 21 |
* +---------+---------+----------+---------+ |
| 22 |
* | 31 | 30 | 29 .. 16 | 15 .. 0 | |
| 23 |
* ----------+---------+----------+---------- |
| 24 |
* | IRQ ACK | IRQ GEN | DST CORE | DATA | |
| 25 |
* +---------+---------+----------+---------+ |
| 26 |
* |
| 27 |
* Status register |
| 28 |
* +----------+-------------+----------+---------+ |
| 29 |
* | 31 | 30 | 29 .. 16 | 15 .. 0 | |
| 30 |
* -----------+-------------+----------+---------+ |
| 31 |
* | Reserved | IRQ Pending | SRC CORE | DATA | |
| 32 |
* +----------+-------------+----------+---------+ |
| 33 |
* |
| 34 |
* Architecture |
| 35 |
* |
| 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. |
| 41 |
* |
| 42 |
* +---------+ +---------+ |
| 43 |
* | CPU | | CPU | |
| 44 |
* | Core 0 |<==\ (memory access) /==>| Core 1 | |
| 45 |
* | [ PIC ]| | | | [ PIC ]| |
| 46 |
* +----^-^--+ | | +----^-^--+ |
| 47 |
* | | v v | | |
| 48 |
* <====|=|=================================|=|==> (memory bus) |
| 49 |
* | | ^ ^ | | |
| 50 |
* (ipi | +------|---------+--------|-------|-+ (device irq) |
| 51 |
* irq | | | | | |
| 52 |
* core0)| +------|---------|--------|-------+ (ipi irq core1) |
| 53 |
* | | | | | |
| 54 |
* +----o-o-+ | +--------+ | |
| 55 |
* | ompic |<===/ | Device |<===/ |
| 56 |
* | IPI | +--------+ |
| 57 |
* +--------+* |
| 58 |
* |
| 59 |
*/ |
| 60 |
|
| 61 |
#include <linux/io.h> |
| 62 |
#include <linux/ioport.h> |
| 63 |
#include <linux/interrupt.h> |
| 64 |
#include <linux/smp.h> |
| 65 |
#include <linux/of.h> |
| 66 |
#include <linux/of_irq.h> |
| 67 |
#include <linux/of_address.h> |
| 68 |
|
| 69 |
#include <linux/irqchip.h> |
| 70 |
|
| 71 |
#define OMPIC_CPUBYTES 8 |
| 72 |
#define OMPIC_CTRL(cpu) (0x0 + (cpu * OMPIC_CPUBYTES)) |
| 73 |
#define OMPIC_STAT(cpu) (0x4 + (cpu * OMPIC_CPUBYTES)) |
| 74 |
|
| 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) |
| 78 |
|
| 79 |
#define OMPIC_STAT_IRQ_PENDING (1 << 30) |
| 80 |
|
| 81 |
#define OMPIC_DATA(x) ((x) & 0xffff) |
| 82 |
|
| 83 |
DEFINE_PER_CPU(unsigned long, ops); |
| 84 |
|
| 85 |
static void __iomem *ompic_base; |
| 86 |
|
| 87 |
static DEFINE_PER_CPU_READ_MOSTLY(int, ipi_dummy_dev); |
| 88 |
|
| 89 |
static inline u32 ompic_readreg(void __iomem *base, loff_t offset) |
| 90 |
{ |
| 91 |
return ioread32be(base + offset); |
| 92 |
} |
| 93 |
|
| 94 |
static void ompic_writereg(void __iomem *base, loff_t offset, u32 data) |
| 95 |
{ |
| 96 |
iowrite32be(data, base + offset); |
| 97 |
} |
| 98 |
|
| 99 |
static void ompic_raise_softirq(const struct cpumask *mask, |
| 100 |
unsigned int ipi_msg) |
| 101 |
{ |
| 102 |
unsigned int dst_cpu; |
| 103 |
unsigned int src_cpu = smp_processor_id(); |
| 104 |
|
| 105 |
for_each_cpu(dst_cpu, mask) { |
| 106 |
set_bit(ipi_msg, &per_cpu(ops, dst_cpu)); |
| 107 |
|
| 108 |
/* |
| 109 |
* On OpenRISC the atomic set_bit() call implies a memory |
| 110 |
* barrier. Otherwise we would need: smp_wmb(); paired |
| 111 |
* with the read in ompic_ipi_handler. |
| 112 |
*/ |
| 113 |
|
| 114 |
ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu), |
| 115 |
OMPIC_CTRL_IRQ_GEN | |
| 116 |
OMPIC_CTRL_DST(dst_cpu) | |
| 117 |
OMPIC_DATA(1)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static irqreturn_t ompic_ipi_handler(int irq, void *dev_id) |
| 122 |
{ |
| 123 |
unsigned int cpu = smp_processor_id(); |
| 124 |
unsigned long *pending_ops = &per_cpu(ops, cpu); |
| 125 |
unsigned long ops; |
| 126 |
|
| 127 |
ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK); |
| 128 |
while ((ops = xchg(pending_ops, 0)) != 0) { |
| 129 |
|
| 130 |
/* |
| 131 |
* On OpenRISC the atomic xchg() call implies a memory |
| 132 |
* barrier. Otherwise we may need an smp_rmb(); paired |
| 133 |
* with the write in ompic_raise_softirq. |
| 134 |
*/ |
| 135 |
|
| 136 |
do { |
| 137 |
unsigned long ipi_msg; |
| 138 |
|
| 139 |
ipi_msg = __ffs(ops); |
| 140 |
ops &= ~(1UL << ipi_msg); |
| 141 |
|
| 142 |
handle_IPI(ipi_msg); |
| 143 |
} while (ops); |
| 144 |
} |
| 145 |
|
| 146 |
return IRQ_HANDLED; |
| 147 |
} |
| 148 |
|
| 149 |
static int __init ompic_of_init(struct device_node *node, |
| 150 |
struct device_node *parent) |
| 151 |
{ |
| 152 |
struct resource res; |
| 153 |
int irq; |
| 154 |
int ret; |
| 155 |
|
| 156 |
/* Validate the DT */ |
| 157 |
if (ompic_base) { |
| 158 |
pr_err("ompic: duplicate ompic's are not supported"); |
| 159 |
return -EEXIST; |
| 160 |
} |
| 161 |
|
| 162 |
if (of_address_to_resource(node, 0, &res)) { |
| 163 |
pr_err("ompic: reg property requires an address and size"); |
| 164 |
return -EINVAL; |
| 165 |
} |
| 166 |
|
| 167 |
if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) { |
| 168 |
pr_err("ompic: reg size, currently %d must be at least %d", |
| 169 |
resource_size(&res), |
| 170 |
(num_possible_cpus() * OMPIC_CPUBYTES)); |
| 171 |
return -EINVAL; |
| 172 |
} |
| 173 |
|
| 174 |
/* Setup the device */ |
| 175 |
ompic_base = ioremap(res.start, resource_size(&res)); |
| 176 |
if (!ompic_base) { |
| 177 |
pr_err("ompic: unable to map registers"); |
| 178 |
return -ENOMEM; |
| 179 |
} |
| 180 |
|
| 181 |
irq = irq_of_parse_and_map(node, 0); |
| 182 |
if (irq <= 0) { |
| 183 |
pr_err("ompic: unable to parse device irq"); |
| 184 |
ret = -EINVAL; |
| 185 |
goto out_unmap; |
| 186 |
} |
| 187 |
|
| 188 |
irq_set_percpu_devid(irq); |
| 189 |
ret = request_percpu_irq(irq, ompic_ipi_handler, "ompic_ipi", |
| 190 |
&ipi_dummy_dev); |
| 191 |
|
| 192 |
if (ret) { |
| 193 |
pr_err("ompic: failed to request irq %d, error: %d", |
| 194 |
irq, ret); |
| 195 |
goto out_irq_disp; |
| 196 |
} |
| 197 |
|
| 198 |
set_smp_cross_call(ompic_raise_softirq, irq); |
| 199 |
|
| 200 |
return 0; |
| 201 |
|
| 202 |
out_irq_disp: |
| 203 |
irq_dispose_mapping(irq); |
| 204 |
out_unmap: |
| 205 |
iounmap(ompic_base); |
| 206 |
ompic_base = NULL; |
| 207 |
return ret; |
| 208 |
} |
| 209 |
IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init); |