blob: 1764bcf8ee6bedc5619abec9f319b9ba5ca5f441 [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>
Joel Porquet41a83e062015-07-07 17:11:46 -040014#include <linux/irqchip.h>
Andrew Bresticker4060bbe2014-10-20 12:03:53 -070015#include <linux/irqchip/mips-gic.h>
Andrew Brestickera7057272014-11-12 11:43:38 -080016#include <linux/of_address.h>
Andrew Bresticker18743d22014-09-18 14:47:24 -070017#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010018#include <linux/smp.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010019
Andrew Brestickera7057272014-11-12 11:43:38 -080020#include <asm/mips-cm.h>
Steven J. Hill98b67c32012-08-31 16:18:49 -050021#include <asm/setup.h>
22#include <asm/traps.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010023
Andrew Brestickera7057272014-11-12 11:43:38 -080024#include <dt-bindings/interrupt-controller/mips-gic.h>
25
Steven J. Hillff867142013-04-10 16:27:04 -050026unsigned int gic_present;
Steven J. Hill98b67c32012-08-31 16:18:49 -050027
Jeffrey Deans822350b2014-07-17 09:20:53 +010028struct gic_pcpu_mask {
Andrew Brestickerfbd55242014-09-18 14:47:25 -070029 DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS);
Jeffrey Deans822350b2014-07-17 09:20:53 +010030};
31
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070032static void __iomem *gic_base;
Steven J. Hill0b271f52012-08-31 16:05:37 -050033static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
Andrew Bresticker95150ae2014-09-18 14:47:21 -070034static DEFINE_SPINLOCK(gic_lock);
Andrew Brestickerc49581a2014-09-18 14:47:23 -070035static struct irq_domain *gic_irq_domain;
Andrew Brestickerfbd55242014-09-18 14:47:25 -070036static int gic_shared_intrs;
Andrew Brestickere9de6882014-09-18 14:47:27 -070037static int gic_vpes;
Andrew Bresticker3263d082014-09-18 14:47:28 -070038static unsigned int gic_cpu_pin;
James Hogan1b6af712015-01-19 15:38:24 +000039static unsigned int timer_cpu_pin;
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -070040static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
Ralf Baechle39b8d522008-04-28 17:14:26 +010041
Andrew Bresticker18743d22014-09-18 14:47:24 -070042static void __gic_irq_dispatch(void);
43
Markos Chandrasc3f57f02015-07-14 10:26:09 +010044static inline u32 gic_read32(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070045{
46 return __raw_readl(gic_base + reg);
47}
48
Markos Chandrasc3f57f02015-07-14 10:26:09 +010049static inline u64 gic_read64(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070050{
Markos Chandrasc3f57f02015-07-14 10:26:09 +010051 return __raw_readq(gic_base + reg);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070052}
53
Markos Chandrasc3f57f02015-07-14 10:26:09 +010054static inline unsigned long gic_read(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070055{
Markos Chandrasc3f57f02015-07-14 10:26:09 +010056 if (!mips_cm_is64)
57 return gic_read32(reg);
58 else
59 return gic_read64(reg);
60}
61
62static inline void gic_write32(unsigned int reg, u32 val)
63{
64 return __raw_writel(val, gic_base + reg);
65}
66
67static inline void gic_write64(unsigned int reg, u64 val)
68{
69 return __raw_writeq(val, gic_base + reg);
70}
71
72static inline void gic_write(unsigned int reg, unsigned long val)
73{
74 if (!mips_cm_is64)
75 return gic_write32(reg, (u32)val);
76 else
77 return gic_write64(reg, (u64)val);
78}
79
80static inline void gic_update_bits(unsigned int reg, unsigned long mask,
81 unsigned long val)
82{
83 unsigned long regval;
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070084
85 regval = gic_read(reg);
86 regval &= ~mask;
87 regval |= val;
88 gic_write(reg, regval);
89}
90
91static inline void gic_reset_mask(unsigned int intr)
92{
93 gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +010094 1ul << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070095}
96
97static inline void gic_set_mask(unsigned int intr)
98{
99 gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100100 1ul << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700101}
102
103static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
104{
105 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100106 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
107 (unsigned long)pol << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700108}
109
110static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
111{
112 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100113 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
114 (unsigned long)trig << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700115}
116
117static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
118{
119 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100120 1ul << GIC_INTR_BIT(intr),
121 (unsigned long)dual << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700122}
123
124static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
125{
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100126 gic_write32(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
127 GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700128}
129
130static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
131{
132 gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
133 GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
134 GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
135}
136
Andrew Brestickera331ce62014-10-20 12:03:59 -0700137#ifdef CONFIG_CLKSRC_MIPS_GIC
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500138cycle_t gic_read_count(void)
139{
140 unsigned int hi, hi2, lo;
141
Markos Chandras6f50c832015-07-09 10:40:49 +0100142 if (mips_cm_is64)
143 return (cycle_t)gic_read(GIC_REG(SHARED, GIC_SH_COUNTER));
144
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500145 do {
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100146 hi = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
147 lo = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_31_00));
148 hi2 = gic_read32(GIC_REG(SHARED, GIC_SH_COUNTER_63_32));
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500149 } while (hi2 != hi);
150
151 return (((cycle_t) hi) << 32) + lo;
152}
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500153
Andrew Bresticker387904f2014-10-20 12:03:49 -0700154unsigned int gic_get_count_width(void)
155{
156 unsigned int bits, config;
157
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700158 config = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
Andrew Bresticker387904f2014-10-20 12:03:49 -0700159 bits = 32 + 4 * ((config & GIC_SH_CONFIG_COUNTBITS_MSK) >>
160 GIC_SH_CONFIG_COUNTBITS_SHF);
161
162 return bits;
163}
164
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500165void gic_write_compare(cycle_t cnt)
166{
Markos Chandras6f50c832015-07-09 10:40:49 +0100167 if (mips_cm_is64) {
168 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE), cnt);
169 } else {
170 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI),
171 (int)(cnt >> 32));
172 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO),
173 (int)(cnt & 0xffffffff));
174 }
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500175}
176
Paul Burton414408d02014-03-05 11:35:53 +0000177void gic_write_cpu_compare(cycle_t cnt, int cpu)
178{
179 unsigned long flags;
180
181 local_irq_save(flags);
182
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700183 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu);
Markos Chandras6f50c832015-07-09 10:40:49 +0100184
185 if (mips_cm_is64) {
186 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE), cnt);
187 } else {
188 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI),
189 (int)(cnt >> 32));
190 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO),
191 (int)(cnt & 0xffffffff));
192 }
Paul Burton414408d02014-03-05 11:35:53 +0000193
194 local_irq_restore(flags);
195}
196
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500197cycle_t gic_read_compare(void)
198{
199 unsigned int hi, lo;
200
Markos Chandras6f50c832015-07-09 10:40:49 +0100201 if (mips_cm_is64)
202 return (cycle_t)gic_read(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE));
203
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100204 hi = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI));
205 lo = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO));
Raghu Gandham0ab2b7d2013-04-10 16:30:12 -0500206
207 return (((cycle_t) hi) << 32) + lo;
208}
Markos Chandras8fa4b932015-03-23 12:32:01 +0000209
210void gic_start_count(void)
211{
212 u32 gicconfig;
213
214 /* Start the counter */
215 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
216 gicconfig &= ~(1 << GIC_SH_CONFIG_COUNTSTOP_SHF);
217 gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
218}
219
220void gic_stop_count(void)
221{
222 u32 gicconfig;
223
224 /* Stop the counter */
225 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
226 gicconfig |= 1 << GIC_SH_CONFIG_COUNTSTOP_SHF;
227 gic_write(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
228}
229
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500230#endif
231
Andrew Brestickere9de6882014-09-18 14:47:27 -0700232static bool gic_local_irq_is_routable(int intr)
233{
234 u32 vpe_ctl;
235
236 /* All local interrupts are routable in EIC mode. */
237 if (cpu_has_veic)
238 return true;
239
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100240 vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700241 switch (intr) {
242 case GIC_LOCAL_INT_TIMER:
243 return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
244 case GIC_LOCAL_INT_PERFCTR:
245 return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
246 case GIC_LOCAL_INT_FDC:
247 return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
248 case GIC_LOCAL_INT_SWINT0:
249 case GIC_LOCAL_INT_SWINT1:
250 return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
251 default:
252 return true;
253 }
254}
255
Andrew Bresticker3263d082014-09-18 14:47:28 -0700256static void gic_bind_eic_interrupt(int irq, int set)
Steven J. Hill98b67c32012-08-31 16:18:49 -0500257{
258 /* Convert irq vector # to hw int # */
259 irq -= GIC_PIN_TO_VEC_OFFSET;
260
261 /* Set irq to use shadow set */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700262 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
263 GIC_VPE_EIC_SS(irq), set);
Steven J. Hill98b67c32012-08-31 16:18:49 -0500264}
265
Ralf Baechle39b8d522008-04-28 17:14:26 +0100266void gic_send_ipi(unsigned int intr)
267{
Andrew Bresticker53a7bc82014-10-20 12:03:57 -0700268 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(intr));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100269}
270
Andrew Brestickere9de6882014-09-18 14:47:27 -0700271int gic_get_c0_compare_int(void)
272{
273 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
274 return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
275 return irq_create_mapping(gic_irq_domain,
276 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
277}
278
279int gic_get_c0_perfcount_int(void)
280{
281 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
James Hogan7e3e6cb2015-01-27 21:45:50 +0000282 /* Is the performance counter shared with the timer? */
Andrew Brestickere9de6882014-09-18 14:47:27 -0700283 if (cp0_perfcount_irq < 0)
284 return -1;
285 return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
286 }
287 return irq_create_mapping(gic_irq_domain,
288 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
289}
290
James Hogan6429e2b2015-01-29 11:14:09 +0000291int gic_get_c0_fdc_int(void)
292{
293 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
294 /* Is the FDC IRQ even present? */
295 if (cp0_fdc_irq < 0)
296 return -1;
297 return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
298 }
299
James Hogan6429e2b2015-01-29 11:14:09 +0000300 return irq_create_mapping(gic_irq_domain,
301 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
302}
303
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200304static void gic_handle_shared_int(bool chained)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100305{
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100306 unsigned int i, intr, virq, gic_reg_step = mips_cm_is64 ? 8 : 4;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700307 unsigned long *pcpu_mask;
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700308 unsigned long pending_reg, intrmask_reg;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700309 DECLARE_BITMAP(pending, GIC_MAX_INTRS);
310 DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100311
312 /* Get per-cpu bitmaps */
Ralf Baechle39b8d522008-04-28 17:14:26 +0100313 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
314
Andrew Bresticker824f3f72014-10-20 12:03:54 -0700315 pending_reg = GIC_REG(SHARED, GIC_SH_PEND);
316 intrmask_reg = GIC_REG(SHARED, GIC_SH_MASK);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100317
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700318 for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700319 pending[i] = gic_read(pending_reg);
320 intrmask[i] = gic_read(intrmask_reg);
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100321 pending_reg += gic_reg_step;
322 intrmask_reg += gic_reg_step;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100323 }
324
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700325 bitmap_and(pending, pending, intrmask, gic_shared_intrs);
326 bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100327
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000328 intr = find_first_bit(pending, gic_shared_intrs);
329 while (intr != gic_shared_intrs) {
330 virq = irq_linear_revmap(gic_irq_domain,
331 GIC_SHARED_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200332 if (chained)
333 generic_handle_irq(virq);
334 else
335 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000336
337 /* go to next pending bit */
338 bitmap_clear(pending, intr, 1);
339 intr = find_first_bit(pending, gic_shared_intrs);
340 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100341}
342
Thomas Gleixner161d0492011-03-23 21:08:58 +0000343static void gic_mask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100344{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700345 gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100346}
347
Thomas Gleixner161d0492011-03-23 21:08:58 +0000348static void gic_unmask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100349{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700350 gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100351}
352
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700353static void gic_ack_irq(struct irq_data *d)
354{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700355 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700356
Andrew Bresticker53a7bc82014-10-20 12:03:57 -0700357 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700358}
359
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700360static int gic_set_type(struct irq_data *d, unsigned int type)
361{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700362 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700363 unsigned long flags;
364 bool is_edge;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100365
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700366 spin_lock_irqsave(&gic_lock, flags);
367 switch (type & IRQ_TYPE_SENSE_MASK) {
368 case IRQ_TYPE_EDGE_FALLING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700369 gic_set_polarity(irq, GIC_POL_NEG);
370 gic_set_trigger(irq, GIC_TRIG_EDGE);
371 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700372 is_edge = true;
373 break;
374 case IRQ_TYPE_EDGE_RISING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700375 gic_set_polarity(irq, GIC_POL_POS);
376 gic_set_trigger(irq, GIC_TRIG_EDGE);
377 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700378 is_edge = true;
379 break;
380 case IRQ_TYPE_EDGE_BOTH:
381 /* polarity is irrelevant in this case */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700382 gic_set_trigger(irq, GIC_TRIG_EDGE);
383 gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700384 is_edge = true;
385 break;
386 case IRQ_TYPE_LEVEL_LOW:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700387 gic_set_polarity(irq, GIC_POL_NEG);
388 gic_set_trigger(irq, GIC_TRIG_LEVEL);
389 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700390 is_edge = false;
391 break;
392 case IRQ_TYPE_LEVEL_HIGH:
393 default:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700394 gic_set_polarity(irq, GIC_POL_POS);
395 gic_set_trigger(irq, GIC_TRIG_LEVEL);
396 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700397 is_edge = false;
398 break;
399 }
400
Thomas Gleixnera595fc52015-06-23 14:41:25 +0200401 if (is_edge)
402 irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
403 handle_edge_irq, NULL);
404 else
405 irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
406 handle_level_irq, NULL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700407 spin_unlock_irqrestore(&gic_lock, flags);
408
409 return 0;
410}
411
412#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000413static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
414 bool force)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100415{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700416 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100417 cpumask_t tmp = CPU_MASK_NONE;
418 unsigned long flags;
419 int i;
420
Rusty Russell0de26522008-12-13 21:20:26 +1030421 cpumask_and(&tmp, cpumask, cpu_online_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030422 if (cpumask_empty(&tmp))
Andrew Bresticker14d160a2014-09-18 14:47:22 -0700423 return -EINVAL;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100424
425 /* Assumption : cpumask refers to a single CPU */
426 spin_lock_irqsave(&gic_lock, flags);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100427
Tony Wuc214c032013-06-21 10:13:08 +0000428 /* Re-route this IRQ */
Rusty Russellf9b531f2015-03-05 10:49:16 +1030429 gic_map_to_vpe(irq, cpumask_first(&tmp));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100430
Tony Wuc214c032013-06-21 10:13:08 +0000431 /* Update the pcpu_masks */
432 for (i = 0; i < NR_CPUS; i++)
433 clear_bit(irq, pcpu_masks[i].pcpu_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030434 set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
Tony Wuc214c032013-06-21 10:13:08 +0000435
Jiang Liu72f86db2015-06-01 16:05:38 +0800436 cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100437 spin_unlock_irqrestore(&gic_lock, flags);
438
Thomas Gleixner161d0492011-03-23 21:08:58 +0000439 return IRQ_SET_MASK_OK_NOCOPY;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100440}
441#endif
442
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700443static struct irq_chip gic_level_irq_controller = {
444 .name = "MIPS GIC",
445 .irq_mask = gic_mask_irq,
446 .irq_unmask = gic_unmask_irq,
447 .irq_set_type = gic_set_type,
448#ifdef CONFIG_SMP
449 .irq_set_affinity = gic_set_affinity,
450#endif
451};
452
453static struct irq_chip gic_edge_irq_controller = {
Thomas Gleixner161d0492011-03-23 21:08:58 +0000454 .name = "MIPS GIC",
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700455 .irq_ack = gic_ack_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000456 .irq_mask = gic_mask_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000457 .irq_unmask = gic_unmask_irq,
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700458 .irq_set_type = gic_set_type,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100459#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000460 .irq_set_affinity = gic_set_affinity,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100461#endif
462};
463
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200464static void gic_handle_local_int(bool chained)
Andrew Brestickere9de6882014-09-18 14:47:27 -0700465{
466 unsigned long pending, masked;
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000467 unsigned int intr, virq;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700468
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100469 pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
470 masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700471
472 bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
473
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000474 intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
475 while (intr != GIC_NUM_LOCAL_INTRS) {
476 virq = irq_linear_revmap(gic_irq_domain,
477 GIC_LOCAL_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200478 if (chained)
479 generic_handle_irq(virq);
480 else
481 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000482
483 /* go to next pending bit */
484 bitmap_clear(&pending, intr, 1);
485 intr = find_first_bit(&pending, GIC_NUM_LOCAL_INTRS);
486 }
Andrew Brestickere9de6882014-09-18 14:47:27 -0700487}
488
489static void gic_mask_local_irq(struct irq_data *d)
490{
491 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
492
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100493 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700494}
495
496static void gic_unmask_local_irq(struct irq_data *d)
497{
498 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
499
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100500 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700501}
502
503static struct irq_chip gic_local_irq_controller = {
504 .name = "MIPS GIC Local",
505 .irq_mask = gic_mask_local_irq,
506 .irq_unmask = gic_unmask_local_irq,
507};
508
509static void gic_mask_local_irq_all_vpes(struct irq_data *d)
510{
511 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
512 int i;
513 unsigned long flags;
514
515 spin_lock_irqsave(&gic_lock, flags);
516 for (i = 0; i < gic_vpes; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700517 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100518 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700519 }
520 spin_unlock_irqrestore(&gic_lock, flags);
521}
522
523static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
524{
525 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
526 int i;
527 unsigned long flags;
528
529 spin_lock_irqsave(&gic_lock, flags);
530 for (i = 0; i < gic_vpes; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700531 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100532 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700533 }
534 spin_unlock_irqrestore(&gic_lock, flags);
535}
536
537static struct irq_chip gic_all_vpes_local_irq_controller = {
538 .name = "MIPS GIC Local",
539 .irq_mask = gic_mask_local_irq_all_vpes,
540 .irq_unmask = gic_unmask_local_irq_all_vpes,
541};
542
Andrew Bresticker18743d22014-09-18 14:47:24 -0700543static void __gic_irq_dispatch(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100544{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200545 gic_handle_local_int(false);
546 gic_handle_shared_int(false);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700547}
548
549static void gic_irq_dispatch(unsigned int irq, struct irq_desc *desc)
550{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200551 gic_handle_local_int(true);
552 gic_handle_shared_int(true);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700553}
554
555#ifdef CONFIG_MIPS_GIC_IPI
556static int gic_resched_int_base;
557static int gic_call_int_base;
558
559unsigned int plat_ipi_resched_int_xlate(unsigned int cpu)
560{
561 return gic_resched_int_base + cpu;
562}
563
564unsigned int plat_ipi_call_int_xlate(unsigned int cpu)
565{
566 return gic_call_int_base + cpu;
567}
568
569static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
570{
571 scheduler_ipi();
572
573 return IRQ_HANDLED;
574}
575
576static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
577{
Alex Smith4ace6132015-07-24 16:57:49 +0100578 generic_smp_call_function_interrupt();
Andrew Bresticker18743d22014-09-18 14:47:24 -0700579
580 return IRQ_HANDLED;
581}
582
583static struct irqaction irq_resched = {
584 .handler = ipi_resched_interrupt,
585 .flags = IRQF_PERCPU,
586 .name = "IPI resched"
587};
588
589static struct irqaction irq_call = {
590 .handler = ipi_call_interrupt,
591 .flags = IRQF_PERCPU,
592 .name = "IPI call"
593};
594
595static __init void gic_ipi_init_one(unsigned int intr, int cpu,
596 struct irqaction *action)
597{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700598 int virq = irq_create_mapping(gic_irq_domain,
599 GIC_SHARED_TO_HWIRQ(intr));
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700600 int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500601
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700602 gic_map_to_vpe(intr, cpu);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700603 for (i = 0; i < NR_CPUS; i++)
604 clear_bit(intr, pcpu_masks[i].pcpu_mask);
Jeffrey Deansb0a88ae2014-07-17 09:20:55 +0100605 set_bit(intr, pcpu_masks[cpu].pcpu_mask);
606
Andrew Bresticker18743d22014-09-18 14:47:24 -0700607 irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
608
609 irq_set_handler(virq, handle_percpu_irq);
610 setup_irq(virq, action);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100611}
612
Andrew Bresticker18743d22014-09-18 14:47:24 -0700613static __init void gic_ipi_init(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100614{
Andrew Bresticker18743d22014-09-18 14:47:24 -0700615 int i;
616
617 /* Use last 2 * NR_CPUS interrupts as IPIs */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700618 gic_resched_int_base = gic_shared_intrs - nr_cpu_ids;
Andrew Bresticker18743d22014-09-18 14:47:24 -0700619 gic_call_int_base = gic_resched_int_base - nr_cpu_ids;
620
621 for (i = 0; i < nr_cpu_ids; i++) {
622 gic_ipi_init_one(gic_call_int_base + i, i, &irq_call);
623 gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched);
624 }
625}
626#else
627static inline void gic_ipi_init(void)
628{
629}
630#endif
631
Andrew Brestickere9de6882014-09-18 14:47:27 -0700632static void __init gic_basic_init(void)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700633{
634 unsigned int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500635
636 board_bind_eic_interrupt = &gic_bind_eic_interrupt;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100637
638 /* Setup defaults */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700639 for (i = 0; i < gic_shared_intrs; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700640 gic_set_polarity(i, GIC_POL_POS);
641 gic_set_trigger(i, GIC_TRIG_LEVEL);
642 gic_reset_mask(i);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100643 }
644
Andrew Brestickere9de6882014-09-18 14:47:27 -0700645 for (i = 0; i < gic_vpes; i++) {
646 unsigned int j;
647
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700648 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700649 for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
650 if (!gic_local_irq_is_routable(j))
651 continue;
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100652 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700653 }
654 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100655}
656
Andrew Brestickere9de6882014-09-18 14:47:27 -0700657static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
658 irq_hw_number_t hw)
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700659{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700660 int intr = GIC_HWIRQ_TO_LOCAL(hw);
661 int ret = 0;
662 int i;
663 unsigned long flags;
664
665 if (!gic_local_irq_is_routable(intr))
666 return -EPERM;
667
668 /*
669 * HACK: These are all really percpu interrupts, but the rest
670 * of the MIPS kernel code does not use the percpu IRQ API for
671 * the CP0 timer and performance counter interrupts.
672 */
James Hoganb720fd82015-01-29 11:14:08 +0000673 switch (intr) {
674 case GIC_LOCAL_INT_TIMER:
675 case GIC_LOCAL_INT_PERFCTR:
676 case GIC_LOCAL_INT_FDC:
677 irq_set_chip_and_handler(virq,
678 &gic_all_vpes_local_irq_controller,
679 handle_percpu_irq);
680 break;
681 default:
Andrew Brestickere9de6882014-09-18 14:47:27 -0700682 irq_set_chip_and_handler(virq,
683 &gic_local_irq_controller,
684 handle_percpu_devid_irq);
685 irq_set_percpu_devid(virq);
James Hoganb720fd82015-01-29 11:14:08 +0000686 break;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700687 }
688
689 spin_lock_irqsave(&gic_lock, flags);
690 for (i = 0; i < gic_vpes; i++) {
691 u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
692
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700693 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700694
695 switch (intr) {
696 case GIC_LOCAL_INT_WD:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100697 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700698 break;
699 case GIC_LOCAL_INT_COMPARE:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100700 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
701 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700702 break;
703 case GIC_LOCAL_INT_TIMER:
James Hogan1b6af712015-01-19 15:38:24 +0000704 /* CONFIG_MIPS_CMP workaround (see __gic_init) */
705 val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100706 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
707 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700708 break;
709 case GIC_LOCAL_INT_PERFCTR:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100710 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
711 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700712 break;
713 case GIC_LOCAL_INT_SWINT0:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100714 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
715 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700716 break;
717 case GIC_LOCAL_INT_SWINT1:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100718 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
719 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700720 break;
721 case GIC_LOCAL_INT_FDC:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100722 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700723 break;
724 default:
725 pr_err("Invalid local IRQ %d\n", intr);
726 ret = -EINVAL;
727 break;
728 }
729 }
730 spin_unlock_irqrestore(&gic_lock, flags);
731
732 return ret;
733}
734
735static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
736 irq_hw_number_t hw)
737{
738 int intr = GIC_HWIRQ_TO_SHARED(hw);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700739 unsigned long flags;
740
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700741 irq_set_chip_and_handler(virq, &gic_level_irq_controller,
742 handle_level_irq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700743
744 spin_lock_irqsave(&gic_lock, flags);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700745 gic_map_to_pin(intr, gic_cpu_pin);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700746 /* Map to VPE 0 by default */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700747 gic_map_to_vpe(intr, 0);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700748 set_bit(intr, pcpu_masks[0].pcpu_mask);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700749 spin_unlock_irqrestore(&gic_lock, flags);
750
751 return 0;
752}
753
Andrew Brestickere9de6882014-09-18 14:47:27 -0700754static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
755 irq_hw_number_t hw)
756{
757 if (GIC_HWIRQ_TO_LOCAL(hw) < GIC_NUM_LOCAL_INTRS)
758 return gic_local_irq_domain_map(d, virq, hw);
759 return gic_shared_irq_domain_map(d, virq, hw);
760}
761
Andrew Brestickera7057272014-11-12 11:43:38 -0800762static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
763 const u32 *intspec, unsigned int intsize,
764 irq_hw_number_t *out_hwirq,
765 unsigned int *out_type)
766{
767 if (intsize != 3)
768 return -EINVAL;
769
770 if (intspec[0] == GIC_SHARED)
771 *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
772 else if (intspec[0] == GIC_LOCAL)
773 *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
774 else
775 return -EINVAL;
776 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
777
778 return 0;
779}
780
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900781static const struct irq_domain_ops gic_irq_domain_ops = {
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700782 .map = gic_irq_domain_map,
Andrew Brestickera7057272014-11-12 11:43:38 -0800783 .xlate = gic_irq_domain_xlate,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700784};
785
Andrew Brestickera7057272014-11-12 11:43:38 -0800786static void __init __gic_init(unsigned long gic_base_addr,
787 unsigned long gic_addrspace_size,
788 unsigned int cpu_vec, unsigned int irqbase,
789 struct device_node *node)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100790{
791 unsigned int gicconfig;
792
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700793 gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100794
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700795 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700796 gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100797 GIC_SH_CONFIG_NUMINTRS_SHF;
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700798 gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100799
Andrew Brestickere9de6882014-09-18 14:47:27 -0700800 gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100801 GIC_SH_CONFIG_NUMVPES_SHF;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700802 gic_vpes = gic_vpes + 1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100803
Andrew Bresticker18743d22014-09-18 14:47:24 -0700804 if (cpu_has_veic) {
805 /* Always use vector 1 in EIC mode */
806 gic_cpu_pin = 0;
James Hogan1b6af712015-01-19 15:38:24 +0000807 timer_cpu_pin = gic_cpu_pin;
Andrew Bresticker18743d22014-09-18 14:47:24 -0700808 set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
809 __gic_irq_dispatch);
810 } else {
811 gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
812 irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
813 gic_irq_dispatch);
James Hogan1b6af712015-01-19 15:38:24 +0000814 /*
815 * With the CMP implementation of SMP (deprecated), other CPUs
816 * are started by the bootloader and put into a timer based
817 * waiting poll loop. We must not re-route those CPU's local
818 * timer interrupts as the wait instruction will never finish,
819 * so just handle whatever CPU interrupt it is routed to by
820 * default.
821 *
822 * This workaround should be removed when CMP support is
823 * dropped.
824 */
825 if (IS_ENABLED(CONFIG_MIPS_CMP) &&
826 gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100827 timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
James Hogan1b6af712015-01-19 15:38:24 +0000828 GIC_VPE_TIMER_MAP)) &
829 GIC_MAP_MSK;
830 irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
831 GIC_CPU_PIN_OFFSET +
832 timer_cpu_pin,
833 gic_irq_dispatch);
834 } else {
835 timer_cpu_pin = gic_cpu_pin;
836 }
Andrew Bresticker18743d22014-09-18 14:47:24 -0700837 }
838
Andrew Brestickera7057272014-11-12 11:43:38 -0800839 gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
Andrew Brestickere9de6882014-09-18 14:47:27 -0700840 gic_shared_intrs, irqbase,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700841 &gic_irq_domain_ops, NULL);
842 if (!gic_irq_domain)
843 panic("Failed to add GIC IRQ domain");
Steven J. Hill0b271f52012-08-31 16:05:37 -0500844
Andrew Brestickere9de6882014-09-18 14:47:27 -0700845 gic_basic_init();
Andrew Bresticker18743d22014-09-18 14:47:24 -0700846
847 gic_ipi_init();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100848}
Andrew Brestickera7057272014-11-12 11:43:38 -0800849
850void __init gic_init(unsigned long gic_base_addr,
851 unsigned long gic_addrspace_size,
852 unsigned int cpu_vec, unsigned int irqbase)
853{
854 __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
855}
856
857static int __init gic_of_init(struct device_node *node,
858 struct device_node *parent)
859{
860 struct resource res;
861 unsigned int cpu_vec, i = 0, reserved = 0;
862 phys_addr_t gic_base;
863 size_t gic_len;
864
865 /* Find the first available CPU vector. */
866 while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
867 i++, &cpu_vec))
868 reserved |= BIT(cpu_vec);
869 for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
870 if (!(reserved & BIT(cpu_vec)))
871 break;
872 }
873 if (cpu_vec == 8) {
874 pr_err("No CPU vectors available for GIC\n");
875 return -ENODEV;
876 }
877
878 if (of_address_to_resource(node, 0, &res)) {
879 /*
880 * Probe the CM for the GIC base address if not specified
881 * in the device-tree.
882 */
883 if (mips_cm_present()) {
884 gic_base = read_gcr_gic_base() &
885 ~CM_GCR_GIC_BASE_GICEN_MSK;
886 gic_len = 0x20000;
887 } else {
888 pr_err("Failed to get GIC memory range\n");
889 return -ENODEV;
890 }
891 } else {
892 gic_base = res.start;
893 gic_len = resource_size(&res);
894 }
895
896 if (mips_cm_present())
897 write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN_MSK);
898 gic_present = true;
899
900 __gic_init(gic_base, gic_len, cpu_vec, 0, node);
901
902 return 0;
903}
904IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);