H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_SPINLOCK_H |
| 2 | #define _ASM_X86_SPINLOCK_H |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 3 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 4 | #include <linux/jump_label.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 6 | #include <asm/page.h> |
| 7 | #include <asm/processor.h> |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 9 | #include <asm/paravirt.h> |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 10 | #include <asm/bitops.h> |
| 11 | |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 12 | /* |
| 13 | * Your basic SMP spinlocks, allowing only a single CPU anywhere |
| 14 | * |
| 15 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 16 | * on the local processor, one does not. |
| 17 | * |
Richard Weinberger | 83be4ff | 2012-08-14 14:47:37 -0700 | [diff] [blame] | 18 | * These are fair FIFO ticket locks, which support up to 2^16 CPUs. |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 19 | * |
| 20 | * (the type definitions are in asm/spinlock_types.h) |
| 21 | */ |
| 22 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_X86_32 |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 24 | # define LOCK_PTR_REG "a" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 25 | #else |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 26 | # define LOCK_PTR_REG "D" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 27 | #endif |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 28 | |
Dave Jones | 09df7c4 | 2014-03-10 19:32:22 -0400 | [diff] [blame] | 29 | #if defined(CONFIG_X86_32) && (defined(CONFIG_X86_PPRO_FENCE)) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 30 | /* |
Dave Jones | 09df7c4 | 2014-03-10 19:32:22 -0400 | [diff] [blame] | 31 | * On PPro SMP, we use a locked operation to unlock |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 32 | * (PPro errata 66, 92) |
| 33 | */ |
| 34 | # define UNLOCK_LOCK_PREFIX LOCK_PREFIX |
| 35 | #else |
| 36 | # define UNLOCK_LOCK_PREFIX |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 39 | /* How long a lock should spin before we consider blocking */ |
| 40 | #define SPIN_THRESHOLD (1 << 15) |
| 41 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 42 | extern struct static_key paravirt_ticketlocks_enabled; |
| 43 | static __always_inline bool static_key_false(struct static_key *key); |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 44 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 45 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
| 46 | |
| 47 | static inline void __ticket_enter_slowpath(arch_spinlock_t *lock) |
| 48 | { |
| 49 | set_bit(0, (volatile unsigned long *)&lock->tickets.tail); |
| 50 | } |
| 51 | |
| 52 | #else /* !CONFIG_PARAVIRT_SPINLOCKS */ |
| 53 | static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock, |
| 54 | __ticket_t ticket) |
| 55 | { |
| 56 | } |
| 57 | static inline void __ticket_unlock_kick(arch_spinlock_t *lock, |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 58 | __ticket_t ticket) |
| 59 | { |
| 60 | } |
| 61 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 62 | #endif /* CONFIG_PARAVIRT_SPINLOCKS */ |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 63 | |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 64 | static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) |
| 65 | { |
| 66 | return lock.tickets.head == lock.tickets.tail; |
| 67 | } |
| 68 | |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Ticket locks are conceptually two parts, one indicating the current head of |
| 71 | * the queue, and the other indicating the current tail. The lock is acquired |
| 72 | * by atomically noting the tail and incrementing it by one (thus adding |
| 73 | * ourself to the queue and noting our position), then waiting until the head |
| 74 | * becomes equal to the the initial value of the tail. |
| 75 | * |
| 76 | * We use an xadd covering *both* parts of the lock, to increment the tail and |
| 77 | * also load the position of the head, which takes care of memory ordering |
| 78 | * issues and should be optimal for the uncontended case. Note the tail must be |
| 79 | * in the high part, because a wide xadd increment of the low part would carry |
| 80 | * up and contaminate the high part. |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 81 | */ |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 82 | static __always_inline void arch_spin_lock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 83 | { |
Jeremy Fitzhardinge | 4a1ed4c | 2013-08-09 19:51:56 +0530 | [diff] [blame] | 84 | register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC }; |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 85 | |
Jeremy Fitzhardinge | 2994488 | 2010-07-13 14:07:45 -0700 | [diff] [blame] | 86 | inc = xadd(&lock->tickets, inc); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 87 | if (likely(inc.head == inc.tail)) |
| 88 | goto out; |
Jeremy Fitzhardinge | c576a3e | 2010-07-03 01:06:04 +0100 | [diff] [blame] | 89 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 90 | inc.tail &= ~TICKET_SLOWPATH_FLAG; |
Jeremy Fitzhardinge | c576a3e | 2010-07-03 01:06:04 +0100 | [diff] [blame] | 91 | for (;;) { |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 92 | unsigned count = SPIN_THRESHOLD; |
| 93 | |
| 94 | do { |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 95 | if (ACCESS_ONCE(lock->tickets.head) == inc.tail) |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 96 | goto out; |
| 97 | cpu_relax(); |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 98 | } while (--count); |
| 99 | __ticket_lock_spinning(lock, inc.tail); |
Jeremy Fitzhardinge | c576a3e | 2010-07-03 01:06:04 +0100 | [diff] [blame] | 100 | } |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 101 | out: barrier(); /* make sure nothing creeps before the lock is taken */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 104 | static __always_inline int arch_spin_trylock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 105 | { |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 106 | arch_spinlock_t old, new; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 107 | |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 108 | old.tickets = ACCESS_ONCE(lock->tickets); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 109 | if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG)) |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 110 | return 0; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 111 | |
Jeremy Fitzhardinge | 4a1ed4c | 2013-08-09 19:51:56 +0530 | [diff] [blame] | 112 | new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT); |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 113 | |
| 114 | /* cmpxchg is a full barrier, so nothing can move before it */ |
| 115 | return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 118 | static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock, |
| 119 | arch_spinlock_t old) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 120 | { |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 121 | arch_spinlock_t new; |
| 122 | |
| 123 | BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS); |
| 124 | |
| 125 | /* Perform the unlock on the "before" copy */ |
| 126 | old.tickets.head += TICKET_LOCK_INC; |
| 127 | |
| 128 | /* Clear the slowpath flag */ |
| 129 | new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT); |
| 130 | |
| 131 | /* |
| 132 | * If the lock is uncontended, clear the flag - use cmpxchg in |
| 133 | * case it changes behind our back though. |
| 134 | */ |
| 135 | if (new.tickets.head != new.tickets.tail || |
| 136 | cmpxchg(&lock->head_tail, old.head_tail, |
| 137 | new.head_tail) != old.head_tail) { |
| 138 | /* |
| 139 | * Lock still has someone queued for it, so wake up an |
| 140 | * appropriate waiter. |
| 141 | */ |
| 142 | __ticket_unlock_kick(lock, old.tickets.head); |
| 143 | } |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 146 | static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 147 | { |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 148 | if (TICKET_SLOWPATH_FLAG && |
| 149 | static_key_false(¶virt_ticketlocks_enabled)) { |
| 150 | arch_spinlock_t prev; |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 151 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 152 | prev = *lock; |
| 153 | add_smp(&lock->tickets.head, TICKET_LOCK_INC); |
| 154 | |
| 155 | /* add_smp() is a full mb() */ |
| 156 | |
| 157 | if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG)) |
| 158 | __ticket_unlock_slowpath(lock, prev); |
| 159 | } else |
| 160 | __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 163 | static inline int arch_spin_is_locked(arch_spinlock_t *lock) |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 164 | { |
Jeremy Fitzhardinge | 84eb950 | 2010-07-02 23:26:36 +0100 | [diff] [blame] | 165 | struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets); |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 166 | |
Jan Beulich | 7931d49 | 2012-02-03 15:06:26 +0000 | [diff] [blame] | 167 | return tmp.tail != tmp.head; |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 170 | static inline int arch_spin_is_contended(arch_spinlock_t *lock) |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 171 | { |
Jeremy Fitzhardinge | 84eb950 | 2010-07-02 23:26:36 +0100 | [diff] [blame] | 172 | struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets); |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 173 | |
Jeremy Fitzhardinge | 4a1ed4c | 2013-08-09 19:51:56 +0530 | [diff] [blame] | 174 | return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC; |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 175 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 176 | #define arch_spin_is_contended arch_spin_is_contended |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 177 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 178 | static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock, |
Jeremy Fitzhardinge | 63d3a75 | 2008-08-19 13:19:36 -0700 | [diff] [blame] | 179 | unsigned long flags) |
| 180 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 181 | arch_spin_lock(lock); |
Jeremy Fitzhardinge | 63d3a75 | 2008-08-19 13:19:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 184 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 185 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 186 | while (arch_spin_is_locked(lock)) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 187 | cpu_relax(); |
| 188 | } |
| 189 | |
Waiman Long | bd01ec1 | 2014-02-03 13:18:57 +0100 | [diff] [blame] | 190 | #ifndef CONFIG_QUEUE_RWLOCK |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Read-write spinlocks, allowing multiple readers |
| 193 | * but only one writer. |
| 194 | * |
| 195 | * NOTE! it is quite common to have readers in interrupts |
| 196 | * but no interrupt writers. For those circumstances we |
| 197 | * can "mix" irq-safe locks - any writer needs to get a |
| 198 | * irq-safe write-lock, but readers can get non-irqsafe |
| 199 | * read-locks. |
| 200 | * |
| 201 | * On x86, we implement read-write locks as a 32-bit counter |
| 202 | * with the high bit (sign) being the "contended" bit. |
| 203 | */ |
| 204 | |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 205 | /** |
| 206 | * read_can_lock - would read_trylock() succeed? |
| 207 | * @lock: the rwlock in question. |
| 208 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 209 | static inline int arch_read_can_lock(arch_rwlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 210 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 211 | return lock->lock > 0; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 214 | /** |
| 215 | * write_can_lock - would write_trylock() succeed? |
| 216 | * @lock: the rwlock in question. |
| 217 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 218 | static inline int arch_write_can_lock(arch_rwlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 219 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 220 | return lock->write == WRITE_LOCK_CMP; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 223 | static inline void arch_read_lock(arch_rwlock_t *rw) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 224 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 225 | asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t" |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 226 | "jns 1f\n" |
| 227 | "call __read_lock_failed\n\t" |
| 228 | "1:\n" |
| 229 | ::LOCK_PTR_REG (rw) : "memory"); |
| 230 | } |
| 231 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 232 | static inline void arch_write_lock(arch_rwlock_t *rw) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 233 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 234 | asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t" |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 235 | "jz 1f\n" |
| 236 | "call __write_lock_failed\n\t" |
| 237 | "1:\n" |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 238 | ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS) |
| 239 | : "memory"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 242 | static inline int arch_read_trylock(arch_rwlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 243 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 244 | READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 245 | |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 246 | if (READ_LOCK_ATOMIC(dec_return)(count) >= 0) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 247 | return 1; |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 248 | READ_LOCK_ATOMIC(inc)(count); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 252 | static inline int arch_write_trylock(arch_rwlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 253 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 254 | atomic_t *count = (atomic_t *)&lock->write; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 255 | |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 256 | if (atomic_sub_and_test(WRITE_LOCK_CMP, count)) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 257 | return 1; |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 258 | atomic_add(WRITE_LOCK_CMP, count); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 262 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 263 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 264 | asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0" |
| 265 | :"+m" (rw->lock) : : "memory"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 268 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 269 | { |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 270 | asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0" |
| 271 | : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 272 | } |
Waiman Long | bd01ec1 | 2014-02-03 13:18:57 +0100 | [diff] [blame] | 273 | #else |
| 274 | #include <asm/qrwlock.h> |
| 275 | #endif /* CONFIG_QUEUE_RWLOCK */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 276 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 277 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 278 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
Robin Holt | f5f7eac | 2009-04-02 16:59:46 -0700 | [diff] [blame] | 279 | |
Jan Beulich | a750036 | 2011-07-19 13:00:45 +0100 | [diff] [blame] | 280 | #undef READ_LOCK_SIZE |
| 281 | #undef READ_LOCK_ATOMIC |
| 282 | #undef WRITE_LOCK_ADD |
| 283 | #undef WRITE_LOCK_SUB |
| 284 | #undef WRITE_LOCK_CMP |
| 285 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 286 | #define arch_spin_relax(lock) cpu_relax() |
| 287 | #define arch_read_relax(lock) cpu_relax() |
| 288 | #define arch_write_relax(lock) cpu_relax() |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 289 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 290 | #endif /* _ASM_X86_SPINLOCK_H */ |