1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83DEFINE_PER_CPU(unsigned long, ops);
84
85static void __iomem *ompic_base;
86
87static inline u32 ompic_readreg(void __iomem *base, loff_t offset)
88{
89 return ioread32be(base + offset);
90}
91
92static void ompic_writereg(void __iomem *base, loff_t offset, u32 data)
93{
94 iowrite32be(data, base + offset);
95}
96
97static void ompic_raise_softirq(const struct cpumask *mask,
98 unsigned int ipi_msg)
99{
100 unsigned int dst_cpu;
101 unsigned int src_cpu = smp_processor_id();
102
103 for_each_cpu(dst_cpu, mask) {
104 set_bit(ipi_msg, &per_cpu(ops, dst_cpu));
105
106
107
108
109
110
111
112 ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu),
113 OMPIC_CTRL_IRQ_GEN |
114 OMPIC_CTRL_DST(dst_cpu) |
115 OMPIC_DATA(1));
116 }
117}
118
119static irqreturn_t ompic_ipi_handler(int irq, void *dev_id)
120{
121 unsigned int cpu = smp_processor_id();
122 unsigned long *pending_ops = &per_cpu(ops, cpu);
123 unsigned long ops;
124
125 ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK);
126 while ((ops = xchg(pending_ops, 0)) != 0) {
127
128
129
130
131
132
133
134 do {
135 unsigned long ipi_msg;
136
137 ipi_msg = __ffs(ops);
138 ops &= ~(1UL << ipi_msg);
139
140 handle_IPI(ipi_msg);
141 } while (ops);
142 }
143
144 return IRQ_HANDLED;
145}
146
147static int __init ompic_of_init(struct device_node *node,
148 struct device_node *parent)
149{
150 struct resource res;
151 int irq;
152 int ret;
153
154
155 if (ompic_base) {
156 pr_err("ompic: duplicate ompic's are not supported");
157 return -EEXIST;
158 }
159
160 if (of_address_to_resource(node, 0, &res)) {
161 pr_err("ompic: reg property requires an address and size");
162 return -EINVAL;
163 }
164
165 if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) {
166 pr_err("ompic: reg size, currently %d must be at least %d",
167 resource_size(&res),
168 (num_possible_cpus() * OMPIC_CPUBYTES));
169 return -EINVAL;
170 }
171
172
173 ompic_base = ioremap(res.start, resource_size(&res));
174 if (!ompic_base) {
175 pr_err("ompic: unable to map registers");
176 return -ENOMEM;
177 }
178
179 irq = irq_of_parse_and_map(node, 0);
180 if (irq <= 0) {
181 pr_err("ompic: unable to parse device irq");
182 ret = -EINVAL;
183 goto out_unmap;
184 }
185
186 ret = request_irq(irq, ompic_ipi_handler, IRQF_PERCPU,
187 "ompic_ipi", NULL);
188 if (ret)
189 goto out_irq_disp;
190
191 set_smp_cross_call(ompic_raise_softirq);
192
193 return 0;
194
195out_irq_disp:
196 irq_dispose_mapping(irq);
197out_unmap:
198 iounmap(ompic_base);
199 ompic_base = NULL;
200 return ret;
201}
202IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init);
203