"Das U-Boot" Source Tree
at jcs/rk3128 299 lines 8.6 kB view raw
1#ifndef _LINUX_KERNEL_H 2#define _LINUX_KERNEL_H 3 4#include <linux/types.h> 5#include <linux/printk.h> /* for printf/pr_* utilities */ 6#include <limits.h> 7 8#define USHRT_MAX ((u16)(~0U)) 9#define SHRT_MAX ((s16)(USHRT_MAX>>1)) 10#define SHRT_MIN ((s16)(-SHRT_MAX - 1)) 11#define INT_MIN (-INT_MAX - 1) 12#define LONG_MAX ((long)(~0UL>>1)) 13#define LONG_MIN (-LONG_MAX - 1) 14#define ULONG_MAX (~0UL) 15#define LLONG_MAX ((long long)(~0ULL>>1)) 16#define LLONG_MIN (-LLONG_MAX - 1) 17#define ULLONG_MAX (~0ULL) 18 19#define U8_MAX ((u8)~0U) 20#define S8_MAX ((s8)(U8_MAX>>1)) 21#define S8_MIN ((s8)(-S8_MAX - 1)) 22#define U16_MAX ((u16)~0U) 23#define S16_MAX ((s16)(U16_MAX>>1)) 24#define S16_MIN ((s16)(-S16_MAX - 1)) 25#define U32_MAX ((u32)~0U) 26#define S32_MAX ((s32)(U32_MAX>>1)) 27#define S32_MIN ((s32)(-S32_MAX - 1)) 28#define U64_MAX ((u64)~0ULL) 29#define S64_MAX ((s64)(U64_MAX>>1)) 30#define S64_MIN ((s64)(-S64_MAX - 1)) 31 32#define INT32_MAX S32_MAX 33 34#define STACK_MAGIC 0xdeadbeef 35 36#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 37 38#define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1) 39#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a)) 40#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 41#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 42#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 43 44#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 45 46/* 47 * This looks more complex than it should be. But we need to 48 * get the type for the ~ right in round_down (it needs to be 49 * as wide as the result!), and we want to evaluate the macro 50 * arguments just once each. 51 */ 52#define __round_mask(x, y) ((__typeof__(x))((y)-1)) 53#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 54#define round_down(x, y) ((x) & ~__round_mask(x, y)) 55 56#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 57#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 58 59#define DIV_ROUND_DOWN_ULL(ll, d) \ 60 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 61 62#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) 63 64#define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 65 66#if BITS_PER_LONG == 32 67# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 68#else 69# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 70#endif 71 72/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ 73#define roundup(x, y) ( \ 74{ \ 75 const typeof(y) __y = y; \ 76 (((x) + (__y - 1)) / __y) * __y; \ 77} \ 78) 79#define rounddown(x, y) ( \ 80{ \ 81 typeof(x) __x = (x); \ 82 __x - (__x % (y)); \ 83} \ 84) 85 86/* 87 * Divide positive or negative dividend by positive divisor and round 88 * to closest integer. Result is undefined for negative divisors and 89 * for negative dividends if the divisor variable type is unsigned. 90 */ 91#define DIV_ROUND_CLOSEST(x, divisor)( \ 92{ \ 93 typeof(x) __x = x; \ 94 typeof(divisor) __d = divisor; \ 95 (((typeof(x))-1) > 0 || \ 96 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ 97 (((__x) + ((__d) / 2)) / (__d)) : \ 98 (((__x) - ((__d) / 2)) / (__d)); \ 99} \ 100) 101/* 102 * Same as above but for u64 dividends. divisor must be a 32-bit 103 * number. 104 */ 105#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 106{ \ 107 typeof(divisor) __d = divisor; \ 108 unsigned long long _tmp = (x) + (__d) / 2; \ 109 do_div(_tmp, __d); \ 110 _tmp; \ 111} \ 112) 113 114/* 115 * Multiplies an integer by a fraction, while avoiding unnecessary 116 * overflow or loss of precision. 117 */ 118#define mult_frac(x, numer, denom)( \ 119{ \ 120 typeof(x) quot = (x) / (denom); \ 121 typeof(x) rem = (x) % (denom); \ 122 (quot * (numer)) + ((rem * (numer)) / (denom)); \ 123} \ 124) 125 126/** 127 * upper_32_bits - return bits 32-63 of a number 128 * @n: the number we're accessing 129 * 130 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 131 * the "right shift count >= width of type" warning when that quantity is 132 * 32-bits. 133 */ 134#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 135 136/** 137 * lower_32_bits - return bits 0-31 of a number 138 * @n: the number we're accessing 139 */ 140#define lower_32_bits(n) ((u32)(n)) 141 142/* 143 * abs() handles unsigned and signed longs, ints, shorts and chars. For all 144 * input types abs() returns a signed long. 145 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() 146 * for those. 147 */ 148#define abs(x) ({ \ 149 long ret; \ 150 if (sizeof(x) == sizeof(long)) { \ 151 long __x = (x); \ 152 ret = (__x < 0) ? -__x : __x; \ 153 } else { \ 154 int __x = (x); \ 155 ret = (__x < 0) ? -__x : __x; \ 156 } \ 157 ret; \ 158 }) 159 160#define abs64(x) ({ \ 161 s64 __x = (x); \ 162 (__x < 0) ? -__x : __x; \ 163 }) 164 165/* 166 * min()/max()/clamp() macros that also do 167 * strict type-checking.. See the 168 * "unnecessary" pointer comparison. 169 */ 170#define min(x, y) ({ \ 171 typeof(x) _min1 = (x); \ 172 typeof(y) _min2 = (y); \ 173 (void) (&_min1 == &_min2); \ 174 _min1 < _min2 ? _min1 : _min2; }) 175 176#define max(x, y) ({ \ 177 typeof(x) _max1 = (x); \ 178 typeof(y) _max2 = (y); \ 179 (void) (&_max1 == &_max2); \ 180 _max1 > _max2 ? _max1 : _max2; }) 181 182#define min3(x, y, z) min((typeof(x))min(x, y), z) 183#define max3(x, y, z) max((typeof(x))max(x, y), z) 184 185/** 186 * min_not_zero - return the minimum that is _not_ zero, unless both are zero 187 * @x: value1 188 * @y: value2 189 */ 190#define min_not_zero(x, y) ({ \ 191 typeof(x) __x = (x); \ 192 typeof(y) __y = (y); \ 193 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 194 195/** 196 * clamp - return a value clamped to a given range with strict typechecking 197 * @val: current value 198 * @lo: lowest allowable value 199 * @hi: highest allowable value 200 * 201 * This macro does strict typechecking of lo/hi to make sure they are of the 202 * same type as val. See the unnecessary pointer comparisons. 203 */ 204#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 205 206/* 207 * ..and if you can't take the strict 208 * types, you can specify one yourself. 209 * 210 * Or not use min/max/clamp at all, of course. 211 */ 212#define min_t(type, x, y) ({ \ 213 type __min1 = (x); \ 214 type __min2 = (y); \ 215 __min1 < __min2 ? __min1: __min2; }) 216 217#define max_t(type, x, y) ({ \ 218 type __max1 = (x); \ 219 type __max2 = (y); \ 220 __max1 > __max2 ? __max1: __max2; }) 221 222/** 223 * clamp_t - return a value clamped to a given range using a given type 224 * @type: the type of variable to use 225 * @val: current value 226 * @lo: minimum allowable value 227 * @hi: maximum allowable value 228 * 229 * This macro does no typechecking and uses temporary variables of type 230 * 'type' to make all the comparisons. 231 */ 232#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 233 234/** 235 * clamp_val - return a value clamped to a given range using val's type 236 * @val: current value 237 * @lo: minimum allowable value 238 * @hi: maximum allowable value 239 * 240 * This macro does no typechecking and uses temporary variables of whatever 241 * type the input argument 'val' is. This is useful when val is an unsigned 242 * type and min and max are literals that will otherwise be assigned a signed 243 * integer type. 244 */ 245#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 246 247/* 248 * swap - swap value of @a and @b 249 */ 250#define swap(a, b) \ 251 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 252 253/** 254 * container_of - cast a member of a structure out to the containing structure 255 * @ptr: the pointer to the member. 256 * @type: the type of the container struct this is embedded in. 257 * @member: the name of the member within the struct. 258 * 259 */ 260#define container_of(ptr, type, member) ({ \ 261 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 262 (type *)( (char *)__mptr - offsetof(type,member) );}) 263 264/* 265 * check_member() - Check the offset of a structure member 266 * 267 * @structure: Name of structure (e.g. global_data) 268 * @member: Name of member (e.g. baudrate) 269 * @offset: Expected offset in bytes 270 */ 271#define check_member(structure, member, offset) _Static_assert( \ 272 offsetof(struct structure, member) == (offset), \ 273 "`struct " #structure "` offset for `" #member "` is not " #offset) 274 275#define __find_closest(x, a, as, op) \ 276({ \ 277 typeof(as) __fc_i, __fc_as = (as) - 1; \ 278 typeof(x) __fc_x = (x); \ 279 typeof(*a) const *__fc_a = (a); \ 280 for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \ 281 if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \ 282 __fc_a[__fc_i + 1], 2)) \ 283 break; \ 284 } \ 285 (__fc_i); \ 286}) 287 288/** 289 * find_closest - locate the closest element in a sorted array 290 * @x: The reference value. 291 * @a: The array in which to look for the closest element. Must be sorted 292 * in ascending order. 293 * @as: Size of 'a'. 294 * 295 * Returns the index of the element closest to 'x'. 296 */ 297#define find_closest(x, a, as) __find_closest(x, a, as, <=) 298 299#endif