aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/mali400/r3p2/mali/common/mali_osk_bitops.h
blob: f262f7dad37d507567885a2fccc5f44f55e8cd41 (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
/*
 * Copyright (C) 2010 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @file mali_osk_bitops.h
 * Implementation of the OS abstraction layer for the kernel device driver
 */

#ifndef __MALI_OSK_BITOPS_H__
#define __MALI_OSK_BITOPS_H__

#ifdef __cplusplus
extern "C"
{
#endif

MALI_STATIC_INLINE void _mali_internal_clear_bit( u32 bit, u32 *addr )
{
	MALI_DEBUG_ASSERT( bit < 32 );
	MALI_DEBUG_ASSERT( NULL != addr );

	(*addr) &= ~(1 << bit);
}

MALI_STATIC_INLINE void _mali_internal_set_bit( u32 bit, u32 *addr )
{
	MALI_DEBUG_ASSERT( bit < 32 );
	MALI_DEBUG_ASSERT( NULL != addr );

	(*addr) |= (1 << bit);
}

MALI_STATIC_INLINE u32 _mali_internal_test_bit( u32 bit, u32 value )
{
	MALI_DEBUG_ASSERT( bit < 32 );
	return value & (1 << bit);
}

MALI_STATIC_INLINE int _mali_internal_find_first_zero_bit( u32 value )
{
	u32 inverted;
	u32 negated;
	u32 isolated;
	u32 leading_zeros;

	/* Begin with xxx...x0yyy...y, where ys are 1, number of ys is in range  0..31 */
	inverted = ~value; /* zzz...z1000...0 */
	/* Using count_trailing_zeros on inverted value -
	 * See ARM System Developers Guide for details of count_trailing_zeros */

	/* Isolate the zero: it is preceeded by a run of 1s, so add 1 to it */
	negated = (u32)-inverted ; /* -a == ~a + 1 (mod 2^n) for n-bit numbers */
    /* negated = xxx...x1000...0 */

	isolated = negated & inverted ; /* xxx...x1000...0 & zzz...z1000...0, zs are ~xs */
	/* And so the first zero bit is in the same position as the 1 == number of 1s that preceeded it
	 * Note that the output is zero if value was all 1s */

	leading_zeros = _mali_osk_clz( isolated );

	return 31 - leading_zeros;
}


/** @defgroup _mali_osk_bitops OSK Non-atomic Bit-operations
 * @{ */

/**
 * These bit-operations do not work atomically, and so locks must be used if
 * atomicity is required.
 *
 * Reference implementations for Little Endian are provided, and so it should
 * not normally be necessary to re-implement these. Efficient bit-twiddling
 * techniques are used where possible, implemented in portable C.
 *
 * Note that these reference implementations rely on _mali_osk_clz() being
 * implemented.
 */

/** @brief Clear a bit in a sequence of 32-bit words
 * @param nr bit number to clear, starting from the (Little-endian) least
 * significant bit
 * @param addr starting point for counting.
 */
MALI_STATIC_INLINE void _mali_osk_clear_nonatomic_bit( u32 nr, u32 *addr )
{
	addr += nr >> 5; /* find the correct word */
	nr = nr & ((1 << 5)-1); /* The bit number within the word */

	_mali_internal_clear_bit( nr, addr );
}

/** @brief Set a bit in a sequence of 32-bit words
 * @param nr bit number to set, starting from the (Little-endian) least
 * significant bit
 * @param addr starting point for counting.
 */
MALI_STATIC_INLINE void _mali_osk_set_nonatomic_bit( u32 nr, u32 *addr )
{
	addr += nr >> 5; /* find the correct word */
	nr = nr & ((1 << 5)-1); /* The bit number within the word */

	_mali_internal_set_bit( nr, addr );
}

/** @brief Test a bit in a sequence of 32-bit words
 * @param nr bit number to test, starting from the (Little-endian) least
 * significant bit
 * @param addr starting point for counting.
 * @return zero if bit was clear, non-zero if set. Do not rely on the return
 * value being related to the actual word under test.
 */
MALI_STATIC_INLINE u32 _mali_osk_test_bit( u32 nr, u32 *addr )
{
	addr += nr >> 5; /* find the correct word */
	nr = nr & ((1 << 5)-1); /* The bit number within the word */

	return _mali_internal_test_bit( nr, *addr );
}

/* Return maxbit if not found */
/** @brief Find the first zero bit in a sequence of 32-bit words
 * @param addr starting point for search.
 * @param maxbit the maximum number of bits to search
 * @return the number of the first zero bit found, or maxbit if none were found
 * in the specified range.
 */
MALI_STATIC_INLINE u32 _mali_osk_find_first_zero_bit( const u32 *addr, u32 maxbit )
{
	u32 total;

	for ( total = 0; total < maxbit; total += 32, ++addr )
	{
		int result;
		result = _mali_internal_find_first_zero_bit( *addr );

		/* non-negative signifies the bit was found */
		if ( result >= 0 )
		{
			total += (u32)result;
			break;
		}
	}

	/* Now check if we reached maxbit or above */
	if ( total >= maxbit )
	{
		total = maxbit;
	}

	return total; /* either the found bit nr, or maxbit if not found */
}
/** @} */ /* end group _mali_osk_bitops */

#ifdef __cplusplus
}
#endif

#endif /* __MALI_OSK_BITOPS_H__ */