| 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> | 
| Catalin Marinas | dcb86e8 | 2005-08-31 21:45:14 +0100 | [diff] [blame] | 29 | #include <linux/cpumask.h> | 
| Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 30 | #include <linux/io.h> | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 31 |  | 
 | 32 | #include <asm/irq.h> | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 33 | #include <asm/mach/irq.h> | 
 | 34 | #include <asm/hardware/gic.h> | 
 | 35 |  | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 36 | static DEFINE_SPINLOCK(irq_controller_lock); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 37 |  | 
| Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 38 | /* Address of GIC 0 CPU interface */ | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 39 | void __iomem *gic_cpu_base_addr __read_mostly; | 
| Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 40 |  | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 41 | /* | 
 | 42 |  * Supported arch specific GIC irq extension. | 
 | 43 |  * Default make them NULL. | 
 | 44 |  */ | 
 | 45 | struct irq_chip gic_arch_extn = { | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 46 | 	.irq_eoi	= NULL, | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 47 | 	.irq_mask	= NULL, | 
 | 48 | 	.irq_unmask	= NULL, | 
 | 49 | 	.irq_retrigger	= NULL, | 
 | 50 | 	.irq_set_type	= NULL, | 
 | 51 | 	.irq_set_wake	= NULL, | 
 | 52 | }; | 
 | 53 |  | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 54 | #ifndef MAX_GIC_NR | 
 | 55 | #define MAX_GIC_NR	1 | 
 | 56 | #endif | 
 | 57 |  | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 58 | static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly; | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 59 |  | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 60 | static inline void __iomem *gic_dist_base(struct irq_data *d) | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 61 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 62 | 	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] | 63 | 	return gic_data->dist_base; | 
 | 64 | } | 
 | 65 |  | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 66 | static inline void __iomem *gic_cpu_base(struct irq_data *d) | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 67 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 68 | 	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] | 69 | 	return gic_data->cpu_base; | 
 | 70 | } | 
 | 71 |  | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 72 | static inline unsigned int gic_irq(struct irq_data *d) | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 73 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 74 | 	struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d); | 
 | 75 | 	return d->irq - gic_data->irq_offset; | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 76 | } | 
 | 77 |  | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 78 | /* | 
 | 79 |  * Routines to acknowledge, disable and enable interrupts | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 80 |  */ | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 81 | static void gic_mask_irq(struct irq_data *d) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 82 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 83 | 	u32 mask = 1 << (d->irq % 32); | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 84 |  | 
 | 85 | 	spin_lock(&irq_controller_lock); | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 86 | 	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] | 87 | 	if (gic_arch_extn.irq_mask) | 
 | 88 | 		gic_arch_extn.irq_mask(d); | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 89 | 	spin_unlock(&irq_controller_lock); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 90 | } | 
 | 91 |  | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 92 | static void gic_unmask_irq(struct irq_data *d) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 93 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 94 | 	u32 mask = 1 << (d->irq % 32); | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 95 |  | 
 | 96 | 	spin_lock(&irq_controller_lock); | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 97 | 	if (gic_arch_extn.irq_unmask) | 
 | 98 | 		gic_arch_extn.irq_unmask(d); | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 99 | 	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] | 100 | 	spin_unlock(&irq_controller_lock); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 101 | } | 
 | 102 |  | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 103 | static void gic_eoi_irq(struct irq_data *d) | 
 | 104 | { | 
 | 105 | 	if (gic_arch_extn.irq_eoi) { | 
 | 106 | 		spin_lock(&irq_controller_lock); | 
 | 107 | 		gic_arch_extn.irq_eoi(d); | 
 | 108 | 		spin_unlock(&irq_controller_lock); | 
 | 109 | 	} | 
 | 110 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 111 | 	writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI); | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 112 | } | 
 | 113 |  | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 114 | static int gic_set_type(struct irq_data *d, unsigned int type) | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 115 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 116 | 	void __iomem *base = gic_dist_base(d); | 
 | 117 | 	unsigned int gicirq = gic_irq(d); | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 118 | 	u32 enablemask = 1 << (gicirq % 32); | 
 | 119 | 	u32 enableoff = (gicirq / 32) * 4; | 
 | 120 | 	u32 confmask = 0x2 << ((gicirq % 16) * 2); | 
 | 121 | 	u32 confoff = (gicirq / 16) * 4; | 
 | 122 | 	bool enabled = false; | 
 | 123 | 	u32 val; | 
 | 124 |  | 
 | 125 | 	/* Interrupt configuration for SGIs can't be changed */ | 
 | 126 | 	if (gicirq < 16) | 
 | 127 | 		return -EINVAL; | 
 | 128 |  | 
 | 129 | 	if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING) | 
 | 130 | 		return -EINVAL; | 
 | 131 |  | 
 | 132 | 	spin_lock(&irq_controller_lock); | 
 | 133 |  | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 134 | 	if (gic_arch_extn.irq_set_type) | 
 | 135 | 		gic_arch_extn.irq_set_type(d, type); | 
 | 136 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 137 | 	val = readl_relaxed(base + GIC_DIST_CONFIG + confoff); | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 138 | 	if (type == IRQ_TYPE_LEVEL_HIGH) | 
 | 139 | 		val &= ~confmask; | 
 | 140 | 	else if (type == IRQ_TYPE_EDGE_RISING) | 
 | 141 | 		val |= confmask; | 
 | 142 |  | 
 | 143 | 	/* | 
 | 144 | 	 * As recommended by the spec, disable the interrupt before changing | 
 | 145 | 	 * the configuration | 
 | 146 | 	 */ | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 147 | 	if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) { | 
 | 148 | 		writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff); | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 149 | 		enabled = true; | 
 | 150 | 	} | 
 | 151 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 152 | 	writel_relaxed(val, base + GIC_DIST_CONFIG + confoff); | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 153 |  | 
 | 154 | 	if (enabled) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 155 | 		writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff); | 
