2 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
4 * Copyright (C) 2007, 2008 Magnus Damm
5 * Copyright (C) 2009, 2010 Paul Mundt
7 * Based on intc2.c and ipr.c
9 * Copyright (C) 1999 Niibe Yutaka & Takeshi Yaegashi
10 * Copyright (C) 2000 Kazumoto Kojima
13 * Copyright (C) 2005, 2006 Paul Mundt
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/irq.h>
23 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/sh_intc.h>
28 #include <linux/sysdev.h>
29 #include <linux/list.h>
30 #include <linux/topology.h>
31 #include <linux/bitmap.h>
32 #include <linux/cpumask.h>
33 #include <asm/sizes.h>
35 #define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
36 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
37 ((addr_e) << 16) | ((addr_d << 24)))
39 #define _INTC_SHIFT(h) (h & 0x1f)
40 #define _INTC_WIDTH(h) ((h >> 5) & 0xf)
41 #define _INTC_FN(h) ((h >> 9) & 0xf)
42 #define _INTC_MODE(h) ((h >> 13) & 0x7)
43 #define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
44 #define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
46 struct intc_handle_int {
57 struct intc_desc_int {
58 struct list_head list;
59 struct sys_device sysdev;
66 struct intc_handle_int *prio;
68 struct intc_handle_int *sense;
69 unsigned int nr_sense;
70 struct intc_window *window;
71 unsigned int nr_windows;
75 static LIST_HEAD(intc_list);
78 * The intc_irq_map provides a global map of bound IRQ vectors for a
79 * given platform. Allocation of IRQs are either static through the CPU
80 * vector map, or dynamic in the case of board mux vectors or MSI.
82 * As this is a central point for all IRQ controllers on the system,
83 * each of the available sources are mapped out here. This combined with
84 * sparseirq makes it quite trivial to keep the vector map tightly packed
85 * when dynamically creating IRQs, as well as tying in to otherwise
86 * unused irq_desc positions in the sparse array.
88 static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
89 static DEFINE_SPINLOCK(vector_lock);
92 #define IS_SMP(x) x.smp
93 #define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
94 #define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
97 #define INTC_REG(d, x, c) (d->reg[(x)])
98 #define SMP_NR(d, x) 1
101 static unsigned int intc_prio_level[NR_IRQS]; /* for now */
102 static unsigned int default_prio_level = 2; /* 2 - 16 */
103 static unsigned long ack_handle[NR_IRQS];
104 #ifdef CONFIG_INTC_BALANCING
105 static unsigned long dist_handle[NR_IRQS];
108 static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
110 struct irq_chip *chip = get_irq_chip(irq);
111 return container_of(chip, struct intc_desc_int, chip);
114 static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
115 unsigned long address)
117 struct intc_window *window;
120 /* scan through physical windows and convert address */
121 for (k = 0; k < d->nr_windows; k++) {
122 window = d->window + k;
124 if (address < window->phys)
127 if (address >= (window->phys + window->size))
130 address -= window->phys;
131 address += (unsigned long)window->virt;
136 /* no windows defined, register must be 1:1 mapped virt:phys */
140 static unsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address)
144 address = intc_phys_to_virt(d, address);
146 for (k = 0; k < d->nr_reg; k++) {
147 if (d->reg[k] == address)
155 static inline unsigned int set_field(unsigned int value,
156 unsigned int field_value,
159 unsigned int width = _INTC_WIDTH(handle);
160 unsigned int shift = _INTC_SHIFT(handle);
162 value &= ~(((1 << width) - 1) << shift);
163 value |= field_value << shift;
167 static void write_8(unsigned long addr, unsigned long h, unsigned long data)
169 __raw_writeb(set_field(0, data, h), addr);
170 (void)__raw_readb(addr); /* Defeat write posting */
173 static void write_16(unsigned long addr, unsigned long h, unsigned long data)
175 __raw_writew(set_field(0, data, h), addr);
176 (void)__raw_readw(addr); /* Defeat write posting */
179 static void write_32(unsigned long addr, unsigned long h, unsigned long data)
181 __raw_writel(set_field(0, data, h), addr);
182 (void)__raw_readl(addr); /* Defeat write posting */
185 static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
188 local_irq_save(flags);
189 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
190 (void)__raw_readb(addr); /* Defeat write posting */
191 local_irq_restore(flags);
194 static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
197 local_irq_save(flags);
198 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
199 (void)__raw_readw(addr); /* Defeat write posting */
200 local_irq_restore(flags);
203 static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
206 local_irq_save(flags);
207 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
208 (void)__raw_readl(addr); /* Defeat write posting */
209 local_irq_restore(flags);
212 enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
214 static void (*intc_reg_fns[])(unsigned long addr,
216 unsigned long data) = {
217 [REG_FN_WRITE_BASE + 0] = write_8,
218 [REG_FN_WRITE_BASE + 1] = write_16,
219 [REG_FN_WRITE_BASE + 3] = write_32,
220 [REG_FN_MODIFY_BASE + 0] = modify_8,
221 [REG_FN_MODIFY_BASE + 1] = modify_16,
222 [REG_FN_MODIFY_BASE + 3] = modify_32,
225 enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
226 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
227 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
228 MODE_PRIO_REG, /* Priority value written to enable interrupt */
229 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
232 static void intc_mode_field(unsigned long addr,
233 unsigned long handle,
234 void (*fn)(unsigned long,
239 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
242 static void intc_mode_zero(unsigned long addr,
243 unsigned long handle,
244 void (*fn)(unsigned long,
252 static void intc_mode_prio(unsigned long addr,
253 unsigned long handle,
254 void (*fn)(unsigned long,
259 fn(addr, handle, intc_prio_level[irq]);
262 static void (*intc_enable_fns[])(unsigned long addr,
263 unsigned long handle,
264 void (*fn)(unsigned long,
267 unsigned int irq) = {
268 [MODE_ENABLE_REG] = intc_mode_field,
269 [MODE_MASK_REG] = intc_mode_zero,
270 [MODE_DUAL_REG] = intc_mode_field,
271 [MODE_PRIO_REG] = intc_mode_prio,
272 [MODE_PCLR_REG] = intc_mode_prio,
275 static void (*intc_disable_fns[])(unsigned long addr,
276 unsigned long handle,
277 void (*fn)(unsigned long,
280 unsigned int irq) = {
281 [MODE_ENABLE_REG] = intc_mode_zero,
282 [MODE_MASK_REG] = intc_mode_field,
283 [MODE_DUAL_REG] = intc_mode_field,
284 [MODE_PRIO_REG] = intc_mode_zero,
285 [MODE_PCLR_REG] = intc_mode_field,
288 #ifdef CONFIG_INTC_BALANCING
289 static inline void intc_balancing_enable(unsigned int irq)
291 struct intc_desc_int *d = get_intc_desc(irq);
292 unsigned long handle = dist_handle[irq];
295 if (irq_balancing_disabled(irq) || !handle)
298 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
299 intc_reg_fns[_INTC_FN(handle)](addr, handle, 1);
302 static inline void intc_balancing_disable(unsigned int irq)
304 struct intc_desc_int *d = get_intc_desc(irq);
305 unsigned long handle = dist_handle[irq];
308 if (irq_balancing_disabled(irq) || !handle)
311 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
312 intc_reg_fns[_INTC_FN(handle)](addr, handle, 0);
315 static unsigned int intc_dist_data(struct intc_desc *desc,
316 struct intc_desc_int *d,
319 struct intc_mask_reg *mr = desc->hw.mask_regs;
320 unsigned int i, j, fn, mode;
321 unsigned long reg_e, reg_d;
323 for (i = 0; mr && enum_id && i < desc->hw.nr_mask_regs; i++) {
324 mr = desc->hw.mask_regs + i;
327 * Skip this entry if there's no auto-distribution
328 * register associated with it.
333 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
334 if (mr->enum_ids[j] != enum_id)
337 fn = REG_FN_MODIFY_BASE;
338 mode = MODE_ENABLE_REG;
339 reg_e = mr->dist_reg;
340 reg_d = mr->dist_reg;
342 fn += (mr->reg_width >> 3) - 1;
343 return _INTC_MK(fn, mode,
344 intc_get_reg(d, reg_e),
345 intc_get_reg(d, reg_d),
347 (mr->reg_width - 1) - j);
352 * It's possible we've gotten here with no distribution options
353 * available for the IRQ in question, so we just skip over those.
358 static inline void intc_balancing_enable(unsigned int irq)
362 static inline void intc_balancing_disable(unsigned int irq)
367 static inline void _intc_enable(unsigned int irq, unsigned long handle)
369 struct intc_desc_int *d = get_intc_desc(irq);
373 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
375 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
378 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
379 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
380 [_INTC_FN(handle)], irq);
383 intc_balancing_enable(irq);
386 static void intc_enable(unsigned int irq)
388 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
391 static void intc_disable(unsigned int irq)
393 struct intc_desc_int *d = get_intc_desc(irq);
394 unsigned long handle = (unsigned long)get_irq_chip_data(irq);
398 intc_balancing_disable(irq);
400 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
402 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
405 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
406 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
407 [_INTC_FN(handle)], irq);
411 static void (*intc_enable_noprio_fns[])(unsigned long addr,
412 unsigned long handle,
413 void (*fn)(unsigned long,
416 unsigned int irq) = {
417 [MODE_ENABLE_REG] = intc_mode_field,
418 [MODE_MASK_REG] = intc_mode_zero,
419 [MODE_DUAL_REG] = intc_mode_field,
420 [MODE_PRIO_REG] = intc_mode_field,
421 [MODE_PCLR_REG] = intc_mode_field,
424 static void intc_enable_disable(struct intc_desc_int *d,
425 unsigned long handle, int do_enable)
429 void (*fn)(unsigned long, unsigned long,
430 void (*)(unsigned long, unsigned long, unsigned long),
434 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
435 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
436 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
437 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
440 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
441 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
442 fn = intc_disable_fns[_INTC_MODE(handle)];
443 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
448 static int intc_set_wake(unsigned int irq, unsigned int on)
450 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
455 * This is held with the irq desc lock held, so we don't require any
456 * additional locking here at the intc desc level. The affinity mask is
457 * later tested in the enable/disable paths.
459 static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
461 if (!cpumask_intersects(cpumask, cpu_online_mask))
464 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
470 static void intc_mask_ack(unsigned int irq)
472 struct intc_desc_int *d = get_intc_desc(irq);
473 unsigned long handle = ack_handle[irq];
478 /* read register and write zero only to the associated bit */
480 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
481 switch (_INTC_FN(handle)) {
482 case REG_FN_MODIFY_BASE + 0: /* 8bit */
484 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
486 case REG_FN_MODIFY_BASE + 1: /* 16bit */
488 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
490 case REG_FN_MODIFY_BASE + 3: /* 32bit */
492 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
501 static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
508 * this doesn't scale well, but...
510 * this function should only be used for cerain uncommon
511 * operations such as intc_set_priority() and intc_set_sense()
512 * and in those rare cases performance doesn't matter that much.
513 * keeping the memory footprint low is more important.
515 * one rather simple way to speed this up and still keep the
516 * memory footprint down is to make sure the array is sorted
517 * and then perform a bisect to lookup the irq.
519 for (i = 0; i < nr_hp; i++) {
520 if ((hp + i)->irq != irq)
529 int intc_set_priority(unsigned int irq, unsigned int prio)
531 struct intc_desc_int *d = get_intc_desc(irq);
532 struct intc_handle_int *ihp;
534 if (!intc_prio_level[irq] || prio <= 1)
537 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
539 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
542 intc_prio_level[irq] = prio;
545 * only set secondary masking method directly
546 * primary masking method is using intc_prio_level[irq]
547 * priority level will be set during next enable()
549 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
550 _intc_enable(irq, ihp->handle);
555 #define VALID(x) (x | 0x80)
557 static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
558 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
559 [IRQ_TYPE_EDGE_RISING] = VALID(1),
560 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
561 /* SH7706, SH7707 and SH7709 do not support high level triggered */
562 #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
563 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
564 !defined(CONFIG_CPU_SUBTYPE_SH7709)
565 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
569 static int intc_set_sense(unsigned int irq, unsigned int type)
571 struct intc_desc_int *d = get_intc_desc(irq);
572 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
573 struct intc_handle_int *ihp;
579 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
581 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
582 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
587 static intc_enum __init intc_grp_id(struct intc_desc *desc,
590 struct intc_group *g = desc->hw.groups;
593 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
594 g = desc->hw.groups + i;
596 for (j = 0; g->enum_ids[j]; j++) {
597 if (g->enum_ids[j] != enum_id)
607 static unsigned int __init _intc_mask_data(struct intc_desc *desc,
608 struct intc_desc_int *d,
610 unsigned int *reg_idx,
611 unsigned int *fld_idx)
613 struct intc_mask_reg *mr = desc->hw.mask_regs;
614 unsigned int fn, mode;
615 unsigned long reg_e, reg_d;
617 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
618 mr = desc->hw.mask_regs + *reg_idx;
620 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
621 if (mr->enum_ids[*fld_idx] != enum_id)
624 if (mr->set_reg && mr->clr_reg) {
625 fn = REG_FN_WRITE_BASE;
626 mode = MODE_DUAL_REG;
630 fn = REG_FN_MODIFY_BASE;
632 mode = MODE_ENABLE_REG;
636 mode = MODE_MASK_REG;
642 fn += (mr->reg_width >> 3) - 1;
643 return _INTC_MK(fn, mode,
644 intc_get_reg(d, reg_e),
645 intc_get_reg(d, reg_d),
647 (mr->reg_width - 1) - *fld_idx);
657 static unsigned int __init intc_mask_data(struct intc_desc *desc,
658 struct intc_desc_int *d,
659 intc_enum enum_id, int do_grps)
665 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
670 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
675 static unsigned int __init _intc_prio_data(struct intc_desc *desc,
676 struct intc_desc_int *d,
678 unsigned int *reg_idx,
679 unsigned int *fld_idx)
681 struct intc_prio_reg *pr = desc->hw.prio_regs;
682 unsigned int fn, n, mode, bit;
683 unsigned long reg_e, reg_d;
685 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
686 pr = desc->hw.prio_regs + *reg_idx;
688 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
689 if (pr->enum_ids[*fld_idx] != enum_id)
692 if (pr->set_reg && pr->clr_reg) {
693 fn = REG_FN_WRITE_BASE;
694 mode = MODE_PCLR_REG;
698 fn = REG_FN_MODIFY_BASE;
699 mode = MODE_PRIO_REG;
706 fn += (pr->reg_width >> 3) - 1;
709 BUG_ON(n * pr->field_width > pr->reg_width);
711 bit = pr->reg_width - (n * pr->field_width);
713 return _INTC_MK(fn, mode,
714 intc_get_reg(d, reg_e),
715 intc_get_reg(d, reg_d),
716 pr->field_width, bit);
726 static unsigned int __init intc_prio_data(struct intc_desc *desc,
727 struct intc_desc_int *d,
728 intc_enum enum_id, int do_grps)
734 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
739 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
744 static void __init intc_enable_disable_enum(struct intc_desc *desc,
745 struct intc_desc_int *d,
746 intc_enum enum_id, int enable)
748 unsigned int i, j, data;
750 /* go through and enable/disable all mask bits */
753 data = _intc_mask_data(desc, d, enum_id, &i, &j);
755 intc_enable_disable(d, data, enable);
759 /* go through and enable/disable all priority fields */
762 data = _intc_prio_data(desc, d, enum_id, &i, &j);
764 intc_enable_disable(d, data, enable);
770 static unsigned int __init intc_ack_data(struct intc_desc *desc,
771 struct intc_desc_int *d,
774 struct intc_mask_reg *mr = desc->hw.ack_regs;
775 unsigned int i, j, fn, mode;
776 unsigned long reg_e, reg_d;
778 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
779 mr = desc->hw.ack_regs + i;
781 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
782 if (mr->enum_ids[j] != enum_id)
785 fn = REG_FN_MODIFY_BASE;
786 mode = MODE_ENABLE_REG;
790 fn += (mr->reg_width >> 3) - 1;
791 return _INTC_MK(fn, mode,
792 intc_get_reg(d, reg_e),
793 intc_get_reg(d, reg_d),
795 (mr->reg_width - 1) - j);
802 static unsigned int __init intc_sense_data(struct intc_desc *desc,
803 struct intc_desc_int *d,
806 struct intc_sense_reg *sr = desc->hw.sense_regs;
807 unsigned int i, j, fn, bit;
809 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
810 sr = desc->hw.sense_regs + i;
812 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
813 if (sr->enum_ids[j] != enum_id)
816 fn = REG_FN_MODIFY_BASE;
817 fn += (sr->reg_width >> 3) - 1;
819 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
821 bit = sr->reg_width - ((j + 1) * sr->field_width);
823 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
824 0, sr->field_width, bit);
831 static void __init intc_register_irq(struct intc_desc *desc,
832 struct intc_desc_int *d,
836 struct intc_handle_int *hp;
837 unsigned int data[2], primary;
840 * Register the IRQ position with the global IRQ map
842 set_bit(irq, intc_irq_map);
845 * Prefer single interrupt source bitmap over other combinations:
847 * 1. bitmap, single interrupt source
848 * 2. priority, single interrupt source
849 * 3. bitmap, multiple interrupt sources (groups)
850 * 4. priority, multiple interrupt sources (groups)
852 data[0] = intc_mask_data(desc, d, enum_id, 0);
853 data[1] = intc_prio_data(desc, d, enum_id, 0);
856 if (!data[0] && data[1])
859 if (!data[0] && !data[1])
860 pr_warning("missing unique irq mask for irq %d (vect 0x%04x)\n",
863 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
864 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
869 BUG_ON(!data[primary]); /* must have primary masking method */
871 disable_irq_nosync(irq);
872 set_irq_chip_and_handler_name(irq, &d->chip,
873 handle_level_irq, "level");
874 set_irq_chip_data(irq, (void *)data[primary]);
878 * - this needs to be at least 2 for 5-bit priorities on 7780
880 intc_prio_level[irq] = default_prio_level;
882 /* enable secondary masking method if present */
884 _intc_enable(irq, data[!primary]);
886 /* add irq to d->prio list if priority is available */
888 hp = d->prio + d->nr_prio;
890 hp->handle = data[1];
894 * only secondary priority should access registers, so
895 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
897 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
898 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
903 /* add irq to d->sense list if sense is available */
904 data[0] = intc_sense_data(desc, d, enum_id);
906 (d->sense + d->nr_sense)->irq = irq;
907 (d->sense + d->nr_sense)->handle = data[0];
911 /* irq should be disabled by default */
914 if (desc->hw.ack_regs)
915 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
917 #ifdef CONFIG_INTC_BALANCING
918 if (desc->hw.mask_regs)
919 dist_handle[irq] = intc_dist_data(desc, d, enum_id);
923 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
927 static unsigned int __init save_reg(struct intc_desc_int *d,
933 value = intc_phys_to_virt(d, value);
945 static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
947 generic_handle_irq((unsigned int)get_irq_data(irq));
950 int __init register_intc_controller(struct intc_desc *desc)
952 unsigned int i, k, smp;
953 struct intc_hw_desc *hw = &desc->hw;
954 struct intc_desc_int *d;
955 struct resource *res;
957 pr_info("Registered controller '%s' with %u IRQs\n",
958 desc->name, hw->nr_vectors);
960 d = kzalloc(sizeof(*d), GFP_NOWAIT);
964 INIT_LIST_HEAD(&d->list);
965 list_add(&d->list, &intc_list);
967 if (desc->num_resources) {
968 d->nr_windows = desc->num_resources;
969 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
974 for (k = 0; k < d->nr_windows; k++) {
975 res = desc->resource + k;
976 WARN_ON(resource_type(res) != IORESOURCE_MEM);
977 d->window[k].phys = res->start;
978 d->window[k].size = resource_size(res);
979 d->window[k].virt = ioremap_nocache(res->start,
981 if (!d->window[k].virt)
986 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
987 #ifdef CONFIG_INTC_BALANCING
989 d->nr_reg += hw->nr_mask_regs;
991 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
992 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
993 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
995 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
1000 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
1006 if (hw->mask_regs) {
1007 for (i = 0; i < hw->nr_mask_regs; i++) {
1008 smp = IS_SMP(hw->mask_regs[i]);
1009 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
1010 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
1011 #ifdef CONFIG_INTC_BALANCING
1012 k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
1017 if (hw->prio_regs) {
1018 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
1023 for (i = 0; i < hw->nr_prio_regs; i++) {
1024 smp = IS_SMP(hw->prio_regs[i]);
1025 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
1026 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
1030 if (hw->sense_regs) {
1031 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
1036 for (i = 0; i < hw->nr_sense_regs; i++)
1037 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
1040 d->chip.name = desc->name;
1041 d->chip.mask = intc_disable;
1042 d->chip.unmask = intc_enable;
1043 d->chip.mask_ack = intc_disable;
1044 d->chip.enable = intc_enable;
1045 d->chip.disable = intc_disable;
1046 d->chip.shutdown = intc_disable;
1047 d->chip.set_type = intc_set_sense;
1048 d->chip.set_wake = intc_set_wake;
1050 d->chip.set_affinity = intc_set_affinity;
1054 for (i = 0; i < hw->nr_ack_regs; i++)
1055 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
1057 d->chip.mask_ack = intc_mask_ack;
1060 /* disable bits matching force_disable before registering irqs */
1061 if (desc->force_disable)
1062 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
1064 /* disable bits matching force_enable before registering irqs */
1065 if (desc->force_enable)
1066 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
1068 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
1070 /* register the vectors one by one */
1071 for (i = 0; i < hw->nr_vectors; i++) {
1072 struct intc_vect *vect = hw->vectors + i;
1073 unsigned int irq = evt2irq(vect->vect);
1074 struct irq_desc *irq_desc;
1079 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
1080 if (unlikely(!irq_desc)) {
1081 pr_err("can't get irq_desc for %d\n", irq);
1085 intc_register_irq(desc, d, vect->enum_id, irq);
1087 for (k = i + 1; k < hw->nr_vectors; k++) {
1088 struct intc_vect *vect2 = hw->vectors + k;
1089 unsigned int irq2 = evt2irq(vect2->vect);
1091 if (vect->enum_id != vect2->enum_id)
1095 * In the case of multi-evt handling and sparse
1096 * IRQ support, each vector still needs to have
1097 * its own backing irq_desc.
1099 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
1100 if (unlikely(!irq_desc)) {
1101 pr_err("can't get irq_desc for %d\n", irq2);
1107 /* redirect this interrupts to the first one */
1108 set_irq_chip(irq2, &dummy_irq_chip);
1109 set_irq_chained_handler(irq2, intc_redirect_irq);
1110 set_irq_data(irq2, (void *)irq);
1114 /* enable bits matching force_enable after registering irqs */
1115 if (desc->force_enable)
1116 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
1128 for (k = 0; k < d->nr_windows; k++)
1129 if (d->window[k].virt)
1130 iounmap(d->window[k].virt);
1136 pr_err("unable to allocate INTC memory\n");
1141 #ifdef CONFIG_INTC_USERIMASK
1142 static void __iomem *uimask;
1144 int register_intc_userimask(unsigned long addr)
1146 if (unlikely(uimask))
1149 uimask = ioremap_nocache(addr, SZ_4K);
1150 if (unlikely(!uimask))
1153 pr_info("userimask support registered for levels 0 -> %d\n",
1154 default_prio_level - 1);
1160 show_intc_userimask(struct sysdev_class *cls,
1161 struct sysdev_class_attribute *attr, char *buf)
1163 return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
1167 store_intc_userimask(struct sysdev_class *cls,
1168 struct sysdev_class_attribute *attr,
1169 const char *buf, size_t count)
1171 unsigned long level;
1173 level = simple_strtoul(buf, NULL, 10);
1176 * Minimal acceptable IRQ levels are in the 2 - 16 range, but
1177 * these are chomped so as to not interfere with normal IRQs.
1179 * Level 1 is a special case on some CPUs in that it's not
1180 * directly settable, but given that USERIMASK cuts off below a
1181 * certain level, we don't care about this limitation here.
1182 * Level 0 on the other hand equates to user masking disabled.
1184 * We use default_prio_level as a cut off so that only special
1185 * case opt-in IRQs can be mangled.
1187 if (level >= default_prio_level)
1190 __raw_writel(0xa5 << 24 | level << 4, uimask);
1195 static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
1196 show_intc_userimask, store_intc_userimask);
1200 show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1202 struct intc_desc_int *d;
1204 d = container_of(dev, struct intc_desc_int, sysdev);
1206 return sprintf(buf, "%s\n", d->chip.name);
1209 static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1211 static int intc_suspend(struct sys_device *dev, pm_message_t state)
1213 struct intc_desc_int *d;
1214 struct irq_desc *desc;
1217 /* get intc controller associated with this sysdev */
1218 d = container_of(dev, struct intc_desc_int, sysdev);
1220 switch (state.event) {
1222 if (d->state.event != PM_EVENT_FREEZE)
1224 for_each_irq_desc(irq, desc) {
1225 if (desc->handle_irq == intc_redirect_irq)
1227 if (desc->chip != &d->chip)
1229 if (desc->status & IRQ_DISABLED)
1235 case PM_EVENT_FREEZE:
1236 /* nothing has to be done */
1238 case PM_EVENT_SUSPEND:
1239 /* enable wakeup irqs belonging to this intc controller */
1240 for_each_irq_desc(irq, desc) {
1241 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1251 static int intc_resume(struct sys_device *dev)
1253 return intc_suspend(dev, PMSG_ON);
1256 static struct sysdev_class intc_sysdev_class = {
1258 .suspend = intc_suspend,
1259 .resume = intc_resume,
1262 /* register this intc as sysdev to allow suspend/resume */
1263 static int __init register_intc_sysdevs(void)
1265 struct intc_desc_int *d;
1269 error = sysdev_class_register(&intc_sysdev_class);
1270 #ifdef CONFIG_INTC_USERIMASK
1271 if (!error && uimask)
1272 error = sysdev_class_create_file(&intc_sysdev_class,
1276 list_for_each_entry(d, &intc_list, list) {
1278 d->sysdev.cls = &intc_sysdev_class;
1279 error = sysdev_register(&d->sysdev);
1281 error = sysdev_create_file(&d->sysdev,
1291 pr_err("sysdev registration error\n");
1295 device_initcall(register_intc_sysdevs);
1298 * Dynamic IRQ allocation and deallocation
1300 unsigned int create_irq_nr(unsigned int irq_want, int node)
1302 unsigned int irq = 0, new;
1303 unsigned long flags;
1304 struct irq_desc *desc;
1306 spin_lock_irqsave(&vector_lock, flags);
1309 * First try the wanted IRQ
1311 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1314 /* .. then fall back to scanning. */
1315 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1316 if (unlikely(new == nr_irqs))
1319 __set_bit(new, intc_irq_map);
1322 desc = irq_to_desc_alloc_node(new, node);
1323 if (unlikely(!desc)) {
1324 pr_err("can't get irq_desc for %d\n", new);
1328 desc = move_irq_desc(desc, node);
1332 spin_unlock_irqrestore(&vector_lock, flags);
1335 dynamic_irq_init(irq);
1337 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1344 int create_irq(void)
1346 int nid = cpu_to_node(smp_processor_id());
1349 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
1356 void destroy_irq(unsigned int irq)
1358 unsigned long flags;
1360 dynamic_irq_cleanup(irq);
1362 spin_lock_irqsave(&vector_lock, flags);
1363 __clear_bit(irq, intc_irq_map);
1364 spin_unlock_irqrestore(&vector_lock, flags);
1367 int reserve_irq_vector(unsigned int irq)
1369 unsigned long flags;
1372 spin_lock_irqsave(&vector_lock, flags);
1373 if (test_and_set_bit(irq, intc_irq_map))
1375 spin_unlock_irqrestore(&vector_lock, flags);
1380 void reserve_irq_legacy(void)
1382 unsigned long flags;
1385 spin_lock_irqsave(&vector_lock, flags);
1386 j = find_first_bit(intc_irq_map, nr_irqs);
1387 for (i = 0; i < j; i++)
1388 __set_bit(i, intc_irq_map);
1389 spin_unlock_irqrestore(&vector_lock, flags);