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> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 11 | #include <linux/smp.h> |
David Howells | ca4d3e67 | 2010-10-07 14:08:54 +0100 | [diff] [blame] | 12 | #include <linux/irq.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 13 | |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/gic.h> |
| 16 | #include <asm/gcmpregs.h> |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 17 | #include <linux/hardirq.h> |
| 18 | #include <asm-generic/bitops/find.h> |
| 19 | |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 20 | unsigned long _gic_base; |
| 21 | unsigned int gic_irq_base; |
| 22 | unsigned int gic_irq_flags[GIC_NUM_INTRS]; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 23 | |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 24 | static struct gic_pcpu_mask pcpu_masks[NR_CPUS]; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 25 | static struct gic_pending_regs pending_regs[NR_CPUS]; |
| 26 | static struct gic_intrmask_regs intrmask_regs[NR_CPUS]; |
| 27 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 28 | void gic_send_ipi(unsigned int intr) |
| 29 | { |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 30 | GICWRITE(GIC_REG(SHARED, GIC_SH_WEDGE), 0x80000000 | intr); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 33 | static void __init vpe_local_setup(unsigned int numvpes) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 34 | { |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 35 | unsigned long timer_interrupt = GIC_INT_TMR; |
| 36 | unsigned long perf_interrupt = GIC_INT_PERFCTR; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 37 | unsigned int vpe_ctl; |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 38 | int i; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 39 | |
| 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 | |
| 59 | unsigned 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. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 85 | return find_first_bit(pending, GIC_NUM_INTRS); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 88 | static void gic_mask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 89 | { |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 90 | GIC_CLR_INTR_MASK(d->irq - gic_irq_base); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 93 | static void gic_unmask_irq(struct irq_data *d) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 94 | { |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 95 | GIC_SET_INTR_MASK(d->irq - gic_irq_base); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #ifdef CONFIG_SMP |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 99 | static DEFINE_SPINLOCK(gic_lock); |
| 100 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 101 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask, |
| 102 | bool force) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 103 | { |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 104 | unsigned int irq = (d->irq - gic_irq_base); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 105 | cpumask_t tmp = CPU_MASK_NONE; |
| 106 | unsigned long flags; |
| 107 | int i; |
| 108 | |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 109 | cpumask_and(&tmp, cpumask, cpu_online_mask); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 110 | if (cpus_empty(tmp)) |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 111 | return -1; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 112 | |
| 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 Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 119 | /* 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 Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 125 | cpumask_copy(d->affinity, cpumask); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 126 | spin_unlock_irqrestore(&gic_lock, flags); |
| 127 | |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 128 | return IRQ_SET_MASK_OK_NOCOPY; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 129 | } |
| 130 | #endif |
| 131 | |
| 132 | static struct irq_chip gic_irq_controller = { |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 133 | .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 Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 139 | #ifdef CONFIG_SMP |
Thomas Gleixner | 161d049 | 2011-03-23 21:08:58 +0000 | [diff] [blame] | 140 | .irq_set_affinity = gic_set_affinity, |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 141 | #endif |
| 142 | }; |
| 143 | |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 144 | static 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 Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 147 | { |
| 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 Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 171 | GIC_CLR_INTR_MASK(intr); |
Steven J. Hill | 2299c49 | 2012-08-31 16:13:07 -0500 | [diff] [blame^] | 172 | |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 173 | /* 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. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 179 | gic_irq_flags[intr] |= GIC_TRIG_EDGE; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 182 | static void __init gic_basic_init(int numintrs, int numvpes, |
| 183 | struct gic_intr_map *intrmap, int mapsize) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 184 | { |
| 185 | unsigned int i, cpu; |
| 186 | |
| 187 | /* Setup defaults */ |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 188 | for (i = 0; i < numintrs; i++) { |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 189 | GIC_SET_POLARITY(i, GIC_POL_POS); |
| 190 | GIC_SET_TRIGGER(i, GIC_TRIG_LEVEL); |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 191 | GIC_CLR_INTR_MASK(i); |
| 192 | if (i < GIC_NUM_INTRS) |
| 193 | gic_irq_flags[i] = 0; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Setup specifics */ |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 197 | for (i = 0; i < mapsize; i++) { |
| 198 | cpu = intrmap[i].cpunum; |
Ralf Baechle | 863cb9b | 2010-09-17 17:07:48 +0100 | [diff] [blame] | 199 | if (cpu == GIC_UNUSED) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 200 | continue; |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 201 | if (cpu == 0 && i != 0 && intrmap[i].flags == 0) |
Tim Anderson | a214cef | 2009-06-17 16:22:25 -0700 | [diff] [blame] | 202 | continue; |
Chris Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 203 | 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 Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | vpe_local_setup(numvpes); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void __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 Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 220 | int numvpes, numintrs; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 221 | |
| 222 | _gic_base = (unsigned long) ioremap_nocache(gic_base_addr, |
| 223 | gic_addrspace_size); |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 224 | gic_irq_base = irqbase; |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 225 | |
| 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 Dearman | 7098f74 | 2009-07-10 01:54:09 -0700 | [diff] [blame] | 234 | gic_basic_init(numintrs, numvpes, intr_map, intr_map_size); |
Steven J. Hill | 0b271f5 | 2012-08-31 16:05:37 -0500 | [diff] [blame] | 235 | |
| 236 | gic_platform_init(numintrs, &gic_irq_controller); |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 237 | } |