9e1bd03b87a61fbdfe34fd6726502f7f3ea0c815
[cascardo/linux.git] / arch / arc / kernel / mcip.c
1 /*
2  * ARC ARConnect (MultiCore IP) support (formerly known as MCIP)
3  *
4  * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/smp.h>
12 #include <linux/irq.h>
13 #include <linux/spinlock.h>
14 #include <asm/irqflags-arcv2.h>
15 #include <asm/mcip.h>
16 #include <asm/setup.h>
17
18 #define SOFTIRQ_IRQ     21
19
20 static char smp_cpuinfo_buf[128];
21 static int idu_detected;
22
23 static DEFINE_RAW_SPINLOCK(mcip_lock);
24
25 static void mcip_setup_per_cpu(int cpu)
26 {
27         smp_ipi_irq_setup(cpu, IPI_IRQ);
28         smp_ipi_irq_setup(cpu, SOFTIRQ_IRQ);
29 }
30
31 static void mcip_ipi_send(int cpu)
32 {
33         unsigned long flags;
34         int ipi_was_pending;
35
36         /* ARConnect can only send IPI to others */
37         if (unlikely(cpu == raw_smp_processor_id())) {
38                 arc_softirq_trigger(SOFTIRQ_IRQ);
39                 return;
40         }
41
42         /*
43          * NOTE: We must spin here if the other cpu hasn't yet
44          * serviced a previous message. This can burn lots
45          * of time, but we MUST follows this protocol or
46          * ipi messages can be lost!!!
47          * Also, we must release the lock in this loop because
48          * the other side may get to this same loop and not
49          * be able to ack -- thus causing deadlock.
50          */
51
52         do {
53                 raw_spin_lock_irqsave(&mcip_lock, flags);
54                 __mcip_cmd(CMD_INTRPT_READ_STATUS, cpu);
55                 ipi_was_pending = read_aux_reg(ARC_REG_MCIP_READBACK);
56                 if (ipi_was_pending == 0)
57                         break; /* break out but keep lock */
58                 raw_spin_unlock_irqrestore(&mcip_lock, flags);
59         } while (1);
60
61         __mcip_cmd(CMD_INTRPT_GENERATE_IRQ, cpu);
62         raw_spin_unlock_irqrestore(&mcip_lock, flags);
63
64 #ifdef CONFIG_ARC_IPI_DBG
65         if (ipi_was_pending)
66                 pr_info("IPI ACK delayed from cpu %d\n", cpu);
67 #endif
68 }
69
70 static void mcip_ipi_clear(int irq)
71 {
72         unsigned int cpu, c;
73         unsigned long flags;
74         unsigned int __maybe_unused copy;
75
76         if (unlikely(irq == SOFTIRQ_IRQ)) {
77                 arc_softirq_clear(irq);
78                 return;
79         }
80
81         raw_spin_lock_irqsave(&mcip_lock, flags);
82
83         /* Who sent the IPI */
84         __mcip_cmd(CMD_INTRPT_CHECK_SOURCE, 0);
85
86         copy = cpu = read_aux_reg(ARC_REG_MCIP_READBACK);       /* 1,2,4,8... */
87
88         /*
89          * In rare case, multiple concurrent IPIs sent to same target can
90          * possibly be coalesced by MCIP into 1 asserted IRQ, so @cpus can be
91          * "vectored" (multiple bits sets) as opposed to typical single bit
92          */
93         do {
94                 c = __ffs(cpu);                 /* 0,1,2,3 */
95                 __mcip_cmd(CMD_INTRPT_GENERATE_ACK, c);
96                 cpu &= ~(1U << c);
97         } while (cpu);
98
99         raw_spin_unlock_irqrestore(&mcip_lock, flags);
100
101 #ifdef CONFIG_ARC_IPI_DBG
102         if (c != __ffs(copy))
103                 pr_info("IPIs from %x coalesced to %x\n",
104                         copy, raw_smp_processor_id());
105 #endif
106 }
107
108 static void mcip_probe_n_setup(void)
109 {
110         struct mcip_bcr {
111 #ifdef CONFIG_CPU_BIG_ENDIAN
112                 unsigned int pad3:8,
113                              idu:1, llm:1, num_cores:6,
114                              iocoh:1,  gfrc:1, dbg:1, pad2:1,
115                              msg:1, sem:1, ipi:1, pad:1,
116                              ver:8;
117 #else
118                 unsigned int ver:8,
119                              pad:1, ipi:1, sem:1, msg:1,
120                              pad2:1, dbg:1, gfrc:1, iocoh:1,
121                              num_cores:6, llm:1, idu:1,
122                              pad3:8;
123 #endif
124         } mp;
125
126         READ_BCR(ARC_REG_MCIP_BCR, mp);
127
128         sprintf(smp_cpuinfo_buf,
129                 "Extn [SMP]\t: ARConnect (v%d): %d cores with %s%s%s%s%s\n",
130                 mp.ver, mp.num_cores,
131                 IS_AVAIL1(mp.ipi, "IPI "),
132                 IS_AVAIL1(mp.idu, "IDU "),
133                 IS_AVAIL1(mp.llm, "LLM "),
134                 IS_AVAIL1(mp.dbg, "DEBUG "),
135                 IS_AVAIL1(mp.gfrc, "GFRC"));
136
137         idu_detected = mp.idu;
138
139         if (mp.dbg) {
140                 __mcip_cmd_data(CMD_DEBUG_SET_SELECT, 0, 0xf);
141                 __mcip_cmd_data(CMD_DEBUG_SET_MASK, 0xf, 0xf);
142         }
143
144         if (IS_ENABLED(CONFIG_ARC_HAS_GFRC) && !mp.gfrc)
145                 panic("kernel trying to use non-existent GFRC\n");
146 }
147
148 struct plat_smp_ops plat_smp_ops = {
149         .info           = smp_cpuinfo_buf,
150         .init_early_smp = mcip_probe_n_setup,
151         .init_per_cpu   = mcip_setup_per_cpu,
152         .ipi_send       = mcip_ipi_send,
153         .ipi_clear      = mcip_ipi_clear,
154 };
155
156 /***************************************************************************
157  * ARCv2 Interrupt Distribution Unit (IDU)
158  *
159  * Connects external "COMMON" IRQs to core intc, providing:
160  *  -dynamic routing (IRQ affinity)
161  *  -load balancing (Round Robin interrupt distribution)
162  *  -1:N distribution
163  *
164  * It physically resides in the MCIP hw block
165  */
166
167 #include <linux/irqchip.h>
168 #include <linux/of.h>
169 #include <linux/of_irq.h>
170
171 /*
172  * Set the DEST for @cmn_irq to @cpu_mask (1 bit per core)
173  */
174 static void idu_set_dest(unsigned int cmn_irq, unsigned int cpu_mask)
175 {
176         __mcip_cmd_data(CMD_IDU_SET_DEST, cmn_irq, cpu_mask);
177 }
178
179 static void idu_set_mode(unsigned int cmn_irq, unsigned int lvl,
180                            unsigned int distr)
181 {
182         union {
183                 unsigned int word;
184                 struct {
185                         unsigned int distr:2, pad:2, lvl:1, pad2:27;
186                 };
187         } data;
188
189         data.distr = distr;
190         data.lvl = lvl;
191         __mcip_cmd_data(CMD_IDU_SET_MODE, cmn_irq, data.word);
192 }
193
194 static void idu_irq_mask(struct irq_data *data)
195 {
196         unsigned long flags;
197
198         raw_spin_lock_irqsave(&mcip_lock, flags);
199         __mcip_cmd_data(CMD_IDU_SET_MASK, data->hwirq, 1);
200         raw_spin_unlock_irqrestore(&mcip_lock, flags);
201 }
202
203 static void idu_irq_unmask(struct irq_data *data)
204 {
205         unsigned long flags;
206
207         raw_spin_lock_irqsave(&mcip_lock, flags);
208         __mcip_cmd_data(CMD_IDU_SET_MASK, data->hwirq, 0);
209         raw_spin_unlock_irqrestore(&mcip_lock, flags);
210 }
211
212 #ifdef CONFIG_SMP
213 static int
214 idu_irq_set_affinity(struct irq_data *data, const struct cpumask *cpumask,
215                      bool force)
216 {
217         unsigned long flags;
218         cpumask_t online;
219
220         /* errout if no online cpu per @cpumask */
221         if (!cpumask_and(&online, cpumask, cpu_online_mask))
222                 return -EINVAL;
223
224         raw_spin_lock_irqsave(&mcip_lock, flags);
225
226         idu_set_dest(data->hwirq, cpumask_bits(&online)[0]);
227         idu_set_mode(data->hwirq, IDU_M_TRIG_LEVEL, IDU_M_DISTRI_RR);
228
229         raw_spin_unlock_irqrestore(&mcip_lock, flags);
230
231         return IRQ_SET_MASK_OK;
232 }
233 #endif
234
235 static struct irq_chip idu_irq_chip = {
236         .name                   = "MCIP IDU Intc",
237         .irq_mask               = idu_irq_mask,
238         .irq_unmask             = idu_irq_unmask,
239 #ifdef CONFIG_SMP
240         .irq_set_affinity       = idu_irq_set_affinity,
241 #endif
242
243 };
244
245 static int idu_first_irq;
246
247 static void idu_cascade_isr(struct irq_desc *desc)
248 {
249         struct irq_domain *domain = irq_desc_get_handler_data(desc);
250         unsigned int core_irq = irq_desc_get_irq(desc);
251         unsigned int idu_irq;
252
253         idu_irq = core_irq - idu_first_irq;
254         generic_handle_irq(irq_find_mapping(domain, idu_irq));
255 }
256
257 static int idu_irq_map(struct irq_domain *d, unsigned int virq, irq_hw_number_t hwirq)
258 {
259         irq_set_chip_and_handler(virq, &idu_irq_chip, handle_level_irq);
260         irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
261
262         return 0;
263 }
264
265 static int idu_irq_xlate(struct irq_domain *d, struct device_node *n,
266                          const u32 *intspec, unsigned int intsize,
267                          irq_hw_number_t *out_hwirq, unsigned int *out_type)
268 {
269         irq_hw_number_t hwirq = *out_hwirq = intspec[0];
270         int distri = intspec[1];
271         unsigned long flags;
272
273         *out_type = IRQ_TYPE_NONE;
274
275         /* XXX: validate distribution scheme again online cpu mask */
276         if (distri == 0) {
277                 /* 0 - Round Robin to all cpus, otherwise 1 bit per core */
278                 raw_spin_lock_irqsave(&mcip_lock, flags);
279                 idu_set_dest(hwirq, BIT(num_online_cpus()) - 1);
280                 idu_set_mode(hwirq, IDU_M_TRIG_LEVEL, IDU_M_DISTRI_RR);
281                 raw_spin_unlock_irqrestore(&mcip_lock, flags);
282         } else {
283                 /*
284                  * DEST based distribution for Level Triggered intr can only
285                  * have 1 CPU, so generalize it to always contain 1 cpu
286                  */
287                 int cpu = ffs(distri);
288
289                 if (cpu != fls(distri))
290                         pr_warn("IDU irq %lx distri mode set to cpu %x\n",
291                                 hwirq, cpu);
292
293                 raw_spin_lock_irqsave(&mcip_lock, flags);
294                 idu_set_dest(hwirq, cpu);
295                 idu_set_mode(hwirq, IDU_M_TRIG_LEVEL, IDU_M_DISTRI_DEST);
296                 raw_spin_unlock_irqrestore(&mcip_lock, flags);
297         }
298
299         return 0;
300 }
301
302 static const struct irq_domain_ops idu_irq_ops = {
303         .xlate  = idu_irq_xlate,
304         .map    = idu_irq_map,
305 };
306
307 /*
308  * [16, 23]: Statically assigned always private-per-core (Timers, WDT, IPI)
309  * [24, 23+C]: If C > 0 then "C" common IRQs
310  * [24+C, N]: Not statically assigned, private-per-core
311  */
312
313
314 static int __init
315 idu_of_init(struct device_node *intc, struct device_node *parent)
316 {
317         struct irq_domain *domain;
318         /* Read IDU BCR to confirm nr_irqs */
319         int nr_irqs = of_irq_count(intc);
320         int i, irq;
321
322         if (!idu_detected)
323                 panic("IDU not detected, but DeviceTree using it");
324
325         pr_info("MCIP: IDU referenced from Devicetree %d irqs\n", nr_irqs);
326
327         domain = irq_domain_add_linear(intc, nr_irqs, &idu_irq_ops, NULL);
328
329         /* Parent interrupts (core-intc) are already mapped */
330
331         for (i = 0; i < nr_irqs; i++) {
332                 /*
333                  * Return parent uplink IRQs (towards core intc) 24,25,.....
334                  * this step has been done before already
335                  * however we need it to get the parent virq and set IDU handler
336                  * as first level isr
337                  */
338                 irq = irq_of_parse_and_map(intc, i);
339                 if (!i)
340                         idu_first_irq = irq;
341
342                 irq_set_chained_handler_and_data(irq, idu_cascade_isr, domain);
343         }
344
345         __mcip_cmd(CMD_IDU_ENABLE, 0);
346
347         return 0;
348 }
349 IRQCHIP_DECLARE(arcv2_idu_intc, "snps,archs-idu-intc", idu_of_init);