blob: a78a2d70cd8b2aa1acd2050a9006f88a9bfab163 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* asm/bitops.h for Linux/CRIS
2 *
3 * TODO: asm versions if speed is needed
4 *
5 * All bit operations return 0 if the bit was cleared before the
6 * operation and != 0 if it was not.
7 *
8 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
9 */
10
11#ifndef _CRIS_BITOPS_H
12#define _CRIS_BITOPS_H
13
14/* Currently this is unsuitable for consumption outside the kernel. */
15#ifdef __KERNEL__
16
Jiri Slaby06245172007-10-18 23:40:26 -070017#ifndef _LINUX_BITOPS_H
18#error only <linux/bitops.h> can be included directly
19#endif
20
Jesper Nilsson556dcee2008-10-21 17:45:58 +020021#include <arch/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/system.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/compiler.h>
25
26/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * set_bit - Atomically set a bit in memory
28 * @nr: the bit to set
29 * @addr: the address to start counting from
30 *
31 * This function is atomic and may not be reordered. See __set_bit()
32 * if you do not require the atomic guarantees.
33 * Note that @nr may be almost arbitrarily large; this function is not
34 * restricted to acting on a single-word quantity.
35 */
36
37#define set_bit(nr, addr) (void)test_and_set_bit(nr, addr)
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * clear_bit - Clears a bit in memory
41 * @nr: Bit to clear
42 * @addr: Address to start counting from
43 *
44 * clear_bit() is atomic and may not be reordered. However, it does
45 * not contain a memory barrier, so if it is used for locking purposes,
46 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
47 * in order to ensure changes are visible on other processors.
48 */
49
50#define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * change_bit - Toggle a bit in memory
54 * @nr: Bit to change
55 * @addr: Address to start counting from
56 *
57 * change_bit() is atomic and may not be reordered.
58 * Note that @nr may be almost arbitrarily large; this function is not
59 * restricted to acting on a single-word quantity.
60 */
61
62#define change_bit(nr, addr) (void)test_and_change_bit(nr, addr)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/**
65 * test_and_set_bit - Set a bit and return its old value
66 * @nr: Bit to set
67 * @addr: Address to count from
68 *
69 * This operation is atomic and cannot be reordered.
70 * It also implies a memory barrier.
71 */
72
Adrian Bunkd9b54442005-11-07 00:58:44 -080073static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 unsigned int mask, retval;
76 unsigned long flags;
77 unsigned int *adr = (unsigned int *)addr;
78
79 adr += nr >> 5;
80 mask = 1 << (nr & 0x1f);
Mikael Starvik5d01e6c2005-07-27 11:44:43 -070081 cris_atomic_save(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 retval = (mask & *adr) != 0;
83 *adr |= mask;
Mikael Starvik5d01e6c2005-07-27 11:44:43 -070084 cris_atomic_restore(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return retval;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * clear_bit() doesn't provide any barrier for the compiler.
90 */
91#define smp_mb__before_clear_bit() barrier()
92#define smp_mb__after_clear_bit() barrier()
93
94/**
95 * test_and_clear_bit - Clear a bit and return its old value
96 * @nr: Bit to clear
97 * @addr: Address to count from
98 *
99 * This operation is atomic and cannot be reordered.
100 * It also implies a memory barrier.
101 */
102
Adrian Bunkd9b54442005-11-07 00:58:44 -0800103static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned int mask, retval;
106 unsigned long flags;
107 unsigned int *adr = (unsigned int *)addr;
108
109 adr += nr >> 5;
110 mask = 1 << (nr & 0x1f);
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700111 cris_atomic_save(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 retval = (mask & *adr) != 0;
113 *adr &= ~mask;
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700114 cris_atomic_restore(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return retval;
116}
117
118/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * test_and_change_bit - Change a bit and return its old value
120 * @nr: Bit to change
121 * @addr: Address to count from
122 *
123 * This operation is atomic and cannot be reordered.
124 * It also implies a memory barrier.
125 */
126
Adrian Bunkd9b54442005-11-07 00:58:44 -0800127static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 unsigned int mask, retval;
130 unsigned long flags;
131 unsigned int *adr = (unsigned int *)addr;
132 adr += nr >> 5;
133 mask = 1 << (nr & 0x1f);
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700134 cris_atomic_save(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 retval = (mask & *adr) != 0;
136 *adr ^= mask;
Mikael Starvik5d01e6c2005-07-27 11:44:43 -0700137 cris_atomic_restore(addr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return retval;
139}
140
Akinobu Mitae9f26df2006-03-26 01:39:21 -0800141#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * Since we define it "external", it collides with the built-in
145 * definition, which doesn't have the same semantics. We don't want to
146 * use -fno-builtin, so just hide the name ffs.
147 */
148#define ffs kernel_ffs
149
Akinobu Mitae9f26df2006-03-26 01:39:21 -0800150#include <asm-generic/bitops/fls.h>
Rusty Russell09997692009-01-03 15:37:14 +1030151#include <asm-generic/bitops/__fls.h>
Akinobu Mitae9f26df2006-03-26 01:39:21 -0800152#include <asm-generic/bitops/fls64.h>
153#include <asm-generic/bitops/hweight.h>
154#include <asm-generic/bitops/find.h>
Nick Piggin26333572007-10-18 03:06:39 -0700155#include <asm-generic/bitops/lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700157#include <asm-generic/bitops/le.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Akinobu Mita148817b2011-07-26 16:09:04 -0700159#include <asm-generic/bitops/ext2-atomic-setbit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Akinobu Mitae9f26df2006-03-26 01:39:21 -0800161#include <asm-generic/bitops/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163#endif /* __KERNEL__ */
164
165#endif /* _CRIS_BITOPS_H */