2 * Copyright (C) 2015 Regents of the University of California
3 * Copyright (C) 2017 SiFive
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation, version 2.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #ifndef _ASM_RISCV_SPINLOCK_H
16 #define _ASM_RISCV_SPINLOCK_H
18 #include <linux/kernel.h>
19 #include <asm/current.h>
22 * Simple spin lock operations. These provide no fairness guarantees.
25 /* FIXME: Replace this with a ticket lock, like MIPS. */
27 #define arch_spin_is_locked(x) (READ_ONCE((x)->lock) != 0)
29 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
31 __asm__
__volatile__ (
32 "amoswap.w.rl x0, x0, %0"
37 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
41 __asm__
__volatile__ (
42 "amoswap.w.aq %0, %2, %1"
43 : "=r" (busy
), "+A" (lock
->lock
)
50 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
53 if (arch_spin_is_locked(lock
))
56 if (arch_spin_trylock(lock
))
61 /***********************************************************/
63 static inline void arch_read_lock(arch_rwlock_t
*lock
)
71 " sc.w.aq %1, %1, %0\n"
73 : "+A" (lock
->lock
), "=&r" (tmp
)
77 static inline void arch_write_lock(arch_rwlock_t
*lock
)
85 " sc.w.aq %1, %1, %0\n"
87 : "+A" (lock
->lock
), "=&r" (tmp
)
91 static inline int arch_read_trylock(arch_rwlock_t
*lock
)
99 " sc.w.aq %1, %1, %0\n"
102 : "+A" (lock
->lock
), "=&r" (busy
)
108 static inline int arch_write_trylock(arch_rwlock_t
*lock
)
112 __asm__
__volatile__(
116 " sc.w.aq %1, %1, %0\n"
119 : "+A" (lock
->lock
), "=&r" (busy
)
125 static inline void arch_read_unlock(arch_rwlock_t
*lock
)
127 __asm__
__volatile__(
128 "amoadd.w.rl x0, %1, %0"
134 static inline void arch_write_unlock(arch_rwlock_t
*lock
)
136 __asm__
__volatile__ (
137 "amoswap.w.rl x0, x0, %0"
142 #endif /* _ASM_RISCV_SPINLOCK_H */