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-2012 Cavium, Inc.
9 #include <linux/interrupt.h>
10 #include <linux/irqdomain.h>
11 #include <linux/bitops.h>
12 #include <linux/percpu.h>
13 #include <linux/slab.h>
14 #include <linux/irq.h>
15 #include <linux/smp.h>
18 #include <asm/octeon/octeon.h>
20 static DEFINE_RAW_SPINLOCK(octeon_irq_ciu0_lock
);
21 static DEFINE_RAW_SPINLOCK(octeon_irq_ciu1_lock
);
23 static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu0_en_mirror
);
24 static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu1_en_mirror
);
26 static __read_mostly u8 octeon_irq_ciu_to_irq
[8][64];
28 union octeon_ciu_chip_data
{
37 struct octeon_core_chip_data
{
38 struct mutex core_irq_mutex
;
44 #define MIPS_CORE_IRQ_LINES 8
46 static struct octeon_core_chip_data octeon_irq_core_chip_data
[MIPS_CORE_IRQ_LINES
];
48 static void octeon_irq_set_ciu_mapping(int irq
, int line
, int bit
,
49 struct irq_chip
*chip
,
50 irq_flow_handler_t handler
)
52 union octeon_ciu_chip_data cd
;
54 irq_set_chip_and_handler(irq
, chip
, handler
);
60 irq_set_chip_data(irq
, cd
.p
);
61 octeon_irq_ciu_to_irq
[line
][bit
] = irq
;
64 static void octeon_irq_force_ciu_mapping(struct irq_domain
*domain
,
65 int irq
, int line
, int bit
)
67 irq_domain_associate(domain
, irq
, line
<< 6 | bit
);
70 static int octeon_coreid_for_cpu(int cpu
)
73 return cpu_logical_map(cpu
);
75 return cvmx_get_core_num();
79 static int octeon_cpu_for_coreid(int coreid
)
82 return cpu_number_map(coreid
);
84 return smp_processor_id();
88 static void octeon_irq_core_ack(struct irq_data
*data
)
90 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
91 unsigned int bit
= cd
->bit
;
94 * We don't need to disable IRQs to make these atomic since
95 * they are already disabled earlier in the low level
98 clear_c0_status(0x100 << bit
);
99 /* The two user interrupts must be cleared manually. */
101 clear_c0_cause(0x100 << bit
);
104 static void octeon_irq_core_eoi(struct irq_data
*data
)
106 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
109 * We don't need to disable IRQs to make these atomic since
110 * they are already disabled earlier in the low level
113 set_c0_status(0x100 << cd
->bit
);
116 static void octeon_irq_core_set_enable_local(void *arg
)
118 struct irq_data
*data
= arg
;
119 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
120 unsigned int mask
= 0x100 << cd
->bit
;
123 * Interrupts are already disabled, so these are atomic.
128 clear_c0_status(mask
);
132 static void octeon_irq_core_disable(struct irq_data
*data
)
134 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
135 cd
->desired_en
= false;
138 static void octeon_irq_core_enable(struct irq_data
*data
)
140 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
141 cd
->desired_en
= true;
144 static void octeon_irq_core_bus_lock(struct irq_data
*data
)
146 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
148 mutex_lock(&cd
->core_irq_mutex
);
151 static void octeon_irq_core_bus_sync_unlock(struct irq_data
*data
)
153 struct octeon_core_chip_data
*cd
= irq_data_get_irq_chip_data(data
);
155 if (cd
->desired_en
!= cd
->current_en
) {
156 on_each_cpu(octeon_irq_core_set_enable_local
, data
, 1);
158 cd
->current_en
= cd
->desired_en
;
161 mutex_unlock(&cd
->core_irq_mutex
);
164 static struct irq_chip octeon_irq_chip_core
= {
166 .irq_enable
= octeon_irq_core_enable
,
167 .irq_disable
= octeon_irq_core_disable
,
168 .irq_ack
= octeon_irq_core_ack
,
169 .irq_eoi
= octeon_irq_core_eoi
,
170 .irq_bus_lock
= octeon_irq_core_bus_lock
,
171 .irq_bus_sync_unlock
= octeon_irq_core_bus_sync_unlock
,
173 .irq_cpu_online
= octeon_irq_core_eoi
,
174 .irq_cpu_offline
= octeon_irq_core_ack
,
175 .flags
= IRQCHIP_ONOFFLINE_ENABLED
,
178 static void __init
octeon_irq_init_core(void)
182 struct octeon_core_chip_data
*cd
;
184 for (i
= 0; i
< MIPS_CORE_IRQ_LINES
; i
++) {
185 cd
= &octeon_irq_core_chip_data
[i
];
186 cd
->current_en
= false;
187 cd
->desired_en
= false;
189 mutex_init(&cd
->core_irq_mutex
);
191 irq
= OCTEON_IRQ_SW0
+ i
;
192 irq_set_chip_data(irq
, cd
);
193 irq_set_chip_and_handler(irq
, &octeon_irq_chip_core
,
198 static int next_cpu_for_irq(struct irq_data
*data
)
203 int weight
= cpumask_weight(data
->affinity
);
206 cpu
= smp_processor_id();
208 cpu
= cpumask_next(cpu
, data
->affinity
);
209 if (cpu
>= nr_cpu_ids
) {
212 } else if (cpumask_test_cpu(cpu
, cpu_online_mask
)) {
216 } else if (weight
== 1) {
217 cpu
= cpumask_first(data
->affinity
);
219 cpu
= smp_processor_id();
223 return smp_processor_id();
227 static void octeon_irq_ciu_enable(struct irq_data
*data
)
229 int cpu
= next_cpu_for_irq(data
);
230 int coreid
= octeon_coreid_for_cpu(cpu
);
233 union octeon_ciu_chip_data cd
;
235 cd
.p
= irq_data_get_irq_chip_data(data
);
237 if (cd
.s
.line
== 0) {
238 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
239 pen
= &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
);
240 set_bit(cd
.s
.bit
, pen
);
241 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), *pen
);
242 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
244 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
245 pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
246 set_bit(cd
.s
.bit
, pen
);
247 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), *pen
);
248 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
252 static void octeon_irq_ciu_enable_local(struct irq_data
*data
)
256 union octeon_ciu_chip_data cd
;
258 cd
.p
= irq_data_get_irq_chip_data(data
);
260 if (cd
.s
.line
== 0) {
261 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
262 pen
= &__get_cpu_var(octeon_irq_ciu0_en_mirror
);
263 set_bit(cd
.s
.bit
, pen
);
264 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen
);
265 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
267 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
268 pen
= &__get_cpu_var(octeon_irq_ciu1_en_mirror
);
269 set_bit(cd
.s
.bit
, pen
);
270 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen
);
271 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
275 static void octeon_irq_ciu_disable_local(struct irq_data
*data
)
279 union octeon_ciu_chip_data cd
;
281 cd
.p
= irq_data_get_irq_chip_data(data
);
283 if (cd
.s
.line
== 0) {
284 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
285 pen
= &__get_cpu_var(octeon_irq_ciu0_en_mirror
);
286 clear_bit(cd
.s
.bit
, pen
);
287 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen
);
288 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
290 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
291 pen
= &__get_cpu_var(octeon_irq_ciu1_en_mirror
);
292 clear_bit(cd
.s
.bit
, pen
);
293 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen
);
294 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
298 static void octeon_irq_ciu_disable_all(struct irq_data
*data
)
303 union octeon_ciu_chip_data cd
;
305 wmb(); /* Make sure flag changes arrive before register updates. */
307 cd
.p
= irq_data_get_irq_chip_data(data
);
309 if (cd
.s
.line
== 0) {
310 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
311 for_each_online_cpu(cpu
) {
312 int coreid
= octeon_coreid_for_cpu(cpu
);
313 pen
= &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
);
314 clear_bit(cd
.s
.bit
, pen
);
315 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), *pen
);
317 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
319 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
320 for_each_online_cpu(cpu
) {
321 int coreid
= octeon_coreid_for_cpu(cpu
);
322 pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
323 clear_bit(cd
.s
.bit
, pen
);
324 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), *pen
);
326 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
330 static void octeon_irq_ciu_enable_all(struct irq_data
*data
)
335 union octeon_ciu_chip_data cd
;
337 cd
.p
= irq_data_get_irq_chip_data(data
);
339 if (cd
.s
.line
== 0) {
340 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
341 for_each_online_cpu(cpu
) {
342 int coreid
= octeon_coreid_for_cpu(cpu
);
343 pen
= &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
);
344 set_bit(cd
.s
.bit
, pen
);
345 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), *pen
);
347 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
349 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
350 for_each_online_cpu(cpu
) {
351 int coreid
= octeon_coreid_for_cpu(cpu
);
352 pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
353 set_bit(cd
.s
.bit
, pen
);
354 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), *pen
);
356 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
361 * Enable the irq on the next core in the affinity set for chips that
362 * have the EN*_W1{S,C} registers.
364 static void octeon_irq_ciu_enable_v2(struct irq_data
*data
)
367 int cpu
= next_cpu_for_irq(data
);
368 union octeon_ciu_chip_data cd
;
370 cd
.p
= irq_data_get_irq_chip_data(data
);
371 mask
= 1ull << (cd
.s
.bit
);
374 * Called under the desc lock, so these should never get out
377 if (cd
.s
.line
== 0) {
378 int index
= octeon_coreid_for_cpu(cpu
) * 2;
379 set_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
));
380 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index
), mask
);
382 int index
= octeon_coreid_for_cpu(cpu
) * 2 + 1;
383 set_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
));
384 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index
), mask
);
389 * Enable the irq on the current CPU for chips that
390 * have the EN*_W1{S,C} registers.
392 static void octeon_irq_ciu_enable_local_v2(struct irq_data
*data
)
395 union octeon_ciu_chip_data cd
;
397 cd
.p
= irq_data_get_irq_chip_data(data
);
398 mask
= 1ull << (cd
.s
.bit
);
400 if (cd
.s
.line
== 0) {
401 int index
= cvmx_get_core_num() * 2;
402 set_bit(cd
.s
.bit
, &__get_cpu_var(octeon_irq_ciu0_en_mirror
));
403 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index
), mask
);
405 int index
= cvmx_get_core_num() * 2 + 1;
406 set_bit(cd
.s
.bit
, &__get_cpu_var(octeon_irq_ciu1_en_mirror
));
407 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index
), mask
);
411 static void octeon_irq_ciu_disable_local_v2(struct irq_data
*data
)
414 union octeon_ciu_chip_data cd
;
416 cd
.p
= irq_data_get_irq_chip_data(data
);
417 mask
= 1ull << (cd
.s
.bit
);
419 if (cd
.s
.line
== 0) {
420 int index
= cvmx_get_core_num() * 2;
421 clear_bit(cd
.s
.bit
, &__get_cpu_var(octeon_irq_ciu0_en_mirror
));
422 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index
), mask
);
424 int index
= cvmx_get_core_num() * 2 + 1;
425 clear_bit(cd
.s
.bit
, &__get_cpu_var(octeon_irq_ciu1_en_mirror
));
426 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index
), mask
);
431 * Write to the W1C bit in CVMX_CIU_INTX_SUM0 to clear the irq.
433 static void octeon_irq_ciu_ack(struct irq_data
*data
)
436 union octeon_ciu_chip_data cd
;
438 cd
.p
= data
->chip_data
;
439 mask
= 1ull << (cd
.s
.bit
);
441 if (cd
.s
.line
== 0) {
442 int index
= cvmx_get_core_num() * 2;
443 cvmx_write_csr(CVMX_CIU_INTX_SUM0(index
), mask
);
445 cvmx_write_csr(CVMX_CIU_INT_SUM1
, mask
);
450 * Disable the irq on the all cores for chips that have the EN*_W1{S,C}
453 static void octeon_irq_ciu_disable_all_v2(struct irq_data
*data
)
457 union octeon_ciu_chip_data cd
;
459 wmb(); /* Make sure flag changes arrive before register updates. */
461 cd
.p
= data
->chip_data
;
462 mask
= 1ull << (cd
.s
.bit
);
464 if (cd
.s
.line
== 0) {
465 for_each_online_cpu(cpu
) {
466 int index
= octeon_coreid_for_cpu(cpu
) * 2;
467 clear_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
));
468 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index
), mask
);
471 for_each_online_cpu(cpu
) {
472 int index
= octeon_coreid_for_cpu(cpu
) * 2 + 1;
473 clear_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
));
474 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index
), mask
);
480 * Enable the irq on the all cores for chips that have the EN*_W1{S,C}
483 static void octeon_irq_ciu_enable_all_v2(struct irq_data
*data
)
487 union octeon_ciu_chip_data cd
;
489 cd
.p
= data
->chip_data
;
490 mask
= 1ull << (cd
.s
.bit
);
492 if (cd
.s
.line
== 0) {
493 for_each_online_cpu(cpu
) {
494 int index
= octeon_coreid_for_cpu(cpu
) * 2;
495 set_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
));
496 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index
), mask
);
499 for_each_online_cpu(cpu
) {
500 int index
= octeon_coreid_for_cpu(cpu
) * 2 + 1;
501 set_bit(cd
.s
.bit
, &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
));
502 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index
), mask
);
507 static void octeon_irq_gpio_setup(struct irq_data
*data
)
509 union cvmx_gpio_bit_cfgx cfg
;
510 union octeon_ciu_chip_data cd
;
511 u32 t
= irqd_get_trigger_type(data
);
513 cd
.p
= irq_data_get_irq_chip_data(data
);
517 cfg
.s
.int_type
= (t
& IRQ_TYPE_EDGE_BOTH
) != 0;
518 cfg
.s
.rx_xor
= (t
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_EDGE_FALLING
)) != 0;
520 /* 140 nS glitch filter*/
524 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd
.s
.bit
- 16), cfg
.u64
);
527 static void octeon_irq_ciu_enable_gpio_v2(struct irq_data
*data
)
529 octeon_irq_gpio_setup(data
);
530 octeon_irq_ciu_enable_v2(data
);
533 static void octeon_irq_ciu_enable_gpio(struct irq_data
*data
)
535 octeon_irq_gpio_setup(data
);
536 octeon_irq_ciu_enable(data
);
539 static int octeon_irq_ciu_gpio_set_type(struct irq_data
*data
, unsigned int t
)
541 irqd_set_trigger_type(data
, t
);
542 octeon_irq_gpio_setup(data
);
544 return IRQ_SET_MASK_OK
;
547 static void octeon_irq_ciu_disable_gpio_v2(struct irq_data
*data
)
549 union octeon_ciu_chip_data cd
;
551 cd
.p
= irq_data_get_irq_chip_data(data
);
552 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd
.s
.bit
- 16), 0);
554 octeon_irq_ciu_disable_all_v2(data
);
557 static void octeon_irq_ciu_disable_gpio(struct irq_data
*data
)
559 union octeon_ciu_chip_data cd
;
561 cd
.p
= irq_data_get_irq_chip_data(data
);
562 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd
.s
.bit
- 16), 0);
564 octeon_irq_ciu_disable_all(data
);
567 static void octeon_irq_ciu_gpio_ack(struct irq_data
*data
)
569 union octeon_ciu_chip_data cd
;
572 cd
.p
= irq_data_get_irq_chip_data(data
);
573 mask
= 1ull << (cd
.s
.bit
- 16);
575 cvmx_write_csr(CVMX_GPIO_INT_CLR
, mask
);
578 static void octeon_irq_handle_gpio(unsigned int irq
, struct irq_desc
*desc
)
580 if (irqd_get_trigger_type(irq_desc_get_irq_data(desc
)) & IRQ_TYPE_EDGE_BOTH
)
581 handle_edge_irq(irq
, desc
);
583 handle_level_irq(irq
, desc
);
588 static void octeon_irq_cpu_offline_ciu(struct irq_data
*data
)
590 int cpu
= smp_processor_id();
591 cpumask_t new_affinity
;
593 if (!cpumask_test_cpu(cpu
, data
->affinity
))
596 if (cpumask_weight(data
->affinity
) > 1) {
598 * It has multi CPU affinity, just remove this CPU
599 * from the affinity set.
601 cpumask_copy(&new_affinity
, data
->affinity
);
602 cpumask_clear_cpu(cpu
, &new_affinity
);
604 /* Otherwise, put it on lowest numbered online CPU. */
605 cpumask_clear(&new_affinity
);
606 cpumask_set_cpu(cpumask_first(cpu_online_mask
), &new_affinity
);
608 __irq_set_affinity_locked(data
, &new_affinity
);
611 static int octeon_irq_ciu_set_affinity(struct irq_data
*data
,
612 const struct cpumask
*dest
, bool force
)
615 bool enable_one
= !irqd_irq_disabled(data
) && !irqd_irq_masked(data
);
617 union octeon_ciu_chip_data cd
;
619 cd
.p
= data
->chip_data
;
622 * For non-v2 CIU, we will allow only single CPU affinity.
623 * This removes the need to do locking in the .ack/.eoi
626 if (cpumask_weight(dest
) != 1)
632 if (cd
.s
.line
== 0) {
633 raw_spin_lock_irqsave(&octeon_irq_ciu0_lock
, flags
);
634 for_each_online_cpu(cpu
) {
635 int coreid
= octeon_coreid_for_cpu(cpu
);
636 unsigned long *pen
= &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
);
638 if (cpumask_test_cpu(cpu
, dest
) && enable_one
) {
640 set_bit(cd
.s
.bit
, pen
);
642 clear_bit(cd
.s
.bit
, pen
);
644 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid
* 2), *pen
);
646 raw_spin_unlock_irqrestore(&octeon_irq_ciu0_lock
, flags
);
648 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
649 for_each_online_cpu(cpu
) {
650 int coreid
= octeon_coreid_for_cpu(cpu
);
651 unsigned long *pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
653 if (cpumask_test_cpu(cpu
, dest
) && enable_one
) {
655 set_bit(cd
.s
.bit
, pen
);
657 clear_bit(cd
.s
.bit
, pen
);
659 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), *pen
);
661 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
667 * Set affinity for the irq for chips that have the EN*_W1{S,C}
670 static int octeon_irq_ciu_set_affinity_v2(struct irq_data
*data
,
671 const struct cpumask
*dest
,
675 bool enable_one
= !irqd_irq_disabled(data
) && !irqd_irq_masked(data
);
677 union octeon_ciu_chip_data cd
;
682 cd
.p
= data
->chip_data
;
683 mask
= 1ull << cd
.s
.bit
;
685 if (cd
.s
.line
== 0) {
686 for_each_online_cpu(cpu
) {
687 unsigned long *pen
= &per_cpu(octeon_irq_ciu0_en_mirror
, cpu
);
688 int index
= octeon_coreid_for_cpu(cpu
) * 2;
689 if (cpumask_test_cpu(cpu
, dest
) && enable_one
) {
691 set_bit(cd
.s
.bit
, pen
);
692 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index
), mask
);
694 clear_bit(cd
.s
.bit
, pen
);
695 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index
), mask
);
699 for_each_online_cpu(cpu
) {
700 unsigned long *pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
701 int index
= octeon_coreid_for_cpu(cpu
) * 2 + 1;
702 if (cpumask_test_cpu(cpu
, dest
) && enable_one
) {
704 set_bit(cd
.s
.bit
, pen
);
705 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index
), mask
);
707 clear_bit(cd
.s
.bit
, pen
);
708 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index
), mask
);
717 * The v1 CIU code already masks things, so supply a dummy version to
718 * the core chip code.
720 static void octeon_irq_dummy_mask(struct irq_data
*data
)
725 * Newer octeon chips have support for lockless CIU operation.
727 static struct irq_chip octeon_irq_chip_ciu_v2
= {
729 .irq_enable
= octeon_irq_ciu_enable_v2
,
730 .irq_disable
= octeon_irq_ciu_disable_all_v2
,
731 .irq_ack
= octeon_irq_ciu_ack
,
732 .irq_mask
= octeon_irq_ciu_disable_local_v2
,
733 .irq_unmask
= octeon_irq_ciu_enable_v2
,
735 .irq_set_affinity
= octeon_irq_ciu_set_affinity_v2
,
736 .irq_cpu_offline
= octeon_irq_cpu_offline_ciu
,
740 static struct irq_chip octeon_irq_chip_ciu
= {
742 .irq_enable
= octeon_irq_ciu_enable
,
743 .irq_disable
= octeon_irq_ciu_disable_all
,
744 .irq_ack
= octeon_irq_ciu_ack
,
745 .irq_mask
= octeon_irq_dummy_mask
,
747 .irq_set_affinity
= octeon_irq_ciu_set_affinity
,
748 .irq_cpu_offline
= octeon_irq_cpu_offline_ciu
,
752 /* The mbox versions don't do any affinity or round-robin. */
753 static struct irq_chip octeon_irq_chip_ciu_mbox_v2
= {
755 .irq_enable
= octeon_irq_ciu_enable_all_v2
,
756 .irq_disable
= octeon_irq_ciu_disable_all_v2
,
757 .irq_ack
= octeon_irq_ciu_disable_local_v2
,
758 .irq_eoi
= octeon_irq_ciu_enable_local_v2
,
760 .irq_cpu_online
= octeon_irq_ciu_enable_local_v2
,
761 .irq_cpu_offline
= octeon_irq_ciu_disable_local_v2
,
762 .flags
= IRQCHIP_ONOFFLINE_ENABLED
,
765 static struct irq_chip octeon_irq_chip_ciu_mbox
= {
767 .irq_enable
= octeon_irq_ciu_enable_all
,
768 .irq_disable
= octeon_irq_ciu_disable_all
,
770 .irq_cpu_online
= octeon_irq_ciu_enable_local
,
771 .irq_cpu_offline
= octeon_irq_ciu_disable_local
,
772 .flags
= IRQCHIP_ONOFFLINE_ENABLED
,
775 static struct irq_chip octeon_irq_chip_ciu_gpio_v2
= {
777 .irq_enable
= octeon_irq_ciu_enable_gpio_v2
,
778 .irq_disable
= octeon_irq_ciu_disable_gpio_v2
,
779 .irq_ack
= octeon_irq_ciu_gpio_ack
,
780 .irq_mask
= octeon_irq_ciu_disable_local_v2
,
781 .irq_unmask
= octeon_irq_ciu_enable_v2
,
782 .irq_set_type
= octeon_irq_ciu_gpio_set_type
,
784 .irq_set_affinity
= octeon_irq_ciu_set_affinity_v2
,
786 .flags
= IRQCHIP_SET_TYPE_MASKED
,
789 static struct irq_chip octeon_irq_chip_ciu_gpio
= {
791 .irq_enable
= octeon_irq_ciu_enable_gpio
,
792 .irq_disable
= octeon_irq_ciu_disable_gpio
,
793 .irq_mask
= octeon_irq_dummy_mask
,
794 .irq_ack
= octeon_irq_ciu_gpio_ack
,
795 .irq_set_type
= octeon_irq_ciu_gpio_set_type
,
797 .irq_set_affinity
= octeon_irq_ciu_set_affinity
,
799 .flags
= IRQCHIP_SET_TYPE_MASKED
,
803 * Watchdog interrupts are special. They are associated with a single
804 * core, so we hardwire the affinity to that core.
806 static void octeon_irq_ciu_wd_enable(struct irq_data
*data
)
810 int coreid
= data
->irq
- OCTEON_IRQ_WDOG0
; /* Bit 0-63 of EN1 */
811 int cpu
= octeon_cpu_for_coreid(coreid
);
813 raw_spin_lock_irqsave(&octeon_irq_ciu1_lock
, flags
);
814 pen
= &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
);
815 set_bit(coreid
, pen
);
816 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid
* 2 + 1), *pen
);
817 raw_spin_unlock_irqrestore(&octeon_irq_ciu1_lock
, flags
);
821 * Watchdog interrupts are special. They are associated with a single
822 * core, so we hardwire the affinity to that core.
824 static void octeon_irq_ciu1_wd_enable_v2(struct irq_data
*data
)
826 int coreid
= data
->irq
- OCTEON_IRQ_WDOG0
;
827 int cpu
= octeon_cpu_for_coreid(coreid
);
829 set_bit(coreid
, &per_cpu(octeon_irq_ciu1_en_mirror
, cpu
));
830 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(coreid
* 2 + 1), 1ull << coreid
);
834 static struct irq_chip octeon_irq_chip_ciu_wd_v2
= {
836 .irq_enable
= octeon_irq_ciu1_wd_enable_v2
,
837 .irq_disable
= octeon_irq_ciu_disable_all_v2
,
838 .irq_mask
= octeon_irq_ciu_disable_local_v2
,
839 .irq_unmask
= octeon_irq_ciu_enable_local_v2
,
842 static struct irq_chip octeon_irq_chip_ciu_wd
= {
844 .irq_enable
= octeon_irq_ciu_wd_enable
,
845 .irq_disable
= octeon_irq_ciu_disable_all
,
846 .irq_mask
= octeon_irq_dummy_mask
,
849 static bool octeon_irq_ciu_is_edge(unsigned int line
, unsigned int bit
)
855 case 48 ... 49: /* GMX DRP */
856 case 50: /* IPD_DRP */
857 case 52 ... 55: /* Timers */
875 struct octeon_irq_gpio_domain_data
{
876 unsigned int base_hwirq
;
879 static int octeon_irq_gpio_xlat(struct irq_domain
*d
,
880 struct device_node
*node
,
882 unsigned int intsize
,
883 unsigned long *out_hwirq
,
884 unsigned int *out_type
)
888 unsigned int trigger
;
890 if (d
->of_node
!= node
)
900 trigger
= intspec
[1];
904 type
= IRQ_TYPE_EDGE_RISING
;
907 type
= IRQ_TYPE_EDGE_FALLING
;
910 type
= IRQ_TYPE_LEVEL_HIGH
;
913 type
= IRQ_TYPE_LEVEL_LOW
;
916 pr_err("Error: (%s) Invalid irq trigger specification: %x\n",
919 type
= IRQ_TYPE_LEVEL_LOW
;
928 static int octeon_irq_ciu_xlat(struct irq_domain
*d
,
929 struct device_node
*node
,
931 unsigned int intsize
,
932 unsigned long *out_hwirq
,
933 unsigned int *out_type
)
935 unsigned int ciu
, bit
;
940 if (ciu
> 1 || bit
> 63)
943 /* These are the GPIO lines */
944 if (ciu
== 0 && bit
>= 16 && bit
< 32)
947 *out_hwirq
= (ciu
<< 6) | bit
;
953 static struct irq_chip
*octeon_irq_ciu_chip
;
954 static struct irq_chip
*octeon_irq_gpio_chip
;
956 static bool octeon_irq_virq_in_range(unsigned int virq
)
958 /* We cannot let it overflow the mapping array. */
959 if (virq
< (1ul << 8 * sizeof(octeon_irq_ciu_to_irq
[0][0])))
962 WARN_ONCE(true, "virq out of range %u.\n", virq
);
966 static int octeon_irq_ciu_map(struct irq_domain
*d
,
967 unsigned int virq
, irq_hw_number_t hw
)
969 unsigned int line
= hw
>> 6;
970 unsigned int bit
= hw
& 63;
972 if (!octeon_irq_virq_in_range(virq
))
975 if (line
> 1 || octeon_irq_ciu_to_irq
[line
][bit
] != 0)
978 if (octeon_irq_ciu_is_edge(line
, bit
))
979 octeon_irq_set_ciu_mapping(virq
, line
, bit
,
983 octeon_irq_set_ciu_mapping(virq
, line
, bit
,
990 static int octeon_irq_gpio_map(struct irq_domain
*d
,
991 unsigned int virq
, irq_hw_number_t hw
)
993 struct octeon_irq_gpio_domain_data
*gpiod
= d
->host_data
;
994 unsigned int line
, bit
;
996 if (!octeon_irq_virq_in_range(virq
))
999 hw
+= gpiod
->base_hwirq
;
1002 if (line
> 1 || octeon_irq_ciu_to_irq
[line
][bit
] != 0)
1005 octeon_irq_set_ciu_mapping(virq
, line
, bit
,
1006 octeon_irq_gpio_chip
,
1007 octeon_irq_handle_gpio
);
1011 static struct irq_domain_ops octeon_irq_domain_ciu_ops
= {
1012 .map
= octeon_irq_ciu_map
,
1013 .xlate
= octeon_irq_ciu_xlat
,
1016 static struct irq_domain_ops octeon_irq_domain_gpio_ops
= {
1017 .map
= octeon_irq_gpio_map
,
1018 .xlate
= octeon_irq_gpio_xlat
,
1021 static void octeon_irq_ip2_v1(void)
1023 const unsigned long core_id
= cvmx_get_core_num();
1024 u64 ciu_sum
= cvmx_read_csr(CVMX_CIU_INTX_SUM0(core_id
* 2));
1026 ciu_sum
&= __get_cpu_var(octeon_irq_ciu0_en_mirror
);
1027 clear_c0_status(STATUSF_IP2
);
1028 if (likely(ciu_sum
)) {
1029 int bit
= fls64(ciu_sum
) - 1;
1030 int irq
= octeon_irq_ciu_to_irq
[0][bit
];
1034 spurious_interrupt();
1036 spurious_interrupt();
1038 set_c0_status(STATUSF_IP2
);
1041 static void octeon_irq_ip2_v2(void)
1043 const unsigned long core_id
= cvmx_get_core_num();
1044 u64 ciu_sum
= cvmx_read_csr(CVMX_CIU_INTX_SUM0(core_id
* 2));
1046 ciu_sum
&= __get_cpu_var(octeon_irq_ciu0_en_mirror
);
1047 if (likely(ciu_sum
)) {
1048 int bit
= fls64(ciu_sum
) - 1;
1049 int irq
= octeon_irq_ciu_to_irq
[0][bit
];
1053 spurious_interrupt();
1055 spurious_interrupt();
1058 static void octeon_irq_ip3_v1(void)
1060 u64 ciu_sum
= cvmx_read_csr(CVMX_CIU_INT_SUM1
);
1062 ciu_sum
&= __get_cpu_var(octeon_irq_ciu1_en_mirror
);
1063 clear_c0_status(STATUSF_IP3
);
1064 if (likely(ciu_sum
)) {
1065 int bit
= fls64(ciu_sum
) - 1;
1066 int irq
= octeon_irq_ciu_to_irq
[1][bit
];
1070 spurious_interrupt();
1072 spurious_interrupt();
1074 set_c0_status(STATUSF_IP3
);
1077 static void octeon_irq_ip3_v2(void)
1079 u64 ciu_sum
= cvmx_read_csr(CVMX_CIU_INT_SUM1
);
1081 ciu_sum
&= __get_cpu_var(octeon_irq_ciu1_en_mirror
);
1082 if (likely(ciu_sum
)) {
1083 int bit
= fls64(ciu_sum
) - 1;
1084 int irq
= octeon_irq_ciu_to_irq
[1][bit
];
1088 spurious_interrupt();
1090 spurious_interrupt();
1094 static void octeon_irq_ip4_mask(void)
1096 clear_c0_status(STATUSF_IP4
);
1097 spurious_interrupt();
1100 static void (*octeon_irq_ip2
)(void);
1101 static void (*octeon_irq_ip3
)(void);
1102 static void (*octeon_irq_ip4
)(void);
1104 void __cpuinitdata (*octeon_irq_setup_secondary
)(void);
1106 static void __cpuinit
octeon_irq_percpu_enable(void)
1111 static void __cpuinit
octeon_irq_init_ciu_percpu(void)
1113 int coreid
= cvmx_get_core_num();
1115 * Disable All CIU Interrupts. The ones we need will be
1116 * enabled later. Read the SUM register so we know the write
1119 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid
* 2)), 0);
1120 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid
* 2 + 1)), 0);
1121 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid
* 2)), 0);
1122 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid
* 2 + 1)), 0);
1123 cvmx_read_csr(CVMX_CIU_INTX_SUM0((coreid
* 2)));
1126 static void __cpuinit
octeon_irq_setup_secondary_ciu(void)
1129 __get_cpu_var(octeon_irq_ciu0_en_mirror
) = 0;
1130 __get_cpu_var(octeon_irq_ciu1_en_mirror
) = 0;
1132 octeon_irq_init_ciu_percpu();
1133 octeon_irq_percpu_enable();
1135 /* Enable the CIU lines */
1136 set_c0_status(STATUSF_IP3
| STATUSF_IP2
);
1137 clear_c0_status(STATUSF_IP4
);
1140 static void __init
octeon_irq_init_ciu(void)
1143 struct irq_chip
*chip
;
1144 struct irq_chip
*chip_mbox
;
1145 struct irq_chip
*chip_wd
;
1146 struct device_node
*gpio_node
;
1147 struct device_node
*ciu_node
;
1148 struct irq_domain
*ciu_domain
= NULL
;
1150 octeon_irq_init_ciu_percpu();
1151 octeon_irq_setup_secondary
= octeon_irq_setup_secondary_ciu
;
1153 if (OCTEON_IS_MODEL(OCTEON_CN58XX_PASS2_X
) ||
1154 OCTEON_IS_MODEL(OCTEON_CN56XX_PASS2_X
) ||
1155 OCTEON_IS_MODEL(OCTEON_CN52XX_PASS2_X
) ||
1156 OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
1157 octeon_irq_ip2
= octeon_irq_ip2_v2
;
1158 octeon_irq_ip3
= octeon_irq_ip3_v2
;
1159 chip
= &octeon_irq_chip_ciu_v2
;
1160 chip_mbox
= &octeon_irq_chip_ciu_mbox_v2
;
1161 chip_wd
= &octeon_irq_chip_ciu_wd_v2
;
1162 octeon_irq_gpio_chip
= &octeon_irq_chip_ciu_gpio_v2
;
1164 octeon_irq_ip2
= octeon_irq_ip2_v1
;
1165 octeon_irq_ip3
= octeon_irq_ip3_v1
;
1166 chip
= &octeon_irq_chip_ciu
;
1167 chip_mbox
= &octeon_irq_chip_ciu_mbox
;
1168 chip_wd
= &octeon_irq_chip_ciu_wd
;
1169 octeon_irq_gpio_chip
= &octeon_irq_chip_ciu_gpio
;
1171 octeon_irq_ciu_chip
= chip
;
1172 octeon_irq_ip4
= octeon_irq_ip4_mask
;
1175 octeon_irq_init_core();
1177 gpio_node
= of_find_compatible_node(NULL
, NULL
, "cavium,octeon-3860-gpio");
1179 struct octeon_irq_gpio_domain_data
*gpiod
;
1181 gpiod
= kzalloc(sizeof(*gpiod
), GFP_KERNEL
);
1183 /* gpio domain host_data is the base hwirq number. */
1184 gpiod
->base_hwirq
= 16;
1185 irq_domain_add_linear(gpio_node
, 16, &octeon_irq_domain_gpio_ops
, gpiod
);
1186 of_node_put(gpio_node
);
1188 pr_warn("Cannot allocate memory for GPIO irq_domain.\n");
1190 pr_warn("Cannot find device node for cavium,octeon-3860-gpio.\n");
1192 ciu_node
= of_find_compatible_node(NULL
, NULL
, "cavium,octeon-3860-ciu");
1194 ciu_domain
= irq_domain_add_tree(ciu_node
, &octeon_irq_domain_ciu_ops
, NULL
);
1195 of_node_put(ciu_node
);
1197 panic("Cannot find device node for cavium,octeon-3860-ciu.");
1200 for (i
= 0; i
< 16; i
++)
1201 octeon_irq_force_ciu_mapping(ciu_domain
, i
+ OCTEON_IRQ_WORKQ0
, 0, i
+ 0);
1203 octeon_irq_set_ciu_mapping(OCTEON_IRQ_MBOX0
, 0, 32, chip_mbox
, handle_percpu_irq
);
1204 octeon_irq_set_ciu_mapping(OCTEON_IRQ_MBOX1
, 0, 33, chip_mbox
, handle_percpu_irq
);
1206 for (i
= 0; i
< 4; i
++)
1207 octeon_irq_force_ciu_mapping(ciu_domain
, i
+ OCTEON_IRQ_PCI_INT0
, 0, i
+ 36);
1208 for (i
= 0; i
< 4; i
++)
1209 octeon_irq_force_ciu_mapping(ciu_domain
, i
+ OCTEON_IRQ_PCI_MSI0
, 0, i
+ 40);
1211 octeon_irq_force_ciu_mapping(ciu_domain
, OCTEON_IRQ_RML
, 0, 46);
1212 for (i
= 0; i
< 4; i
++)
1213 octeon_irq_force_ciu_mapping(ciu_domain
, i
+ OCTEON_IRQ_TIMER0
, 0, i
+ 52);
1215 octeon_irq_force_ciu_mapping(ciu_domain
, OCTEON_IRQ_USB0
, 0, 56);
1216 octeon_irq_force_ciu_mapping(ciu_domain
, OCTEON_IRQ_BOOTDMA
, 0, 63);
1219 for (i
= 0; i
< 16; i
++)
1220 octeon_irq_set_ciu_mapping(i
+ OCTEON_IRQ_WDOG0
, 1, i
+ 0, chip_wd
, handle_level_irq
);
1222 octeon_irq_force_ciu_mapping(ciu_domain
, OCTEON_IRQ_USB1
, 1, 17);
1224 /* Enable the CIU lines */
1225 set_c0_status(STATUSF_IP3
| STATUSF_IP2
);
1226 clear_c0_status(STATUSF_IP4
);
1229 void __init
arch_init_irq(void)
1232 /* Set the default affinity to the boot cpu. */
1233 cpumask_clear(irq_default_affinity
);
1234 cpumask_set_cpu(smp_processor_id(), irq_default_affinity
);
1236 octeon_irq_init_ciu();
1239 asmlinkage
void plat_irq_dispatch(void)
1241 unsigned long cop0_cause
;
1242 unsigned long cop0_status
;
1245 cop0_cause
= read_c0_cause();
1246 cop0_status
= read_c0_status();
1247 cop0_cause
&= cop0_status
;
1248 cop0_cause
&= ST0_IM
;
1250 if (unlikely(cop0_cause
& STATUSF_IP2
))
1252 else if (unlikely(cop0_cause
& STATUSF_IP3
))
1254 else if (unlikely(cop0_cause
& STATUSF_IP4
))
1256 else if (likely(cop0_cause
))
1257 do_IRQ(fls(cop0_cause
) - 9 + MIPS_CPU_IRQ_BASE
);
1263 #ifdef CONFIG_HOTPLUG_CPU
1265 void fixup_irqs(void)
1270 #endif /* CONFIG_HOTPLUG_CPU */