x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / x86 / include / asm / bitops.h
blob41639ce8fd634a4e8436a062006b763702749fc1
1 #ifndef _ASM_X86_BITOPS_H
2 #define _ASM_X86_BITOPS_H
4 /*
5 * Copyright 1992, Linus Torvalds.
7 * Note: inlines with more than a single statement should be marked
8 * __always_inline to avoid problems with older gcc's inlining heuristics.
9 */
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
13 #endif
15 #include <linux/compiler.h>
16 #include <asm/alternative.h>
18 #if BITS_PER_LONG == 32
19 # define _BITOPS_LONG_SHIFT 5
20 #elif BITS_PER_LONG == 64
21 # define _BITOPS_LONG_SHIFT 6
22 #else
23 # error "Unexpected BITS_PER_LONG"
24 #endif
26 #define BIT_64(n) (U64_C(1) << (n))
29 * These have to be done with inline assembly: that way the bit-setting
30 * is guaranteed to be atomic. All bit operations return 0 if the bit
31 * was cleared before the operation and != 0 if it was not.
33 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
36 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
37 /* Technically wrong, but this avoids compilation errors on some gcc
38 versions. */
39 #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
40 #else
41 #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
42 #endif
44 #define ADDR BITOP_ADDR(addr)
47 * We do the locked ops that don't return the old value as
48 * a mask operation on a byte.
50 #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
51 #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
52 #define CONST_MASK(nr) (1 << ((nr) & 7))
54 /**
55 * set_bit - Atomically set a bit in memory
56 * @nr: the bit to set
57 * @addr: the address to start counting from
59 * This function is atomic and may not be reordered. See __set_bit()
60 * if you do not require the atomic guarantees.
62 * Note: there are no guarantees that this function will not be reordered
63 * on non x86 architectures, so if you are writing portable code,
64 * make sure not to rely on its reordering guarantees.
66 * Note that @nr may be almost arbitrarily large; this function is not
67 * restricted to acting on a single-word quantity.
69 static __always_inline void
70 set_bit(long nr, volatile unsigned long *addr)
72 if (IS_IMMEDIATE(nr)) {
73 asm volatile(LOCK_PREFIX "orb %1,%0"
74 : CONST_MASK_ADDR(nr, addr)
75 : "iq" ((u8)CONST_MASK(nr))
76 : "memory");
77 } else {
78 asm volatile(LOCK_PREFIX "bts %1,%0"
79 : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
83 /**
84 * __set_bit - Set a bit in memory
85 * @nr: the bit to set
86 * @addr: the address to start counting from
88 * Unlike set_bit(), this function is non-atomic and may be reordered.
89 * If it's called on the same region of memory simultaneously, the effect
90 * may be that only one operation succeeds.
92 static inline void __set_bit(long nr, volatile unsigned long *addr)
94 asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
97 /**
98 * clear_bit - Clears a bit in memory
99 * @nr: Bit to clear
100 * @addr: Address to start counting from
102 * clear_bit() is atomic and may not be reordered. However, it does
103 * not contain a memory barrier, so if it is used for locking purposes,
104 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
105 * in order to ensure changes are visible on other processors.
107 static __always_inline void
108 clear_bit(long nr, volatile unsigned long *addr)
110 if (IS_IMMEDIATE(nr)) {
111 asm volatile(LOCK_PREFIX "andb %1,%0"
112 : CONST_MASK_ADDR(nr, addr)
113 : "iq" ((u8)~CONST_MASK(nr)));
114 } else {
115 asm volatile(LOCK_PREFIX "btr %1,%0"
116 : BITOP_ADDR(addr)
117 : "Ir" (nr));
122 * clear_bit_unlock - Clears a bit in memory
123 * @nr: Bit to clear
124 * @addr: Address to start counting from
126 * clear_bit() is atomic and implies release semantics before the memory
127 * operation. It can be used for an unlock.
129 static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
131 barrier();
132 clear_bit(nr, addr);
135 static inline void __clear_bit(long nr, volatile unsigned long *addr)
137 asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
141 * __clear_bit_unlock - Clears a bit in memory
142 * @nr: Bit to clear
143 * @addr: Address to start counting from
145 * __clear_bit() is non-atomic and implies release semantics before the memory
146 * operation. It can be used for an unlock if no other CPUs can concurrently
147 * modify other bits in the word.
149 * No memory barrier is required here, because x86 cannot reorder stores past
150 * older loads. Same principle as spin_unlock.
152 static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
154 barrier();
155 __clear_bit(nr, addr);
158 #define smp_mb__before_clear_bit() barrier()
159 #define smp_mb__after_clear_bit() barrier()
162 * __change_bit - Toggle a bit in memory
163 * @nr: the bit to change
164 * @addr: the address to start counting from
166 * Unlike change_bit(), this function is non-atomic and may be reordered.
167 * If it's called on the same region of memory simultaneously, the effect
168 * may be that only one operation succeeds.
170 static inline void __change_bit(long nr, volatile unsigned long *addr)
172 asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
176 * change_bit - Toggle a bit in memory
177 * @nr: Bit to change
178 * @addr: Address to start counting from
180 * change_bit() is atomic and may not be reordered.
181 * Note that @nr may be almost arbitrarily large; this function is not
182 * restricted to acting on a single-word quantity.
184 static inline void change_bit(long nr, volatile unsigned long *addr)
186 if (IS_IMMEDIATE(nr)) {
187 asm volatile(LOCK_PREFIX "xorb %1,%0"
188 : CONST_MASK_ADDR(nr, addr)
189 : "iq" ((u8)CONST_MASK(nr)));
190 } else {
191 asm volatile(LOCK_PREFIX "btc %1,%0"
192 : BITOP_ADDR(addr)
193 : "Ir" (nr));
198 * test_and_set_bit - Set a bit and return its old value
199 * @nr: Bit to set
200 * @addr: Address to count from
202 * This operation is atomic and cannot be reordered.
203 * It also implies a memory barrier.
205 static inline int test_and_set_bit(long nr, volatile unsigned long *addr)
207 int oldbit;
209 asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
210 "sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
212 return oldbit;
216 * test_and_set_bit_lock - Set a bit and return its old value for lock
217 * @nr: Bit to set
218 * @addr: Address to count from
220 * This is the same as test_and_set_bit on x86.
222 static __always_inline int
223 test_and_set_bit_lock(long nr, volatile unsigned long *addr)
225 return test_and_set_bit(nr, addr);
229 * __test_and_set_bit - Set a bit and return its old value
230 * @nr: Bit to set
231 * @addr: Address to count from
233 * This operation is non-atomic and can be reordered.
234 * If two examples of this operation race, one can appear to succeed
235 * but actually fail. You must protect multiple accesses with a lock.
237 static inline int __test_and_set_bit(long nr, volatile unsigned long *addr)
239 int oldbit;
241 asm("bts %2,%1\n\t"
242 "sbb %0,%0"
243 : "=r" (oldbit), ADDR
244 : "Ir" (nr));
245 return oldbit;
249 * test_and_clear_bit - Clear a bit and return its old value
250 * @nr: Bit to clear
251 * @addr: Address to count from
253 * This operation is atomic and cannot be reordered.
254 * It also implies a memory barrier.
256 static inline int test_and_clear_bit(long nr, volatile unsigned long *addr)
258 int oldbit;
260 asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
261 "sbb %0,%0"
262 : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
264 return oldbit;
268 * __test_and_clear_bit - Clear a bit and return its old value
269 * @nr: Bit to clear
270 * @addr: Address to count from
272 * This operation is non-atomic and can be reordered.
273 * If two examples of this operation race, one can appear to succeed
274 * but actually fail. You must protect multiple accesses with a lock.
276 * Note: the operation is performed atomically with respect to
277 * the local CPU, but not other CPUs. Portable code should not
278 * rely on this behaviour.
279 * KVM relies on this behaviour on x86 for modifying memory that is also
280 * accessed from a hypervisor on the same CPU if running in a VM: don't change
281 * this without also updating arch/x86/kernel/kvm.c
283 static inline int __test_and_clear_bit(long nr, volatile unsigned long *addr)
285 int oldbit;
287 asm volatile("btr %2,%1\n\t"
288 "sbb %0,%0"
289 : "=r" (oldbit), ADDR
290 : "Ir" (nr));
291 return oldbit;
294 /* WARNING: non atomic and it can be reordered! */
295 static inline int __test_and_change_bit(long nr, volatile unsigned long *addr)
297 int oldbit;
299 asm volatile("btc %2,%1\n\t"
300 "sbb %0,%0"
301 : "=r" (oldbit), ADDR
302 : "Ir" (nr) : "memory");
304 return oldbit;
308 * test_and_change_bit - Change a bit and return its old value
309 * @nr: Bit to change
310 * @addr: Address to count from
312 * This operation is atomic and cannot be reordered.
313 * It also implies a memory barrier.
315 static inline int test_and_change_bit(long nr, volatile unsigned long *addr)
317 int oldbit;
319 asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
320 "sbb %0,%0"
321 : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
323 return oldbit;
326 static __always_inline int constant_test_bit(long nr, const volatile unsigned long *addr)
328 return ((1UL << (nr & (BITS_PER_LONG-1))) &
329 (addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
332 static inline int variable_test_bit(long nr, volatile const unsigned long *addr)
334 int oldbit;
336 asm volatile("bt %2,%1\n\t"
337 "sbb %0,%0"
338 : "=r" (oldbit)
339 : "m" (*(unsigned long *)addr), "Ir" (nr));
341 return oldbit;
344 #if 0 /* Fool kernel-doc since it doesn't do macros yet */
346 * test_bit - Determine whether a bit is set
347 * @nr: bit number to test
348 * @addr: Address to start counting from
350 static int test_bit(int nr, const volatile unsigned long *addr);
351 #endif
353 #define test_bit(nr, addr) \
354 (__builtin_constant_p((nr)) \
355 ? constant_test_bit((nr), (addr)) \
356 : variable_test_bit((nr), (addr)))
359 * __ffs - find first set bit in word
360 * @word: The word to search
362 * Undefined if no bit exists, so code should check against 0 first.
364 static inline unsigned long __ffs(unsigned long word)
366 asm("rep; bsf %1,%0"
367 : "=r" (word)
368 : "rm" (word));
369 return word;
373 * ffz - find first zero bit in word
374 * @word: The word to search
376 * Undefined if no zero exists, so code should check against ~0UL first.
378 static inline unsigned long ffz(unsigned long word)
380 asm("rep; bsf %1,%0"
381 : "=r" (word)
382 : "r" (~word));
383 return word;
387 * __fls: find last set bit in word
388 * @word: The word to search
390 * Undefined if no set bit exists, so code should check against 0 first.
392 static inline unsigned long __fls(unsigned long word)
394 asm("bsr %1,%0"
395 : "=r" (word)
396 : "rm" (word));
397 return word;
400 #undef ADDR
402 #ifdef __KERNEL__
404 * ffs - find first set bit in word
405 * @x: the word to search
407 * This is defined the same way as the libc and compiler builtin ffs
408 * routines, therefore differs in spirit from the other bitops.
410 * ffs(value) returns 0 if value is 0 or the position of the first
411 * set bit if value is nonzero. The first (least significant) bit
412 * is at position 1.
414 static inline int ffs(int x)
416 int r;
418 #ifdef CONFIG_X86_64
420 * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the
421 * dest reg is undefined if x==0, but their CPU architect says its
422 * value is written to set it to the same as before, except that the
423 * top 32 bits will be cleared.
425 * We cannot do this on 32 bits because at the very least some
426 * 486 CPUs did not behave this way.
428 asm("bsfl %1,%0"
429 : "=r" (r)
430 : "rm" (x), "0" (-1));
431 #elif defined(CONFIG_X86_CMOV)
432 asm("bsfl %1,%0\n\t"
433 "cmovzl %2,%0"
434 : "=&r" (r) : "rm" (x), "r" (-1));
435 #else
436 asm("bsfl %1,%0\n\t"
437 "jnz 1f\n\t"
438 "movl $-1,%0\n"
439 "1:" : "=r" (r) : "rm" (x));
440 #endif
441 return r + 1;
445 * fls - find last set bit in word
446 * @x: the word to search
448 * This is defined in a similar way as the libc and compiler builtin
449 * ffs, but returns the position of the most significant set bit.
451 * fls(value) returns 0 if value is 0 or the position of the last
452 * set bit if value is nonzero. The last (most significant) bit is
453 * at position 32.
455 static inline int fls(int x)
457 int r;
459 #ifdef CONFIG_X86_64
461 * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the
462 * dest reg is undefined if x==0, but their CPU architect says its
463 * value is written to set it to the same as before, except that the
464 * top 32 bits will be cleared.
466 * We cannot do this on 32 bits because at the very least some
467 * 486 CPUs did not behave this way.
469 asm("bsrl %1,%0"
470 : "=r" (r)
471 : "rm" (x), "0" (-1));
472 #elif defined(CONFIG_X86_CMOV)
473 asm("bsrl %1,%0\n\t"
474 "cmovzl %2,%0"
475 : "=&r" (r) : "rm" (x), "rm" (-1));
476 #else
477 asm("bsrl %1,%0\n\t"
478 "jnz 1f\n\t"
479 "movl $-1,%0\n"
480 "1:" : "=r" (r) : "rm" (x));
481 #endif
482 return r + 1;
486 * fls64 - find last set bit in a 64-bit word
487 * @x: the word to search
489 * This is defined in a similar way as the libc and compiler builtin
490 * ffsll, but returns the position of the most significant set bit.
492 * fls64(value) returns 0 if value is 0 or the position of the last
493 * set bit if value is nonzero. The last (most significant) bit is
494 * at position 64.
496 #ifdef CONFIG_X86_64
497 static __always_inline int fls64(__u64 x)
499 int bitpos = -1;
501 * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the
502 * dest reg is undefined if x==0, but their CPU architect says its
503 * value is written to set it to the same as before.
505 asm("bsrq %1,%q0"
506 : "+r" (bitpos)
507 : "rm" (x));
508 return bitpos + 1;
510 #else
511 #include <asm-generic/bitops/fls64.h>
512 #endif
514 #include <asm-generic/bitops/find.h>
516 #include <asm-generic/bitops/sched.h>
518 #define ARCH_HAS_FAST_MULTIPLIER 1
520 #include <asm/arch_hweight.h>
522 #include <asm-generic/bitops/const_hweight.h>
524 #include <asm-generic/bitops/le.h>
526 #include <asm-generic/bitops/ext2-atomic-setbit.h>
528 #endif /* __KERNEL__ */
529 #endif /* _ASM_X86_BITOPS_H */