MIPS: OCTEON: Don't do acknowledge operations for level triggered irqs.
[deliverable/linux.git] / arch / mips / cavium-octeon / octeon-irq.c
CommitLineData
5b3b1688
DD
1/*
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
4 * for more details.
5 *
a0c16582 6 * Copyright (C) 2004-2012 Cavium, Inc.
5b3b1688 7 */
0c326387 8
5b3b1688 9#include <linux/interrupt.h>
a0c16582 10#include <linux/irqdomain.h>
0c326387
DD
11#include <linux/bitops.h>
12#include <linux/percpu.h>
a0c16582 13#include <linux/slab.h>
0c326387 14#include <linux/irq.h>
631330f5 15#include <linux/smp.h>
a0c16582 16#include <linux/of.h>
5b3b1688
DD
17
18#include <asm/octeon/octeon.h>
88fd8589 19#include <asm/octeon/cvmx-ciu2-defs.h>
5b3b1688 20
0c326387
DD
21static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu0_en_mirror);
22static DEFINE_PER_CPU(unsigned long, octeon_irq_ciu1_en_mirror);
1a7e68f2 23static DEFINE_PER_CPU(raw_spinlock_t, octeon_irq_ciu_spinlock);
0c326387
DD
24
25static __read_mostly u8 octeon_irq_ciu_to_irq[8][64];
26
27union octeon_ciu_chip_data {
28 void *p;
29 unsigned long l;
30 struct {
88fd8589
DD
31 unsigned long line:6;
32 unsigned long bit:6;
33 unsigned long gpio_line:6;
0c326387
DD
34 } s;
35};
36
37struct octeon_core_chip_data {
38 struct mutex core_irq_mutex;
39 bool current_en;
40 bool desired_en;
41 u8 bit;
42};
43
44#define MIPS_CORE_IRQ_LINES 8
45
46static struct octeon_core_chip_data octeon_irq_core_chip_data[MIPS_CORE_IRQ_LINES];
47
88fd8589 48static void octeon_irq_set_ciu_mapping(int irq, int line, int bit, int gpio_line,
a0c16582
DD
49 struct irq_chip *chip,
50 irq_flow_handler_t handler)
0c326387
DD
51{
52 union octeon_ciu_chip_data cd;
53
54 irq_set_chip_and_handler(irq, chip, handler);
55
56 cd.l = 0;
57 cd.s.line = line;
58 cd.s.bit = bit;
88fd8589 59 cd.s.gpio_line = gpio_line;
0c326387
DD
60
61 irq_set_chip_data(irq, cd.p);
62 octeon_irq_ciu_to_irq[line][bit] = irq;
63}
64
87161ccd
DD
65static void octeon_irq_force_ciu_mapping(struct irq_domain *domain,
66 int irq, int line, int bit)
67{
68 irq_domain_associate(domain, irq, line << 6 | bit);
69}
70
cd847b78
DD
71static int octeon_coreid_for_cpu(int cpu)
72{
73#ifdef CONFIG_SMP
74 return cpu_logical_map(cpu);
75#else
76 return cvmx_get_core_num();
77#endif
78}
79
0c326387
DD
80static int octeon_cpu_for_coreid(int coreid)
81{
82#ifdef CONFIG_SMP
83 return cpu_number_map(coreid);
84#else
85 return smp_processor_id();
86#endif
87}
88
89static void octeon_irq_core_ack(struct irq_data *data)
5b3b1688 90{
0c326387
DD
91 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
92 unsigned int bit = cd->bit;
93
5b3b1688
DD
94 /*
95 * We don't need to disable IRQs to make these atomic since
96 * they are already disabled earlier in the low level
97 * interrupt code.
98 */
99 clear_c0_status(0x100 << bit);
100 /* The two user interrupts must be cleared manually. */
101 if (bit < 2)
102 clear_c0_cause(0x100 << bit);
103}
104
0c326387 105static void octeon_irq_core_eoi(struct irq_data *data)
5b3b1688 106{
0c326387
DD
107 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
108
5b3b1688
DD
109 /*
110 * We don't need to disable IRQs to make these atomic since
111 * they are already disabled earlier in the low level
112 * interrupt code.
113 */
0c326387 114 set_c0_status(0x100 << cd->bit);
5b3b1688
DD
115}
116
0c326387 117static void octeon_irq_core_set_enable_local(void *arg)
5b3b1688 118{
0c326387
DD
119 struct irq_data *data = arg;
120 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
121 unsigned int mask = 0x100 << cd->bit;
5b3b1688
DD
122
123 /*
0c326387 124 * Interrupts are already disabled, so these are atomic.
5b3b1688 125 */
0c326387
DD
126 if (cd->desired_en)
127 set_c0_status(mask);
128 else
129 clear_c0_status(mask);
130
5b3b1688
DD
131}
132
0c326387 133static void octeon_irq_core_disable(struct irq_data *data)
5b3b1688 134{
0c326387
DD
135 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
136 cd->desired_en = false;
5b3b1688
DD
137}
138
0c326387 139static void octeon_irq_core_enable(struct irq_data *data)
5b3b1688 140{
0c326387
DD
141 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
142 cd->desired_en = true;
5b3b1688
DD
143}
144
0c326387
DD
145static void octeon_irq_core_bus_lock(struct irq_data *data)
146{
147 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
5b3b1688 148
0c326387
DD
149 mutex_lock(&cd->core_irq_mutex);
150}
5b3b1688 151
0c326387 152static void octeon_irq_core_bus_sync_unlock(struct irq_data *data)
5b3b1688 153{
0c326387
DD
154 struct octeon_core_chip_data *cd = irq_data_get_irq_chip_data(data);
155
156 if (cd->desired_en != cd->current_en) {
157 on_each_cpu(octeon_irq_core_set_enable_local, data, 1);
158
159 cd->current_en = cd->desired_en;
5aae1fd4
DD
160 }
161
0c326387 162 mutex_unlock(&cd->core_irq_mutex);
5b3b1688
DD
163}
164
0c326387
DD
165static struct irq_chip octeon_irq_chip_core = {
166 .name = "Core",
167 .irq_enable = octeon_irq_core_enable,
168 .irq_disable = octeon_irq_core_disable,
169 .irq_ack = octeon_irq_core_ack,
170 .irq_eoi = octeon_irq_core_eoi,
171 .irq_bus_lock = octeon_irq_core_bus_lock,
172 .irq_bus_sync_unlock = octeon_irq_core_bus_sync_unlock,
173
5b7cd6fd
TG
174 .irq_cpu_online = octeon_irq_core_eoi,
175 .irq_cpu_offline = octeon_irq_core_ack,
176 .flags = IRQCHIP_ONOFFLINE_ENABLED,
0c326387
DD
177};
178
179static void __init octeon_irq_init_core(void)
180{
181 int i;
182 int irq;
183 struct octeon_core_chip_data *cd;
184
185 for (i = 0; i < MIPS_CORE_IRQ_LINES; i++) {
186 cd = &octeon_irq_core_chip_data[i];
187 cd->current_en = false;
188 cd->desired_en = false;
189 cd->bit = i;
190 mutex_init(&cd->core_irq_mutex);
191
192 irq = OCTEON_IRQ_SW0 + i;
87161ccd
DD
193 irq_set_chip_data(irq, cd);
194 irq_set_chip_and_handler(irq, &octeon_irq_chip_core,
195 handle_percpu_irq);
0c326387
DD
196 }
197}
198
199static int next_cpu_for_irq(struct irq_data *data)
5aae1fd4
DD
200{
201
202#ifdef CONFIG_SMP
0c326387
DD
203 int cpu;
204 int weight = cpumask_weight(data->affinity);
5aae1fd4
DD
205
206 if (weight > 1) {
0c326387 207 cpu = smp_processor_id();
5aae1fd4 208 for (;;) {
0c326387 209 cpu = cpumask_next(cpu, data->affinity);
5aae1fd4
DD
210 if (cpu >= nr_cpu_ids) {
211 cpu = -1;
212 continue;
213 } else if (cpumask_test_cpu(cpu, cpu_online_mask)) {
214 break;
215 }
216 }
5aae1fd4 217 } else if (weight == 1) {
0c326387 218 cpu = cpumask_first(data->affinity);
5aae1fd4 219 } else {
0c326387 220 cpu = smp_processor_id();
5aae1fd4 221 }
0c326387 222 return cpu;
5aae1fd4 223#else
0c326387 224 return smp_processor_id();
5aae1fd4
DD
225#endif
226}
227
0c326387 228static void octeon_irq_ciu_enable(struct irq_data *data)
5aae1fd4 229{
0c326387
DD
230 int cpu = next_cpu_for_irq(data);
231 int coreid = octeon_coreid_for_cpu(cpu);
232 unsigned long *pen;
5aae1fd4 233 unsigned long flags;
0c326387 234 union octeon_ciu_chip_data cd;
1a7e68f2 235 raw_spinlock_t *lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
0c326387
DD
236
237 cd.p = irq_data_get_irq_chip_data(data);
5aae1fd4 238
1a7e68f2 239 raw_spin_lock_irqsave(lock, flags);
0c326387 240 if (cd.s.line == 0) {
0c326387 241 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
1a7e68f2
DD
242 __set_bit(cd.s.bit, pen);
243 /*
244 * Must be visible to octeon_irq_ip{2,3}_ciu() before
245 * enabling the irq.
246 */
247 wmb();
0c326387 248 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
0c326387 249 } else {
0c326387 250 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
1a7e68f2
DD
251 __set_bit(cd.s.bit, pen);
252 /*
253 * Must be visible to octeon_irq_ip{2,3}_ciu() before
254 * enabling the irq.
255 */
256 wmb();
0c326387 257 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
0c326387 258 }
1a7e68f2 259 raw_spin_unlock_irqrestore(lock, flags);
5aae1fd4
DD
260}
261
0c326387
DD
262static void octeon_irq_ciu_enable_local(struct irq_data *data)
263{
264 unsigned long *pen;
265 unsigned long flags;
266 union octeon_ciu_chip_data cd;
35898716 267 raw_spinlock_t *lock = this_cpu_ptr(&octeon_irq_ciu_spinlock);
0c326387
DD
268
269 cd.p = irq_data_get_irq_chip_data(data);
270
1a7e68f2 271 raw_spin_lock_irqsave(lock, flags);
0c326387 272 if (cd.s.line == 0) {
35898716 273 pen = this_cpu_ptr(&octeon_irq_ciu0_en_mirror);
1a7e68f2
DD
274 __set_bit(cd.s.bit, pen);
275 /*
276 * Must be visible to octeon_irq_ip{2,3}_ciu() before
277 * enabling the irq.
278 */
279 wmb();
0c326387 280 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen);
0c326387 281 } else {
35898716 282 pen = this_cpu_ptr(&octeon_irq_ciu1_en_mirror);
1a7e68f2
DD
283 __set_bit(cd.s.bit, pen);
284 /*
285 * Must be visible to octeon_irq_ip{2,3}_ciu() before
286 * enabling the irq.
287 */
288 wmb();
0c326387 289 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen);
0c326387 290 }
1a7e68f2 291 raw_spin_unlock_irqrestore(lock, flags);
0c326387
DD
292}
293
294static void octeon_irq_ciu_disable_local(struct irq_data *data)
295{
296 unsigned long *pen;
297 unsigned long flags;
298 union octeon_ciu_chip_data cd;
35898716 299 raw_spinlock_t *lock = this_cpu_ptr(&octeon_irq_ciu_spinlock);
0c326387
DD
300
301 cd.p = irq_data_get_irq_chip_data(data);
302
1a7e68f2 303 raw_spin_lock_irqsave(lock, flags);
0c326387 304 if (cd.s.line == 0) {
35898716 305 pen = this_cpu_ptr(&octeon_irq_ciu0_en_mirror);
1a7e68f2
DD
306 __clear_bit(cd.s.bit, pen);
307 /*
308 * Must be visible to octeon_irq_ip{2,3}_ciu() before
309 * enabling the irq.
310 */
311 wmb();
0c326387 312 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num() * 2), *pen);
0c326387 313 } else {
35898716 314 pen = this_cpu_ptr(&octeon_irq_ciu1_en_mirror);
1a7e68f2
DD
315 __clear_bit(cd.s.bit, pen);
316 /*
317 * Must be visible to octeon_irq_ip{2,3}_ciu() before
318 * enabling the irq.
319 */
320 wmb();
0c326387 321 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num() * 2 + 1), *pen);
0c326387 322 }
1a7e68f2 323 raw_spin_unlock_irqrestore(lock, flags);
0c326387
DD
324}
325
326static void octeon_irq_ciu_disable_all(struct irq_data *data)
5b3b1688 327{
5b3b1688 328 unsigned long flags;
0c326387
DD
329 unsigned long *pen;
330 int cpu;
331 union octeon_ciu_chip_data cd;
1a7e68f2 332 raw_spinlock_t *lock;
5b3b1688 333
0c326387
DD
334 cd.p = irq_data_get_irq_chip_data(data);
335
1a7e68f2
DD
336 for_each_online_cpu(cpu) {
337 int coreid = octeon_coreid_for_cpu(cpu);
338 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
339 if (cd.s.line == 0)
0c326387 340 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
1a7e68f2 341 else
0c326387 342 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
1a7e68f2
DD
343
344 raw_spin_lock_irqsave(lock, flags);
345 __clear_bit(cd.s.bit, pen);
346 /*
347 * Must be visible to octeon_irq_ip{2,3}_ciu() before
348 * enabling the irq.
349 */
350 wmb();
351 if (cd.s.line == 0)
352 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
353 else
0c326387 354 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
1a7e68f2 355 raw_spin_unlock_irqrestore(lock, flags);
0c326387 356 }
5b3b1688
DD
357}
358
0c326387 359static void octeon_irq_ciu_enable_all(struct irq_data *data)
5b3b1688 360{
5b3b1688 361 unsigned long flags;
0c326387 362 unsigned long *pen;
5b3b1688 363 int cpu;
0c326387 364 union octeon_ciu_chip_data cd;
1a7e68f2 365 raw_spinlock_t *lock;
0c326387
DD
366
367 cd.p = irq_data_get_irq_chip_data(data);
368
1a7e68f2
DD
369 for_each_online_cpu(cpu) {
370 int coreid = octeon_coreid_for_cpu(cpu);
371 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
372 if (cd.s.line == 0)
0c326387 373 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
1a7e68f2 374 else
0c326387 375 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
1a7e68f2
DD
376
377 raw_spin_lock_irqsave(lock, flags);
378 __set_bit(cd.s.bit, pen);
379 /*
380 * Must be visible to octeon_irq_ip{2,3}_ciu() before
381 * enabling the irq.
382 */
383 wmb();
384 if (cd.s.line == 0)
385 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
386 else
0c326387 387 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
1a7e68f2 388 raw_spin_unlock_irqrestore(lock, flags);
5b3b1688 389 }
cd847b78
DD
390}
391
392/*
5aae1fd4
DD
393 * Enable the irq on the next core in the affinity set for chips that
394 * have the EN*_W1{S,C} registers.
cd847b78 395 */
0c326387 396static void octeon_irq_ciu_enable_v2(struct irq_data *data)
cd847b78 397{
0c326387
DD
398 u64 mask;
399 int cpu = next_cpu_for_irq(data);
400 union octeon_ciu_chip_data cd;
cd847b78 401
0c326387
DD
402 cd.p = irq_data_get_irq_chip_data(data);
403 mask = 1ull << (cd.s.bit);
404
405 /*
406 * Called under the desc lock, so these should never get out
407 * of sync.
408 */
409 if (cd.s.line == 0) {
410 int index = octeon_coreid_for_cpu(cpu) * 2;
411 set_bit(cd.s.bit, &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
5aae1fd4 412 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
0c326387
DD
413 } else {
414 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
415 set_bit(cd.s.bit, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
416 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
5aae1fd4 417 }
cd847b78
DD
418}
419
420/*
5aae1fd4
DD
421 * Enable the irq on the current CPU for chips that
422 * have the EN*_W1{S,C} registers.
cd847b78 423 */
0c326387 424static void octeon_irq_ciu_enable_local_v2(struct irq_data *data)
cd847b78 425{
0c326387
DD
426 u64 mask;
427 union octeon_ciu_chip_data cd;
428
429 cd.p = irq_data_get_irq_chip_data(data);
430 mask = 1ull << (cd.s.bit);
cd847b78 431
0c326387
DD
432 if (cd.s.line == 0) {
433 int index = cvmx_get_core_num() * 2;
35898716 434 set_bit(cd.s.bit, this_cpu_ptr(&octeon_irq_ciu0_en_mirror));
0c326387
DD
435 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
436 } else {
437 int index = cvmx_get_core_num() * 2 + 1;
35898716 438 set_bit(cd.s.bit, this_cpu_ptr(&octeon_irq_ciu1_en_mirror));
0c326387
DD
439 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
440 }
441}
442
443static void octeon_irq_ciu_disable_local_v2(struct irq_data *data)
444{
445 u64 mask;
446 union octeon_ciu_chip_data cd;
447
448 cd.p = irq_data_get_irq_chip_data(data);
449 mask = 1ull << (cd.s.bit);
450
451 if (cd.s.line == 0) {
452 int index = cvmx_get_core_num() * 2;
35898716 453 clear_bit(cd.s.bit, this_cpu_ptr(&octeon_irq_ciu0_en_mirror));
0c326387
DD
454 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
455 } else {
456 int index = cvmx_get_core_num() * 2 + 1;
35898716 457 clear_bit(cd.s.bit, this_cpu_ptr(&octeon_irq_ciu1_en_mirror));
0c326387
DD
458 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
459 }
cd847b78
DD
460}
461
86568dc4 462/*
0c326387 463 * Write to the W1C bit in CVMX_CIU_INTX_SUM0 to clear the irq.
86568dc4 464 */
0c326387
DD
465static void octeon_irq_ciu_ack(struct irq_data *data)
466{
467 u64 mask;
468 union octeon_ciu_chip_data cd;
469
88fd8589 470 cd.p = irq_data_get_irq_chip_data(data);
0c326387
DD
471 mask = 1ull << (cd.s.bit);
472
473 if (cd.s.line == 0) {
474 int index = cvmx_get_core_num() * 2;
5aae1fd4 475 cvmx_write_csr(CVMX_CIU_INTX_SUM0(index), mask);
0c326387
DD
476 } else {
477 cvmx_write_csr(CVMX_CIU_INT_SUM1, mask);
5aae1fd4 478 }
86568dc4
DD
479}
480
dbb103b2 481/*
0c326387 482 * Disable the irq on the all cores for chips that have the EN*_W1{S,C}
dbb103b2
DD
483 * registers.
484 */
0c326387 485static void octeon_irq_ciu_disable_all_v2(struct irq_data *data)
dbb103b2 486{
0c326387
DD
487 int cpu;
488 u64 mask;
489 union octeon_ciu_chip_data cd;
dbb103b2 490
88fd8589 491 cd.p = irq_data_get_irq_chip_data(data);
0c326387
DD
492 mask = 1ull << (cd.s.bit);
493
494 if (cd.s.line == 0) {
495 for_each_online_cpu(cpu) {
496 int index = octeon_coreid_for_cpu(cpu) * 2;
497 clear_bit(cd.s.bit, &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
498 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
499 }
500 } else {
501 for_each_online_cpu(cpu) {
502 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
503 clear_bit(cd.s.bit, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
504 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
505 }
506 }
dbb103b2
DD
507}
508
cd847b78 509/*
0c326387 510 * Enable the irq on the all cores for chips that have the EN*_W1{S,C}
cd847b78
DD
511 * registers.
512 */
0c326387 513static void octeon_irq_ciu_enable_all_v2(struct irq_data *data)
cd847b78 514{
cd847b78 515 int cpu;
0c326387
DD
516 u64 mask;
517 union octeon_ciu_chip_data cd;
518
88fd8589 519 cd.p = irq_data_get_irq_chip_data(data);
0c326387
DD
520 mask = 1ull << (cd.s.bit);
521
522 if (cd.s.line == 0) {
523 for_each_online_cpu(cpu) {
524 int index = octeon_coreid_for_cpu(cpu) * 2;
525 set_bit(cd.s.bit, &per_cpu(octeon_irq_ciu0_en_mirror, cpu));
526 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
527 }
528 } else {
529 for_each_online_cpu(cpu) {
530 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
531 set_bit(cd.s.bit, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
532 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
533 }
cd847b78 534 }
5b3b1688
DD
535}
536
6d1ab4c2
DD
537static void octeon_irq_gpio_setup(struct irq_data *data)
538{
539 union cvmx_gpio_bit_cfgx cfg;
540 union octeon_ciu_chip_data cd;
541 u32 t = irqd_get_trigger_type(data);
542
543 cd.p = irq_data_get_irq_chip_data(data);
544
545 cfg.u64 = 0;
546 cfg.s.int_en = 1;
547 cfg.s.int_type = (t & IRQ_TYPE_EDGE_BOTH) != 0;
548 cfg.s.rx_xor = (t & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) != 0;
549
550 /* 140 nS glitch filter*/
551 cfg.s.fil_cnt = 7;
552 cfg.s.fil_sel = 3;
553
88fd8589 554 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd.s.gpio_line), cfg.u64);
6d1ab4c2
DD
555}
556
557static void octeon_irq_ciu_enable_gpio_v2(struct irq_data *data)
558{
559 octeon_irq_gpio_setup(data);
560 octeon_irq_ciu_enable_v2(data);
561}
562
563static void octeon_irq_ciu_enable_gpio(struct irq_data *data)
564{
565 octeon_irq_gpio_setup(data);
566 octeon_irq_ciu_enable(data);
567}
568
569static int octeon_irq_ciu_gpio_set_type(struct irq_data *data, unsigned int t)
570{
571 irqd_set_trigger_type(data, t);
572 octeon_irq_gpio_setup(data);
573
574 return IRQ_SET_MASK_OK;
575}
576
577static void octeon_irq_ciu_disable_gpio_v2(struct irq_data *data)
578{
579 union octeon_ciu_chip_data cd;
580
581 cd.p = irq_data_get_irq_chip_data(data);
88fd8589 582 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd.s.gpio_line), 0);
6d1ab4c2
DD
583
584 octeon_irq_ciu_disable_all_v2(data);
585}
586
587static void octeon_irq_ciu_disable_gpio(struct irq_data *data)
588{
589 union octeon_ciu_chip_data cd;
590
591 cd.p = irq_data_get_irq_chip_data(data);
88fd8589 592 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd.s.gpio_line), 0);
6d1ab4c2
DD
593
594 octeon_irq_ciu_disable_all(data);
595}
596
597static void octeon_irq_ciu_gpio_ack(struct irq_data *data)
598{
599 union octeon_ciu_chip_data cd;
600 u64 mask;
601
602 cd.p = irq_data_get_irq_chip_data(data);
88fd8589 603 mask = 1ull << (cd.s.gpio_line);
6d1ab4c2
DD
604
605 cvmx_write_csr(CVMX_GPIO_INT_CLR, mask);
606}
607
608static void octeon_irq_handle_gpio(unsigned int irq, struct irq_desc *desc)
609{
5ebf1f29 610 if (irq_get_trigger_type(irq) & IRQ_TYPE_EDGE_BOTH)
6d1ab4c2
DD
611 handle_edge_irq(irq, desc);
612 else
613 handle_level_irq(irq, desc);
614}
615
5b3b1688 616#ifdef CONFIG_SMP
0c326387
DD
617
618static void octeon_irq_cpu_offline_ciu(struct irq_data *data)
619{
620 int cpu = smp_processor_id();
621 cpumask_t new_affinity;
622
623 if (!cpumask_test_cpu(cpu, data->affinity))
624 return;
625
626 if (cpumask_weight(data->affinity) > 1) {
627 /*
628 * It has multi CPU affinity, just remove this CPU
629 * from the affinity set.
630 */
631 cpumask_copy(&new_affinity, data->affinity);
632 cpumask_clear_cpu(cpu, &new_affinity);
633 } else {
634 /* Otherwise, put it on lowest numbered online CPU. */
635 cpumask_clear(&new_affinity);
636 cpumask_set_cpu(cpumask_first(cpu_online_mask), &new_affinity);
637 }
01f8fa4f 638 irq_set_affinity_locked(data, &new_affinity, false);
0c326387
DD
639}
640
641static int octeon_irq_ciu_set_affinity(struct irq_data *data,
642 const struct cpumask *dest, bool force)
5b3b1688
DD
643{
644 int cpu;
5b7cd6fd 645 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
b6b74d54 646 unsigned long flags;
0c326387 647 union octeon_ciu_chip_data cd;
1a7e68f2
DD
648 unsigned long *pen;
649 raw_spinlock_t *lock;
0c326387 650
88fd8589 651 cd.p = irq_data_get_irq_chip_data(data);
5b3b1688 652
5aae1fd4
DD
653 /*
654 * For non-v2 CIU, we will allow only single CPU affinity.
655 * This removes the need to do locking in the .ack/.eoi
656 * functions.
657 */
658 if (cpumask_weight(dest) != 1)
659 return -EINVAL;
660
5b7cd6fd 661 if (!enable_one)
0c326387
DD
662 return 0;
663
0c326387 664
1a7e68f2
DD
665 for_each_online_cpu(cpu) {
666 int coreid = octeon_coreid_for_cpu(cpu);
667
668 lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
669 raw_spin_lock_irqsave(lock, flags);
670
671 if (cd.s.line == 0)
672 pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
673 else
674 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
675
676 if (cpumask_test_cpu(cpu, dest) && enable_one) {
677 enable_one = 0;
678 __set_bit(cd.s.bit, pen);
679 } else {
680 __clear_bit(cd.s.bit, pen);
0c326387 681 }
1a7e68f2
DD
682 /*
683 * Must be visible to octeon_irq_ip{2,3}_ciu() before
684 * enabling the irq.
685 */
686 wmb();
0c326387 687
1a7e68f2
DD
688 if (cd.s.line == 0)
689 cvmx_write_csr(CVMX_CIU_INTX_EN0(coreid * 2), *pen);
690 else
0c326387 691 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
1a7e68f2
DD
692
693 raw_spin_unlock_irqrestore(lock, flags);
5b3b1688 694 }
d5dedd45 695 return 0;
5b3b1688 696}
cd847b78
DD
697
698/*
699 * Set affinity for the irq for chips that have the EN*_W1{S,C}
700 * registers.
701 */
0c326387
DD
702static int octeon_irq_ciu_set_affinity_v2(struct irq_data *data,
703 const struct cpumask *dest,
704 bool force)
cd847b78
DD
705{
706 int cpu;
5b7cd6fd 707 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
0c326387
DD
708 u64 mask;
709 union octeon_ciu_chip_data cd;
710
5b7cd6fd 711 if (!enable_one)
0c326387
DD
712 return 0;
713
88fd8589 714 cd.p = irq_data_get_irq_chip_data(data);
0c326387
DD
715 mask = 1ull << cd.s.bit;
716
717 if (cd.s.line == 0) {
718 for_each_online_cpu(cpu) {
719 unsigned long *pen = &per_cpu(octeon_irq_ciu0_en_mirror, cpu);
720 int index = octeon_coreid_for_cpu(cpu) * 2;
721 if (cpumask_test_cpu(cpu, dest) && enable_one) {
5b7cd6fd 722 enable_one = false;
0c326387
DD
723 set_bit(cd.s.bit, pen);
724 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1S(index), mask);
725 } else {
726 clear_bit(cd.s.bit, pen);
727 cvmx_write_csr(CVMX_CIU_INTX_EN0_W1C(index), mask);
728 }
729 }
730 } else {
731 for_each_online_cpu(cpu) {
732 unsigned long *pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
733 int index = octeon_coreid_for_cpu(cpu) * 2 + 1;
734 if (cpumask_test_cpu(cpu, dest) && enable_one) {
5b7cd6fd 735 enable_one = false;
0c326387
DD
736 set_bit(cd.s.bit, pen);
737 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(index), mask);
738 } else {
739 clear_bit(cd.s.bit, pen);
740 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1C(index), mask);
741 }
5aae1fd4 742 }
cd847b78
DD
743 }
744 return 0;
745}
5b3b1688
DD
746#endif
747
cd847b78
DD
748/*
749 * Newer octeon chips have support for lockless CIU operation.
750 */
0c326387 751static struct irq_chip octeon_irq_chip_ciu_v2 = {
2e3ecab1
DD
752 .name = "CIU",
753 .irq_enable = octeon_irq_ciu_enable_v2,
754 .irq_disable = octeon_irq_ciu_disable_all_v2,
755 .irq_mask = octeon_irq_ciu_disable_local_v2,
756 .irq_unmask = octeon_irq_ciu_enable_v2,
757#ifdef CONFIG_SMP
758 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
759 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
760#endif
761};
762
763static struct irq_chip octeon_irq_chip_ciu_v2_edge = {
0c326387
DD
764 .name = "CIU",
765 .irq_enable = octeon_irq_ciu_enable_v2,
766 .irq_disable = octeon_irq_ciu_disable_all_v2,
0c326387
DD
767 .irq_ack = octeon_irq_ciu_ack,
768 .irq_mask = octeon_irq_ciu_disable_local_v2,
769 .irq_unmask = octeon_irq_ciu_enable_v2,
5b3b1688 770#ifdef CONFIG_SMP
0c326387
DD
771 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
772 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
5b3b1688
DD
773#endif
774};
775
0c326387 776static struct irq_chip octeon_irq_chip_ciu = {
2e3ecab1
DD
777 .name = "CIU",
778 .irq_enable = octeon_irq_ciu_enable,
779 .irq_disable = octeon_irq_ciu_disable_all,
780 .irq_mask = octeon_irq_ciu_disable_local,
781 .irq_unmask = octeon_irq_ciu_enable,
782#ifdef CONFIG_SMP
783 .irq_set_affinity = octeon_irq_ciu_set_affinity,
784 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
785#endif
786};
787
788static struct irq_chip octeon_irq_chip_ciu_edge = {
0c326387
DD
789 .name = "CIU",
790 .irq_enable = octeon_irq_ciu_enable,
791 .irq_disable = octeon_irq_ciu_disable_all,
0c326387 792 .irq_ack = octeon_irq_ciu_ack,
1a7e68f2
DD
793 .irq_mask = octeon_irq_ciu_disable_local,
794 .irq_unmask = octeon_irq_ciu_enable,
0c326387
DD
795#ifdef CONFIG_SMP
796 .irq_set_affinity = octeon_irq_ciu_set_affinity,
797 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
798#endif
86568dc4
DD
799};
800
0c326387
DD
801/* The mbox versions don't do any affinity or round-robin. */
802static struct irq_chip octeon_irq_chip_ciu_mbox_v2 = {
803 .name = "CIU-M",
804 .irq_enable = octeon_irq_ciu_enable_all_v2,
805 .irq_disable = octeon_irq_ciu_disable_all_v2,
806 .irq_ack = octeon_irq_ciu_disable_local_v2,
807 .irq_eoi = octeon_irq_ciu_enable_local_v2,
808
5b7cd6fd
TG
809 .irq_cpu_online = octeon_irq_ciu_enable_local_v2,
810 .irq_cpu_offline = octeon_irq_ciu_disable_local_v2,
811 .flags = IRQCHIP_ONOFFLINE_ENABLED,
0c326387 812};
5b3b1688 813
0c326387
DD
814static struct irq_chip octeon_irq_chip_ciu_mbox = {
815 .name = "CIU-M",
816 .irq_enable = octeon_irq_ciu_enable_all,
817 .irq_disable = octeon_irq_ciu_disable_all,
1a7e68f2
DD
818 .irq_ack = octeon_irq_ciu_disable_local,
819 .irq_eoi = octeon_irq_ciu_enable_local,
5b3b1688 820
5b7cd6fd
TG
821 .irq_cpu_online = octeon_irq_ciu_enable_local,
822 .irq_cpu_offline = octeon_irq_ciu_disable_local,
823 .flags = IRQCHIP_ONOFFLINE_ENABLED,
0c326387
DD
824};
825
6d1ab4c2
DD
826static struct irq_chip octeon_irq_chip_ciu_gpio_v2 = {
827 .name = "CIU-GPIO",
828 .irq_enable = octeon_irq_ciu_enable_gpio_v2,
829 .irq_disable = octeon_irq_ciu_disable_gpio_v2,
830 .irq_ack = octeon_irq_ciu_gpio_ack,
831 .irq_mask = octeon_irq_ciu_disable_local_v2,
832 .irq_unmask = octeon_irq_ciu_enable_v2,
833 .irq_set_type = octeon_irq_ciu_gpio_set_type,
834#ifdef CONFIG_SMP
835 .irq_set_affinity = octeon_irq_ciu_set_affinity_v2,
cf355704 836 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
6d1ab4c2
DD
837#endif
838 .flags = IRQCHIP_SET_TYPE_MASKED,
839};
840
841static struct irq_chip octeon_irq_chip_ciu_gpio = {
842 .name = "CIU-GPIO",
843 .irq_enable = octeon_irq_ciu_enable_gpio,
844 .irq_disable = octeon_irq_ciu_disable_gpio,
1a7e68f2
DD
845 .irq_mask = octeon_irq_ciu_disable_local,
846 .irq_unmask = octeon_irq_ciu_enable,
6d1ab4c2
DD
847 .irq_ack = octeon_irq_ciu_gpio_ack,
848 .irq_set_type = octeon_irq_ciu_gpio_set_type,
849#ifdef CONFIG_SMP
850 .irq_set_affinity = octeon_irq_ciu_set_affinity,
cf355704 851 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
6d1ab4c2
DD
852#endif
853 .flags = IRQCHIP_SET_TYPE_MASKED,
854};
855
0c326387
DD
856/*
857 * Watchdog interrupts are special. They are associated with a single
858 * core, so we hardwire the affinity to that core.
859 */
860static void octeon_irq_ciu_wd_enable(struct irq_data *data)
5b3b1688 861{
5b3b1688 862 unsigned long flags;
0c326387
DD
863 unsigned long *pen;
864 int coreid = data->irq - OCTEON_IRQ_WDOG0; /* Bit 0-63 of EN1 */
865 int cpu = octeon_cpu_for_coreid(coreid);
1a7e68f2 866 raw_spinlock_t *lock = &per_cpu(octeon_irq_ciu_spinlock, cpu);
5b3b1688 867
1a7e68f2 868 raw_spin_lock_irqsave(lock, flags);
0c326387 869 pen = &per_cpu(octeon_irq_ciu1_en_mirror, cpu);
1a7e68f2
DD
870 __set_bit(coreid, pen);
871 /*
872 * Must be visible to octeon_irq_ip{2,3}_ciu() before enabling
873 * the irq.
874 */
875 wmb();
0c326387 876 cvmx_write_csr(CVMX_CIU_INTX_EN1(coreid * 2 + 1), *pen);
1a7e68f2 877 raw_spin_unlock_irqrestore(lock, flags);
5b3b1688
DD
878}
879
5aae1fd4
DD
880/*
881 * Watchdog interrupts are special. They are associated with a single
882 * core, so we hardwire the affinity to that core.
883 */
0c326387 884static void octeon_irq_ciu1_wd_enable_v2(struct irq_data *data)
5aae1fd4 885{
0c326387
DD
886 int coreid = data->irq - OCTEON_IRQ_WDOG0;
887 int cpu = octeon_cpu_for_coreid(coreid);
5aae1fd4 888
0c326387
DD
889 set_bit(coreid, &per_cpu(octeon_irq_ciu1_en_mirror, cpu));
890 cvmx_write_csr(CVMX_CIU_INTX_EN1_W1S(coreid * 2 + 1), 1ull << coreid);
5aae1fd4
DD
891}
892
0c326387
DD
893
894static struct irq_chip octeon_irq_chip_ciu_wd_v2 = {
895 .name = "CIU-W",
896 .irq_enable = octeon_irq_ciu1_wd_enable_v2,
897 .irq_disable = octeon_irq_ciu_disable_all_v2,
898 .irq_mask = octeon_irq_ciu_disable_local_v2,
899 .irq_unmask = octeon_irq_ciu_enable_local_v2,
900};
901
902static struct irq_chip octeon_irq_chip_ciu_wd = {
903 .name = "CIU-W",
904 .irq_enable = octeon_irq_ciu_wd_enable,
905 .irq_disable = octeon_irq_ciu_disable_all,
1a7e68f2
DD
906 .irq_mask = octeon_irq_ciu_disable_local,
907 .irq_unmask = octeon_irq_ciu_enable_local,
0c326387
DD
908};
909
a0c16582
DD
910static bool octeon_irq_ciu_is_edge(unsigned int line, unsigned int bit)
911{
912 bool edge = false;
913
914 if (line == 0)
915 switch (bit) {
916 case 48 ... 49: /* GMX DRP */
917 case 50: /* IPD_DRP */
918 case 52 ... 55: /* Timers */
919 case 58: /* MPI */
920 edge = true;
921 break;
922 default:
923 break;
924 }
925 else /* line == 1 */
926 switch (bit) {
927 case 47: /* PTP */
928 edge = true;
929 break;
930 default:
931 break;
932 }
933 return edge;
934}
935
936struct octeon_irq_gpio_domain_data {
937 unsigned int base_hwirq;
938};
939
940static int octeon_irq_gpio_xlat(struct irq_domain *d,
941 struct device_node *node,
942 const u32 *intspec,
943 unsigned int intsize,
944 unsigned long *out_hwirq,
945 unsigned int *out_type)
946{
947 unsigned int type;
948 unsigned int pin;
949 unsigned int trigger;
a0c16582
DD
950
951 if (d->of_node != node)
952 return -EINVAL;
953
954 if (intsize < 2)
955 return -EINVAL;
956
957 pin = intspec[0];
958 if (pin >= 16)
959 return -EINVAL;
960
961 trigger = intspec[1];
962
963 switch (trigger) {
964 case 1:
965 type = IRQ_TYPE_EDGE_RISING;
966 break;
967 case 2:
968 type = IRQ_TYPE_EDGE_FALLING;
969 break;
970 case 4:
971 type = IRQ_TYPE_LEVEL_HIGH;
972 break;
973 case 8:
974 type = IRQ_TYPE_LEVEL_LOW;
975 break;
976 default:
977 pr_err("Error: (%s) Invalid irq trigger specification: %x\n",
978 node->name,
979 trigger);
980 type = IRQ_TYPE_LEVEL_LOW;
981 break;
982 }
983 *out_type = type;
87161ccd 984 *out_hwirq = pin;
a0c16582
DD
985
986 return 0;
987}
988
989static int octeon_irq_ciu_xlat(struct irq_domain *d,
990 struct device_node *node,
991 const u32 *intspec,
992 unsigned int intsize,
993 unsigned long *out_hwirq,
994 unsigned int *out_type)
995{
996 unsigned int ciu, bit;
997
998 ciu = intspec[0];
999 bit = intspec[1];
1000
1001 if (ciu > 1 || bit > 63)
1002 return -EINVAL;
1003
a0c16582
DD
1004 *out_hwirq = (ciu << 6) | bit;
1005 *out_type = 0;
1006
1007 return 0;
1008}
1009
1010static struct irq_chip *octeon_irq_ciu_chip;
2e3ecab1 1011static struct irq_chip *octeon_irq_ciu_chip_edge;
a0c16582
DD
1012static struct irq_chip *octeon_irq_gpio_chip;
1013
1014static bool octeon_irq_virq_in_range(unsigned int virq)
1015{
1016 /* We cannot let it overflow the mapping array. */
1017 if (virq < (1ul << 8 * sizeof(octeon_irq_ciu_to_irq[0][0])))
1018 return true;
1019
1020 WARN_ONCE(true, "virq out of range %u.\n", virq);
1021 return false;
1022}
1023
1024static int octeon_irq_ciu_map(struct irq_domain *d,
1025 unsigned int virq, irq_hw_number_t hw)
1026{
1027 unsigned int line = hw >> 6;
1028 unsigned int bit = hw & 63;
1029
1030 if (!octeon_irq_virq_in_range(virq))
1031 return -EINVAL;
1032
2eddb708
AH
1033 /* Don't map irq if it is reserved for GPIO. */
1034 if (line == 0 && bit >= 16 && bit <32)
1035 return 0;
1036
a0c16582
DD
1037 if (line > 1 || octeon_irq_ciu_to_irq[line][bit] != 0)
1038 return -EINVAL;
1039
1040 if (octeon_irq_ciu_is_edge(line, bit))
88fd8589 1041 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
2e3ecab1 1042 octeon_irq_ciu_chip_edge,
a0c16582
DD
1043 handle_edge_irq);
1044 else
88fd8589 1045 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
a0c16582
DD
1046 octeon_irq_ciu_chip,
1047 handle_level_irq);
1048
1049 return 0;
1050}
1051
88fd8589
DD
1052static int octeon_irq_gpio_map_common(struct irq_domain *d,
1053 unsigned int virq, irq_hw_number_t hw,
1054 int line_limit, struct irq_chip *chip)
a0c16582 1055{
87161ccd
DD
1056 struct octeon_irq_gpio_domain_data *gpiod = d->host_data;
1057 unsigned int line, bit;
a0c16582
DD
1058
1059 if (!octeon_irq_virq_in_range(virq))
1060 return -EINVAL;
1061
d41d547a
AS
1062 line = (hw + gpiod->base_hwirq) >> 6;
1063 bit = (hw + gpiod->base_hwirq) & 63;
88fd8589 1064 if (line > line_limit || octeon_irq_ciu_to_irq[line][bit] != 0)
a0c16582
DD
1065 return -EINVAL;
1066
88fd8589
DD
1067 octeon_irq_set_ciu_mapping(virq, line, bit, hw,
1068 chip, octeon_irq_handle_gpio);
a0c16582
DD
1069 return 0;
1070}
1071
88fd8589
DD
1072static int octeon_irq_gpio_map(struct irq_domain *d,
1073 unsigned int virq, irq_hw_number_t hw)
1074{
1075 return octeon_irq_gpio_map_common(d, virq, hw, 1, octeon_irq_gpio_chip);
1076}
1077
a0c16582
DD
1078static struct irq_domain_ops octeon_irq_domain_ciu_ops = {
1079 .map = octeon_irq_ciu_map,
1080 .xlate = octeon_irq_ciu_xlat,
1081};
1082
1083static struct irq_domain_ops octeon_irq_domain_gpio_ops = {
1084 .map = octeon_irq_gpio_map,
1085 .xlate = octeon_irq_gpio_xlat,
1086};
1087
1a7e68f2 1088static void octeon_irq_ip2_ciu(void)
cd847b78 1089{
0c326387
DD
1090 const unsigned long core_id = cvmx_get_core_num();
1091 u64 ciu_sum = cvmx_read_csr(CVMX_CIU_INTX_SUM0(core_id * 2));
1092
35898716 1093 ciu_sum &= __this_cpu_read(octeon_irq_ciu0_en_mirror);
0c326387
DD
1094 if (likely(ciu_sum)) {
1095 int bit = fls64(ciu_sum) - 1;
1096 int irq = octeon_irq_ciu_to_irq[0][bit];
1097 if (likely(irq))
1098 do_IRQ(irq);
1099 else
1100 spurious_interrupt();
1101 } else {
1102 spurious_interrupt();
5aae1fd4 1103 }
cd847b78 1104}
cd847b78 1105
1a7e68f2 1106static void octeon_irq_ip3_ciu(void)
dbb103b2 1107{
0c326387
DD
1108 u64 ciu_sum = cvmx_read_csr(CVMX_CIU_INT_SUM1);
1109
35898716 1110 ciu_sum &= __this_cpu_read(octeon_irq_ciu1_en_mirror);
0c326387
DD
1111 if (likely(ciu_sum)) {
1112 int bit = fls64(ciu_sum) - 1;
1113 int irq = octeon_irq_ciu_to_irq[1][bit];
1114 if (likely(irq))
1115 do_IRQ(irq);
1116 else
1117 spurious_interrupt();
1118 } else {
1119 spurious_interrupt();
1120 }
dbb103b2
DD
1121}
1122
88fd8589
DD
1123static bool octeon_irq_use_ip4;
1124
078a55fc 1125static void octeon_irq_local_enable_ip4(void *arg)
88fd8589
DD
1126{
1127 set_c0_status(STATUSF_IP4);
1128}
1129
0c326387 1130static void octeon_irq_ip4_mask(void)
cd847b78 1131{
0c326387
DD
1132 clear_c0_status(STATUSF_IP4);
1133 spurious_interrupt();
5b3b1688
DD
1134}
1135
0c326387
DD
1136static void (*octeon_irq_ip2)(void);
1137static void (*octeon_irq_ip3)(void);
1138static void (*octeon_irq_ip4)(void);
5b3b1688 1139
078a55fc 1140void (*octeon_irq_setup_secondary)(void);
5aae1fd4 1141
078a55fc 1142void octeon_irq_set_ip4_handler(octeon_irq_ip4_handler_t h)
88fd8589
DD
1143{
1144 octeon_irq_ip4 = h;
1145 octeon_irq_use_ip4 = true;
1146 on_each_cpu(octeon_irq_local_enable_ip4, NULL, 1);
1147}
1148
078a55fc 1149static void octeon_irq_percpu_enable(void)
0c326387
DD
1150{
1151 irq_cpu_online();
1152}
1153
078a55fc 1154static void octeon_irq_init_ciu_percpu(void)
0c326387
DD
1155{
1156 int coreid = cvmx_get_core_num();
1a7e68f2
DD
1157
1158
35898716
CL
1159 __this_cpu_write(octeon_irq_ciu0_en_mirror, 0);
1160 __this_cpu_write(octeon_irq_ciu1_en_mirror, 0);
1a7e68f2 1161 wmb();
35898716 1162 raw_spin_lock_init(this_cpu_ptr(&octeon_irq_ciu_spinlock));
5b3b1688 1163 /*
0c326387
DD
1164 * Disable All CIU Interrupts. The ones we need will be
1165 * enabled later. Read the SUM register so we know the write
1166 * completed.
5b3b1688 1167 */
0c326387
DD
1168 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2)), 0);
1169 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2 + 1)), 0);
1170 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2)), 0);
1171 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2 + 1)), 0);
1172 cvmx_read_csr(CVMX_CIU_INTX_SUM0((coreid * 2)));
5b3b1688 1173}
cd847b78 1174
88fd8589
DD
1175static void octeon_irq_init_ciu2_percpu(void)
1176{
1177 u64 regx, ipx;
1178 int coreid = cvmx_get_core_num();
1179 u64 base = CVMX_CIU2_EN_PPX_IP2_WRKQ(coreid);
1180
1181 /*
1182 * Disable All CIU2 Interrupts. The ones we need will be
1183 * enabled later. Read the SUM register so we know the write
1184 * completed.
1185 *
1186 * There are 9 registers and 3 IPX levels with strides 0x1000
1187 * and 0x200 respectivly. Use loops to clear them.
1188 */
1189 for (regx = 0; regx <= 0x8000; regx += 0x1000) {
1190 for (ipx = 0; ipx <= 0x400; ipx += 0x200)
1191 cvmx_write_csr(base + regx + ipx, 0);
1192 }
1193
1194 cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(coreid));
1195}
1196
078a55fc 1197static void octeon_irq_setup_secondary_ciu(void)
cd847b78 1198{
0c326387
DD
1199 octeon_irq_init_ciu_percpu();
1200 octeon_irq_percpu_enable();
5b3b1688 1201
0c326387
DD
1202 /* Enable the CIU lines */
1203 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1204 clear_c0_status(STATUSF_IP4);
1205}
5aae1fd4 1206
88fd8589
DD
1207static void octeon_irq_setup_secondary_ciu2(void)
1208{
1209 octeon_irq_init_ciu2_percpu();
1210 octeon_irq_percpu_enable();
1211
1212 /* Enable the CIU lines */
1213 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1214 if (octeon_irq_use_ip4)
1215 set_c0_status(STATUSF_IP4);
1216 else
1217 clear_c0_status(STATUSF_IP4);
1218}
1219
0c326387
DD
1220static void __init octeon_irq_init_ciu(void)
1221{
1222 unsigned int i;
1223 struct irq_chip *chip;
2e3ecab1 1224 struct irq_chip *chip_edge;
0c326387
DD
1225 struct irq_chip *chip_mbox;
1226 struct irq_chip *chip_wd;
a0c16582
DD
1227 struct device_node *gpio_node;
1228 struct device_node *ciu_node;
87161ccd 1229 struct irq_domain *ciu_domain = NULL;
0c326387
DD
1230
1231 octeon_irq_init_ciu_percpu();
1232 octeon_irq_setup_secondary = octeon_irq_setup_secondary_ciu;
5aae1fd4 1233
1a7e68f2
DD
1234 octeon_irq_ip2 = octeon_irq_ip2_ciu;
1235 octeon_irq_ip3 = octeon_irq_ip3_ciu;
0c326387
DD
1236 if (OCTEON_IS_MODEL(OCTEON_CN58XX_PASS2_X) ||
1237 OCTEON_IS_MODEL(OCTEON_CN56XX_PASS2_X) ||
1238 OCTEON_IS_MODEL(OCTEON_CN52XX_PASS2_X) ||
debe6a62 1239 OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) {
0c326387 1240 chip = &octeon_irq_chip_ciu_v2;
2e3ecab1 1241 chip_edge = &octeon_irq_chip_ciu_v2_edge;
0c326387
DD
1242 chip_mbox = &octeon_irq_chip_ciu_mbox_v2;
1243 chip_wd = &octeon_irq_chip_ciu_wd_v2;
a0c16582 1244 octeon_irq_gpio_chip = &octeon_irq_chip_ciu_gpio_v2;
0c326387 1245 } else {
0c326387 1246 chip = &octeon_irq_chip_ciu;
2e3ecab1 1247 chip_edge = &octeon_irq_chip_ciu_edge;
0c326387
DD
1248 chip_mbox = &octeon_irq_chip_ciu_mbox;
1249 chip_wd = &octeon_irq_chip_ciu_wd;
a0c16582 1250 octeon_irq_gpio_chip = &octeon_irq_chip_ciu_gpio;
0c326387 1251 }
a0c16582 1252 octeon_irq_ciu_chip = chip;
2e3ecab1 1253 octeon_irq_ciu_chip_edge = chip_edge;
0c326387
DD
1254 octeon_irq_ip4 = octeon_irq_ip4_mask;
1255
1256 /* Mips internal */
1257 octeon_irq_init_core();
1258
a0c16582
DD
1259 gpio_node = of_find_compatible_node(NULL, NULL, "cavium,octeon-3860-gpio");
1260 if (gpio_node) {
1261 struct octeon_irq_gpio_domain_data *gpiod;
1262
1263 gpiod = kzalloc(sizeof(*gpiod), GFP_KERNEL);
1264 if (gpiod) {
1265 /* gpio domain host_data is the base hwirq number. */
1266 gpiod->base_hwirq = 16;
1267 irq_domain_add_linear(gpio_node, 16, &octeon_irq_domain_gpio_ops, gpiod);
1268 of_node_put(gpio_node);
1269 } else
1270 pr_warn("Cannot allocate memory for GPIO irq_domain.\n");
1271 } else
1272 pr_warn("Cannot find device node for cavium,octeon-3860-gpio.\n");
1273
1274 ciu_node = of_find_compatible_node(NULL, NULL, "cavium,octeon-3860-ciu");
1275 if (ciu_node) {
87161ccd 1276 ciu_domain = irq_domain_add_tree(ciu_node, &octeon_irq_domain_ciu_ops, NULL);
c9f0f0c0 1277 irq_set_default_host(ciu_domain);
a0c16582
DD
1278 of_node_put(ciu_node);
1279 } else
87161ccd
DD
1280 panic("Cannot find device node for cavium,octeon-3860-ciu.");
1281
1282 /* CIU_0 */
1283 for (i = 0; i < 16; i++)
1284 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_WORKQ0, 0, i + 0);
1285
88fd8589
DD
1286 octeon_irq_set_ciu_mapping(OCTEON_IRQ_MBOX0, 0, 32, 0, chip_mbox, handle_percpu_irq);
1287 octeon_irq_set_ciu_mapping(OCTEON_IRQ_MBOX1, 0, 33, 0, chip_mbox, handle_percpu_irq);
87161ccd
DD
1288
1289 for (i = 0; i < 4; i++)
1290 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_PCI_INT0, 0, i + 36);
1291 for (i = 0; i < 4; i++)
1292 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_PCI_MSI0, 0, i + 40);
1293
a53825ef 1294 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_TWSI, 0, 45);
87161ccd
DD
1295 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_RML, 0, 46);
1296 for (i = 0; i < 4; i++)
1297 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_TIMER0, 0, i + 52);
1298
1299 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_USB0, 0, 56);
a53825ef 1300 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_TWSI2, 0, 59);
87161ccd
DD
1301
1302 /* CIU_1 */
1303 for (i = 0; i < 16; i++)
88fd8589 1304 octeon_irq_set_ciu_mapping(i + OCTEON_IRQ_WDOG0, 1, i + 0, 0, chip_wd, handle_level_irq);
87161ccd
DD
1305
1306 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_USB1, 1, 17);
a0c16582 1307
0c326387
DD
1308 /* Enable the CIU lines */
1309 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1310 clear_c0_status(STATUSF_IP4);
1311}
5aae1fd4 1312
88fd8589
DD
1313/*
1314 * Watchdog interrupts are special. They are associated with a single
1315 * core, so we hardwire the affinity to that core.
1316 */
1317static void octeon_irq_ciu2_wd_enable(struct irq_data *data)
1318{
1319 u64 mask;
1320 u64 en_addr;
1321 int coreid = data->irq - OCTEON_IRQ_WDOG0;
1322 union octeon_ciu_chip_data cd;
1323
1324 cd.p = irq_data_get_irq_chip_data(data);
1325 mask = 1ull << (cd.s.bit);
1326
1327 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) + (0x1000ull * cd.s.line);
1328 cvmx_write_csr(en_addr, mask);
1329
1330}
1331
1332static void octeon_irq_ciu2_enable(struct irq_data *data)
1333{
1334 u64 mask;
1335 u64 en_addr;
1336 int cpu = next_cpu_for_irq(data);
1337 int coreid = octeon_coreid_for_cpu(cpu);
1338 union octeon_ciu_chip_data cd;
1339
1340 cd.p = irq_data_get_irq_chip_data(data);
1341 mask = 1ull << (cd.s.bit);
1342
1343 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) + (0x1000ull * cd.s.line);
1344 cvmx_write_csr(en_addr, mask);
1345}
1346
1347static void octeon_irq_ciu2_enable_local(struct irq_data *data)
1348{
1349 u64 mask;
1350 u64 en_addr;
1351 int coreid = cvmx_get_core_num();
1352 union octeon_ciu_chip_data cd;
1353
1354 cd.p = irq_data_get_irq_chip_data(data);
1355 mask = 1ull << (cd.s.bit);
1356
1357 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(coreid) + (0x1000ull * cd.s.line);
1358 cvmx_write_csr(en_addr, mask);
1359
1360}
1361
1362static void octeon_irq_ciu2_disable_local(struct irq_data *data)
1363{
1364 u64 mask;
1365 u64 en_addr;
1366 int coreid = cvmx_get_core_num();
1367 union octeon_ciu_chip_data cd;
1368
1369 cd.p = irq_data_get_irq_chip_data(data);
1370 mask = 1ull << (cd.s.bit);
1371
1372 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(coreid) + (0x1000ull * cd.s.line);
1373 cvmx_write_csr(en_addr, mask);
1374
1375}
1376
1377static void octeon_irq_ciu2_ack(struct irq_data *data)
1378{
1379 u64 mask;
1380 u64 en_addr;
1381 int coreid = cvmx_get_core_num();
1382 union octeon_ciu_chip_data cd;
1383
1384 cd.p = irq_data_get_irq_chip_data(data);
1385 mask = 1ull << (cd.s.bit);
1386
1387 en_addr = CVMX_CIU2_RAW_PPX_IP2_WRKQ(coreid) + (0x1000ull * cd.s.line);
1388 cvmx_write_csr(en_addr, mask);
1389
1390}
1391
1392static void octeon_irq_ciu2_disable_all(struct irq_data *data)
1393{
1394 int cpu;
1395 u64 mask;
1396 union octeon_ciu_chip_data cd;
1397
1398 cd.p = irq_data_get_irq_chip_data(data);
1399 mask = 1ull << (cd.s.bit);
1400
1401 for_each_online_cpu(cpu) {
1402 u64 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(octeon_coreid_for_cpu(cpu)) + (0x1000ull * cd.s.line);
1403 cvmx_write_csr(en_addr, mask);
1404 }
1405}
1406
1407static void octeon_irq_ciu2_mbox_enable_all(struct irq_data *data)
1408{
1409 int cpu;
1410 u64 mask;
1411
1412 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1413
1414 for_each_online_cpu(cpu) {
1415 u64 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1S(octeon_coreid_for_cpu(cpu));
1416 cvmx_write_csr(en_addr, mask);
1417 }
1418}
1419
1420static void octeon_irq_ciu2_mbox_disable_all(struct irq_data *data)
1421{
1422 int cpu;
1423 u64 mask;
1424
1425 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1426
1427 for_each_online_cpu(cpu) {
1428 u64 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1C(octeon_coreid_for_cpu(cpu));
1429 cvmx_write_csr(en_addr, mask);
1430 }
1431}
1432
1433static void octeon_irq_ciu2_mbox_enable_local(struct irq_data *data)
1434{
1435 u64 mask;
1436 u64 en_addr;
1437 int coreid = cvmx_get_core_num();
1438
1439 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1440 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1S(coreid);
1441 cvmx_write_csr(en_addr, mask);
1442}
1443
1444static void octeon_irq_ciu2_mbox_disable_local(struct irq_data *data)
1445{
1446 u64 mask;
1447 u64 en_addr;
1448 int coreid = cvmx_get_core_num();
1449
1450 mask = 1ull << (data->irq - OCTEON_IRQ_MBOX0);
1451 en_addr = CVMX_CIU2_EN_PPX_IP3_MBOX_W1C(coreid);
1452 cvmx_write_csr(en_addr, mask);
1453}
1454
1455#ifdef CONFIG_SMP
1456static int octeon_irq_ciu2_set_affinity(struct irq_data *data,
1457 const struct cpumask *dest, bool force)
1458{
1459 int cpu;
1460 bool enable_one = !irqd_irq_disabled(data) && !irqd_irq_masked(data);
1461 u64 mask;
1462 union octeon_ciu_chip_data cd;
1463
1464 if (!enable_one)
1465 return 0;
1466
1467 cd.p = irq_data_get_irq_chip_data(data);
1468 mask = 1ull << cd.s.bit;
1469
1470 for_each_online_cpu(cpu) {
1471 u64 en_addr;
1472 if (cpumask_test_cpu(cpu, dest) && enable_one) {
1473 enable_one = false;
1474 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1S(octeon_coreid_for_cpu(cpu)) + (0x1000ull * cd.s.line);
1475 } else {
1476 en_addr = CVMX_CIU2_EN_PPX_IP2_WRKQ_W1C(octeon_coreid_for_cpu(cpu)) + (0x1000ull * cd.s.line);
1477 }
1478 cvmx_write_csr(en_addr, mask);
1479 }
1480
1481 return 0;
1482}
1483#endif
1484
1485static void octeon_irq_ciu2_enable_gpio(struct irq_data *data)
1486{
1487 octeon_irq_gpio_setup(data);
1488 octeon_irq_ciu2_enable(data);
1489}
1490
1491static void octeon_irq_ciu2_disable_gpio(struct irq_data *data)
1492{
1493 union octeon_ciu_chip_data cd;
1494 cd.p = irq_data_get_irq_chip_data(data);
1495
1496 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(cd.s.gpio_line), 0);
1497
1498 octeon_irq_ciu2_disable_all(data);
1499}
1500
1501static struct irq_chip octeon_irq_chip_ciu2 = {
2e3ecab1
DD
1502 .name = "CIU2-E",
1503 .irq_enable = octeon_irq_ciu2_enable,
1504 .irq_disable = octeon_irq_ciu2_disable_all,
1505 .irq_mask = octeon_irq_ciu2_disable_local,
1506 .irq_unmask = octeon_irq_ciu2_enable,
1507#ifdef CONFIG_SMP
1508 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1509 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1510#endif
1511};
1512
1513static struct irq_chip octeon_irq_chip_ciu2_edge = {
88fd8589
DD
1514 .name = "CIU2-E",
1515 .irq_enable = octeon_irq_ciu2_enable,
1516 .irq_disable = octeon_irq_ciu2_disable_all,
1517 .irq_ack = octeon_irq_ciu2_ack,
1518 .irq_mask = octeon_irq_ciu2_disable_local,
1519 .irq_unmask = octeon_irq_ciu2_enable,
1520#ifdef CONFIG_SMP
1521 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1522 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1523#endif
1524};
1525
1526static struct irq_chip octeon_irq_chip_ciu2_mbox = {
1527 .name = "CIU2-M",
1528 .irq_enable = octeon_irq_ciu2_mbox_enable_all,
1529 .irq_disable = octeon_irq_ciu2_mbox_disable_all,
1530 .irq_ack = octeon_irq_ciu2_mbox_disable_local,
1531 .irq_eoi = octeon_irq_ciu2_mbox_enable_local,
1532
1533 .irq_cpu_online = octeon_irq_ciu2_mbox_enable_local,
1534 .irq_cpu_offline = octeon_irq_ciu2_mbox_disable_local,
1535 .flags = IRQCHIP_ONOFFLINE_ENABLED,
1536};
1537
1538static struct irq_chip octeon_irq_chip_ciu2_wd = {
1539 .name = "CIU2-W",
1540 .irq_enable = octeon_irq_ciu2_wd_enable,
1541 .irq_disable = octeon_irq_ciu2_disable_all,
1542 .irq_mask = octeon_irq_ciu2_disable_local,
1543 .irq_unmask = octeon_irq_ciu2_enable_local,
1544};
1545
1546static struct irq_chip octeon_irq_chip_ciu2_gpio = {
1547 .name = "CIU-GPIO",
1548 .irq_enable = octeon_irq_ciu2_enable_gpio,
1549 .irq_disable = octeon_irq_ciu2_disable_gpio,
1550 .irq_ack = octeon_irq_ciu_gpio_ack,
1551 .irq_mask = octeon_irq_ciu2_disable_local,
1552 .irq_unmask = octeon_irq_ciu2_enable,
1553 .irq_set_type = octeon_irq_ciu_gpio_set_type,
1554#ifdef CONFIG_SMP
1555 .irq_set_affinity = octeon_irq_ciu2_set_affinity,
1556 .irq_cpu_offline = octeon_irq_cpu_offline_ciu,
1557#endif
1558 .flags = IRQCHIP_SET_TYPE_MASKED,
1559};
1560
1561static int octeon_irq_ciu2_xlat(struct irq_domain *d,
1562 struct device_node *node,
1563 const u32 *intspec,
1564 unsigned int intsize,
1565 unsigned long *out_hwirq,
1566 unsigned int *out_type)
1567{
1568 unsigned int ciu, bit;
1569
1570 ciu = intspec[0];
1571 bit = intspec[1];
1572
88fd8589
DD
1573 *out_hwirq = (ciu << 6) | bit;
1574 *out_type = 0;
1575
1576 return 0;
1577}
1578
1579static bool octeon_irq_ciu2_is_edge(unsigned int line, unsigned int bit)
1580{
1581 bool edge = false;
1582
1583 if (line == 3) /* MIO */
1584 switch (bit) {
70342287 1585 case 2: /* IPD_DRP */
88fd8589
DD
1586 case 8 ... 11: /* Timers */
1587 case 48: /* PTP */
1588 edge = true;
1589 break;
1590 default:
1591 break;
1592 }
1593 else if (line == 6) /* PKT */
1594 switch (bit) {
1595 case 52 ... 53: /* ILK_DRP */
70342287 1596 case 8 ... 12: /* GMX_DRP */
88fd8589
DD
1597 edge = true;
1598 break;
1599 default:
1600 break;
1601 }
1602 return edge;
1603}
1604
1605static int octeon_irq_ciu2_map(struct irq_domain *d,
1606 unsigned int virq, irq_hw_number_t hw)
1607{
1608 unsigned int line = hw >> 6;
1609 unsigned int bit = hw & 63;
1610
1611 if (!octeon_irq_virq_in_range(virq))
1612 return -EINVAL;
1613
2eddb708
AH
1614 /*
1615 * Don't map irq if it is reserved for GPIO.
1616 * (Line 7 are the GPIO lines.)
1617 */
1618 if (line == 7)
1619 return 0;
1620
1621 if (line > 7 || octeon_irq_ciu_to_irq[line][bit] != 0)
88fd8589
DD
1622 return -EINVAL;
1623
1624 if (octeon_irq_ciu2_is_edge(line, bit))
1625 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
2e3ecab1 1626 &octeon_irq_chip_ciu2_edge,
88fd8589
DD
1627 handle_edge_irq);
1628 else
1629 octeon_irq_set_ciu_mapping(virq, line, bit, 0,
1630 &octeon_irq_chip_ciu2,
1631 handle_level_irq);
1632
1633 return 0;
1634}
1635static int octeon_irq_ciu2_gpio_map(struct irq_domain *d,
1636 unsigned int virq, irq_hw_number_t hw)
1637{
1638 return octeon_irq_gpio_map_common(d, virq, hw, 7, &octeon_irq_chip_ciu2_gpio);
1639}
1640
1641static struct irq_domain_ops octeon_irq_domain_ciu2_ops = {
1642 .map = octeon_irq_ciu2_map,
1643 .xlate = octeon_irq_ciu2_xlat,
1644};
1645
1646static struct irq_domain_ops octeon_irq_domain_ciu2_gpio_ops = {
1647 .map = octeon_irq_ciu2_gpio_map,
1648 .xlate = octeon_irq_gpio_xlat,
1649};
1650
1651static void octeon_irq_ciu2(void)
1652{
1653 int line;
1654 int bit;
1655 int irq;
1656 u64 src_reg, src, sum;
1657 const unsigned long core_id = cvmx_get_core_num();
1658
1659 sum = cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(core_id)) & 0xfful;
1660
1661 if (unlikely(!sum))
1662 goto spurious;
1663
1664 line = fls64(sum) - 1;
1665 src_reg = CVMX_CIU2_SRC_PPX_IP2_WRKQ(core_id) + (0x1000 * line);
1666 src = cvmx_read_csr(src_reg);
1667
1668 if (unlikely(!src))
1669 goto spurious;
1670
1671 bit = fls64(src) - 1;
1672 irq = octeon_irq_ciu_to_irq[line][bit];
1673 if (unlikely(!irq))
1674 goto spurious;
1675
1676 do_IRQ(irq);
1677 goto out;
1678
1679spurious:
1680 spurious_interrupt();
1681out:
1682 /* CN68XX pass 1.x has an errata that accessing the ACK registers
1683 can stop interrupts from propagating */
1684 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
1685 cvmx_read_csr(CVMX_CIU2_INTR_CIU_READY);
1686 else
1687 cvmx_read_csr(CVMX_CIU2_ACK_PPX_IP2(core_id));
1688 return;
1689}
1690
1691static void octeon_irq_ciu2_mbox(void)
1692{
1693 int line;
1694
1695 const unsigned long core_id = cvmx_get_core_num();
1696 u64 sum = cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP3(core_id)) >> 60;
1697
1698 if (unlikely(!sum))
1699 goto spurious;
1700
1701 line = fls64(sum) - 1;
1702
1703 do_IRQ(OCTEON_IRQ_MBOX0 + line);
1704 goto out;
1705
1706spurious:
1707 spurious_interrupt();
1708out:
1709 /* CN68XX pass 1.x has an errata that accessing the ACK registers
1710 can stop interrupts from propagating */
1711 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
1712 cvmx_read_csr(CVMX_CIU2_INTR_CIU_READY);
1713 else
1714 cvmx_read_csr(CVMX_CIU2_ACK_PPX_IP3(core_id));
1715 return;
1716}
1717
1718static void __init octeon_irq_init_ciu2(void)
1719{
1720 unsigned int i;
1721 struct device_node *gpio_node;
1722 struct device_node *ciu_node;
1723 struct irq_domain *ciu_domain = NULL;
1724
1725 octeon_irq_init_ciu2_percpu();
1726 octeon_irq_setup_secondary = octeon_irq_setup_secondary_ciu2;
1727
1728 octeon_irq_ip2 = octeon_irq_ciu2;
1729 octeon_irq_ip3 = octeon_irq_ciu2_mbox;
1730 octeon_irq_ip4 = octeon_irq_ip4_mask;
1731
1732 /* Mips internal */
1733 octeon_irq_init_core();
1734
1735 gpio_node = of_find_compatible_node(NULL, NULL, "cavium,octeon-3860-gpio");
1736 if (gpio_node) {
1737 struct octeon_irq_gpio_domain_data *gpiod;
1738
1739 gpiod = kzalloc(sizeof(*gpiod), GFP_KERNEL);
1740 if (gpiod) {
1741 /* gpio domain host_data is the base hwirq number. */
1742 gpiod->base_hwirq = 7 << 6;
1743 irq_domain_add_linear(gpio_node, 16, &octeon_irq_domain_ciu2_gpio_ops, gpiod);
1744 of_node_put(gpio_node);
1745 } else
1746 pr_warn("Cannot allocate memory for GPIO irq_domain.\n");
1747 } else
1748 pr_warn("Cannot find device node for cavium,octeon-3860-gpio.\n");
1749
1750 ciu_node = of_find_compatible_node(NULL, NULL, "cavium,octeon-6880-ciu2");
1751 if (ciu_node) {
1752 ciu_domain = irq_domain_add_tree(ciu_node, &octeon_irq_domain_ciu2_ops, NULL);
c9f0f0c0 1753 irq_set_default_host(ciu_domain);
88fd8589
DD
1754 of_node_put(ciu_node);
1755 } else
1756 panic("Cannot find device node for cavium,octeon-6880-ciu2.");
1757
1758 /* CUI2 */
1759 for (i = 0; i < 64; i++)
1760 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_WORKQ0, 0, i);
1761
1762 for (i = 0; i < 32; i++)
1763 octeon_irq_set_ciu_mapping(i + OCTEON_IRQ_WDOG0, 1, i, 0,
1764 &octeon_irq_chip_ciu2_wd, handle_level_irq);
1765
1766 for (i = 0; i < 4; i++)
1767 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_TIMER0, 3, i + 8);
1768
1769 octeon_irq_force_ciu_mapping(ciu_domain, OCTEON_IRQ_USB0, 3, 44);
1770
1771 for (i = 0; i < 4; i++)
1772 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_PCI_INT0, 4, i);
1773
1774 for (i = 0; i < 4; i++)
1775 octeon_irq_force_ciu_mapping(ciu_domain, i + OCTEON_IRQ_PCI_MSI0, 4, i + 8);
1776
1777 irq_set_chip_and_handler(OCTEON_IRQ_MBOX0, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
1778 irq_set_chip_and_handler(OCTEON_IRQ_MBOX1, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
1779 irq_set_chip_and_handler(OCTEON_IRQ_MBOX2, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
1780 irq_set_chip_and_handler(OCTEON_IRQ_MBOX3, &octeon_irq_chip_ciu2_mbox, handle_percpu_irq);
1781
1782 /* Enable the CIU lines */
1783 set_c0_status(STATUSF_IP3 | STATUSF_IP2);
1784 clear_c0_status(STATUSF_IP4);
1785}
1786
5b3b1688
DD
1787void __init arch_init_irq(void)
1788{
5b3b1688
DD
1789#ifdef CONFIG_SMP
1790 /* Set the default affinity to the boot cpu. */
1791 cpumask_clear(irq_default_affinity);
1792 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
1793#endif
88fd8589
DD
1794 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
1795 octeon_irq_init_ciu2();
1796 else
1797 octeon_irq_init_ciu();
5b3b1688
DD
1798}
1799
1800asmlinkage void plat_irq_dispatch(void)
1801{
5b3b1688
DD
1802 unsigned long cop0_cause;
1803 unsigned long cop0_status;
5b3b1688
DD
1804
1805 while (1) {
1806 cop0_cause = read_c0_cause();
1807 cop0_status = read_c0_status();
1808 cop0_cause &= cop0_status;
1809 cop0_cause &= ST0_IM;
1810
0c326387
DD
1811 if (unlikely(cop0_cause & STATUSF_IP2))
1812 octeon_irq_ip2();
1813 else if (unlikely(cop0_cause & STATUSF_IP3))
1814 octeon_irq_ip3();
1815 else if (unlikely(cop0_cause & STATUSF_IP4))
1816 octeon_irq_ip4();
1817 else if (likely(cop0_cause))
5b3b1688 1818 do_IRQ(fls(cop0_cause) - 9 + MIPS_CPU_IRQ_BASE);
0c326387 1819 else
5b3b1688 1820 break;
5b3b1688
DD
1821 }
1822}
773cb77d
RB
1823
1824#ifdef CONFIG_HOTPLUG_CPU
773cb77d 1825
17efb59a 1826void octeon_fixup_irqs(void)
773cb77d 1827{
0c326387 1828 irq_cpu_offline();
773cb77d
RB
1829}
1830
1831#endif /* CONFIG_HOTPLUG_CPU */
This page took 0.648595 seconds and 5 git commands to generate.