Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 6 | * Copyright (C) 1999, 2000, 06 Ralf Baechle (ralf@linux-mips.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 8 | */ |
| 9 | #ifndef _ASM_SPINLOCK_H |
| 10 | #define _ASM_SPINLOCK_H |
| 11 | |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 12 | #include <linux/compiler.h> |
| 13 | |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 14 | #include <asm/barrier.h> |
Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 15 | #include <asm/processor.h> |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 16 | #include <asm/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/war.h> |
| 18 | |
| 19 | /* |
| 20 | * Your basic SMP spinlocks, allowing only a single CPU anywhere |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 21 | * |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 22 | * Simple spin lock operations. There are two variants, one clears IRQ's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * on the local processor, one does not. |
| 24 | * |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 25 | * These are fair FIFO ticket locks |
| 26 | * |
| 27 | * (the type definitions are in asm/spinlock_types.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Ticket locks are conceptually two parts, one indicating the current head of |
| 33 | * the queue, and the other indicating the current tail. The lock is acquired |
| 34 | * by atomically noting the tail and incrementing it by one (thus adding |
| 35 | * ourself to the queue and noting our position), then waiting until the head |
| 36 | * becomes equal to the the initial value of the tail. |
| 37 | */ |
| 38 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 39 | static inline int arch_spin_is_locked(arch_spinlock_t *lock) |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 40 | { |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 41 | u32 counters = ACCESS_ONCE(lock->lock); |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 42 | |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 43 | return ((counters >> 16) ^ counters) & 0xffff; |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Paul Burton | 5fac4f7 | 2015-07-30 08:16:10 -0700 | [diff] [blame] | 46 | static inline int arch_spin_value_unlocked(arch_spinlock_t lock) |
| 47 | { |
| 48 | return lock.h.serving_now == lock.h.ticket; |
| 49 | } |
| 50 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 51 | #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock) |
Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 52 | |
| 53 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
| 54 | { |
| 55 | u16 owner = READ_ONCE(lock->h.serving_now); |
| 56 | smp_rmb(); |
| 57 | for (;;) { |
| 58 | arch_spinlock_t tmp = READ_ONCE(*lock); |
| 59 | |
| 60 | if (tmp.h.serving_now == tmp.h.ticket || |
| 61 | tmp.h.serving_now != owner) |
| 62 | break; |
| 63 | |
| 64 | cpu_relax(); |
| 65 | } |
| 66 | smp_acquire__after_ctrl_dep(); |
| 67 | } |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 68 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 69 | static inline int arch_spin_is_contended(arch_spinlock_t *lock) |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 70 | { |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 71 | u32 counters = ACCESS_ONCE(lock->lock); |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 72 | |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 73 | return (((counters >> 16) - counters) & 0xffff) > 1; |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 74 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 75 | #define arch_spin_is_contended arch_spin_is_contended |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 76 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 77 | static inline void arch_spin_lock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 79 | int my_ticket; |
| 80 | int tmp; |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 81 | int inc = 0x10000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | if (R10000_LLSC_WAR) { |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 84 | __asm__ __volatile__ ( |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 85 | " .set push # arch_spin_lock \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 86 | " .set noreorder \n" |
| 87 | " \n" |
| 88 | "1: ll %[ticket], %[ticket_ptr] \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 89 | " addu %[my_ticket], %[ticket], %[inc] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 90 | " sc %[my_ticket], %[ticket_ptr] \n" |
| 91 | " beqzl %[my_ticket], 1b \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | " nop \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 93 | " srl %[my_ticket], %[ticket], 16 \n" |
| 94 | " andi %[ticket], %[ticket], 0xffff \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 95 | " bne %[ticket], %[my_ticket], 4f \n" |
| 96 | " subu %[ticket], %[my_ticket], %[ticket] \n" |
| 97 | "2: \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 98 | " .subsection 2 \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 99 | "4: andi %[ticket], %[ticket], 0xffff \n" |
David Daney | 0e6826c | 2009-03-27 10:07:02 -0700 | [diff] [blame] | 100 | " sll %[ticket], 5 \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 101 | " \n" |
| 102 | "6: bnez %[ticket], 6b \n" |
| 103 | " subu %[ticket], 1 \n" |
| 104 | " \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 105 | " lhu %[ticket], %[serving_now_ptr] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 106 | " beq %[ticket], %[my_ticket], 2b \n" |
| 107 | " subu %[ticket], %[my_ticket], %[ticket] \n" |
David Daney | 0e6826c | 2009-03-27 10:07:02 -0700 | [diff] [blame] | 108 | " b 4b \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 109 | " subu %[ticket], %[ticket], 1 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 110 | " .previous \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 111 | " .set pop \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 112 | : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 113 | [serving_now_ptr] "+m" (lock->h.serving_now), |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 114 | [ticket] "=&r" (tmp), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 115 | [my_ticket] "=&r" (my_ticket) |
| 116 | : [inc] "r" (inc)); |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 117 | } else { |
| 118 | __asm__ __volatile__ ( |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 119 | " .set push # arch_spin_lock \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 120 | " .set noreorder \n" |
| 121 | " \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 122 | "1: ll %[ticket], %[ticket_ptr] \n" |
| 123 | " addu %[my_ticket], %[ticket], %[inc] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 124 | " sc %[my_ticket], %[ticket_ptr] \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 125 | " beqz %[my_ticket], 1b \n" |
| 126 | " srl %[my_ticket], %[ticket], 16 \n" |
| 127 | " andi %[ticket], %[ticket], 0xffff \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 128 | " bne %[ticket], %[my_ticket], 4f \n" |
| 129 | " subu %[ticket], %[my_ticket], %[ticket] \n" |
| 130 | "2: \n" |
| 131 | " .subsection 2 \n" |
Markos Chandras | 9ff897c | 2015-04-20 10:54:34 +0100 | [diff] [blame] | 132 | "4: andi %[ticket], %[ticket], 0xffff \n" |
David Daney | 0e6826c | 2009-03-27 10:07:02 -0700 | [diff] [blame] | 133 | " sll %[ticket], 5 \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 134 | " \n" |
| 135 | "6: bnez %[ticket], 6b \n" |
| 136 | " subu %[ticket], 1 \n" |
| 137 | " \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 138 | " lhu %[ticket], %[serving_now_ptr] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 139 | " beq %[ticket], %[my_ticket], 2b \n" |
| 140 | " subu %[ticket], %[my_ticket], %[ticket] \n" |
David Daney | 0e6826c | 2009-03-27 10:07:02 -0700 | [diff] [blame] | 141 | " b 4b \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 142 | " subu %[ticket], %[ticket], 1 \n" |
| 143 | " .previous \n" |
| 144 | " .set pop \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 145 | : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 146 | [serving_now_ptr] "+m" (lock->h.serving_now), |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 147 | [ticket] "=&r" (tmp), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 148 | [my_ticket] "=&r" (my_ticket) |
| 149 | : [inc] "r" (inc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 151 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 152 | smp_llsc_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 155 | static inline void arch_spin_unlock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 157 | unsigned int serving_now = lock->h.serving_now + 1; |
| 158 | wmb(); |
| 159 | lock->h.serving_now = (u16)serving_now; |
| 160 | nudge_writes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 163 | static inline unsigned int arch_spin_trylock(arch_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 165 | int tmp, tmp2, tmp3; |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 166 | int inc = 0x10000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | if (R10000_LLSC_WAR) { |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 169 | __asm__ __volatile__ ( |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 170 | " .set push # arch_spin_trylock \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 171 | " .set noreorder \n" |
| 172 | " \n" |
| 173 | "1: ll %[ticket], %[ticket_ptr] \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 174 | " srl %[my_ticket], %[ticket], 16 \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 175 | " andi %[now_serving], %[ticket], 0xffff \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 176 | " bne %[my_ticket], %[now_serving], 3f \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 177 | " addu %[ticket], %[ticket], %[inc] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 178 | " sc %[ticket], %[ticket_ptr] \n" |
| 179 | " beqzl %[ticket], 1b \n" |
| 180 | " li %[ticket], 1 \n" |
| 181 | "2: \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 182 | " .subsection 2 \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 183 | "3: b 2b \n" |
| 184 | " li %[ticket], 0 \n" |
Ralf Baechle | f65e4fa | 2006-09-28 01:45:21 +0100 | [diff] [blame] | 185 | " .previous \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 186 | " .set pop \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 187 | : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock), |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 188 | [ticket] "=&r" (tmp), |
| 189 | [my_ticket] "=&r" (tmp2), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 190 | [now_serving] "=&r" (tmp3) |
| 191 | : [inc] "r" (inc)); |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 192 | } else { |
| 193 | __asm__ __volatile__ ( |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 194 | " .set push # arch_spin_trylock \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 195 | " .set noreorder \n" |
| 196 | " \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 197 | "1: ll %[ticket], %[ticket_ptr] \n" |
| 198 | " srl %[my_ticket], %[ticket], 16 \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 199 | " andi %[now_serving], %[ticket], 0xffff \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 200 | " bne %[my_ticket], %[now_serving], 3f \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 201 | " addu %[ticket], %[ticket], %[inc] \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 202 | " sc %[ticket], %[ticket_ptr] \n" |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 203 | " beqz %[ticket], 1b \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 204 | " li %[ticket], 1 \n" |
| 205 | "2: \n" |
| 206 | " .subsection 2 \n" |
| 207 | "3: b 2b \n" |
| 208 | " li %[ticket], 0 \n" |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 209 | " .previous \n" |
| 210 | " .set pop \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 211 | : [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock), |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 212 | [ticket] "=&r" (tmp), |
| 213 | [my_ticket] "=&r" (tmp2), |
David Daney | 500c2e1 | 2010-02-04 11:31:49 -0800 | [diff] [blame] | 214 | [now_serving] "=&r" (tmp3) |
| 215 | : [inc] "r" (inc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 218 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 219 | |
Ralf Baechle | 2a31b03 | 2008-08-28 15:17:49 +0100 | [diff] [blame] | 220 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Read-write spinlocks, allowing multiple readers but only one writer. |
| 225 | * |
| 226 | * NOTE! it is quite common to have readers in interrupts but no interrupt |
| 227 | * writers. For those circumstances we can "mix" irq-safe locks - any writer |
| 228 | * needs to get a irq-safe write-lock, but readers can get non-irqsafe |
| 229 | * read-locks. |
| 230 | */ |
| 231 | |
Ralf Baechle | e3c4807 | 2005-02-03 13:34:45 +0000 | [diff] [blame] | 232 | /* |
| 233 | * read_can_lock - would read_trylock() succeed? |
| 234 | * @lock: the rwlock in question. |
| 235 | */ |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 236 | #define arch_read_can_lock(rw) ((rw)->lock >= 0) |
Ralf Baechle | e3c4807 | 2005-02-03 13:34:45 +0000 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * write_can_lock - would write_trylock() succeed? |
| 240 | * @lock: the rwlock in question. |
| 241 | */ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 242 | #define arch_write_can_lock(rw) (!(rw)->lock) |
Ralf Baechle | e3c4807 | 2005-02-03 13:34:45 +0000 | [diff] [blame] | 243 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 244 | static inline void arch_read_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | unsigned int tmp; |
| 247 | |
| 248 | if (R10000_LLSC_WAR) { |
| 249 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 250 | " .set noreorder # arch_read_lock \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | "1: ll %1, %2 \n" |
| 252 | " bltz %1, 1b \n" |
| 253 | " addu %1, 1 \n" |
| 254 | " sc %1, %0 \n" |
| 255 | " beqzl %1, 1b \n" |
| 256 | " nop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | " .set reorder \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 258 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 259 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | : "memory"); |
| 261 | } else { |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 262 | do { |
| 263 | __asm__ __volatile__( |
| 264 | "1: ll %1, %2 # arch_read_lock \n" |
| 265 | " bltz %1, 1b \n" |
| 266 | " addu %1, 1 \n" |
| 267 | "2: sc %1, %0 \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 268 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 269 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 270 | : "memory"); |
| 271 | } while (unlikely(!tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 273 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 274 | smp_llsc_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 277 | static inline void arch_read_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | unsigned int tmp; |
| 280 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 281 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (R10000_LLSC_WAR) { |
| 284 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 285 | "1: ll %1, %2 # arch_read_unlock \n" |
Markos Chandras | 5182221 | 2015-03-03 18:48:48 +0000 | [diff] [blame] | 286 | " addiu %1, -1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | " sc %1, %0 \n" |
| 288 | " beqzl %1, 1b \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 289 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 290 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | : "memory"); |
| 292 | } else { |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 293 | do { |
| 294 | __asm__ __volatile__( |
| 295 | "1: ll %1, %2 # arch_read_unlock \n" |
Markos Chandras | 5753762 | 2014-11-24 14:11:39 +0000 | [diff] [blame] | 296 | " addiu %1, -1 \n" |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 297 | " sc %1, %0 \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 298 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 299 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 300 | : "memory"); |
| 301 | } while (unlikely(!tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 305 | static inline void arch_write_lock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | unsigned int tmp; |
| 308 | |
| 309 | if (R10000_LLSC_WAR) { |
| 310 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 311 | " .set noreorder # arch_write_lock \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | "1: ll %1, %2 \n" |
| 313 | " bnez %1, 1b \n" |
| 314 | " lui %1, 0x8000 \n" |
| 315 | " sc %1, %0 \n" |
| 316 | " beqzl %1, 1b \n" |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 317 | " nop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | " .set reorder \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 319 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 320 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | : "memory"); |
| 322 | } else { |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 323 | do { |
| 324 | __asm__ __volatile__( |
| 325 | "1: ll %1, %2 # arch_write_lock \n" |
| 326 | " bnez %1, 1b \n" |
| 327 | " lui %1, 0x8000 \n" |
| 328 | "2: sc %1, %0 \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 329 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp) |
| 330 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 331 | : "memory"); |
| 332 | } while (unlikely(!tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 334 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 335 | smp_llsc_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 338 | static inline void arch_write_unlock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Leonid Yegoshin | 6f6ed48 | 2015-06-01 17:09:52 -0700 | [diff] [blame] | 340 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 343 | " # arch_write_unlock \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | " sw $0, %0 \n" |
| 345 | : "=m" (rw->lock) |
| 346 | : "m" (rw->lock) |
| 347 | : "memory"); |
| 348 | } |
| 349 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 350 | static inline int arch_read_trylock(arch_rwlock_t *rw) |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 351 | { |
| 352 | unsigned int tmp; |
| 353 | int ret; |
| 354 | |
| 355 | if (R10000_LLSC_WAR) { |
| 356 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 357 | " .set noreorder # arch_read_trylock \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 358 | " li %2, 0 \n" |
| 359 | "1: ll %1, %3 \n" |
Dave Johnson | d52c2d5 | 2007-03-05 20:50:27 -0500 | [diff] [blame] | 360 | " bltz %1, 2f \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 361 | " addu %1, 1 \n" |
| 362 | " sc %1, %0 \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 363 | " .set reorder \n" |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 364 | " beqzl %1, 1b \n" |
| 365 | " nop \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 366 | __WEAK_LLSC_MB |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 367 | " li %2, 1 \n" |
| 368 | "2: \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 369 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret) |
| 370 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 371 | : "memory"); |
| 372 | } else { |
| 373 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 374 | " .set noreorder # arch_read_trylock \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 375 | " li %2, 0 \n" |
| 376 | "1: ll %1, %3 \n" |
Dave Johnson | d52c2d5 | 2007-03-05 20:50:27 -0500 | [diff] [blame] | 377 | " bltz %1, 2f \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 378 | " addu %1, 1 \n" |
| 379 | " sc %1, %0 \n" |
| 380 | " beqz %1, 1b \n" |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 381 | " nop \n" |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 382 | " .set reorder \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 383 | __WEAK_LLSC_MB |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 384 | " li %2, 1 \n" |
| 385 | "2: \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 386 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret) |
| 387 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 388 | : "memory"); |
| 389 | } |
| 390 | |
| 391 | return ret; |
| 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 394 | static inline int arch_write_trylock(arch_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | unsigned int tmp; |
| 397 | int ret; |
| 398 | |
| 399 | if (R10000_LLSC_WAR) { |
| 400 | __asm__ __volatile__( |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 401 | " .set noreorder # arch_write_trylock \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | " li %2, 0 \n" |
| 403 | "1: ll %1, %3 \n" |
| 404 | " bnez %1, 2f \n" |
| 405 | " lui %1, 0x8000 \n" |
| 406 | " sc %1, %0 \n" |
| 407 | " beqzl %1, 1b \n" |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 408 | " nop \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 409 | __WEAK_LLSC_MB |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | " li %2, 1 \n" |
| 411 | " .set reorder \n" |
| 412 | "2: \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 413 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret) |
| 414 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | : "memory"); |
| 416 | } else { |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 417 | do { |
| 418 | __asm__ __volatile__( |
| 419 | " ll %1, %3 # arch_write_trylock \n" |
| 420 | " li %2, 0 \n" |
| 421 | " bnez %1, 2f \n" |
| 422 | " lui %1, 0x8000 \n" |
| 423 | " sc %1, %0 \n" |
| 424 | " li %2, 1 \n" |
| 425 | "2: \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 426 | : "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 427 | "=&r" (ret) |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 428 | : GCC_OFF_SMALL_ASM() (rw->lock) |
Ralf Baechle | e01961c | 2013-04-11 00:16:53 +0200 | [diff] [blame] | 429 | : "memory"); |
| 430 | } while (unlikely(!tmp)); |
| 431 | |
| 432 | smp_llsc_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 438 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 439 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
Ralf Baechle | 65316fd | 2006-08-31 14:16:06 +0100 | [diff] [blame] | 440 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 441 | #define arch_spin_relax(lock) cpu_relax() |
| 442 | #define arch_read_relax(lock) cpu_relax() |
| 443 | #define arch_write_relax(lock) cpu_relax() |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | #endif /* _ASM_SPINLOCK_H */ |