1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#undef DEBUG
28
29#include <linux/export.h>
30#include <linux/threads.h>
31#include <linux/kernel_stat.h>
32#include <linux/signal.h>
33#include <linux/sched.h>
34#include <linux/ptrace.h>
35#include <linux/ioport.h>
36#include <linux/interrupt.h>
37#include <linux/timex.h>
38#include <linux/init.h>
39#include <linux/slab.h>
40#include <linux/delay.h>
41#include <linux/irq.h>
42#include <linux/seq_file.h>
43#include <linux/cpumask.h>
44#include <linux/profile.h>
45#include <linux/bitops.h>
46#include <linux/list.h>
47#include <linux/radix-tree.h>
48#include <linux/mutex.h>
49#include <linux/pci.h>
50#include <linux/debugfs.h>
51#include <linux/of.h>
52#include <linux/of_irq.h>
53
54#include <linux/uaccess.h>
55#include <asm/io.h>
56#include <asm/pgtable.h>
57#include <asm/irq.h>
58#include <asm/cache.h>
59#include <asm/prom.h>
60#include <asm/ptrace.h>
61#include <asm/machdep.h>
62#include <asm/udbg.h>
63#include <asm/smp.h>
64#include <asm/livepatch.h>
65#include <asm/asm-prototypes.h>
66#include <asm/hw_irq.h>
67
68#ifdef CONFIG_PPC64
69#include <asm/paca.h>
70#include <asm/firmware.h>
71#include <asm/lv1call.h>
72#endif
73#define CREATE_TRACE_POINTS
74#include <asm/trace.h>
75#include <asm/cpu_has_feature.h>
76
77DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
78EXPORT_PER_CPU_SYMBOL(irq_stat);
79
80#ifdef CONFIG_PPC32
81atomic_t ppc_n_lost_interrupts;
82
83#ifdef CONFIG_TAU_INT
84extern int tau_initialized;
85u32 tau_interrupts(unsigned long cpu);
86#endif
87#endif
88
89#ifdef CONFIG_PPC64
90
91int distribute_irqs = 1;
92
93static inline notrace unsigned long get_irq_happened(void)
94{
95 unsigned long happened;
96
97 __asm__ __volatile__("lbz %0,%1(13)"
98 : "=r" (happened) : "i" (offsetof(struct paca_struct, irq_happened)));
99
100 return happened;
101}
102
103static inline notrace int decrementer_check_overflow(void)
104{
105 u64 now = get_tb_or_rtc();
106 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
107
108 return now >= *next_tb;
109}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125notrace unsigned int __check_irq_replay(void)
126{
127
128
129
130
131
132 unsigned char happened = local_paca->irq_happened;
133
134
135
136
137
138 trace_hardirqs_on();
139 trace_hardirqs_off();
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 if (happened & PACA_IRQ_HARD_DIS) {
155 local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
156
157
158
159
160
161
162 if (!(happened & PACA_IRQ_DEC)) {
163 if (decrementer_check_overflow()) {
164 local_paca->irq_happened |= PACA_IRQ_DEC;
165 happened |= PACA_IRQ_DEC;
166 }
167 }
168 }
169
170
171
172
173
174 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
175 u64 tmp, tmp2;
176 lv1_get_version_info(&tmp, &tmp2);
177 }
178
179
180
181
182
183
184 if (happened & PACA_IRQ_HMI) {
185 local_paca->irq_happened &= ~PACA_IRQ_HMI;
186 return 0xe60;
187 }
188
189 if (happened & PACA_IRQ_DEC) {
190 local_paca->irq_happened &= ~PACA_IRQ_DEC;
191 return 0x900;
192 }
193
194 if (happened & PACA_IRQ_PMI) {
195 local_paca->irq_happened &= ~PACA_IRQ_PMI;
196 return 0xf00;
197 }
198
199 if (happened & PACA_IRQ_EE) {
200 local_paca->irq_happened &= ~PACA_IRQ_EE;
201 return 0x500;
202 }
203
204#ifdef CONFIG_PPC_BOOK3E
205
206
207
208
209
210 if (happened & PACA_IRQ_EE_EDGE) {
211 local_paca->irq_happened &= ~PACA_IRQ_EE_EDGE;
212 return 0x500;
213 }
214
215 if (happened & PACA_IRQ_DBELL) {
216 local_paca->irq_happened &= ~PACA_IRQ_DBELL;
217 return 0x280;
218 }
219#else
220 if (happened & PACA_IRQ_DBELL) {
221 local_paca->irq_happened &= ~PACA_IRQ_DBELL;
222 return 0xa00;
223 }
224#endif
225
226
227 BUG_ON(local_paca->irq_happened != 0);
228
229 return 0;
230}
231
232notrace void arch_local_irq_restore(unsigned long mask)
233{
234 unsigned char irq_happened;
235 unsigned int replay;
236
237
238 irq_soft_mask_set(mask);
239 if (mask)
240 return;
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 irq_happened = get_irq_happened();
256 if (!irq_happened) {
257#ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
258 WARN_ON_ONCE(!(mfmsr() & MSR_EE));
259#endif
260 return;
261 }
262
263
264
265
266
267
268
269 if (!(irq_happened & PACA_IRQ_HARD_DIS)) {
270#ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
271 WARN_ON_ONCE(!(mfmsr() & MSR_EE));
272#endif
273 __hard_irq_disable();
274#ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
275 } else {
276
277
278
279
280
281
282 if (WARN_ON_ONCE(mfmsr() & MSR_EE))
283 __hard_irq_disable();
284#endif
285 }
286
287 irq_soft_mask_set(IRQS_ALL_DISABLED);
288 trace_hardirqs_off();
289
290
291
292
293
294
295 replay = __check_irq_replay();
296
297
298 trace_hardirqs_on();
299 irq_soft_mask_set(IRQS_ENABLED);
300
301
302
303
304
305 if (replay) {
306 __replay_interrupt(replay);
307 return;
308 }
309
310
311 __hard_irq_enable();
312}
313EXPORT_SYMBOL(arch_local_irq_restore);
314
315
316
317
318
319
320
321
322
323
324void notrace restore_interrupts(void)
325{
326 if (irqs_disabled()) {
327 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
328 local_irq_enable();
329 } else
330 __hard_irq_enable();
331}
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348bool prep_irq_for_idle(void)
349{
350
351
352
353
354 __hard_irq_disable();
355 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
356
357
358
359
360
361 if (lazy_irq_pending())
362 return false;
363
364
365 trace_hardirqs_on();
366
367
368
369
370
371
372
373 local_paca->irq_happened &= ~PACA_IRQ_HARD_DIS;
374 irq_soft_mask_set(IRQS_ENABLED);
375
376
377 return true;
378}
379
380#ifdef CONFIG_PPC_BOOK3S
381
382
383
384
385
386
387bool prep_irq_for_idle_irqsoff(void)
388{
389 WARN_ON(!irqs_disabled());
390
391
392
393
394
395 __hard_irq_disable();
396 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
397
398
399
400
401
402 if (lazy_irq_pending())
403 return false;
404
405
406 trace_hardirqs_on();
407
408 return true;
409}
410
411
412
413
414
415
416
417
418
419
420
421#define IRQ_SYSTEM_RESET 0xff
422
423static const u8 srr1_to_lazyirq[0x10] = {
424 0, 0, 0,
425 PACA_IRQ_DBELL,
426 IRQ_SYSTEM_RESET,
427 PACA_IRQ_DBELL,
428 PACA_IRQ_DEC,
429 0,
430 PACA_IRQ_EE,
431 PACA_IRQ_EE,
432 PACA_IRQ_HMI,
433 0, 0, 0, 0, 0 };
434
435void replay_system_reset(void)
436{
437 struct pt_regs regs;
438
439 ppc_save_regs(®s);
440 regs.trap = 0x100;
441 get_paca()->in_nmi = 1;
442 system_reset_exception(®s);
443 get_paca()->in_nmi = 0;
444}
445EXPORT_SYMBOL_GPL(replay_system_reset);
446
447void irq_set_pending_from_srr1(unsigned long srr1)
448{
449 unsigned int idx = (srr1 & SRR1_WAKEMASK_P8) >> 18;
450 u8 reason = srr1_to_lazyirq[idx];
451
452
453
454
455
456
457 if (unlikely(reason == IRQ_SYSTEM_RESET)) {
458 replay_system_reset();
459 return;
460 }
461
462
463
464
465
466
467
468
469
470
471 local_paca->irq_happened |= reason;
472}
473#endif
474
475
476
477
478void force_external_irq_replay(void)
479{
480
481
482
483
484 WARN_ON(!arch_irqs_disabled());
485
486
487
488
489
490
491 __hard_irq_disable();
492 local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
493
494
495 local_paca->irq_happened |= PACA_IRQ_EE;
496}
497
498#endif
499
500int arch_show_interrupts(struct seq_file *p, int prec)
501{
502 int j;
503
504#if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
505 if (tau_initialized) {
506 seq_printf(p, "%*s: ", prec, "TAU");
507 for_each_online_cpu(j)
508 seq_printf(p, "%10u ", tau_interrupts(j));
509 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
510 }
511#endif
512
513 seq_printf(p, "%*s: ", prec, "LOC");
514 for_each_online_cpu(j)
515 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_event);
516 seq_printf(p, " Local timer interrupts for timer event device\n");
517
518 seq_printf(p, "%*s: ", prec, "BCT");
519 for_each_online_cpu(j)
520 seq_printf(p, "%10u ", per_cpu(irq_stat, j).broadcast_irqs_event);
521 seq_printf(p, " Broadcast timer interrupts for timer event device\n");
522
523 seq_printf(p, "%*s: ", prec, "LOC");
524 for_each_online_cpu(j)
525 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_others);
526 seq_printf(p, " Local timer interrupts for others\n");
527
528 seq_printf(p, "%*s: ", prec, "SPU");
529 for_each_online_cpu(j)
530 seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
531 seq_printf(p, " Spurious interrupts\n");
532
533 seq_printf(p, "%*s: ", prec, "PMI");
534 for_each_online_cpu(j)
535 seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
536 seq_printf(p, " Performance monitoring interrupts\n");
537
538 seq_printf(p, "%*s: ", prec, "MCE");
539 for_each_online_cpu(j)
540 seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
541 seq_printf(p, " Machine check exceptions\n");
542
543 if (cpu_has_feature(CPU_FTR_HVMODE)) {
544 seq_printf(p, "%*s: ", prec, "HMI");
545 for_each_online_cpu(j)
546 seq_printf(p, "%10u ",
547 per_cpu(irq_stat, j).hmi_exceptions);
548 seq_printf(p, " Hypervisor Maintenance Interrupts\n");
549 }
550
551 seq_printf(p, "%*s: ", prec, "NMI");
552 for_each_online_cpu(j)
553 seq_printf(p, "%10u ", per_cpu(irq_stat, j).sreset_irqs);
554 seq_printf(p, " System Reset interrupts\n");
555
556#ifdef CONFIG_PPC_WATCHDOG
557 seq_printf(p, "%*s: ", prec, "WDG");
558 for_each_online_cpu(j)
559 seq_printf(p, "%10u ", per_cpu(irq_stat, j).soft_nmi_irqs);
560 seq_printf(p, " Watchdog soft-NMI interrupts\n");
561#endif
562
563#ifdef CONFIG_PPC_DOORBELL
564 if (cpu_has_feature(CPU_FTR_DBELL)) {
565 seq_printf(p, "%*s: ", prec, "DBL");
566 for_each_online_cpu(j)
567 seq_printf(p, "%10u ", per_cpu(irq_stat, j).doorbell_irqs);
568 seq_printf(p, " Doorbell interrupts\n");
569 }
570#endif
571
572 return 0;
573}
574
575
576
577
578u64 arch_irq_stat_cpu(unsigned int cpu)
579{
580 u64 sum = per_cpu(irq_stat, cpu).timer_irqs_event;
581
582 sum += per_cpu(irq_stat, cpu).broadcast_irqs_event;
583 sum += per_cpu(irq_stat, cpu).pmu_irqs;
584 sum += per_cpu(irq_stat, cpu).mce_exceptions;
585 sum += per_cpu(irq_stat, cpu).spurious_irqs;
586 sum += per_cpu(irq_stat, cpu).timer_irqs_others;
587 sum += per_cpu(irq_stat, cpu).hmi_exceptions;
588 sum += per_cpu(irq_stat, cpu).sreset_irqs;
589#ifdef CONFIG_PPC_WATCHDOG
590 sum += per_cpu(irq_stat, cpu).soft_nmi_irqs;
591#endif
592#ifdef CONFIG_PPC_DOORBELL
593 sum += per_cpu(irq_stat, cpu).doorbell_irqs;
594#endif
595
596 return sum;
597}
598
599static inline void check_stack_overflow(void)
600{
601#ifdef CONFIG_DEBUG_STACKOVERFLOW
602 long sp;
603
604 sp = current_stack_pointer() & (THREAD_SIZE-1);
605
606
607 if (unlikely(sp < 2048)) {
608 pr_err("do_IRQ: stack overflow: %ld\n", sp);
609 dump_stack();
610 }
611#endif
612}
613
614void __do_irq(struct pt_regs *regs)
615{
616 unsigned int irq;
617
618 irq_enter();
619
620 trace_irq_entry(regs);
621
622 check_stack_overflow();
623
624
625
626
627
628
629 irq = ppc_md.get_irq();
630
631
632 may_hard_irq_enable();
633
634
635 if (unlikely(!irq))
636 __this_cpu_inc(irq_stat.spurious_irqs);
637 else
638 generic_handle_irq(irq);
639
640 trace_irq_exit(regs);
641
642 irq_exit();
643}
644
645void do_IRQ(struct pt_regs *regs)
646{
647 struct pt_regs *old_regs = set_irq_regs(regs);
648 void *cursp, *irqsp, *sirqsp;
649
650
651 cursp = (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
652 irqsp = hardirq_ctx[raw_smp_processor_id()];
653 sirqsp = softirq_ctx[raw_smp_processor_id()];
654
655
656 if (unlikely(cursp == irqsp || cursp == sirqsp)) {
657 __do_irq(regs);
658 set_irq_regs(old_regs);
659 return;
660 }
661
662 call_do_irq(regs, irqsp);
663
664 set_irq_regs(old_regs);
665}
666
667void __init init_IRQ(void)
668{
669 if (ppc_md.init_IRQ)
670 ppc_md.init_IRQ();
671}
672
673#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
674void *critirq_ctx[NR_CPUS] __read_mostly;
675void *dbgirq_ctx[NR_CPUS] __read_mostly;
676void *mcheckirq_ctx[NR_CPUS] __read_mostly;
677#endif
678
679void *softirq_ctx[NR_CPUS] __read_mostly;
680void *hardirq_ctx[NR_CPUS] __read_mostly;
681
682void do_softirq_own_stack(void)
683{
684 call_do_softirq(softirq_ctx[smp_processor_id()]);
685}
686
687irq_hw_number_t virq_to_hw(unsigned int virq)
688{
689 struct irq_data *irq_data = irq_get_irq_data(virq);
690 return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
691}
692EXPORT_SYMBOL_GPL(virq_to_hw);
693
694#ifdef CONFIG_SMP
695int irq_choose_cpu(const struct cpumask *mask)
696{
697 int cpuid;
698
699 if (cpumask_equal(mask, cpu_online_mask)) {
700 static int irq_rover;
701 static DEFINE_RAW_SPINLOCK(irq_rover_lock);
702 unsigned long flags;
703
704
705do_round_robin:
706 raw_spin_lock_irqsave(&irq_rover_lock, flags);
707
708 irq_rover = cpumask_next(irq_rover, cpu_online_mask);
709 if (irq_rover >= nr_cpu_ids)
710 irq_rover = cpumask_first(cpu_online_mask);
711
712 cpuid = irq_rover;
713
714 raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
715 } else {
716 cpuid = cpumask_first_and(mask, cpu_online_mask);
717 if (cpuid >= nr_cpu_ids)
718 goto do_round_robin;
719 }
720
721 return get_hard_smp_processor_id(cpuid);
722}
723#else
724int irq_choose_cpu(const struct cpumask *mask)
725{
726 return hard_smp_processor_id();
727}
728#endif
729
730#ifdef CONFIG_PPC64
731static int __init setup_noirqdistrib(char *str)
732{
733 distribute_irqs = 0;
734 return 1;
735}
736
737__setup("noirqdistrib", setup_noirqdistrib);
738#endif
739