Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame] | 1 | /* |
| 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 Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 9 | #include <linux/bitmap.h> |
| 10 | #include <linux/init.h> |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/sched.h> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 13 | #include <linux/smp.h> |
David Howells | ca4d3e67 | 2010-10-07 14:08:54 +0100 | [diff] [blame] | 14 | #include <linux/irq.h> |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 15 | #include <linux/clocksource.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 16 | |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/gic.h> |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 19 | #include <asm/setup.h> |
| 20 | #include <asm/traps.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 21 | #include <linux/hardirq.h> |
| 22 | #include <asm-generic/bitops/find.h> |
| 23 | |
Steven J. Hill | 28ea215 | 2013-04-10 16:27:50 -0500 | [diff] [blame] | 24 | unsigned int gic_frequency; |
Steven J. Hill | ff86714 | 2013-04-10 16:27:04 -0500 | [diff] [blame] | 25 | unsigned int gic_present; |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 26 | unsigned long _gic_base; |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 27 | unsigned int gic_cpu_pin; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 28 | |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 29 | struct gic_pcpu_mask { |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 30 | DECLARE_BITMAP(pcpu_mask, GIC_MAX_INTRS); |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct gic_pending_regs { |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 34 | DECLARE_BITMAP(pending, GIC_MAX_INTRS); |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct gic_intrmask_regs { |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 38 | DECLARE_BITMAP(intrmask, GIC_MAX_INTRS); |
Jeffrey Deans | 822350b | 2014-07-17 09:20:53 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 41 | static struct gic_pcpu_mask pcpu_masks[NR_CPUS]; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 42 | static struct gic_pending_regs pending_regs[NR_CPUS]; |
| 43 | static struct gic_intrmask_regs intrmask_regs[NR_CPUS]; |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 44 | static DEFINE_SPINLOCK(gic_lock); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 45 | static struct irq_domain *gic_irq_domain; |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 46 | static int gic_shared_intrs; |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 47 | static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 48 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 49 | static void __gic_irq_dispatch(void); |
| 50 | |
Raghu Gandham | 0ab2b7d | 2013-04-10 16:30:12 -0500 | [diff] [blame] | 51 | #if defined(CONFIG_CSRC_GIC) || defined(CONFIG_CEVT_GIC) |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 52 | cycle_t gic_read_count(void) |
| 53 | { |
| 54 | unsigned int hi, hi2, lo; |
| 55 | |
| 56 | do { |
| 57 | GICREAD(GIC_REG(SHARED, GIC_SH_COUNTER_63_32), hi); |
| 58 | GICREAD(GIC_REG(SHARED, GIC_SH_COUNTER_31_00), lo); |
| 59 | GICREAD(GIC_REG(SHARED, GIC_SH_COUNTER_63_32), hi2); |
| 60 | } while (hi2 != hi); |
| 61 | |
| 62 | return (((cycle_t) hi) << 32) + lo; |
| 63 | } |
Raghu Gandham | 0ab2b7d | 2013-04-10 16:30:12 -0500 | [diff] [blame] | 64 | |
| 65 | void gic_write_compare(cycle_t cnt) |
| 66 | { |
| 67 | GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI), |
| 68 | (int)(cnt >> 32)); |
| 69 | GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO), |
| 70 | (int)(cnt & 0xffffffff)); |
| 71 | } |
| 72 | |
Paul Burton | 414408d0 | 2014-03-05 11:35:53 +0000 | [diff] [blame] | 73 | void gic_write_cpu_compare(cycle_t cnt, int cpu) |
| 74 | { |
| 75 | unsigned long flags; |
| 76 | |
| 77 | local_irq_save(flags); |
| 78 | |
| 79 | GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), cpu); |
| 80 | GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_HI), |
| 81 | (int)(cnt >> 32)); |
| 82 | GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_COMPARE_LO), |
| 83 | (int)(cnt & 0xffffffff)); |
| 84 | |
| 85 | local_irq_restore(flags); |
| 86 | } |
| 87 | |
Raghu Gandham | 0ab2b7d | 2013-04-10 16:30:12 -0500 | [diff] [blame] | 88 | cycle_t gic_read_compare(void) |
| 89 | { |
| 90 | unsigned int hi, lo; |
| 91 | |
| 92 | GICREAD(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_HI), hi); |
| 93 | GICREAD(GIC_REG(VPE_LOCAL, GIC_VPE_COMPARE_LO), lo); |
| 94 | |
| 95 | return (((cycle_t) hi) << 32) + lo; |
| 96 | } |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 97 | #endif |
| 98 | |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 99 | unsigned int gic_get_timer_pending(void) |
| 100 | { |
| 101 | unsigned int vpe_pending; |
| 102 | |
| 103 | GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), 0); |
| 104 | GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_PEND), vpe_pending); |
Ralf Baechle | 635c9907 | 2014-10-21 14:12:49 +0200 | [diff] [blame] | 105 | return vpe_pending & GIC_VPE_PEND_TIMER_MSK; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void gic_bind_eic_interrupt(int irq, int set) |
| 109 | { |
| 110 | /* Convert irq vector # to hw int # */ |
| 111 | irq -= GIC_PIN_TO_VEC_OFFSET; |
| 112 | |
| 113 | /* Set irq to use shadow set */ |
| 114 | GICWRITE(GIC_REG_ADDR(VPE_LOCAL, GIC_VPE_EIC_SS(irq)), set); |
| 115 | } |
| 116 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 117 | void gic_send_ipi(unsigned int intr) |
| 118 | { |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 119 | GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), 0x80000000 | intr); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 122 | static void __init vpe_local_setup(unsigned int numvpes) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 123 | { |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 124 | unsigned long timer_intr = GIC_INT_TMR; |
| 125 | unsigned long perf_intr = GIC_INT_PERFCTR; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 126 | unsigned int vpe_ctl; |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame] | 127 | int i; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 128 | |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 129 | if (cpu_has_veic) { |
| 130 | /* |
| 131 | * GIC timer interrupt -> CPU HW Int X (vector X+2) -> |
| 132 | * map to pin X+2-1 (since GIC adds 1) |
| 133 | */ |
| 134 | timer_intr += (GIC_CPU_TO_VEC_OFFSET - GIC_PIN_TO_VEC_OFFSET); |
| 135 | /* |
| 136 | * GIC perfcnt interrupt -> CPU HW Int X (vector X+2) -> |
| 137 | * map to pin X+2-1 (since GIC adds 1) |
| 138 | */ |
| 139 | perf_intr += (GIC_CPU_TO_VEC_OFFSET - GIC_PIN_TO_VEC_OFFSET); |
| 140 | } |
| 141 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 142 | /* |
| 143 | * Setup the default performance counter timer interrupts |
| 144 | * for all VPEs |
| 145 | */ |
| 146 | for (i = 0; i < numvpes; i++) { |
| 147 | GICWRITE(GIC_REG(VPE_LOCAL, GIC_VPE_OTHER_ADDR), i); |
| 148 | |
| 149 | /* Are Interrupts locally routable? */ |
| 150 | GICREAD(GIC_REG(VPE_OTHER, GIC_VPE_CTL), vpe_ctl); |
| 151 | if (vpe_ctl & GIC_VPE_CTL_TIMER_RTBL_MSK) |
| 152 | GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_TIMER_MAP), |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 153 | GIC_MAP_TO_PIN_MSK | timer_intr); |
| 154 | if (cpu_has_veic) { |
| 155 | set_vi_handler(timer_intr + GIC_PIN_TO_VEC_OFFSET, |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 156 | __gic_irq_dispatch); |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 157 | } |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 158 | |
| 159 | if (vpe_ctl & GIC_VPE_CTL_PERFCNT_RTBL_MSK) |
| 160 | GICWRITE(GIC_REG(VPE_OTHER, GIC_VPE_PERFCTR_MAP), |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 161 | GIC_MAP_TO_PIN_MSK | perf_intr); |
| 162 | if (cpu_has_veic) { |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 163 | set_vi_handler(perf_intr + GIC_PIN_TO_VEC_OFFSET, |
| 164 | __gic_irq_dispatch); |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 165 | } |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Raghu Gandham | 0ab2b7d | 2013-04-10 16:30:12 -0500 | [diff] [blame] | 169 | unsigned int gic_compare_int(void) |
| 170 | { |
| 171 | unsigned int pending; |
| 172 | |
| 173 | GICREAD(GIC_REG(VPE_LOCAL, GIC_VPE_PEND), pending); |
| 174 | if (pending & GIC_VPE_PEND_CMP_MSK) |
| 175 | return 1; |
| 176 | else |
| 177 | return 0; |
| 178 | } |
| 179 | |
Jeffrey Deans | 31521a7 | 2014-07-17 09:20:57 +0100 | [diff] [blame] | 180 | void gic_get_int_mask(unsigned long *dst, const unsigned long *src) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 181 | { |
| 182 | unsigned int i; |
| 183 | unsigned long *pending, *intrmask, *pcpu_mask; |
| 184 | unsigned long *pending_abs, *intrmask_abs; |
| 185 | |
| 186 | /* Get per-cpu bitmaps */ |
| 187 | pending = pending_regs[smp_processor_id()].pending; |
| 188 | intrmask = intrmask_regs[smp_processor_id()].intrmask; |
| 189 | pcpu_mask = pcpu_masks[smp_processor_id()].pcpu_mask; |
| 190 | |
| 191 | pending_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED, |
| 192 | GIC_SH_PEND_31_0_OFS); |
| 193 | intrmask_abs = (unsigned long *) GIC_REG_ABS_ADDR(SHARED, |
| 194 | GIC_SH_MASK_31_0_OFS); |
| 195 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 196 | for (i = 0; i < BITS_TO_LONGS(gic_shared_intrs); i++) { |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 197 | GICREAD(*pending_abs, pending[i]); |
| 198 | GICREAD(*intrmask_abs, intrmask[i]); |
| 199 | pending_abs++; |
| 200 | intrmask_abs++; |
| 201 | } |
| 202 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 203 | bitmap_and(pending, pending, intrmask, gic_shared_intrs); |
| 204 | bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs); |
| 205 | bitmap_and(dst, src, pending, gic_shared_intrs); |
Jeffrey Deans | 31521a7 | 2014-07-17 09:20:57 +0100 | [diff] [blame] | 206 | } |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 207 | |
Jeffrey Deans | 31521a7 | 2014-07-17 09:20:57 +0100 | [diff] [blame] | 208 | unsigned int gic_get_int(void) |
| 209 | { |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 210 | DECLARE_BITMAP(interrupts, GIC_MAX_INTRS); |
Jeffrey Deans | 31521a7 | 2014-07-17 09:20:57 +0100 | [diff] [blame] | 211 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 212 | bitmap_fill(interrupts, gic_shared_intrs); |
Jeffrey Deans | 31521a7 | 2014-07-17 09:20:57 +0100 | [diff] [blame] | 213 | gic_get_int_mask(interrupts, interrupts); |
| 214 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 215 | return find_first_bit(interrupts, gic_shared_intrs); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 218 | static void gic_mask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 219 | { |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 220 | GIC_CLR_INTR_MASK(d->hwirq); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 223 | static void gic_unmask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 224 | { |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 225 | GIC_SET_INTR_MASK(d->hwirq); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 228 | static void gic_ack_irq(struct irq_data *d) |
| 229 | { |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 230 | unsigned int irq = d->hwirq; |
| 231 | |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 232 | GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), irq); |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 235 | static int gic_set_type(struct irq_data *d, unsigned int type) |
| 236 | { |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 237 | unsigned int irq = d->hwirq; |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 238 | unsigned long flags; |
| 239 | bool is_edge; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 240 | |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 241 | spin_lock_irqsave(&gic_lock, flags); |
| 242 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 243 | case IRQ_TYPE_EDGE_FALLING: |
| 244 | GIC_SET_POLARITY(irq, GIC_POL_NEG); |
| 245 | GIC_SET_TRIGGER(irq, GIC_TRIG_EDGE); |
| 246 | GIC_SET_DUAL(irq, GIC_TRIG_DUAL_DISABLE); |
| 247 | is_edge = true; |
| 248 | break; |
| 249 | case IRQ_TYPE_EDGE_RISING: |
| 250 | GIC_SET_POLARITY(irq, GIC_POL_POS); |
| 251 | GIC_SET_TRIGGER(irq, GIC_TRIG_EDGE); |
| 252 | GIC_SET_DUAL(irq, GIC_TRIG_DUAL_DISABLE); |
| 253 | is_edge = true; |
| 254 | break; |
| 255 | case IRQ_TYPE_EDGE_BOTH: |
| 256 | /* polarity is irrelevant in this case */ |
| 257 | GIC_SET_TRIGGER(irq, GIC_TRIG_EDGE); |
| 258 | GIC_SET_DUAL(irq, GIC_TRIG_DUAL_ENABLE); |
| 259 | is_edge = true; |
| 260 | break; |
| 261 | case IRQ_TYPE_LEVEL_LOW: |
| 262 | GIC_SET_POLARITY(irq, GIC_POL_NEG); |
| 263 | GIC_SET_TRIGGER(irq, GIC_TRIG_LEVEL); |
| 264 | GIC_SET_DUAL(irq, GIC_TRIG_DUAL_DISABLE); |
| 265 | is_edge = false; |
| 266 | break; |
| 267 | case IRQ_TYPE_LEVEL_HIGH: |
| 268 | default: |
| 269 | GIC_SET_POLARITY(irq, GIC_POL_POS); |
| 270 | GIC_SET_TRIGGER(irq, GIC_TRIG_LEVEL); |
| 271 | GIC_SET_DUAL(irq, GIC_TRIG_DUAL_DISABLE); |
| 272 | is_edge = false; |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | if (is_edge) { |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 277 | __irq_set_chip_handler_name_locked(d->irq, |
| 278 | &gic_edge_irq_controller, |
| 279 | handle_edge_irq, NULL); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 280 | } else { |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 281 | __irq_set_chip_handler_name_locked(d->irq, |
| 282 | &gic_level_irq_controller, |
| 283 | handle_level_irq, NULL); |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 284 | } |
| 285 | spin_unlock_irqrestore(&gic_lock, flags); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | #ifdef CONFIG_SMP |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 291 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask, |
| 292 | bool force) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 293 | { |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 294 | unsigned int irq = d->hwirq; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 295 | cpumask_t tmp = CPU_MASK_NONE; |
| 296 | unsigned long flags; |
| 297 | int i; |
| 298 | |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 299 | cpumask_and(&tmp, cpumask, cpu_online_mask); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 300 | if (cpus_empty(tmp)) |
Andrew Bresticker | 14d160a | 2014-09-18 14:47:22 -0700 | [diff] [blame] | 301 | return -EINVAL; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 302 | |
| 303 | /* Assumption : cpumask refers to a single CPU */ |
| 304 | spin_lock_irqsave(&gic_lock, flags); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 305 | |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 306 | /* Re-route this IRQ */ |
| 307 | GIC_SH_MAP_TO_VPE_SMASK(irq, first_cpu(tmp)); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 308 | |
Tony Wu | c214c03 | 2013-06-21 10:13:08 +0000 | [diff] [blame] | 309 | /* Update the pcpu_masks */ |
| 310 | for (i = 0; i < NR_CPUS; i++) |
| 311 | clear_bit(irq, pcpu_masks[i].pcpu_mask); |
| 312 | set_bit(irq, pcpu_masks[first_cpu(tmp)].pcpu_mask); |
| 313 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 314 | cpumask_copy(d->affinity, cpumask); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 315 | spin_unlock_irqrestore(&gic_lock, flags); |
| 316 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 317 | return IRQ_SET_MASK_OK_NOCOPY; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 318 | } |
| 319 | #endif |
| 320 | |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 321 | static struct irq_chip gic_level_irq_controller = { |
| 322 | .name = "MIPS GIC", |
| 323 | .irq_mask = gic_mask_irq, |
| 324 | .irq_unmask = gic_unmask_irq, |
| 325 | .irq_set_type = gic_set_type, |
| 326 | #ifdef CONFIG_SMP |
| 327 | .irq_set_affinity = gic_set_affinity, |
| 328 | #endif |
| 329 | }; |
| 330 | |
| 331 | static struct irq_chip gic_edge_irq_controller = { |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 332 | .name = "MIPS GIC", |
Andrew Bresticker | 5561c9e | 2014-09-18 14:47:20 -0700 | [diff] [blame] | 333 | .irq_ack = gic_ack_irq, |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 334 | .irq_mask = gic_mask_irq, |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 335 | .irq_unmask = gic_unmask_irq, |
Andrew Bresticker | 95150ae | 2014-09-18 14:47:21 -0700 | [diff] [blame] | 336 | .irq_set_type = gic_set_type, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 337 | #ifdef CONFIG_SMP |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 338 | .irq_set_affinity = gic_set_affinity, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 339 | #endif |
| 340 | }; |
| 341 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 342 | static void __gic_irq_dispatch(void) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 343 | { |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 344 | unsigned int intr, virq; |
| 345 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 346 | while ((intr = gic_get_int()) != gic_shared_intrs) { |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 347 | virq = irq_linear_revmap(gic_irq_domain, intr); |
| 348 | do_IRQ(virq); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void gic_irq_dispatch(unsigned int irq, struct irq_desc *desc) |
| 353 | { |
| 354 | __gic_irq_dispatch(); |
| 355 | } |
| 356 | |
| 357 | #ifdef CONFIG_MIPS_GIC_IPI |
| 358 | static int gic_resched_int_base; |
| 359 | static int gic_call_int_base; |
| 360 | |
| 361 | unsigned int plat_ipi_resched_int_xlate(unsigned int cpu) |
| 362 | { |
| 363 | return gic_resched_int_base + cpu; |
| 364 | } |
| 365 | |
| 366 | unsigned int plat_ipi_call_int_xlate(unsigned int cpu) |
| 367 | { |
| 368 | return gic_call_int_base + cpu; |
| 369 | } |
| 370 | |
| 371 | static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id) |
| 372 | { |
| 373 | scheduler_ipi(); |
| 374 | |
| 375 | return IRQ_HANDLED; |
| 376 | } |
| 377 | |
| 378 | static irqreturn_t ipi_call_interrupt(int irq, void *dev_id) |
| 379 | { |
| 380 | smp_call_function_interrupt(); |
| 381 | |
| 382 | return IRQ_HANDLED; |
| 383 | } |
| 384 | |
| 385 | static struct irqaction irq_resched = { |
| 386 | .handler = ipi_resched_interrupt, |
| 387 | .flags = IRQF_PERCPU, |
| 388 | .name = "IPI resched" |
| 389 | }; |
| 390 | |
| 391 | static struct irqaction irq_call = { |
| 392 | .handler = ipi_call_interrupt, |
| 393 | .flags = IRQF_PERCPU, |
| 394 | .name = "IPI call" |
| 395 | }; |
| 396 | |
| 397 | static __init void gic_ipi_init_one(unsigned int intr, int cpu, |
| 398 | struct irqaction *action) |
| 399 | { |
| 400 | int virq = irq_create_mapping(gic_irq_domain, intr); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 401 | int i; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 402 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 403 | GIC_SH_MAP_TO_VPE_SMASK(intr, cpu); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 404 | for (i = 0; i < NR_CPUS; i++) |
| 405 | clear_bit(intr, pcpu_masks[i].pcpu_mask); |
Jeffrey Deans | b0a88ae | 2014-07-17 09:20:55 +0100 | [diff] [blame] | 406 | set_bit(intr, pcpu_masks[cpu].pcpu_mask); |
| 407 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 408 | irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING); |
| 409 | |
| 410 | irq_set_handler(virq, handle_percpu_irq); |
| 411 | setup_irq(virq, action); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 414 | static __init void gic_ipi_init(void) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 415 | { |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 416 | int i; |
| 417 | |
| 418 | /* Use last 2 * NR_CPUS interrupts as IPIs */ |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 419 | gic_resched_int_base = gic_shared_intrs - nr_cpu_ids; |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 420 | gic_call_int_base = gic_resched_int_base - nr_cpu_ids; |
| 421 | |
| 422 | for (i = 0; i < nr_cpu_ids; i++) { |
| 423 | gic_ipi_init_one(gic_call_int_base + i, i, &irq_call); |
| 424 | gic_ipi_init_one(gic_resched_int_base + i, i, &irq_resched); |
| 425 | } |
| 426 | } |
| 427 | #else |
| 428 | static inline void gic_ipi_init(void) |
| 429 | { |
| 430 | } |
| 431 | #endif |
| 432 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 433 | static void __init gic_basic_init(int numvpes) |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 434 | { |
| 435 | unsigned int i; |
Steven J. Hill | 98b67c3 | 2012-08-31 16:18:49 -0500 | [diff] [blame] | 436 | |
| 437 | board_bind_eic_interrupt = &gic_bind_eic_interrupt; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 438 | |
| 439 | /* Setup defaults */ |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 440 | for (i = 0; i < gic_shared_intrs; i++) { |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 441 | GIC_SET_POLARITY(i, GIC_POL_POS); |
| 442 | GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL); |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 443 | GIC_CLR_INTR_MASK(i); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | vpe_local_setup(numvpes); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 449 | static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq, |
| 450 | irq_hw_number_t hw) |
| 451 | { |
| 452 | unsigned long flags; |
| 453 | |
Andrew Bresticker | 4a6a3ea3 | 2014-09-18 14:47:26 -0700 | [diff] [blame^] | 454 | irq_set_chip_and_handler(virq, &gic_level_irq_controller, |
| 455 | handle_level_irq); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 456 | |
| 457 | spin_lock_irqsave(&gic_lock, flags); |
| 458 | GICWRITE(GIC_REG_ADDR(SHARED, GIC_SH_MAP_TO_PIN(hw)), |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 459 | GIC_MAP_TO_PIN_MSK | gic_cpu_pin); |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 460 | /* Map to VPE 0 by default */ |
| 461 | GIC_SH_MAP_TO_VPE_SMASK(hw, 0); |
| 462 | set_bit(hw, pcpu_masks[0].pcpu_mask); |
| 463 | spin_unlock_irqrestore(&gic_lock, flags); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static struct irq_domain_ops gic_irq_domain_ops = { |
| 469 | .map = gic_irq_domain_map, |
| 470 | .xlate = irq_domain_xlate_twocell, |
| 471 | }; |
| 472 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 473 | void __init gic_init(unsigned long gic_base_addr, |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 474 | unsigned long gic_addrspace_size, unsigned int cpu_vec, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 475 | unsigned int irqbase) |
| 476 | { |
| 477 | unsigned int gicconfig; |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 478 | int numvpes, numintrs; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 479 | |
| 480 | _gic_base = (unsigned long) ioremap_nocache(gic_base_addr, |
| 481 | gic_addrspace_size); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 482 | |
| 483 | GICREAD(GIC_REG(SHARED, GIC_SH_CONFIG), gicconfig); |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 484 | gic_shared_intrs = (gicconfig & GIC_SH_CONFIG_NUMINTRS_MSK) >> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 485 | GIC_SH_CONFIG_NUMINTRS_SHF; |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 486 | gic_shared_intrs = ((gic_shared_intrs + 1) * 8); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 487 | |
| 488 | numvpes = (gicconfig & GIC_SH_CONFIG_NUMVPES_MSK) >> |
| 489 | GIC_SH_CONFIG_NUMVPES_SHF; |
Steven J. Hill | 3234f44 | 2012-08-31 16:23:49 -0500 | [diff] [blame] | 490 | numvpes = numvpes + 1; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 491 | |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 492 | if (cpu_has_veic) { |
| 493 | /* Always use vector 1 in EIC mode */ |
| 494 | gic_cpu_pin = 0; |
| 495 | set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET, |
| 496 | __gic_irq_dispatch); |
| 497 | } else { |
| 498 | gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET; |
| 499 | irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec, |
| 500 | gic_irq_dispatch); |
| 501 | } |
| 502 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 503 | gic_irq_domain = irq_domain_add_simple(NULL, gic_shared_intrs, irqbase, |
Andrew Bresticker | c49581a | 2014-09-18 14:47:23 -0700 | [diff] [blame] | 504 | &gic_irq_domain_ops, NULL); |
| 505 | if (!gic_irq_domain) |
| 506 | panic("Failed to add GIC IRQ domain"); |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 507 | |
Andrew Bresticker | fbd5524 | 2014-09-18 14:47:25 -0700 | [diff] [blame] | 508 | gic_basic_init(numvpes); |
Andrew Bresticker | 18743d2 | 2014-09-18 14:47:24 -0700 | [diff] [blame] | 509 | |
| 510 | gic_ipi_init(); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 511 | } |