170af2f8aSWaiman Long /* 270af2f8aSWaiman Long * Queue read/write lock 370af2f8aSWaiman Long * 470af2f8aSWaiman Long * This program is free software; you can redistribute it and/or modify 570af2f8aSWaiman Long * it under the terms of the GNU General Public License as published by 670af2f8aSWaiman Long * the Free Software Foundation; either version 2 of the License, or 770af2f8aSWaiman Long * (at your option) any later version. 870af2f8aSWaiman Long * 970af2f8aSWaiman Long * This program is distributed in the hope that it will be useful, 1070af2f8aSWaiman Long * but WITHOUT ANY WARRANTY; without even the implied warranty of 1170af2f8aSWaiman Long * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1270af2f8aSWaiman Long * GNU General Public License for more details. 1370af2f8aSWaiman Long * 1470af2f8aSWaiman Long * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P. 1570af2f8aSWaiman Long * 1670af2f8aSWaiman Long * Authors: Waiman Long <waiman.long@hp.com> 1770af2f8aSWaiman Long */ 1870af2f8aSWaiman Long #ifndef __ASM_GENERIC_QRWLOCK_H 1970af2f8aSWaiman Long #define __ASM_GENERIC_QRWLOCK_H 2070af2f8aSWaiman Long 2170af2f8aSWaiman Long #include <linux/atomic.h> 2270af2f8aSWaiman Long #include <asm/barrier.h> 2370af2f8aSWaiman Long #include <asm/processor.h> 2470af2f8aSWaiman Long 2570af2f8aSWaiman Long #include <asm-generic/qrwlock_types.h> 2670af2f8aSWaiman Long 2770af2f8aSWaiman Long /* 2870af2f8aSWaiman Long * Writer states & reader shift and bias 2970af2f8aSWaiman Long */ 3070af2f8aSWaiman Long #define _QW_WAITING 1 /* A writer is waiting */ 3170af2f8aSWaiman Long #define _QW_LOCKED 0xff /* A writer holds the lock */ 3270af2f8aSWaiman Long #define _QW_WMASK 0xff /* Writer mask */ 3370af2f8aSWaiman Long #define _QR_SHIFT 8 /* Reader count shift */ 3470af2f8aSWaiman Long #define _QR_BIAS (1U << _QR_SHIFT) 3570af2f8aSWaiman Long 3670af2f8aSWaiman Long /* 3770af2f8aSWaiman Long * External function declarations 3870af2f8aSWaiman Long */ 390e06e5beSWaiman Long extern void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts); 40f7d71f20SWaiman Long extern void queued_write_lock_slowpath(struct qrwlock *lock); 4170af2f8aSWaiman Long 4270af2f8aSWaiman Long /** 43f7d71f20SWaiman Long * queued_read_can_lock- would read_trylock() succeed? 4470af2f8aSWaiman Long * @lock: Pointer to queue rwlock structure 4570af2f8aSWaiman Long */ 46f7d71f20SWaiman Long static inline int queued_read_can_lock(struct qrwlock *lock) 4770af2f8aSWaiman Long { 4870af2f8aSWaiman Long return !(atomic_read(&lock->cnts) & _QW_WMASK); 4970af2f8aSWaiman Long } 5070af2f8aSWaiman Long 5170af2f8aSWaiman Long /** 52f7d71f20SWaiman Long * queued_write_can_lock- would write_trylock() succeed? 5370af2f8aSWaiman Long * @lock: Pointer to queue rwlock structure 5470af2f8aSWaiman Long */ 55f7d71f20SWaiman Long static inline int queued_write_can_lock(struct qrwlock *lock) 5670af2f8aSWaiman Long { 5770af2f8aSWaiman Long return !atomic_read(&lock->cnts); 5870af2f8aSWaiman Long } 5970af2f8aSWaiman Long 6070af2f8aSWaiman Long /** 61f7d71f20SWaiman Long * queued_read_trylock - try to acquire read lock of a queue rwlock 6270af2f8aSWaiman Long * @lock : Pointer to queue rwlock structure 6370af2f8aSWaiman Long * Return: 1 if lock acquired, 0 if failed 6470af2f8aSWaiman Long */ 65f7d71f20SWaiman Long static inline int queued_read_trylock(struct qrwlock *lock) 6670af2f8aSWaiman Long { 6770af2f8aSWaiman Long u32 cnts; 6870af2f8aSWaiman Long 6970af2f8aSWaiman Long cnts = atomic_read(&lock->cnts); 7070af2f8aSWaiman Long if (likely(!(cnts & _QW_WMASK))) { 71*77e430e3SWill Deacon cnts = (u32)atomic_add_return_acquire(_QR_BIAS, &lock->cnts); 7270af2f8aSWaiman Long if (likely(!(cnts & _QW_WMASK))) 7370af2f8aSWaiman Long return 1; 7470af2f8aSWaiman Long atomic_sub(_QR_BIAS, &lock->cnts); 7570af2f8aSWaiman Long } 7670af2f8aSWaiman Long return 0; 7770af2f8aSWaiman Long } 7870af2f8aSWaiman Long 7970af2f8aSWaiman Long /** 80f7d71f20SWaiman Long * queued_write_trylock - try to acquire write lock of a queue rwlock 8170af2f8aSWaiman Long * @lock : Pointer to queue rwlock structure 8270af2f8aSWaiman Long * Return: 1 if lock acquired, 0 if failed 8370af2f8aSWaiman Long */ 84f7d71f20SWaiman Long static inline int queued_write_trylock(struct qrwlock *lock) 8570af2f8aSWaiman Long { 8670af2f8aSWaiman Long u32 cnts; 8770af2f8aSWaiman Long 8870af2f8aSWaiman Long cnts = atomic_read(&lock->cnts); 8970af2f8aSWaiman Long if (unlikely(cnts)) 9070af2f8aSWaiman Long return 0; 9170af2f8aSWaiman Long 92*77e430e3SWill Deacon return likely(atomic_cmpxchg_acquire(&lock->cnts, 9370af2f8aSWaiman Long cnts, cnts | _QW_LOCKED) == cnts); 9470af2f8aSWaiman Long } 9570af2f8aSWaiman Long /** 96f7d71f20SWaiman Long * queued_read_lock - acquire read lock of a queue rwlock 9770af2f8aSWaiman Long * @lock: Pointer to queue rwlock structure 9870af2f8aSWaiman Long */ 99f7d71f20SWaiman Long static inline void queued_read_lock(struct qrwlock *lock) 10070af2f8aSWaiman Long { 10170af2f8aSWaiman Long u32 cnts; 10270af2f8aSWaiman Long 103*77e430e3SWill Deacon cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts); 10470af2f8aSWaiman Long if (likely(!(cnts & _QW_WMASK))) 10570af2f8aSWaiman Long return; 10670af2f8aSWaiman Long 10770af2f8aSWaiman Long /* The slowpath will decrement the reader count, if necessary. */ 1080e06e5beSWaiman Long queued_read_lock_slowpath(lock, cnts); 10970af2f8aSWaiman Long } 11070af2f8aSWaiman Long 11170af2f8aSWaiman Long /** 112f7d71f20SWaiman Long * queued_write_lock - acquire write lock of a queue rwlock 11370af2f8aSWaiman Long * @lock : Pointer to queue rwlock structure 11470af2f8aSWaiman Long */ 115f7d71f20SWaiman Long static inline void queued_write_lock(struct qrwlock *lock) 11670af2f8aSWaiman Long { 11770af2f8aSWaiman Long /* Optimize for the unfair lock case where the fair flag is 0. */ 118*77e430e3SWill Deacon if (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0) 11970af2f8aSWaiman Long return; 12070af2f8aSWaiman Long 121f7d71f20SWaiman Long queued_write_lock_slowpath(lock); 12270af2f8aSWaiman Long } 12370af2f8aSWaiman Long 12470af2f8aSWaiman Long /** 125f7d71f20SWaiman Long * queued_read_unlock - release read lock of a queue rwlock 12670af2f8aSWaiman Long * @lock : Pointer to queue rwlock structure 12770af2f8aSWaiman Long */ 128f7d71f20SWaiman Long static inline void queued_read_unlock(struct qrwlock *lock) 12970af2f8aSWaiman Long { 13070af2f8aSWaiman Long /* 13170af2f8aSWaiman Long * Atomically decrement the reader count 13270af2f8aSWaiman Long */ 133*77e430e3SWill Deacon (void)atomic_sub_return_release(_QR_BIAS, &lock->cnts); 13470af2f8aSWaiman Long } 13570af2f8aSWaiman Long 13670af2f8aSWaiman Long /** 137f7d71f20SWaiman Long * queued_write_unlock - release write lock of a queue rwlock 13870af2f8aSWaiman Long * @lock : Pointer to queue rwlock structure 13970af2f8aSWaiman Long */ 140f7d71f20SWaiman Long static inline void queued_write_unlock(struct qrwlock *lock) 14170af2f8aSWaiman Long { 1422b2a85a4SWill Deacon smp_store_release((u8 *)&lock->cnts, 0); 14370af2f8aSWaiman Long } 14470af2f8aSWaiman Long 14570af2f8aSWaiman Long /* 14670af2f8aSWaiman Long * Remapping rwlock architecture specific functions to the corresponding 14770af2f8aSWaiman Long * queue rwlock functions. 14870af2f8aSWaiman Long */ 149f7d71f20SWaiman Long #define arch_read_can_lock(l) queued_read_can_lock(l) 150f7d71f20SWaiman Long #define arch_write_can_lock(l) queued_write_can_lock(l) 151f7d71f20SWaiman Long #define arch_read_lock(l) queued_read_lock(l) 152f7d71f20SWaiman Long #define arch_write_lock(l) queued_write_lock(l) 153f7d71f20SWaiman Long #define arch_read_trylock(l) queued_read_trylock(l) 154f7d71f20SWaiman Long #define arch_write_trylock(l) queued_write_trylock(l) 155f7d71f20SWaiman Long #define arch_read_unlock(l) queued_read_unlock(l) 156f7d71f20SWaiman Long #define arch_write_unlock(l) queued_write_unlock(l) 15770af2f8aSWaiman Long 15870af2f8aSWaiman Long #endif /* __ASM_GENERIC_QRWLOCK_H */ 159