Commit 52ea2a56 authored by Will Deacon's avatar Will Deacon Committed by Catalin Marinas

arm64: locks: introduce ticket-based spinlock implementation

This patch introduces a ticket lock implementation for arm64, along the
same lines as the implementation for arch/arm/.
Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Signed-off-by: default avatarWill Deacon <will.deacon@arm.com>
Signed-off-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
parent e29a074b
...@@ -22,17 +22,10 @@ ...@@ -22,17 +22,10 @@
/* /*
* Spinlock implementation. * Spinlock implementation.
* *
* The old value is read exclusively and the new one, if unlocked, is written
* exclusively. In case of failure, the loop is restarted.
*
* The memory barriers are implicit with the load-acquire and store-release * The memory barriers are implicit with the load-acquire and store-release
* instructions. * instructions.
*
* Unlocked value: 0
* Locked value: 1
*/ */
#define arch_spin_is_locked(x) ((x)->lock != 0)
#define arch_spin_unlock_wait(lock) \ #define arch_spin_unlock_wait(lock) \
do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0) do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
...@@ -41,32 +34,51 @@ ...@@ -41,32 +34,51 @@
static inline void arch_spin_lock(arch_spinlock_t *lock) static inline void arch_spin_lock(arch_spinlock_t *lock)
{ {
unsigned int tmp; unsigned int tmp;
arch_spinlock_t lockval, newval;
asm volatile( asm volatile(
" sevl\n" /* Atomically increment the next ticket. */
"1: wfe\n" " prfm pstl1strm, %3\n"
"2: ldaxr %w0, %1\n" "1: ldaxr %w0, %3\n"
" cbnz %w0, 1b\n" " add %w1, %w0, %w5\n"
" stxr %w0, %w2, %1\n" " stxr %w2, %w1, %3\n"
" cbnz %w0, 2b\n" " cbnz %w2, 1b\n"
: "=&r" (tmp), "+Q" (lock->lock) /* Did we get the lock? */
: "r" (1) " eor %w1, %w0, %w0, ror #16\n"
: "cc", "memory"); " 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");
} }
static inline int arch_spin_trylock(arch_spinlock_t *lock) static inline int arch_spin_trylock(arch_spinlock_t *lock)
{ {
unsigned int tmp; unsigned int tmp;
arch_spinlock_t lockval;
asm volatile( asm volatile(
"2: ldaxr %w0, %1\n" " prfm pstl1strm, %2\n"
" cbnz %w0, 1f\n" "1: ldaxr %w0, %2\n"
" stxr %w0, %w2, %1\n" " eor %w1, %w0, %w0, ror #16\n"
" cbnz %w0, 2b\n" " cbnz %w1, 2f\n"
"1:\n" " add %w0, %w0, %3\n"
: "=&r" (tmp), "+Q" (lock->lock) " stxr %w1, %w0, %2\n"
: "r" (1) " cbnz %w1, 1b\n"
: "cc", "memory"); "2:"
: "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
: "I" (1 << TICKET_SHIFT)
: "memory");
return !tmp; return !tmp;
} }
...@@ -74,9 +86,24 @@ static inline int arch_spin_trylock(arch_spinlock_t *lock) ...@@ -74,9 +86,24 @@ static inline int arch_spin_trylock(arch_spinlock_t *lock)
static inline void arch_spin_unlock(arch_spinlock_t *lock) static inline void arch_spin_unlock(arch_spinlock_t *lock)
{ {
asm volatile( asm volatile(
" stlr %w1, %0\n" " stlrh %w1, %0\n"
: "=Q" (lock->lock) : "r" (0) : "memory"); : "=Q" (lock->owner)
: "r" (lock->owner + 1)
: "memory");
}
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
{
arch_spinlock_t lockval = ACCESS_ONCE(*lock);
return lockval.owner != lockval.next;
}
static inline int arch_spin_is_contended(arch_spinlock_t *lock)
{
arch_spinlock_t lockval = ACCESS_ONCE(*lock);
return (lockval.next - lockval.owner) > 1;
} }
#define arch_spin_is_contended arch_spin_is_contended
/* /*
* Write lock implementation. * Write lock implementation.
......
...@@ -20,14 +20,14 @@ ...@@ -20,14 +20,14 @@
# error "please don't include this file directly" # error "please don't include this file directly"
#endif #endif
/* We only require natural alignment for exclusive accesses. */ #define TICKET_SHIFT 16
#define __lock_aligned
typedef struct { typedef struct {
volatile unsigned int lock; u16 owner;
} arch_spinlock_t; u16 next;
} __aligned(4) arch_spinlock_t;
#define __ARCH_SPIN_LOCK_UNLOCKED { 0 } #define __ARCH_SPIN_LOCK_UNLOCKED { 0 , 0 }
typedef struct { typedef struct {
volatile unsigned int lock; volatile unsigned int lock;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment