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