xref: /kvm-unit-tests/lib/arm/asm/bitops.h (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
1 #ifndef _ASMARM_BITOPS_H_
2 #define _ASMARM_BITOPS_H_
3 /*
4  * Adapted from
5  *   arch/arm/lib/bitops.h
6  *
7  * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.
10  */
11 
12 #ifndef _BITOPS_H_
13 #error only <bitops.h> can be included directly
14 #endif
15 
16 #define BITS_PER_LONG	32
17 
18 #define HAVE_BUILTIN_FLS 1
19 
20 #define ATOMIC_BITOP(insn, mask, word)				\
21 ({								\
22 	unsigned long tmp1, tmp2;				\
23 	asm volatile(						\
24 	"1:	ldrex	%0, [%2]\n"				\
25 		insn"	%0, %0, %3\n"				\
26 	"	strex	%1, %0, [%2]\n"				\
27 	"	cmp	%1, #0\n"				\
28 	"	bne	1b\n"					\
29 	: "=&r" (tmp1), "=&r" (tmp2)				\
30 	: "r" (word), "r" (mask)				\
31 	: "cc");						\
32 })
33 
34 #define ATOMIC_TESTOP(insn, mask, word, old)			\
35 ({								\
36 	unsigned long tmp1, tmp2;				\
37 	asm volatile(						\
38 	"1:	ldrex	%0, [%3]\n"				\
39 	"	and	%1, %0, %4\n"				\
40 		insn"	%0, %0, %4\n"				\
41 	"	strex	%2, %0, [%3]\n"				\
42 	"	cmp	%2, #0\n"				\
43 	"	bne	1b\n"					\
44 	: "=&r" (tmp1), "=&r" (old), "=&r" (tmp2)		\
45 	: "r" (word), "r" (mask)				\
46 	: "cc");						\
47 })
48 
49 extern void set_bit(int nr, volatile unsigned long *addr);
50 extern void clear_bit(int nr, volatile unsigned long *addr);
51 extern int test_bit(int nr, const volatile unsigned long *addr);
52 extern int test_and_set_bit(int nr, volatile unsigned long *addr);
53 extern int test_and_clear_bit(int nr, volatile unsigned long *addr);
54 
55 #endif /* _ASMARM_BITOPS_H_ */
56