blob: a906284215b7d989c8995c57acbf5e7788b9de9c [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
Andrew Brestickere9de6882014-09-18 14:47:27 -070084static bool gic_local_irq_is_routable(int intr)
85{
86 u32 vpe_ctl;
87
88 /* All local interrupts are routable in EIC mode. */
89 if (cpu_has_veic)
90 return true;
91
Markos Chandrasc3f57f02015-07-14 10:26:09 +010092 vpe_ctl = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_CTL));
Andrew Brestickere9de6882014-09-18 14:47:27 -070093 switch (intr) {
94 case GIC_LOCAL_INT_TIMER:
95 return vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK;
96 case GIC_LOCAL_INT_PERFCTR:
97 return vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK;
98 case GIC_LOCAL_INT_FDC:
99 return vpe_ctl & GIC_VPE_CTL_FDC_RTBL_MSK;
100 case GIC_LOCAL_INT_SWINT0:
101 case GIC_LOCAL_INT_SWINT1:
102 return vpe_ctl & GIC_VPE_CTL_SWINT_RTBL_MSK;
103 default:
104 return true;
105 }
106}
107
Andrew Bresticker3263d082014-09-18 14:47:28 -0700108static void gic_bind_eic_interrupt(int irq, int set)
Steven J. Hill98b67c32012-08-31 16:18:49 -0500109{
110 /* Convert irq vector # to hw int # */
111 irq -= GIC_PIN_TO_VEC_OFFSET;
112
113 /* Set irq to use shadow set */
Andrew Bresticker5f68fea2014-10-20 12:03:52 -0700114 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_EIC_SHADOW_SET_BASE) +
115 GIC_VPE_EIC_SS(irq), set);
Steven J. Hill98b67c32012-08-31 16:18:49 -0500116}
117
Qais Yousefbb11cff2015-12-08 13:20:28 +0000118static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100119{
Qais Yousefbb11cff2015-12-08 13:20:28 +0000120 irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
121
Paul Burton36807462017-08-12 21:36:24 -0700122 write_gic_wedge(GIC_WEDGE_RW | hwirq);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100123}
124
Andrew Brestickere9de6882014-09-18 14:47:27 -0700125int gic_get_c0_compare_int(void)
126{
127 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
128 return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
129 return irq_create_mapping(gic_irq_domain,
130 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
131}
132
133int gic_get_c0_perfcount_int(void)
134{
135 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
James Hogan7e3e6cb2015-01-27 21:45:50 +0000136 /* Is the performance counter shared with the timer? */
Andrew Brestickere9de6882014-09-18 14:47:27 -0700137 if (cp0_perfcount_irq < 0)
138 return -1;
139 return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
140 }
141 return irq_create_mapping(gic_irq_domain,
142 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
143}
144
James Hogan6429e2b2015-01-29 11:14:09 +0000145int gic_get_c0_fdc_int(void)
146{
147 if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
148 /* Is the FDC IRQ even present? */
149 if (cp0_fdc_irq < 0)
150 return -1;
151 return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
152 }
153
James Hogan6429e2b2015-01-29 11:14:09 +0000154 return irq_create_mapping(gic_irq_domain,
155 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
156}
157
Alex Smithc0a9f722015-10-12 10:40:43 +0100158int gic_get_usm_range(struct resource *gic_usm_res)
159{
160 if (!gic_present)
161 return -1;
162
163 gic_usm_res->start = __gic_base_addr + USM_VISIBLE_SECTION_OFS;
164 gic_usm_res->end = gic_usm_res->start + (USM_VISIBLE_SECTION_SIZE - 1);
165
166 return 0;
167}
168
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200169static void gic_handle_shared_int(bool chained)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100170{
Paul Burtone98fcb22017-08-12 21:36:16 -0700171 unsigned int intr, virq;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700172 unsigned long *pcpu_mask;
Andrew Bresticker8f5ee792014-10-20 12:03:56 -0700173 DECLARE_BITMAP(pending, GIC_MAX_INTRS);
174 DECLARE_BITMAP(intrmask, GIC_MAX_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100175
176 /* Get per-cpu bitmaps */
Ralf Baechle39b8d522008-04-28 17:14:26 +0100177 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
178
Paul Burtone98fcb22017-08-12 21:36:16 -0700179 if (mips_cm_is64) {
180 __ioread64_copy(pending, addr_gic_pend(),
181 DIV_ROUND_UP(gic_shared_intrs, 64));
182 __ioread64_copy(intrmask, addr_gic_mask(),
183 DIV_ROUND_UP(gic_shared_intrs, 64));
184 } else {
185 __ioread32_copy(pending, addr_gic_pend(),
186 DIV_ROUND_UP(gic_shared_intrs, 32));
187 __ioread32_copy(intrmask, addr_gic_mask(),
188 DIV_ROUND_UP(gic_shared_intrs, 32));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100189 }
190
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700191 bitmap_and(pending, pending, intrmask, gic_shared_intrs);
192 bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100193
Paul Burtoncae750b2016-08-19 18:11:19 +0100194 for_each_set_bit(intr, pending, gic_shared_intrs) {
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000195 virq = irq_linear_revmap(gic_irq_domain,
196 GIC_SHARED_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200197 if (chained)
198 generic_handle_irq(virq);
199 else
200 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000201 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100202}
203
Thomas Gleixner161d0492011-03-23 21:08:58 +0000204static void gic_mask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100205{
Paul Burton87554b02017-08-12 21:36:18 -0700206 write_gic_rmask(BIT(GIC_HWIRQ_TO_SHARED(d->hwirq)));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100207}
208
Thomas Gleixner161d0492011-03-23 21:08:58 +0000209static void gic_unmask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100210{
Paul Burton87554b02017-08-12 21:36:18 -0700211 write_gic_smask(BIT(GIC_HWIRQ_TO_SHARED(d->hwirq)));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100212}
213
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700214static void gic_ack_irq(struct irq_data *d)
215{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700216 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700217
Paul Burton36807462017-08-12 21:36:24 -0700218 write_gic_wedge(irq);
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700219}
220
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700221static int gic_set_type(struct irq_data *d, unsigned int type)
222{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700223 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700224 unsigned long flags;
225 bool is_edge;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100226
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700227 spin_lock_irqsave(&gic_lock, flags);
228 switch (type & IRQ_TYPE_SENSE_MASK) {
229 case IRQ_TYPE_EDGE_FALLING:
Paul Burton80e5f9c2017-08-12 21:36:19 -0700230 change_gic_pol(irq, GIC_POL_FALLING_EDGE);
Paul Burton471aa962017-08-12 21:36:20 -0700231 change_gic_trig(irq, GIC_TRIG_EDGE);
Paul Burtonc26ba672017-08-12 21:36:21 -0700232 change_gic_dual(irq, GIC_DUAL_SINGLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700233 is_edge = true;
234 break;
235 case IRQ_TYPE_EDGE_RISING:
Paul Burton80e5f9c2017-08-12 21:36:19 -0700236 change_gic_pol(irq, GIC_POL_RISING_EDGE);
Paul Burton471aa962017-08-12 21:36:20 -0700237 change_gic_trig(irq, GIC_TRIG_EDGE);
Paul Burtonc26ba672017-08-12 21:36:21 -0700238 change_gic_dual(irq, GIC_DUAL_SINGLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700239 is_edge = true;
240 break;
241 case IRQ_TYPE_EDGE_BOTH:
242 /* polarity is irrelevant in this case */
Paul Burton471aa962017-08-12 21:36:20 -0700243 change_gic_trig(irq, GIC_TRIG_EDGE);
Paul Burtonc26ba672017-08-12 21:36:21 -0700244 change_gic_dual(irq, GIC_DUAL_DUAL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700245 is_edge = true;
246 break;
247 case IRQ_TYPE_LEVEL_LOW:
Paul Burton80e5f9c2017-08-12 21:36:19 -0700248 change_gic_pol(irq, GIC_POL_ACTIVE_LOW);
Paul Burton471aa962017-08-12 21:36:20 -0700249 change_gic_trig(irq, GIC_TRIG_LEVEL);
Paul Burtonc26ba672017-08-12 21:36:21 -0700250 change_gic_dual(irq, GIC_DUAL_SINGLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700251 is_edge = false;
252 break;
253 case IRQ_TYPE_LEVEL_HIGH:
254 default:
Paul Burton80e5f9c2017-08-12 21:36:19 -0700255 change_gic_pol(irq, GIC_POL_ACTIVE_HIGH);
Paul Burton471aa962017-08-12 21:36:20 -0700256 change_gic_trig(irq, GIC_TRIG_LEVEL);
Paul Burtonc26ba672017-08-12 21:36:21 -0700257 change_gic_dual(irq, GIC_DUAL_SINGLE);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700258 is_edge = false;
259 break;
260 }
261
Thomas Gleixnera595fc52015-06-23 14:41:25 +0200262 if (is_edge)
263 irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
264 handle_edge_irq, NULL);
265 else
266 irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
267 handle_level_irq, NULL);
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700268 spin_unlock_irqrestore(&gic_lock, flags);
269
270 return 0;
271}
272
273#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000274static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
275 bool force)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100276{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700277 unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100278 cpumask_t tmp = CPU_MASK_NONE;
279 unsigned long flags;
280 int i;
281
Rusty Russell0de26522008-12-13 21:20:26 +1030282 cpumask_and(&tmp, cpumask, cpu_online_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030283 if (cpumask_empty(&tmp))
Andrew Bresticker14d160a2014-09-18 14:47:22 -0700284 return -EINVAL;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100285
286 /* Assumption : cpumask refers to a single CPU */
287 spin_lock_irqsave(&gic_lock, flags);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100288
Tony Wuc214c032013-06-21 10:13:08 +0000289 /* Re-route this IRQ */
Paul Burton0efe3cb2017-08-12 21:36:23 -0700290 write_gic_map_vp(irq, BIT(mips_cm_vp_id(cpumask_first(&tmp))));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100291
Tony Wuc214c032013-06-21 10:13:08 +0000292 /* Update the pcpu_masks */
Paul Burton91951f92016-04-21 11:31:54 +0100293 for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
Tony Wuc214c032013-06-21 10:13:08 +0000294 clear_bit(irq, pcpu_masks[i].pcpu_mask);
Rusty Russellf9b531f2015-03-05 10:49:16 +1030295 set_bit(irq, pcpu_masks[cpumask_first(&tmp)].pcpu_mask);
Tony Wuc214c032013-06-21 10:13:08 +0000296
Jiang Liu72f86db2015-06-01 16:05:38 +0800297 cpumask_copy(irq_data_get_affinity_mask(d), cpumask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100298 spin_unlock_irqrestore(&gic_lock, flags);
299
Thomas Gleixner161d0492011-03-23 21:08:58 +0000300 return IRQ_SET_MASK_OK_NOCOPY;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100301}
302#endif
303
Andrew Bresticker4a6a3ea32014-09-18 14:47:26 -0700304static struct irq_chip gic_level_irq_controller = {
305 .name = "MIPS GIC",
306 .irq_mask = gic_mask_irq,
307 .irq_unmask = gic_unmask_irq,
308 .irq_set_type = gic_set_type,
309#ifdef CONFIG_SMP
310 .irq_set_affinity = gic_set_affinity,
311#endif
312};
313
314static struct irq_chip gic_edge_irq_controller = {
Thomas Gleixner161d0492011-03-23 21:08:58 +0000315 .name = "MIPS GIC",
Andrew Bresticker5561c9e2014-09-18 14:47:20 -0700316 .irq_ack = gic_ack_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000317 .irq_mask = gic_mask_irq,
Thomas Gleixner161d0492011-03-23 21:08:58 +0000318 .irq_unmask = gic_unmask_irq,
Andrew Bresticker95150ae2014-09-18 14:47:21 -0700319 .irq_set_type = gic_set_type,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100320#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000321 .irq_set_affinity = gic_set_affinity,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100322#endif
Qais Yousefbb11cff2015-12-08 13:20:28 +0000323 .ipi_send_single = gic_send_ipi,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100324};
325
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200326static void gic_handle_local_int(bool chained)
Andrew Brestickere9de6882014-09-18 14:47:27 -0700327{
328 unsigned long pending, masked;
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000329 unsigned int intr, virq;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700330
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100331 pending = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_PEND));
332 masked = gic_read32(GIC_REG(VPE_LOCAL, GIC_VPE_MASK));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700333
334 bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
335
Paul Burton0f4ed152016-09-13 17:54:27 +0100336 for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) {
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000337 virq = irq_linear_revmap(gic_irq_domain,
338 GIC_LOCAL_TO_HWIRQ(intr));
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200339 if (chained)
340 generic_handle_irq(virq);
341 else
342 do_IRQ(virq);
Qais Yousefd7eb4f22015-01-19 11:51:29 +0000343 }
Andrew Brestickere9de6882014-09-18 14:47:27 -0700344}
345
346static void gic_mask_local_irq(struct irq_data *d)
347{
348 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
349
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100350 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700351}
352
353static void gic_unmask_local_irq(struct irq_data *d)
354{
355 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
356
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100357 gic_write32(GIC_REG(VPE_LOCAL, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700358}
359
360static struct irq_chip gic_local_irq_controller = {
361 .name = "MIPS GIC Local",
362 .irq_mask = gic_mask_local_irq,
363 .irq_unmask = gic_unmask_local_irq,
364};
365
366static void gic_mask_local_irq_all_vpes(struct irq_data *d)
367{
368 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
369 int i;
370 unsigned long flags;
371
372 spin_lock_irqsave(&gic_lock, flags);
373 for (i = 0; i < gic_vpes; i++) {
Paul Burtond46812b2016-02-03 03:15:27 +0000374 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
375 mips_cm_vp_id(i));
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100376 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700377 }
378 spin_unlock_irqrestore(&gic_lock, flags);
379}
380
381static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
382{
383 int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
384 int i;
385 unsigned long flags;
386
387 spin_lock_irqsave(&gic_lock, flags);
388 for (i = 0; i < gic_vpes; i++) {
Paul Burtond46812b2016-02-03 03:15:27 +0000389 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
390 mips_cm_vp_id(i));
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100391 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_SMASK), 1 << intr);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700392 }
393 spin_unlock_irqrestore(&gic_lock, flags);
394}
395
396static struct irq_chip gic_all_vpes_local_irq_controller = {
397 .name = "MIPS GIC Local",
398 .irq_mask = gic_mask_local_irq_all_vpes,
399 .irq_unmask = gic_unmask_local_irq_all_vpes,
400};
401
Andrew Bresticker18743d22014-09-18 14:47:24 -0700402static void __gic_irq_dispatch(void)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100403{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200404 gic_handle_local_int(false);
405 gic_handle_shared_int(false);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700406}
407
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200408static void gic_irq_dispatch(struct irq_desc *desc)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700409{
Rabin Vincent1b3ed362015-06-12 10:01:56 +0200410 gic_handle_local_int(true);
411 gic_handle_shared_int(true);
Andrew Bresticker18743d22014-09-18 14:47:24 -0700412}
413
Andrew Brestickere9de6882014-09-18 14:47:27 -0700414static void __init gic_basic_init(void)
Andrew Bresticker18743d22014-09-18 14:47:24 -0700415{
416 unsigned int i;
Steven J. Hill98b67c32012-08-31 16:18:49 -0500417
418 board_bind_eic_interrupt = &gic_bind_eic_interrupt;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100419
420 /* Setup defaults */
Andrew Brestickerfbd55242014-09-18 14:47:25 -0700421 for (i = 0; i < gic_shared_intrs; i++) {
Paul Burton80e5f9c2017-08-12 21:36:19 -0700422 change_gic_pol(i, GIC_POL_ACTIVE_HIGH);
Paul Burton471aa962017-08-12 21:36:20 -0700423 change_gic_trig(i, GIC_TRIG_LEVEL);
Paul Burton87554b02017-08-12 21:36:18 -0700424 write_gic_rmask(BIT(i));
Ralf Baechle39b8d522008-04-28 17:14:26 +0100425 }
426
Andrew Brestickere9de6882014-09-18 14:47:27 -0700427 for (i = 0; i < gic_vpes; i++) {
428 unsigned int j;
429
Paul Burtond46812b2016-02-03 03:15:27 +0000430 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
431 mips_cm_vp_id(i));
Andrew Brestickere9de6882014-09-18 14:47:27 -0700432 for (j = 0; j < GIC_NUM_LOCAL_INTRS; j++) {
433 if (!gic_local_irq_is_routable(j))
434 continue;
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100435 gic_write32(GIC_REG(VPE_OTHER, GIC_VPE_RMASK), 1 << j);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700436 }
437 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100438}
439
Andrew Brestickere9de6882014-09-18 14:47:27 -0700440static int gic_local_irq_domain_map(struct irq_domain *d, unsigned int virq,
441 irq_hw_number_t hw)
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700442{
Andrew Brestickere9de6882014-09-18 14:47:27 -0700443 int intr = GIC_HWIRQ_TO_LOCAL(hw);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700444 int i;
445 unsigned long flags;
Paul Burtona0dc5cb52017-08-12 21:36:17 -0700446 u32 val;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700447
448 if (!gic_local_irq_is_routable(intr))
449 return -EPERM;
450
Paul Burtona0dc5cb52017-08-12 21:36:17 -0700451 if (intr > GIC_LOCAL_INT_FDC) {
452 pr_err("Invalid local IRQ %d\n", intr);
453 return -EINVAL;
454 }
455
456 if (intr == GIC_LOCAL_INT_TIMER) {
457 /* CONFIG_MIPS_CMP workaround (see __gic_init) */
458 val = GIC_MAP_PIN_MAP_TO_PIN | timer_cpu_pin;
459 } else {
460 val = GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin;
461 }
462
Andrew Brestickere9de6882014-09-18 14:47:27 -0700463 spin_lock_irqsave(&gic_lock, flags);
464 for (i = 0; i < gic_vpes; i++) {
Paul Burtona0dc5cb52017-08-12 21:36:17 -0700465 write_gic_vl_other(mips_cm_vp_id(i));
466 write_gic_vo_map(intr, val);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700467 }
468 spin_unlock_irqrestore(&gic_lock, flags);
469
Paul Burtona0dc5cb52017-08-12 21:36:17 -0700470 return 0;
Andrew Brestickere9de6882014-09-18 14:47:27 -0700471}
472
473static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
Qais Yousef2af70a92015-12-08 13:20:23 +0000474 irq_hw_number_t hw, unsigned int vpe)
Andrew Brestickere9de6882014-09-18 14:47:27 -0700475{
476 int intr = GIC_HWIRQ_TO_SHARED(hw);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700477 unsigned long flags;
Qais Yousef78930f02015-12-08 13:20:26 +0000478 int i;
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700479
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700480 spin_lock_irqsave(&gic_lock, flags);
Paul Burtond3e8cf42017-08-12 21:36:22 -0700481 write_gic_map_pin(intr, GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin);
Paul Burton0efe3cb2017-08-12 21:36:23 -0700482 write_gic_map_vp(intr, BIT(mips_cm_vp_id(vpe)));
Paul Burton91951f92016-04-21 11:31:54 +0100483 for (i = 0; i < min(gic_vpes, NR_CPUS); i++)
Qais Yousef78930f02015-12-08 13:20:26 +0000484 clear_bit(intr, pcpu_masks[i].pcpu_mask);
Qais Yousef2af70a92015-12-08 13:20:23 +0000485 set_bit(intr, pcpu_masks[vpe].pcpu_mask);
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700486 spin_unlock_irqrestore(&gic_lock, flags);
487
488 return 0;
489}
490
Paul Burtonb87281e2017-04-20 10:07:35 +0100491static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
Qais Yousefc98c18222015-12-08 13:20:24 +0000492 const u32 *intspec, unsigned int intsize,
493 irq_hw_number_t *out_hwirq,
494 unsigned int *out_type)
495{
496 if (intsize != 3)
497 return -EINVAL;
498
499 if (intspec[0] == GIC_SHARED)
500 *out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
501 else if (intspec[0] == GIC_LOCAL)
502 *out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
503 else
504 return -EINVAL;
505 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
506
507 return 0;
508}
509
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100510static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
511 irq_hw_number_t hwirq)
Qais Yousefc98c18222015-12-08 13:20:24 +0000512{
Paul Burtonb87281e2017-04-20 10:07:35 +0100513 int err;
Qais Yousefc98c18222015-12-08 13:20:24 +0000514
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100515 if (hwirq >= GIC_SHARED_HWIRQ_BASE) {
Paul Burtonb87281e2017-04-20 10:07:35 +0100516 /* verify that shared irqs don't conflict with an IPI irq */
517 if (test_bit(GIC_HWIRQ_TO_SHARED(hwirq), ipi_resrv))
518 return -EBUSY;
Qais Yousefc98c18222015-12-08 13:20:24 +0000519
Paul Burtonb87281e2017-04-20 10:07:35 +0100520 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
521 &gic_level_irq_controller,
522 NULL);
523 if (err)
524 return err;
525
526 return gic_shared_irq_domain_map(d, virq, hwirq, 0);
Qais Yousefc98c18222015-12-08 13:20:24 +0000527 }
528
Paul Burtonb87281e2017-04-20 10:07:35 +0100529 switch (GIC_HWIRQ_TO_LOCAL(hwirq)) {
530 case GIC_LOCAL_INT_TIMER:
531 case GIC_LOCAL_INT_PERFCTR:
532 case GIC_LOCAL_INT_FDC:
533 /*
534 * HACK: These are all really percpu interrupts, but
535 * the rest of the MIPS kernel code does not use the
536 * percpu IRQ API for them.
537 */
538 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
539 &gic_all_vpes_local_irq_controller,
540 NULL);
541 if (err)
542 return err;
543
544 irq_set_handler(virq, handle_percpu_irq);
545 break;
546
547 default:
548 err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
549 &gic_local_irq_controller,
550 NULL);
551 if (err)
552 return err;
553
554 irq_set_handler(virq, handle_percpu_devid_irq);
555 irq_set_percpu_devid(virq);
556 break;
557 }
558
559 return gic_local_irq_domain_map(d, virq, hwirq);
Qais Yousefc98c18222015-12-08 13:20:24 +0000560}
561
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100562static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
563 unsigned int nr_irqs, void *arg)
564{
565 struct irq_fwspec *fwspec = arg;
566 irq_hw_number_t hwirq;
567
568 if (fwspec->param[0] == GIC_SHARED)
569 hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]);
570 else
571 hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]);
572
573 return gic_irq_domain_map(d, virq, hwirq);
574}
575
Paul Burtonb87281e2017-04-20 10:07:35 +0100576void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
Qais Yousefc98c18222015-12-08 13:20:24 +0000577 unsigned int nr_irqs)
578{
Qais Yousefc98c18222015-12-08 13:20:24 +0000579}
580
Paul Burtonb87281e2017-04-20 10:07:35 +0100581static const struct irq_domain_ops gic_irq_domain_ops = {
582 .xlate = gic_irq_domain_xlate,
583 .alloc = gic_irq_domain_alloc,
584 .free = gic_irq_domain_free,
Matt Redfearn8ada00a2017-04-20 10:07:36 +0100585 .map = gic_irq_domain_map,
Qais Yousef2af70a92015-12-08 13:20:23 +0000586};
587
588static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
589 const u32 *intspec, unsigned int intsize,
590 irq_hw_number_t *out_hwirq,
591 unsigned int *out_type)
592{
593 /*
594 * There's nothing to translate here. hwirq is dynamically allocated and
595 * the irq type is always edge triggered.
596 * */
597 *out_hwirq = 0;
598 *out_type = IRQ_TYPE_EDGE_RISING;
599
600 return 0;
601}
602
603static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
604 unsigned int nr_irqs, void *arg)
605{
606 struct cpumask *ipimask = arg;
Paul Burtonb87281e2017-04-20 10:07:35 +0100607 irq_hw_number_t hwirq, base_hwirq;
608 int cpu, ret, i;
Qais Yousef2af70a92015-12-08 13:20:23 +0000609
Paul Burtonb87281e2017-04-20 10:07:35 +0100610 base_hwirq = find_first_bit(ipi_available, gic_shared_intrs);
611 if (base_hwirq == gic_shared_intrs)
612 return -ENOMEM;
Qais Yousef2af70a92015-12-08 13:20:23 +0000613
Paul Burtonb87281e2017-04-20 10:07:35 +0100614 /* check that we have enough space */
615 for (i = base_hwirq; i < nr_irqs; i++) {
616 if (!test_bit(i, ipi_available))
617 return -EBUSY;
618 }
619 bitmap_clear(ipi_available, base_hwirq, nr_irqs);
620
621 /* map the hwirq for each cpu consecutively */
622 i = 0;
623 for_each_cpu(cpu, ipimask) {
624 hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
625
626 ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
627 &gic_edge_irq_controller,
628 NULL);
629 if (ret)
630 goto error;
631
632 ret = irq_domain_set_hwirq_and_chip(d->parent, virq + i, hwirq,
Qais Yousef2af70a92015-12-08 13:20:23 +0000633 &gic_edge_irq_controller,
634 NULL);
635 if (ret)
636 goto error;
637
638 ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
639 if (ret)
640 goto error;
Paul Burtonb87281e2017-04-20 10:07:35 +0100641
642 ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
643 if (ret)
644 goto error;
645
646 i++;
Qais Yousef2af70a92015-12-08 13:20:23 +0000647 }
648
649 return 0;
650error:
Paul Burtonb87281e2017-04-20 10:07:35 +0100651 bitmap_set(ipi_available, base_hwirq, nr_irqs);
Qais Yousef2af70a92015-12-08 13:20:23 +0000652 return ret;
653}
654
655void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
656 unsigned int nr_irqs)
657{
Paul Burtonb87281e2017-04-20 10:07:35 +0100658 irq_hw_number_t base_hwirq;
659 struct irq_data *data;
660
661 data = irq_get_irq_data(virq);
662 if (!data)
663 return;
664
665 base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
666 bitmap_set(ipi_available, base_hwirq, nr_irqs);
Qais Yousef2af70a92015-12-08 13:20:23 +0000667}
668
669int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
670 enum irq_domain_bus_token bus_token)
671{
672 bool is_ipi;
673
674 switch (bus_token) {
675 case DOMAIN_BUS_IPI:
676 is_ipi = d->bus_token == bus_token;
Paul Burton547aefc2016-07-05 14:26:00 +0100677 return (!node || to_of_node(d->fwnode) == node) && is_ipi;
Qais Yousef2af70a92015-12-08 13:20:23 +0000678 break;
679 default:
680 return 0;
681 }
682}
683
Tobias Klauser0b7e8152017-06-02 10:20:56 +0200684static const struct irq_domain_ops gic_ipi_domain_ops = {
Qais Yousef2af70a92015-12-08 13:20:23 +0000685 .xlate = gic_ipi_domain_xlate,
686 .alloc = gic_ipi_domain_alloc,
687 .free = gic_ipi_domain_free,
688 .match = gic_ipi_domain_match,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700689};
690
Andrew Brestickera7057272014-11-12 11:43:38 -0800691static void __init __gic_init(unsigned long gic_base_addr,
692 unsigned long gic_addrspace_size,
693 unsigned int cpu_vec, unsigned int irqbase,
694 struct device_node *node)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100695{
Paul Burtonba01cf02016-05-17 15:31:06 +0100696 unsigned int gicconfig, cpu;
Qais Yousef16a80832015-12-08 13:20:30 +0000697 unsigned int v[2];
Ralf Baechle39b8d522008-04-28 17:14:26 +0100698
Alex Smithc0a9f722015-10-12 10:40:43 +0100699 __gic_base_addr = gic_base_addr;
700
Paul Burton582e2b42017-08-12 21:36:10 -0700701 mips_gic_base = ioremap_nocache(gic_base_addr, gic_addrspace_size);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100702
Paul Burton36807462017-08-12 21:36:24 -0700703 gicconfig = read_gic_config();
704 gic_shared_intrs = gicconfig & GIC_CONFIG_NUMINTERRUPTS;
705 gic_shared_intrs >>= __fls(GIC_CONFIG_NUMINTERRUPTS);
706 gic_shared_intrs = (gic_shared_intrs + 1) * 8;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100707
Paul Burton36807462017-08-12 21:36:24 -0700708 gic_vpes = gicconfig & GIC_CONFIG_PVPS;
709 gic_vpes >>= __fls(GIC_CONFIG_PVPS);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700710 gic_vpes = gic_vpes + 1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100711
Andrew Bresticker18743d22014-09-18 14:47:24 -0700712 if (cpu_has_veic) {
Paul Burtonba01cf02016-05-17 15:31:06 +0100713 /* Set EIC mode for all VPEs */
714 for_each_present_cpu(cpu) {
715 gic_write(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR),
716 mips_cm_vp_id(cpu));
717 gic_write(GIC_REG(VPE_OTHER, GIC_VPE_CTL),
718 GIC_VPE_CTL_EIC_MODE_MSK);
719 }
720
Andrew Bresticker18743d22014-09-18 14:47:24 -0700721 /* Always use vector 1 in EIC mode */
722 gic_cpu_pin = 0;
James Hogan1b6af712015-01-19 15:38:24 +0000723 timer_cpu_pin = gic_cpu_pin;
Andrew Bresticker18743d22014-09-18 14:47:24 -0700724 set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
725 __gic_irq_dispatch);
726 } else {
727 gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
728 irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
729 gic_irq_dispatch);
James Hogan1b6af712015-01-19 15:38:24 +0000730 /*
731 * With the CMP implementation of SMP (deprecated), other CPUs
732 * are started by the bootloader and put into a timer based
733 * waiting poll loop. We must not re-route those CPU's local
734 * timer interrupts as the wait instruction will never finish,
735 * so just handle whatever CPU interrupt it is routed to by
736 * default.
737 *
738 * This workaround should be removed when CMP support is
739 * dropped.
740 */
741 if (IS_ENABLED(CONFIG_MIPS_CMP) &&
742 gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER)) {
Markos Chandrasc3f57f02015-07-14 10:26:09 +0100743 timer_cpu_pin = gic_read32(GIC_REG(VPE_LOCAL,
James Hogan1b6af712015-01-19 15:38:24 +0000744 GIC_VPE_TIMER_MAP)) &
745 GIC_MAP_MSK;
746 irq_set_chained_handler(MIPS_CPU_IRQ_BASE +
747 GIC_CPU_PIN_OFFSET +
748 timer_cpu_pin,
749 gic_irq_dispatch);
750 } else {
751 timer_cpu_pin = gic_cpu_pin;
752 }
Andrew Bresticker18743d22014-09-18 14:47:24 -0700753 }
754
Andrew Brestickera7057272014-11-12 11:43:38 -0800755 gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
Andrew Brestickere9de6882014-09-18 14:47:27 -0700756 gic_shared_intrs, irqbase,
Andrew Brestickerc49581a2014-09-18 14:47:23 -0700757 &gic_irq_domain_ops, NULL);
758 if (!gic_irq_domain)
759 panic("Failed to add GIC IRQ domain");
Steven J. Hill0b271f52012-08-31 16:05:37 -0500760
Qais Yousef2af70a92015-12-08 13:20:23 +0000761 gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
762 IRQ_DOMAIN_FLAG_IPI_PER_CPU,
763 GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
764 node, &gic_ipi_domain_ops, NULL);
765 if (!gic_ipi_domain)
766 panic("Failed to add GIC IPI domain");
767
Marc Zyngier96f0d932017-06-22 11:42:50 +0100768 irq_domain_update_bus_token(gic_ipi_domain, DOMAIN_BUS_IPI);
Qais Yousef2af70a92015-12-08 13:20:23 +0000769
Qais Yousef16a80832015-12-08 13:20:30 +0000770 if (node &&
771 !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
772 bitmap_set(ipi_resrv, v[0], v[1]);
773 } else {
774 /* Make the last 2 * gic_vpes available for IPIs */
775 bitmap_set(ipi_resrv,
776 gic_shared_intrs - 2 * gic_vpes,
777 2 * gic_vpes);
778 }
Qais Yousef2af70a92015-12-08 13:20:23 +0000779
Paul Burtonf8dcd9e2017-04-20 10:07:34 +0100780 bitmap_copy(ipi_available, ipi_resrv, GIC_MAX_INTRS);
Andrew Brestickere9de6882014-09-18 14:47:27 -0700781 gic_basic_init();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100782}
Andrew Brestickera7057272014-11-12 11:43:38 -0800783
784void __init gic_init(unsigned long gic_base_addr,
785 unsigned long gic_addrspace_size,
786 unsigned int cpu_vec, unsigned int irqbase)
787{
788 __gic_init(gic_base_addr, gic_addrspace_size, cpu_vec, irqbase, NULL);
789}
790
791static int __init gic_of_init(struct device_node *node,
792 struct device_node *parent)
793{
794 struct resource res;
795 unsigned int cpu_vec, i = 0, reserved = 0;
796 phys_addr_t gic_base;
797 size_t gic_len;
798
799 /* Find the first available CPU vector. */
800 while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
801 i++, &cpu_vec))
802 reserved |= BIT(cpu_vec);
803 for (cpu_vec = 2; cpu_vec < 8; cpu_vec++) {
804 if (!(reserved & BIT(cpu_vec)))
805 break;
806 }
807 if (cpu_vec == 8) {
808 pr_err("No CPU vectors available for GIC\n");
809 return -ENODEV;
810 }
811
812 if (of_address_to_resource(node, 0, &res)) {
813 /*
814 * Probe the CM for the GIC base address if not specified
815 * in the device-tree.
816 */
817 if (mips_cm_present()) {
818 gic_base = read_gcr_gic_base() &
Paul Burton93c5bba52017-08-12 19:49:27 -0700819 ~CM_GCR_GIC_BASE_GICEN;
Andrew Brestickera7057272014-11-12 11:43:38 -0800820 gic_len = 0x20000;
821 } else {
822 pr_err("Failed to get GIC memory range\n");
823 return -ENODEV;
824 }
825 } else {
826 gic_base = res.start;
827 gic_len = resource_size(&res);
828 }
829
James Hogana0ffec32017-08-12 21:36:09 -0700830 if (mips_cm_present()) {
Paul Burton93c5bba52017-08-12 19:49:27 -0700831 write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN);
James Hogana0ffec32017-08-12 21:36:09 -0700832 /* Ensure GIC region is enabled before trying to access it */
833 __sync();
834 }
Andrew Brestickera7057272014-11-12 11:43:38 -0800835 gic_present = true;
836
837 __gic_init(gic_base, gic_len, cpu_vec, 0, node);
838
839 return 0;
840}
841IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);