blob: 770c075525186515476abbbc68a9ff9d3027e549 [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>
10#include <linux/init.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010011#include <linux/smp.h>
David Howellsca4d3e672010-10-07 14:08:54 +010012#include <linux/irq.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010013
14#include <asm/io.h>
15#include <asm/gic.h>
16#include <asm/gcmpregs.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010017#include <linux/hardirq.h>
18#include <asm-generic/bitops/find.h>
19
Steven J. Hill0b271f52012-08-31 16:05:37 -050020unsigned long _gic_base;
21unsigned int gic_irq_base;
22unsigned int gic_irq_flags[GIC_NUM_INTRS];
Ralf Baechle39b8d522008-04-28 17:14:26 +010023
Steven J. Hill0b271f52012-08-31 16:05:37 -050024static struct gic_pcpu_mask pcpu_masks[NR_CPUS];
Ralf Baechle39b8d522008-04-28 17:14:26 +010025static struct gic_pending_regs pending_regs[NR_CPUS];
26static struct gic_intrmask_regs intrmask_regs[NR_CPUS];
27
Ralf Baechle39b8d522008-04-28 17:14:26 +010028void gic_send_ipi(unsigned int intr)
29{
Ralf Baechle39b8d522008-04-28 17:14:26 +010030 GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), 0x80000000 | intr);
Ralf Baechle39b8d522008-04-28 17:14:26 +010031}
32
Chris Dearman7098f742009-07-10 01:54:09 -070033static void __init vpe_local_setup(unsigned int numvpes)
Ralf Baechle39b8d522008-04-28 17:14:26 +010034{
Steven J. Hill2299c492012-08-31 16:13:07 -050035 unsigned long timer_interrupt = GIC_INT_TMR;
36 unsigned long perf_interrupt = GIC_INT_PERFCTR;
Ralf Baechle39b8d522008-04-28 17:14:26 +010037 unsigned int vpe_ctl;
Steven J. Hill2299c492012-08-31 16:13:07 -050038 int i;
Ralf Baechle39b8d522008-04-28 17:14:26 +010039
40 /*
41 * Setup the default performance counter timer interrupts
42 * for all VPEs
43 */
44 for (i = 0; i < numvpes; i++) {
45 GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i);
46
47 /* Are Interrupts locally routable? */
48 GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_CTL), vpe_ctl);
49 if (vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK)
50 GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP),
51 GIC_MAP_TO_PIN_MSK | timer_interrupt);
52
53 if (vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK)
54 GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP),
55 GIC_MAP_TO_PIN_MSK | perf_interrupt);
56 }
57}
58
59unsigned int gic_get_int(void)
60{
61 unsigned int i;
62 unsigned long *pending, *intrmask, *pcpu_mask;
63 unsigned long *pending_abs, *intrmask_abs;
64
65 /* Get per-cpu bitmaps */
66 pending = pending_regs[smp_processor_id()].pending;
67 intrmask = intrmask_regs[smp_processor_id()].intrmask;
68 pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask;
69
70 pending_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
71 GIC_SH_PEND_31_0_OFS);
72 intrmask_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED,
73 GIC_SH_MASK_31_0_OFS);
74
75 for (i = 0; i < BITS_TO_LONGS(GIC_NUM_INTRS); i++) {
76 GICREAD(*pending_abs, pending[i]);
77 GICREAD(*intrmask_abs, intrmask[i]);
78 pending_abs++;
79 intrmask_abs++;
80 }
81
82 bitmap_and(pending, pending, intrmask, GIC_NUM_INTRS);
83 bitmap_and(pending, pending, pcpu_mask, GIC_NUM_INTRS);
84
Steven J. Hill2299c492012-08-31 16:13:07 -050085 return find_first_bit(pending, GIC_NUM_INTRS);
Ralf Baechle39b8d522008-04-28 17:14:26 +010086}
87
Thomas Gleixner161d0492011-03-23 21:08:58 +000088static void gic_mask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +010089{
Steven J. Hill2299c492012-08-31 16:13:07 -050090 GIC_CLR_INTR_MASK(d->irq - gic_irq_base);
Ralf Baechle39b8d522008-04-28 17:14:26 +010091}
92
Thomas Gleixner161d0492011-03-23 21:08:58 +000093static void gic_unmask_irq(struct irq_data *d)
Ralf Baechle39b8d522008-04-28 17:14:26 +010094{
Steven J. Hill2299c492012-08-31 16:13:07 -050095 GIC_SET_INTR_MASK(d->irq - gic_irq_base);
Ralf Baechle39b8d522008-04-28 17:14:26 +010096}
97
98#ifdef CONFIG_SMP
Ralf Baechle39b8d522008-04-28 17:14:26 +010099static DEFINE_SPINLOCK(gic_lock);
100
Thomas Gleixner161d0492011-03-23 21:08:58 +0000101static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
102 bool force)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100103{
Steven J. Hill2299c492012-08-31 16:13:07 -0500104 unsigned int irq = (d->irq - gic_irq_base);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100105 cpumask_t tmp = CPU_MASK_NONE;
106 unsigned long flags;
107 int i;
108
Rusty Russell0de26522008-12-13 21:20:26 +1030109 cpumask_and(&tmp, cpumask, cpu_online_mask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100110 if (cpus_empty(tmp))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700111 return -1;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100112
113 /* Assumption : cpumask refers to a single CPU */
114 spin_lock_irqsave(&gic_lock, flags);
115 for (;;) {
116 /* Re-route this IRQ */
117 GIC_SH_MAP_TO_VPE_SMASK(irq, first_cpu(tmp));
118
Ralf Baechle39b8d522008-04-28 17:14:26 +0100119 /* Update the pcpu_masks */
120 for (i = 0; i < NR_CPUS; i++)
121 clear_bit(irq, pcpu_masks[i].pcpu_mask);
122 set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask);
123
124 }
Thomas Gleixner161d0492011-03-23 21:08:58 +0000125 cpumask_copy(d->affinity, cpumask);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100126 spin_unlock_irqrestore(&gic_lock, flags);
127
Thomas Gleixner161d0492011-03-23 21:08:58 +0000128 return IRQ_SET_MASK_OK_NOCOPY;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100129}
130#endif
131
132static struct irq_chip gic_irq_controller = {
Thomas Gleixner161d0492011-03-23 21:08:58 +0000133 .name = "MIPS GIC",
134 .irq_ack = gic_irq_ack,
135 .irq_mask = gic_mask_irq,
136 .irq_mask_ack = gic_mask_irq,
137 .irq_unmask = gic_unmask_irq,
138 .irq_eoi = gic_unmask_irq,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100139#ifdef CONFIG_SMP
Thomas Gleixner161d0492011-03-23 21:08:58 +0000140 .irq_set_affinity = gic_set_affinity,
Ralf Baechle39b8d522008-04-28 17:14:26 +0100141#endif
142};
143
Chris Dearman7098f742009-07-10 01:54:09 -0700144static void __init gic_setup_intr(unsigned int intr, unsigned int cpu,
145 unsigned int pin, unsigned int polarity, unsigned int trigtype,
146 unsigned int flags)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100147{
148 /* Setup Intr to Pin mapping */
149 if (pin & GIC_MAP_TO_NMI_MSK) {
150 GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)), pin);
151 /* FIXME: hack to route NMI to all cpu's */
152 for (cpu = 0; cpu < NR_CPUS; cpu += 32) {
153 GICWRITE(GIC_REG_ADDR(SHARED,
154 GIC_SH_MAP_TO_VPE_REG_OFF(intr, cpu)),
155 0xffffffff);
156 }
157 } else {
158 GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(intr)),
159 GIC_MAP_TO_PIN_MSK | pin);
160 /* Setup Intr to CPU mapping */
161 GIC_SH_MAP_TO_VPE_SMASK(intr, cpu);
162 }
163
164 /* Setup Intr Polarity */
165 GIC_SET_POLARITY(intr, polarity);
166
167 /* Setup Intr Trigger Type */
168 GIC_SET_TRIGGER(intr, trigtype);
169
170 /* Init Intr Masks */
Chris Dearman7098f742009-07-10 01:54:09 -0700171 GIC_CLR_INTR_MASK(intr);
Steven J. Hill2299c492012-08-31 16:13:07 -0500172
Chris Dearman7098f742009-07-10 01:54:09 -0700173 /* Initialise per-cpu Interrupt software masks */
174 if (flags & GIC_FLAG_IPI)
175 set_bit(intr, pcpu_masks[cpu].pcpu_mask);
176 if (flags & GIC_FLAG_TRANSPARENT)
177 GIC_SET_INTR_MASK(intr);
178 if (trigtype == GIC_TRIG_EDGE)
Steven J. Hill0b271f52012-08-31 16:05:37 -0500179 gic_irq_flags[intr] |= GIC_TRIG_EDGE;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100180}
181
Chris Dearman7098f742009-07-10 01:54:09 -0700182static void __init gic_basic_init(int numintrs, int numvpes,
183 struct gic_intr_map *intrmap, int mapsize)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100184{
185 unsigned int i, cpu;
186
187 /* Setup defaults */
Chris Dearman7098f742009-07-10 01:54:09 -0700188 for (i = 0; i < numintrs; i++) {
Ralf Baechle39b8d522008-04-28 17:14:26 +0100189 GIC_SET_POLARITY(i, GIC_POL_POS);
190 GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL);
Chris Dearman7098f742009-07-10 01:54:09 -0700191 GIC_CLR_INTR_MASK(i);
192 if (i < GIC_NUM_INTRS)
193 gic_irq_flags[i] = 0;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100194 }
195
196 /* Setup specifics */
Chris Dearman7098f742009-07-10 01:54:09 -0700197 for (i = 0; i < mapsize; i++) {
198 cpu = intrmap[i].cpunum;
Ralf Baechle863cb9b2010-09-17 17:07:48 +0100199 if (cpu == GIC_UNUSED)
Ralf Baechle39b8d522008-04-28 17:14:26 +0100200 continue;
Chris Dearman7098f742009-07-10 01:54:09 -0700201 if (cpu == 0 && i != 0 && intrmap[i].flags == 0)
Tim Andersona214cef2009-06-17 16:22:25 -0700202 continue;
Chris Dearman7098f742009-07-10 01:54:09 -0700203 gic_setup_intr(i,
204 intrmap[i].cpunum,
205 intrmap[i].pin,
206 intrmap[i].polarity,
207 intrmap[i].trigtype,
208 intrmap[i].flags);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100209 }
210
211 vpe_local_setup(numvpes);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100212}
213
214void __init gic_init(unsigned long gic_base_addr,
215 unsigned long gic_addrspace_size,
216 struct gic_intr_map *intr_map, unsigned int intr_map_size,
217 unsigned int irqbase)
218{
219 unsigned int gicconfig;
Chris Dearman7098f742009-07-10 01:54:09 -0700220 int numvpes, numintrs;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100221
222 _gic_base = (unsigned long) ioremap_nocache(gic_base_addr,
223 gic_addrspace_size);
Steven J. Hill0b271f52012-08-31 16:05:37 -0500224 gic_irq_base = irqbase;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100225
226 GICREAD(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig);
227 numintrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >>
228 GIC_SH_CONFIG_NUMINTRS_SHF;
229 numintrs = ((numintrs + 1) * 8);
230
231 numvpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >>
232 GIC_SH_CONFIG_NUMVPES_SHF;
233
Chris Dearman7098f742009-07-10 01:54:09 -0700234 gic_basic_init(numintrs, numvpes, intr_map, intr_map_size);
Steven J. Hill0b271f52012-08-31 16:05:37 -0500235
236 gic_platform_init(numintrs, &gic_irq_controller);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100237}