Skip to content

Commit 1efe4ce

Browse files
Stuart Menefypmundt
authored andcommitted
sh: GUSA atomic rollback support.
This implements kernel-level atomic rollback built on top of gUSA, as an alternative non-IRQ based atomicity method. This is generally a faster method for platforms that are lacking the LL/SC pairs that SH-4A and later use, and is only supportable on legacy cores. Signed-off-by: Stuart Menefy <stuart.menefy@st.com> Signed-off-by: Paul Mundt <lethal@linux-sh.org>
1 parent 53ff094 commit 1efe4ce

13 files changed

Lines changed: 591 additions & 181 deletions

File tree

‎arch/sh/Kconfig‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ source "kernel/Kconfig.preempt"
692692

693693
config GUSA
694694
def_bool y
695-
depends on !SMP
695+
depends on !SMP && SUPERH32
696696
help
697697
This enables support for gUSA (general UserSpace Atomicity).
698698
This is the default implementation for both UP and non-ll/sc
@@ -704,6 +704,16 @@ config GUSA
704704
This should only be disabled for special cases where alternate
705705
atomicity implementations exist.
706706

707+
config GUSA_RB
708+
bool "Implement atomic operations by roll-back (gRB) (EXPERIMENTAL)"
709+
depends on GUSA && CPU_SH3 || (CPU_SH4 && !CPU_SH4A)
710+
help
711+
Enabling this option will allow the kernel to implement some
712+
atomic operations using a software implemention of load-locked/
713+
store-conditional (LLSC). On machines which do not have hardware
714+
LLSC, this should be more efficient than the other alternative of
715+
disabling insterrupts around the atomic sequence.
716+
707717
endmenu
708718

709719
menu "Boot options"

‎arch/sh/kernel/cpu/sh3/entry.S‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#include <linux/linkage.h>
1414
#include <asm/asm-offsets.h>
1515
#include <asm/thread_info.h>
16-
#include <asm/cpu/mmu_context.h>
1716
#include <asm/unistd.h>
17+
#include <asm/cpu/mmu_context.h>
18+
#include <asm/page.h>
1819

1920
! NOTE:
2021
! GNU as (as of 2.9.1) changes bf/s into bt/s and bra, when the address
@@ -409,6 +410,27 @@ ENTRY(handle_exception)
409410
! Using k0, k1 for scratch registers (r0_bank1, r1_bank),
410411
! save all registers onto stack.
411412
!
413+
414+
#ifdef CONFIG_GUSA
415+
! Check for roll back gRB (User and Kernel)
416+
mov r15, k0
417+
shll k0
418+
bf/s 1f
419+
shll k0
420+
bf/s 1f
421+
stc spc, k1
422+
stc r0_bank, k0
423+
cmp/hs k0, k1 ! test k1 (saved PC) >= k0 (saved r0)
424+
bt/s 2f
425+
stc r1_bank, k1
426+
427+
add #-2, k0
428+
add r15, k0
429+
ldc k0, spc ! PC = saved r0 + r15 - 2
430+
2: mov k1, r15 ! SP = r1
431+
1:
432+
#endif
433+
412434
stc ssr, k0 ! Is it from kernel space?
413435
shll k0 ! Check MD bit (bit30) by shifting it into...
414436
shll k0 ! ...the T bit

‎arch/sh/kernel/entry-common.S‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,6 @@ work_notifysig:
176176
jmp @r1
177177
lds r0, pr
178178
work_resched:
179-
#if defined(CONFIG_GUSA) && !defined(CONFIG_PREEMPT)
180-
! gUSA handling
181-
mov.l @(OFF_SP,r15), r0 ! get user space stack pointer
182-
mov r0, r1
183-
shll r0
184-
bf/s 1f
185-
shll r0
186-
bf/s 1f
187-
mov #OFF_PC, r0
188-
! SP >= 0xc0000000 : gUSA mark
189-
mov.l @(r0,r15), r2 ! get user space PC (program counter)
190-
mov.l @(OFF_R0,r15), r3 ! end point
191-
cmp/hs r3, r2 ! r2 >= r3?
192-
bt 1f
193-
add r3, r1 ! rewind point #2
194-
mov.l r1, @(r0,r15) ! reset PC to rewind point #2
195-
!
196-
1:
197-
#endif
198179
mov.l 1f, r1
199180
jsr @r1 ! schedule
200181
nop

