summaryrefslogtreecommitdiffstats
path: root/arch/ppc/kernel/bitops.c
blob: fb5a19e3a3e054677d38787911548f851676060c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 1996 Paul Mackerras.
 */

#include <linux/kernel.h>
#include <asm/bitops.h>

/*
 * I left these here since the problems with "cc" make it difficult to keep
 * them in bitops.h -- Cort
 */
void set_bit(int nr, volatile void *addr)
{
	unsigned int t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "set_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%2
	or	%0,%0,%1
	stwcx.	%0,0,%2
	bne	1b"
	: "=&r" (t)		/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");
}

void clear_bit(int nr, volatile void *addr)
{
	unsigned int t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "clear_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%2
	andc	%0,%0,%1
	stwcx.	%0,0,%2
	bne	1b"
	: "=&r" (t)		/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");
}

void change_bit(int nr, volatile void *addr)
{
	unsigned int t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "change_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%2
	xor	%0,%0,%1
	stwcx.	%0,0,%2
	bne	1b"
	: "=&r" (t)		/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");
}

int test_and_set_bit(int nr, volatile void *addr)
{
	unsigned int old, t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "test_and_set_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%3
	or	%1,%0,%2
	stwcx.	%1,0,%3
	bne	1b"
	: "=&r" (old), "=&r" (t)	/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");

	return (old & mask) != 0;
}

int test_and_clear_bit(int nr, volatile void *addr)
{
	unsigned int old, t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "test_and_clear_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%3
	andc	%1,%0,%2
	stwcx.	%1,0,%3
	bne	1b"
	: "=&r" (old), "=&r" (t)	/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");

	return (old & mask) != 0;
}

int test_and_change_bit(int nr, volatile void *addr)
{
	unsigned int old, t;
	unsigned int mask = 1 << (nr & 0x1f);
	volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);

	if ((unsigned long)addr & 3)
		printk(KERN_ERR "test_and_change_bit(%x, %p)\n", nr, addr);
	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%3
	xor	%1,%0,%2
	stwcx.	%1,0,%3
	bne	1b"
	: "=&r" (old), "=&r" (t)	/*, "=m" (*p)*/
	: "r" (mask), "r" (p)
	: "cc");

	return (old & mask) != 0;
}

/* I put it in bitops.h -- Cort */
#if 0
int ffz(unsigned int x)
{
	int n;

	x = ~x & (x+1);		/* set LS zero to 1, other bits to 0 */
	__asm__ ("cntlzw %0,%1" : "=r" (n) : "r" (x));
	return 31 - n;
}

/*
 * This implementation of find_{first,next}_zero_bit was stolen from
 * Linus' asm-alpha/bitops.h.
 */

int find_first_zero_bit(void * addr, int size)
{
	unsigned int * p = ((unsigned int *) addr);
	unsigned int result = 0;
	unsigned int tmp;

	if (size == 0)
		return 0;
	while (size & ~31UL) {
		if (~(tmp = *(p++)))
			goto found_middle;
		result += 32;
		size -= 32;
	}
	if (!size)
		return result;
	tmp = *p;
	tmp |= ~0UL << size;
found_middle:
	return result + ffz(tmp);
}

/*
 * Find next zero bit in a bitmap reasonably efficiently..
 */
int find_next_zero_bit(void * addr, int size, int offset)
{
	unsigned int * p = ((unsigned int *) addr) + (offset >> 5);
	unsigned int result = offset & ~31UL;
	unsigned int tmp;

	if (offset >= size)
		return size;
	size -= result;
	offset &= 31UL;
	if (offset) {
		tmp = *(p++);
		tmp |= ~0UL >> (32-offset);
		if (size < 32)
			goto found_first;
		if (~tmp)
			goto found_middle;
		size -= 32;
		result += 32;
	}
	while (size & ~31UL) {
		if (~(tmp = *(p++)))
			goto found_middle;
		result += 32;
		size -= 32;
	}
	if (!size)
		return result;
	tmp = *p;
found_first:
	tmp |= ~0UL << size;
found_middle:
	return result + ffz(tmp);
}
#endif