Skip to content

Commit 38fef73

Browse files
committed
xtensa: implement fake NMI
In case perf IRQ is the highest of the medium-level IRQs, and is alone on its level, it may be treated as NMI: - LOCKLEVEL is defined to be one level less than EXCM level, - IRQ masking never lowers current IRQ level, - new fake exception cause code, EXCCAUSE_MAPPED_NMI is assigned to that IRQ; new second level exception handler, do_nmi, assigned to it handles it as NMI, - atomic operations in configurations without s32c1i still need to mask all interrupts. Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
1 parent 98e2983 commit 38fef73

9 files changed

Lines changed: 183 additions & 27 deletions

File tree

‎arch/xtensa/include/asm/atomic.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* Locking interrupts looks like this:
3131
*
32-
* rsil a15, LOCKLEVEL
32+
* rsil a15, TOPLEVEL
3333
* <code>
3434
* wsr a15, PS
3535
* rsync
@@ -106,7 +106,7 @@ static inline void atomic_##op(int i, atomic_t * v) \
106106
unsigned int vval; \
107107
\
108108
__asm__ __volatile__( \
109-
" rsil a15, "__stringify(LOCKLEVEL)"\n"\
109+
" rsil a15, "__stringify(TOPLEVEL)"\n"\
110110
" l32i %0, %2, 0\n" \
111111
" " #op " %0, %0, %1\n" \
112112
" s32i %0, %2, 0\n" \
@@ -124,7 +124,7 @@ static inline int atomic_##op##_return(int i, atomic_t * v) \
124124
unsigned int vval; \
125125
\
126126
__asm__ __volatile__( \
127-
" rsil a15,"__stringify(LOCKLEVEL)"\n" \
127+
" rsil a15,"__stringify(TOPLEVEL)"\n" \
128128
" l32i %0, %2, 0\n" \
129129
" " #op " %0, %0, %1\n" \
130130
" s32i %0, %2, 0\n" \
@@ -272,7 +272,7 @@ static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
272272
unsigned int vval;
273273

274274
__asm__ __volatile__(
275-
" rsil a15,"__stringify(LOCKLEVEL)"\n"
275+
" rsil a15,"__stringify(TOPLEVEL)"\n"
276276
" l32i %0, %2, 0\n"
277277
" xor %1, %4, %3\n"
278278
" and %0, %0, %4\n"
@@ -306,7 +306,7 @@ static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
306306
unsigned int vval;
307307

308308
__asm__ __volatile__(
309-
" rsil a15,"__stringify(LOCKLEVEL)"\n"
309+
" rsil a15,"__stringify(TOPLEVEL)"\n"
310310
" l32i %0, %2, 0\n"
311311
" or %0, %0, %1\n"
312312
" s32i %0, %2, 0\n"

‎arch/xtensa/include/asm/cmpxchg.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ __cmpxchg_u32(volatile int *p, int old, int new)
3434
return new;
3535
#else
3636
__asm__ __volatile__(
37-
" rsil a15, "__stringify(LOCKLEVEL)"\n"
37+
" rsil a15, "__stringify(TOPLEVEL)"\n"
3838
" l32i %0, %1, 0\n"
3939
" bne %0, %2, 1f\n"
4040
" s32i %3, %1, 0\n"
@@ -123,7 +123,7 @@ static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
123123
#else
124124
unsigned long tmp;
125125
__asm__ __volatile__(
126-
" rsil a15, "__stringify(LOCKLEVEL)"\n"
126+
" rsil a15, "__stringify(TOPLEVEL)"\n"
127127
" l32i %0, %1, 0\n"
128128
" s32i %2, %1, 0\n"
129129
" wsr a15, ps\n"

‎arch/xtensa/include/asm/irqflags.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* for more details.
77
*
88
* Copyright (C) 2001 - 2005 Tensilica Inc.
9+
* Copyright (C) 2015 Cadence Design Systems Inc.
910
*/
1011

1112
#ifndef _XTENSA_IRQFLAGS_H
@@ -23,8 +24,27 @@ static inline unsigned long arch_local_save_flags(void)
2324
static inline unsigned long arch_local_irq_save(void)
2425
{
2526
unsigned long flags;
26-
asm volatile("rsil %0, "__stringify(LOCKLEVEL)
27+
#if XTENSA_FAKE_NMI
28+
#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
29+
unsigned long tmp;
30+
31+
asm volatile("rsr %0, ps\t\n"
32+
"extui %1, %0, 0, 4\t\n"
33+
"bgei %1, "__stringify(LOCKLEVEL)", 1f\t\n"
34+
"rsil %0, "__stringify(LOCKLEVEL)"\n"
35+
"1:"
36+
: "=a" (flags), "=a" (tmp) :: "memory");
37+
#else
38+
asm volatile("rsr %0, ps\t\n"
39+
"or %0, %0, %1\t\n"
40+
"xsr %0, ps\t\n"
41+
"rsync"
42+
: "=&a" (flags) : "a" (LOCKLEVEL) : "memory");
43+
#endif
44+
#else
45+
asm volatile("rsil %0, "__stringify(LOCKLEVEL)
2746
: "=a" (flags) :: "memory");
47+
#endif
2848
return flags;
2949
}
3050

‎arch/xtensa/include/asm/processor.h‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/*
2-
* include/asm-xtensa/processor.h
3-
*
42
* This file is subject to the terms and conditions of the GNU General Public
53
* License. See the file "COPYING" in the main directory of this archive
64
* for more details.
75
*
86
* Copyright (C) 2001 - 2008 Tensilica Inc.
7+
* Copyright (C) 2015 Cadence Design Systems Inc.
98
*/
109

1110
#ifndef _XTENSA_PROCESSOR_H
@@ -44,6 +43,14 @@
4443
#define STACK_TOP TASK_SIZE
4544
#define STACK_TOP_MAX STACK_TOP
4645

46+
/*
47+
* General exception cause assigned to fake NMI. Fake NMI needs to be handled
48+
* differently from other interrupts, but it uses common kernel entry/exit
49+
* code.
50+
*/
51+
52+
#define EXCCAUSE_MAPPED_NMI 62
53+
4754
/*
4855
* General exception cause assigned to debug exceptions. Debug exceptions go
4956
* to their own vector, rather than the general exception vectors (user,
@@ -65,10 +72,30 @@
6572

6673
#define VALID_DOUBLE_EXCEPTION_ADDRESS 64
6774

75+
#define XTENSA_INT_LEVEL(intno) _XTENSA_INT_LEVEL(intno)
76+
#define _XTENSA_INT_LEVEL(intno) XCHAL_INT##intno##_LEVEL
77+
78+
#define XTENSA_INTLEVEL_MASK(level) _XTENSA_INTLEVEL_MASK(level)
79+
#define _XTENSA_INTLEVEL_MASK(level) (XCHAL_INTLEVEL##level##_MASK)
80+
81+
#define IS_POW2(v) (((v) & ((v) - 1)) == 0)
82+
83+
#define PROFILING_INTLEVEL XTENSA_INT_LEVEL(XCHAL_PROFILING_INTERRUPT)
84+
6885
/* LOCKLEVEL defines the interrupt level that masks all
6986
* general-purpose interrupts.
7087
*/
88+
#if defined(CONFIG_XTENSA_VARIANT_HAVE_PERF_EVENTS) && \
89+
defined(XCHAL_PROFILING_INTERRUPT) && \
90+
PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
91+
XCHAL_EXCM_LEVEL > 1 && \
92+
IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL))
93+
#define LOCKLEVEL (XCHAL_EXCM_LEVEL - 1)
94+
#else
7195
#define LOCKLEVEL XCHAL_EXCM_LEVEL
96+
#endif
97+
#define TOPLEVEL XCHAL_EXCM_LEVEL
98+
#define XTENSA_FAKE_NMI (LOCKLEVEL < TOPLEVEL)
7299

73100
/* WSBITS and WBBITS are the width of the WINDOWSTART and WINDOWBASE
74101
* registers

‎arch/xtensa/kernel/entry.S‎

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/*
2-
* arch/xtensa/kernel/entry.S
3-
*
42
* Low-level exception handling
53
*
64
* This file is subject to the terms and conditions of the GNU General Public
75
* License. See the file "COPYING" in the main directory of this archive
86
* for more details.
97
*
108
* Copyright (C) 2004 - 2008 by Tensilica Inc.
9+
* Copyright (C) 2015 Cadence Design Systems Inc.
1110
*
1211
* Chris Zankel <chris@zankel.net>
1312
*
@@ -75,6 +74,27 @@
7574
#endif
7675
.endm
7776

77+
78+
.macro irq_save flags tmp
79+
#if XTENSA_FAKE_NMI
80+
#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
81+
rsr \flags, ps
82+
extui \tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
83+
bgei \tmp, LOCKLEVEL, 99f
84+
rsil \tmp, LOCKLEVEL
85+
99:
86+
#else
87+
movi \tmp, LOCKLEVEL
88+
rsr \flags, ps
89+
or \flags, \flags, \tmp
90+
xsr \flags, ps
91+
rsync
92+
#endif
93+
#else
94+
rsil \flags, LOCKLEVEL
95+
#endif
96+
.endm
97+
7898
/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
7999

80100
/*
@@ -352,11 +372,11 @@ common_exception:
352372

353373
/* It is now save to restore the EXC_TABLE_FIXUP variable. */
354374

355-
rsr a0, exccause
375+
rsr a2, exccause
356376
movi a3, 0
357-
rsr a2, excsave1
358-
s32i a0, a1, PT_EXCCAUSE
359-
s32i a3, a2, EXC_TABLE_FIXUP
377+
rsr a0, excsave1
378+
s32i a2, a1, PT_EXCCAUSE
379+
s32i a3, a0, EXC_TABLE_FIXUP
360380

361381
/* All unrecoverable states are saved on stack, now, and a1 is valid.
362382
* Now we can allow exceptions again. In case we've got an interrupt
@@ -367,19 +387,46 @@ common_exception:
367387
*/
368388

369389
rsr a3, ps
370-
addi a0, a0, -EXCCAUSE_LEVEL1_INTERRUPT
371-
movi a2, LOCKLEVEL
390+
s32i a3, a1, PT_PS # save ps
391+
392+
#if XTENSA_FAKE_NMI
393+
/* Correct PS needs to be saved in the PT_PS:
394+
* - in case of exception or level-1 interrupt it's in the PS,
395+
* and is already saved.
396+
* - in case of medium level interrupt it's in the excsave2.
397+
*/
398+
movi a0, EXCCAUSE_MAPPED_NMI
399+
extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
400+
beq a2, a0, .Lmedium_level_irq
401+
bnei a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception
402+
beqz a3, .Llevel1_irq # level-1 IRQ sets ps.intlevel to 0
403+
404+
.Lmedium_level_irq:
405+
rsr a0, excsave2
406+
s32i a0, a1, PT_PS # save medium-level interrupt ps
407+
bgei a3, LOCKLEVEL, .Lexception
408+
409+
.Llevel1_irq:
410+
movi a3, LOCKLEVEL
411+
412+
.Lexception:
413+
movi a0, 1 << PS_WOE_BIT
414+
or a3, a3, a0
415+
#else
416+
addi a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT
417+
movi a0, LOCKLEVEL
372418
extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
373419
# a3 = PS.INTLEVEL
374-
moveqz a3, a2, a0 # a3 = LOCKLEVEL iff interrupt
420+
moveqz a3, a0, a2 # a3 = LOCKLEVEL iff interrupt
375421
movi a2, 1 << PS_WOE_BIT
376422
or a3, a3, a2
377423
rsr a2, exccause
424+
#endif
425+
378426
/* restore return address (or 0 if return to userspace) */
379427
rsr a0, depc
380-
xsr a3, ps
381-
382-
s32i a3, a1, PT_PS # save ps
428+
wsr a3, ps
429+
rsync # PS.WOE => rsync => overflow
383430

384431
/* Save lbeg, lend */
385432

@@ -417,8 +464,13 @@ common_exception:
417464
.global common_exception_return
418465
common_exception_return:
419466

467+
#if XTENSA_FAKE_NMI
468+
l32i a2, a1, PT_EXCCAUSE
469+
movi a3, EXCCAUSE_MAPPED_NMI
470+
beq a2, a3, .LNMIexit
471+
#endif
420472
1:
421-
rsil a2, LOCKLEVEL
473+
irq_save a2, a3
422474
#ifdef CONFIG_TRACE_IRQFLAGS
423475
movi a4, trace_hardirqs_off
424476
callx4 a4
@@ -481,6 +533,12 @@ common_exception_return:
481533
j 1b
482534
#endif
483535

536+
#if XTENSA_FAKE_NMI
537+
.LNMIexit:
538+
l32i a3, a1, PT_PS
539+
_bbci.l a3, PS_UM_BIT, 4f
540+
#endif
541+
484542
5:
485543
#ifdef CONFIG_DEBUG_TLB_SANITY
486544
l32i a4, a1, PT_DEPC
@@ -1564,6 +1622,13 @@ ENTRY(fast_second_level_miss)
15641622
rfde
15651623

15661624
9: l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 0
1625+
bnez a0, 8b
1626+
1627+
/* Even more unlikely case active_mm == 0.
1628+
* We can get here with NMI in the middle of context_switch that
1629+
* touches vmalloc area.
1630+
*/
1631+
movi a0, init_mm
15671632
j 8b
15681633

15691634
#if (DCACHE_WAY_SIZE > PAGE_SIZE)
@@ -1867,7 +1932,7 @@ ENTRY(_switch_to)
18671932

18681933
/* Disable ints while we manipulate the stack pointer. */
18691934

1870-
rsil a14, LOCKLEVEL
1935+
irq_save a14, a3
18711936
rsync
18721937

18731938
/* Switch CPENABLE */

‎arch/xtensa/kernel/irq.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <asm/platform.h>
3030

3131
atomic_t irq_err_count;
32+
DECLARE_PER_CPU(unsigned long, nmi_count);
3233

3334
asmlinkage void do_IRQ(int hwirq, struct pt_regs *regs)
3435
{
@@ -57,11 +58,18 @@ asmlinkage void do_IRQ(int hwirq, struct pt_regs *regs)
5758

5859
int arch_show_interrupts(struct seq_file *p, int prec)
5960
{
61+
unsigned cpu __maybe_unused;
6062
#ifdef CONFIG_SMP
6163
show_ipi_list(p, prec);
6264
#endif
6365
seq_printf(p, "%*s: ", prec, "ERR");
6466
seq_printf(p, "%10u\n", atomic_read(&irq_err_count));
67+
#if XTENSA_FAKE_NMI
68+
seq_printf(p, "%*s:", prec, "NMI");
69+
for_each_online_cpu(cpu)
70+
seq_printf(p, " %10lu", per_cpu(nmi_count, cpu));
71+
seq_puts(p, " Non-maskable interrupts\n");
72+
#endif
6573
return 0;
6674
}
6775

‎arch/xtensa/kernel/perf_event.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ void perf_event_print_debug(void)
359359
local_irq_restore(flags);
360360
}
361361

362-
static irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id)
362+
irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id)
363363
{
364364
irqreturn_t rc = IRQ_NONE;
365365
struct xtensa_pmu_events *ev = this_cpu_ptr(&xtensa_pmu_events);
@@ -436,10 +436,14 @@ static int __init xtensa_pmu_init(void)
436436
int irq = irq_create_mapping(NULL, XCHAL_PROFILING_INTERRUPT);
437437

438438
perf_cpu_notifier(xtensa_pmu_notifier);
439+
#if XTENSA_FAKE_NMI
440+
enable_irq(irq);
441+
#else
439442
ret = request_irq(irq, xtensa_pmu_irq_handler, IRQF_PERCPU,
440443
"pmu", NULL);
441444
if (ret < 0)
442445
return ret;
446+
#endif
443447

444448
ret = perf_pmu_register(&xtensa_pmu, "cpu", PERF_TYPE_RAW);
445449
if (ret)

0 commit comments

Comments
 (0)