blob: f3c1ebfdd0aabaa8ec8cc87d089f9fe74ce1899f [file] [log] [blame]
Russell Kingf27ecac2005-08-18 21:31:00 +01001/*
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
17 * associated CPU.
18 *
19 * Note that IRQs 0-31 are special - they are local to each CPU.
20 * As such, the enable set/clear, pending set/clear and active bit
21 * registers are banked per-cpu for these sources.
22 */
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/list.h>
26#include <linux/smp.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010027#include <linux/cpumask.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010028
29#include <asm/irq.h>
30#include <asm/io.h>
31#include <asm/mach/irq.h>
32#include <asm/hardware/gic.h>
33
34static void __iomem *gic_dist_base;
35static void __iomem *gic_cpu_base;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010036static DEFINE_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010037
38/*
39 * Routines to acknowledge, disable and enable interrupts
40 *
41 * Linux assumes that when we're done with an interrupt we need to
42 * unmask it, in the same way we need to unmask an interrupt when
43 * we first enable it.
44 *
45 * The GIC has a seperate notion of "end of interrupt" to re-enable
46 * an interrupt after handling, in order to support hardware
47 * prioritisation.
48 *
49 * We can make the GIC behave in the way that Linux expects by making
50 * our "acknowledge" routine disable the interrupt, then mark it as
51 * complete.
52 */
53static void gic_ack_irq(unsigned int irq)
54{
55 u32 mask = 1 << (irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010056
57 spin_lock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010058 writel(mask, gic_dist_base + GIC_DIST_ENABLE_CLEAR + (irq / 32) * 4);
59 writel(irq, gic_cpu_base + GIC_CPU_EOI);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010060 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010061}
62
63static void gic_mask_irq(unsigned int irq)
64{
65 u32 mask = 1 << (irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010066
67 spin_lock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010068 writel(mask, gic_dist_base + GIC_DIST_ENABLE_CLEAR + (irq / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010069 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010070}
71
72static void gic_unmask_irq(unsigned int irq)
73{
74 u32 mask = 1 << (irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010075
76 spin_lock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010077 writel(mask, gic_dist_base + GIC_DIST_ENABLE_SET + (irq / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010078 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010079}
80
Catalin Marinasa06f5462005-09-30 16:07:05 +010081#ifdef CONFIG_SMP
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010082static void gic_set_cpu(unsigned int irq, cpumask_t mask_val)
Russell Kingf27ecac2005-08-18 21:31:00 +010083{
84 void __iomem *reg = gic_dist_base + GIC_DIST_TARGET + (irq & ~3);
85 unsigned int shift = (irq % 4) * 8;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010086 unsigned int cpu = first_cpu(mask_val);
Russell Kingf27ecac2005-08-18 21:31:00 +010087 u32 val;
88
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010089 spin_lock(&irq_controller_lock);
90 irq_desc[irq].cpu = cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +010091 val = readl(reg) & ~(0xff << shift);
92 val |= 1 << (cpu + shift);
93 writel(val, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010094 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010095}
Catalin Marinasa06f5462005-09-30 16:07:05 +010096#endif
Russell Kingf27ecac2005-08-18 21:31:00 +010097
98static struct irqchip gic_chip = {
99 .ack = gic_ack_irq,
100 .mask = gic_mask_irq,
101 .unmask = gic_unmask_irq,
102#ifdef CONFIG_SMP
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100103 .set_affinity = gic_set_cpu,
Russell Kingf27ecac2005-08-18 21:31:00 +0100104#endif
105};
106
107void __init gic_dist_init(void __iomem *base)
108{
109 unsigned int max_irq, i;
110 u32 cpumask = 1 << smp_processor_id();
111
112 cpumask |= cpumask << 8;
113 cpumask |= cpumask << 16;
114
115 gic_dist_base = base;
116
117 writel(0, base + GIC_DIST_CTRL);
118
119 /*
120 * Find out how many interrupts are supported.
121 */
122 max_irq = readl(base + GIC_DIST_CTR) & 0x1f;
123 max_irq = (max_irq + 1) * 32;
124
125 /*
126 * The GIC only supports up to 1020 interrupt sources.
127 * Limit this to either the architected maximum, or the
128 * platform maximum.
129 */
130 if (max_irq > max(1020, NR_IRQS))
131 max_irq = max(1020, NR_IRQS);
132
133 /*
134 * Set all global interrupts to be level triggered, active low.
135 */
136 for (i = 32; i < max_irq; i += 16)
137 writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
138
139 /*
140 * Set all global interrupts to this CPU only.
141 */
142 for (i = 32; i < max_irq; i += 4)
143 writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
144
145 /*
146 * Set priority on all interrupts.
147 */
148 for (i = 0; i < max_irq; i += 4)
149 writel(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
150
151 /*
152 * Disable all interrupts.
153 */
154 for (i = 0; i < max_irq; i += 32)
155 writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
156
157 /*
158 * Setup the Linux IRQ subsystem.
159 */
160 for (i = 29; i < max_irq; i++) {
161 set_irq_chip(i, &gic_chip);
162 set_irq_handler(i, do_level_IRQ);
163 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
164 }
165
166 writel(1, base + GIC_DIST_CTRL);
167}
168
169void __cpuinit gic_cpu_init(void __iomem *base)
170{
171 gic_cpu_base = base;
172 writel(0xf0, base + GIC_CPU_PRIMASK);
173 writel(1, base + GIC_CPU_CTRL);
174}
175
176#ifdef CONFIG_SMP
177void gic_raise_softirq(cpumask_t cpumask, unsigned int irq)
178{
179 unsigned long map = *cpus_addr(cpumask);
180
181 writel(map << 16 | irq, gic_dist_base + GIC_DIST_SOFTINT);
182}
183#endif