| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SPINLOCK_H | 
|  | 2 | #define __ASM_SPINLOCK_H | 
|  | 3 |  | 
|  | 4 | #include <asm/atomic.h> | 
|  | 5 | #include <asm/rwlock.h> | 
|  | 6 | #include <asm/page.h> | 
| Andrew Morton | 2bd0cfb | 2006-09-27 01:49:42 -0700 | [diff] [blame] | 7 | #include <asm/processor.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | /* | 
|  | 10 | * Your basic SMP spinlocks, allowing only a single CPU anywhere | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 11 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Simple spin lock operations.  There are two variants, one clears IRQ's | 
|  | 13 | * on the local processor, one does not. | 
|  | 14 | * | 
|  | 15 | * We make no fairness assumptions. They have a cost. | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 16 | * | 
|  | 17 | * (the type definitions are in asm/spinlock_types.h) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 20 | static inline int __raw_spin_is_locked(raw_spinlock_t *lock) | 
|  | 21 | { | 
|  | 22 | return *(volatile signed int *)(&(lock)->slock) <= 0; | 
|  | 23 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 25 | static inline void __raw_spin_lock(raw_spinlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 27 | asm volatile( | 
|  | 28 | "\n1:\t" | 
|  | 29 | LOCK_PREFIX " ; decl %0\n\t" | 
|  | 30 | "jns 2f\n" | 
|  | 31 | "3:\n" | 
|  | 32 | "rep;nop\n\t" | 
|  | 33 | "cmpl $0,%0\n\t" | 
|  | 34 | "jle 3b\n\t" | 
|  | 35 | "jmp 1b\n" | 
|  | 36 | "2:\t" : "=m" (lock->slock) : : "memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Andi Kleen | 87e1652 | 2006-12-07 02:14:00 +0100 | [diff] [blame] | 39 | /* | 
|  | 40 | * Same as __raw_spin_lock, but reenable interrupts during spinning. | 
|  | 41 | */ | 
|  | 42 | #ifndef CONFIG_PROVE_LOCKING | 
|  | 43 | static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags) | 
|  | 44 | { | 
|  | 45 | asm volatile( | 
|  | 46 | "\n1:\t" | 
|  | 47 | LOCK_PREFIX " ; decl %0\n\t" | 
|  | 48 | "jns 5f\n" | 
|  | 49 | "testl $0x200, %1\n\t"	/* interrupts were disabled? */ | 
|  | 50 | "jz 4f\n\t" | 
|  | 51 | "sti\n" | 
|  | 52 | "3:\t" | 
|  | 53 | "rep;nop\n\t" | 
|  | 54 | "cmpl $0, %0\n\t" | 
|  | 55 | "jle 3b\n\t" | 
|  | 56 | "cli\n\t" | 
|  | 57 | "jmp 1b\n" | 
|  | 58 | "4:\t" | 
|  | 59 | "rep;nop\n\t" | 
|  | 60 | "cmpl $0, %0\n\t" | 
|  | 61 | "jg 1b\n\t" | 
|  | 62 | "jmp 4b\n" | 
|  | 63 | "5:\n\t" | 
|  | 64 | : "+m" (lock->slock) : "r" ((unsigned)flags) : "memory"); | 
|  | 65 | } | 
|  | 66 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 68 | static inline int __raw_spin_trylock(raw_spinlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { | 
| Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 70 | int oldval; | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 71 |  | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 72 | asm volatile( | 
| Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 73 | "xchgl %0,%1" | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 74 | :"=q" (oldval), "=m" (lock->slock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | :"0" (0) : "memory"); | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 76 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return oldval > 0; | 
|  | 78 | } | 
|  | 79 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 80 | static inline void __raw_spin_unlock(raw_spinlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 82 | asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 85 | static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock) | 
|  | 86 | { | 
|  | 87 | while (__raw_spin_is_locked(lock)) | 
|  | 88 | cpu_relax(); | 
|  | 89 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | /* | 
|  | 92 | * Read-write spinlocks, allowing multiple readers | 
|  | 93 | * but only one writer. | 
|  | 94 | * | 
|  | 95 | * NOTE! it is quite common to have readers in interrupts | 
|  | 96 | * but no interrupt writers. For those circumstances we | 
|  | 97 | * can "mix" irq-safe locks - any writer needs to get a | 
|  | 98 | * irq-safe write-lock, but readers can get non-irqsafe | 
|  | 99 | * read-locks. | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 100 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | * On x86, we implement read-write locks as a 32-bit counter | 
|  | 102 | * with the high bit (sign) being the "contended" bit. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 |  | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 105 | static inline int __raw_read_can_lock(raw_rwlock_t *lock) | 
|  | 106 | { | 
|  | 107 | return (int)(lock)->lock > 0; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static inline int __raw_write_can_lock(raw_rwlock_t *lock) | 
|  | 111 | { | 
|  | 112 | return (lock)->lock == RW_LOCK_BIAS; | 
|  | 113 | } | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | static inline void __raw_read_lock(raw_rwlock_t *rw) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 117 | asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t" | 
|  | 118 | "jns 1f\n" | 
|  | 119 | "call __read_lock_failed\n" | 
|  | 120 | "1:\n" | 
|  | 121 | ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 124 | static inline void __raw_write_lock(raw_rwlock_t *rw) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { | 
| Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 126 | asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t" | 
|  | 127 | "jz 1f\n" | 
|  | 128 | "\tcall __write_lock_failed\n\t" | 
|  | 129 | "1:\n" | 
|  | 130 | ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 133 | static inline int __raw_read_trylock(raw_rwlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { | 
|  | 135 | atomic_t *count = (atomic_t *)lock; | 
|  | 136 | atomic_dec(count); | 
|  | 137 | if (atomic_read(count) >= 0) | 
|  | 138 | return 1; | 
|  | 139 | atomic_inc(count); | 
|  | 140 | return 0; | 
|  | 141 | } | 
|  | 142 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 143 | static inline int __raw_write_trylock(raw_rwlock_t *lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { | 
|  | 145 | atomic_t *count = (atomic_t *)lock; | 
|  | 146 | if (atomic_sub_and_test(RW_LOCK_BIAS, count)) | 
|  | 147 | return 1; | 
|  | 148 | atomic_add(RW_LOCK_BIAS, count); | 
|  | 149 | return 0; | 
|  | 150 | } | 
|  | 151 |  | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 152 | static inline void __raw_read_unlock(raw_rwlock_t *rw) | 
|  | 153 | { | 
| Andi Kleen | 841be8d | 2006-08-30 19:37:13 +0200 | [diff] [blame] | 154 | asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory"); | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
|  | 157 | static inline void __raw_write_unlock(raw_rwlock_t *rw) | 
|  | 158 | { | 
| Andi Kleen | 841be8d | 2006-08-30 19:37:13 +0200 | [diff] [blame] | 159 | asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0" | 
| Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 160 | : "=m" (rw->lock) : : "memory"); | 
|  | 161 | } | 
|  | 162 |  | 
| Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 163 | #define _raw_spin_relax(lock)	cpu_relax() | 
|  | 164 | #define _raw_read_relax(lock)	cpu_relax() | 
|  | 165 | #define _raw_write_relax(lock)	cpu_relax() | 
|  | 166 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | #endif /* __ASM_SPINLOCK_H */ |