]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/asm-i386/spinlock.h
[PATCH] i386 spinlocks: disable interrupts only if we enabled them
[mirror_ubuntu-bionic-kernel.git] / include / asm-i386 / spinlock.h
CommitLineData
1da177e4
LT
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>
7#include <linux/config.h>
8#include <linux/compiler.h>
9
1da177e4
LT
10/*
11 * Your basic SMP spinlocks, allowing only a single CPU anywhere
fb1c8f93 12 *
1da177e4
LT
13 * Simple spin lock operations. There are two variants, one clears IRQ's
14 * on the local processor, one does not.
15 *
16 * We make no fairness assumptions. They have a cost.
fb1c8f93
IM
17 *
18 * (the type definitions are in asm/spinlock_types.h)
1da177e4
LT
19 */
20
fb1c8f93
IM
21#define __raw_spin_is_locked(x) \
22 (*(volatile signed char *)(&(x)->slock) <= 0)
1da177e4 23
fb1c8f93 24#define __raw_spin_lock_string \
1da177e4
LT
25 "\n1:\t" \
26 "lock ; decb %0\n\t" \
27 "jns 3f\n" \
28 "2:\t" \
29 "rep;nop\n\t" \
30 "cmpb $0,%0\n\t" \
31 "jle 2b\n\t" \
32 "jmp 1b\n" \
33 "3:\n\t"
34
fb1c8f93 35#define __raw_spin_lock_string_flags \
1da177e4
LT
36 "\n1:\t" \
37 "lock ; decb %0\n\t" \
42c059e0 38 "jns 5f\n" \
1da177e4
LT
39 "2:\t" \
40 "testl $0x200, %1\n\t" \
42c059e0
CE
41 "jz 4f\n\t" \
42 "sti\n" \
1da177e4
LT
43 "3:\t" \
44 "rep;nop\n\t" \
45 "cmpb $0, %0\n\t" \
46 "jle 3b\n\t" \
47 "cli\n\t" \
48 "jmp 1b\n" \
42c059e0
CE
49 "4:\t" \
50 "rep;nop\n\t" \
51 "cmpb $0, %0\n\t" \
52 "jg 1b\n\t" \
53 "jmp 4b\n" \
54 "5:\n\t"
1da177e4 55
9a0b5817
GH
56#define __raw_spin_lock_string_up \
57 "\n\tdecb %0"
58
fb1c8f93
IM
59static inline void __raw_spin_lock(raw_spinlock_t *lock)
60{
9a0b5817
GH
61 alternative_smp(
62 __raw_spin_lock_string,
63 __raw_spin_lock_string_up,
64 "=m" (lock->slock) : : "memory");
fb1c8f93
IM
65}
66
67static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
68{
9a0b5817
GH
69 alternative_smp(
70 __raw_spin_lock_string_flags,
71 __raw_spin_lock_string_up,
72 "=m" (lock->slock) : "r" (flags) : "memory");
fb1c8f93
IM
73}
74
75static inline int __raw_spin_trylock(raw_spinlock_t *lock)
76{
77 char oldval;
78 __asm__ __volatile__(
79 "xchgb %b0,%1"
80 :"=q" (oldval), "=m" (lock->slock)
81 :"0" (0) : "memory");
82 return oldval > 0;
83}
84
1da177e4 85/*
fb1c8f93
IM
86 * __raw_spin_unlock based on writing $1 to the low byte.
87 * This method works. Despite all the confusion.
88 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
1da177e4
LT
89 * (PPro errata 66, 92)
90 */
91
92#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
93
fb1c8f93 94#define __raw_spin_unlock_string \
1da177e4
LT
95 "movb $1,%0" \
96 :"=m" (lock->slock) : : "memory"
97
98
fb1c8f93 99static inline void __raw_spin_unlock(raw_spinlock_t *lock)
1da177e4 100{
1da177e4 101 __asm__ __volatile__(
fb1c8f93 102 __raw_spin_unlock_string
1da177e4
LT
103 );
104}
105
106#else
107
fb1c8f93 108#define __raw_spin_unlock_string \
1da177e4
LT
109 "xchgb %b0, %1" \
110 :"=q" (oldval), "=m" (lock->slock) \
111 :"0" (oldval) : "memory"
112
fb1c8f93 113static inline void __raw_spin_unlock(raw_spinlock_t *lock)
1da177e4
LT
114{
115 char oldval = 1;
1da177e4 116
1da177e4 117 __asm__ __volatile__(
fb1c8f93
IM
118 __raw_spin_unlock_string
119 );
1da177e4
LT
120}
121
1da177e4 122#endif
1da177e4 123
fb1c8f93
IM
124#define __raw_spin_unlock_wait(lock) \
125 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
1da177e4
LT
126
127/*
128 * Read-write spinlocks, allowing multiple readers
129 * but only one writer.
130 *
131 * NOTE! it is quite common to have readers in interrupts
132 * but no interrupt writers. For those circumstances we
133 * can "mix" irq-safe locks - any writer needs to get a
134 * irq-safe write-lock, but readers can get non-irqsafe
135 * read-locks.
fb1c8f93
IM
136 *
137 * On x86, we implement read-write locks as a 32-bit counter
138 * with the high bit (sign) being the "contended" bit.
139 *
140 * The inline assembly is non-obvious. Think about it.
141 *
142 * Changed to use the same technique as rw semaphores. See
143 * semaphore.h for details. -ben
144 *
145 * the helpers are in arch/i386/kernel/semaphore.c
1da177e4 146 */
1da177e4
LT
147
148/**
149 * read_can_lock - would read_trylock() succeed?
150 * @lock: the rwlock in question.
151 */
fb1c8f93 152#define __raw_read_can_lock(x) ((int)(x)->lock > 0)
1da177e4
LT
153
154/**
155 * write_can_lock - would write_trylock() succeed?
156 * @lock: the rwlock in question.
157 */
fb1c8f93 158#define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
1da177e4 159
fb1c8f93 160static inline void __raw_read_lock(raw_rwlock_t *rw)
1da177e4 161{
1da177e4
LT
162 __build_read_lock(rw, "__read_lock_failed");
163}
164
fb1c8f93 165static inline void __raw_write_lock(raw_rwlock_t *rw)
1da177e4 166{
1da177e4
LT
167 __build_write_lock(rw, "__write_lock_failed");
168}
169
fb1c8f93 170static inline int __raw_read_trylock(raw_rwlock_t *lock)
1da177e4
LT
171{
172 atomic_t *count = (atomic_t *)lock;
173 atomic_dec(count);
174 if (atomic_read(count) >= 0)
175 return 1;
176 atomic_inc(count);
177 return 0;
178}
179
fb1c8f93 180static inline int __raw_write_trylock(raw_rwlock_t *lock)
1da177e4
LT
181{
182 atomic_t *count = (atomic_t *)lock;
183 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
184 return 1;
185 atomic_add(RW_LOCK_BIAS, count);
186 return 0;
187}
188
fb1c8f93
IM
189static inline void __raw_read_unlock(raw_rwlock_t *rw)
190{
9a0b5817 191 asm volatile(LOCK_PREFIX "incl %0" :"=m" (rw->lock) : : "memory");
fb1c8f93
IM
192}
193
194static inline void __raw_write_unlock(raw_rwlock_t *rw)
195{
9a0b5817 196 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
fb1c8f93
IM
197 : "=m" (rw->lock) : : "memory");
198}
199
1da177e4 200#endif /* __ASM_SPINLOCK_H */