1 /* Public domain. */ 2 3 #ifndef _LINUX_KERNEL_H 4 #define _LINUX_KERNEL_H 5 6 #include <sys/stdint.h> 7 #include <sys/param.h> 8 #include <sys/systm.h> 9 #include <sys/stdarg.h> 10 #include <sys/malloc.h> 11 12 #include <ddb/db_var.h> 13 14 #include <linux/types.h> 15 #include <linux/compiler.h> 16 #include <linux/bitops.h> 17 #include <linux/log2.h> 18 #include <linux/linkage.h> 19 #include <linux/printk.h> 20 #include <linux/typecheck.h> 21 #include <asm/byteorder.h> 22 23 #define swap(a, b) \ 24 do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0) 25 26 #define container_of(ptr, type, member) ({ \ 27 const __typeof( ((type *)0)->member ) *__mptr = (ptr); \ 28 (type *)( (char *)__mptr - offsetof(type,member) );}) 29 30 #define S8_MAX INT8_MAX 31 #define S16_MAX INT16_MAX 32 #define S32_MAX INT32_MAX 33 #define S64_MAX INT64_MAX 34 35 #define U8_MAX UINT8_MAX 36 #define U16_MAX UINT16_MAX 37 #define U32_MAX UINT32_MAX 38 #define U64_C(x) UINT64_C(x) 39 #define U64_MAX UINT64_MAX 40 41 #define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0) 42 43 #define ARRAY_SIZE nitems 44 45 #define lower_32_bits(n) ((u32)(n)) 46 #define upper_32_bits(_val) ((u32)(((_val) >> 16) >> 16)) 47 48 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg) 49 50 #define min_t(t, a, b) ({ \ 51 t __min_a = (a); \ 52 t __min_b = (b); \ 53 __min_a < __min_b ? __min_a : __min_b; }) 54 55 #define max_t(t, a, b) ({ \ 56 t __max_a = (a); \ 57 t __max_b = (b); \ 58 __max_a > __max_b ? __max_a : __max_b; }) 59 60 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b) 61 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b) 62 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b) 63 64 #define min(a, b) MIN(a, b) 65 #define max(a, b) MAX(a, b) 66 #define min3(x, y, z) MIN(x, MIN(y, z)) 67 #define max3(x, y, z) MAX(x, MAX(y, z)) 68 69 #define mult_frac(x, n, d) (((x) * (n)) / (d)) 70 71 #define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 72 #define round_down(x, y) (((x) / (y)) * (y)) /* y is power of two */ 73 #define rounddown(x, y) (((x) / (y)) * (y)) /* arbitary y */ 74 #define DIV_ROUND_UP(x, y) (((x) + ((y) - 1)) / (y)) 75 #define DIV_ROUND_UP_ULL(x, y) DIV_ROUND_UP(x, y) 76 #define DIV_ROUND_DOWN(x, y) ((x) / (y)) 77 #define DIV_ROUND_DOWN_ULL(x, y) DIV_ROUND_DOWN(x, y) 78 #define DIV_ROUND_CLOSEST(x, y) (((x) + ((y) / 2)) / (y)) 79 #define DIV_ROUND_CLOSEST_ULL(x, y) DIV_ROUND_CLOSEST(x, y) 80 81 static inline char * 82 kasprintf(int flags, const char *fmt, ...) 83 { 84 char *buf; 85 size_t len; 86 va_list ap; 87 88 va_start(ap, fmt); 89 len = vsnprintf(NULL, 0, fmt, ap); 90 va_end(ap); 91 92 buf = malloc(len, M_DRM, flags); 93 if (buf) { 94 va_start(ap, fmt); 95 vsnprintf(buf, len, fmt, ap); 96 va_end(ap); 97 } 98 99 return buf; 100 } 101 102 static inline char * 103 kvasprintf(int flags, const char *fmt, va_list ap) 104 { 105 char *buf; 106 size_t len; 107 108 len = vsnprintf(NULL, 0, fmt, ap); 109 110 buf = malloc(len, M_DRM, flags); 111 if (buf) { 112 vsnprintf(buf, len, fmt, ap); 113 } 114 115 return buf; 116 } 117 118 static inline int 119 _in_dbg_master(void) 120 { 121 #ifdef DDB 122 return (db_is_active); 123 #endif 124 return (0); 125 } 126 127 #define oops_in_progress _in_dbg_master() 128 129 #define might_sleep() 130 #define might_sleep_if(x) 131 132 #define add_taint(x, y) 133 #define TAINT_MACHINE_CHECK 0 134 #define LOCKDEP_STILL_OK 0 135 136 #define u64_to_user_ptr(x) ((void *)(uintptr_t)(x)) 137 138 #endif 139