1b8b572e1SStephen Rothwell #ifndef __ASM_SPINLOCK_H 2b8b572e1SStephen Rothwell #define __ASM_SPINLOCK_H 3b8b572e1SStephen Rothwell #ifdef __KERNEL__ 4b8b572e1SStephen Rothwell 5b8b572e1SStephen Rothwell /* 6b8b572e1SStephen Rothwell * Simple spin lock operations. 7b8b572e1SStephen Rothwell * 8b8b572e1SStephen Rothwell * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM 9b8b572e1SStephen Rothwell * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM 10b8b572e1SStephen Rothwell * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM 11b8b572e1SStephen Rothwell * Rework to support virtual processors 12b8b572e1SStephen Rothwell * 13b8b572e1SStephen Rothwell * Type of int is used as a full 64b word is not necessary. 14b8b572e1SStephen Rothwell * 15b8b572e1SStephen Rothwell * This program is free software; you can redistribute it and/or 16b8b572e1SStephen Rothwell * modify it under the terms of the GNU General Public License 17b8b572e1SStephen Rothwell * as published by the Free Software Foundation; either version 18b8b572e1SStephen Rothwell * 2 of the License, or (at your option) any later version. 19b8b572e1SStephen Rothwell * 20b8b572e1SStephen Rothwell * (the type definitions are in asm/spinlock_types.h) 21b8b572e1SStephen Rothwell */ 22b8b572e1SStephen Rothwell #include <linux/irqflags.h> 23b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 24b8b572e1SStephen Rothwell #include <asm/paca.h> 25b8b572e1SStephen Rothwell #include <asm/hvcall.h> 26b8b572e1SStephen Rothwell #endif 27b8b572e1SStephen Rothwell #include <asm/asm-compat.h> 28b8b572e1SStephen Rothwell #include <asm/synch.h> 294e14a4d1SAnton Blanchard #include <asm/ppc-opcode.h> 30b8b572e1SStephen Rothwell 31919fc6e3SPaul E. McKenney #define smp_mb__after_unlock_lock() smp_mb() /* Full ordering for lock. */ 32919fc6e3SPaul E. McKenney 330199c4e6SThomas Gleixner #define arch_spin_is_locked(x) ((x)->slock != 0) 34b8b572e1SStephen Rothwell 35b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 36b8b572e1SStephen Rothwell /* use 0x800000yy when locked, where yy == CPU number */ 3754bb7f4bSAnton Blanchard #ifdef __BIG_ENDIAN__ 38b8b572e1SStephen Rothwell #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token)) 39b8b572e1SStephen Rothwell #else 4054bb7f4bSAnton Blanchard #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index)) 4154bb7f4bSAnton Blanchard #endif 4254bb7f4bSAnton Blanchard #else 43b8b572e1SStephen Rothwell #define LOCK_TOKEN 1 44b8b572e1SStephen Rothwell #endif 45b8b572e1SStephen Rothwell 46b8b572e1SStephen Rothwell #if defined(CONFIG_PPC64) && defined(CONFIG_SMP) 47b8b572e1SStephen Rothwell #define CLEAR_IO_SYNC (get_paca()->io_sync = 0) 48b8b572e1SStephen Rothwell #define SYNC_IO do { \ 49b8b572e1SStephen Rothwell if (unlikely(get_paca()->io_sync)) { \ 50b8b572e1SStephen Rothwell mb(); \ 51b8b572e1SStephen Rothwell get_paca()->io_sync = 0; \ 52b8b572e1SStephen Rothwell } \ 53b8b572e1SStephen Rothwell } while (0) 54b8b572e1SStephen Rothwell #else 55b8b572e1SStephen Rothwell #define CLEAR_IO_SYNC 56b8b572e1SStephen Rothwell #define SYNC_IO 57b8b572e1SStephen Rothwell #endif 58b8b572e1SStephen Rothwell 59b8b572e1SStephen Rothwell /* 60b8b572e1SStephen Rothwell * This returns the old value in the lock, so we succeeded 61b8b572e1SStephen Rothwell * in getting the lock if the return value is 0. 62b8b572e1SStephen Rothwell */ 630199c4e6SThomas Gleixner static inline unsigned long __arch_spin_trylock(arch_spinlock_t *lock) 64b8b572e1SStephen Rothwell { 65b8b572e1SStephen Rothwell unsigned long tmp, token; 66b8b572e1SStephen Rothwell 67b8b572e1SStephen Rothwell token = LOCK_TOKEN; 68b8b572e1SStephen Rothwell __asm__ __volatile__( 694e14a4d1SAnton Blanchard "1: " PPC_LWARX(%0,0,%2,1) "\n\ 70b8b572e1SStephen Rothwell cmpwi 0,%0,0\n\ 71b8b572e1SStephen Rothwell bne- 2f\n\ 72b8b572e1SStephen Rothwell stwcx. %1,0,%2\n\ 73f10e2e5bSAnton Blanchard bne- 1b\n" 74f10e2e5bSAnton Blanchard PPC_ACQUIRE_BARRIER 75f10e2e5bSAnton Blanchard "2:" 76f10e2e5bSAnton Blanchard : "=&r" (tmp) 77b8b572e1SStephen Rothwell : "r" (token), "r" (&lock->slock) 78b8b572e1SStephen Rothwell : "cr0", "memory"); 79b8b572e1SStephen Rothwell 80b8b572e1SStephen Rothwell return tmp; 81b8b572e1SStephen Rothwell } 82b8b572e1SStephen Rothwell 830199c4e6SThomas Gleixner static inline int arch_spin_trylock(arch_spinlock_t *lock) 84b8b572e1SStephen Rothwell { 85b8b572e1SStephen Rothwell CLEAR_IO_SYNC; 860199c4e6SThomas Gleixner return __arch_spin_trylock(lock) == 0; 87b8b572e1SStephen Rothwell } 88b8b572e1SStephen Rothwell 89b8b572e1SStephen Rothwell /* 90b8b572e1SStephen Rothwell * On a system with shared processors (that is, where a physical 91b8b572e1SStephen Rothwell * processor is multiplexed between several virtual processors), 92b8b572e1SStephen Rothwell * there is no point spinning on a lock if the holder of the lock 93b8b572e1SStephen Rothwell * isn't currently scheduled on a physical processor. Instead 94b8b572e1SStephen Rothwell * we detect this situation and ask the hypervisor to give the 95b8b572e1SStephen Rothwell * rest of our timeslice to the lock holder. 96b8b572e1SStephen Rothwell * 97b8b572e1SStephen Rothwell * So that we can tell which virtual processor is holding a lock, 98b8b572e1SStephen Rothwell * we put 0x80000000 | smp_processor_id() in the lock when it is 99b8b572e1SStephen Rothwell * held. Conveniently, we have a word in the paca that holds this 100b8b572e1SStephen Rothwell * value. 101b8b572e1SStephen Rothwell */ 102b8b572e1SStephen Rothwell 1031b041885SStephen Rothwell #if defined(CONFIG_PPC_SPLPAR) 104b8b572e1SStephen Rothwell /* We only yield to the hypervisor if we are in shared processor mode */ 105f13c13a0SAnton Blanchard #define SHARED_PROCESSOR (lppaca_shared_proc(local_paca->lppaca_ptr)) 106445c8951SThomas Gleixner extern void __spin_yield(arch_spinlock_t *lock); 107fb3a6bbcSThomas Gleixner extern void __rw_yield(arch_rwlock_t *lock); 1081b041885SStephen Rothwell #else /* SPLPAR */ 109b8b572e1SStephen Rothwell #define __spin_yield(x) barrier() 110b8b572e1SStephen Rothwell #define __rw_yield(x) barrier() 111b8b572e1SStephen Rothwell #define SHARED_PROCESSOR 0 112b8b572e1SStephen Rothwell #endif 113b8b572e1SStephen Rothwell 1140199c4e6SThomas Gleixner static inline void arch_spin_lock(arch_spinlock_t *lock) 115b8b572e1SStephen Rothwell { 116b8b572e1SStephen Rothwell CLEAR_IO_SYNC; 117b8b572e1SStephen Rothwell while (1) { 1180199c4e6SThomas Gleixner if (likely(__arch_spin_trylock(lock) == 0)) 119b8b572e1SStephen Rothwell break; 120b8b572e1SStephen Rothwell do { 121b8b572e1SStephen Rothwell HMT_low(); 122b8b572e1SStephen Rothwell if (SHARED_PROCESSOR) 123b8b572e1SStephen Rothwell __spin_yield(lock); 124b8b572e1SStephen Rothwell } while (unlikely(lock->slock != 0)); 125b8b572e1SStephen Rothwell HMT_medium(); 126b8b572e1SStephen Rothwell } 127b8b572e1SStephen Rothwell } 128b8b572e1SStephen Rothwell 129b8b572e1SStephen Rothwell static inline 1300199c4e6SThomas Gleixner void arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags) 131b8b572e1SStephen Rothwell { 132b8b572e1SStephen Rothwell unsigned long flags_dis; 133b8b572e1SStephen Rothwell 134b8b572e1SStephen Rothwell CLEAR_IO_SYNC; 135b8b572e1SStephen Rothwell while (1) { 1360199c4e6SThomas Gleixner if (likely(__arch_spin_trylock(lock) == 0)) 137b8b572e1SStephen Rothwell break; 138b8b572e1SStephen Rothwell local_save_flags(flags_dis); 139b8b572e1SStephen Rothwell local_irq_restore(flags); 140b8b572e1SStephen Rothwell do { 141b8b572e1SStephen Rothwell HMT_low(); 142b8b572e1SStephen Rothwell if (SHARED_PROCESSOR) 143b8b572e1SStephen Rothwell __spin_yield(lock); 144b8b572e1SStephen Rothwell } while (unlikely(lock->slock != 0)); 145b8b572e1SStephen Rothwell HMT_medium(); 146b8b572e1SStephen Rothwell local_irq_restore(flags_dis); 147b8b572e1SStephen Rothwell } 148b8b572e1SStephen Rothwell } 149b8b572e1SStephen Rothwell 1500199c4e6SThomas Gleixner static inline void arch_spin_unlock(arch_spinlock_t *lock) 151b8b572e1SStephen Rothwell { 152b8b572e1SStephen Rothwell SYNC_IO; 1530199c4e6SThomas Gleixner __asm__ __volatile__("# arch_spin_unlock\n\t" 154f10e2e5bSAnton Blanchard PPC_RELEASE_BARRIER: : :"memory"); 155b8b572e1SStephen Rothwell lock->slock = 0; 156b8b572e1SStephen Rothwell } 157b8b572e1SStephen Rothwell 158b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 1590199c4e6SThomas Gleixner extern void arch_spin_unlock_wait(arch_spinlock_t *lock); 160b8b572e1SStephen Rothwell #else 1610199c4e6SThomas Gleixner #define arch_spin_unlock_wait(lock) \ 1620199c4e6SThomas Gleixner do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0) 163b8b572e1SStephen Rothwell #endif 164b8b572e1SStephen Rothwell 165b8b572e1SStephen Rothwell /* 166b8b572e1SStephen Rothwell * Read-write spinlocks, allowing multiple readers 167b8b572e1SStephen Rothwell * but only one writer. 168b8b572e1SStephen Rothwell * 169b8b572e1SStephen Rothwell * NOTE! it is quite common to have readers in interrupts 170b8b572e1SStephen Rothwell * but no interrupt writers. For those circumstances we 171b8b572e1SStephen Rothwell * can "mix" irq-safe locks - any writer needs to get a 172b8b572e1SStephen Rothwell * irq-safe write-lock, but readers can get non-irqsafe 173b8b572e1SStephen Rothwell * read-locks. 174b8b572e1SStephen Rothwell */ 175b8b572e1SStephen Rothwell 176e5931943SThomas Gleixner #define arch_read_can_lock(rw) ((rw)->lock >= 0) 177e5931943SThomas Gleixner #define arch_write_can_lock(rw) (!(rw)->lock) 178b8b572e1SStephen Rothwell 179b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 180b8b572e1SStephen Rothwell #define __DO_SIGN_EXTEND "extsw %0,%0\n" 181b8b572e1SStephen Rothwell #define WRLOCK_TOKEN LOCK_TOKEN /* it's negative */ 182b8b572e1SStephen Rothwell #else 183b8b572e1SStephen Rothwell #define __DO_SIGN_EXTEND 184b8b572e1SStephen Rothwell #define WRLOCK_TOKEN (-1) 185b8b572e1SStephen Rothwell #endif 186b8b572e1SStephen Rothwell 187b8b572e1SStephen Rothwell /* 188b8b572e1SStephen Rothwell * This returns the old value in the lock + 1, 189b8b572e1SStephen Rothwell * so we got a read lock if the return value is > 0. 190b8b572e1SStephen Rothwell */ 191e5931943SThomas Gleixner static inline long __arch_read_trylock(arch_rwlock_t *rw) 192b8b572e1SStephen Rothwell { 193b8b572e1SStephen Rothwell long tmp; 194b8b572e1SStephen Rothwell 195b8b572e1SStephen Rothwell __asm__ __volatile__( 1964e14a4d1SAnton Blanchard "1: " PPC_LWARX(%0,0,%1,1) "\n" 197b8b572e1SStephen Rothwell __DO_SIGN_EXTEND 198b8b572e1SStephen Rothwell " addic. %0,%0,1\n\ 199b8b572e1SStephen Rothwell ble- 2f\n" 200b8b572e1SStephen Rothwell PPC405_ERR77(0,%1) 201b8b572e1SStephen Rothwell " stwcx. %0,0,%1\n\ 202f10e2e5bSAnton Blanchard bne- 1b\n" 203f10e2e5bSAnton Blanchard PPC_ACQUIRE_BARRIER 204f10e2e5bSAnton Blanchard "2:" : "=&r" (tmp) 205b8b572e1SStephen Rothwell : "r" (&rw->lock) 206b8b572e1SStephen Rothwell : "cr0", "xer", "memory"); 207b8b572e1SStephen Rothwell 208b8b572e1SStephen Rothwell return tmp; 209b8b572e1SStephen Rothwell } 210b8b572e1SStephen Rothwell 211b8b572e1SStephen Rothwell /* 212b8b572e1SStephen Rothwell * This returns the old value in the lock, 213b8b572e1SStephen Rothwell * so we got the write lock if the return value is 0. 214b8b572e1SStephen Rothwell */ 215e5931943SThomas Gleixner static inline long __arch_write_trylock(arch_rwlock_t *rw) 216b8b572e1SStephen Rothwell { 217b8b572e1SStephen Rothwell long tmp, token; 218b8b572e1SStephen Rothwell 219b8b572e1SStephen Rothwell token = WRLOCK_TOKEN; 220b8b572e1SStephen Rothwell __asm__ __volatile__( 2214e14a4d1SAnton Blanchard "1: " PPC_LWARX(%0,0,%2,1) "\n\ 222b8b572e1SStephen Rothwell cmpwi 0,%0,0\n\ 223b8b572e1SStephen Rothwell bne- 2f\n" 224b8b572e1SStephen Rothwell PPC405_ERR77(0,%1) 225b8b572e1SStephen Rothwell " stwcx. %1,0,%2\n\ 226f10e2e5bSAnton Blanchard bne- 1b\n" 227f10e2e5bSAnton Blanchard PPC_ACQUIRE_BARRIER 228f10e2e5bSAnton Blanchard "2:" : "=&r" (tmp) 229b8b572e1SStephen Rothwell : "r" (token), "r" (&rw->lock) 230b8b572e1SStephen Rothwell : "cr0", "memory"); 231b8b572e1SStephen Rothwell 232b8b572e1SStephen Rothwell return tmp; 233b8b572e1SStephen Rothwell } 234b8b572e1SStephen Rothwell 235e5931943SThomas Gleixner static inline void arch_read_lock(arch_rwlock_t *rw) 236b8b572e1SStephen Rothwell { 237b8b572e1SStephen Rothwell while (1) { 238e5931943SThomas Gleixner if (likely(__arch_read_trylock(rw) > 0)) 239b8b572e1SStephen Rothwell break; 240b8b572e1SStephen Rothwell do { 241b8b572e1SStephen Rothwell HMT_low(); 242b8b572e1SStephen Rothwell if (SHARED_PROCESSOR) 243b8b572e1SStephen Rothwell __rw_yield(rw); 244b8b572e1SStephen Rothwell } while (unlikely(rw->lock < 0)); 245b8b572e1SStephen Rothwell HMT_medium(); 246b8b572e1SStephen Rothwell } 247b8b572e1SStephen Rothwell } 248b8b572e1SStephen Rothwell 249e5931943SThomas Gleixner static inline void arch_write_lock(arch_rwlock_t *rw) 250b8b572e1SStephen Rothwell { 251b8b572e1SStephen Rothwell while (1) { 252e5931943SThomas Gleixner if (likely(__arch_write_trylock(rw) == 0)) 253b8b572e1SStephen Rothwell break; 254b8b572e1SStephen Rothwell do { 255b8b572e1SStephen Rothwell HMT_low(); 256b8b572e1SStephen Rothwell if (SHARED_PROCESSOR) 257b8b572e1SStephen Rothwell __rw_yield(rw); 258b8b572e1SStephen Rothwell } while (unlikely(rw->lock != 0)); 259b8b572e1SStephen Rothwell HMT_medium(); 260b8b572e1SStephen Rothwell } 261b8b572e1SStephen Rothwell } 262b8b572e1SStephen Rothwell 263e5931943SThomas Gleixner static inline int arch_read_trylock(arch_rwlock_t *rw) 264b8b572e1SStephen Rothwell { 265e5931943SThomas Gleixner return __arch_read_trylock(rw) > 0; 266b8b572e1SStephen Rothwell } 267b8b572e1SStephen Rothwell 268e5931943SThomas Gleixner static inline int arch_write_trylock(arch_rwlock_t *rw) 269b8b572e1SStephen Rothwell { 270e5931943SThomas Gleixner return __arch_write_trylock(rw) == 0; 271b8b572e1SStephen Rothwell } 272b8b572e1SStephen Rothwell 273e5931943SThomas Gleixner static inline void arch_read_unlock(arch_rwlock_t *rw) 274b8b572e1SStephen Rothwell { 275b8b572e1SStephen Rothwell long tmp; 276b8b572e1SStephen Rothwell 277b8b572e1SStephen Rothwell __asm__ __volatile__( 278b8b572e1SStephen Rothwell "# read_unlock\n\t" 279f10e2e5bSAnton Blanchard PPC_RELEASE_BARRIER 280b8b572e1SStephen Rothwell "1: lwarx %0,0,%1\n\ 281b8b572e1SStephen Rothwell addic %0,%0,-1\n" 282b8b572e1SStephen Rothwell PPC405_ERR77(0,%1) 283b8b572e1SStephen Rothwell " stwcx. %0,0,%1\n\ 284b8b572e1SStephen Rothwell bne- 1b" 285b8b572e1SStephen Rothwell : "=&r"(tmp) 286b8b572e1SStephen Rothwell : "r"(&rw->lock) 287efc3624cSPaul Mackerras : "cr0", "xer", "memory"); 288b8b572e1SStephen Rothwell } 289b8b572e1SStephen Rothwell 290e5931943SThomas Gleixner static inline void arch_write_unlock(arch_rwlock_t *rw) 291b8b572e1SStephen Rothwell { 292b8b572e1SStephen Rothwell __asm__ __volatile__("# write_unlock\n\t" 293f10e2e5bSAnton Blanchard PPC_RELEASE_BARRIER: : :"memory"); 294b8b572e1SStephen Rothwell rw->lock = 0; 295b8b572e1SStephen Rothwell } 296b8b572e1SStephen Rothwell 297e5931943SThomas Gleixner #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) 298e5931943SThomas Gleixner #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) 299f5f7eac4SRobin Holt 3000199c4e6SThomas Gleixner #define arch_spin_relax(lock) __spin_yield(lock) 3010199c4e6SThomas Gleixner #define arch_read_relax(lock) __rw_yield(lock) 3020199c4e6SThomas Gleixner #define arch_write_relax(lock) __rw_yield(lock) 303b8b572e1SStephen Rothwell 304b8b572e1SStephen Rothwell #endif /* __KERNEL__ */ 305b8b572e1SStephen Rothwell #endif /* __ASM_SPINLOCK_H */ 306