blob: 7445c3b58c44766be932f7f2c633fe2d1f8e2cd4 [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 Porquet41a83e02015-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
Paul Burtone83f7e02017-08-12 19:49:41 -070020#include <asm/mips-cps.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;
Paul Burton582e2b42017-08-12 21:36:10 -070027void __iomem *mips_gic_base;
Steven J. Hill98b67c32012-08-31 16:18:49 -050028
Jeffrey Deans822350b2014-07-17 09:20:53 +010029struct gic_pcpu_mask {
Andrew Brestickerfbd55242014-09-18 14:47:25 -070030 DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS);
Jeffrey Deans822350b2014-07-17 09:20:53 +010031};
32
Alex Smithc0a9f722015-10-12 10:40:43 +010033static unsigned long __gic_base_addr;
Steven J. Hill0b271f52012-08-31 16:05:37 -050034static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
Andrew Bresticker95150ae2014-09-18 14:47:21 -070035static DEFINE_SPINLOCK(gic_lock);
Andrew Brestickerc49581a2014-09-18 14:47:23 -070036static struct irq_domain *gic_irq_domain;
Qais Yousef2af70a92015-12-08 13:20:23 +000037static struct irq_domain *gic_ipi_domain;
Andrew Brestickerfbd55242014-09-18 14:47:25 -070038static int gic_shared_intrs;
Andrew Brestickere9de6882014-09-18 14:47:27 -070039static int gic_vpes;
Andrew Bresticker3263d082014-09-18 14:47:28 -070040static unsigned int gic_cpu_pin;
James Hogan1b6af712015-01-19 15:38:24 +000041static unsigned int timer_cpu_pin;
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -070042static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
Qais Yousef2af70a92015-12-08 13:20:23 +000043DECLARE_BITMAP(ipi_resrv, GIC_MAX_INTRS);
Paul Burtonf8dcd9e2017-04-20 10:07:34 +010044DECLARE_BITMAP(ipi_available, GIC_MAX_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +010045
Andrew Bresticker18743d22014-09-18 14:47:24 -070046static void __gic_irq_dispatch(void);
47
Markos Chandrasc3f57f02015-07-14 10:26:09 +010048static inline u32 gic_read32(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070049{
Paul Burton582e2b42017-08-12 21:36:10 -070050 return __raw_readl(mips_gic_base + reg);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070051}
52
Markos Chandrasc3f57f02015-07-14 10:26:09 +010053static inline u64 gic_read64(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070054{
Paul Burton582e2b42017-08-12 21:36:10 -070055 return __raw_readq(mips_gic_base + reg);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070056}
57
Markos Chandrasc3f57f02015-07-14 10:26:09 +010058static inline unsigned long gic_read(unsigned int reg)
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070059{
Markos Chandrasc3f57f02015-07-14 10:26:09 +010060 if (!mips_cm_is64)
61 return gic_read32(reg);
62 else
63 return gic_read64(reg);
64}
65
66static inline void gic_write32(unsigned int reg, u32 val)
67{
Paul Burton582e2b42017-08-12 21:36:10 -070068 return __raw_writel(val, mips_gic_base + reg);
Markos Chandrasc3f57f02015-07-14 10:26:09 +010069}
70
71static inline void gic_write64(unsigned int reg, u64 val)
72{
Paul Burton582e2b42017-08-12 21:36:10 -070073 return __raw_writeq(val, mips_gic_base + reg);
Markos Chandrasc3f57f02015-07-14 10:26:09 +010074}
75
76static inline void gic_write(unsigned int reg, unsigned long val)
77{
78 if (!mips_cm_is64)
79 return gic_write32(reg, (u32)val);
80 else
81 return gic_write64(reg, (u64)val);
82}
83
84static inline void gic_update_bits(unsigned int reg, unsigned long mask,
85 unsigned long val)
86{
87 unsigned long regval;
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070088
89 regval = gic_read(reg);
90 regval &= ~mask;
91 regval |= val;
92 gic_write(reg, regval);
93}
94
95static inline void gic_reset_mask(unsigned int intr)
96{
97 gic_write(GIC_REG(SHARED, GIC_SH_RMASK) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +010098 1ul << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -070099}
100
101static inline void gic_set_mask(unsigned int intr)
102{
103 gic_write(GIC_REG(SHARED, GIC_SH_SMASK) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100104 1ul << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700105}
106
107static inline void gic_set_polarity(unsigned int intr, unsigned int pol)
108{
109 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_POLARITY) +
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100110 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
111 (unsigned long)pol << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700112}
113
114static inline void gic_set_trigger(unsigned int intr, unsigned int trig)
115{
116 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_TRIGGER) +
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100117 GIC_INTR_OFS(intr), 1ul << GIC_INTR_BIT(intr),
118 (unsigned long)trig << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700119}
120
121static inline void gic_set_dual_edge(unsigned int intr, unsigned int dual)
122{
123 gic_update_bits(GIC_REG(SHARED, GIC_SH_SET_DUAL) + GIC_INTR_OFS(intr),
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100124 1ul << GIC_INTR_BIT(intr),
125 (unsigned long)dual << GIC_INTR_BIT(intr));
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700126}
127
128static inline void gic_map_to_pin(unsigned int intr, unsigned int pin)
129{
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100130 gic_write32(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_PIN_BASE) +
131 GIC_SH_MAP_TO_PIN(intr), GIC_MAP_TO_PIN_MSK | pin);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700132}
133
134static inline void gic_map_to_vpe(unsigned int intr, unsigned int vpe)
135{
136 gic_write(GIC_REG(SHARED, GIC_SH_INTR_MAP_TO_VPE_BASE) +
137 GIC_SH_MAP_TO_VPE_REG_OFF(intr, vpe),
138 GIC_SH_MAP_TO_VPE_REG_BIT(vpe));
139}
140
Andrew Brestickere9de6882014-09-18 14:47:27 -0700141static bool gic_local_irq_is_routable(int intr)
142{
143 u32 vpe_ctl;
144
145 /* All local interrupts are routable in EIC mode. */
146 if (cpu_has_veic)
147 return true;
148
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100149 vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700150 switch (intr) {
151 case GIC_LOCAL_INT_TIMER:
152 return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
153 case GIC_LOCAL_INT_PERFCTR:
154 return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
155 case GIC_LOCAL_INT_FDC:
156 return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
157 case GIC_LOCAL_INT_SWINT0:
158 case GIC_LOCAL_INT_SWINT1:
159 return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
160 default:
161 return true;
162 }
163}
164
Andrew Bresticker3263d082014-09-18 14:47:28 -0700165static void gic_bind_eic_interrupt(int irq, int set)
Steven J. Hill98b67c32012-08-31 16:18:49 -0500166{
167 /* Convert irq vector # to hw int # */
168 irq -= GIC_PIN_TO_VEC_OFFSET;
169
170 /* Set irq to use shadow set */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700171 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
172 GIC_VPE_EIC_SS(irq), set);
Steven J. Hill98b67c32012-08-31 16:18:49 -0500173}
174
Qais Yousefbb11cff2015-12-08 13:20:28 +0000175static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100176{
Qais Yousefbb11cff2015-12-08 13:20:28 +0000177 irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
178
179 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_SET(hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100180}
181
Andrew Brestickere9de6882014-09-18 14:47:27 -0700182int gic_get_c0_compare_int(void)
183{
184 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
185 return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
186 return irq_create_mapping(gic_irq_domain,
187 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
188}
189
190int gic_get_c0_perfcount_int(void)
191{
192 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
James Hogan7e3e6cb2015-01-27 21:45:50 +0000193 /* Is the performance counter shared with the timer? */
Andrew Brestickere9de6882014-09-18 14:47:27 -0700194 if (cp0_perfcount_irq < 0)
195 return -1;
196 return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
197 }
198 return irq_create_mapping(gic_irq_domain,
199 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
200}
201
James Hogan6429e2b2015-01-29 11:14:09 +0000202int gic_get_c0_fdc_int(void)
203{
204 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
205 /* Is the FDC IRQ even present? */
206 if (cp0_fdc_irq < 0)
207 return -1;
208 return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
209 }
210
James Hogan6429e2b2015-01-29 11:14:09 +0000211 return irq_create_mapping(gic_irq_domain,
212 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
213}
214
Alex Smithc0a9f722015-10-12 10:40:43 +0100215int gic_get_usm_range(struct resource *gic_usm_res)
216{
217 if (!gic_present)
218 return -1;
219
220 gic_usm_res->start = __gic_base_addr + USM_VISIBLE_SECTION_OFS;
221 gic_usm_res->end = gic_usm_res->start + (USM_VISIBLE_SECTION_SIZE - 1);
222
223 return 0;
224}
225
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200226static void gic_handle_shared_int(bool chained)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100227{
Paul Burtone98fcb22017-08-12 21:36:16 -0700228 unsigned int intr, virq;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700229 unsigned long *pcpu_mask;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700230 DECLARE_BITMAP(pending, GIC_MAX_INTRS);
231 DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100232
233 /* Get per-cpu bitmaps */
Ralf Baechle39b8d522008-04-28 17:14:26 +0100234 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
235
Paul Burtone98fcb22017-08-12 21:36:16 -0700236 if (mips_cm_is64) {
237 __ioread64_copy(pending, addr_gic_pend(),
238 DIV_ROUND_UP(gic_shared_intrs, 64));
239 __ioread64_copy(intrmask, addr_gic_mask(),
240 DIV_ROUND_UP(gic_shared_intrs, 64));
241 } else {
242 __ioread32_copy(pending, addr_gic_pend(),
243 DIV_ROUND_UP(gic_shared_intrs, 32));
244 __ioread32_copy(intrmask, addr_gic_mask(),
245 DIV_ROUND_UP(gic_shared_intrs, 32));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100246 }
247
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700248 bitmap_and(pending, pending, intrmask, gic_shared_intrs);
249 bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100250
Paul Burtoncae750b2016-08-19 18:11:19 +0100251 for_each_set_bit(intr, pending, gic_shared_intrs) {
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000252 virq = irq_linear_revmap(gic_irq_domain,
253 GIC_SHARED_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200254 if (chained)
255 generic_handle_irq(virq);
256 else
257 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000258 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100259}
260
Thomas Gleixner161d0492011-03-23 21:08:58 +0000261static void gic_mask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100262{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700263 gic_reset_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100264}
265
Thomas Gleixner161d0492011-03-23 21:08:58 +0000266static void gic_unmask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100267{
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700268 gic_set_mask(GIC_HWIRQ_TO_SHARED(d->hwirq));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100269}
270
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700271static void gic_ack_irq(struct irq_data *d)
272{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700273 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700274
Andrew Bresticker53a7bc82014-10-20 12:03:57 -0700275 gic_write(GIC_REG(SHARED, GIC_SH_WEDGE), GIC_SH_WEDGE_CLR(irq));
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700276}
277
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700278static int gic_set_type(struct irq_data *d, unsigned int type)
279{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700280 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700281 unsigned long flags;
282 bool is_edge;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100283
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700284 spin_lock_irqsave(&gic_lock, flags);
285 switch (type & IRQ_TYPE_SENSE_MASK) {
286 case IRQ_TYPE_EDGE_FALLING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700287 gic_set_polarity(irq, GIC_POL_NEG);
288 gic_set_trigger(irq, GIC_TRIG_EDGE);
289 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700290 is_edge = true;
291 break;
292 case IRQ_TYPE_EDGE_RISING:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700293 gic_set_polarity(irq, GIC_POL_POS);
294 gic_set_trigger(irq, GIC_TRIG_EDGE);
295 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700296 is_edge = true;
297 break;
298 case IRQ_TYPE_EDGE_BOTH:
299 /* polarity is irrelevant in this case */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700300 gic_set_trigger(irq, GIC_TRIG_EDGE);
301 gic_set_dual_edge(irq, GIC_TRIG_DUAL_ENABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700302 is_edge = true;
303 break;
304 case IRQ_TYPE_LEVEL_LOW:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700305 gic_set_polarity(irq, GIC_POL_NEG);
306 gic_set_trigger(irq, GIC_TRIG_LEVEL);
307 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700308 is_edge = false;
309 break;
310 case IRQ_TYPE_LEVEL_HIGH:
311 default:
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700312 gic_set_polarity(irq, GIC_POL_POS);
313 gic_set_trigger(irq, GIC_TRIG_LEVEL);
314 gic_set_dual_edge(irq, GIC_TRIG_DUAL_DISABLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700315 is_edge = false;
316 break;
317 }
318
Thomas Gleixnera595fc52015-06-23 14:41:25 +0200319 if (is_edge)
320 irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
321 handle_edge_irq, NULL);
322 else
323 irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
324 handle_level_irq, NULL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700325 spin_unlock_irqrestore(&gic_lock, flags);
326
327 return 0;
328}
329
330#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000331static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
332 bool force)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100333{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700334 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100335 cpumask_t tmp = CPU_MASK_NONE;
336 unsigned long flags;
337 int i;
338
Rusty Russell0de26522008-12-13 21:20:26 +1030339 cpumask_and(&tmp, cpumask, cpu_online_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030340 if (cpumask_empty(&tmp))
Andrew Bresticker14d160a2014-09-18 14:47:22 -0700341 return -EINVAL;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100342
343 /* Assumption : cpumask refers to a single CPU */
344 spin_lock_irqsave(&gic_lock, flags);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100345
Tony Wuc214c032013-06-21 10:13:08 +0000346 /* Re-route this IRQ */
Paul Burtonab41f6c2015-09-22 11:29:10 -0700347 gic_map_to_vpe(irq, mips_cm_vp_id(cpumask_first(&tmp)));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100348
Tony Wuc214c032013-06-21 10:13:08 +0000349 /* Update the pcpu_masks */
Paul Burton91951f92016-04-21 11:31:54 +0100350 for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
Tony Wuc214c032013-06-21 10:13:08 +0000351 clear_bit(irq, pcpu_masks[i].pcpu_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030352 set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
Tony Wuc214c032013-06-21 10:13:08 +0000353
Jiang Liu72f86db2015-06-01 16:05:38 +0800354 cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100355 spin_unlock_irqrestore(&gic_lock, flags);
356
Thomas Gleixner161d0492011-03-23 21:08:58 +0000357 return IRQ_SET_MASK_OK_NOCOPY;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100358}
359#endif
360
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700361static struct irq_chip gic_level_irq_controller = {
362 .name = "MIPS GIC",
363 .irq_mask = gic_mask_irq,
364 .irq_unmask = gic_unmask_irq,
365 .irq_set_type = gic_set_type,
366#ifdef CONFIG_SMP
367 .irq_set_affinity = gic_set_affinity,
368#endif
369};
370
371static struct irq_chip gic_edge_irq_controller = {
Thomas Gleixner161d0492011-03-23 21:08:58 +0000372 .name = "MIPS GIC",
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700373 .irq_ack = gic_ack_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000374 .irq_mask = gic_mask_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000375 .irq_unmask = gic_unmask_irq,
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700376 .irq_set_type = gic_set_type,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100377#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000378 .irq_set_affinity = gic_set_affinity,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100379#endif
Qais Yousefbb11cff2015-12-08 13:20:28 +0000380 .ipi_send_single = gic_send_ipi,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100381};
382
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200383static void gic_handle_local_int(bool chained)
Andrew Brestickere9de6882014-09-18 14:47:27 -0700384{
385 unsigned long pending, masked;
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000386 unsigned int intr, virq;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700387
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100388 pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
389 masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700390
391 bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
392
Paul Burton0f4ed152016-09-13 17:54:27 +0100393 for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) {
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000394 virq = irq_linear_revmap(gic_irq_domain,
395 GIC_LOCAL_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200396 if (chained)
397 generic_handle_irq(virq);
398 else
399 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000400 }
Andrew Brestickere9de6882014-09-18 14:47:27 -0700401}
402
403static void gic_mask_local_irq(struct irq_data *d)
404{
405 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
406
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100407 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700408}
409
410static void gic_unmask_local_irq(struct irq_data *d)
411{
412 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
413
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100414 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700415}
416
417static struct irq_chip gic_local_irq_controller = {
418 .name = "MIPS GIC Local",
419 .irq_mask = gic_mask_local_irq,
420 .irq_unmask = gic_unmask_local_irq,
421};
422
423static void gic_mask_local_irq_all_vpes(struct irq_data *d)
424{
425 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
426 int i;
427 unsigned long flags;
428
429 spin_lock_irqsave(&gic_lock, flags);
430 for (i = 0; i < gic_vpes; i++) {
Paul Burtond46812b2016-02-03 03:15:27 +0000431 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
432 mips_cm_vp_id(i));
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100433 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700434 }
435 spin_unlock_irqrestore(&gic_lock, flags);
436}
437
438static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
439{
440 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
441 int i;
442 unsigned long flags;
443
444 spin_lock_irqsave(&gic_lock, flags);
445 for (i = 0; i < gic_vpes; i++) {
Paul Burtond46812b2016-02-03 03:15:27 +0000446 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
447 mips_cm_vp_id(i));
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100448 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700449 }
450 spin_unlock_irqrestore(&gic_lock, flags);
451}
452
453static struct irq_chip gic_all_vpes_local_irq_controller = {
454 .name = "MIPS GIC Local",
455 .irq_mask = gic_mask_local_irq_all_vpes,
456 .irq_unmask = gic_unmask_local_irq_all_vpes,
457};
458
Andrew Bresticker18743d22014-09-18 14:47:24 -0700459static void __gic_irq_dispatch(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100460{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200461 gic_handle_local_int(false);
462 gic_handle_shared_int(false);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700463}
464
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200465static void gic_irq_dispatch(struct irq_desc *desc)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700466{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200467 gic_handle_local_int(true);
468 gic_handle_shared_int(true);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700469}
470
Andrew Brestickere9de6882014-09-18 14:47:27 -0700471static void __init gic_basic_init(void)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700472{
473 unsigned int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500474
475 board_bind_eic_interrupt = &gic_bind_eic_interrupt;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100476
477 /* Setup defaults */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700478 for (i = 0; i < gic_shared_intrs; i++) {
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700479 gic_set_polarity(i, GIC_POL_POS);
480 gic_set_trigger(i, GIC_TRIG_LEVEL);
481 gic_reset_mask(i);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100482 }
483
Andrew Brestickere9de6882014-09-18 14:47:27 -0700484 for (i = 0; i < gic_vpes; i++) {
485 unsigned int j;
486
Paul Burtond46812b2016-02-03 03:15:27 +0000487 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
488 mips_cm_vp_id(i));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700489 for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
490 if (!gic_local_irq_is_routable(j))
491 continue;
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100492 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700493 }
494 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100495}
496
Andrew Brestickere9de6882014-09-18 14:47:27 -0700497static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
498 irq_hw_number_t hw)
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700499{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700500 int intr = GIC_HWIRQ_TO_LOCAL(hw);
501 int ret = 0;
502 int i;
503 unsigned long flags;
504
505 if (!gic_local_irq_is_routable(intr))
506 return -EPERM;
507
Andrew Brestickere9de6882014-09-18 14:47:27 -0700508 spin_lock_irqsave(&gic_lock, flags);
509 for (i = 0; i < gic_vpes; i++) {
510 u32 val = GIC_MAP_TO_PIN_MSK | gic_cpu_pin;
511
Paul Burtond46812b2016-02-03 03:15:27 +0000512 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
513 mips_cm_vp_id(i));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700514
515 switch (intr) {
516 case GIC_LOCAL_INT_WD:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100517 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_WD_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700518 break;
519 case GIC_LOCAL_INT_COMPARE:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100520 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_MAP),
521 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700522 break;
523 case GIC_LOCAL_INT_TIMER:
James Hogan1b6af712015-01-19 15:38:24 +0000524 /* CONFIG_MIPS_CMP workaround (see __gic_init) */
525 val = GIC_MAP_TO_PIN_MSK | timer_cpu_pin;
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100526 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
527 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700528 break;
529 case GIC_LOCAL_INT_PERFCTR:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100530 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
531 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700532 break;
533 case GIC_LOCAL_INT_SWINT0:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100534 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT0_MAP),
535 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700536 break;
537 case GIC_LOCAL_INT_SWINT1:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100538 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SWINT1_MAP),
539 val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700540 break;
541 case GIC_LOCAL_INT_FDC:
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100542 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_FDC_MAP), val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700543 break;
544 default:
545 pr_err("Invalid local IRQ %d\n", intr);
546 ret = -EINVAL;
547 break;
548 }
549 }
550 spin_unlock_irqrestore(&gic_lock, flags);
551
552 return ret;
553}
554
555static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
Qais Yousef2af70a92015-12-08 13:20:23 +0000556 irq_hw_number_t hw, unsigned int vpe)
Andrew Brestickere9de6882014-09-18 14:47:27 -0700557{
558 int intr = GIC_HWIRQ_TO_SHARED(hw);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700559 unsigned long flags;
Qais Yousef78930f02015-12-08 13:20:26 +0000560 int i;
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700561
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700562 spin_lock_irqsave(&gic_lock, flags);
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700563 gic_map_to_pin(intr, gic_cpu_pin);
Paul Burton99ec8a32016-07-05 14:25:59 +0100564 gic_map_to_vpe(intr, mips_cm_vp_id(vpe));
Paul Burton91951f92016-04-21 11:31:54 +0100565 for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
Qais Yousef78930f02015-12-08 13:20:26 +0000566 clear_bit(intr, pcpu_masks[i].pcpu_mask);
Qais Yousef2af70a92015-12-08 13:20:23 +0000567 set_bit(intr, pcpu_masks[vpe].pcpu_mask);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700568 spin_unlock_irqrestore(&gic_lock, flags);
569
570 return 0;
571}
572
Paul Burtonb87281e2017-04-20 10:07:35 +0100573static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
Qais Yousefc98c18222015-12-08 13:20:24 +0000574 const u32 *intspec, unsigned int intsize,
575 irq_hw_number_t *out_hwirq,
576 unsigned int *out_type)
577{
578 if (intsize != 3)
579 return -EINVAL;
580
581 if (intspec[0] == GIC_SHARED)
582 *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
583 else if (intspec[0] == GIC_LOCAL)
584 *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
585 else
586 return -EINVAL;
587 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
588
589 return 0;
590}
591
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100592static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
593 irq_hw_number_t hwirq)
Qais Yousefc98c18222015-12-08 13:20:24 +0000594{
Paul Burtonb87281e2017-04-20 10:07:35 +0100595 int err;
Qais Yousefc98c18222015-12-08 13:20:24 +0000596
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100597 if (hwirq >= GIC_SHARED_HWIRQ_BASE) {
Paul Burtonb87281e2017-04-20 10:07:35 +0100598 /* verify that shared irqs don't conflict with an IPI irq */
599 if (test_bit(GIC_HWIRQ_TO_SHARED(hwirq), ipi_resrv))
600 return -EBUSY;
Qais Yousefc98c18222015-12-08 13:20:24 +0000601
Paul Burtonb87281e2017-04-20 10:07:35 +0100602 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
603 &gic_level_irq_controller,
604 NULL);
605 if (err)
606 return err;
607
608 return gic_shared_irq_domain_map(d, virq, hwirq, 0);
Qais Yousefc98c18222015-12-08 13:20:24 +0000609 }
610
Paul Burtonb87281e2017-04-20 10:07:35 +0100611 switch (GIC_HWIRQ_TO_LOCAL(hwirq)) {
612 case GIC_LOCAL_INT_TIMER:
613 case GIC_LOCAL_INT_PERFCTR:
614 case GIC_LOCAL_INT_FDC:
615 /*
616 * HACK: These are all really percpu interrupts, but
617 * the rest of the MIPS kernel code does not use the
618 * percpu IRQ API for them.
619 */
620 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
621 &gic_all_vpes_local_irq_controller,
622 NULL);
623 if (err)
624 return err;
625
626 irq_set_handler(virq, handle_percpu_irq);
627 break;
628
629 default:
630 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
631 &gic_local_irq_controller,
632 NULL);
633 if (err)
634 return err;
635
636 irq_set_handler(virq, handle_percpu_devid_irq);
637 irq_set_percpu_devid(virq);
638 break;
639 }
640
641 return gic_local_irq_domain_map(d, virq, hwirq);
Qais Yousefc98c18222015-12-08 13:20:24 +0000642}
643
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100644static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
645 unsigned int nr_irqs, void *arg)
646{
647 struct irq_fwspec *fwspec = arg;
648 irq_hw_number_t hwirq;
649
650 if (fwspec->param[0] == GIC_SHARED)
651 hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]);
652 else
653 hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]);
654
655 return gic_irq_domain_map(d, virq, hwirq);
656}
657
Paul Burtonb87281e2017-04-20 10:07:35 +0100658void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
Qais Yousefc98c18222015-12-08 13:20:24 +0000659 unsigned int nr_irqs)
660{
Qais Yousefc98c18222015-12-08 13:20:24 +0000661}
662
Paul Burtonb87281e2017-04-20 10:07:35 +0100663static const struct irq_domain_ops gic_irq_domain_ops = {
664 .xlate = gic_irq_domain_xlate,
665 .alloc = gic_irq_domain_alloc,
666 .free = gic_irq_domain_free,
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100667 .map = gic_irq_domain_map,
Qais Yousef2af70a92015-12-08 13:20:23 +0000668};
669
670static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
671 const u32 *intspec, unsigned int intsize,
672 irq_hw_number_t *out_hwirq,
673 unsigned int *out_type)
674{
675 /*
676 * There's nothing to translate here. hwirq is dynamically allocated and
677 * the irq type is always edge triggered.
678 * */
679 *out_hwirq = 0;
680 *out_type = IRQ_TYPE_EDGE_RISING;
681
682 return 0;
683}
684
685static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
686 unsigned int nr_irqs, void *arg)
687{
688 struct cpumask *ipimask = arg;
Paul Burtonb87281e2017-04-20 10:07:35 +0100689 irq_hw_number_t hwirq, base_hwirq;
690 int cpu, ret, i;
Qais Yousef2af70a92015-12-08 13:20:23 +0000691
Paul Burtonb87281e2017-04-20 10:07:35 +0100692 base_hwirq = find_first_bit(ipi_available, gic_shared_intrs);
693 if (base_hwirq == gic_shared_intrs)
694 return -ENOMEM;
Qais Yousef2af70a92015-12-08 13:20:23 +0000695
Paul Burtonb87281e2017-04-20 10:07:35 +0100696 /* check that we have enough space */
697 for (i = base_hwirq; i < nr_irqs; i++) {
698 if (!test_bit(i, ipi_available))
699 return -EBUSY;
700 }
701 bitmap_clear(ipi_available, base_hwirq, nr_irqs);
702
703 /* map the hwirq for each cpu consecutively */
704 i = 0;
705 for_each_cpu(cpu, ipimask) {
706 hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
707
708 ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
709 &gic_edge_irq_controller,
710 NULL);
711 if (ret)
712 goto error;
713
714 ret = irq_domain_set_hwirq_and_chip(d->parent, virq + i, hwirq,
Qais Yousef2af70a92015-12-08 13:20:23 +0000715 &gic_edge_irq_controller,
716 NULL);
717 if (ret)
718 goto error;
719
720 ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
721 if (ret)
722 goto error;
Paul Burtonb87281e2017-04-20 10:07:35 +0100723
724 ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
725 if (ret)
726 goto error;
727
728 i++;
Qais Yousef2af70a92015-12-08 13:20:23 +0000729 }
730
731 return 0;
732error:
Paul Burtonb87281e2017-04-20 10:07:35 +0100733 bitmap_set(ipi_available, base_hwirq, nr_irqs);
Qais Yousef2af70a92015-12-08 13:20:23 +0000734 return ret;
735}
736
737void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
738 unsigned int nr_irqs)
739{
Paul Burtonb87281e2017-04-20 10:07:35 +0100740 irq_hw_number_t base_hwirq;
741 struct irq_data *data;
742
743 data = irq_get_irq_data(virq);
744 if (!data)
745 return;
746
747 base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
748 bitmap_set(ipi_available, base_hwirq, nr_irqs);
Qais Yousef2af70a92015-12-08 13:20:23 +0000749}
750
751int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
752 enum irq_domain_bus_token bus_token)
753{
754 bool is_ipi;
755
756 switch (bus_token) {
757 case DOMAIN_BUS_IPI:
758 is_ipi = d->bus_token == bus_token;
Paul Burton547aefc2016-07-05 14:26:00 +0100759 return (!node || to_of_node(d->fwnode) == node) && is_ipi;
Qais Yousef2af70a92015-12-08 13:20:23 +0000760 break;
761 default:
762 return 0;
763 }
764}
765
Tobias Klauser0b7e8152017-06-02 10:20:56 +0200766static const struct irq_domain_ops gic_ipi_domain_ops = {
Qais Yousef2af70a92015-12-08 13:20:23 +0000767 .xlate = gic_ipi_domain_xlate,
768 .alloc = gic_ipi_domain_alloc,
769 .free = gic_ipi_domain_free,
770 .match = gic_ipi_domain_match,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700771};
772
Andrew Brestickera7057272014-11-12 11:43:38 -0800773static void __init __gic_init(unsigned long gic_base_addr,
774 unsigned long gic_addrspace_size,
775 unsigned int cpu_vec, unsigned int irqbase,
776 struct device_node *node)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100777{
Paul Burtonba01cf02016-05-17 15:31:06 +0100778 unsigned int gicconfig, cpu;
Qais Yousef16a80832015-12-08 13:20:30 +0000779 unsigned int v[2];
Ralf Baechle39b8d522008-04-28 17:14:26 +0100780
Alex Smithc0a9f722015-10-12 10:40:43 +0100781 __gic_base_addr = gic_base_addr;
782
Paul Burton582e2b42017-08-12 21:36:10 -0700783 mips_gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100784
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700785 gicconfig = gic_read(GIC_REG(SHARED, GIC_SH_CONFIG));
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700786 gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100787 GIC_SH_CONFIG_NUMINTRS_SHF;
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700788 gic_shared_intrs = ((gic_shared_intrs + 1) * 8);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100789
Andrew Brestickere9de6882014-09-18 14:47:27 -0700790 gic_vpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
Ralf Baechle39b8d522008-04-28 17:14:26 +0100791 GIC_SH_CONFIG_NUMVPES_SHF;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700792 gic_vpes = gic_vpes + 1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100793
Andrew Bresticker18743d22014-09-18 14:47:24 -0700794 if (cpu_has_veic) {
Paul Burtonba01cf02016-05-17 15:31:06 +0100795 /* Set EIC mode for all VPEs */
796 for_each_present_cpu(cpu) {
797 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
798 mips_cm_vp_id(cpu));
799 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_CTL),
800 GIC_VPE_CTL_EIC_MODE_MSK);
801 }
802
Andrew Bresticker18743d22014-09-18 14:47:24 -0700803 /* Always use vector 1 in EIC mode */
804 gic_cpu_pin = 0;
James Hogan1b6af712015-01-19 15:38:24 +0000805 timer_cpu_pin = gic_cpu_pin;
Andrew Bresticker18743d22014-09-18 14:47:24 -0700806 set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
807 __gic_irq_dispatch);
808 } else {
809 gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
810 irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
811 gic_irq_dispatch);
James Hogan1b6af712015-01-19 15:38:24 +0000812 /*
813 * With the CMP implementation of SMP (deprecated), other CPUs
814 * are started by the bootloader and put into a timer based
815 * waiting poll loop. We must not re-route those CPU's local
816 * timer interrupts as the wait instruction will never finish,
817 * so just handle whatever CPU interrupt it is routed to by
818 * default.
819 *
820 * This workaround should be removed when CMP support is
821 * dropped.
822 */
823 if (IS_ENABLED(CONFIG_MIPS_CMP) &&
824 gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100825 timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
James Hogan1b6af712015-01-19 15:38:24 +0000826 GIC_VPE_TIMER_MAP)) &
827 GIC_MAP_MSK;
828 irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
829 GIC_CPU_PIN_OFFSET +
830 timer_cpu_pin,
831 gic_irq_dispatch);
832 } else {
833 timer_cpu_pin = gic_cpu_pin;
834 }
Andrew Bresticker18743d22014-09-18 14:47:24 -0700835 }
836
Andrew Brestickera7057272014-11-12 11:43:38 -0800837 gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
Andrew Brestickere9de6882014-09-18 14:47:27 -0700838 gic_shared_intrs, irqbase,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700839 &gic_irq_domain_ops, NULL);
840 if (!gic_irq_domain)
841 panic("Failed to add GIC IRQ domain");
Steven J. Hill0b271f52012-08-31 16:05:37 -0500842
Qais Yousef2af70a92015-12-08 13:20:23 +0000843 gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
844 IRQ_DOMAIN_FLAG_IPI_PER_CPU,
845 GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
846 node, &gic_ipi_domain_ops, NULL);
847 if (!gic_ipi_domain)
848 panic("Failed to add GIC IPI domain");
849
Marc Zyngier96f0d932017-06-22 11:42:50 +0100850 irq_domain_update_bus_token(gic_ipi_domain, DOMAIN_BUS_IPI);
Qais Yousef2af70a92015-12-08 13:20:23 +0000851
Qais Yousef16a80832015-12-08 13:20:30 +0000852 if (node &&
853 !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
854 bitmap_set(ipi_resrv, v[0], v[1]);
855 } else {
856 /* Make the last 2 * gic_vpes available for IPIs */
857 bitmap_set(ipi_resrv,
858 gic_shared_intrs - 2 * gic_vpes,
859 2 * gic_vpes);
860 }
Qais Yousef2af70a92015-12-08 13:20:23 +0000861
Paul Burtonf8dcd9e2017-04-20 10:07:34 +0100862 bitmap_copy(ipi_available, ipi_resrv, GIC_MAX_INTRS);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700863 gic_basic_init();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100864}
Andrew Brestickera7057272014-11-12 11:43:38 -0800865
866void __init gic_init(unsigned long gic_base_addr,
867 unsigned long gic_addrspace_size,
868 unsigned int cpu_vec, unsigned int irqbase)
869{
870 __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
871}
872
873static int __init gic_of_init(struct device_node *node,
874 struct device_node *parent)
875{
876 struct resource res;
877 unsigned int cpu_vec, i = 0, reserved = 0;
878 phys_addr_t gic_base;
879 size_t gic_len;
880
881 /* Find the first available CPU vector. */
882 while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
883 i++, &cpu_vec))
884 reserved |= BIT(cpu_vec);
885 for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
886 if (!(reserved & BIT(cpu_vec)))
887 break;
888 }
889 if (cpu_vec == 8) {
890 pr_err("No CPU vectors available for GIC\n");
891 return -ENODEV;
892 }
893
894 if (of_address_to_resource(node, 0, &res)) {
895 /*
896 * Probe the CM for the GIC base address if not specified
897 * in the device-tree.
898 */
899 if (mips_cm_present()) {
900 gic_base = read_gcr_gic_base() &
Paul Burton93c5bba52017-08-12 19:49:27 -0700901 ~CM_GCR_GIC_BASE_GICEN;
Andrew Brestickera7057272014-11-12 11:43:38 -0800902 gic_len = 0x20000;
903 } else {
904 pr_err("Failed to get GIC memory range\n");
905 return -ENODEV;
906 }
907 } else {
908 gic_base = res.start;
909 gic_len = resource_size(&res);
910 }
911
James Hogana0ffec32017-08-12 21:36:09 -0700912 if (mips_cm_present()) {
Paul Burton93c5bba52017-08-12 19:49:27 -0700913 write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN);
James Hogana0ffec32017-08-12 21:36:09 -0700914 /* Ensure GIC region is enabled before trying to access it */
915 __sync();
916 }
Andrew Brestickera7057272014-11-12 11:43:38 -0800917 gic_present = true;
918
919 __gic_init(gic_base, gic_len, cpu_vec, 0, node);
920
921 return 0;
922}
923IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);