2012-03-05 18:49:30 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#ifndef __ASM_SPINLOCK_H
|
|
|
|
#define __ASM_SPINLOCK_H
|
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
#include <asm/lse.h>
|
2012-03-05 18:49:30 +07:00
|
|
|
#include <asm/spinlock_types.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Spinlock implementation.
|
|
|
|
*
|
|
|
|
* The memory barriers are implicit with the load-acquire and store-release
|
|
|
|
* instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void arch_spin_lock(arch_spinlock_t *lock)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
2013-10-09 21:54:26 +07:00
|
|
|
arch_spinlock_t lockval, newval;
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
asm volatile(
|
2013-10-09 21:54:26 +07:00
|
|
|
/* Atomically increment the next ticket. */
|
2015-02-10 10:03:15 +07:00
|
|
|
ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2013-10-09 21:54:26 +07:00
|
|
|
" prfm pstl1strm, %3\n"
|
|
|
|
"1: ldaxr %w0, %3\n"
|
|
|
|
" add %w1, %w0, %w5\n"
|
|
|
|
" stxr %w2, %w1, %3\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
" cbnz %w2, 1b\n",
|
|
|
|
/* LSE atomics */
|
|
|
|
" mov %w2, %w5\n"
|
|
|
|
" ldadda %w2, %w0, %3\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
__nops(3)
|
2015-02-10 10:03:15 +07:00
|
|
|
)
|
|
|
|
|
2013-10-09 21:54:26 +07:00
|
|
|
/* Did we get the lock? */
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbz %w1, 3f\n"
|
|
|
|
/*
|
|
|
|
* No: spin on the owner. Send a local event to avoid missing an
|
|
|
|
* unlock before the exclusive load.
|
|
|
|
*/
|
|
|
|
" sevl\n"
|
|
|
|
"2: wfe\n"
|
|
|
|
" ldaxrh %w2, %4\n"
|
|
|
|
" eor %w1, %w2, %w0, lsr #16\n"
|
|
|
|
" cbnz %w1, 2b\n"
|
|
|
|
/* We got the lock. Critical section starts here. */
|
|
|
|
"3:"
|
|
|
|
: "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
|
|
|
|
: "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
|
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_spin_trylock(arch_spinlock_t *lock)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
2013-10-09 21:54:26 +07:00
|
|
|
arch_spinlock_t lockval;
|
2012-03-05 18:49:30 +07:00
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
|
|
|
" prfm pstl1strm, %2\n"
|
|
|
|
"1: ldaxr %w0, %2\n"
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbnz %w1, 2f\n"
|
|
|
|
" add %w0, %w0, %3\n"
|
|
|
|
" stxr %w1, %w0, %2\n"
|
|
|
|
" cbnz %w1, 1b\n"
|
|
|
|
"2:",
|
|
|
|
/* LSE atomics */
|
|
|
|
" ldr %w0, %2\n"
|
|
|
|
" eor %w1, %w0, %w0, ror #16\n"
|
|
|
|
" cbnz %w1, 1f\n"
|
|
|
|
" add %w1, %w0, %3\n"
|
|
|
|
" casa %w0, %w1, %2\n"
|
|
|
|
" and %w1, %w1, #0xffff\n"
|
|
|
|
" eor %w1, %w1, %w0, lsr #16\n"
|
|
|
|
"1:")
|
2013-10-09 21:54:26 +07:00
|
|
|
: "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
|
|
|
|
: "I" (1 << TICKET_SHIFT)
|
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
return !tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_spin_unlock(arch_spinlock_t *lock)
|
|
|
|
{
|
2015-02-10 10:03:15 +07:00
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2015-07-28 20:48:00 +07:00
|
|
|
" ldrh %w1, %0\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
" add %w1, %w1, #1\n"
|
|
|
|
" stlrh %w1, %0",
|
|
|
|
/* LSE atomics */
|
|
|
|
" mov %w1, #1\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
" staddlh %w1, %0\n"
|
|
|
|
__nops(1))
|
2015-02-10 10:03:15 +07:00
|
|
|
: "=Q" (lock->owner), "=&r" (tmp)
|
|
|
|
:
|
2013-10-09 21:54:26 +07:00
|
|
|
: "memory");
|
|
|
|
}
|
|
|
|
|
2013-10-09 21:54:27 +07:00
|
|
|
static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
|
|
|
|
{
|
|
|
|
return lock.owner == lock.next;
|
|
|
|
}
|
|
|
|
|
2013-10-09 21:54:26 +07:00
|
|
|
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
|
|
|
|
{
|
2017-06-30 05:53:02 +07:00
|
|
|
/*
|
|
|
|
* Ensure prior spin_lock operations to other locks have completed
|
|
|
|
* on this CPU before we test whether "lock" is locked.
|
|
|
|
*/
|
|
|
|
smp_mb(); /* ^^^ */
|
2014-11-24 16:53:11 +07:00
|
|
|
return !arch_spin_value_unlocked(READ_ONCE(*lock));
|
2013-10-09 21:54:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_spin_is_contended(arch_spinlock_t *lock)
|
|
|
|
{
|
2014-11-24 16:53:11 +07:00
|
|
|
arch_spinlock_t lockval = READ_ONCE(*lock);
|
2013-10-09 21:54:26 +07:00
|
|
|
return (lockval.next - lockval.owner) > 1;
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
2013-10-09 21:54:26 +07:00
|
|
|
#define arch_spin_is_contended arch_spin_is_contended
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write lock implementation.
|
|
|
|
*
|
|
|
|
* Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
|
|
|
|
* exclusively held.
|
|
|
|
*
|
|
|
|
* The memory barriers are implicit with the load-acquire and store-release
|
|
|
|
* instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void arch_write_lock(arch_rwlock_t *rw)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2012-03-05 18:49:30 +07:00
|
|
|
" sevl\n"
|
|
|
|
"1: wfe\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
"2: ldaxr %w0, %1\n"
|
2012-03-05 18:49:30 +07:00
|
|
|
" cbnz %w0, 1b\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
" stxr %w0, %w2, %1\n"
|
2012-03-05 18:49:30 +07:00
|
|
|
" cbnz %w0, 2b\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
__nops(1),
|
2015-02-10 10:03:15 +07:00
|
|
|
/* LSE atomics */
|
|
|
|
"1: mov %w0, wzr\n"
|
|
|
|
"2: casa %w0, %w2, %1\n"
|
|
|
|
" cbz %w0, 3f\n"
|
|
|
|
" ldxr %w0, %1\n"
|
|
|
|
" cbz %w0, 2b\n"
|
|
|
|
" wfe\n"
|
|
|
|
" b 1b\n"
|
|
|
|
"3:")
|
2013-02-04 19:12:33 +07:00
|
|
|
: "=&r" (tmp), "+Q" (rw->lock)
|
|
|
|
: "r" (0x80000000)
|
2014-02-04 19:29:13 +07:00
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_write_trylock(arch_rwlock_t *rw)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2015-07-23 00:25:52 +07:00
|
|
|
"1: ldaxr %w0, %1\n"
|
|
|
|
" cbnz %w0, 2f\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
" stxr %w0, %w2, %1\n"
|
2015-07-23 00:25:52 +07:00
|
|
|
" cbnz %w0, 1b\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
"2:",
|
|
|
|
/* LSE atomics */
|
|
|
|
" mov %w0, wzr\n"
|
|
|
|
" casa %w0, %w2, %1\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
__nops(2))
|
2013-02-04 19:12:33 +07:00
|
|
|
: "=&r" (tmp), "+Q" (rw->lock)
|
|
|
|
: "r" (0x80000000)
|
2014-02-04 19:29:13 +07:00
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
return !tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_write_unlock(arch_rwlock_t *rw)
|
|
|
|
{
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
" stlr wzr, %0",
|
|
|
|
" swpl wzr, wzr, %0")
|
|
|
|
: "=Q" (rw->lock) :: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write_can_lock - would write_trylock() succeed? */
|
|
|
|
#define arch_write_can_lock(x) ((x)->lock == 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read lock implementation.
|
|
|
|
*
|
|
|
|
* It exclusively loads the lock value, increments it and stores the new value
|
|
|
|
* back if positive and the CPU still exclusively owns the location. If the
|
|
|
|
* value is negative, the lock is already held.
|
|
|
|
*
|
|
|
|
* During unlocking there may be multiple active read locks but no write lock.
|
|
|
|
*
|
|
|
|
* The memory barriers are implicit with the load-acquire and store-release
|
|
|
|
* instructions.
|
2015-02-10 10:03:15 +07:00
|
|
|
*
|
|
|
|
* Note that in UNDEFINED cases, such as unlocking a lock twice, the LL/SC
|
|
|
|
* and LSE implementations may exhibit different behaviour (although this
|
|
|
|
* will have no effect on lockdep).
|
2012-03-05 18:49:30 +07:00
|
|
|
*/
|
|
|
|
static inline void arch_read_lock(arch_rwlock_t *rw)
|
|
|
|
{
|
|
|
|
unsigned int tmp, tmp2;
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
" sevl\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2012-03-05 18:49:30 +07:00
|
|
|
"1: wfe\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
"2: ldaxr %w0, %2\n"
|
2012-03-05 18:49:30 +07:00
|
|
|
" add %w0, %w0, #1\n"
|
|
|
|
" tbnz %w0, #31, 1b\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
" stxr %w1, %w0, %2\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
" cbnz %w1, 2b\n"
|
|
|
|
__nops(1),
|
2015-02-10 10:03:15 +07:00
|
|
|
/* LSE atomics */
|
|
|
|
"1: wfe\n"
|
|
|
|
"2: ldxr %w0, %2\n"
|
|
|
|
" adds %w1, %w0, #1\n"
|
|
|
|
" tbnz %w1, #31, 1b\n"
|
|
|
|
" casa %w0, %w1, %2\n"
|
|
|
|
" sbc %w0, %w1, %w0\n"
|
|
|
|
" cbnz %w0, 2b")
|
2013-02-04 19:12:33 +07:00
|
|
|
: "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
|
|
|
|
:
|
2015-02-10 10:03:15 +07:00
|
|
|
: "cc", "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_read_unlock(arch_rwlock_t *rw)
|
|
|
|
{
|
|
|
|
unsigned int tmp, tmp2;
|
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
2013-02-04 19:12:33 +07:00
|
|
|
"1: ldxr %w0, %2\n"
|
2012-03-05 18:49:30 +07:00
|
|
|
" sub %w0, %w0, #1\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
" stlxr %w1, %w0, %2\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
" cbnz %w1, 1b",
|
|
|
|
/* LSE atomics */
|
|
|
|
" movn %w0, #0\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
" staddl %w0, %2\n"
|
|
|
|
__nops(2))
|
2013-02-04 19:12:33 +07:00
|
|
|
: "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
|
|
|
|
:
|
2014-02-04 19:29:13 +07:00
|
|
|
: "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int arch_read_trylock(arch_rwlock_t *rw)
|
|
|
|
{
|
2015-02-10 10:03:15 +07:00
|
|
|
unsigned int tmp, tmp2;
|
2012-03-05 18:49:30 +07:00
|
|
|
|
2015-02-10 10:03:15 +07:00
|
|
|
asm volatile(ARM64_LSE_ATOMIC_INSN(
|
|
|
|
/* LL/SC */
|
|
|
|
" mov %w1, #1\n"
|
2015-07-23 00:25:52 +07:00
|
|
|
"1: ldaxr %w0, %2\n"
|
2012-03-05 18:49:30 +07:00
|
|
|
" add %w0, %w0, #1\n"
|
2015-07-23 00:25:52 +07:00
|
|
|
" tbnz %w0, #31, 2f\n"
|
2013-02-04 19:12:33 +07:00
|
|
|
" stxr %w1, %w0, %2\n"
|
2015-07-23 00:25:52 +07:00
|
|
|
" cbnz %w1, 1b\n"
|
2015-02-10 10:03:15 +07:00
|
|
|
"2:",
|
|
|
|
/* LSE atomics */
|
|
|
|
" ldr %w0, %2\n"
|
|
|
|
" adds %w1, %w0, #1\n"
|
|
|
|
" tbnz %w1, #31, 1f\n"
|
|
|
|
" casa %w0, %w1, %2\n"
|
|
|
|
" sbc %w1, %w1, %w0\n"
|
2016-09-06 22:42:58 +07:00
|
|
|
__nops(1)
|
2015-02-10 10:03:15 +07:00
|
|
|
"1:")
|
|
|
|
: "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
|
2013-02-04 19:12:33 +07:00
|
|
|
:
|
2015-02-10 10:03:15 +07:00
|
|
|
: "cc", "memory");
|
2012-03-05 18:49:30 +07:00
|
|
|
|
|
|
|
return !tmp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read_can_lock - would read_trylock() succeed? */
|
|
|
|
#define arch_read_can_lock(x) ((x)->lock < 0x80000000)
|
|
|
|
|
locking: Introduce smp_mb__after_spinlock()
Since its inception, our understanding of ACQUIRE, esp. as applied to
spinlocks, has changed somewhat. Also, I wonder if, with a simple
change, we cannot make it provide more.
The problem with the comment is that the STORE done by spin_lock isn't
itself ordered by the ACQUIRE, and therefore a later LOAD can pass over
it and cross with any prior STORE, rendering the default WMB
insufficient (pointed out by Alan).
Now, this is only really a problem on PowerPC and ARM64, both of
which already defined smp_mb__before_spinlock() as a smp_mb().
At the same time, we can get a much stronger construct if we place
that same barrier _inside_ the spin_lock(). In that case we upgrade
the RCpc spinlock to an RCsc. That would make all schedule() calls
fully transitive against one another.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Will Deacon <will.deacon@arm.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Paul McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-09-05 16:37:53 +07:00
|
|
|
/* See include/linux/spinlock.h */
|
|
|
|
#define smp_mb__after_spinlock() smp_mb()
|
2016-09-05 17:56:05 +07:00
|
|
|
|
2012-03-05 18:49:30 +07:00
|
|
|
#endif /* __ASM_SPINLOCK_H */
|