sh: intc: userimask support.
[linux-2.6-block.git] / drivers / sh / intc.c
1 /*
2  * Shared interrupt handling code for IPR and INTC2 types of IRQs.
3  *
4  * Copyright (C) 2007, 2008 Magnus Damm
5  * Copyright (C) 2009, 2010 Paul Mundt
6  *
7  * Based on intc2.c and ipr.c
8  *
9  * Copyright (C) 1999  Niibe Yutaka & Takeshi Yaegashi
10  * Copyright (C) 2000  Kazumoto Kojima
11  * Copyright (C) 2001  David J. Mckay (david.mckay@st.com)
12  * Copyright (C) 2003  Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
13  * Copyright (C) 2005, 2006  Paul Mundt
14  *
15  * This file is subject to the terms and conditions of the GNU General Public
16  * License.  See the file "COPYING" in the main directory of this archive
17  * for more details.
18  */
19 #include <linux/init.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/io.h>
23 #include <linux/interrupt.h>
24 #include <linux/sh_intc.h>
25 #include <linux/sysdev.h>
26 #include <linux/list.h>
27 #include <linux/topology.h>
28 #include <linux/bitmap.h>
29 #include <linux/cpumask.h>
30 #include <asm/sizes.h>
31
32 #define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
33         ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
34          ((addr_e) << 16) | ((addr_d << 24)))
35
36 #define _INTC_SHIFT(h) (h & 0x1f)
37 #define _INTC_WIDTH(h) ((h >> 5) & 0xf)
38 #define _INTC_FN(h) ((h >> 9) & 0xf)
39 #define _INTC_MODE(h) ((h >> 13) & 0x7)
40 #define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
41 #define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
42
43 struct intc_handle_int {
44         unsigned int irq;
45         unsigned long handle;
46 };
47
48 struct intc_window {
49         phys_addr_t phys;
50         void __iomem *virt;
51         unsigned long size;
52 };
53
54 struct intc_desc_int {
55         struct list_head list;
56         struct sys_device sysdev;
57         pm_message_t state;
58         unsigned long *reg;
59 #ifdef CONFIG_SMP
60         unsigned long *smp;
61 #endif
62         unsigned int nr_reg;
63         struct intc_handle_int *prio;
64         unsigned int nr_prio;
65         struct intc_handle_int *sense;
66         unsigned int nr_sense;
67         struct intc_window *window;
68         unsigned int nr_windows;
69         struct irq_chip chip;
70 };
71
72 static LIST_HEAD(intc_list);
73
74 /*
75  * The intc_irq_map provides a global map of bound IRQ vectors for a
76  * given platform. Allocation of IRQs are either static through the CPU
77  * vector map, or dynamic in the case of board mux vectors or MSI.
78  *
79  * As this is a central point for all IRQ controllers on the system,
80  * each of the available sources are mapped out here. This combined with
81  * sparseirq makes it quite trivial to keep the vector map tightly packed
82  * when dynamically creating IRQs, as well as tying in to otherwise
83  * unused irq_desc positions in the sparse array.
84  */
85 static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
86 static DEFINE_SPINLOCK(vector_lock);
87
88 #ifdef CONFIG_SMP
89 #define IS_SMP(x) x.smp
90 #define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
91 #define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
92 #else
93 #define IS_SMP(x) 0
94 #define INTC_REG(d, x, c) (d->reg[(x)])
95 #define SMP_NR(d, x) 1
96 #endif
97
98 static unsigned int intc_prio_level[NR_IRQS];   /* for now */
99 static unsigned int default_prio_level = 2;     /* 2 - 16 */
100 static unsigned long ack_handle[NR_IRQS];
101
102 static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
103 {
104         struct irq_chip *chip = get_irq_chip(irq);
105         return container_of(chip, struct intc_desc_int, chip);
106 }
107
108 static inline unsigned int set_field(unsigned int value,
109                                      unsigned int field_value,
110                                      unsigned int handle)
111 {
112         unsigned int width = _INTC_WIDTH(handle);
113         unsigned int shift = _INTC_SHIFT(handle);
114
115         value &= ~(((1 << width) - 1) << shift);
116         value |= field_value << shift;
117         return value;
118 }
119
120 static void write_8(unsigned long addr, unsigned long h, unsigned long data)
121 {
122         __raw_writeb(set_field(0, data, h), addr);
123         (void)__raw_readb(addr);        /* Defeat write posting */
124 }
125
126 static void write_16(unsigned long addr, unsigned long h, unsigned long data)
127 {
128         __raw_writew(set_field(0, data, h), addr);
129         (void)__raw_readw(addr);        /* Defeat write posting */
130 }
131
132 static void write_32(unsigned long addr, unsigned long h, unsigned long data)
133 {
134         __raw_writel(set_field(0, data, h), addr);
135         (void)__raw_readl(addr);        /* Defeat write posting */
136 }
137
138 static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
139 {
140         unsigned long flags;
141         local_irq_save(flags);
142         __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
143         (void)__raw_readb(addr);        /* Defeat write posting */
144         local_irq_restore(flags);
145 }
146
147 static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
148 {
149         unsigned long flags;
150         local_irq_save(flags);
151         __raw_writew(set_field(__raw_readw(addr), data, h), addr);
152         (void)__raw_readw(addr);        /* Defeat write posting */
153         local_irq_restore(flags);
154 }
155
156 static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
157 {
158         unsigned long flags;
159         local_irq_save(flags);
160         __raw_writel(set_field(__raw_readl(addr), data, h), addr);
161         (void)__raw_readl(addr);        /* Defeat write posting */
162         local_irq_restore(flags);
163 }
164
165 enum {  REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
166
167 static void (*intc_reg_fns[])(unsigned long addr,
168                               unsigned long h,
169                               unsigned long data) = {
170         [REG_FN_WRITE_BASE + 0] = write_8,
171         [REG_FN_WRITE_BASE + 1] = write_16,
172         [REG_FN_WRITE_BASE + 3] = write_32,
173         [REG_FN_MODIFY_BASE + 0] = modify_8,
174         [REG_FN_MODIFY_BASE + 1] = modify_16,
175         [REG_FN_MODIFY_BASE + 3] = modify_32,
176 };
177
178 enum {  MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
179         MODE_MASK_REG,       /* Bit(s) set -> interrupt disabled */
180         MODE_DUAL_REG,       /* Two registers, set bit to enable / disable */
181         MODE_PRIO_REG,       /* Priority value written to enable interrupt */
182         MODE_PCLR_REG,       /* Above plus all bits set to disable interrupt */
183 };
184
185 static void intc_mode_field(unsigned long addr,
186                             unsigned long handle,
187                             void (*fn)(unsigned long,
188                                        unsigned long,
189                                        unsigned long),
190                             unsigned int irq)
191 {
192         fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
193 }
194
195 static void intc_mode_zero(unsigned long addr,
196                            unsigned long handle,
197                            void (*fn)(unsigned long,
198                                        unsigned long,
199                                        unsigned long),
200                            unsigned int irq)
201 {
202         fn(addr, handle, 0);
203 }
204
205 static void intc_mode_prio(unsigned long addr,
206                            unsigned long handle,
207                            void (*fn)(unsigned long,
208                                        unsigned long,
209                                        unsigned long),
210                            unsigned int irq)
211 {
212         fn(addr, handle, intc_prio_level[irq]);
213 }
214
215 static void (*intc_enable_fns[])(unsigned long addr,
216                                  unsigned long handle,
217                                  void (*fn)(unsigned long,
218                                             unsigned long,
219                                             unsigned long),
220                                  unsigned int irq) = {
221         [MODE_ENABLE_REG] = intc_mode_field,
222         [MODE_MASK_REG] = intc_mode_zero,
223         [MODE_DUAL_REG] = intc_mode_field,
224         [MODE_PRIO_REG] = intc_mode_prio,
225         [MODE_PCLR_REG] = intc_mode_prio,
226 };
227
228 static void (*intc_disable_fns[])(unsigned long addr,
229                                   unsigned long handle,
230                                   void (*fn)(unsigned long,
231                                              unsigned long,
232                                              unsigned long),
233                                   unsigned int irq) = {
234         [MODE_ENABLE_REG] = intc_mode_zero,
235         [MODE_MASK_REG] = intc_mode_field,
236         [MODE_DUAL_REG] = intc_mode_field,
237         [MODE_PRIO_REG] = intc_mode_zero,
238         [MODE_PCLR_REG] = intc_mode_field,
239 };
240
241 static inline void _intc_enable(unsigned int irq, unsigned long handle)
242 {
243         struct intc_desc_int *d = get_intc_desc(irq);
244         unsigned long addr;
245         unsigned int cpu;
246
247         for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
248 #ifdef CONFIG_SMP
249                 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
250                         continue;
251 #endif
252                 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
253                 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
254                                                     [_INTC_FN(handle)], irq);
255         }
256 }
257
258 static void intc_enable(unsigned int irq)
259 {
260         _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
261 }
262
263 static void intc_disable(unsigned int irq)
264 {
265         struct intc_desc_int *d = get_intc_desc(irq);
266         unsigned long handle = (unsigned long) get_irq_chip_data(irq);
267         unsigned long addr;
268         unsigned int cpu;
269
270         for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
271 #ifdef CONFIG_SMP
272                 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
273                         continue;
274 #endif
275                 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
276                 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
277                                                      [_INTC_FN(handle)], irq);
278         }
279 }
280
281 static void (*intc_enable_noprio_fns[])(unsigned long addr,
282                                         unsigned long handle,
283                                         void (*fn)(unsigned long,
284                                                    unsigned long,
285                                                    unsigned long),
286                                         unsigned int irq) = {
287         [MODE_ENABLE_REG] = intc_mode_field,
288         [MODE_MASK_REG] = intc_mode_zero,
289         [MODE_DUAL_REG] = intc_mode_field,
290         [MODE_PRIO_REG] = intc_mode_field,
291         [MODE_PCLR_REG] = intc_mode_field,
292 };
293
294 static void intc_enable_disable(struct intc_desc_int *d,
295                                 unsigned long handle, int do_enable)
296 {
297         unsigned long addr;
298         unsigned int cpu;
299         void (*fn)(unsigned long, unsigned long,
300                    void (*)(unsigned long, unsigned long, unsigned long),
301                    unsigned int);
302
303         if (do_enable) {
304                 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
305                         addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
306                         fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
307                         fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
308                 }
309         } else {
310                 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
311                         addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
312                         fn = intc_disable_fns[_INTC_MODE(handle)];
313                         fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
314                 }
315         }
316 }
317
318 static int intc_set_wake(unsigned int irq, unsigned int on)
319 {
320         return 0; /* allow wakeup, but setup hardware in intc_suspend() */
321 }
322
323 #ifdef CONFIG_SMP
324 /*
325  * This is held with the irq desc lock held, so we don't require any
326  * additional locking here at the intc desc level. The affinity mask is
327  * later tested in the enable/disable paths.
328  */
329 static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
330 {
331         if (!cpumask_intersects(cpumask, cpu_online_mask))
332                 return -1;
333
334         cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
335
336         return 0;
337 }
338 #endif
339
340 static void intc_mask_ack(unsigned int irq)
341 {
342         struct intc_desc_int *d = get_intc_desc(irq);
343         unsigned long handle = ack_handle[irq];
344         unsigned long addr;
345
346         intc_disable(irq);
347
348         /* read register and write zero only to the assocaited bit */
349
350         if (handle) {
351                 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
352                 switch (_INTC_FN(handle)) {
353                 case REG_FN_MODIFY_BASE + 0:    /* 8bit */
354                         __raw_readb(addr);
355                         __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
356                         break;
357                 case REG_FN_MODIFY_BASE + 1:    /* 16bit */
358                         __raw_readw(addr);
359                         __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
360                         break;
361                 case REG_FN_MODIFY_BASE + 3:    /* 32bit */
362                         __raw_readl(addr);
363                         __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
364                         break;
365                 default:
366                         BUG();
367                         break;
368                 }
369         }
370 }
371
372 static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
373                                              unsigned int nr_hp,
374                                              unsigned int irq)
375 {
376         int i;
377
378         /* this doesn't scale well, but...
379          *
380          * this function should only be used for cerain uncommon
381          * operations such as intc_set_priority() and intc_set_sense()
382          * and in those rare cases performance doesn't matter that much.
383          * keeping the memory footprint low is more important.
384          *
385          * one rather simple way to speed this up and still keep the
386          * memory footprint down is to make sure the array is sorted
387          * and then perform a bisect to lookup the irq.
388          */
389
390         for (i = 0; i < nr_hp; i++) {
391                 if ((hp + i)->irq != irq)
392                         continue;
393
394                 return hp + i;
395         }
396
397         return NULL;
398 }
399
400 int intc_set_priority(unsigned int irq, unsigned int prio)
401 {
402         struct intc_desc_int *d = get_intc_desc(irq);
403         struct intc_handle_int *ihp;
404
405         if (!intc_prio_level[irq] || prio <= 1)
406                 return -EINVAL;
407
408         ihp = intc_find_irq(d->prio, d->nr_prio, irq);
409         if (ihp) {
410                 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
411                         return -EINVAL;
412
413                 intc_prio_level[irq] = prio;
414
415                 /*
416                  * only set secondary masking method directly
417                  * primary masking method is using intc_prio_level[irq]
418                  * priority level will be set during next enable()
419                  */
420
421                 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
422                         _intc_enable(irq, ihp->handle);
423         }
424         return 0;
425 }
426
427 #define VALID(x) (x | 0x80)
428
429 static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
430         [IRQ_TYPE_EDGE_FALLING] = VALID(0),
431         [IRQ_TYPE_EDGE_RISING] = VALID(1),
432         [IRQ_TYPE_LEVEL_LOW] = VALID(2),
433         /* SH7706, SH7707 and SH7709 do not support high level triggered */
434 #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
435     !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
436     !defined(CONFIG_CPU_SUBTYPE_SH7709)
437         [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
438 #endif
439 };
440
441 static int intc_set_sense(unsigned int irq, unsigned int type)
442 {
443         struct intc_desc_int *d = get_intc_desc(irq);
444         unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
445         struct intc_handle_int *ihp;
446         unsigned long addr;
447
448         if (!value)
449                 return -EINVAL;
450
451         ihp = intc_find_irq(d->sense, d->nr_sense, irq);
452         if (ihp) {
453                 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
454                 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
455         }
456         return 0;
457 }
458
459 static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
460                                        unsigned long address)
461 {
462         struct intc_window *window;
463         int k;
464
465         /* scan through physical windows and convert address */
466         for (k = 0; k < d->nr_windows; k++) {
467                 window = d->window + k;
468
469                 if (address < window->phys)
470                         continue;
471
472                 if (address >= (window->phys + window->size))
473                         continue;
474
475                 address -= window->phys;
476                 address += (unsigned long)window->virt;
477
478                 return address;
479         }
480
481         /* no windows defined, register must be 1:1 mapped virt:phys */
482         return address;
483 }
484
485 static unsigned int __init intc_get_reg(struct intc_desc_int *d,
486                                         unsigned long address)
487 {
488         unsigned int k;
489
490         address = intc_phys_to_virt(d, address);
491
492         for (k = 0; k < d->nr_reg; k++) {
493                 if (d->reg[k] == address)
494                         return k;
495         }
496
497         BUG();
498         return 0;
499 }
500
501 static intc_enum __init intc_grp_id(struct intc_desc *desc,
502                                     intc_enum enum_id)
503 {
504         struct intc_group *g = desc->hw.groups;
505         unsigned int i, j;
506
507         for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
508                 g = desc->hw.groups + i;
509
510                 for (j = 0; g->enum_ids[j]; j++) {
511                         if (g->enum_ids[j] != enum_id)
512                                 continue;
513
514                         return g->enum_id;
515                 }
516         }
517
518         return 0;
519 }
520
521 static unsigned int __init _intc_mask_data(struct intc_desc *desc,
522                                            struct intc_desc_int *d,
523                                            intc_enum enum_id,
524                                            unsigned int *reg_idx,
525                                            unsigned int *fld_idx)
526 {
527         struct intc_mask_reg *mr = desc->hw.mask_regs;
528         unsigned int fn, mode;
529         unsigned long reg_e, reg_d;
530
531         while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
532                 mr = desc->hw.mask_regs + *reg_idx;
533
534                 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
535                         if (mr->enum_ids[*fld_idx] != enum_id)
536                                 continue;
537
538                         if (mr->set_reg && mr->clr_reg) {
539                                 fn = REG_FN_WRITE_BASE;
540                                 mode = MODE_DUAL_REG;
541                                 reg_e = mr->clr_reg;
542                                 reg_d = mr->set_reg;
543                         } else {
544                                 fn = REG_FN_MODIFY_BASE;
545                                 if (mr->set_reg) {
546                                         mode = MODE_ENABLE_REG;
547                                         reg_e = mr->set_reg;
548                                         reg_d = mr->set_reg;
549                                 } else {
550                                         mode = MODE_MASK_REG;
551                                         reg_e = mr->clr_reg;
552                                         reg_d = mr->clr_reg;
553                                 }
554                         }
555
556                         fn += (mr->reg_width >> 3) - 1;
557                         return _INTC_MK(fn, mode,
558                                         intc_get_reg(d, reg_e),
559                                         intc_get_reg(d, reg_d),
560                                         1,
561                                         (mr->reg_width - 1) - *fld_idx);
562                 }
563
564                 *fld_idx = 0;
565                 (*reg_idx)++;
566         }
567
568         return 0;
569 }
570
571 static unsigned int __init intc_mask_data(struct intc_desc *desc,
572                                           struct intc_desc_int *d,
573                                           intc_enum enum_id, int do_grps)
574 {
575         unsigned int i = 0;
576         unsigned int j = 0;
577         unsigned int ret;
578
579         ret = _intc_mask_data(desc, d, enum_id, &i, &j);
580         if (ret)
581                 return ret;
582
583         if (do_grps)
584                 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
585
586         return 0;
587 }
588
589 static unsigned int __init _intc_prio_data(struct intc_desc *desc,
590                                            struct intc_desc_int *d,
591                                            intc_enum enum_id,
592                                            unsigned int *reg_idx,
593                                            unsigned int *fld_idx)
594 {
595         struct intc_prio_reg *pr = desc->hw.prio_regs;
596         unsigned int fn, n, mode, bit;
597         unsigned long reg_e, reg_d;
598
599         while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
600                 pr = desc->hw.prio_regs + *reg_idx;
601
602                 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
603                         if (pr->enum_ids[*fld_idx] != enum_id)
604                                 continue;
605
606                         if (pr->set_reg && pr->clr_reg) {
607                                 fn = REG_FN_WRITE_BASE;
608                                 mode = MODE_PCLR_REG;
609                                 reg_e = pr->set_reg;
610                                 reg_d = pr->clr_reg;
611                         } else {
612                                 fn = REG_FN_MODIFY_BASE;
613                                 mode = MODE_PRIO_REG;
614                                 if (!pr->set_reg)
615                                         BUG();
616                                 reg_e = pr->set_reg;
617                                 reg_d = pr->set_reg;
618                         }
619
620                         fn += (pr->reg_width >> 3) - 1;
621                         n = *fld_idx + 1;
622
623                         BUG_ON(n * pr->field_width > pr->reg_width);
624
625                         bit = pr->reg_width - (n * pr->field_width);
626
627                         return _INTC_MK(fn, mode,
628                                         intc_get_reg(d, reg_e),
629                                         intc_get_reg(d, reg_d),
630                                         pr->field_width, bit);
631                 }
632
633                 *fld_idx = 0;
634                 (*reg_idx)++;
635         }
636
637         return 0;
638 }
639
640 static unsigned int __init intc_prio_data(struct intc_desc *desc,
641                                           struct intc_desc_int *d,
642                                           intc_enum enum_id, int do_grps)
643 {
644         unsigned int i = 0;
645         unsigned int j = 0;
646         unsigned int ret;
647
648         ret = _intc_prio_data(desc, d, enum_id, &i, &j);
649         if (ret)
650                 return ret;
651
652         if (do_grps)
653                 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
654
655         return 0;
656 }
657
658 static void __init intc_enable_disable_enum(struct intc_desc *desc,
659                                             struct intc_desc_int *d,
660                                             intc_enum enum_id, int enable)
661 {
662         unsigned int i, j, data;
663
664         /* go through and enable/disable all mask bits */
665         i = j = 0;
666         do {
667                 data = _intc_mask_data(desc, d, enum_id, &i, &j);
668                 if (data)
669                         intc_enable_disable(d, data, enable);
670                 j++;
671         } while (data);
672
673         /* go through and enable/disable all priority fields */
674         i = j = 0;
675         do {
676                 data = _intc_prio_data(desc, d, enum_id, &i, &j);
677                 if (data)
678                         intc_enable_disable(d, data, enable);
679
680                 j++;
681         } while (data);
682 }
683
684 static unsigned int __init intc_ack_data(struct intc_desc *desc,
685                                           struct intc_desc_int *d,
686                                           intc_enum enum_id)
687 {
688         struct intc_mask_reg *mr = desc->hw.ack_regs;
689         unsigned int i, j, fn, mode;
690         unsigned long reg_e, reg_d;
691
692         for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
693                 mr = desc->hw.ack_regs + i;
694
695                 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
696                         if (mr->enum_ids[j] != enum_id)
697                                 continue;
698
699                         fn = REG_FN_MODIFY_BASE;
700                         mode = MODE_ENABLE_REG;
701                         reg_e = mr->set_reg;
702                         reg_d = mr->set_reg;
703
704                         fn += (mr->reg_width >> 3) - 1;
705                         return _INTC_MK(fn, mode,
706                                         intc_get_reg(d, reg_e),
707                                         intc_get_reg(d, reg_d),
708                                         1,
709                                         (mr->reg_width - 1) - j);
710                 }
711         }
712
713         return 0;
714 }
715
716 static unsigned int __init intc_sense_data(struct intc_desc *desc,
717                                            struct intc_desc_int *d,
718                                            intc_enum enum_id)
719 {
720         struct intc_sense_reg *sr = desc->hw.sense_regs;
721         unsigned int i, j, fn, bit;
722
723         for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
724                 sr = desc->hw.sense_regs + i;
725
726                 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
727                         if (sr->enum_ids[j] != enum_id)
728                                 continue;
729
730                         fn = REG_FN_MODIFY_BASE;
731                         fn += (sr->reg_width >> 3) - 1;
732
733                         BUG_ON((j + 1) * sr->field_width > sr->reg_width);
734
735                         bit = sr->reg_width - ((j + 1) * sr->field_width);
736
737                         return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
738                                         0, sr->field_width, bit);
739                 }
740         }
741
742         return 0;
743 }
744
745 static void __init intc_register_irq(struct intc_desc *desc,
746                                      struct intc_desc_int *d,
747                                      intc_enum enum_id,
748                                      unsigned int irq)
749 {
750         struct intc_handle_int *hp;
751         unsigned int data[2], primary;
752
753         /*
754          * Register the IRQ position with the global IRQ map
755          */
756         set_bit(irq, intc_irq_map);
757
758         /* Prefer single interrupt source bitmap over other combinations:
759          * 1. bitmap, single interrupt source
760          * 2. priority, single interrupt source
761          * 3. bitmap, multiple interrupt sources (groups)
762          * 4. priority, multiple interrupt sources (groups)
763          */
764
765         data[0] = intc_mask_data(desc, d, enum_id, 0);
766         data[1] = intc_prio_data(desc, d, enum_id, 0);
767
768         primary = 0;
769         if (!data[0] && data[1])
770                 primary = 1;
771
772         if (!data[0] && !data[1])
773                 pr_warning("intc: missing unique irq mask for "
774                            "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
775
776         data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
777         data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
778
779         if (!data[primary])
780                 primary ^= 1;
781
782         BUG_ON(!data[primary]); /* must have primary masking method */
783
784         disable_irq_nosync(irq);
785         set_irq_chip_and_handler_name(irq, &d->chip,
786                                       handle_level_irq, "level");
787         set_irq_chip_data(irq, (void *)data[primary]);
788
789         /* set priority level
790          * - this needs to be at least 2 for 5-bit priorities on 7780
791          */
792         intc_prio_level[irq] = default_prio_level;
793
794         /* enable secondary masking method if present */
795         if (data[!primary])
796                 _intc_enable(irq, data[!primary]);
797
798         /* add irq to d->prio list if priority is available */
799         if (data[1]) {
800                 hp = d->prio + d->nr_prio;
801                 hp->irq = irq;
802                 hp->handle = data[1];
803
804                 if (primary) {
805                         /*
806                          * only secondary priority should access registers, so
807                          * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
808                          */
809
810                         hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
811                         hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
812                 }
813                 d->nr_prio++;
814         }
815
816         /* add irq to d->sense list if sense is available */
817         data[0] = intc_sense_data(desc, d, enum_id);
818         if (data[0]) {
819                 (d->sense + d->nr_sense)->irq = irq;
820                 (d->sense + d->nr_sense)->handle = data[0];
821                 d->nr_sense++;
822         }
823
824         /* irq should be disabled by default */
825         d->chip.mask(irq);
826
827         if (desc->hw.ack_regs)
828                 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
829
830 #ifdef CONFIG_ARM
831         set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
832 #endif
833 }
834
835 static unsigned int __init save_reg(struct intc_desc_int *d,
836                                     unsigned int cnt,
837                                     unsigned long value,
838                                     unsigned int smp)
839 {
840         if (value) {
841                 value = intc_phys_to_virt(d, value);
842
843                 d->reg[cnt] = value;
844 #ifdef CONFIG_SMP
845                 d->smp[cnt] = smp;
846 #endif
847                 return 1;
848         }
849
850         return 0;
851 }
852
853 static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
854 {
855         generic_handle_irq((unsigned int)get_irq_data(irq));
856 }
857
858 int __init register_intc_controller(struct intc_desc *desc)
859 {
860         unsigned int i, k, smp;
861         struct intc_hw_desc *hw = &desc->hw;
862         struct intc_desc_int *d;
863         struct resource *res;
864
865         pr_info("intc: Registered controller '%s' with %u IRQs\n",
866                 desc->name, hw->nr_vectors);
867
868         d = kzalloc(sizeof(*d), GFP_NOWAIT);
869         if (!d)
870                 goto err0;
871
872         INIT_LIST_HEAD(&d->list);
873         list_add(&d->list, &intc_list);
874
875         if (desc->num_resources) {
876                 d->nr_windows = desc->num_resources;
877                 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
878                                     GFP_NOWAIT);
879                 if (!d->window)
880                         goto err1;
881
882                 for (k = 0; k < d->nr_windows; k++) {
883                         res = desc->resource + k;
884                         WARN_ON(resource_type(res) != IORESOURCE_MEM);
885                         d->window[k].phys = res->start;
886                         d->window[k].size = resource_size(res);
887                         d->window[k].virt = ioremap_nocache(res->start,
888                                                          resource_size(res));
889                         if (!d->window[k].virt)
890                                 goto err2;
891                 }
892         }
893
894         d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
895         d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
896         d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
897         d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
898
899         d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
900         if (!d->reg)
901                 goto err2;
902
903 #ifdef CONFIG_SMP
904         d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
905         if (!d->smp)
906                 goto err3;
907 #endif
908         k = 0;
909
910         if (hw->mask_regs) {
911                 for (i = 0; i < hw->nr_mask_regs; i++) {
912                         smp = IS_SMP(hw->mask_regs[i]);
913                         k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
914                         k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
915                 }
916         }
917
918         if (hw->prio_regs) {
919                 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
920                                   GFP_NOWAIT);
921                 if (!d->prio)
922                         goto err4;
923
924                 for (i = 0; i < hw->nr_prio_regs; i++) {
925                         smp = IS_SMP(hw->prio_regs[i]);
926                         k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
927                         k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
928                 }
929         }
930
931         if (hw->sense_regs) {
932                 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
933                                    GFP_NOWAIT);
934                 if (!d->sense)
935                         goto err5;
936
937                 for (i = 0; i < hw->nr_sense_regs; i++)
938                         k += save_reg(d, k, hw->sense_regs[i].reg, 0);
939         }
940
941         d->chip.name = desc->name;
942         d->chip.mask = intc_disable;
943         d->chip.unmask = intc_enable;
944         d->chip.mask_ack = intc_disable;
945         d->chip.enable = intc_enable;
946         d->chip.disable = intc_disable;
947         d->chip.shutdown = intc_disable;
948         d->chip.set_type = intc_set_sense;
949         d->chip.set_wake = intc_set_wake;
950 #ifdef CONFIG_SMP
951         d->chip.set_affinity = intc_set_affinity;
952 #endif
953
954         if (hw->ack_regs) {
955                 for (i = 0; i < hw->nr_ack_regs; i++)
956                         k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
957
958                 d->chip.mask_ack = intc_mask_ack;
959         }
960
961         /* disable bits matching force_disable before registering irqs */
962         if (desc->force_disable)
963                 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
964
965         /* disable bits matching force_enable before registering irqs */
966         if (desc->force_enable)
967                 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
968
969         BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
970
971         /* register the vectors one by one */
972         for (i = 0; i < hw->nr_vectors; i++) {
973                 struct intc_vect *vect = hw->vectors + i;
974                 unsigned int irq = evt2irq(vect->vect);
975                 struct irq_desc *irq_desc;
976
977                 if (!vect->enum_id)
978                         continue;
979
980                 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
981                 if (unlikely(!irq_desc)) {
982                         pr_err("can't get irq_desc for %d\n", irq);
983                         continue;
984                 }
985
986                 intc_register_irq(desc, d, vect->enum_id, irq);
987
988                 for (k = i + 1; k < hw->nr_vectors; k++) {
989                         struct intc_vect *vect2 = hw->vectors + k;
990                         unsigned int irq2 = evt2irq(vect2->vect);
991
992                         if (vect->enum_id != vect2->enum_id)
993                                 continue;
994
995                         /*
996                          * In the case of multi-evt handling and sparse
997                          * IRQ support, each vector still needs to have
998                          * its own backing irq_desc.
999                          */
1000                         irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
1001                         if (unlikely(!irq_desc)) {
1002                                 pr_err("can't get irq_desc for %d\n", irq2);
1003                                 continue;
1004                         }
1005
1006                         vect2->enum_id = 0;
1007
1008                         /* redirect this interrupts to the first one */
1009                         set_irq_chip(irq2, &dummy_irq_chip);
1010                         set_irq_chained_handler(irq2, intc_redirect_irq);
1011                         set_irq_data(irq2, (void *)irq);
1012                 }
1013         }
1014
1015         /* enable bits matching force_enable after registering irqs */
1016         if (desc->force_enable)
1017                 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
1018
1019         return 0;
1020 err5:
1021         kfree(d->prio);
1022 err4:
1023 #ifdef CONFIG_SMP
1024         kfree(d->smp);
1025 err3:
1026 #endif
1027         kfree(d->reg);
1028 err2:
1029         for (k = 0; k < d->nr_windows; k++)
1030                 if (d->window[k].virt)
1031                         iounmap(d->window[k].virt);
1032
1033         kfree(d->window);
1034 err1:
1035         kfree(d);
1036 err0:
1037         pr_err("unable to allocate INTC memory\n");
1038
1039         return -ENOMEM;
1040 }
1041
1042 #ifdef CONFIG_INTC_USERIMASK
1043 static void __iomem *uimask;
1044
1045 int register_intc_userimask(unsigned long addr)
1046 {
1047         if (unlikely(uimask))
1048                 return -EBUSY;
1049
1050         uimask = ioremap_nocache(addr, SZ_4K);
1051         if (unlikely(!uimask))
1052                 return -ENOMEM;
1053
1054         pr_info("intc: userimask support registered for levels 0 -> %d\n",
1055                 default_prio_level - 1);
1056
1057         return 0;
1058 }
1059
1060 static ssize_t
1061 show_intc_userimask(struct sysdev_class *cls,
1062                     struct sysdev_class_attribute *attr, char *buf)
1063 {
1064         return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
1065 }
1066
1067 static ssize_t
1068 store_intc_userimask(struct sysdev_class *cls,
1069                      struct sysdev_class_attribute *attr,
1070                      const char *buf, size_t count)
1071 {
1072         unsigned long level;
1073
1074         level = simple_strtoul(buf, NULL, 10);
1075
1076         /*
1077          * Minimal acceptable IRQ levels are in the 2 - 16 range, but
1078          * these are chomped so as to not interfere with normal IRQs.
1079          *
1080          * Level 1 is a special case on some CPUs in that it's not
1081          * directly settable, but given that USERIMASK cuts off below a
1082          * certain level, we don't care about this limitation here.
1083          * Level 0 on the other hand equates to user masking disabled.
1084          *
1085          * We use default_prio_level as a cut off so that only special
1086          * case opt-in IRQs can be mangled.
1087          */
1088         if (level >= default_prio_level)
1089                 return -EINVAL;
1090
1091         __raw_writel(0xa5 << 24 | level << 4, uimask);
1092
1093         return count;
1094 }
1095
1096 static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
1097                          show_intc_userimask, store_intc_userimask);
1098 #endif
1099
1100 static ssize_t
1101 show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1102 {
1103         struct intc_desc_int *d;
1104
1105         d = container_of(dev, struct intc_desc_int, sysdev);
1106
1107         return sprintf(buf, "%s\n", d->chip.name);
1108 }
1109
1110 static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1111
1112 static int intc_suspend(struct sys_device *dev, pm_message_t state)
1113 {
1114         struct intc_desc_int *d;
1115         struct irq_desc *desc;
1116         int irq;
1117
1118         /* get intc controller associated with this sysdev */
1119         d = container_of(dev, struct intc_desc_int, sysdev);
1120
1121         switch (state.event) {
1122         case PM_EVENT_ON:
1123                 if (d->state.event != PM_EVENT_FREEZE)
1124                         break;
1125                 for_each_irq_desc(irq, desc) {
1126                         if (desc->handle_irq == intc_redirect_irq)
1127                                 continue;
1128                         if (desc->chip != &d->chip)
1129                                 continue;
1130                         if (desc->status & IRQ_DISABLED)
1131                                 intc_disable(irq);
1132                         else
1133                                 intc_enable(irq);
1134                 }
1135                 break;
1136         case PM_EVENT_FREEZE:
1137                 /* nothing has to be done */
1138                 break;
1139         case PM_EVENT_SUSPEND:
1140                 /* enable wakeup irqs belonging to this intc controller */
1141                 for_each_irq_desc(irq, desc) {
1142                         if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1143                                 intc_enable(irq);
1144                 }
1145                 break;
1146         }
1147         d->state = state;
1148
1149         return 0;
1150 }
1151
1152 static int intc_resume(struct sys_device *dev)
1153 {
1154         return intc_suspend(dev, PMSG_ON);
1155 }
1156
1157 static struct sysdev_class intc_sysdev_class = {
1158         .name = "intc",
1159         .suspend = intc_suspend,
1160         .resume = intc_resume,
1161 };
1162
1163 /* register this intc as sysdev to allow suspend/resume */
1164 static int __init register_intc_sysdevs(void)
1165 {
1166         struct intc_desc_int *d;
1167         int error;
1168         int id = 0;
1169
1170         error = sysdev_class_register(&intc_sysdev_class);
1171 #ifdef CONFIG_INTC_USERIMASK
1172         if (!error && uimask)
1173                 error = sysdev_class_create_file(&intc_sysdev_class,
1174                                                  &attr_userimask);
1175 #endif
1176         if (!error) {
1177                 list_for_each_entry(d, &intc_list, list) {
1178                         d->sysdev.id = id;
1179                         d->sysdev.cls = &intc_sysdev_class;
1180                         error = sysdev_register(&d->sysdev);
1181                         if (error == 0)
1182                                 error = sysdev_create_file(&d->sysdev,
1183                                                            &attr_name);
1184                         if (error)
1185                                 break;
1186
1187                         id++;
1188                 }
1189         }
1190
1191         if (error)
1192                 pr_err("intc: sysdev registration error\n");
1193
1194         return error;
1195 }
1196 device_initcall(register_intc_sysdevs);
1197
1198 /*
1199  * Dynamic IRQ allocation and deallocation
1200  */
1201 unsigned int create_irq_nr(unsigned int irq_want, int node)
1202 {
1203         unsigned int irq = 0, new;
1204         unsigned long flags;
1205         struct irq_desc *desc;
1206
1207         spin_lock_irqsave(&vector_lock, flags);
1208
1209         /*
1210          * First try the wanted IRQ
1211          */
1212         if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1213                 new = irq_want;
1214         } else {
1215                 /* .. then fall back to scanning. */
1216                 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1217                 if (unlikely(new == nr_irqs))
1218                         goto out_unlock;
1219
1220                 __set_bit(new, intc_irq_map);
1221         }
1222
1223         desc = irq_to_desc_alloc_node(new, node);
1224         if (unlikely(!desc)) {
1225                 pr_err("can't get irq_desc for %d\n", new);
1226                 goto out_unlock;
1227         }
1228
1229         desc = move_irq_desc(desc, node);
1230         irq = new;
1231
1232 out_unlock:
1233         spin_unlock_irqrestore(&vector_lock, flags);
1234
1235         if (irq > 0) {
1236                 dynamic_irq_init(irq);
1237 #ifdef CONFIG_ARM
1238                 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1239 #endif
1240         }
1241
1242         return irq;
1243 }
1244
1245 int create_irq(void)
1246 {
1247         int nid = cpu_to_node(smp_processor_id());
1248         int irq;
1249
1250         irq = create_irq_nr(NR_IRQS_LEGACY, nid);
1251         if (irq == 0)
1252                 irq = -1;
1253
1254         return irq;
1255 }
1256
1257 void destroy_irq(unsigned int irq)
1258 {
1259         unsigned long flags;
1260
1261         dynamic_irq_cleanup(irq);
1262
1263         spin_lock_irqsave(&vector_lock, flags);
1264         __clear_bit(irq, intc_irq_map);
1265         spin_unlock_irqrestore(&vector_lock, flags);
1266 }
1267
1268 int reserve_irq_vector(unsigned int irq)
1269 {
1270         unsigned long flags;
1271         int ret = 0;
1272
1273         spin_lock_irqsave(&vector_lock, flags);
1274         if (test_and_set_bit(irq, intc_irq_map))
1275                 ret = -EBUSY;
1276         spin_unlock_irqrestore(&vector_lock, flags);
1277
1278         return ret;
1279 }
1280
1281 void reserve_irq_legacy(void)
1282 {
1283         unsigned long flags;
1284         int i, j;
1285
1286         spin_lock_irqsave(&vector_lock, flags);
1287         j = find_first_bit(intc_irq_map, nr_irqs);
1288         for (i = 0; i < j; i++)
1289                 __set_bit(i, intc_irq_map);
1290         spin_unlock_irqrestore(&vector_lock, flags);
1291 }