Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/common/gic.c |
| 3 | * |
| 4 | * Copyright (C) 2002 ARM Limited, All Rights Reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Interrupt architecture for the GIC: |
| 11 | * |
| 12 | * o There is one Interrupt Distributor, which receives interrupts |
| 13 | * from system devices and sends them to the Interrupt Controllers. |
| 14 | * |
| 15 | * o There is one CPU Interface per CPU, which sends interrupts sent |
| 16 | * by the Distributor, and interrupts generated locally, to the |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 17 | * associated CPU. The base address of the CPU interface is usually |
| 18 | * aliased so that the same address points to different chips depending |
| 19 | * on the CPU it is accessed from. |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 20 | * |
| 21 | * Note that IRQs 0-31 are special - they are local to each CPU. |
| 22 | * As such, the enable set/clear, pending set/clear and active bit |
| 23 | * registers are banked per-cpu for these sources. |
| 24 | */ |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/smp.h> |
Colin Cross | 254056f | 2011-02-10 12:54:10 -0800 | [diff] [blame^] | 29 | #include <linux/cpu_pm.h> |
Catalin Marinas | dcb86e8 | 2005-08-31 21:45:14 +0100 | [diff] [blame] | 30 | #include <linux/cpumask.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 31 | #include <linux/io.h> |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 32 | |
| 33 | #include <asm/irq.h> |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 34 | #include <asm/mach/irq.h> |
| 35 | #include <asm/hardware/gic.h> |
| 36 | |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(irq_controller_lock); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 38 | |
Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 39 | /* Address of GIC 0 CPU interface */ |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 40 | void __iomem *gic_cpu_base_addr __read_mostly; |
Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 41 | |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 42 | /* |
| 43 | * Supported arch specific GIC irq extension. |
| 44 | * Default make them NULL. |
| 45 | */ |
| 46 | struct irq_chip gic_arch_extn = { |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 47 | .irq_eoi = NULL, |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 48 | .irq_mask = NULL, |
| 49 | .irq_unmask = NULL, |
| 50 | .irq_retrigger = NULL, |
| 51 | .irq_set_type = NULL, |
| 52 | .irq_set_wake = NULL, |
| 53 | }; |
| 54 | |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 55 | #ifndef MAX_GIC_NR |
| 56 | #define MAX_GIC_NR 1 |
| 57 | #endif |
| 58 | |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 59 | static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly; |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 60 | |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 61 | static inline void __iomem *gic_dist_base(struct irq_data *d) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 62 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 63 | struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 64 | return gic_data->dist_base; |
| 65 | } |
| 66 | |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 67 | static inline void __iomem *gic_cpu_base(struct irq_data *d) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 68 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 69 | struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 70 | return gic_data->cpu_base; |
| 71 | } |
| 72 | |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 73 | static inline unsigned int gic_irq(struct irq_data *d) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 74 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 75 | struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d); |
| 76 | return d->irq - gic_data->irq_offset; |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 79 | /* |
| 80 | * Routines to acknowledge, disable and enable interrupts |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 81 | */ |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 82 | static void gic_mask_irq(struct irq_data *d) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 83 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 84 | u32 mask = 1 << (d->irq % 32); |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 85 | |
| 86 | spin_lock(&irq_controller_lock); |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 87 | writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4); |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 88 | if (gic_arch_extn.irq_mask) |
| 89 | gic_arch_extn.irq_mask(d); |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 90 | spin_unlock(&irq_controller_lock); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 93 | static void gic_unmask_irq(struct irq_data *d) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 94 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 95 | u32 mask = 1 << (d->irq % 32); |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 96 | |
| 97 | spin_lock(&irq_controller_lock); |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 98 | if (gic_arch_extn.irq_unmask) |
| 99 | gic_arch_extn.irq_unmask(d); |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 100 | writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4); |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 101 | spin_unlock(&irq_controller_lock); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 104 | static void gic_eoi_irq(struct irq_data *d) |
| 105 | { |
| 106 | if (gic_arch_extn.irq_eoi) { |
| 107 | spin_lock(&irq_controller_lock); |
| 108 | gic_arch_extn.irq_eoi(d); |
| 109 | spin_unlock(&irq_controller_lock); |
| 110 | } |
| 111 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 112 | writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI); |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 115 | static int gic_set_type(struct irq_data *d, unsigned int type) |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 116 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 117 | void __iomem *base = gic_dist_base(d); |
| 118 | unsigned int gicirq = gic_irq(d); |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 119 | u32 enablemask = 1 << (gicirq % 32); |
| 120 | u32 enableoff = (gicirq / 32) * 4; |
| 121 | u32 confmask = 0x2 << ((gicirq % 16) * 2); |
| 122 | u32 confoff = (gicirq / 16) * 4; |
| 123 | bool enabled = false; |
| 124 | u32 val; |
| 125 | |
| 126 | /* Interrupt configuration for SGIs can't be changed */ |
| 127 | if (gicirq < 16) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | spin_lock(&irq_controller_lock); |
| 134 | |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 135 | if (gic_arch_extn.irq_set_type) |
| 136 | gic_arch_extn.irq_set_type(d, type); |
| 137 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 138 | val = readl_relaxed(base + GIC_DIST_CONFIG + confoff); |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 139 | if (type == IRQ_TYPE_LEVEL_HIGH) |
| 140 | val &= ~confmask; |
| 141 | else if (type == IRQ_TYPE_EDGE_RISING) |
| 142 | val |= confmask; |
| 143 | |
| 144 | /* |
| 145 | * As recommended by the spec, disable the interrupt before changing |
| 146 | * the configuration |
| 147 | */ |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 148 | if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) { |
| 149 | writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff); |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 150 | enabled = true; |
| 151 | } |
| 152 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 153 | writel_relaxed(val, base + GIC_DIST_CONFIG + confoff); |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 154 | |
| 155 | if (enabled) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 156 | writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff); |
Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 157 | |
| 158 | spin_unlock(&irq_controller_lock); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 163 | static int gic_retrigger(struct irq_data *d) |
| 164 | { |
| 165 | if (gic_arch_extn.irq_retrigger) |
| 166 | return gic_arch_extn.irq_retrigger(d); |
| 167 | |
| 168 | return -ENXIO; |
| 169 | } |
| 170 | |
Catalin Marinas | a06f546 | 2005-09-30 16:07:05 +0100 | [diff] [blame] | 171 | #ifdef CONFIG_SMP |
Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 172 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 173 | bool force) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 174 | { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 175 | void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3); |
| 176 | unsigned int shift = (d->irq % 4) * 8; |
Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 177 | unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask); |
Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 178 | u32 val, mask, bit; |
| 179 | |
Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 180 | if (cpu >= 8 || cpu >= nr_cpu_ids) |
Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 181 | return -EINVAL; |
| 182 | |
| 183 | mask = 0xff << shift; |
| 184 | bit = 1 << (cpu + shift); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 185 | |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 186 | spin_lock(&irq_controller_lock); |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 187 | val = readl_relaxed(reg) & ~mask; |
| 188 | writel_relaxed(val | bit, reg); |
Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 189 | spin_unlock(&irq_controller_lock); |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 190 | |
Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 191 | return IRQ_SET_MASK_OK; |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 192 | } |
Catalin Marinas | a06f546 | 2005-09-30 16:07:05 +0100 | [diff] [blame] | 193 | #endif |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 194 | |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 195 | #ifdef CONFIG_PM |
| 196 | static int gic_set_wake(struct irq_data *d, unsigned int on) |
| 197 | { |
| 198 | int ret = -ENXIO; |
| 199 | |
| 200 | if (gic_arch_extn.irq_set_wake) |
| 201 | ret = gic_arch_extn.irq_set_wake(d, on); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | #else |
| 207 | #define gic_set_wake NULL |
| 208 | #endif |
| 209 | |
Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 210 | static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 211 | { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 212 | struct gic_chip_data *chip_data = irq_get_handler_data(irq); |
| 213 | struct irq_chip *chip = irq_get_chip(irq); |
Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 214 | unsigned int cascade_irq, gic_irq; |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 215 | unsigned long status; |
| 216 | |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 217 | chained_irq_enter(chip, desc); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 218 | |
| 219 | spin_lock(&irq_controller_lock); |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 220 | status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 221 | spin_unlock(&irq_controller_lock); |
| 222 | |
Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 223 | gic_irq = (status & 0x3ff); |
| 224 | if (gic_irq == 1023) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 225 | goto out; |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 226 | |
Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 227 | cascade_irq = gic_irq + chip_data->irq_offset; |
| 228 | if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS)) |
| 229 | do_bad_IRQ(cascade_irq, desc); |
| 230 | else |
| 231 | generic_handle_irq(cascade_irq); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 232 | |
| 233 | out: |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 234 | chained_irq_exit(chip, desc); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 235 | } |
| 236 | |
David Brownell | 38c677c | 2006-08-01 22:26:25 +0100 | [diff] [blame] | 237 | static struct irq_chip gic_chip = { |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 238 | .name = "GIC", |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 239 | .irq_mask = gic_mask_irq, |
| 240 | .irq_unmask = gic_unmask_irq, |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 241 | .irq_eoi = gic_eoi_irq, |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 242 | .irq_set_type = gic_set_type, |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 243 | .irq_retrigger = gic_retrigger, |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 244 | #ifdef CONFIG_SMP |
Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 245 | .irq_set_affinity = gic_set_affinity, |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 246 | #endif |
Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 247 | .irq_set_wake = gic_set_wake, |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 248 | }; |
| 249 | |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 250 | void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq) |
| 251 | { |
| 252 | if (gic_nr >= MAX_GIC_NR) |
| 253 | BUG(); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 254 | if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0) |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 255 | BUG(); |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 256 | irq_set_chained_handler(irq, gic_handle_cascade_irq); |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 259 | static void __init gic_dist_init(struct gic_chip_data *gic, |
Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 260 | unsigned int irq_start) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 261 | { |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 262 | unsigned int gic_irqs, irq_limit, i; |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 263 | void __iomem *base = gic->dist_base; |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 264 | u32 cpumask = 1 << smp_processor_id(); |
| 265 | |
| 266 | cpumask |= cpumask << 8; |
| 267 | cpumask |= cpumask << 16; |
| 268 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 269 | writel_relaxed(0, base + GIC_DIST_CTRL); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Find out how many interrupts are supported. |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 273 | * The GIC only supports up to 1020 interrupt sources. |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 274 | */ |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 275 | gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f; |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 276 | gic_irqs = (gic_irqs + 1) * 32; |
| 277 | if (gic_irqs > 1020) |
| 278 | gic_irqs = 1020; |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 279 | |
Colin Cross | 254056f | 2011-02-10 12:54:10 -0800 | [diff] [blame^] | 280 | gic->gic_irqs = gic_irqs; |
| 281 | |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 282 | /* |
| 283 | * Set all global interrupts to be level triggered, active low. |
| 284 | */ |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 285 | for (i = 32; i < gic_irqs; i += 16) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 286 | writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * Set all global interrupts to this CPU only. |
| 290 | */ |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 291 | for (i = 32; i < gic_irqs; i += 4) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 292 | writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 293 | |
| 294 | /* |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 295 | * Set priority on all global interrupts. |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 296 | */ |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 297 | for (i = 32; i < gic_irqs; i += 4) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 298 | writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 299 | |
| 300 | /* |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 301 | * Disable all interrupts. Leave the PPI and SGIs alone |
| 302 | * as these enables are banked registers. |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 303 | */ |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 304 | for (i = 32; i < gic_irqs; i += 32) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 305 | writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 306 | |
| 307 | /* |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 308 | * Limit number of interrupts registered to the platform maximum |
| 309 | */ |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 310 | irq_limit = gic->irq_offset + gic_irqs; |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 311 | if (WARN_ON(irq_limit > NR_IRQS)) |
| 312 | irq_limit = NR_IRQS; |
| 313 | |
| 314 | /* |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 315 | * Setup the Linux IRQ subsystem. |
| 316 | */ |
Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 317 | for (i = irq_start; i < irq_limit; i++) { |
Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 318 | irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq); |
Thomas Gleixner | 9323f261 | 2011-03-24 13:29:39 +0100 | [diff] [blame] | 319 | irq_set_chip_data(i, gic); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 320 | set_irq_flags(i, IRQF_VALID | IRQF_PROBE); |
| 321 | } |
| 322 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 323 | writel_relaxed(1, base + GIC_DIST_CTRL); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 326 | static void __cpuinit gic_cpu_init(struct gic_chip_data *gic) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 327 | { |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 328 | void __iomem *dist_base = gic->dist_base; |
| 329 | void __iomem *base = gic->cpu_base; |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 330 | int i; |
| 331 | |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 332 | /* |
| 333 | * Deal with the banked PPI and SGI interrupts - disable all |
| 334 | * PPI interrupts, ensure all SGI interrupts are enabled. |
| 335 | */ |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 336 | writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR); |
| 337 | writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET); |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Set priority on PPI and SGI interrupts |
| 341 | */ |
| 342 | for (i = 0; i < 32; i += 4) |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 343 | writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4); |
Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 344 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 345 | writel_relaxed(0xf0, base + GIC_CPU_PRIMASK); |
| 346 | writel_relaxed(1, base + GIC_CPU_CTRL); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Colin Cross | 254056f | 2011-02-10 12:54:10 -0800 | [diff] [blame^] | 349 | #ifdef CONFIG_CPU_PM |
| 350 | /* |
| 351 | * Saves the GIC distributor registers during suspend or idle. Must be called |
| 352 | * with interrupts disabled but before powering down the GIC. After calling |
| 353 | * this function, no interrupts will be delivered by the GIC, and another |
| 354 | * platform-specific wakeup source must be enabled. |
| 355 | */ |
| 356 | static void gic_dist_save(unsigned int gic_nr) |
| 357 | { |
| 358 | unsigned int gic_irqs; |
| 359 | void __iomem *dist_base; |
| 360 | int i; |
| 361 | |
| 362 | if (gic_nr >= MAX_GIC_NR) |
| 363 | BUG(); |
| 364 | |
| 365 | gic_irqs = gic_data[gic_nr].gic_irqs; |
| 366 | dist_base = gic_data[gic_nr].dist_base; |
| 367 | |
| 368 | if (!dist_base) |
| 369 | return; |
| 370 | |
| 371 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++) |
| 372 | gic_data[gic_nr].saved_spi_conf[i] = |
| 373 | readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4); |
| 374 | |
| 375 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++) |
| 376 | gic_data[gic_nr].saved_spi_target[i] = |
| 377 | readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4); |
| 378 | |
| 379 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) |
| 380 | gic_data[gic_nr].saved_spi_enable[i] = |
| 381 | readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Restores the GIC distributor registers during resume or when coming out of |
| 386 | * idle. Must be called before enabling interrupts. If a level interrupt |
| 387 | * that occured while the GIC was suspended is still present, it will be |
| 388 | * handled normally, but any edge interrupts that occured will not be seen by |
| 389 | * the GIC and need to be handled by the platform-specific wakeup source. |
| 390 | */ |
| 391 | static void gic_dist_restore(unsigned int gic_nr) |
| 392 | { |
| 393 | unsigned int gic_irqs; |
| 394 | unsigned int i; |
| 395 | void __iomem *dist_base; |
| 396 | |
| 397 | if (gic_nr >= MAX_GIC_NR) |
| 398 | BUG(); |
| 399 | |
| 400 | gic_irqs = gic_data[gic_nr].gic_irqs; |
| 401 | dist_base = gic_data[gic_nr].dist_base; |
| 402 | |
| 403 | if (!dist_base) |
| 404 | return; |
| 405 | |
| 406 | writel_relaxed(0, dist_base + GIC_DIST_CTRL); |
| 407 | |
| 408 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++) |
| 409 | writel_relaxed(gic_data[gic_nr].saved_spi_conf[i], |
| 410 | dist_base + GIC_DIST_CONFIG + i * 4); |
| 411 | |
| 412 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++) |
| 413 | writel_relaxed(0xa0a0a0a0, |
| 414 | dist_base + GIC_DIST_PRI + i * 4); |
| 415 | |
| 416 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++) |
| 417 | writel_relaxed(gic_data[gic_nr].saved_spi_target[i], |
| 418 | dist_base + GIC_DIST_TARGET + i * 4); |
| 419 | |
| 420 | for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) |
| 421 | writel_relaxed(gic_data[gic_nr].saved_spi_enable[i], |
| 422 | dist_base + GIC_DIST_ENABLE_SET + i * 4); |
| 423 | |
| 424 | writel_relaxed(1, dist_base + GIC_DIST_CTRL); |
| 425 | } |
| 426 | |
| 427 | static void gic_cpu_save(unsigned int gic_nr) |
| 428 | { |
| 429 | int i; |
| 430 | u32 *ptr; |
| 431 | void __iomem *dist_base; |
| 432 | void __iomem *cpu_base; |
| 433 | |
| 434 | if (gic_nr >= MAX_GIC_NR) |
| 435 | BUG(); |
| 436 | |
| 437 | dist_base = gic_data[gic_nr].dist_base; |
| 438 | cpu_base = gic_data[gic_nr].cpu_base; |
| 439 | |
| 440 | if (!dist_base || !cpu_base) |
| 441 | return; |
| 442 | |
| 443 | ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable); |
| 444 | for (i = 0; i < DIV_ROUND_UP(32, 32); i++) |
| 445 | ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4); |
| 446 | |
| 447 | ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf); |
| 448 | for (i = 0; i < DIV_ROUND_UP(32, 16); i++) |
| 449 | ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4); |
| 450 | |
| 451 | } |
| 452 | |
| 453 | static void gic_cpu_restore(unsigned int gic_nr) |
| 454 | { |
| 455 | int i; |
| 456 | u32 *ptr; |
| 457 | void __iomem *dist_base; |
| 458 | void __iomem *cpu_base; |
| 459 | |
| 460 | if (gic_nr >= MAX_GIC_NR) |
| 461 | BUG(); |
| 462 | |
| 463 | dist_base = gic_data[gic_nr].dist_base; |
| 464 | cpu_base = gic_data[gic_nr].cpu_base; |
| 465 | |
| 466 | if (!dist_base || !cpu_base) |
| 467 | return; |
| 468 | |
| 469 | ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable); |
| 470 | for (i = 0; i < DIV_ROUND_UP(32, 32); i++) |
| 471 | writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4); |
| 472 | |
| 473 | ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf); |
| 474 | for (i = 0; i < DIV_ROUND_UP(32, 16); i++) |
| 475 | writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4); |
| 476 | |
| 477 | for (i = 0; i < DIV_ROUND_UP(32, 4); i++) |
| 478 | writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4); |
| 479 | |
| 480 | writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK); |
| 481 | writel_relaxed(1, cpu_base + GIC_CPU_CTRL); |
| 482 | } |
| 483 | |
| 484 | static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v) |
| 485 | { |
| 486 | int i; |
| 487 | |
| 488 | for (i = 0; i < MAX_GIC_NR; i++) { |
| 489 | switch (cmd) { |
| 490 | case CPU_PM_ENTER: |
| 491 | gic_cpu_save(i); |
| 492 | break; |
| 493 | case CPU_PM_ENTER_FAILED: |
| 494 | case CPU_PM_EXIT: |
| 495 | gic_cpu_restore(i); |
| 496 | break; |
| 497 | case CPU_CLUSTER_PM_ENTER: |
| 498 | gic_dist_save(i); |
| 499 | break; |
| 500 | case CPU_CLUSTER_PM_ENTER_FAILED: |
| 501 | case CPU_CLUSTER_PM_EXIT: |
| 502 | gic_dist_restore(i); |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return NOTIFY_OK; |
| 508 | } |
| 509 | |
| 510 | static struct notifier_block gic_notifier_block = { |
| 511 | .notifier_call = gic_notifier, |
| 512 | }; |
| 513 | |
| 514 | static void __init gic_pm_init(struct gic_chip_data *gic) |
| 515 | { |
| 516 | gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4, |
| 517 | sizeof(u32)); |
| 518 | BUG_ON(!gic->saved_ppi_enable); |
| 519 | |
| 520 | gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4, |
| 521 | sizeof(u32)); |
| 522 | BUG_ON(!gic->saved_ppi_conf); |
| 523 | |
| 524 | cpu_pm_register_notifier(&gic_notifier_block); |
| 525 | } |
| 526 | #else |
| 527 | static void __init gic_pm_init(struct gic_chip_data *gic) |
| 528 | { |
| 529 | } |
| 530 | #endif |
| 531 | |
Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 532 | void __init gic_init(unsigned int gic_nr, unsigned int irq_start, |
| 533 | void __iomem *dist_base, void __iomem *cpu_base) |
| 534 | { |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 535 | struct gic_chip_data *gic; |
| 536 | |
| 537 | BUG_ON(gic_nr >= MAX_GIC_NR); |
| 538 | |
| 539 | gic = &gic_data[gic_nr]; |
| 540 | gic->dist_base = dist_base; |
| 541 | gic->cpu_base = cpu_base; |
| 542 | gic->irq_offset = (irq_start - 1) & ~31; |
| 543 | |
Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 544 | if (gic_nr == 0) |
| 545 | gic_cpu_base_addr = cpu_base; |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 546 | |
| 547 | gic_dist_init(gic, irq_start); |
| 548 | gic_cpu_init(gic); |
Colin Cross | 254056f | 2011-02-10 12:54:10 -0800 | [diff] [blame^] | 549 | gic_pm_init(gic); |
Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Russell King | 3848953 | 2010-12-04 16:01:03 +0000 | [diff] [blame] | 552 | void __cpuinit gic_secondary_init(unsigned int gic_nr) |
| 553 | { |
Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 554 | BUG_ON(gic_nr >= MAX_GIC_NR); |
| 555 | |
| 556 | gic_cpu_init(&gic_data[gic_nr]); |
Russell King | 3848953 | 2010-12-04 16:01:03 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Russell King | ac61d14 | 2010-12-06 10:38:14 +0000 | [diff] [blame] | 559 | void __cpuinit gic_enable_ppi(unsigned int irq) |
| 560 | { |
| 561 | unsigned long flags; |
| 562 | |
| 563 | local_irq_save(flags); |
Thomas Gleixner | fdea77b | 2011-03-24 12:48:54 +0100 | [diff] [blame] | 564 | irq_set_status_flags(irq, IRQ_NOPROBE); |
Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 565 | gic_unmask_irq(irq_get_irq_data(irq)); |
Russell King | ac61d14 | 2010-12-06 10:38:14 +0000 | [diff] [blame] | 566 | local_irq_restore(flags); |
| 567 | } |
| 568 | |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 569 | #ifdef CONFIG_SMP |
Russell King | 8266810 | 2009-05-17 16:20:18 +0100 | [diff] [blame] | 570 | void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 571 | { |
Russell King | 8266810 | 2009-05-17 16:20:18 +0100 | [diff] [blame] | 572 | unsigned long map = *cpus_addr(*mask); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 573 | |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 574 | /* |
| 575 | * Ensure that stores to Normal memory are visible to the |
| 576 | * other CPUs before issuing the IPI. |
| 577 | */ |
| 578 | dsb(); |
| 579 | |
Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 580 | /* this always happens on GIC0 */ |
Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 581 | writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT); |
Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 582 | } |
| 583 | #endif |