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