‎arch/sh/kernel/process_32.c‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -322,25 +322,6 @@ struct task_struct *__switch_to(struct task_struct *prev,
322322
unlazy_fpu(prev, task_pt_regs(prev));
323323
#endif
324324

325-
#if defined(CONFIG_GUSA) && defined(CONFIG_PREEMPT)
326-
{
327-
struct pt_regs *regs;
328-
329-
preempt_disable();
330-
regs = task_pt_regs(prev);
331-
if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
332-
int offset = (int)regs->regs[15];
333-
334-
/* Reset stack pointer: clear critical region mark */
335-
regs->regs[15] = regs->regs[1];
336-
if (regs->pc < regs->regs[0])
337-
/* Go to rewind point */
338-
regs->pc = regs->regs[0] + offset;
339-
}
340-
preempt_enable_no_resched();
341-
}
342-
#endif
343-
344325
#ifdef CONFIG_MMU
345326
/*
346327
* Restore the kernel mode register

‎arch/sh/kernel/signal_32.c‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -507,24 +507,6 @@ handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
507507
ctrl_inw(regs->pc - 4));
508508
break;
509509
}
510-
#ifdef CONFIG_GUSA
511-
} else {
512-
/* gUSA handling */
513-
preempt_disable();
514-
515-
if (regs->regs[15] >= 0xc0000000) {
516-
int offset = (int)regs->regs[15];
517-
518-
/* Reset stack pointer: clear critical region mark */
519-
regs->regs[15] = regs->regs[1];
520-
if (regs->pc < regs->regs[0])
521-
/* Go to rewind point #1 */
522-
regs->pc = regs->regs[0] + offset -
523-
instruction_size(ctrl_inw(regs->pc-4));
524-
}
525-
526-
preempt_enable_no_resched();
527-
#endif
528510
}
529511

530512
/* Set up the stack frame */

‎include/asm-sh/atomic-grb.h‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#ifndef __ASM_SH_ATOMIC_GRB_H
2+
#define __ASM_SH_ATOMIC_GRB_H
3+
4+
static inline void atomic_add(int i, atomic_t *v)
5+
{
6+
int tmp;
7+
8+
__asm__ __volatile__ (
9+
" .align 2 \n\t"
10+
" mova 1f, r0 \n\t" /* r0 = end point */
11+
" mov r15, r1 \n\t" /* r1 = saved sp */
12+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
13+
" mov.l @%1, %0 \n\t" /* load old value */
14+
" add %2, %0 \n\t" /* add */
15+
" mov.l %0, @%1 \n\t" /* store new value */
16+
"1: mov r1, r15 \n\t" /* LOGOUT */
17+
: "=&r" (tmp),
18+
"+r" (v)
19+
: "r" (i)
20+
: "memory" , "r0", "r1");
21+
}
22+
23+
static inline void atomic_sub(int i, atomic_t *v)
24+
{
25+
int tmp;
26+
27+
__asm__ __volatile__ (
28+
" .align 2 \n\t"
29+
" mova 1f, r0 \n\t" /* r0 = end point */
30+
" mov r15, r1 \n\t" /* r1 = saved sp */
31+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
32+
" mov.l @%1, %0 \n\t" /* load old value */
33+
" sub %2, %0 \n\t" /* sub */
34+
" mov.l %0, @%1 \n\t" /* store new value */
35+
"1: mov r1, r15 \n\t" /* LOGOUT */
36+
: "=&r" (tmp),
37+
"+r" (v)
38+
: "r" (i)
39+
: "memory" , "r0", "r1");
40+
}
41+
42+
static inline int atomic_add_return(int i, atomic_t *v)
43+
{
44+
int tmp;
45+
46+
__asm__ __volatile__ (
47+
" .align 2 \n\t"
48+
" mova 1f, r0 \n\t" /* r0 = end point */
49+
" mov r15, r1 \n\t" /* r1 = saved sp */
50+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
51+
" mov.l @%1, %0 \n\t" /* load old value */
52+
" add %2, %0 \n\t" /* add */
53+
" mov.l %0, @%1 \n\t" /* store new value */
54+
"1: mov r1, r15 \n\t" /* LOGOUT */
55+
: "=&r" (tmp),
56+
"+r" (v)
57+
: "r" (i)
58+
: "memory" , "r0", "r1");
59+
60+
return tmp;
61+
}
62+
63+
static inline int atomic_sub_return(int i, atomic_t *v)
64+
{
65+
int tmp;
66+
67+
__asm__ __volatile__ (
68+
" .align 2 \n\t"
69+
" mova 1f, r0 \n\t" /* r0 = end point */
70+
" mov r15, r1 \n\t" /* r1 = saved sp */
71+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
72+
" mov.l @%1, %0 \n\t" /* load old value */
73+
" sub %2, %0 \n\t" /* sub */
74+
" mov.l %0, @%1 \n\t" /* store new value */
75+
"1: mov r1, r15 \n\t" /* LOGOUT */
76+
: "=&r" (tmp),
77+
"+r" (v)
78+
: "r" (i)
79+
: "memory", "r0", "r1");
80+
81+
return tmp;
82+
}
83+
84+
static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
85+
{
86+
int tmp;
87+
unsigned int _mask = ~mask;
88+
89+
__asm__ __volatile__ (
90+
" .align 2 \n\t"
91+
" mova 1f, r0 \n\t" /* r0 = end point */
92+
" mov r15, r1 \n\t" /* r1 = saved sp */
93+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
94+
" mov.l @%1, %0 \n\t" /* load old value */
95+
" and %2, %0 \n\t" /* add */
96+
" mov.l %0, @%1 \n\t" /* store new value */
97+
"1: mov r1, r15 \n\t" /* LOGOUT */
98+
: "=&r" (tmp),
99+
"+r" (v)
100+
: "r" (_mask)
101+
: "memory" , "r0", "r1");
102+
}
103+
104+
static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
105+
{
106+
int tmp;
107+
108+
__asm__ __volatile__ (
109+
" .align 2 \n\t"
110+
" mova 1f, r0 \n\t" /* r0 = end point */
111+
" mov r15, r1 \n\t" /* r1 = saved sp */
112+
" mov #-6, r15 \n\t" /* LOGIN: r15 = size */
113+
" mov.l @%1, %0 \n\t" /* load old value */
114+
" or %2, %0 \n\t" /* or */
115+
" mov.l %0, @%1 \n\t" /* store new value */
116+
"1: mov r1, r15 \n\t" /* LOGOUT */
117+
: "=&r" (tmp),
118+
"+r" (v)
119+
: "r" (mask)
120+
: "memory" , "r0", "r1");
121+
}
122+
123+
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
124+
{
125+
int ret;
126+
127+
__asm__ __volatile__ (
128+
" .align 2 \n\t"
129+
" mova 1f, r0 \n\t"
130+
" nop \n\t"
131+
" mov r15, r1 \n\t"
132+
" mov #-8, r15 \n\t"
133+
" mov.l @%1, %0 \n\t"
134+
" cmp/eq %2, %0 \n\t"
135+
" bf 1f \n\t"
136+
" mov.l %3, @%1 \n\t"
137+
"1: mov r1, r15 \n\t"
138+
: "=&r" (ret)
139+
: "r" (v), "r" (old), "r" (new)
140+
: "memory" , "r0", "r1" , "t");
141+
142+
return ret;
143+
}
144+
145+
static inline int atomic_add_unless(atomic_t *v, int a, int u)
146+
{
147+
int ret;
148+
unsigned long tmp;
149+
150+
__asm__ __volatile__ (
151+
" .align 2 \n\t"
152+
" mova 1f, r0 \n\t"
153+
" nop \n\t"
154+
" mov r15, r1 \n\t"
155+
" mov #-12, r15 \n\t"
156+
" mov.l @%2, %1 \n\t"
157+
" mov %1, %0 \n\t"
158+
" cmp/eq %4, %0 \n\t"
159+
" bt/s 1f \n\t"
160+
" add %3, %1 \n\t"
161+
" mov.l %1, @%2 \n\t"
162+
"1: mov r1, r15 \n\t"
163+
: "=&r" (ret), "=&r" (tmp)
164+
: "r" (v), "r" (a), "r" (u)
165+
: "memory" , "r0", "r1" , "t");
166+
167+
return ret != u;
168+
}
169+
#endif /* __ASM_SH_ATOMIC_GRB_H */

‎include/asm-sh/atomic.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ typedef struct { volatile int counter; } atomic_t;
1717
#include <linux/compiler.h>
1818
#include <asm/system.h>
1919

20-
#ifdef CONFIG_CPU_SH4A
20+
#if defined(CONFIG_GUSA_RB)
21+
#include <asm/atomic-grb.h>
22+
#elif defined(CONFIG_CPU_SH4A)
2123
#include <asm/atomic-llsc.h>
2224
#else
2325
#include <asm/atomic-irq.h>
@@ -44,6 +46,7 @@ typedef struct { volatile int counter; } atomic_t;
4446
#define atomic_inc(v) atomic_add(1,(v))
4547
#define atomic_dec(v) atomic_sub(1,(v))
4648

49+
#ifndef CONFIG_GUSA_RB
4750
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
4851
{
4952
int ret;
@@ -58,8 +61,6 @@ static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
5861
return ret;
5962
}
6063

61-
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
62-
6364
static inline int atomic_add_unless(atomic_t *v, int a, int u)
6465
{
6566
int ret;
@@ -73,6 +74,9 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
7374

7475
return ret != u;
7576
}
77+
#endif
78+
79+
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
7680
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
7781

7882
/* Atomic operations are already serializing on SH */

0 commit comments

Comments
 (0)