Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
[pandora-kernel.git] / arch / arm / common / gic.c
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
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.
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>
29 #include <linux/cpu_pm.h>
30 #include <linux/cpumask.h>
31 #include <linux/io.h>
32 #include <linux/interrupt.h>
33 #include <linux/percpu.h>
34 #include <linux/slab.h>
35
36 #include <asm/irq.h>
37 #include <asm/mach/irq.h>
38 #include <asm/hardware/gic.h>
39
40 static DEFINE_RAW_SPINLOCK(irq_controller_lock);
41
42 /* Address of GIC 0 CPU interface */
43 void __iomem *gic_cpu_base_addr __read_mostly;
44
45 /*
46  * Supported arch specific GIC irq extension.
47  * Default make them NULL.
48  */
49 struct irq_chip gic_arch_extn = {
50         .irq_eoi        = NULL,
51         .irq_mask       = NULL,
52         .irq_unmask     = NULL,
53         .irq_retrigger  = NULL,
54         .irq_set_type   = NULL,
55         .irq_set_wake   = NULL,
56 };
57
58 #ifndef MAX_GIC_NR
59 #define MAX_GIC_NR      1
60 #endif
61
62 static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
63
64 static inline void __iomem *gic_dist_base(struct irq_data *d)
65 {
66         struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
67         return gic_data->dist_base;
68 }
69
70 static inline void __iomem *gic_cpu_base(struct irq_data *d)
71 {
72         struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
73         return gic_data->cpu_base;
74 }
75
76 static inline unsigned int gic_irq(struct irq_data *d)
77 {
78         struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
79         return d->irq - gic_data->irq_offset;
80 }
81
82 /*
83  * Routines to acknowledge, disable and enable interrupts
84  */
85 static void gic_mask_irq(struct irq_data *d)
86 {
87         u32 mask = 1 << (d->irq % 32);
88
89         raw_spin_lock(&irq_controller_lock);
90         writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
91         if (gic_arch_extn.irq_mask)
92                 gic_arch_extn.irq_mask(d);
93         raw_spin_unlock(&irq_controller_lock);
94 }
95
96 static void gic_unmask_irq(struct irq_data *d)
97 {
98         u32 mask = 1 << (d->irq % 32);
99
100         raw_spin_lock(&irq_controller_lock);
101         if (gic_arch_extn.irq_unmask)
102                 gic_arch_extn.irq_unmask(d);
103         writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
104         raw_spin_unlock(&irq_controller_lock);
105 }
106
107 static void gic_eoi_irq(struct irq_data *d)
108 {
109         if (gic_arch_extn.irq_eoi) {
110                 raw_spin_lock(&irq_controller_lock);
111                 gic_arch_extn.irq_eoi(d);
112                 raw_spin_unlock(&irq_controller_lock);
113         }
114
115         writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
116 }
117
118 static int gic_set_type(struct irq_data *d, unsigned int type)
119 {
120         void __iomem *base = gic_dist_base(d);
121         unsigned int gicirq = gic_irq(d);
122         u32 enablemask = 1 << (gicirq % 32);
123         u32 enableoff = (gicirq / 32) * 4;
124         u32 confmask = 0x2 << ((gicirq % 16) * 2);
125         u32 confoff = (gicirq / 16) * 4;
126         bool enabled = false;
127         u32 val;
128
129         /* Interrupt configuration for SGIs can't be changed */
130         if (gicirq < 16)
131                 return -EINVAL;
132
133         if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
134                 return -EINVAL;
135
136         raw_spin_lock(&irq_controller_lock);
137
138         if (gic_arch_extn.irq_set_type)
139                 gic_arch_extn.irq_set_type(d, type);
140
141         val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
142         if (type == IRQ_TYPE_LEVEL_HIGH)
143                 val &= ~confmask;
144         else if (type == IRQ_TYPE_EDGE_RISING)
145                 val |= confmask;
146
147         /*
148          * As recommended by the spec, disable the interrupt before changing
149          * the configuration
150          */
151         if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
152                 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
153                 enabled = true;
154         }
155
156         writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
157
158         if (enabled)
159                 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
160
161         raw_spin_unlock(&irq_controller_lock);
162
163         return 0;
164 }
165
166 static int gic_retrigger(struct irq_data *d)
167 {
168         if (gic_arch_extn.irq_retrigger)
169                 return gic_arch_extn.irq_retrigger(d);
170
171         return -ENXIO;
172 }
173
174 #ifdef CONFIG_SMP
175 static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
176                             bool force)
177 {
178         void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
179         unsigned int shift = (d->irq % 4) * 8;
180         unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
181         u32 val, mask, bit;
182
183         if (cpu >= 8 || cpu >= nr_cpu_ids)
184                 return -EINVAL;
185
186         mask = 0xff << shift;
187         bit = 1 << (cpu_logical_map(cpu) + shift);
188
189         raw_spin_lock(&irq_controller_lock);
190         val = readl_relaxed(reg) & ~mask;
191         writel_relaxed(val | bit, reg);
192         raw_spin_unlock(&irq_controller_lock);
193
194         return IRQ_SET_MASK_OK;
195 }
196 #endif
197
198 #ifdef CONFIG_PM
199 static int gic_set_wake(struct irq_data *d, unsigned int on)
200 {
201         int ret = -ENXIO;
202
203         if (gic_arch_extn.irq_set_wake)
204                 ret = gic_arch_extn.irq_set_wake(d, on);
205
206         return ret;
207 }
208
209 #else
210 #define gic_set_wake    NULL
211 #endif
212
213 static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
214 {
215         struct gic_chip_data *chip_data = irq_get_handler_data(irq);
216         struct irq_chip *chip = irq_get_chip(irq);
217         unsigned int cascade_irq, gic_irq;
218         unsigned long status;
219
220         chained_irq_enter(chip, desc);
221
222         raw_spin_lock(&irq_controller_lock);
223         status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
224         raw_spin_unlock(&irq_controller_lock);
225
226         gic_irq = (status & 0x3ff);
227         if (gic_irq == 1023)
228                 goto out;
229
230         cascade_irq = gic_irq + chip_data->irq_offset;
231         if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
232                 do_bad_IRQ(cascade_irq, desc);
233         else
234                 generic_handle_irq(cascade_irq);
235
236  out:
237         chained_irq_exit(chip, desc);
238 }
239
240 static struct irq_chip gic_chip = {
241         .name                   = "GIC",
242         .irq_mask               = gic_mask_irq,
243         .irq_unmask             = gic_unmask_irq,
244         .irq_eoi                = gic_eoi_irq,
245         .irq_set_type           = gic_set_type,
246         .irq_retrigger          = gic_retrigger,
247 #ifdef CONFIG_SMP
248         .irq_set_affinity       = gic_set_affinity,
249 #endif
250         .irq_set_wake           = gic_set_wake,
251 };
252
253 void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
254 {
255         if (gic_nr >= MAX_GIC_NR)
256                 BUG();
257         if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
258                 BUG();
259         irq_set_chained_handler(irq, gic_handle_cascade_irq);
260 }
261
262 static void __init gic_dist_init(struct gic_chip_data *gic,
263         unsigned int irq_start)
264 {
265         unsigned int gic_irqs, irq_limit, i;
266         u32 cpumask;
267         void __iomem *base = gic->dist_base;
268         u32 cpu = 0;
269         u32 nrppis = 0, ppi_base = 0;
270
271 #ifdef CONFIG_SMP
272         cpu = cpu_logical_map(smp_processor_id());
273 #endif
274
275         cpumask = 1 << cpu;
276         cpumask |= cpumask << 8;
277         cpumask |= cpumask << 16;
278
279         writel_relaxed(0, base + GIC_DIST_CTRL);
280
281         /*
282          * Find out how many interrupts are supported.
283          * The GIC only supports up to 1020 interrupt sources.
284          */
285         gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
286         gic_irqs = (gic_irqs + 1) * 32;
287         if (gic_irqs > 1020)
288                 gic_irqs = 1020;
289
290         gic->gic_irqs = gic_irqs;
291
292         /*
293          * Nobody would be insane enough to use PPIs on a secondary
294          * GIC, right?
295          */
296         if (gic == &gic_data[0]) {
297                 nrppis = (32 - irq_start) & 31;
298
299                 /* The GIC only supports up to 16 PPIs. */
300                 if (nrppis > 16)
301                         BUG();
302
303                 ppi_base = gic->irq_offset + 32 - nrppis;
304         }
305
306         pr_info("Configuring GIC with %d sources (%d PPIs)\n",
307                 gic_irqs, (gic == &gic_data[0]) ? nrppis : 0);
308
309         /*
310          * Set all global interrupts to be level triggered, active low.
311          */
312         for (i = 32; i < gic_irqs; i += 16)
313                 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
314
315         /*
316          * Set all global interrupts to this CPU only.
317          */
318         for (i = 32; i < gic_irqs; i += 4)
319                 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
320
321         /*
322          * Set priority on all global interrupts.
323          */
324         for (i = 32; i < gic_irqs; i += 4)
325                 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
326
327         /*
328          * Disable all interrupts.  Leave the PPI and SGIs alone
329          * as these enables are banked registers.
330          */
331         for (i = 32; i < gic_irqs; i += 32)
332                 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
333
334         /*
335          * Limit number of interrupts registered to the platform maximum
336          */
337         irq_limit = gic->irq_offset + gic_irqs;
338         if (WARN_ON(irq_limit > NR_IRQS))
339                 irq_limit = NR_IRQS;
340
341         /*
342          * Setup the Linux IRQ subsystem.
343          */
344         for (i = 0; i < nrppis; i++) {
345                 int ppi = i + ppi_base;
346
347                 irq_set_percpu_devid(ppi);
348                 irq_set_chip_and_handler(ppi, &gic_chip,
349                                          handle_percpu_devid_irq);
350                 irq_set_chip_data(ppi, gic);
351                 set_irq_flags(ppi, IRQF_VALID | IRQF_NOAUTOEN);
352         }
353
354         for (i = irq_start + nrppis; i < irq_limit; i++) {
355                 irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
356                 irq_set_chip_data(i, gic);
357                 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
358         }
359
360         writel_relaxed(1, base + GIC_DIST_CTRL);
361 }
362
363 static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
364 {
365         void __iomem *dist_base = gic->dist_base;
366         void __iomem *base = gic->cpu_base;
367         int i;
368
369         /*
370          * Deal with the banked PPI and SGI interrupts - disable all
371          * PPI interrupts, ensure all SGI interrupts are enabled.
372          */
373         writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
374         writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
375
376         /*
377          * Set priority on PPI and SGI interrupts
378          */
379         for (i = 0; i < 32; i += 4)
380                 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
381
382         writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
383         writel_relaxed(1, base + GIC_CPU_CTRL);
384 }
385
386 #ifdef CONFIG_CPU_PM
387 /*
388  * Saves the GIC distributor registers during suspend or idle.  Must be called
389  * with interrupts disabled but before powering down the GIC.  After calling
390  * this function, no interrupts will be delivered by the GIC, and another
391  * platform-specific wakeup source must be enabled.
392  */
393 static void gic_dist_save(unsigned int gic_nr)
394 {
395         unsigned int gic_irqs;
396         void __iomem *dist_base;
397         int i;
398
399         if (gic_nr >= MAX_GIC_NR)
400                 BUG();
401
402         gic_irqs = gic_data[gic_nr].gic_irqs;
403         dist_base = gic_data[gic_nr].dist_base;
404
405         if (!dist_base)
406                 return;
407
408         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
409                 gic_data[gic_nr].saved_spi_conf[i] =
410                         readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
411
412         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
413                 gic_data[gic_nr].saved_spi_target[i] =
414                         readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
415
416         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
417                 gic_data[gic_nr].saved_spi_enable[i] =
418                         readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
419 }
420
421 /*
422  * Restores the GIC distributor registers during resume or when coming out of
423  * idle.  Must be called before enabling interrupts.  If a level interrupt
424  * that occured while the GIC was suspended is still present, it will be
425  * handled normally, but any edge interrupts that occured will not be seen by
426  * the GIC and need to be handled by the platform-specific wakeup source.
427  */
428 static void gic_dist_restore(unsigned int gic_nr)
429 {
430         unsigned int gic_irqs;
431         unsigned int i;
432         void __iomem *dist_base;
433
434         if (gic_nr >= MAX_GIC_NR)
435                 BUG();
436
437         gic_irqs = gic_data[gic_nr].gic_irqs;
438         dist_base = gic_data[gic_nr].dist_base;
439
440         if (!dist_base)
441                 return;
442
443         writel_relaxed(0, dist_base + GIC_DIST_CTRL);
444
445         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
446                 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
447                         dist_base + GIC_DIST_CONFIG + i * 4);
448
449         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
450                 writel_relaxed(0xa0a0a0a0,
451                         dist_base + GIC_DIST_PRI + i * 4);
452
453         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
454                 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
455                         dist_base + GIC_DIST_TARGET + i * 4);
456
457         for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
458                 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
459                         dist_base + GIC_DIST_ENABLE_SET + i * 4);
460
461         writel_relaxed(1, dist_base + GIC_DIST_CTRL);
462 }
463
464 static void gic_cpu_save(unsigned int gic_nr)
465 {
466         int i;
467         u32 *ptr;
468         void __iomem *dist_base;
469         void __iomem *cpu_base;
470
471         if (gic_nr >= MAX_GIC_NR)
472                 BUG();
473
474         dist_base = gic_data[gic_nr].dist_base;
475         cpu_base = gic_data[gic_nr].cpu_base;
476
477         if (!dist_base || !cpu_base)
478                 return;
479
480         ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
481         for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
482                 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
483
484         ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
485         for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
486                 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
487
488 }
489
490 static void gic_cpu_restore(unsigned int gic_nr)
491 {
492         int i;
493         u32 *ptr;
494         void __iomem *dist_base;
495         void __iomem *cpu_base;
496
497         if (gic_nr >= MAX_GIC_NR)
498                 BUG();
499
500         dist_base = gic_data[gic_nr].dist_base;
501         cpu_base = gic_data[gic_nr].cpu_base;
502
503         if (!dist_base || !cpu_base)
504                 return;
505
506         ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
507         for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
508                 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
509
510         ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
511         for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
512                 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
513
514         for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
515                 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
516
517         writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
518         writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
519 }
520
521 static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
522 {
523         int i;
524
525         for (i = 0; i < MAX_GIC_NR; i++) {
526                 switch (cmd) {
527                 case CPU_PM_ENTER:
528                         gic_cpu_save(i);
529                         break;
530                 case CPU_PM_ENTER_FAILED:
531                 case CPU_PM_EXIT:
532                         gic_cpu_restore(i);
533                         break;
534                 case CPU_CLUSTER_PM_ENTER:
535                         gic_dist_save(i);
536                         break;
537                 case CPU_CLUSTER_PM_ENTER_FAILED:
538                 case CPU_CLUSTER_PM_EXIT:
539                         gic_dist_restore(i);
540                         break;
541                 }
542         }
543
544         return NOTIFY_OK;
545 }
546
547 static struct notifier_block gic_notifier_block = {
548         .notifier_call = gic_notifier,
549 };
550
551 static void __init gic_pm_init(struct gic_chip_data *gic)
552 {
553         gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
554                 sizeof(u32));
555         BUG_ON(!gic->saved_ppi_enable);
556
557         gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
558                 sizeof(u32));
559         BUG_ON(!gic->saved_ppi_conf);
560
561         cpu_pm_register_notifier(&gic_notifier_block);
562 }
563 #else
564 static void __init gic_pm_init(struct gic_chip_data *gic)
565 {
566 }
567 #endif
568
569 void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
570         void __iomem *dist_base, void __iomem *cpu_base)
571 {
572         struct gic_chip_data *gic;
573
574         BUG_ON(gic_nr >= MAX_GIC_NR);
575
576         gic = &gic_data[gic_nr];
577         gic->dist_base = dist_base;
578         gic->cpu_base = cpu_base;
579         gic->irq_offset = (irq_start - 1) & ~31;
580
581         if (gic_nr == 0)
582                 gic_cpu_base_addr = cpu_base;
583
584         gic_chip.flags |= gic_arch_extn.flags;
585         gic_dist_init(gic, irq_start);
586         gic_cpu_init(gic);
587         gic_pm_init(gic);
588 }
589
590 void __cpuinit gic_secondary_init(unsigned int gic_nr)
591 {
592         BUG_ON(gic_nr >= MAX_GIC_NR);
593
594         gic_cpu_init(&gic_data[gic_nr]);
595 }
596
597 #ifdef CONFIG_SMP
598 void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
599 {
600         int cpu;
601         unsigned long map = 0;
602
603         /* Convert our logical CPU mask into a physical one. */
604         for_each_cpu(cpu, mask)
605                 map |= 1 << cpu_logical_map(cpu);
606
607         /*
608          * Ensure that stores to Normal memory are visible to the
609          * other CPUs before issuing the IPI.
610          */
611         dsb();
612
613         /* this always happens on GIC0 */
614         writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
615 }
616 #endif