| Rabin Vincent | 5c0c1f0 | 2010-05-28 04:37:38 +0100 | [diff] [blame] | 156 |  | 
 | 157 | 	spin_unlock(&irq_controller_lock); | 
 | 158 |  | 
 | 159 | 	return 0; | 
 | 160 | } | 
 | 161 |  | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 162 | static int gic_retrigger(struct irq_data *d) | 
 | 163 | { | 
 | 164 | 	if (gic_arch_extn.irq_retrigger) | 
 | 165 | 		return gic_arch_extn.irq_retrigger(d); | 
 | 166 |  | 
 | 167 | 	return -ENXIO; | 
 | 168 | } | 
 | 169 |  | 
| Catalin Marinas | a06f546 | 2005-09-30 16:07:05 +0100 | [diff] [blame] | 170 | #ifdef CONFIG_SMP | 
| Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 171 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val, | 
 | 172 | 			    bool force) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 173 | { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 174 | 	void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3); | 
 | 175 | 	unsigned int shift = (d->irq % 4) * 8; | 
| Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 176 | 	unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask); | 
| Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 177 | 	u32 val, mask, bit; | 
 | 178 |  | 
| Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 179 | 	if (cpu >= 8 || cpu >= nr_cpu_ids) | 
| Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 180 | 		return -EINVAL; | 
 | 181 |  | 
 | 182 | 	mask = 0xff << shift; | 
 | 183 | 	bit = 1 << (cpu + shift); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 184 |  | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 185 | 	spin_lock(&irq_controller_lock); | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 186 | 	val = readl_relaxed(reg) & ~mask; | 
 | 187 | 	writel_relaxed(val | bit, reg); | 
