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-2016 Cavium, Inc.
9 #include <linux/of_address.h>
10 #include <linux/interrupt.h>
11 #include <linux/irqdomain.h>
12 #include <linux/bitops.h>
13 #include <linux/of_irq.h>
14 #include <linux/percpu.h>
15 #include <linux/slab.h>
16 #include <linux/irq.h>
17 #include <linux/smp.h>
20 #include <asm/octeon/octeon.h>
21 #include <asm/octeon/cvmx-ciu2-defs.h>
22 #include <asm/octeon/cvmx-ciu3-defs.h>
24 static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu0_en_mirror);
25 static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu1_en_mirror);
26 static DEFINE_PER_CPU(raw_spinlock_t, octeon_irq_ciu_spinlock);
27 static DEFINE_PER_CPU(unsigned int, octeon_irq_ciu3_idt_ip2);
29 static DEFINE_PER_CPU(unsigned int, octeon_irq_ciu3_idt_ip3);
30 static DEFINE_PER_CPU(struct octeon_ciu3_info *, octeon_ciu3_info);
31 #define CIU3_MBOX_PER_CORE 10
34 * The 8 most significant bits of the intsn identify the interrupt major block.
35 * Each major block might use its own interrupt domain. Thus 256 domains are
38 #define MAX_CIU3_DOMAINS 256
40 typedef irq_hw_number_t (*octeon_ciu3_intsn2hw_t)(struct irq_domain *, unsigned int);
42 /* Information for each ciu3 in the system */
43 struct octeon_ciu3_info {
46 struct irq_domain *domain[MAX_CIU3_DOMAINS];
47 octeon_ciu3_intsn2hw_t intsn2hw[MAX_CIU3_DOMAINS];
50 /* Each ciu3 in the system uses its own data (one ciu3 per node) */
51 static struct octeon_ciu3_info *octeon_ciu3_info_per_node[4];
53 struct octeon_irq_ciu_domain_data {
54 int num_sum; /* number of sum registers (2 or 3). */
57 /* Register offsets from ciu3_addr */
58 #define CIU3_CONST 0x220
59 #define CIU3_IDT_CTL(_idt) ((_idt) * 8 + 0x110000)
60 #define CIU3_IDT_PP(_idt, _idx) ((_idt) * 32 + (_idx) * 8 + 0x120000)
61 #define CIU3_IDT_IO(_idt) ((_idt) * 8 + 0x130000)
62 #define CIU3_DEST_PP_INT(_pp_ip) ((_pp_ip) * 8 + 0x200000)
63 #define CIU3_DEST_IO_INT(_io) ((_io) * 8 + 0x210000)
64 #define CIU3_ISC_CTL(_intsn) ((_intsn) * 8 + 0x80000000)
65 #define CIU3_ISC_W1C(_intsn) ((_intsn) * 8 + 0x90000000)
66 #define CIU3_ISC_W1S(_intsn) ((_intsn) * 8 + 0xa0000000)
68 static __read_mostly int octeon_irq_ciu_to_irq[8][64];
70 struct octeon_ciu_chip_data {
72 struct { /* only used for ciu3 */
76 struct { /* only used for ciu/ciu2 */
82 int current_cpu; /* Next CPU expected to take this irq */
83 int ciu_node; /* NUMA node number of the CIU */
86 struct octeon_core_chip_data {
87 struct mutex core_irq_mutex;
93 #define MIPS_CORE_IRQ_LINES 8
95 static struct octeon_core_chip_data octeon_irq_core_chip_data[MIPS_CORE_IRQ_LINES];
97 static int octeon_irq_set_ciu_mapping(int irq, int line, int bit, int gpio_line,
98 struct irq_chip *chip,
99 irq_flow_handler_t handler)
101 struct octeon_ciu_chip_data *cd;
103 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
107 irq_set_chip_and_handler(irq, chip, handler);
111 cd->gpio_line = gpio_line;
113 irq_set_chip_data(irq, cd);
114 octeon_irq_ciu_to_irq[line][bit] = irq;
118 static void octeon_irq_free_cd(struct irq_domain *d, unsigned int irq)
120 struct irq_data *data = irq_get_irq_data(irq);
121 struct octeon_ciu_chip_data *cd = irq_data_get_irq_chip_data(data);
123 irq_set_chip_data(irq, NULL);
127 static int octeon_irq_force_ciu_mapping(struct irq_domain *domain,
128 int irq, int line, int bit)
130 struct device_node *of_node;
133 of_node = irq_domain_get_of_node(domain);
136 ret = irq_alloc_desc_at(irq, of_node_to_nid(of_node));
140 return irq_domain_associate(domain, irq, line << 6 | bit);
143 static int octeon_coreid_for_cpu(int cpu)
146 return cpu_logical_map(cpu);
148 return cvmx_get_core_num();
152 static int octeon_cpu_for_coreid(int coreid)
155 return cpu_number_map(coreid);
157 return smp_processor_id();
161 static void octeon_irq_core_ack(struct irq_data *data)
163 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
164 unsigned int bit = cd->bit;
167 * We don't need to disable IRQs to make these atomic since
168 * they are already disabled earlier in the low level
171 clear_c0_status(0x100 << bit);
172 /* The two user interrupts must be cleared manually. */
174 clear_c0_cause(0x100 << bit);
177 static void octeon_irq_core_eoi(struct irq_data *data)
179 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
182 * We don't need to disable IRQs to make these atomic since
183 * they are already disabled earlier in the low level
186 set_c0_status(0x100 << cd->bit);
189 static void octeon_irq_core_set_enable_local(void *arg)
191 struct irq_data *data = arg;
192 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
193 unsigned int mask = 0x100 << cd->bit;
196 * Interrupts are already disabled, so these are atomic.
201 clear_c0_status(mask);
205 static void octeon_irq_core_disable(struct irq_data *data)
207 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
208 cd->desired_en = false;
211 static void octeon_irq_core_enable(struct irq_data *data)
213 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
214 cd->desired_en = true;
217 static void octeon_irq_core_bus_lock(struct irq_data *data)
219 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
221 mutex_lock(&cd->core_irq_mutex);
224 static void octeon_irq_core_bus_sync_unlock(struct irq_data *data)
226 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
228 if (cd->desired_en != cd->current_en) {
229 on_each_cpu(octeon_irq_core_set_enable_local, data, 1);
231 cd->current_en = cd->desired_en;
234 mutex_unlock(&cd->core_irq_mutex);
237 static struct irq_chip octeon_irq_chip_core = {
239 .irq_enable = octeon_irq_core_enable,
240 .irq_disable = octeon_irq_core_disable,
241 .irq_ack = octeon_irq_core_ack,
242 .irq_eoi = octeon_irq_core_eoi,
243 .irq_bus_lock = octeon_irq_core_bus_lock,
244 .irq_bus_sync_unlock = octeon_irq_core_bus_sync_unlock,
246 .irq_cpu_online = octeon_irq_core_eoi,
247 .irq_cpu_offline = octeon_irq_core_ack,
248 .flags = IRQCHIP_ONOFFLINE_ENABLED,
251 static void __init octeon_irq_init_core(void)
255 struct octeon_core_chip_data *cd;
257 for (i = 0; i < MIPS_CORE_IRQ_LINES; i++) {
258 cd = &octeon_irq_core_chip_data[i];
259 cd->current_en = false;
260 cd->desired_en = false;
262 mutex_init(&cd->core_irq_mutex);
264 irq = OCTEON_IRQ_SW0 + i;
265 irq_set_chip_data(irq, cd);
266 irq_set_chip_and_handler(irq, &octeon_irq_chip_core,
271 static int next_cpu_for_irq(struct irq_data *data)
276 struct cpumask *mask = irq_data_get_affinity_mask(data);
277 int weight = cpumask_weight(mask);
278 struct octeon_ciu_chip_data *cd = irq_data_get_irq_chip_data(data);
281 cpu = cd->current_cpu;
283 cpu = cpumask_next(cpu, mask);
284 if (cpu >= nr_cpu_ids) {
287 } else if (cpumask_test_cpu(cpu, cpu_online_mask)) {
291 } else if (weight == 1) {
292 cpu = cpumask_first(mask);
294 cpu = smp_processor_id();
296 cd->current_cpu = cpu;
299 return smp_processor_id();
303 static void octeon_irq_ciu_enable(struct irq_data *data)
305 int cpu = next_cpu_for_irq(data);
306 int coreid = octeon_coreid_for_cpu(cpu);
309 struct octeon_ciu_chip_data *cd;
310 raw_spinlock_t *lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
312 cd = irq_data_get_irq_chip_data(data);
314 raw_spin_lock_irqsave(lock, flags);
316 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
317 __set_bit(cd->bit, pen);
319 * Must be visible to octeon_irq_ip{2,3}_ciu() before
323 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
325 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
326 __set_bit(cd->bit, pen);
328 * Must be visible to octeon_irq_ip{2,3}_ciu() before
332 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
334 raw_spin_unlock_irqrestore(lock, flags);
337 static void octeon_irq_ciu_enable_local(struct irq_data *data)
341 struct octeon_ciu_chip_data *cd;
342 raw_spinlock_t *lock = this_cpu_ptr(&octeon_irq_ciu_spinlock);
344 cd = irq_data_get_irq_chip_data(data);
346 raw_spin_lock_irqsave(lock, flags);
348 pen = this_cpu_ptr(&octeon_irq_ciu0_en_mirror);
349 __set_bit(cd->bit, pen);
351 * Must be visible to octeon_irq_ip{2,3}_ciu() before
355 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen);
357 pen = this_cpu_ptr(&octeon_irq_ciu1_en_mirror);
358 __set_bit(cd->bit, pen);
360 * Must be visible to octeon_irq_ip{2,3}_ciu() before
364 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen);
366 raw_spin_unlock_irqrestore(lock, flags);
369 static void octeon_irq_ciu_disable_local(struct irq_data *data)
373 struct octeon_ciu_chip_data *cd;
374 raw_spinlock_t *lock = this_cpu_ptr(&octeon_irq_ciu_spinlock);
376 cd = irq_data_get_irq_chip_data(data);
378 raw_spin_lock_irqsave(lock, flags);
380 pen = this_cpu_ptr(&octeon_irq_ciu0_en_mirror);
381 __clear_bit(cd->bit, pen);
383 * Must be visible to octeon_irq_ip{2,3}_ciu() before
387 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen);
389 pen = this_cpu_ptr(&octeon_irq_ciu1_en_mirror);
390 __clear_bit(cd->bit, pen);
392 * Must be visible to octeon_irq_ip{2,3}_ciu() before
396 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen);
398 raw_spin_unlock_irqrestore(lock, flags);
401 static void octeon_irq_ciu_disable_all(struct irq_data *data)
406 struct octeon_ciu_chip_data *cd;
407 raw_spinlock_t *lock;
409 cd = irq_data_get_irq_chip_data(data);
411 for_each_online_cpu(cpu) {
412 int coreid = octeon_coreid_for_cpu(cpu);
413 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
415 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
417 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
419 raw_spin_lock_irqsave(lock, flags);
420 __clear_bit(cd->bit, pen);
422 * Must be visible to octeon_irq_ip{2,3}_ciu() before
427 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
429 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
430 raw_spin_unlock_irqrestore(lock, flags);
434 static void octeon_irq_ciu_enable_all(struct irq_data *data)
439 struct octeon_ciu_chip_data *cd;
440 raw_spinlock_t *lock;
442 cd = irq_data_get_irq_chip_data(data);
444 for_each_online_cpu(cpu) {
445 int coreid = octeon_coreid_for_cpu(cpu);
446 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
448 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
450 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
452 raw_spin_lock_irqsave(lock, flags);
453 __set_bit(cd->bit, pen);
455 * Must be visible to octeon_irq_ip{2,3}_ciu() before
460 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
462 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
463 raw_spin_unlock_irqrestore(lock, flags);
468 * Enable the irq on the next core in the affinity set for chips that
469 * have the EN*_W1{S,C} registers.
471 static void octeon_irq_ciu_enable_v2(struct irq_data *data)
474 int cpu = next_cpu_for_irq(data);
475 struct octeon_ciu_chip_data *cd;
477 cd = irq_data_get_irq_chip_data(data);
478 mask = 1ull << (cd->bit);
481 * Called under the desc lock, so these should never get out
485 int index = octeon_coreid_for_cpu(cpu) * 2;
486 set_bit(cd->bit, &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
487 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
489 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
490 set_bit(cd->bit, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
491 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
496 * Enable the irq in the sum2 registers.
498 static void octeon_irq_ciu_enable_sum2(struct irq_data *data)
501 int cpu = next_cpu_for_irq(data);
502 int index = octeon_coreid_for_cpu(cpu);
503 struct octeon_ciu_chip_data *cd;
505 cd = irq_data_get_irq_chip_data(data);
506 mask = 1ull << (cd->bit);
508 cvmx_write_csr(CVMX_CIU_EN2_PPX_IP4_W1S(index), mask);
512 * Disable the irq in the sum2 registers.
514 static void octeon_irq_ciu_disable_local_sum2(struct irq_data *data)
517 int cpu = next_cpu_for_irq(data);
518 int index = octeon_coreid_for_cpu(cpu);
519 struct octeon_ciu_chip_data *cd;
521 cd = irq_data_get_irq_chip_data(data);
522 mask = 1ull << (cd->bit);
524 cvmx_write_csr(CVMX_CIU_EN2_PPX_IP4_W1C(index), mask);
527 static void octeon_irq_ciu_ack_sum2(struct irq_data *data)
530 int cpu = next_cpu_for_irq(data);
531 int index = octeon_coreid_for_cpu(cpu);
532 struct octeon_ciu_chip_data *cd;
534 cd = irq_data_get_irq_chip_data(data);
535 mask = 1ull << (cd->bit);
537 cvmx_write_csr(CVMX_CIU_SUM2_PPX_IP4(index), mask);
540 static void octeon_irq_ciu_disable_all_sum2(struct irq_data *data)
543 struct octeon_ciu_chip_data *cd;
546 cd = irq_data_get_irq_chip_data(data);
547 mask = 1ull << (cd->bit);
549 for_each_online_cpu(cpu) {
550 int coreid = octeon_coreid_for_cpu(cpu);
552 cvmx_write_csr(CVMX_CIU_EN2_PPX_IP4_W1C(coreid), mask);
557 * Enable the irq on the current CPU for chips that
558 * have the EN*_W1{S,C} registers.
560 static void octeon_irq_ciu_enable_local_v2(struct irq_data *data)
563 struct octeon_ciu_chip_data *cd;
565 cd = irq_data_get_irq_chip_data(data);
566 mask = 1ull << (cd->bit);
569 int index = cvmx_get_core_num() * 2;
570 set_bit(cd->bit, this_cpu_ptr(&octeon_irq_ciu0_en_mirror));
571 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
573 int index = cvmx_get_core_num() * 2 + 1;
574 set_bit(cd->bit, this_cpu_ptr(&octeon_irq_ciu1_en_mirror));
575 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
579 static void octeon_irq_ciu_disable_local_v2(struct irq_data *data)
582 struct octeon_ciu_chip_data *cd;
584 cd = irq_data_get_irq_chip_data(data);
585 mask = 1ull << (cd->bit);
588 int index = cvmx_get_core_num() * 2;
589 clear_bit(cd->bit, this_cpu_ptr(&octeon_irq_ciu0_en_mirror));
590 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
592 int index = cvmx_get_core_num() * 2 + 1;
593 clear_bit(cd->bit, this_cpu_ptr(&octeon_irq_ciu1_en_mirror));
594 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
599 * Write to the W1C bit in CVMX_CIU_INTX_SUM0 to clear the irq.
601 static void octeon_irq_ciu_ack(struct irq_data *data)
604 struct octeon_ciu_chip_data *cd;
606 cd = irq_data_get_irq_chip_data(data);
607 mask = 1ull << (cd->bit);
610 int index = cvmx_get_core_num() * 2;
611 cvmx_write_csr(CVMX_CIU_INTX_SUM0(index), mask);
613 cvmx_write_csr(CVMX_CIU_INT_SUM1, mask);
618 * Disable the irq on the all cores for chips that have the EN*_W1{S,C}
621 static void octeon_irq_ciu_disable_all_v2(struct irq_data *data)
625 struct octeon_ciu_chip_data *cd;
627 cd = irq_data_get_irq_chip_data(data);
628 mask = 1ull << (cd->bit);
631 for_each_online_cpu(cpu) {
632 int index = octeon_coreid_for_cpu(cpu) * 2;
634 &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
635 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
638 for_each_online_cpu(cpu) {
639 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
641 &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
642 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
648 * Enable the irq on the all cores for chips that have the EN*_W1{S,C}
651 static void octeon_irq_ciu_enable_all_v2(struct irq_data *data)
655 struct octeon_ciu_chip_data *cd;
657 cd = irq_data_get_irq_chip_data(data);
658 mask = 1ull << (cd->bit);
661 for_each_online_cpu(cpu) {
662 int index = octeon_coreid_for_cpu(cpu) * 2;
664 &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
665 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
668 for_each_online_cpu(cpu) {
669 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
671 &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
672 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
677 static int octeon_irq_ciu_set_type(struct irq_data *data, unsigned int t)
679 irqd_set_trigger_type(data, t);
681 if (t & IRQ_TYPE_EDGE_BOTH)
682 irq_set_handler_locked(data, handle_edge_irq);
684 irq_set_handler_locked(data, handle_level_irq);
686 return IRQ_SET_MASK_OK;
689 static void octeon_irq_gpio_setup(struct irq_data *data)
691 union cvmx_gpio_bit_cfgx cfg;
692 struct octeon_ciu_chip_data *cd;
693 u32 t = irqd_get_trigger_type(data);
695 cd = irq_data_get_irq_chip_data(data);
699 cfg.s.int_type = (t & IRQ_TYPE_EDGE_BOTH) != 0;
700 cfg.s.rx_xor = (t & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) != 0;
702 /* 140 nS glitch filter*/
706 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd->gpio_line), cfg.u64);
709 static void octeon_irq_ciu_enable_gpio_v2(struct irq_data *data)
711 octeon_irq_gpio_setup(data);
712 octeon_irq_ciu_enable_v2(data);
715 static void octeon_irq_ciu_enable_gpio(struct irq_data *data)
717 octeon_irq_gpio_setup(data);
718 octeon_irq_ciu_enable(data);
721 static int octeon_irq_ciu_gpio_set_type(struct irq_data *data, unsigned int t)
723 irqd_set_trigger_type(data, t);
724 octeon_irq_gpio_setup(data);
726 if (t & IRQ_TYPE_EDGE_BOTH)
727 irq_set_handler_locked(data, handle_edge_irq);
729 irq_set_handler_locked(data, handle_level_irq);
731 return IRQ_SET_MASK_OK;
734 static void octeon_irq_ciu_disable_gpio_v2(struct irq_data *data)
736 struct octeon_ciu_chip_data *cd;
738 cd = irq_data_get_irq_chip_data(data);
739 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd->gpio_line), 0);
741 octeon_irq_ciu_disable_all_v2(data);
744 static void octeon_irq_ciu_disable_gpio(struct irq_data *data)
746 struct octeon_ciu_chip_data *cd;
748 cd = irq_data_get_irq_chip_data(data);
749 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd->gpio_line), 0);
751 octeon_irq_ciu_disable_all(data);
754 static void octeon_irq_ciu_gpio_ack(struct irq_data *data)
756 struct octeon_ciu_chip_data *cd;
759 cd = irq_data_get_irq_chip_data(data);
760 mask = 1ull << (cd->gpio_line);
762 cvmx_write_csr(CVMX_GPIO_INT_CLR, mask);
767 static void octeon_irq_cpu_offline_ciu(struct irq_data *data)
769 int cpu = smp_processor_id();
770 cpumask_t new_affinity;
771 struct cpumask *mask = irq_data_get_affinity_mask(data);
773 if (!cpumask_test_cpu(cpu, mask))
776 if (cpumask_weight(mask) > 1) {
778 * It has multi CPU affinity, just remove this CPU
779 * from the affinity set.
781 cpumask_copy(&new_affinity, mask);
782 cpumask_clear_cpu(cpu, &new_affinity);
784 /* Otherwise, put it on lowest numbered online CPU. */
785 cpumask_clear(&new_affinity);
786 cpumask_set_cpu(cpumask_first(cpu_online_mask), &new_affinity);
788 irq_set_affinity_locked(data, &new_affinity, false);
791 static int octeon_irq_ciu_set_affinity(struct irq_data *data,
792 const struct cpumask *dest, bool force)
795 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
797 struct octeon_ciu_chip_data *cd;
799 raw_spinlock_t *lock;
801 cd = irq_data_get_irq_chip_data(data);
804 * For non-v2 CIU, we will allow only single CPU affinity.
805 * This removes the need to do locking in the .ack/.eoi
808 if (cpumask_weight(dest) != 1)
815 for_each_online_cpu(cpu) {
816 int coreid = octeon_coreid_for_cpu(cpu);
818 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
819 raw_spin_lock_irqsave(lock, flags);
822 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
824 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
826 if (cpumask_test_cpu(cpu, dest) && enable_one) {
828 __set_bit(cd->bit, pen);
830 __clear_bit(cd->bit, pen);
833 * Must be visible to octeon_irq_ip{2,3}_ciu() before
839 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
841 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
843 raw_spin_unlock_irqrestore(lock, flags);
849 * Set affinity for the irq for chips that have the EN*_W1{S,C}
852 static int octeon_irq_ciu_set_affinity_v2(struct irq_data *data,
853 const struct cpumask *dest,
857 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
859 struct octeon_ciu_chip_data *cd;
864 cd = irq_data_get_irq_chip_data(data);
865 mask = 1ull << cd->bit;
868 for_each_online_cpu(cpu) {
869 unsigned long *pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
870 int index = octeon_coreid_for_cpu(cpu) * 2;
871 if (cpumask_test_cpu(cpu, dest) && enable_one) {
873 set_bit(cd->bit, pen);
874 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
876 clear_bit(cd->bit, pen);
877 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
881 for_each_online_cpu(cpu) {
882 unsigned long *pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
883 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
884 if (cpumask_test_cpu(cpu, dest) && enable_one) {
886 set_bit(cd->bit, pen);
887 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
889 clear_bit(cd->bit, pen);
890 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
897 static int octeon_irq_ciu_set_affinity_sum2(struct irq_data *data,
898 const struct cpumask *dest,
902 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
904 struct octeon_ciu_chip_data *cd;
909 cd = irq_data_get_irq_chip_data(data);
910 mask = 1ull << cd->bit;
912 for_each_online_cpu(cpu) {
913 int index = octeon_coreid_for_cpu(cpu);
915 if (cpumask_test_cpu(cpu, dest) && enable_one) {
917 cvmx_write_csr(CVMX_CIU_EN2_PPX_IP4_W1S(index), mask);
919 cvmx_write_csr(CVMX_CIU_EN2_PPX_IP4_W1C(index), mask);
926 static unsigned int edge_startup(struct irq_data *data)
928 /* ack any pending edge-irq at startup, so there is
929 * an _edge_ to fire on when the event reappears.
931 data->chip->irq_ack(data);
932 data->chip->irq_enable(data);
937 * Newer octeon chips have support for lockless CIU operation.
939 static struct irq_chip octeon_irq_chip_ciu_v2 = {
941 .irq_enable = octeon_irq_ciu_enable_v2,
942 .irq_disable = octeon_irq_ciu_disable_all_v2,
943 .irq_mask = octeon_irq_ciu_disable_local_v2,
944 .irq_unmask = octeon_irq_ciu_enable_v2,
946 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
947 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
951 static struct irq_chip octeon_irq_chip_ciu_v2_edge = {
953 .irq_enable = octeon_irq_ciu_enable_v2,
954 .irq_disable = octeon_irq_ciu_disable_all_v2,
955 .irq_ack = octeon_irq_ciu_ack,
956 .irq_mask = octeon_irq_ciu_disable_local_v2,
957 .irq_unmask = octeon_irq_ciu_enable_v2,
959 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
960 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
965 * Newer octeon chips have support for lockless CIU operation.
967 static struct irq_chip octeon_irq_chip_ciu_sum2 = {
969 .irq_enable = octeon_irq_ciu_enable_sum2,
970 .irq_disable = octeon_irq_ciu_disable_all_sum2,
971 .irq_mask = octeon_irq_ciu_disable_local_sum2,
972 .irq_unmask = octeon_irq_ciu_enable_sum2,
974 .irq_set_affinity = octeon_irq_ciu_set_affinity_sum2,
975 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
979 static struct irq_chip octeon_irq_chip_ciu_sum2_edge = {
981 .irq_enable = octeon_irq_ciu_enable_sum2,
982 .irq_disable = octeon_irq_ciu_disable_all_sum2,
983 .irq_ack = octeon_irq_ciu_ack_sum2,
984 .irq_mask = octeon_irq_ciu_disable_local_sum2,
985 .irq_unmask = octeon_irq_ciu_enable_sum2,
987 .irq_set_affinity = octeon_irq_ciu_set_affinity_sum2,
988 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
992 static struct irq_chip octeon_irq_chip_ciu = {
994 .irq_enable = octeon_irq_ciu_enable,
995 .irq_disable = octeon_irq_ciu_disable_all,
996 .irq_mask = octeon_irq_ciu_disable_local,
997 .irq_unmask = octeon_irq_ciu_enable,
999 .irq_set_affinity = octeon_irq_ciu_set_affinity,
1000 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1004 static struct irq_chip octeon_irq_chip_ciu_edge = {
1006 .irq_enable = octeon_irq_ciu_enable,
1007 .irq_disable = octeon_irq_ciu_disable_all,
1008 .irq_ack = octeon_irq_ciu_ack,
1009 .irq_mask = octeon_irq_ciu_disable_local,
1010 .irq_unmask = octeon_irq_ciu_enable,
1012 .irq_set_affinity = octeon_irq_ciu_set_affinity,
1013 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1017 /* The mbox versions don't do any affinity or round-robin. */
1018 static struct irq_chip octeon_irq_chip_ciu_mbox_v2 = {
1020 .irq_enable = octeon_irq_ciu_enable_all_v2,
1021 .irq_disable = octeon_irq_ciu_disable_all_v2,
1022 .irq_ack = octeon_irq_ciu_disable_local_v2,
1023 .irq_eoi = octeon_irq_ciu_enable_local_v2,
1025 .irq_cpu_online = octeon_irq_ciu_enable_local_v2,
1026 .irq_cpu_offline = octeon_irq_ciu_disable_local_v2,
1027 .flags = IRQCHIP_ONOFFLINE_ENABLED,
1030 static struct irq_chip octeon_irq_chip_ciu_mbox = {
1032 .irq_enable = octeon_irq_ciu_enable_all,
1033 .irq_disable = octeon_irq_ciu_disable_all,
1034 .irq_ack = octeon_irq_ciu_disable_local,
1035 .irq_eoi = octeon_irq_ciu_enable_local,
1037 .irq_cpu_online = octeon_irq_ciu_enable_local,
1038 .irq_cpu_offline = octeon_irq_ciu_disable_local,
1039 .flags = IRQCHIP_ONOFFLINE_ENABLED,
1042 static struct irq_chip octeon_irq_chip_ciu_gpio_v2 = {
1044 .irq_enable = octeon_irq_ciu_enable_gpio_v2,
1045 .irq_disable = octeon_irq_ciu_disable_gpio_v2,
1046 .irq_ack = octeon_irq_ciu_gpio_ack,
1047 .irq_mask = octeon_irq_ciu_disable_local_v2,
1048 .irq_unmask = octeon_irq_ciu_enable_v2,
1049 .irq_set_type = octeon_irq_ciu_gpio_set_type,
1051 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
1052 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1054 .flags = IRQCHIP_SET_TYPE_MASKED,
1057 static struct irq_chip octeon_irq_chip_ciu_gpio = {
1059 .irq_enable = octeon_irq_ciu_enable_gpio,
1060 .irq_disable = octeon_irq_ciu_disable_gpio,
1061 .irq_mask = octeon_irq_ciu_disable_local,
1062 .irq_unmask = octeon_irq_ciu_enable,
1063 .irq_ack = octeon_irq_ciu_gpio_ack,
1064 .irq_set_type = octeon_irq_ciu_gpio_set_type,
1066 .irq_set_affinity = octeon_irq_ciu_set_affinity,
1067 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1069 .flags = IRQCHIP_SET_TYPE_MASKED,
1073 * Watchdog interrupts are special. They are associated with a single
1074 * core, so we hardwire the affinity to that core.
1076 static void octeon_irq_ciu_wd_enable(struct irq_data *data)
1078 unsigned long flags;
1080 int coreid = data->irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
1081 int cpu = octeon_cpu_for_coreid(coreid);
1082 raw_spinlock_t *lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
1084 raw_spin_lock_irqsave(lock, flags);
1085 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
1086 __set_bit(coreid, pen);
1088 * Must be visible to octeon_irq_ip{2,3}_ciu() before enabling
1092 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
1093 raw_spin_unlock_irqrestore(lock, flags);
1097 * Watchdog interrupts are special. They are associated with a single
1098 * core, so we hardwire the affinity to that core.
1100 static void octeon_irq_ciu1_wd_enable_v2(struct irq_data *data)
1102 int coreid = data->irq - OCTEON_IRQ_WDOG0;
1103 int cpu = octeon_cpu_for_coreid(coreid);
1105 set_bit(coreid, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
1106 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(coreid * 2 + 1), 1ull << coreid);
1110 static struct irq_chip octeon_irq_chip_ciu_wd_v2 = {
1112 .irq_enable = octeon_irq_ciu1_wd_enable_v2,
1113 .irq_disable = octeon_irq_ciu_disable_all_v2,
1114 .irq_mask = octeon_irq_ciu_disable_local_v2,
1115 .irq_unmask = octeon_irq_ciu_enable_local_v2,
1118 static struct irq_chip octeon_irq_chip_ciu_wd = {
1120 .irq_enable = octeon_irq_ciu_wd_enable,
1121 .irq_disable = octeon_irq_ciu_disable_all,
1122 .irq_mask = octeon_irq_ciu_disable_local,
1123 .irq_unmask = octeon_irq_ciu_enable_local,
1126 static bool octeon_irq_ciu_is_edge(unsigned int line, unsigned int bit)
1132 case 48 ... 49: /* GMX DRP */
1133 case 50: /* IPD_DRP */
1134 case 52 ... 55: /* Timers */
1141 else /* line == 1 */
1152 struct octeon_irq_gpio_domain_data {
1153 unsigned int base_hwirq;
1156 static int octeon_irq_gpio_xlat(struct irq_domain *d,
1157 struct device_node *node,
1159 unsigned int intsize,
1160 unsigned long *out_hwirq,
1161 unsigned int *out_type)
1165 unsigned int trigger;
1167 if (irq_domain_get_of_node(d) != node)
1177 trigger = intspec[1];
1181 type = IRQ_TYPE_EDGE_RISING;
1184 type = IRQ_TYPE_EDGE_FALLING;
1187 type = IRQ_TYPE_LEVEL_HIGH;
1190 type = IRQ_TYPE_LEVEL_LOW;
1193 pr_err("Error: (%s) Invalid irq trigger specification: %x\n",
1196 type = IRQ_TYPE_LEVEL_LOW;
1205 static int octeon_irq_ciu_xlat(struct irq_domain *d,
1206 struct device_node *node,
1208 unsigned int intsize,
1209 unsigned long *out_hwirq,
1210 unsigned int *out_type)
1212 unsigned int ciu, bit;
1213 struct octeon_irq_ciu_domain_data *dd = d->host_data;
1218 if (ciu >= dd->num_sum || bit > 63)
1221 *out_hwirq = (ciu << 6) | bit;
1227 static struct irq_chip *octeon_irq_ciu_chip;
1228 static struct irq_chip *octeon_irq_ciu_chip_edge;
1229 static struct irq_chip *octeon_irq_gpio_chip;
1231 static int octeon_irq_ciu_map(struct irq_domain *d,
1232 unsigned int virq, irq_hw_number_t hw)
1235 unsigned int line = hw >> 6;
1236 unsigned int bit = hw & 63;
1237 struct octeon_irq_ciu_domain_data *dd = d->host_data;
1239 if (line >= dd->num_sum || octeon_irq_ciu_to_irq[line][bit] != 0)
1243 if (octeon_irq_ciu_is_edge(line, bit))
1244 rv = octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1245 &octeon_irq_chip_ciu_sum2_edge,
1248 rv = octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1249 &octeon_irq_chip_ciu_sum2,
1252 if (octeon_irq_ciu_is_edge(line, bit))
1253 rv = octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1254 octeon_irq_ciu_chip_edge,
1257 rv = octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1258 octeon_irq_ciu_chip,
1264 static int octeon_irq_gpio_map(struct irq_domain *d,
1265 unsigned int virq, irq_hw_number_t hw)
1267 struct octeon_irq_gpio_domain_data *gpiod = d->host_data;
1268 unsigned int line, bit;
1271 line = (hw + gpiod->base_hwirq) >> 6;
1272 bit = (hw + gpiod->base_hwirq) & 63;
1273 if (line >= ARRAY_SIZE(octeon_irq_ciu_to_irq) ||
1274 octeon_irq_ciu_to_irq[line][bit] != 0)
1278 * Default to handle_level_irq. If the DT contains a different
1279 * trigger type, it will call the irq_set_type callback and
1280 * the handler gets updated.
1282 r = octeon_irq_set_ciu_mapping(virq, line, bit, hw,
1283 octeon_irq_gpio_chip, handle_level_irq);
1287 static struct irq_domain_ops octeon_irq_domain_ciu_ops = {
1288 .map = octeon_irq_ciu_map,
1289 .unmap = octeon_irq_free_cd,
1290 .xlate = octeon_irq_ciu_xlat,
1293 static struct irq_domain_ops octeon_irq_domain_gpio_ops = {
1294 .map = octeon_irq_gpio_map,
1295 .unmap = octeon_irq_free_cd,
1296 .xlate = octeon_irq_gpio_xlat,
1299 static void octeon_irq_ip2_ciu(void)
1301 const unsigned long core_id = cvmx_get_core_num();
1302 u64 ciu_sum = cvmx_read_csr(CVMX_CIU_INTX_SUM0(core_id * 2));
1304 ciu_sum &= __this_cpu_read(octeon_irq_ciu0_en_mirror);
1305 if (likely(ciu_sum)) {
1306 int bit = fls64(ciu_sum) - 1;
1307 int irq = octeon_irq_ciu_to_irq[0][bit];
1311 spurious_interrupt();
1313 spurious_interrupt();
1317 static void octeon_irq_ip3_ciu(void)
1319 u64 ciu_sum = cvmx_read_csr(CVMX_CIU_INT_SUM1);
1321 ciu_sum &= __this_cpu_read(octeon_irq_ciu1_en_mirror);
1322 if (likely(ciu_sum)) {
1323 int bit = fls64(ciu_sum) - 1;
1324 int irq = octeon_irq_ciu_to_irq[1][bit];
1328 spurious_interrupt();
1330 spurious_interrupt();
1334 static void octeon_irq_ip4_ciu(void)
1336 int coreid = cvmx_get_core_num();
1337 u64 ciu_sum = cvmx_read_csr(CVMX_CIU_SUM2_PPX_IP4(coreid));
1338 u64 ciu_en = cvmx_read_csr(CVMX_CIU_EN2_PPX_IP4(coreid));
1341 if (likely(ciu_sum)) {
1342 int bit = fls64(ciu_sum) - 1;
1343 int irq = octeon_irq_ciu_to_irq[2][bit];
1348 spurious_interrupt();
1350 spurious_interrupt();
1354 static bool octeon_irq_use_ip4;
1356 static void octeon_irq_local_enable_ip4(void *arg)
1358 set_c0_status(STATUSF_IP4);
1361 static void octeon_irq_ip4_mask(void)
1363 clear_c0_status(STATUSF_IP4);
1364 spurious_interrupt();
1367 static void (*octeon_irq_ip2)(void);
1368 static void (*octeon_irq_ip3)(void);
1369 static void (*octeon_irq_ip4)(void);
1371 void (*octeon_irq_setup_secondary)(void);
1373 void octeon_irq_set_ip4_handler(octeon_irq_ip4_handler_t h)
1376 octeon_irq_use_ip4 = true;
1377 on_each_cpu(octeon_irq_local_enable_ip4, NULL, 1);
1380 static void octeon_irq_percpu_enable(void)
1385 static void octeon_irq_init_ciu_percpu(void)
1387 int coreid = cvmx_get_core_num();
1390 __this_cpu_write(octeon_irq_ciu0_en_mirror, 0);
1391 __this_cpu_write(octeon_irq_ciu1_en_mirror, 0);
1393 raw_spin_lock_init(this_cpu_ptr(&octeon_irq_ciu_spinlock));
1395 * Disable All CIU Interrupts. The ones we need will be
1396 * enabled later. Read the SUM register so we know the write
1399 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2)), 0);
1400 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2 + 1)), 0);
1401 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2)), 0);
1402 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2 + 1)), 0);
1403 cvmx_read_csr(CVMX_CIU_INTX_SUM0((coreid * 2)));
1406 static void octeon_irq_init_ciu2_percpu(void)
1409 int coreid = cvmx_get_core_num();
1410 u64 base = CVMX_CIU2_EN_PPX_IP2_WRKQ(coreid);
1413 * Disable All CIU2 Interrupts. The ones we need will be
1414 * enabled later. Read the SUM register so we know the write
1417 * There are 9 registers and 3 IPX levels with strides 0x1000
1418 * and 0x200 respectivly. Use loops to clear them.
1420 for (regx = 0; regx <= 0x8000; regx += 0x1000) {
1421 for (ipx = 0; ipx <= 0x400; ipx += 0x200)
1422 cvmx_write_csr(base + regx + ipx, 0);
1425 cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(coreid));
1428 static void octeon_irq_setup_secondary_ciu(void)
1430 octeon_irq_init_ciu_percpu();
1431 octeon_irq_percpu_enable();
1433 /* Enable the CIU lines */
1434 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1435 if (octeon_irq_use_ip4)
1436 set_c0_status(STATUSF_IP4);
1438 clear_c0_status(STATUSF_IP4);
1441 static void octeon_irq_setup_secondary_ciu2(void)
1443 octeon_irq_init_ciu2_percpu();
1444 octeon_irq_percpu_enable();
1446 /* Enable the CIU lines */
1447 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1448 if (octeon_irq_use_ip4)
1449 set_c0_status(STATUSF_IP4);
1451 clear_c0_status(STATUSF_IP4);
1454 static int __init octeon_irq_init_ciu(
1455 struct device_node *ciu_node, struct device_node *parent)
1458 struct irq_chip *chip;
1459 struct irq_chip *chip_edge;
1460 struct irq_chip *chip_mbox;
1461 struct irq_chip *chip_wd;
1462 struct irq_domain *ciu_domain = NULL;
1463 struct octeon_irq_ciu_domain_data *dd;
1465 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
1469 octeon_irq_init_ciu_percpu();
1470 octeon_irq_setup_secondary = octeon_irq_setup_secondary_ciu;
1472 octeon_irq_ip2 = octeon_irq_ip2_ciu;
1473 octeon_irq_ip3 = octeon_irq_ip3_ciu;
1474 if ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3())
1475 && !OCTEON_IS_MODEL(OCTEON_CN63XX)) {
1476 octeon_irq_ip4 = octeon_irq_ip4_ciu;
1478 octeon_irq_use_ip4 = true;
1480 octeon_irq_ip4 = octeon_irq_ip4_mask;
1482 octeon_irq_use_ip4 = false;
1484 if (OCTEON_IS_MODEL(OCTEON_CN58XX_PASS2_X) ||
1485 OCTEON_IS_MODEL(OCTEON_CN56XX_PASS2_X) ||
1486 OCTEON_IS_MODEL(OCTEON_CN52XX_PASS2_X) ||
1487 OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) {
1488 chip = &octeon_irq_chip_ciu_v2;
1489 chip_edge = &octeon_irq_chip_ciu_v2_edge;
1490 chip_mbox = &octeon_irq_chip_ciu_mbox_v2;
1491 chip_wd = &octeon_irq_chip_ciu_wd_v2;
1492 octeon_irq_gpio_chip = &octeon_irq_chip_ciu_gpio_v2;
1494 chip = &octeon_irq_chip_ciu;
1495 chip_edge = &octeon_irq_chip_ciu_edge;
1496 chip_mbox = &octeon_irq_chip_ciu_mbox;
1497 chip_wd = &octeon_irq_chip_ciu_wd;
1498 octeon_irq_gpio_chip = &octeon_irq_chip_ciu_gpio;
1500 octeon_irq_ciu_chip = chip;
1501 octeon_irq_ciu_chip_edge = chip_edge;
1504 octeon_irq_init_core();
1506 ciu_domain = irq_domain_add_tree(
1507 ciu_node, &octeon_irq_domain_ciu_ops, dd);
1508 irq_set_default_host(ciu_domain);
1511 for (i = 0; i < 16; i++) {
1512 r = octeon_irq_force_ciu_mapping(
1513 ciu_domain, i + OCTEON_IRQ_WORKQ0, 0, i + 0);
1518 r = octeon_irq_set_ciu_mapping(
1519 OCTEON_IRQ_MBOX0, 0, 32, 0, chip_mbox, handle_percpu_irq);
1522 r = octeon_irq_set_ciu_mapping(
1523 OCTEON_IRQ_MBOX1, 0, 33, 0, chip_mbox, handle_percpu_irq);
1527 for (i = 0; i < 4; i++) {
1528 r = octeon_irq_force_ciu_mapping(
1529 ciu_domain, i + OCTEON_IRQ_PCI_INT0, 0, i + 36);
1533 for (i = 0; i < 4; i++) {
1534 r = octeon_irq_force_ciu_mapping(
1535 ciu_domain, i + OCTEON_IRQ_PCI_MSI0, 0, i + 40);
1540 r = octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_TWSI, 0, 45);
1544 r = octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_RML, 0, 46);
1548 for (i = 0; i < 4; i++) {
1549 r = octeon_irq_force_ciu_mapping(
1550 ciu_domain, i + OCTEON_IRQ_TIMER0, 0, i + 52);
1555 r = octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_TWSI2, 0, 59);
1560 for (i = 0; i < 16; i++) {
1561 r = octeon_irq_set_ciu_mapping(
1562 i + OCTEON_IRQ_WDOG0, 1, i + 0, 0, chip_wd,
1568 /* Enable the CIU lines */
1569 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1570 if (octeon_irq_use_ip4)
1571 set_c0_status(STATUSF_IP4);
1573 clear_c0_status(STATUSF_IP4);
1580 static int __init octeon_irq_init_gpio(
1581 struct device_node *gpio_node, struct device_node *parent)
1583 struct octeon_irq_gpio_domain_data *gpiod;
1584 u32 interrupt_cells;
1585 unsigned int base_hwirq;
1588 r = of_property_read_u32(parent, "#interrupt-cells", &interrupt_cells);
1592 if (interrupt_cells == 1) {
1595 r = of_property_read_u32_index(gpio_node, "interrupts", 0, &v);
1597 pr_warn("No \"interrupts\" property.\n");
1601 } else if (interrupt_cells == 2) {
1604 r = of_property_read_u32_index(gpio_node, "interrupts", 0, &v0);
1606 pr_warn("No \"interrupts\" property.\n");
1609 r = of_property_read_u32_index(gpio_node, "interrupts", 1, &v1);
1611 pr_warn("No \"interrupts\" property.\n");
1614 base_hwirq = (v0 << 6) | v1;
1616 pr_warn("Bad \"#interrupt-cells\" property: %u\n",
1621 gpiod = kzalloc(sizeof(*gpiod), GFP_KERNEL);
1623 /* gpio domain host_data is the base hwirq number. */
1624 gpiod->base_hwirq = base_hwirq;
1625 irq_domain_add_linear(
1626 gpio_node, 16, &octeon_irq_domain_gpio_ops, gpiod);
1628 pr_warn("Cannot allocate memory for GPIO irq_domain.\n");
1633 * Clear the OF_POPULATED flag that was set by of_irq_init()
1634 * so that all GPIO devices will be probed.
1636 of_node_clear_flag(gpio_node, OF_POPULATED);
1641 * Watchdog interrupts are special. They are associated with a single
1642 * core, so we hardwire the affinity to that core.
1644 static void octeon_irq_ciu2_wd_enable(struct irq_data *data)
1648 int coreid = data->irq - OCTEON_IRQ_WDOG0;
1649 struct octeon_ciu_chip_data *cd;
1651 cd = irq_data_get_irq_chip_data(data);
1652 mask = 1ull << (cd->bit);
1654 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) +
1655 (0x1000ull * cd->line);
1656 cvmx_write_csr(en_addr, mask);
1660 static void octeon_irq_ciu2_enable(struct irq_data *data)
1664 int cpu = next_cpu_for_irq(data);
1665 int coreid = octeon_coreid_for_cpu(cpu);
1666 struct octeon_ciu_chip_data *cd;
1668 cd = irq_data_get_irq_chip_data(data);
1669 mask = 1ull << (cd->bit);
1671 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) +
1672 (0x1000ull * cd->line);
1673 cvmx_write_csr(en_addr, mask);
1676 static void octeon_irq_ciu2_enable_local(struct irq_data *data)
1680 int coreid = cvmx_get_core_num();
1681 struct octeon_ciu_chip_data *cd;
1683 cd = irq_data_get_irq_chip_data(data);
1684 mask = 1ull << (cd->bit);
1686 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) +
1687 (0x1000ull * cd->line);
1688 cvmx_write_csr(en_addr, mask);
1692 static void octeon_irq_ciu2_disable_local(struct irq_data *data)
1696 int coreid = cvmx_get_core_num();
1697 struct octeon_ciu_chip_data *cd;
1699 cd = irq_data_get_irq_chip_data(data);
1700 mask = 1ull << (cd->bit);
1702 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(coreid) +
1703 (0x1000ull * cd->line);
1704 cvmx_write_csr(en_addr, mask);
1708 static void octeon_irq_ciu2_ack(struct irq_data *data)
1712 int coreid = cvmx_get_core_num();
1713 struct octeon_ciu_chip_data *cd;
1715 cd = irq_data_get_irq_chip_data(data);
1716 mask = 1ull << (cd->bit);
1718 en_addr = CVMX_CIU2_RAW_PPX_IP2_WRKQ(coreid) + (0x1000ull * cd->line);
1719 cvmx_write_csr(en_addr, mask);
1723 static void octeon_irq_ciu2_disable_all(struct irq_data *data)
1727 struct octeon_ciu_chip_data *cd;
1729 cd = irq_data_get_irq_chip_data(data);
1730 mask = 1ull << (cd->bit);
1732 for_each_online_cpu(cpu) {
1733 u64 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(
1734 octeon_coreid_for_cpu(cpu)) + (0x1000ull * cd->line);
1735 cvmx_write_csr(en_addr, mask);
1739 static void octeon_irq_ciu2_mbox_enable_all(struct irq_data *data)
1744 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1746 for_each_online_cpu(cpu) {
1747 u64 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1S(
1748 octeon_coreid_for_cpu(cpu));
1749 cvmx_write_csr(en_addr, mask);
1753 static void octeon_irq_ciu2_mbox_disable_all(struct irq_data *data)
1758 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1760 for_each_online_cpu(cpu) {
1761 u64 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1C(
1762 octeon_coreid_for_cpu(cpu));
1763 cvmx_write_csr(en_addr, mask);
1767 static void octeon_irq_ciu2_mbox_enable_local(struct irq_data *data)
1771 int coreid = cvmx_get_core_num();
1773 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1774 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1S(coreid);
1775 cvmx_write_csr(en_addr, mask);
1778 static void octeon_irq_ciu2_mbox_disable_local(struct irq_data *data)
1782 int coreid = cvmx_get_core_num();
1784 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1785 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1C(coreid);
1786 cvmx_write_csr(en_addr, mask);
1790 static int octeon_irq_ciu2_set_affinity(struct irq_data *data,
1791 const struct cpumask *dest, bool force)
1794 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
1796 struct octeon_ciu_chip_data *cd;
1801 cd = irq_data_get_irq_chip_data(data);
1802 mask = 1ull << cd->bit;
1804 for_each_online_cpu(cpu) {
1806 if (cpumask_test_cpu(cpu, dest) && enable_one) {
1808 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(
1809 octeon_coreid_for_cpu(cpu)) +
1810 (0x1000ull * cd->line);
1812 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(
1813 octeon_coreid_for_cpu(cpu)) +
1814 (0x1000ull * cd->line);
1816 cvmx_write_csr(en_addr, mask);
1823 static void octeon_irq_ciu2_enable_gpio(struct irq_data *data)
1825 octeon_irq_gpio_setup(data);
1826 octeon_irq_ciu2_enable(data);
1829 static void octeon_irq_ciu2_disable_gpio(struct irq_data *data)
1831 struct octeon_ciu_chip_data *cd;
1833 cd = irq_data_get_irq_chip_data(data);
1835 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd->gpio_line), 0);
1837 octeon_irq_ciu2_disable_all(data);
1840 static struct irq_chip octeon_irq_chip_ciu2 = {
1842 .irq_enable = octeon_irq_ciu2_enable,
1843 .irq_disable = octeon_irq_ciu2_disable_all,
1844 .irq_mask = octeon_irq_ciu2_disable_local,
1845 .irq_unmask = octeon_irq_ciu2_enable,
1847 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1848 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1852 static struct irq_chip octeon_irq_chip_ciu2_edge = {
1854 .irq_enable = octeon_irq_ciu2_enable,
1855 .irq_disable = octeon_irq_ciu2_disable_all,
1856 .irq_ack = octeon_irq_ciu2_ack,
1857 .irq_mask = octeon_irq_ciu2_disable_local,
1858 .irq_unmask = octeon_irq_ciu2_enable,
1860 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1861 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1865 static struct irq_chip octeon_irq_chip_ciu2_mbox = {
1867 .irq_enable = octeon_irq_ciu2_mbox_enable_all,
1868 .irq_disable = octeon_irq_ciu2_mbox_disable_all,
1869 .irq_ack = octeon_irq_ciu2_mbox_disable_local,
1870 .irq_eoi = octeon_irq_ciu2_mbox_enable_local,
1872 .irq_cpu_online = octeon_irq_ciu2_mbox_enable_local,
1873 .irq_cpu_offline = octeon_irq_ciu2_mbox_disable_local,
1874 .flags = IRQCHIP_ONOFFLINE_ENABLED,
1877 static struct irq_chip octeon_irq_chip_ciu2_wd = {
1879 .irq_enable = octeon_irq_ciu2_wd_enable,
1880 .irq_disable = octeon_irq_ciu2_disable_all,
1881 .irq_mask = octeon_irq_ciu2_disable_local,
1882 .irq_unmask = octeon_irq_ciu2_enable_local,
1885 static struct irq_chip octeon_irq_chip_ciu2_gpio = {
1887 .irq_enable = octeon_irq_ciu2_enable_gpio,
1888 .irq_disable = octeon_irq_ciu2_disable_gpio,
1889 .irq_ack = octeon_irq_ciu_gpio_ack,
1890 .irq_mask = octeon_irq_ciu2_disable_local,
1891 .irq_unmask = octeon_irq_ciu2_enable,
1892 .irq_set_type = octeon_irq_ciu_gpio_set_type,
1894 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1895 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1897 .flags = IRQCHIP_SET_TYPE_MASKED,
1900 static int octeon_irq_ciu2_xlat(struct irq_domain *d,
1901 struct device_node *node,
1903 unsigned int intsize,
1904 unsigned long *out_hwirq,
1905 unsigned int *out_type)
1907 unsigned int ciu, bit;
1912 *out_hwirq = (ciu << 6) | bit;
1918 static bool octeon_irq_ciu2_is_edge(unsigned int line, unsigned int bit)
1922 if (line == 3) /* MIO */
1924 case 2: /* IPD_DRP */
1925 case 8 ... 11: /* Timers */
1932 else if (line == 6) /* PKT */
1934 case 52 ... 53: /* ILK_DRP */
1935 case 8 ... 12: /* GMX_DRP */
1944 static int octeon_irq_ciu2_map(struct irq_domain *d,
1945 unsigned int virq, irq_hw_number_t hw)
1947 unsigned int line = hw >> 6;
1948 unsigned int bit = hw & 63;
1951 * Don't map irq if it is reserved for GPIO.
1952 * (Line 7 are the GPIO lines.)
1957 if (line > 7 || octeon_irq_ciu_to_irq[line][bit] != 0)
1960 if (octeon_irq_ciu2_is_edge(line, bit))
1961 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1962 &octeon_irq_chip_ciu2_edge,
1965 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1966 &octeon_irq_chip_ciu2,
1972 static struct irq_domain_ops octeon_irq_domain_ciu2_ops = {
1973 .map = octeon_irq_ciu2_map,
1974 .unmap = octeon_irq_free_cd,
1975 .xlate = octeon_irq_ciu2_xlat,
1978 static void octeon_irq_ciu2(void)
1983 u64 src_reg, src, sum;
1984 const unsigned long core_id = cvmx_get_core_num();
1986 sum = cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(core_id)) & 0xfful;
1991 line = fls64(sum) - 1;
1992 src_reg = CVMX_CIU2_SRC_PPX_IP2_WRKQ(core_id) + (0x1000 * line);
1993 src = cvmx_read_csr(src_reg);
1998 bit = fls64(src) - 1;
1999 irq = octeon_irq_ciu_to_irq[line][bit];
2007 spurious_interrupt();
2009 /* CN68XX pass 1.x has an errata that accessing the ACK registers
2010 can stop interrupts from propagating */
2011 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
2012 cvmx_read_csr(CVMX_CIU2_INTR_CIU_READY);
2014 cvmx_read_csr(CVMX_CIU2_ACK_PPX_IP2(core_id));
2018 static void octeon_irq_ciu2_mbox(void)
2022 const unsigned long core_id = cvmx_get_core_num();
2023 u64 sum = cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP3(core_id)) >> 60;
2028 line = fls64(sum) - 1;
2030 do_IRQ(OCTEON_IRQ_MBOX0 + line);
2034 spurious_interrupt();
2036 /* CN68XX pass 1.x has an errata that accessing the ACK registers
2037 can stop interrupts from propagating */
2038 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
2039 cvmx_read_csr(CVMX_CIU2_INTR_CIU_READY);
2041 cvmx_read_csr(CVMX_CIU2_ACK_PPX_IP3(core_id));
2045 static int __init octeon_irq_init_ciu2(
2046 struct device_node *ciu_node, struct device_node *parent)
2049 struct irq_domain *ciu_domain = NULL;
2051 octeon_irq_init_ciu2_percpu();
2052 octeon_irq_setup_secondary = octeon_irq_setup_secondary_ciu2;
2054 octeon_irq_gpio_chip = &octeon_irq_chip_ciu2_gpio;
2055 octeon_irq_ip2 = octeon_irq_ciu2;
2056 octeon_irq_ip3 = octeon_irq_ciu2_mbox;
2057 octeon_irq_ip4 = octeon_irq_ip4_mask;
2060 octeon_irq_init_core();
2062 ciu_domain = irq_domain_add_tree(
2063 ciu_node, &octeon_irq_domain_ciu2_ops, NULL);
2064 irq_set_default_host(ciu_domain);
2067 for (i = 0; i < 64; i++) {
2068 r = octeon_irq_force_ciu_mapping(
2069 ciu_domain, i + OCTEON_IRQ_WORKQ0, 0, i);
2074 for (i = 0; i < 32; i++) {
2075 r = octeon_irq_set_ciu_mapping(i + OCTEON_IRQ_WDOG0, 1, i, 0,
2076 &octeon_irq_chip_ciu2_wd, handle_level_irq);
2081 for (i = 0; i < 4; i++) {
2082 r = octeon_irq_force_ciu_mapping(
2083 ciu_domain, i + OCTEON_IRQ_TIMER0, 3, i + 8);
2088 for (i = 0; i < 4; i++) {
2089 r = octeon_irq_force_ciu_mapping(
2090 ciu_domain, i + OCTEON_IRQ_PCI_INT0, 4, i);
2095 for (i = 0; i < 4; i++) {
2096 r = octeon_irq_force_ciu_mapping(
2097 ciu_domain, i + OCTEON_IRQ_PCI_MSI0, 4, i + 8);
2102 irq_set_chip_and_handler(OCTEON_IRQ_MBOX0, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
2103 irq_set_chip_and_handler(OCTEON_IRQ_MBOX1, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
2104 irq_set_chip_and_handler(OCTEON_IRQ_MBOX2, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
2105 irq_set_chip_and_handler(OCTEON_IRQ_MBOX3, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
2107 /* Enable the CIU lines */
2108 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
2109 clear_c0_status(STATUSF_IP4);
2115 struct octeon_irq_cib_host_data {
2116 raw_spinlock_t lock;
2122 struct octeon_irq_cib_chip_data {
2123 struct octeon_irq_cib_host_data *host_data;
2127 static void octeon_irq_cib_enable(struct irq_data *data)
2129 unsigned long flags;
2131 struct octeon_irq_cib_chip_data *cd = irq_data_get_irq_chip_data(data);
2132 struct octeon_irq_cib_host_data *host_data = cd->host_data;
2134 raw_spin_lock_irqsave(&host_data->lock, flags);
2135 en = cvmx_read_csr(host_data->en_reg);
2136 en |= 1ull << cd->bit;
2137 cvmx_write_csr(host_data->en_reg, en);
2138 raw_spin_unlock_irqrestore(&host_data->lock, flags);
2141 static void octeon_irq_cib_disable(struct irq_data *data)
2143 unsigned long flags;
2145 struct octeon_irq_cib_chip_data *cd = irq_data_get_irq_chip_data(data);
2146 struct octeon_irq_cib_host_data *host_data = cd->host_data;
2148 raw_spin_lock_irqsave(&host_data->lock, flags);
2149 en = cvmx_read_csr(host_data->en_reg);
2150 en &= ~(1ull << cd->bit);
2151 cvmx_write_csr(host_data->en_reg, en);
2152 raw_spin_unlock_irqrestore(&host_data->lock, flags);
2155 static int octeon_irq_cib_set_type(struct irq_data *data, unsigned int t)
2157 irqd_set_trigger_type(data, t);
2158 return IRQ_SET_MASK_OK;
2161 static struct irq_chip octeon_irq_chip_cib = {
2163 .irq_enable = octeon_irq_cib_enable,
2164 .irq_disable = octeon_irq_cib_disable,
2165 .irq_mask = octeon_irq_cib_disable,
2166 .irq_unmask = octeon_irq_cib_enable,
2167 .irq_set_type = octeon_irq_cib_set_type,
2170 static int octeon_irq_cib_xlat(struct irq_domain *d,
2171 struct device_node *node,
2173 unsigned int intsize,
2174 unsigned long *out_hwirq,
2175 unsigned int *out_type)
2177 unsigned int type = 0;
2183 case 0: /* unofficial value, but we might as well let it work. */
2184 case 4: /* official value for level triggering. */
2185 *out_type = IRQ_TYPE_LEVEL_HIGH;
2187 case 1: /* official value for edge triggering. */
2188 *out_type = IRQ_TYPE_EDGE_RISING;
2190 default: /* Nothing else is acceptable. */
2194 *out_hwirq = intspec[0];
2199 static int octeon_irq_cib_map(struct irq_domain *d,
2200 unsigned int virq, irq_hw_number_t hw)
2202 struct octeon_irq_cib_host_data *host_data = d->host_data;
2203 struct octeon_irq_cib_chip_data *cd;
2205 if (hw >= host_data->max_bits) {
2206 pr_err("ERROR: %s mapping %u is to big!\n",
2207 irq_domain_get_of_node(d)->name, (unsigned)hw);
2211 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
2215 cd->host_data = host_data;
2218 irq_set_chip_and_handler(virq, &octeon_irq_chip_cib,
2220 irq_set_chip_data(virq, cd);
2224 static struct irq_domain_ops octeon_irq_domain_cib_ops = {
2225 .map = octeon_irq_cib_map,
2226 .unmap = octeon_irq_free_cd,
2227 .xlate = octeon_irq_cib_xlat,
2230 /* Chain to real handler. */
2231 static irqreturn_t octeon_irq_cib_handler(int my_irq, void *data)
2238 struct irq_domain *cib_domain = data;
2239 struct octeon_irq_cib_host_data *host_data = cib_domain->host_data;
2241 en = cvmx_read_csr(host_data->en_reg);
2242 raw = cvmx_read_csr(host_data->raw_reg);
2246 for (i = 0; i < host_data->max_bits; i++) {
2247 if ((bits & 1ull << i) == 0)
2249 irq = irq_find_mapping(cib_domain, i);
2251 unsigned long flags;
2253 pr_err("ERROR: CIB bit %d@%llx IRQ unhandled, disabling\n",
2254 i, host_data->raw_reg);
2255 raw_spin_lock_irqsave(&host_data->lock, flags);
2256 en = cvmx_read_csr(host_data->en_reg);
2258 cvmx_write_csr(host_data->en_reg, en);
2259 cvmx_write_csr(host_data->raw_reg, 1ull << i);
2260 raw_spin_unlock_irqrestore(&host_data->lock, flags);
2262 struct irq_desc *desc = irq_to_desc(irq);
2263 struct irq_data *irq_data = irq_desc_get_irq_data(desc);
2264 /* If edge, acknowledge the bit we will be sending. */
2265 if (irqd_get_trigger_type(irq_data) &
2267 cvmx_write_csr(host_data->raw_reg, 1ull << i);
2268 generic_handle_irq_desc(desc);
2275 static int __init octeon_irq_init_cib(struct device_node *ciu_node,
2276 struct device_node *parent)
2280 struct octeon_irq_cib_host_data *host_data;
2283 struct irq_domain *cib_domain;
2285 parent_irq = irq_of_parse_and_map(ciu_node, 0);
2287 pr_err("ERROR: Couldn't acquire parent_irq for %s\n",
2292 host_data = kzalloc(sizeof(*host_data), GFP_KERNEL);
2295 raw_spin_lock_init(&host_data->lock);
2297 addr = of_get_address(ciu_node, 0, NULL, NULL);
2299 pr_err("ERROR: Couldn't acquire reg(0) %s\n", ciu_node->name);
2302 host_data->raw_reg = (u64)phys_to_virt(
2303 of_translate_address(ciu_node, addr));
2305 addr = of_get_address(ciu_node, 1, NULL, NULL);
2307 pr_err("ERROR: Couldn't acquire reg(1) %s\n", ciu_node->name);
2310 host_data->en_reg = (u64)phys_to_virt(
2311 of_translate_address(ciu_node, addr));
2313 r = of_property_read_u32(ciu_node, "cavium,max-bits", &val);
2315 pr_err("ERROR: Couldn't read cavium,max-bits from %s\n",
2319 host_data->max_bits = val;
2321 cib_domain = irq_domain_add_linear(ciu_node, host_data->max_bits,
2322 &octeon_irq_domain_cib_ops,
2325 pr_err("ERROR: Couldn't irq_domain_add_linear()\n");
2329 cvmx_write_csr(host_data->en_reg, 0); /* disable all IRQs */
2330 cvmx_write_csr(host_data->raw_reg, ~0); /* ack any outstanding */
2332 r = request_irq(parent_irq, octeon_irq_cib_handler,
2333 IRQF_NO_THREAD, "cib", cib_domain);
2335 pr_err("request_irq cib failed %d\n", r);
2338 pr_info("CIB interrupt controller probed: %llx %d\n",
2339 host_data->raw_reg, host_data->max_bits);
2343 int octeon_irq_ciu3_xlat(struct irq_domain *d,
2344 struct device_node *node,
2346 unsigned int intsize,
2347 unsigned long *out_hwirq,
2348 unsigned int *out_type)
2350 struct octeon_ciu3_info *ciu3_info = d->host_data;
2351 unsigned int hwirq, type, intsn_major;
2352 union cvmx_ciu3_iscx_ctl isc;
2359 if (hwirq >= (1 << 20))
2362 intsn_major = hwirq >> 12;
2363 switch (intsn_major) {
2364 case 0x04: /* Software handled separately. */
2370 isc.u64 = cvmx_read_csr(ciu3_info->ciu3_addr + CIU3_ISC_CTL(hwirq));
2375 case 4: /* official value for level triggering. */
2376 *out_type = IRQ_TYPE_LEVEL_HIGH;
2378 case 0: /* unofficial value, but we might as well let it work. */
2379 case 1: /* official value for edge triggering. */
2380 *out_type = IRQ_TYPE_EDGE_RISING;
2382 default: /* Nothing else is acceptable. */
2391 void octeon_irq_ciu3_enable(struct irq_data *data)
2394 union cvmx_ciu3_iscx_ctl isc_ctl;
2395 union cvmx_ciu3_iscx_w1c isc_w1c;
2398 struct octeon_ciu_chip_data *cd;
2400 cpu = next_cpu_for_irq(data);
2402 cd = irq_data_get_irq_chip_data(data);
2406 cvmx_write_csr(cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn), isc_w1c.u64);
2408 isc_ctl_addr = cd->ciu3_addr + CIU3_ISC_CTL(cd->intsn);
2411 isc_ctl.s.idt = per_cpu(octeon_irq_ciu3_idt_ip2, cpu);
2412 cvmx_write_csr(isc_ctl_addr, isc_ctl.u64);
2413 cvmx_read_csr(isc_ctl_addr);
2416 void octeon_irq_ciu3_disable(struct irq_data *data)
2419 union cvmx_ciu3_iscx_w1c isc_w1c;
2421 struct octeon_ciu_chip_data *cd;
2423 cd = irq_data_get_irq_chip_data(data);
2428 isc_ctl_addr = cd->ciu3_addr + CIU3_ISC_CTL(cd->intsn);
2429 cvmx_write_csr(cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn), isc_w1c.u64);
2430 cvmx_write_csr(isc_ctl_addr, 0);
2431 cvmx_read_csr(isc_ctl_addr);
2434 void octeon_irq_ciu3_ack(struct irq_data *data)
2437 union cvmx_ciu3_iscx_w1c isc_w1c;
2438 struct octeon_ciu_chip_data *cd;
2439 u32 trigger_type = irqd_get_trigger_type(data);
2442 * We use a single irq_chip, so we have to do nothing to ack a
2445 if (!(trigger_type & IRQ_TYPE_EDGE_BOTH))
2448 cd = irq_data_get_irq_chip_data(data);
2453 isc_w1c_addr = cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn);
2454 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2455 cvmx_read_csr(isc_w1c_addr);
2458 void octeon_irq_ciu3_mask(struct irq_data *data)
2460 union cvmx_ciu3_iscx_w1c isc_w1c;
2462 struct octeon_ciu_chip_data *cd;
2464 cd = irq_data_get_irq_chip_data(data);
2469 isc_w1c_addr = cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn);
2470 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2471 cvmx_read_csr(isc_w1c_addr);
2474 void octeon_irq_ciu3_mask_ack(struct irq_data *data)
2476 union cvmx_ciu3_iscx_w1c isc_w1c;
2478 struct octeon_ciu_chip_data *cd;
2479 u32 trigger_type = irqd_get_trigger_type(data);
2481 cd = irq_data_get_irq_chip_data(data);
2487 * We use a single irq_chip, so only ack an edge (!level)
2490 if (trigger_type & IRQ_TYPE_EDGE_BOTH)
2493 isc_w1c_addr = cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn);
2494 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2495 cvmx_read_csr(isc_w1c_addr);
2499 int octeon_irq_ciu3_set_affinity(struct irq_data *data,
2500 const struct cpumask *dest, bool force)
2502 union cvmx_ciu3_iscx_ctl isc_ctl;
2503 union cvmx_ciu3_iscx_w1c isc_w1c;
2506 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
2507 struct octeon_ciu_chip_data *cd = irq_data_get_irq_chip_data(data);
2509 if (!cpumask_subset(dest, cpumask_of_node(cd->ciu_node)))
2513 return IRQ_SET_MASK_OK;
2515 cd = irq_data_get_irq_chip_data(data);
2516 cpu = cpumask_first(dest);
2517 if (cpu >= nr_cpu_ids)
2518 cpu = smp_processor_id();
2519 cd->current_cpu = cpu;
2523 cvmx_write_csr(cd->ciu3_addr + CIU3_ISC_W1C(cd->intsn), isc_w1c.u64);
2525 isc_ctl_addr = cd->ciu3_addr + CIU3_ISC_CTL(cd->intsn);
2528 isc_ctl.s.idt = per_cpu(octeon_irq_ciu3_idt_ip2, cpu);
2529 cvmx_write_csr(isc_ctl_addr, isc_ctl.u64);
2530 cvmx_read_csr(isc_ctl_addr);
2532 return IRQ_SET_MASK_OK;
2536 static struct irq_chip octeon_irq_chip_ciu3 = {
2538 .irq_startup = edge_startup,
2539 .irq_enable = octeon_irq_ciu3_enable,
2540 .irq_disable = octeon_irq_ciu3_disable,
2541 .irq_ack = octeon_irq_ciu3_ack,
2542 .irq_mask = octeon_irq_ciu3_mask,
2543 .irq_mask_ack = octeon_irq_ciu3_mask_ack,
2544 .irq_unmask = octeon_irq_ciu3_enable,
2545 .irq_set_type = octeon_irq_ciu_set_type,
2547 .irq_set_affinity = octeon_irq_ciu3_set_affinity,
2548 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
2552 int octeon_irq_ciu3_mapx(struct irq_domain *d, unsigned int virq,
2553 irq_hw_number_t hw, struct irq_chip *chip)
2555 struct octeon_ciu3_info *ciu3_info = d->host_data;
2556 struct octeon_ciu_chip_data *cd = kzalloc_node(sizeof(*cd), GFP_KERNEL,
2561 cd->current_cpu = -1;
2562 cd->ciu3_addr = ciu3_info->ciu3_addr;
2563 cd->ciu_node = ciu3_info->node;
2564 irq_set_chip_and_handler(virq, chip, handle_edge_irq);
2565 irq_set_chip_data(virq, cd);
2570 static int octeon_irq_ciu3_map(struct irq_domain *d,
2571 unsigned int virq, irq_hw_number_t hw)
2573 return octeon_irq_ciu3_mapx(d, virq, hw, &octeon_irq_chip_ciu3);
2576 static struct irq_domain_ops octeon_dflt_domain_ciu3_ops = {
2577 .map = octeon_irq_ciu3_map,
2578 .unmap = octeon_irq_free_cd,
2579 .xlate = octeon_irq_ciu3_xlat,
2582 static void octeon_irq_ciu3_ip2(void)
2584 union cvmx_ciu3_destx_pp_int dest_pp_int;
2585 struct octeon_ciu3_info *ciu3_info;
2588 ciu3_info = __this_cpu_read(octeon_ciu3_info);
2589 ciu3_addr = ciu3_info->ciu3_addr;
2591 dest_pp_int.u64 = cvmx_read_csr(ciu3_addr + CIU3_DEST_PP_INT(3 * cvmx_get_local_core_num()));
2593 if (likely(dest_pp_int.s.intr)) {
2594 irq_hw_number_t intsn = dest_pp_int.s.intsn;
2596 struct irq_domain *domain;
2597 /* Get the domain to use from the major block */
2598 int block = intsn >> 12;
2601 domain = ciu3_info->domain[block];
2602 if (ciu3_info->intsn2hw[block])
2603 hw = ciu3_info->intsn2hw[block](domain, intsn);
2607 ret = handle_domain_irq(domain, hw, NULL);
2609 union cvmx_ciu3_iscx_w1c isc_w1c;
2610 u64 isc_w1c_addr = ciu3_addr + CIU3_ISC_W1C(intsn);
2614 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2615 cvmx_read_csr(isc_w1c_addr);
2616 spurious_interrupt();
2619 spurious_interrupt();
2624 * 10 mbox per core starting from zero.
2625 * Base mbox is core * 10
2627 static unsigned int octeon_irq_ciu3_base_mbox_intsn(int core)
2629 /* SW (mbox) are 0x04 in bits 12..19 */
2630 return 0x04000 + CIU3_MBOX_PER_CORE * core;
2633 static unsigned int octeon_irq_ciu3_mbox_intsn_for_core(int core, unsigned int mbox)
2635 return octeon_irq_ciu3_base_mbox_intsn(core) + mbox;
2638 static unsigned int octeon_irq_ciu3_mbox_intsn_for_cpu(int cpu, unsigned int mbox)
2640 int local_core = octeon_coreid_for_cpu(cpu) & 0x3f;
2642 return octeon_irq_ciu3_mbox_intsn_for_core(local_core, mbox);
2645 static void octeon_irq_ciu3_mbox(void)
2647 union cvmx_ciu3_destx_pp_int dest_pp_int;
2648 struct octeon_ciu3_info *ciu3_info;
2650 int core = cvmx_get_local_core_num();
2652 ciu3_info = __this_cpu_read(octeon_ciu3_info);
2653 ciu3_addr = ciu3_info->ciu3_addr;
2655 dest_pp_int.u64 = cvmx_read_csr(ciu3_addr + CIU3_DEST_PP_INT(1 + 3 * core));
2657 if (likely(dest_pp_int.s.intr)) {
2658 irq_hw_number_t intsn = dest_pp_int.s.intsn;
2659 int mbox = intsn - octeon_irq_ciu3_base_mbox_intsn(core);
2661 if (likely(mbox >= 0 && mbox < CIU3_MBOX_PER_CORE)) {
2662 do_IRQ(mbox + OCTEON_IRQ_MBOX0);
2664 union cvmx_ciu3_iscx_w1c isc_w1c;
2665 u64 isc_w1c_addr = ciu3_addr + CIU3_ISC_W1C(intsn);
2669 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2670 cvmx_read_csr(isc_w1c_addr);
2671 spurious_interrupt();
2674 spurious_interrupt();
2678 void octeon_ciu3_mbox_send(int cpu, unsigned int mbox)
2680 struct octeon_ciu3_info *ciu3_info;
2682 union cvmx_ciu3_iscx_w1s isc_w1s;
2685 if (WARN_ON_ONCE(mbox >= CIU3_MBOX_PER_CORE))
2688 intsn = octeon_irq_ciu3_mbox_intsn_for_cpu(cpu, mbox);
2689 ciu3_info = per_cpu(octeon_ciu3_info, cpu);
2690 isc_w1s_addr = ciu3_info->ciu3_addr + CIU3_ISC_W1S(intsn);
2695 cvmx_write_csr(isc_w1s_addr, isc_w1s.u64);
2696 cvmx_read_csr(isc_w1s_addr);
2699 static void octeon_irq_ciu3_mbox_set_enable(struct irq_data *data, int cpu, bool en)
2701 struct octeon_ciu3_info *ciu3_info;
2703 u64 isc_ctl_addr, isc_w1c_addr;
2704 union cvmx_ciu3_iscx_ctl isc_ctl;
2705 unsigned int mbox = data->irq - OCTEON_IRQ_MBOX0;
2707 intsn = octeon_irq_ciu3_mbox_intsn_for_cpu(cpu, mbox);
2708 ciu3_info = per_cpu(octeon_ciu3_info, cpu);
2709 isc_w1c_addr = ciu3_info->ciu3_addr + CIU3_ISC_W1C(intsn);
2710 isc_ctl_addr = ciu3_info->ciu3_addr + CIU3_ISC_CTL(intsn);
2715 cvmx_write_csr(isc_w1c_addr, isc_ctl.u64);
2716 cvmx_write_csr(isc_ctl_addr, 0);
2718 unsigned int idt = per_cpu(octeon_irq_ciu3_idt_ip3, cpu);
2722 isc_ctl.s.idt = idt;
2723 cvmx_write_csr(isc_ctl_addr, isc_ctl.u64);
2725 cvmx_read_csr(isc_ctl_addr);
2728 static void octeon_irq_ciu3_mbox_enable(struct irq_data *data)
2731 unsigned int mbox = data->irq - OCTEON_IRQ_MBOX0;
2733 WARN_ON(mbox >= CIU3_MBOX_PER_CORE);
2735 for_each_online_cpu(cpu)
2736 octeon_irq_ciu3_mbox_set_enable(data, cpu, true);
2739 static void octeon_irq_ciu3_mbox_disable(struct irq_data *data)
2742 unsigned int mbox = data->irq - OCTEON_IRQ_MBOX0;
2744 WARN_ON(mbox >= CIU3_MBOX_PER_CORE);
2746 for_each_online_cpu(cpu)
2747 octeon_irq_ciu3_mbox_set_enable(data, cpu, false);
2750 static void octeon_irq_ciu3_mbox_ack(struct irq_data *data)
2752 struct octeon_ciu3_info *ciu3_info;
2755 union cvmx_ciu3_iscx_w1c isc_w1c;
2756 unsigned int mbox = data->irq - OCTEON_IRQ_MBOX0;
2758 intsn = octeon_irq_ciu3_mbox_intsn_for_core(cvmx_get_local_core_num(), mbox);
2763 ciu3_info = __this_cpu_read(octeon_ciu3_info);
2764 isc_w1c_addr = ciu3_info->ciu3_addr + CIU3_ISC_W1C(intsn);
2765 cvmx_write_csr(isc_w1c_addr, isc_w1c.u64);
2766 cvmx_read_csr(isc_w1c_addr);
2769 static void octeon_irq_ciu3_mbox_cpu_online(struct irq_data *data)
2771 octeon_irq_ciu3_mbox_set_enable(data, smp_processor_id(), true);
2774 static void octeon_irq_ciu3_mbox_cpu_offline(struct irq_data *data)
2776 octeon_irq_ciu3_mbox_set_enable(data, smp_processor_id(), false);
2779 static int octeon_irq_ciu3_alloc_resources(struct octeon_ciu3_info *ciu3_info)
2781 u64 b = ciu3_info->ciu3_addr;
2782 int idt_ip2, idt_ip3, idt_ip4;
2784 int core = cvmx_get_local_core_num();
2787 __this_cpu_write(octeon_ciu3_info, ciu3_info);
2790 * 4 idt per core starting from 1 because zero is reserved.
2791 * Base idt per core is 4 * core + 1
2793 idt_ip2 = core * 4 + 1;
2794 idt_ip3 = core * 4 + 2;
2795 idt_ip4 = core * 4 + 3;
2796 unused_idt2 = core * 4 + 4;
2797 __this_cpu_write(octeon_irq_ciu3_idt_ip2, idt_ip2);
2798 __this_cpu_write(octeon_irq_ciu3_idt_ip3, idt_ip3);
2800 /* ip2 interrupts for this CPU */
2801 cvmx_write_csr(b + CIU3_IDT_CTL(idt_ip2), 0);
2802 cvmx_write_csr(b + CIU3_IDT_PP(idt_ip2, 0), 1ull << core);
2803 cvmx_write_csr(b + CIU3_IDT_IO(idt_ip2), 0);
2805 /* ip3 interrupts for this CPU */
2806 cvmx_write_csr(b + CIU3_IDT_CTL(idt_ip3), 1);
2807 cvmx_write_csr(b + CIU3_IDT_PP(idt_ip3, 0), 1ull << core);
2808 cvmx_write_csr(b + CIU3_IDT_IO(idt_ip3), 0);
2810 /* ip4 interrupts for this CPU */
2811 cvmx_write_csr(b + CIU3_IDT_CTL(idt_ip4), 2);
2812 cvmx_write_csr(b + CIU3_IDT_PP(idt_ip4, 0), 0);
2813 cvmx_write_csr(b + CIU3_IDT_IO(idt_ip4), 0);
2815 cvmx_write_csr(b + CIU3_IDT_CTL(unused_idt2), 0);
2816 cvmx_write_csr(b + CIU3_IDT_PP(unused_idt2, 0), 0);
2817 cvmx_write_csr(b + CIU3_IDT_IO(unused_idt2), 0);
2819 for (i = 0; i < CIU3_MBOX_PER_CORE; i++) {
2820 unsigned int intsn = octeon_irq_ciu3_mbox_intsn_for_core(core, i);
2822 cvmx_write_csr(b + CIU3_ISC_W1C(intsn), 2);
2823 cvmx_write_csr(b + CIU3_ISC_CTL(intsn), 0);
2829 static void octeon_irq_setup_secondary_ciu3(void)
2831 struct octeon_ciu3_info *ciu3_info;
2833 ciu3_info = octeon_ciu3_info_per_node[cvmx_get_node_num()];
2834 octeon_irq_ciu3_alloc_resources(ciu3_info);
2837 /* Enable the CIU lines */
2838 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
2839 if (octeon_irq_use_ip4)
2840 set_c0_status(STATUSF_IP4);
2842 clear_c0_status(STATUSF_IP4);
2845 static struct irq_chip octeon_irq_chip_ciu3_mbox = {
2847 .irq_enable = octeon_irq_ciu3_mbox_enable,
2848 .irq_disable = octeon_irq_ciu3_mbox_disable,
2849 .irq_ack = octeon_irq_ciu3_mbox_ack,
2851 .irq_cpu_online = octeon_irq_ciu3_mbox_cpu_online,
2852 .irq_cpu_offline = octeon_irq_ciu3_mbox_cpu_offline,
2853 .flags = IRQCHIP_ONOFFLINE_ENABLED,
2856 static int __init octeon_irq_init_ciu3(struct device_node *ciu_node,
2857 struct device_node *parent)
2861 struct irq_domain *domain;
2862 struct octeon_ciu3_info *ciu3_info;
2863 const __be32 *zero_addr;
2865 union cvmx_ciu3_const consts;
2867 node = 0; /* of_node_to_nid(ciu_node); */
2868 ciu3_info = kzalloc_node(sizeof(*ciu3_info), GFP_KERNEL, node);
2873 zero_addr = of_get_address(ciu_node, 0, NULL, NULL);
2874 if (WARN_ON(!zero_addr))
2877 base_addr = of_translate_address(ciu_node, zero_addr);
2878 base_addr = (u64)phys_to_virt(base_addr);
2880 ciu3_info->ciu3_addr = base_addr;
2881 ciu3_info->node = node;
2883 consts.u64 = cvmx_read_csr(base_addr + CIU3_CONST);
2885 octeon_irq_setup_secondary = octeon_irq_setup_secondary_ciu3;
2887 octeon_irq_ip2 = octeon_irq_ciu3_ip2;
2888 octeon_irq_ip3 = octeon_irq_ciu3_mbox;
2889 octeon_irq_ip4 = octeon_irq_ip4_mask;
2891 if (node == cvmx_get_node_num()) {
2893 octeon_irq_init_core();
2895 /* Only do per CPU things if it is the CIU of the boot node. */
2896 i = irq_alloc_descs_from(OCTEON_IRQ_MBOX0, 8, node);
2899 for (i = 0; i < 8; i++)
2900 irq_set_chip_and_handler(i + OCTEON_IRQ_MBOX0,
2901 &octeon_irq_chip_ciu3_mbox, handle_percpu_irq);
2905 * Initialize all domains to use the default domain. Specific major
2906 * blocks will overwrite the default domain as needed.
2908 domain = irq_domain_add_tree(ciu_node, &octeon_dflt_domain_ciu3_ops,
2910 for (i = 0; i < MAX_CIU3_DOMAINS; i++)
2911 ciu3_info->domain[i] = domain;
2913 octeon_ciu3_info_per_node[node] = ciu3_info;
2915 if (node == cvmx_get_node_num()) {
2916 /* Only do per CPU things if it is the CIU of the boot node. */
2917 octeon_irq_ciu3_alloc_resources(ciu3_info);
2919 irq_set_default_host(domain);
2921 octeon_irq_use_ip4 = false;
2922 /* Enable the CIU lines */
2923 set_c0_status(STATUSF_IP2 | STATUSF_IP3);
2924 clear_c0_status(STATUSF_IP4);
2930 static struct of_device_id ciu_types[] __initdata = {
2931 {.compatible = "cavium,octeon-3860-ciu", .data = octeon_irq_init_ciu},
2932 {.compatible = "cavium,octeon-3860-gpio", .data = octeon_irq_init_gpio},
2933 {.compatible = "cavium,octeon-6880-ciu2", .data = octeon_irq_init_ciu2},
2934 {.compatible = "cavium,octeon-7890-ciu3", .data = octeon_irq_init_ciu3},
2935 {.compatible = "cavium,octeon-7130-cib", .data = octeon_irq_init_cib},
2939 void __init arch_init_irq(void)
2942 /* Set the default affinity to the boot cpu. */
2943 cpumask_clear(irq_default_affinity);
2944 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
2946 of_irq_init(ciu_types);
2949 asmlinkage void plat_irq_dispatch(void)
2951 unsigned long cop0_cause;
2952 unsigned long cop0_status;
2955 cop0_cause = read_c0_cause();
2956 cop0_status = read_c0_status();
2957 cop0_cause &= cop0_status;
2958 cop0_cause &= ST0_IM;
2960 if (cop0_cause & STATUSF_IP2)
2962 else if (cop0_cause & STATUSF_IP3)
2964 else if (cop0_cause & STATUSF_IP4)
2966 else if (cop0_cause)
2967 do_IRQ(fls(cop0_cause) - 9 + MIPS_CPU_IRQ_BASE);
2973 #ifdef CONFIG_HOTPLUG_CPU
2975 void octeon_fixup_irqs(void)
2980 #endif /* CONFIG_HOTPLUG_CPU */
2982 struct irq_domain *octeon_irq_get_block_domain(int node, uint8_t block)
2984 struct octeon_ciu3_info *ciu3_info;
2986 ciu3_info = octeon_ciu3_info_per_node[node & CVMX_NODE_MASK];
2987 return ciu3_info->domain[block];
2989 EXPORT_SYMBOL(octeon_irq_get_block_domain);