2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2004-2008 Cavium Networks
9 #include <linux/interrupt.h>
10 #include <linux/hardirq.h>
12 #include <asm/octeon/octeon.h>
14 DEFINE_RWLOCK(octeon_irq_ciu0_rwlock
);
15 DEFINE_RWLOCK(octeon_irq_ciu1_rwlock
);
16 DEFINE_SPINLOCK(octeon_irq_msi_lock
);
18 static void octeon_irq_core_ack(unsigned int irq
)
20 unsigned int bit
= irq
- OCTEON_IRQ_SW0
;
22 * We don't need to disable IRQs to make these atomic since
23 * they are already disabled earlier in the low level
26 clear_c0_status(0x100 << bit
);
27 /* The two user interrupts must be cleared manually. */
29 clear_c0_cause(0x100 << bit
);
32 static void octeon_irq_core_eoi(unsigned int irq
)
34 struct irq_desc
*desc
= irq_desc
+ irq
;
35 unsigned int bit
= irq
- OCTEON_IRQ_SW0
;
37 * If an IRQ is being processed while we are disabling it the
38 * handler will attempt to unmask the interrupt after it has
41 if (desc
->status
& IRQ_DISABLED
)
44 /* There is a race here. We should fix it. */
47 * We don't need to disable IRQs to make these atomic since
48 * they are already disabled earlier in the low level
51 set_c0_status(0x100 << bit
);
54 static void octeon_irq_core_enable(unsigned int irq
)
57 unsigned int bit
= irq
- OCTEON_IRQ_SW0
;
60 * We need to disable interrupts to make sure our updates are
63 local_irq_save(flags
);
64 set_c0_status(0x100 << bit
);
65 local_irq_restore(flags
);
68 static void octeon_irq_core_disable_local(unsigned int irq
)
71 unsigned int bit
= irq
- OCTEON_IRQ_SW0
;
73 * We need to disable interrupts to make sure our updates are
76 local_irq_save(flags
);
77 clear_c0_status(0x100 << bit
);
78 local_irq_restore(flags
);
81 static void octeon_irq_core_disable(unsigned int irq
)
84 on_each_cpu((void (*)(void *)) octeon_irq_core_disable_local
,
85 (void *) (long) irq
, 1);
87 octeon_irq_core_disable_local(irq
);
91 static struct irq_chip octeon_irq_chip_core
= {
93 .enable
= octeon_irq_core_enable
,
94 .disable
= octeon_irq_core_disable
,
95 .ack
= octeon_irq_core_ack
,
96 .eoi
= octeon_irq_core_eoi
,
100 static void octeon_irq_ciu0_ack(unsigned int irq
)
103 * In order to avoid any locking accessing the CIU, we
104 * acknowledge CIU interrupts by disabling all of them. This
105 * way we can use a per core register and avoid any out of
106 * core locking requirements. This has the side affect that
107 * CIU interrupts can't be processed recursively.
109 * We don't need to disable IRQs to make these atomic since
110 * they are already disabled earlier in the low level
113 clear_c0_status(0x100 << 2);
116 static void octeon_irq_ciu0_eoi(unsigned int irq
)
119 * Enable all CIU interrupts again. We don't need to disable
120 * IRQs to make these atomic since they are already disabled
121 * earlier in the low level interrupt code.
123 set_c0_status(0x100 << 2);
126 static void octeon_irq_ciu0_enable(unsigned int irq
)
128 int coreid
= cvmx_get_core_num();
131 int bit
= irq
- OCTEON_IRQ_WORKQ0
; /* Bit 0-63 of EN0 */
134 * A read lock is used here to make sure only one core is ever
135 * updating the CIU enable bits at a time. During an enable
136 * the cores don't interfere with each other. During a disable
137 * the write lock stops any enables that might cause a
140 read_lock_irqsave(&octeon_irq_ciu0_rwlock
, flags
);
141 en0
= cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
143 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), en0
);
144 cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
145 read_unlock_irqrestore(&octeon_irq_ciu0_rwlock
, flags
);
148 static void octeon_irq_ciu0_disable(unsigned int irq
)
150 int bit
= irq
- OCTEON_IRQ_WORKQ0
; /* Bit 0-63 of EN0 */
155 write_lock_irqsave(&octeon_irq_ciu0_rwlock
, flags
);
156 for_each_online_cpu(cpu
) {
157 int coreid
= cpu_logical_map(cpu
);
158 en0
= cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
159 en0
&= ~(1ull << bit
);
160 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), en0
);
163 * We need to do a read after the last update to make sure all
166 cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2));
167 write_unlock_irqrestore(&octeon_irq_ciu0_rwlock
, flags
);
169 int coreid
= cvmx_get_core_num();
170 local_irq_save(flags
);
171 en0
= cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
172 en0
&= ~(1ull << bit
);
173 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), en0
);
174 cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
175 local_irq_restore(flags
);
180 static void octeon_irq_ciu0_set_affinity(unsigned int irq
, const struct cpumask
*dest
)
183 int bit
= irq
- OCTEON_IRQ_WORKQ0
; /* Bit 0-63 of EN0 */
185 write_lock(&octeon_irq_ciu0_rwlock
);
186 for_each_online_cpu(cpu
) {
187 int coreid
= cpu_logical_map(cpu
);
189 cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid
* 2));
190 if (cpumask_test_cpu(cpu
, dest
))
193 en0
&= ~(1ull << bit
);
194 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), en0
);
197 * We need to do a read after the last update to make sure all
200 cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2));
201 write_unlock(&octeon_irq_ciu0_rwlock
);
205 static struct irq_chip octeon_irq_chip_ciu0
= {
207 .enable
= octeon_irq_ciu0_enable
,
208 .disable
= octeon_irq_ciu0_disable
,
209 .ack
= octeon_irq_ciu0_ack
,
210 .eoi
= octeon_irq_ciu0_eoi
,
212 .set_affinity
= octeon_irq_ciu0_set_affinity
,
217 static void octeon_irq_ciu1_ack(unsigned int irq
)
220 * In order to avoid any locking accessing the CIU, we
221 * acknowledge CIU interrupts by disabling all of them. This
222 * way we can use a per core register and avoid any out of
223 * core locking requirements. This has the side affect that
224 * CIU interrupts can't be processed recursively. We don't
225 * need to disable IRQs to make these atomic since they are
226 * already disabled earlier in the low level interrupt code.
228 clear_c0_status(0x100 << 3);
231 static void octeon_irq_ciu1_eoi(unsigned int irq
)
234 * Enable all CIU interrupts again. We don't need to disable
235 * IRQs to make these atomic since they are already disabled
236 * earlier in the low level interrupt code.
238 set_c0_status(0x100 << 3);
241 static void octeon_irq_ciu1_enable(unsigned int irq
)
243 int coreid
= cvmx_get_core_num();
246 int bit
= irq
- OCTEON_IRQ_WDOG0
; /* Bit 0-63 of EN1 */
249 * A read lock is used here to make sure only one core is ever
250 * updating the CIU enable bits at a time. During an enable
251 * the cores don't interfere with each other. During a disable
252 * the write lock stops any enables that might cause a
255 read_lock_irqsave(&octeon_irq_ciu1_rwlock
, flags
);
256 en1
= cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1));
258 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), en1
);
259 cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1));
260 read_unlock_irqrestore(&octeon_irq_ciu1_rwlock
, flags
);
263 static void octeon_irq_ciu1_disable(unsigned int irq
)
265 int bit
= irq
- OCTEON_IRQ_WDOG0
; /* Bit 0-63 of EN1 */
270 write_lock_irqsave(&octeon_irq_ciu1_rwlock
, flags
);
271 for_each_online_cpu(cpu
) {
272 int coreid
= cpu_logical_map(cpu
);
273 en1
= cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1));
274 en1
&= ~(1ull << bit
);
275 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), en1
);
278 * We need to do a read after the last update to make sure all
281 cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
282 write_unlock_irqrestore(&octeon_irq_ciu1_rwlock
, flags
);
284 int coreid
= cvmx_get_core_num();
285 local_irq_save(flags
);
286 en1
= cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1));
287 en1
&= ~(1ull << bit
);
288 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), en1
);
289 cvmx_read_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1));
290 local_irq_restore(flags
);
295 static void octeon_irq_ciu1_set_affinity(unsigned int irq
, const struct cpumask
*dest
)
298 int bit
= irq
- OCTEON_IRQ_WDOG0
; /* Bit 0-63 of EN1 */
300 write_lock(&octeon_irq_ciu1_rwlock
);
301 for_each_online_cpu(cpu
) {
302 int coreid
= cpu_logical_map(cpu
);
304 cvmx_read_csr(CVMX_CIU_INTX_EN1
306 if (cpumask_test_cpu(cpu
, dest
))
309 en1
&= ~(1ull << bit
);
310 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), en1
);
313 * We need to do a read after the last update to make sure all
316 cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1));
317 write_unlock(&octeon_irq_ciu1_rwlock
);
321 static struct irq_chip octeon_irq_chip_ciu1
= {
323 .enable
= octeon_irq_ciu1_enable
,
324 .disable
= octeon_irq_ciu1_disable
,
325 .ack
= octeon_irq_ciu1_ack
,
326 .eoi
= octeon_irq_ciu1_eoi
,
328 .set_affinity
= octeon_irq_ciu1_set_affinity
,
332 #ifdef CONFIG_PCI_MSI
334 static void octeon_irq_msi_ack(unsigned int irq
)
336 if (!octeon_has_feature(OCTEON_FEATURE_PCIE
)) {
337 /* These chips have PCI */
338 cvmx_write_csr(CVMX_NPI_NPI_MSI_RCV
,
339 1ull << (irq
- OCTEON_IRQ_MSI_BIT0
));
342 * These chips have PCIe. Thankfully the ACK doesn't
345 cvmx_write_csr(CVMX_PEXP_NPEI_MSI_RCV0
,
346 1ull << (irq
- OCTEON_IRQ_MSI_BIT0
));
350 static void octeon_irq_msi_eoi(unsigned int irq
)
355 static void octeon_irq_msi_enable(unsigned int irq
)
357 if (!octeon_has_feature(OCTEON_FEATURE_PCIE
)) {
359 * Octeon PCI doesn't have the ability to mask/unmask
360 * MSI interrupts individually. Instead of
361 * masking/unmasking them in groups of 16, we simple
362 * assume MSI devices are well behaved. MSI
363 * interrupts are always enable and the ACK is assumed
367 /* These chips have PCIe. Note that we only support
368 * the first 64 MSI interrupts. Unfortunately all the
369 * MSI enables are in the same register. We use
370 * MSI0's lock to control access to them all.
374 spin_lock_irqsave(&octeon_irq_msi_lock
, flags
);
375 en
= cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0
);
376 en
|= 1ull << (irq
- OCTEON_IRQ_MSI_BIT0
);
377 cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0
, en
);
378 cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0
);
379 spin_unlock_irqrestore(&octeon_irq_msi_lock
, flags
);
383 static void octeon_irq_msi_disable(unsigned int irq
)
385 if (!octeon_has_feature(OCTEON_FEATURE_PCIE
)) {
386 /* See comment in enable */
389 * These chips have PCIe. Note that we only support
390 * the first 64 MSI interrupts. Unfortunately all the
391 * MSI enables are in the same register. We use
392 * MSI0's lock to control access to them all.
396 spin_lock_irqsave(&octeon_irq_msi_lock
, flags
);
397 en
= cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0
);
398 en
&= ~(1ull << (irq
- OCTEON_IRQ_MSI_BIT0
));
399 cvmx_write_csr(CVMX_PEXP_NPEI_MSI_ENB0
, en
);
400 cvmx_read_csr(CVMX_PEXP_NPEI_MSI_ENB0
);
401 spin_unlock_irqrestore(&octeon_irq_msi_lock
, flags
);
405 static struct irq_chip octeon_irq_chip_msi
= {
407 .enable
= octeon_irq_msi_enable
,
408 .disable
= octeon_irq_msi_disable
,
409 .ack
= octeon_irq_msi_ack
,
410 .eoi
= octeon_irq_msi_eoi
,
414 void __init
arch_init_irq(void)
419 /* Set the default affinity to the boot cpu. */
420 cpumask_clear(irq_default_affinity
);
421 cpumask_set_cpu(smp_processor_id(), irq_default_affinity
);
424 if (NR_IRQS
< OCTEON_IRQ_LAST
)
425 pr_err("octeon_irq_init: NR_IRQS is set too low\n");
427 /* 0 - 15 reserved for i8259 master and slave controller. */
429 /* 17 - 23 Mips internal */
430 for (irq
= OCTEON_IRQ_SW0
; irq
<= OCTEON_IRQ_TIMER
; irq
++) {
431 set_irq_chip_and_handler(irq
, &octeon_irq_chip_core
,
435 /* 24 - 87 CIU_INT_SUM0 */
436 for (irq
= OCTEON_IRQ_WORKQ0
; irq
<= OCTEON_IRQ_BOOTDMA
; irq
++) {
437 set_irq_chip_and_handler(irq
, &octeon_irq_chip_ciu0
,
441 /* 88 - 151 CIU_INT_SUM1 */
442 for (irq
= OCTEON_IRQ_WDOG0
; irq
<= OCTEON_IRQ_RESERVED151
; irq
++) {
443 set_irq_chip_and_handler(irq
, &octeon_irq_chip_ciu1
,
447 #ifdef CONFIG_PCI_MSI
448 /* 152 - 215 PCI/PCIe MSI interrupts */
449 for (irq
= OCTEON_IRQ_MSI_BIT0
; irq
<= OCTEON_IRQ_MSI_BIT63
; irq
++) {
450 set_irq_chip_and_handler(irq
, &octeon_irq_chip_msi
,
454 set_c0_status(0x300 << 2);
457 asmlinkage
void plat_irq_dispatch(void)
459 const unsigned long core_id
= cvmx_get_core_num();
460 const uint64_t ciu_sum0_address
= CVMX_CIU_INTX_SUM0(core_id
* 2);
461 const uint64_t ciu_en0_address
= CVMX_CIU_INTX_EN0(core_id
* 2);
462 const uint64_t ciu_sum1_address
= CVMX_CIU_INT_SUM1
;
463 const uint64_t ciu_en1_address
= CVMX_CIU_INTX_EN1(core_id
* 2 + 1);
464 unsigned long cop0_cause
;
465 unsigned long cop0_status
;
470 cop0_cause
= read_c0_cause();
471 cop0_status
= read_c0_status();
472 cop0_cause
&= cop0_status
;
473 cop0_cause
&= ST0_IM
;
475 if (unlikely(cop0_cause
& STATUSF_IP2
)) {
476 ciu_sum
= cvmx_read_csr(ciu_sum0_address
);
477 ciu_en
= cvmx_read_csr(ciu_en0_address
);
480 do_IRQ(fls64(ciu_sum
) + OCTEON_IRQ_WORKQ0
- 1);
482 spurious_interrupt();
483 } else if (unlikely(cop0_cause
& STATUSF_IP3
)) {
484 ciu_sum
= cvmx_read_csr(ciu_sum1_address
);
485 ciu_en
= cvmx_read_csr(ciu_en1_address
);
488 do_IRQ(fls64(ciu_sum
) + OCTEON_IRQ_WDOG0
- 1);
490 spurious_interrupt();
491 } else if (likely(cop0_cause
)) {
492 do_IRQ(fls(cop0_cause
) - 9 + MIPS_CPU_IRQ_BASE
);