| Thomas Gleixner | c4bfa28 | 2006-07-01 22:32:14 +0100 | [diff] [blame] | 188 | 	spin_unlock(&irq_controller_lock); | 
| Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 189 |  | 
| Russell King | 5dfc54e | 2011-07-21 15:00:57 +0100 | [diff] [blame] | 190 | 	return IRQ_SET_MASK_OK; | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 191 | } | 
| Catalin Marinas | a06f546 | 2005-09-30 16:07:05 +0100 | [diff] [blame] | 192 | #endif | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 193 |  | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 194 | #ifdef CONFIG_PM | 
 | 195 | static int gic_set_wake(struct irq_data *d, unsigned int on) | 
 | 196 | { | 
 | 197 | 	int ret = -ENXIO; | 
 | 198 |  | 
 | 199 | 	if (gic_arch_extn.irq_set_wake) | 
 | 200 | 		ret = gic_arch_extn.irq_set_wake(d, on); | 
 | 201 |  | 
 | 202 | 	return ret; | 
 | 203 | } | 
 | 204 |  | 
 | 205 | #else | 
 | 206 | #define gic_set_wake	NULL | 
 | 207 | #endif | 
 | 208 |  | 
| Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 209 | 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] | 210 | { | 
| Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 211 | 	struct gic_chip_data *chip_data = irq_get_handler_data(irq); | 
 | 212 | 	struct irq_chip *chip = irq_get_chip(irq); | 
| Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 213 | 	unsigned int cascade_irq, gic_irq; | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 214 | 	unsigned long status; | 
 | 215 |  | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 216 | 	chained_irq_enter(chip, desc); | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 217 |  | 
 | 218 | 	spin_lock(&irq_controller_lock); | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 219 | 	status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK); | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 220 | 	spin_unlock(&irq_controller_lock); | 
 | 221 |  | 
| Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 222 | 	gic_irq = (status & 0x3ff); | 
 | 223 | 	if (gic_irq == 1023) | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 224 | 		goto out; | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 225 |  | 
| Russell King | 0f347bb | 2007-05-17 10:11:34 +0100 | [diff] [blame] | 226 | 	cascade_irq = gic_irq + chip_data->irq_offset; | 
 | 227 | 	if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS)) | 
 | 228 | 		do_bad_IRQ(cascade_irq, desc); | 
 | 229 | 	else | 
 | 230 | 		generic_handle_irq(cascade_irq); | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 231 |  | 
 | 232 |  out: | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 233 | 	chained_irq_exit(chip, desc); | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 234 | } | 
 | 235 |  | 
