blob: cb674696810d8400e0238b3f05a3b618df50a137 [file] [log] [blame]
Steven J. Hill2299c492012-08-31 16:13:07 -05001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2008 Ralf Baechle (ralf@linux-mips.org)
7 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
8 */
Ralf Baechle39b8d522008-04-28 17:14:26 +01009#include <linux/bitmap.h>
Andrew Brestickerfb8f7be12014-10-20 12:03:55 -070010#include <linux/clocksource.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010011#include <linux/init.h>
Andrew Bresticker18743d22014-09-18 14:47:24 -070012#include <linux/interrupt.h>
Andrew Brestickerfb8f7be12014-10-20 12:03:55 -070013#include <linux/irq.h>
Andrew Bresticker4060bbe2014-10-20 12:03:53 -070014#include <linux/irqchip/mips-gic.h>
Andrew Bresticker18743d22014-09-18 14:47:24 -070015#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010016#include <linux/smp.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010017
Steven J. Hill98b67c32012-08-31 16:18:49 -050018#include <asm/setup.h>
19#include <asm/traps.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010020
Steven J. Hill28ea2152013-04-10 16:27:50 -050021unsigned int gic_frequency;
Steven J. Hillff867142013-04-10 16:27:04 -050022unsigned int gic_present;
Steven J. Hill98b67c32012-08-31 16:18:49 -050023
Jeffrey Deans822350b2014-07-17 09:20:53 +010024struct gic_pcpu_mask {
Andrew Brestickerfbd55242014-09-18 14:47:25 -070025 DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS);
Jeffrey Deans822350b2014-07-17 09:20:53 +010026};
27
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070028static void __iomem *gic_base;
Steven J. Hill0b271f52012-08-31 16:05:37 -050029static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
Andrew Bresticker95150ae2014-09-18 14:47:21 -070030static DEFINE_SPINLOCK(gic_lock);
Andrew Brestickerc49581a2014-09-18 14:47:23 -070031static struct irq_domain *gic_irq_domain;
Andrew Brestickerfbd55242014-09-18 14:47:25 -070032static int gic_shared_intrs;
Andrew Brestickere9de6882014-09-18 14:47:27 -070033static int gic_vpes;
Andrew Bresticker3263d082014-09-18 14:47:28 -070034static unsigned int gic_cpu_pin;
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -070035static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
Ralf Baechle39b8d522008-04-28 17:14:26 +010036
Andrew Bresticker18743d22014-09-18 14:47:24 -070037static void __gic_irq_dispatch(void);
38
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070039static inline unsigned int gic_read(unsigned int reg)
40{
41 return __raw_readl(gic_base + reg);
42}
43
44static inline void gic_write(unsigned int reg, unsigned int val)
45{
46 __raw_writel(val, gic_base + reg);
47}
48
49static inline void gic_update_bits(unsigned int reg, unsigned int mask,
50 unsigned int val)
51{
52 unsigned int regval;
53
54 regval = gic_read(reg);
55 regval &= ~mask;
56 regval |= val;
57 gic_write(reg, regval);
58}
59
60static inline void gic_reset_mask(unsigned int intr)
61{
62 gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
63 1 << GIC_INTR_BIT(intr));
64}
65
66static inline void gic_set_mask(unsigned int intr)
67{
68 gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
69 1 << GIC_INTR_BIT(intr));
70}
71
72static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
73{
74 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
75 GIC_INTR_OFS(intr), 1 << GIC_INTR_BIT(intr),
76 pol << GIC_INTR_BIT(intr));
77}
78
79static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
80{
81 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
82 GIC_INTR_OFS(intr), 1 << GIC_INTR_BIT(intr),
83 trig << GIC_INTR_BIT(intr));
84}
85
86static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
87{
88 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
89 1 << GIC_INTR_BIT(intr),
90 dual << GIC_INTR_BIT(intr));
91}
92
93static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
94{
95 gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
96 GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
97}
98
99static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
100{
101 gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
102 GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
103 GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
104}
105
Andrew Brestickera331ce62014-10-20 12:03:59 -0700106#ifdef CONFIG_CLKSRC_MIPS_GIC
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500107cycle_t gic_read_count(void)
108{
109 unsigned int hi, hi2, lo;
110
111 do {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700112 hi = gic_read(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
113 lo = gic_read(GIC_REG(SHARED, GIC_SH_COUNTER_31_00));
114 hi2 = gic_read(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500115 } while (hi2 != hi);
116
117 return (((cycle_t) hi) << 32) + lo;
118}
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500119
Andrew Bresticker387904f2014-10-20 12:03:49 -0700120unsigned int gic_get_count_width(void)
121{
122 unsigned int bits, config;
123
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700124 config = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
Andrew Bresticker387904f2014-10-20 12:03:49 -0700125 bits = 32 + 4 * ((config & GIC_SH_CONFIG_COUNTBITS_MSK) >>
126 GIC_SH_CONFIG_COUNTBITS_SHF);
127
128 return bits;
129}
130
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500131void gic_write_compare(cycle_t cnt)
132{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700133 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI),
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500134 (int)(cnt >> 32));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700135 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO),
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500136 (int)(cnt & 0xffffffff));
137}
138
Paul Burton414408d02014-03-05 11:35:53 +0000139void gic_write_cpu_compare(cycle_t cnt, int cpu)
140{
141 unsigned long flags;
142
143 local_irq_save(flags);
144
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700145 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu);
146 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI),
Paul Burton414408d02014-03-05 11:35:53 +0000147 (int)(cnt >> 32));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700148 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO),
Paul Burton414408d02014-03-05 11:35:53 +0000149 (int)(cnt & 0xffffffff));
150
151 local_irq_restore(flags);
152}
153
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500154cycle_t gic_read_compare(void)
155{
156 unsigned int hi, lo;
157
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700158 hi = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI));
159 lo = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO));
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500160
161 return (((cycle_t) hi) << 32) + lo;
162}
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500163#endif
164
Andrew Brestickere9de6882014-09-18 14:47:27 -0700165static bool gic_local_irq_is_routable(int intr)
166{
167 u32 vpe_ctl;
168
169 /* All local interrupts are routable in EIC mode. */
170 if (cpu_has_veic)
171 return true;
172
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700173 vpe_ctl = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700174 switch (intr) {
175 case GIC_LOCAL_INT_TIMER:
176 return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
177 case GIC_LOCAL_INT_PERFCTR:
178 return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
179 case GIC_LOCAL_INT_FDC:
180 return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
181 case GIC_LOCAL_INT_SWINT0:
182 case GIC_LOCAL_INT_SWINT1:
183 return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
184 default:
185 return true;
186 }
187}
188
Steven J. Hill98b67c32012-08-31 16:18:49 -0500189unsigned int gic_get_timer_pending(void)
190{
191 unsigned int vpe_pending;
192
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700193 vpe_pending = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
Ralf Baechle635c99072014-10-21 14:12:49 +0200194 return vpe_pending & GIC_VPE_PEND_TIMER_MSK;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500195}
196
Andrew Bresticker3263d082014-09-18 14:47:28 -0700197static void gic_bind_eic_interrupt(int irq, int set)
Steven J. Hill98b67c32012-08-31 16:18:49 -0500198{
199 /* Convert irq vector # to hw int # */
200 irq -= GIC_PIN_TO_VEC_OFFSET;
201
202 /* Set irq to use shadow set */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700203 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
204 GIC_VPE_EIC_SS(irq), set);
Steven J. Hill98b67c32012-08-31 16:18:49 -0500205}
206
Ralf Baechle39b8d522008-04-28 17:14:26 +0100207void gic_send_ipi(unsigned int intr)
208{
Andrew Bresticker53a7bc82014-10-20 12:03:57 -0700209 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(intr));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100210}
211
Andrew Brestickere9de6882014-09-18 14:47:27 -0700212int gic_get_c0_compare_int(void)
213{
214 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
215 return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
216 return irq_create_mapping(gic_irq_domain,
217 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
218}
219
220int gic_get_c0_perfcount_int(void)
221{
222 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
223 /* Is the erformance counter shared with the timer? */
224 if (cp0_perfcount_irq < 0)
225 return -1;
226 return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
227 }
228 return irq_create_mapping(gic_irq_domain,
229 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
230}
231
Andrew Bresticker3263d082014-09-18 14:47:28 -0700232static unsigned int gic_get_int(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100233{
234 unsigned int i;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700235 unsigned long *pcpu_mask;
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700236 unsigned long pending_reg, intrmask_reg;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700237 DECLARE_BITMAP(pending, GIC_MAX_INTRS);
238 DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100239
240 /* Get per-cpu bitmaps */
Ralf Baechle39b8d522008-04-28 17:14:26 +0100241 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
242
Andrew Bresticker824f3f72014-10-20 12:03:54 -0700243 pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
244 intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100245
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700246 for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700247 pending[i] = gic_read(pending_reg);
248 intrmask[i] = gic_read(intrmask_reg);
249 pending_reg += 0x4;
250 intrmask_reg += 0x4;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100251 }
252
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700253 bitmap_and(pending, pending, intrmask, gic_shared_intrs);
254 bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100255
Andrew Bresticker3263d082014-09-18 14:47:28 -0700256 return find_first_bit(pending, gic_shared_intrs);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100257}
258
Thomas Gleixner161d0492011-03-23 21:08:58 +0000259static void gic_mask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100260{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700261 gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100262}
263
Thomas Gleixner161d0492011-03-23 21:08:58 +0000264static void gic_unmask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100265{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700266 gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100267}
268
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700269static void gic_ack_irq(struct irq_data *d)
270{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700271 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700272
Andrew Bresticker53a7bc82014-10-20 12:03:57 -0700273 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700274}
275
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700276static int gic_set_type(struct irq_data *d, unsigned int type)
277{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700278 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700279 unsigned long flags;
280 bool is_edge;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100281
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700282 spin_lock_irqsave(&gic_lock, flags);
283 switch (type & IRQ_TYPE_SENSE_MASK) {
284 case IRQ_TYPE_EDGE_FALLING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700285 gic_set_polarity(irq, GIC_POL_NEG);
286 gic_set_trigger(irq, GIC_TRIG_EDGE);
287 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700288 is_edge = true;
289 break;
290 case IRQ_TYPE_EDGE_RISING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700291 gic_set_polarity(irq, GIC_POL_POS);
292 gic_set_trigger(irq, GIC_TRIG_EDGE);
293 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700294 is_edge = true;
295 break;
296 case IRQ_TYPE_EDGE_BOTH:
297 /* polarity is irrelevant in this case */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700298 gic_set_trigger(irq, GIC_TRIG_EDGE);
299 gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700300 is_edge = true;
301 break;
302 case IRQ_TYPE_LEVEL_LOW:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700303 gic_set_polarity(irq, GIC_POL_NEG);
304 gic_set_trigger(irq, GIC_TRIG_LEVEL);
305 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700306 is_edge = false;
307 break;
308 case IRQ_TYPE_LEVEL_HIGH:
309 default:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700310 gic_set_polarity(irq, GIC_POL_POS);
311 gic_set_trigger(irq, GIC_TRIG_LEVEL);
312 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700313 is_edge = false;
314 break;
315 }
316
317 if (is_edge) {
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700318 __irq_set_chip_handler_name_locked(d->irq,
319 &gic_edge_irq_controller,
320 handle_edge_irq, NULL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700321 } else {
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700322 __irq_set_chip_handler_name_locked(d->irq,
323 &gic_level_irq_controller,
324 handle_level_irq, NULL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700325 }
326 spin_unlock_irqrestore(&gic_lock, flags);
327
328 return 0;
329}
330
331#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000332static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
333 bool force)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100334{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700335 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100336 cpumask_t tmp = CPU_MASK_NONE;
337 unsigned long flags;
338 int i;
339
Rusty Russell0de26522008-12-13 21:20:26 +1030340 cpumask_and(&tmp, cpumask, cpu_online_mask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100341 if (cpus_empty(tmp))
Andrew Bresticker14d160a2014-09-18 14:47:22 -0700342 return -EINVAL;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100343
344 /* Assumption : cpumask refers to a single CPU */
345 spin_lock_irqsave(&gic_lock, flags);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100346
Tony Wuc214c032013-06-21 10:13:08 +0000347 /* Re-route this IRQ */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700348 gic_map_to_vpe(irq, first_cpu(tmp));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100349
Tony Wuc214c032013-06-21 10:13:08 +0000350 /* Update the pcpu_masks */
351 for (i = 0; i < NR_CPUS; i++)
352 clear_bit(irq, pcpu_masks[i].pcpu_mask);
353 set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask);
354
Thomas Gleixner161d0492011-03-23 21:08:58 +0000355 cpumask_copy(d->affinity, cpumask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100356 spin_unlock_irqrestore(&gic_lock, flags);
357
Thomas Gleixner161d0492011-03-23 21:08:58 +0000358 return IRQ_SET_MASK_OK_NOCOPY;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100359}
360#endif
361
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700362static struct irq_chip gic_level_irq_controller = {
363 .name = "MIPS GIC",
364 .irq_mask = gic_mask_irq,
365 .irq_unmask = gic_unmask_irq,
366 .irq_set_type = gic_set_type,
367#ifdef CONFIG_SMP
368 .irq_set_affinity = gic_set_affinity,
369#endif
370};
371
372static struct irq_chip gic_edge_irq_controller = {
Thomas Gleixner161d0492011-03-23 21:08:58 +0000373 .name = "MIPS GIC",
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700374 .irq_ack = gic_ack_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000375 .irq_mask = gic_mask_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000376 .irq_unmask = gic_unmask_irq,
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700377 .irq_set_type = gic_set_type,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100378#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000379 .irq_set_affinity = gic_set_affinity,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100380#endif
381};
382
Andrew Brestickere9de6882014-09-18 14:47:27 -0700383static unsigned int gic_get_local_int(void)
384{
385 unsigned long pending, masked;
386
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700387 pending = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
388 masked = gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700389
390 bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
391
392 return find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
393}
394
395static void gic_mask_local_irq(struct irq_data *d)
396{
397 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
398
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700399 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700400}
401
402static void gic_unmask_local_irq(struct irq_data *d)
403{
404 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
405
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700406 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700407}
408
409static struct irq_chip gic_local_irq_controller = {
410 .name = "MIPS GIC Local",
411 .irq_mask = gic_mask_local_irq,
412 .irq_unmask = gic_unmask_local_irq,
413};
414
415static void gic_mask_local_irq_all_vpes(struct irq_data *d)
416{
417 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
418 int i;
419 unsigned long flags;
420
421 spin_lock_irqsave(&gic_lock, flags);
422 for (i = 0; i < gic_vpes; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700423 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
424 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700425 }
426 spin_unlock_irqrestore(&gic_lock, flags);
427}
428
429static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
430{
431 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
432 int i;
433 unsigned long flags;
434
435 spin_lock_irqsave(&gic_lock, flags);
436 for (i = 0; i < gic_vpes; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700437 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
438 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700439 }
440 spin_unlock_irqrestore(&gic_lock, flags);
441}
442
443static struct irq_chip gic_all_vpes_local_irq_controller = {
444 .name = "MIPS GIC Local",
445 .irq_mask = gic_mask_local_irq_all_vpes,
446 .irq_unmask = gic_unmask_local_irq_all_vpes,
447};
448
Andrew Bresticker18743d22014-09-18 14:47:24 -0700449static void __gic_irq_dispatch(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100450{
Andrew Bresticker18743d22014-09-18 14:47:24 -0700451 unsigned int intr, virq;
452
Andrew Brestickere9de6882014-09-18 14:47:27 -0700453 while ((intr = gic_get_local_int()) != GIC_NUM_LOCAL_INTRS) {
454 virq = irq_linear_revmap(gic_irq_domain,
455 GIC_LOCAL_TO_HWIRQ(intr));
456 do_IRQ(virq);
457 }
458
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700459 while ((intr = gic_get_int()) != gic_shared_intrs) {
Andrew Brestickere9de6882014-09-18 14:47:27 -0700460 virq = irq_linear_revmap(gic_irq_domain,
461 GIC_SHARED_TO_HWIRQ(intr));
Andrew Bresticker18743d22014-09-18 14:47:24 -0700462 do_IRQ(virq);
463 }
464}
465
466static void gic_irq_dispatch(unsigned int irq, struct irq_desc *desc)
467{
468 __gic_irq_dispatch();
469}
470
471#ifdef CONFIG_MIPS_GIC_IPI
472static int gic_resched_int_base;
473static int gic_call_int_base;
474
475unsigned int plat_ipi_resched_int_xlate(unsigned int cpu)
476{
477 return gic_resched_int_base + cpu;
478}
479
480unsigned int plat_ipi_call_int_xlate(unsigned int cpu)
481{
482 return gic_call_int_base + cpu;
483}
484
485static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
486{
487 scheduler_ipi();
488
489 return IRQ_HANDLED;
490}
491
492static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
493{
494 smp_call_function_interrupt();
495
496 return IRQ_HANDLED;
497}
498
499static struct irqaction irq_resched = {
500 .handler = ipi_resched_interrupt,
501 .flags = IRQF_PERCPU,
502 .name = "IPI resched"
503};
504
505static struct irqaction irq_call = {
506 .handler = ipi_call_interrupt,
507 .flags = IRQF_PERCPU,
508 .name = "IPI call"
509};
510
511static __init void gic_ipi_init_one(unsigned int intr, int cpu,
512 struct irqaction *action)
513{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700514 int virq = irq_create_mapping(gic_irq_domain,
515 GIC_SHARED_TO_HWIRQ(intr));
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700516 int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500517
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700518 gic_map_to_vpe(intr, cpu);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700519 for (i = 0; i < NR_CPUS; i++)
520 clear_bit(intr, pcpu_masks[i].pcpu_mask);
Jeffrey Deansb0a88ae2014-07-17 09:20:55 +0100521 set_bit(intr, pcpu_masks[cpu].pcpu_mask);
522
Andrew Bresticker18743d22014-09-18 14:47:24 -0700523 irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
524
525 irq_set_handler(virq, handle_percpu_irq);
526 setup_irq(virq, action);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100527}
528
Andrew Bresticker18743d22014-09-18 14:47:24 -0700529static __init void gic_ipi_init(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100530{
Andrew Bresticker18743d22014-09-18 14:47:24 -0700531 int i;
532
533 /* Use last 2 * NR_CPUS interrupts as IPIs */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700534 gic_resched_int_base = gic_shared_intrs - nr_cpu_ids;
Andrew Bresticker18743d22014-09-18 14:47:24 -0700535 gic_call_int_base = gic_resched_int_base - nr_cpu_ids;
536
537 for (i = 0; i < nr_cpu_ids; i++) {
538 gic_ipi_init_one(gic_call_int_base + i, i, &irq_call);
539 gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched);
540 }
541}
542#else
543static inline void gic_ipi_init(void)
544{
545}
546#endif
547
Andrew Brestickere9de6882014-09-18 14:47:27 -0700548static void __init gic_basic_init(void)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700549{
550 unsigned int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500551
552 board_bind_eic_interrupt = &gic_bind_eic_interrupt;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100553
554 /* Setup defaults */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700555 for (i = 0; i < gic_shared_intrs; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700556 gic_set_polarity(i, GIC_POL_POS);
557 gic_set_trigger(i, GIC_TRIG_LEVEL);
558 gic_reset_mask(i);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100559 }
560
Andrew Brestickere9de6882014-09-18 14:47:27 -0700561 for (i = 0; i < gic_vpes; i++) {
562 unsigned int j;
563
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700564 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700565 for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
566 if (!gic_local_irq_is_routable(j))
567 continue;
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700568 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700569 }
570 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100571}
572
Andrew Brestickere9de6882014-09-18 14:47:27 -0700573static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
574 irq_hw_number_t hw)
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700575{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700576 int intr = GIC_HWIRQ_TO_LOCAL(hw);
577 int ret = 0;
578 int i;
579 unsigned long flags;
580
581 if (!gic_local_irq_is_routable(intr))
582 return -EPERM;
583
584 /*
585 * HACK: These are all really percpu interrupts, but the rest
586 * of the MIPS kernel code does not use the percpu IRQ API for
587 * the CP0 timer and performance counter interrupts.
588 */
589 if (intr != GIC_LOCAL_INT_TIMER && intr != GIC_LOCAL_INT_PERFCTR) {
590 irq_set_chip_and_handler(virq,
591 &gic_local_irq_controller,
592 handle_percpu_devid_irq);
593 irq_set_percpu_devid(virq);
594 } else {
595 irq_set_chip_and_handler(virq,
596 &gic_all_vpes_local_irq_controller,
597 handle_percpu_irq);
598 }
599
600 spin_lock_irqsave(&gic_lock, flags);
601 for (i = 0; i < gic_vpes; i++) {
602 u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
603
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700604 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700605
606 switch (intr) {
607 case GIC_LOCAL_INT_WD:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700608 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700609 break;
610 case GIC_LOCAL_INT_COMPARE:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700611 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700612 break;
613 case GIC_LOCAL_INT_TIMER:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700614 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700615 break;
616 case GIC_LOCAL_INT_PERFCTR:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700617 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700618 break;
619 case GIC_LOCAL_INT_SWINT0:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700620 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700621 break;
622 case GIC_LOCAL_INT_SWINT1:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700623 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700624 break;
625 case GIC_LOCAL_INT_FDC:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700626 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700627 break;
628 default:
629 pr_err("Invalid local IRQ %d\n", intr);
630 ret = -EINVAL;
631 break;
632 }
633 }
634 spin_unlock_irqrestore(&gic_lock, flags);
635
636 return ret;
637}
638
639static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
640 irq_hw_number_t hw)
641{
642 int intr = GIC_HWIRQ_TO_SHARED(hw);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700643 unsigned long flags;
644
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700645 irq_set_chip_and_handler(virq, &gic_level_irq_controller,
646 handle_level_irq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700647
648 spin_lock_irqsave(&gic_lock, flags);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700649 gic_map_to_pin(intr, gic_cpu_pin);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700650 /* Map to VPE 0 by default */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700651 gic_map_to_vpe(intr, 0);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700652 set_bit(intr, pcpu_masks[0].pcpu_mask);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700653 spin_unlock_irqrestore(&gic_lock, flags);
654
655 return 0;
656}
657
Andrew Brestickere9de6882014-09-18 14:47:27 -0700658static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
659 irq_hw_number_t hw)
660{
661 if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
662 return gic_local_irq_domain_map(d, virq, hw);
663 return gic_shared_irq_domain_map(d, virq, hw);
664}
665
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700666static struct irq_domain_ops gic_irq_domain_ops = {
667 .map = gic_irq_domain_map,
668 .xlate = irq_domain_xlate_twocell,
669};
670
Ralf Baechle39b8d522008-04-28 17:14:26 +0100671void __init gic_init(unsigned long gic_base_addr,
Andrew Bresticker18743d22014-09-18 14:47:24 -0700672 unsigned long gic_addrspace_size, unsigned int cpu_vec,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100673 unsigned int irqbase)
674{
675 unsigned int gicconfig;
676
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700677 gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100678
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700679 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700680 gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100681 GIC_SH_CONFIG_NUMINTRS_SHF;
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700682 gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100683
Andrew Brestickere9de6882014-09-18 14:47:27 -0700684 gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100685 GIC_SH_CONFIG_NUMVPES_SHF;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700686 gic_vpes = gic_vpes + 1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100687
Andrew Bresticker18743d22014-09-18 14:47:24 -0700688 if (cpu_has_veic) {
689 /* Always use vector 1 in EIC mode */
690 gic_cpu_pin = 0;
691 set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
692 __gic_irq_dispatch);
693 } else {
694 gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
695 irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
696 gic_irq_dispatch);
697 }
698
Andrew Brestickere9de6882014-09-18 14:47:27 -0700699 gic_irq_domain = irq_domain_add_simple(NULL, GIC_NUM_LOCAL_INTRS +
700 gic_shared_intrs, irqbase,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700701 &gic_irq_domain_ops, NULL);
702 if (!gic_irq_domain)
703 panic("Failed to add GIC IRQ domain");
Steven J. Hill0b271f52012-08-31 16:05:37 -0500704
Andrew Brestickere9de6882014-09-18 14:47:27 -0700705 gic_basic_init();
Andrew Bresticker18743d22014-09-18 14:47:24 -0700706
707 gic_ipi_init();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100708}