2 * Marvell Armada 370 and Armada XP SoC IRQ handling
4 * Copyright (C) 2012 Marvell
6 * Lior Amsalem <alior@marvell.com>
7 * Gregory CLEMENT <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
9 * Ben Dooks <ben.dooks@codethink.co.uk>
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/irqchip/chained_irq.h>
22 #include <linux/cpu.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_pci.h>
27 #include <linux/irqdomain.h>
28 #include <linux/slab.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/msi.h>
31 #include <asm/mach/arch.h>
32 #include <asm/exception.h>
33 #include <asm/smp_plat.h>
34 #include <asm/mach/irq.h>
38 /* Interrupt Controller Registers Map */
39 #define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
40 #define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
42 #define ARMADA_370_XP_INT_CONTROL (0x00)
43 #define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
44 #define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
45 #define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
46 #define ARMADA_370_XP_INT_SOURCE_CPU_MASK 0xF
47 #define ARMADA_370_XP_INT_IRQ_FIQ_MASK(cpuid) ((BIT(0) | BIT(8)) << cpuid)
49 #define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
50 #define ARMADA_375_PPI_CAUSE (0x10)
52 #define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
53 #define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
54 #define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
56 #define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
58 #define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
60 #define IPI_DOORBELL_START (0)
61 #define IPI_DOORBELL_END (8)
62 #define IPI_DOORBELL_MASK 0xFF
63 #define PCI_MSI_DOORBELL_START (16)
64 #define PCI_MSI_DOORBELL_NR (16)
65 #define PCI_MSI_DOORBELL_END (32)
66 #define PCI_MSI_DOORBELL_MASK 0xFFFF0000
68 static void __iomem
*per_cpu_int_base
;
69 static void __iomem
*main_int_base
;
70 static struct irq_domain
*armada_370_xp_mpic_domain
;
71 static u32 doorbell_mask_reg
;
73 static struct irq_domain
*armada_370_xp_msi_domain
;
74 static DECLARE_BITMAP(msi_used
, PCI_MSI_DOORBELL_NR
);
75 static DEFINE_MUTEX(msi_used_lock
);
76 static phys_addr_t msi_doorbell_addr
;
81 * For shared global interrupts, mask/unmask global enable bit
82 * For CPU interrupts, mask/unmask the calling CPU's bit
84 static void armada_370_xp_irq_mask(struct irq_data
*d
)
86 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
88 if (hwirq
!= ARMADA_370_XP_TIMER0_PER_CPU_IRQ
)
89 writel(hwirq
, main_int_base
+
90 ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS
);
92 writel(hwirq
, per_cpu_int_base
+
93 ARMADA_370_XP_INT_SET_MASK_OFFS
);
96 static void armada_370_xp_irq_unmask(struct irq_data
*d
)
98 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
100 if (hwirq
!= ARMADA_370_XP_TIMER0_PER_CPU_IRQ
)
101 writel(hwirq
, main_int_base
+
102 ARMADA_370_XP_INT_SET_ENABLE_OFFS
);
104 writel(hwirq
, per_cpu_int_base
+
105 ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
108 #ifdef CONFIG_PCI_MSI
110 static int armada_370_xp_alloc_msi(void)
114 mutex_lock(&msi_used_lock
);
115 hwirq
= find_first_zero_bit(&msi_used
, PCI_MSI_DOORBELL_NR
);
116 if (hwirq
>= PCI_MSI_DOORBELL_NR
)
119 set_bit(hwirq
, msi_used
);
120 mutex_unlock(&msi_used_lock
);
125 static void armada_370_xp_free_msi(int hwirq
)
127 mutex_lock(&msi_used_lock
);
128 if (!test_bit(hwirq
, msi_used
))
129 pr_err("trying to free unused MSI#%d\n", hwirq
);
131 clear_bit(hwirq
, msi_used
);
132 mutex_unlock(&msi_used_lock
);
135 static int armada_370_xp_setup_msi_irq(struct msi_controller
*chip
,
136 struct pci_dev
*pdev
,
137 struct msi_desc
*desc
)
142 /* We support MSI, but not MSI-X */
143 if (desc
->msi_attrib
.is_msix
)
146 hwirq
= armada_370_xp_alloc_msi();
150 virq
= irq_create_mapping(armada_370_xp_msi_domain
, hwirq
);
152 armada_370_xp_free_msi(hwirq
);
156 irq_set_msi_desc(virq
, desc
);
158 msg
.address_lo
= msi_doorbell_addr
;
160 msg
.data
= 0xf00 | (hwirq
+ 16);
162 pci_write_msi_msg(virq
, &msg
);
166 static void armada_370_xp_teardown_msi_irq(struct msi_controller
*chip
,
169 struct irq_data
*d
= irq_get_irq_data(irq
);
170 unsigned long hwirq
= d
->hwirq
;
172 irq_dispose_mapping(irq
);
173 armada_370_xp_free_msi(hwirq
);
176 static struct irq_chip armada_370_xp_msi_irq_chip
= {
177 .name
= "armada_370_xp_msi_irq",
178 .irq_enable
= pci_msi_unmask_irq
,
179 .irq_disable
= pci_msi_mask_irq
,
180 .irq_mask
= pci_msi_mask_irq
,
181 .irq_unmask
= pci_msi_unmask_irq
,
184 static int armada_370_xp_msi_map(struct irq_domain
*domain
, unsigned int virq
,
187 irq_set_chip_and_handler(virq
, &armada_370_xp_msi_irq_chip
,
189 set_irq_flags(virq
, IRQF_VALID
);
194 static const struct irq_domain_ops armada_370_xp_msi_irq_ops
= {
195 .map
= armada_370_xp_msi_map
,
198 static int armada_370_xp_msi_init(struct device_node
*node
,
199 phys_addr_t main_int_phys_base
)
201 struct msi_controller
*msi_chip
;
205 msi_doorbell_addr
= main_int_phys_base
+
206 ARMADA_370_XP_SW_TRIG_INT_OFFS
;
208 msi_chip
= kzalloc(sizeof(*msi_chip
), GFP_KERNEL
);
212 msi_chip
->setup_irq
= armada_370_xp_setup_msi_irq
;
213 msi_chip
->teardown_irq
= armada_370_xp_teardown_msi_irq
;
214 msi_chip
->of_node
= node
;
216 armada_370_xp_msi_domain
=
217 irq_domain_add_linear(NULL
, PCI_MSI_DOORBELL_NR
,
218 &armada_370_xp_msi_irq_ops
,
220 if (!armada_370_xp_msi_domain
) {
225 ret
= of_pci_msi_chip_add(msi_chip
);
227 irq_domain_remove(armada_370_xp_msi_domain
);
232 reg
= readl(per_cpu_int_base
+ ARMADA_370_XP_IN_DRBEL_MSK_OFFS
)
233 | PCI_MSI_DOORBELL_MASK
;
235 writel(reg
, per_cpu_int_base
+
236 ARMADA_370_XP_IN_DRBEL_MSK_OFFS
);
238 /* Unmask IPI interrupt */
239 writel(1, per_cpu_int_base
+ ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
244 static inline int armada_370_xp_msi_init(struct device_node
*node
,
245 phys_addr_t main_int_phys_base
)
252 static DEFINE_RAW_SPINLOCK(irq_controller_lock
);
254 static int armada_xp_set_affinity(struct irq_data
*d
,
255 const struct cpumask
*mask_val
, bool force
)
257 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
258 unsigned long reg
, mask
;
261 /* Select a single core from the affinity mask which is online */
262 cpu
= cpumask_any_and(mask_val
, cpu_online_mask
);
263 mask
= 1UL << cpu_logical_map(cpu
);
265 raw_spin_lock(&irq_controller_lock
);
266 reg
= readl(main_int_base
+ ARMADA_370_XP_INT_SOURCE_CTL(hwirq
));
267 reg
= (reg
& (~ARMADA_370_XP_INT_SOURCE_CPU_MASK
)) | mask
;
268 writel(reg
, main_int_base
+ ARMADA_370_XP_INT_SOURCE_CTL(hwirq
));
269 raw_spin_unlock(&irq_controller_lock
);
271 return IRQ_SET_MASK_OK
;
275 static struct irq_chip armada_370_xp_irq_chip
= {
276 .name
= "armada_370_xp_irq",
277 .irq_mask
= armada_370_xp_irq_mask
,
278 .irq_mask_ack
= armada_370_xp_irq_mask
,
279 .irq_unmask
= armada_370_xp_irq_unmask
,
281 .irq_set_affinity
= armada_xp_set_affinity
,
285 static int armada_370_xp_mpic_irq_map(struct irq_domain
*h
,
286 unsigned int virq
, irq_hw_number_t hw
)
288 armada_370_xp_irq_mask(irq_get_irq_data(virq
));
289 if (hw
!= ARMADA_370_XP_TIMER0_PER_CPU_IRQ
)
290 writel(hw
, per_cpu_int_base
+
291 ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
293 writel(hw
, main_int_base
+ ARMADA_370_XP_INT_SET_ENABLE_OFFS
);
294 irq_set_status_flags(virq
, IRQ_LEVEL
);
296 if (hw
== ARMADA_370_XP_TIMER0_PER_CPU_IRQ
) {
297 irq_set_percpu_devid(virq
);
298 irq_set_chip_and_handler(virq
, &armada_370_xp_irq_chip
,
299 handle_percpu_devid_irq
);
302 irq_set_chip_and_handler(virq
, &armada_370_xp_irq_chip
,
305 set_irq_flags(virq
, IRQF_VALID
| IRQF_PROBE
);
311 static void armada_mpic_send_doorbell(const struct cpumask
*mask
,
315 unsigned long map
= 0;
317 /* Convert our logical CPU mask into a physical one. */
318 for_each_cpu(cpu
, mask
)
319 map
|= 1 << cpu_logical_map(cpu
);
322 * Ensure that stores to Normal memory are visible to the
323 * other CPUs before issuing the IPI.
328 writel((map
<< 8) | irq
, main_int_base
+
329 ARMADA_370_XP_SW_TRIG_INT_OFFS
);
332 static void armada_xp_mpic_smp_cpu_init(void)
337 control
= readl(main_int_base
+ ARMADA_370_XP_INT_CONTROL
);
338 nr_irqs
= (control
>> 2) & 0x3ff;
340 for (i
= 0; i
< nr_irqs
; i
++)
341 writel(i
, per_cpu_int_base
+ ARMADA_370_XP_INT_SET_MASK_OFFS
);
343 /* Clear pending IPIs */
344 writel(0, per_cpu_int_base
+ ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS
);
346 /* Enable first 8 IPIs */
347 writel(IPI_DOORBELL_MASK
, per_cpu_int_base
+
348 ARMADA_370_XP_IN_DRBEL_MSK_OFFS
);
350 /* Unmask IPI interrupt */
351 writel(0, per_cpu_int_base
+ ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
354 static int armada_xp_mpic_secondary_init(struct notifier_block
*nfb
,
355 unsigned long action
, void *hcpu
)
357 if (action
== CPU_STARTING
|| action
== CPU_STARTING_FROZEN
)
358 armada_xp_mpic_smp_cpu_init();
362 static struct notifier_block armada_370_xp_mpic_cpu_notifier
= {
363 .notifier_call
= armada_xp_mpic_secondary_init
,
367 #endif /* CONFIG_SMP */
369 static struct irq_domain_ops armada_370_xp_mpic_irq_ops
= {
370 .map
= armada_370_xp_mpic_irq_map
,
371 .xlate
= irq_domain_xlate_onecell
,
374 #ifdef CONFIG_PCI_MSI
375 static void armada_370_xp_handle_msi_irq(struct pt_regs
*regs
, bool is_chained
)
379 msimask
= readl_relaxed(per_cpu_int_base
+
380 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS
)
381 & PCI_MSI_DOORBELL_MASK
;
383 writel(~msimask
, per_cpu_int_base
+
384 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS
);
386 for (msinr
= PCI_MSI_DOORBELL_START
;
387 msinr
< PCI_MSI_DOORBELL_END
; msinr
++) {
390 if (!(msimask
& BIT(msinr
)))
394 irq
= irq_find_mapping(armada_370_xp_msi_domain
,
396 generic_handle_irq(irq
);
399 handle_domain_irq(armada_370_xp_msi_domain
,
405 static void armada_370_xp_handle_msi_irq(struct pt_regs
*r
, bool b
) {}
408 static void armada_370_xp_mpic_handle_cascade_irq(unsigned int irq
,
409 struct irq_desc
*desc
)
411 struct irq_chip
*chip
= irq_get_chip(irq
);
412 unsigned long irqmap
, irqn
, irqsrc
, cpuid
;
413 unsigned int cascade_irq
;
415 chained_irq_enter(chip
, desc
);
417 irqmap
= readl_relaxed(per_cpu_int_base
+ ARMADA_375_PPI_CAUSE
);
418 cpuid
= cpu_logical_map(smp_processor_id());
420 for_each_set_bit(irqn
, &irqmap
, BITS_PER_LONG
) {
421 irqsrc
= readl_relaxed(main_int_base
+
422 ARMADA_370_XP_INT_SOURCE_CTL(irqn
));
424 /* Check if the interrupt is not masked on current CPU.
425 * Test IRQ (0-1) and FIQ (8-9) mask bits.
427 if (!(irqsrc
& ARMADA_370_XP_INT_IRQ_FIQ_MASK(cpuid
)))
431 armada_370_xp_handle_msi_irq(NULL
, true);
435 cascade_irq
= irq_find_mapping(armada_370_xp_mpic_domain
, irqn
);
436 generic_handle_irq(cascade_irq
);
439 chained_irq_exit(chip
, desc
);
442 static void __exception_irq_entry
443 armada_370_xp_handle_irq(struct pt_regs
*regs
)
448 irqstat
= readl_relaxed(per_cpu_int_base
+
449 ARMADA_370_XP_CPU_INTACK_OFFS
);
450 irqnr
= irqstat
& 0x3FF;
456 handle_domain_irq(armada_370_xp_mpic_domain
,
463 armada_370_xp_handle_msi_irq(regs
, false);
470 ipimask
= readl_relaxed(per_cpu_int_base
+
471 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS
)
474 writel(~ipimask
, per_cpu_int_base
+
475 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS
);
477 /* Handle all pending doorbells */
478 for (ipinr
= IPI_DOORBELL_START
;
479 ipinr
< IPI_DOORBELL_END
; ipinr
++) {
480 if (ipimask
& (0x1 << ipinr
))
481 handle_IPI(ipinr
, regs
);
490 static int armada_370_xp_mpic_suspend(void)
492 doorbell_mask_reg
= readl(per_cpu_int_base
+
493 ARMADA_370_XP_IN_DRBEL_MSK_OFFS
);
497 static void armada_370_xp_mpic_resume(void)
502 /* Re-enable interrupts */
503 nirqs
= (readl(main_int_base
+ ARMADA_370_XP_INT_CONTROL
) >> 2) & 0x3ff;
504 for (irq
= 0; irq
< nirqs
; irq
++) {
505 struct irq_data
*data
;
508 virq
= irq_linear_revmap(armada_370_xp_mpic_domain
, irq
);
512 if (irq
!= ARMADA_370_XP_TIMER0_PER_CPU_IRQ
)
513 writel(irq
, per_cpu_int_base
+
514 ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
516 writel(irq
, main_int_base
+
517 ARMADA_370_XP_INT_SET_ENABLE_OFFS
);
519 data
= irq_get_irq_data(virq
);
520 if (!irqd_irq_disabled(data
))
521 armada_370_xp_irq_unmask(data
);
524 /* Reconfigure doorbells for IPIs and MSIs */
525 writel(doorbell_mask_reg
,
526 per_cpu_int_base
+ ARMADA_370_XP_IN_DRBEL_MSK_OFFS
);
527 if (doorbell_mask_reg
& IPI_DOORBELL_MASK
)
528 writel(0, per_cpu_int_base
+ ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
529 if (doorbell_mask_reg
& PCI_MSI_DOORBELL_MASK
)
530 writel(1, per_cpu_int_base
+ ARMADA_370_XP_INT_CLEAR_MASK_OFFS
);
533 struct syscore_ops armada_370_xp_mpic_syscore_ops
= {
534 .suspend
= armada_370_xp_mpic_suspend
,
535 .resume
= armada_370_xp_mpic_resume
,
538 static int __init
armada_370_xp_mpic_of_init(struct device_node
*node
,
539 struct device_node
*parent
)
541 struct resource main_int_res
, per_cpu_int_res
;
542 int parent_irq
, nr_irqs
, i
;
545 BUG_ON(of_address_to_resource(node
, 0, &main_int_res
));
546 BUG_ON(of_address_to_resource(node
, 1, &per_cpu_int_res
));
548 BUG_ON(!request_mem_region(main_int_res
.start
,
549 resource_size(&main_int_res
),
551 BUG_ON(!request_mem_region(per_cpu_int_res
.start
,
552 resource_size(&per_cpu_int_res
),
555 main_int_base
= ioremap(main_int_res
.start
,
556 resource_size(&main_int_res
));
557 BUG_ON(!main_int_base
);
559 per_cpu_int_base
= ioremap(per_cpu_int_res
.start
,
560 resource_size(&per_cpu_int_res
));
561 BUG_ON(!per_cpu_int_base
);
563 control
= readl(main_int_base
+ ARMADA_370_XP_INT_CONTROL
);
564 nr_irqs
= (control
>> 2) & 0x3ff;
566 for (i
= 0; i
< nr_irqs
; i
++)
567 writel(i
, main_int_base
+ ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS
);
569 armada_370_xp_mpic_domain
=
570 irq_domain_add_linear(node
, nr_irqs
,
571 &armada_370_xp_mpic_irq_ops
, NULL
);
573 BUG_ON(!armada_370_xp_mpic_domain
);
576 armada_xp_mpic_smp_cpu_init();
579 armada_370_xp_msi_init(node
, main_int_res
.start
);
581 parent_irq
= irq_of_parse_and_map(node
, 0);
582 if (parent_irq
<= 0) {
583 irq_set_default_host(armada_370_xp_mpic_domain
);
584 set_handle_irq(armada_370_xp_handle_irq
);
586 set_smp_cross_call(armada_mpic_send_doorbell
);
587 register_cpu_notifier(&armada_370_xp_mpic_cpu_notifier
);
590 irq_set_chained_handler(parent_irq
,
591 armada_370_xp_mpic_handle_cascade_irq
);
594 register_syscore_ops(&armada_370_xp_mpic_syscore_ops
);
599 IRQCHIP_DECLARE(armada_370_xp_mpic
, "marvell,mpic", armada_370_xp_mpic_of_init
);