| David Brownell | 38c677c | 2006-08-01 22:26:25 +0100 | [diff] [blame] | 236 | static struct irq_chip gic_chip = { | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 237 | 	.name			= "GIC", | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 238 | 	.irq_mask		= gic_mask_irq, | 
 | 239 | 	.irq_unmask		= gic_unmask_irq, | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 240 | 	.irq_eoi		= gic_eoi_irq, | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 241 | 	.irq_set_type		= gic_set_type, | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 242 | 	.irq_retrigger		= gic_retrigger, | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 243 | #ifdef CONFIG_SMP | 
| Russell King | c191789 | 2011-01-23 12:12:01 +0000 | [diff] [blame] | 244 | 	.irq_set_affinity	= gic_set_affinity, | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 245 | #endif | 
| Santosh Shilimkar | d7ed36a | 2011-03-02 08:03:22 +0100 | [diff] [blame] | 246 | 	.irq_set_wake		= gic_set_wake, | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 247 | }; | 
 | 248 |  | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 249 | void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq) | 
 | 250 | { | 
 | 251 | 	if (gic_nr >= MAX_GIC_NR) | 
 | 252 | 		BUG(); | 
| Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 253 | 	if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0) | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 254 | 		BUG(); | 
| Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 255 | 	irq_set_chained_handler(irq, gic_handle_cascade_irq); | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 256 | } | 
 | 257 |  | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 258 | static void __init gic_dist_init(struct gic_chip_data *gic, | 
| Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 259 | 	unsigned int irq_start) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 260 | { | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 261 | 	unsigned int gic_irqs, irq_limit, i; | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 262 | 	void __iomem *base = gic->dist_base; | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 263 | 	u32 cpumask = 1 << smp_processor_id(); | 
 | 264 |  | 
 | 265 | 	cpumask |= cpumask << 8; | 
 | 266 | 	cpumask |= cpumask << 16; | 
 | 267 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 268 | 	writel_relaxed(0, base + GIC_DIST_CTRL); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 269 |  | 
 | 270 | 	/* | 
 | 271 | 	 * Find out how many interrupts are supported. | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 272 | 	 * The GIC only supports up to 1020 interrupt sources. | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 273 | 	 */ | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 274 | 	gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f; | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 275 | 	gic_irqs = (gic_irqs + 1) * 32; | 
 | 276 | 	if (gic_irqs > 1020) | 
 | 277 | 		gic_irqs = 1020; | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 278 |  | 
 | 279 | 	/* | 
 | 280 | 	 * Set all global interrupts to be level triggered, active low. | 
 | 281 | 	 */ | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 282 | 	for (i = 32; i < gic_irqs; i += 16) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 283 | 		writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 284 |  | 
 | 285 | 	/* | 
 | 286 | 	 * Set all global interrupts to this CPU only. | 
 | 287 | 	 */ | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 288 | 	for (i = 32; i < gic_irqs; i += 4) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 289 | 		writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 290 |  | 
 | 291 | 	/* | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 292 | 	 * Set priority on all global interrupts. | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 293 | 	 */ | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 294 | 	for (i = 32; i < gic_irqs; i += 4) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 295 | 		writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 296 |  | 
 | 297 | 	/* | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 298 | 	 * Disable all interrupts.  Leave the PPI and SGIs alone | 
 | 299 | 	 * as these enables are banked registers. | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 300 | 	 */ | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 301 | 	for (i = 32; i < gic_irqs; i += 32) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 302 | 		writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 303 |  | 
 | 304 | 	/* | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 305 | 	 * Limit number of interrupts registered to the platform maximum | 
 | 306 | 	 */ | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 307 | 	irq_limit = gic->irq_offset + gic_irqs; | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 308 | 	if (WARN_ON(irq_limit > NR_IRQS)) | 
 | 309 | 		irq_limit = NR_IRQS; | 
 | 310 |  | 
 | 311 | 	/* | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 312 | 	 * Setup the Linux IRQ subsystem. | 
 | 313 | 	 */ | 
| Pawel Moll | e6afec9 | 2010-11-26 13:45:43 +0100 | [diff] [blame] | 314 | 	for (i = irq_start; i < irq_limit; i++) { | 
| Will Deacon | 1a01753 | 2011-02-09 12:01:12 +0000 | [diff] [blame] | 315 | 		irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq); | 
| Thomas Gleixner | 9323f261 | 2011-03-24 13:29:39 +0100 | [diff] [blame] | 316 | 		irq_set_chip_data(i, gic); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 317 | 		set_irq_flags(i, IRQF_VALID | IRQF_PROBE); | 
 | 318 | 	} | 
 | 319 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 320 | 	writel_relaxed(1, base + GIC_DIST_CTRL); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 321 | } | 
 | 322 |  | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 323 | static void __cpuinit gic_cpu_init(struct gic_chip_data *gic) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 324 | { | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 325 | 	void __iomem *dist_base = gic->dist_base; | 
 | 326 | 	void __iomem *base = gic->cpu_base; | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 327 | 	int i; | 
 | 328 |  | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 329 | 	/* | 
 | 330 | 	 * Deal with the banked PPI and SGI interrupts - disable all | 
 | 331 | 	 * PPI interrupts, ensure all SGI interrupts are enabled. | 
 | 332 | 	 */ | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 333 | 	writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR); | 
 | 334 | 	writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET); | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 335 |  | 
 | 336 | 	/* | 
 | 337 | 	 * Set priority on PPI and SGI interrupts | 
 | 338 | 	 */ | 
 | 339 | 	for (i = 0; i < 32; i += 4) | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 340 | 		writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4); | 
| Russell King | 9395f6e | 2010-11-11 23:10:30 +0000 | [diff] [blame] | 341 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 342 | 	writel_relaxed(0xf0, base + GIC_CPU_PRIMASK); | 
 | 343 | 	writel_relaxed(1, base + GIC_CPU_CTRL); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 344 | } | 
 | 345 |  | 
| Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 346 | void __init gic_init(unsigned int gic_nr, unsigned int irq_start, | 
 | 347 | 	void __iomem *dist_base, void __iomem *cpu_base) | 
 | 348 | { | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 349 | 	struct gic_chip_data *gic; | 
 | 350 |  | 
 | 351 | 	BUG_ON(gic_nr >= MAX_GIC_NR); | 
 | 352 |  | 
 | 353 | 	gic = &gic_data[gic_nr]; | 
 | 354 | 	gic->dist_base = dist_base; | 
 | 355 | 	gic->cpu_base = cpu_base; | 
 | 356 | 	gic->irq_offset = (irq_start - 1) & ~31; | 
 | 357 |  | 
| Russell King | ff2e27a | 2010-12-04 16:13:29 +0000 | [diff] [blame] | 358 | 	if (gic_nr == 0) | 
 | 359 | 		gic_cpu_base_addr = cpu_base; | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 360 |  | 
 | 361 | 	gic_dist_init(gic, irq_start); | 
 | 362 | 	gic_cpu_init(gic); | 
| Russell King | b580b89 | 2010-12-04 15:55:14 +0000 | [diff] [blame] | 363 | } | 
 | 364 |  | 
| Russell King | 3848953 | 2010-12-04 16:01:03 +0000 | [diff] [blame] | 365 | void __cpuinit gic_secondary_init(unsigned int gic_nr) | 
 | 366 | { | 
| Russell King | bef8f9e | 2010-12-04 16:50:58 +0000 | [diff] [blame] | 367 | 	BUG_ON(gic_nr >= MAX_GIC_NR); | 
 | 368 |  | 
 | 369 | 	gic_cpu_init(&gic_data[gic_nr]); | 
| Russell King | 3848953 | 2010-12-04 16:01:03 +0000 | [diff] [blame] | 370 | } | 
 | 371 |  | 
| Russell King | ac61d14 | 2010-12-06 10:38:14 +0000 | [diff] [blame] | 372 | void __cpuinit gic_enable_ppi(unsigned int irq) | 
 | 373 | { | 
 | 374 | 	unsigned long flags; | 
 | 375 |  | 
 | 376 | 	local_irq_save(flags); | 
| Thomas Gleixner | fdea77b | 2011-03-24 12:48:54 +0100 | [diff] [blame] | 377 | 	irq_set_status_flags(irq, IRQ_NOPROBE); | 
| Lennert Buytenhek | 7d1f428 | 2010-11-29 10:18:20 +0100 | [diff] [blame] | 378 | 	gic_unmask_irq(irq_get_irq_data(irq)); | 
| Russell King | ac61d14 | 2010-12-06 10:38:14 +0000 | [diff] [blame] | 379 | 	local_irq_restore(flags); | 
 | 380 | } | 
 | 381 |  | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 382 | #ifdef CONFIG_SMP | 
| Russell King | 8266810 | 2009-05-17 16:20:18 +0100 | [diff] [blame] | 383 | void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 384 | { | 
| Russell King | 8266810 | 2009-05-17 16:20:18 +0100 | [diff] [blame] | 385 | 	unsigned long map = *cpus_addr(*mask); | 
| Russell King | f27ecac | 2005-08-18 21:31:00 +0100 | [diff] [blame] | 386 |  | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 387 | 	/* | 
 | 388 | 	 * Ensure that stores to Normal memory are visible to the | 
 | 389 | 	 * other CPUs before issuing the IPI. | 
 | 390 | 	 */ | 
 | 391 | 	dsb(); | 
 | 392 |  | 
| Catalin Marinas | b3a1bde | 2007-02-14 19:14:56 +0100 | [diff] [blame] | 393 | 	/* this always happens on GIC0 */ | 
| Santosh Shilimkar | 6ac77e4 | 2011-03-28 19:27:46 +0530 | [diff] [blame] | 394 | 	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] | 395 | } | 
 | 396 | #endif |