2 * Copyright (C) 2010 ARM Limited. All rights reserved.
4 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
7 * A copy of the licence is included with the program, and can also be obtained from Free Software
8 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 * @file mali_osk_bitops.h
13 * Implementation of the OS abstraction layer for the kernel device driver
16 #ifndef __MALI_OSK_BITOPS_H__
17 #define __MALI_OSK_BITOPS_H__
24 MALI_STATIC_INLINE void _mali_internal_clear_bit( u32 bit, u32 *addr )
26 MALI_DEBUG_ASSERT( bit < 32 );
27 MALI_DEBUG_ASSERT( NULL != addr );
29 (*addr) &= ~(1 << bit);
32 MALI_STATIC_INLINE void _mali_internal_set_bit( u32 bit, u32 *addr )
34 MALI_DEBUG_ASSERT( bit < 32 );
35 MALI_DEBUG_ASSERT( NULL != addr );
37 (*addr) |= (1 << bit);
40 MALI_STATIC_INLINE u32 _mali_internal_test_bit( u32 bit, u32 value )
42 MALI_DEBUG_ASSERT( bit < 32 );
43 return value & (1 << bit);
46 MALI_STATIC_INLINE int _mali_internal_find_first_zero_bit( u32 value )
53 /* Begin with xxx...x0yyy...y, where ys are 1, number of ys is in range 0..31 */
54 inverted = ~value; /* zzz...z1000...0 */
55 /* Using count_trailing_zeros on inverted value -
56 * See ARM System Developers Guide for details of count_trailing_zeros */
58 /* Isolate the zero: it is preceeded by a run of 1s, so add 1 to it */
59 negated = (u32)-inverted ; /* -a == ~a + 1 (mod 2^n) for n-bit numbers */
60 /* negated = xxx...x1000...0 */
62 isolated = negated & inverted ; /* xxx...x1000...0 & zzz...z1000...0, zs are ~xs */
63 /* And so the first zero bit is in the same position as the 1 == number of 1s that preceeded it
64 * Note that the output is zero if value was all 1s */
66 leading_zeros = _mali_osk_clz( isolated );
68 return 31 - leading_zeros;
72 /** @defgroup _mali_osk_bitops OSK Non-atomic Bit-operations
76 * These bit-operations do not work atomically, and so locks must be used if
77 * atomicity is required.
79 * Reference implementations for Little Endian are provided, and so it should
80 * not normally be necessary to re-implement these. Efficient bit-twiddling
81 * techniques are used where possible, implemented in portable C.
83 * Note that these reference implementations rely on _mali_osk_clz() being
87 /** @brief Clear a bit in a sequence of 32-bit words
88 * @param nr bit number to clear, starting from the (Little-endian) least
90 * @param addr starting point for counting.
92 MALI_STATIC_INLINE void _mali_osk_clear_nonatomic_bit( u32 nr, u32 *addr )
94 addr += nr >> 5; /* find the correct word */
95 nr = nr & ((1 << 5)-1); /* The bit number within the word */
97 _mali_internal_clear_bit( nr, addr );
100 /** @brief Set a bit in a sequence of 32-bit words
101 * @param nr bit number to set, starting from the (Little-endian) least
103 * @param addr starting point for counting.
105 MALI_STATIC_INLINE void _mali_osk_set_nonatomic_bit( u32 nr, u32 *addr )
107 addr += nr >> 5; /* find the correct word */
108 nr = nr & ((1 << 5)-1); /* The bit number within the word */
110 _mali_internal_set_bit( nr, addr );
113 /** @brief Test a bit in a sequence of 32-bit words
114 * @param nr bit number to test, starting from the (Little-endian) least
116 * @param addr starting point for counting.
117 * @return zero if bit was clear, non-zero if set. Do not rely on the return
118 * value being related to the actual word under test.
120 MALI_STATIC_INLINE u32 _mali_osk_test_bit( u32 nr, u32 *addr )
122 addr += nr >> 5; /* find the correct word */
123 nr = nr & ((1 << 5)-1); /* The bit number within the word */
125 return _mali_internal_test_bit( nr, *addr );
128 /* Return maxbit if not found */
129 /** @brief Find the first zero bit in a sequence of 32-bit words
130 * @param addr starting point for search.
131 * @param maxbit the maximum number of bits to search
132 * @return the number of the first zero bit found, or maxbit if none were found
133 * in the specified range.
135 MALI_STATIC_INLINE u32 _mali_osk_find_first_zero_bit( const u32 *addr, u32 maxbit )
139 for ( total = 0; total < maxbit; total += 32, ++addr )
142 result = _mali_internal_find_first_zero_bit( *addr );
144 /* non-negative signifies the bit was found */
147 total += (u32)result;
152 /* Now check if we reached maxbit or above */
153 if ( total >= maxbit )
158 return total; /* either the found bit nr, or maxbit if not found */
160 /** @} */ /* end group _mali_osk_bitops */
166 #endif /* __MALI_OSK_BITOPS_H__ */