1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2014-2016 François Tigeot 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 #ifndef _LINUX_KERNEL_H_ 30 #define _LINUX_KERNEL_H_ 31 32 #include <sys/systm.h> 33 #include <sys/param.h> 34 #include <sys/libkern.h> 35 #include <sys/stat.h> 36 37 #include <linux/bitops.h> 38 #include <linux/compiler.h> 39 #include <linux/log2.h> 40 #include <linux/types.h> 41 #include <linux/printk.h> 42 43 #define KERN_CONT "" 44 #define KERN_EMERG "<0>" 45 #define KERN_ALERT "<1>" 46 #define KERN_CRIT "<2>" 47 #define KERN_ERR "<3>" 48 #define KERN_WARNING "<4>" 49 #define KERN_NOTICE "<5>" 50 #define KERN_INFO "<6>" 51 #define KERN_DEBUG "<7>" 52 53 #define BUG() do \ 54 { \ 55 panic("BUG in %s at %s:%u", \ 56 __func__, __FILE__, __LINE__); \ 57 } while (0) 58 59 #define BUG_ON(condition) do { if (condition) BUG(); } while(0) 60 61 #define _WARN_STR(x) #x 62 63 #define WARN_ON(condition) ({ \ 64 int __ret = !!(condition); \ 65 if (__ret) \ 66 kprintf("WARNING %s failed at %s:%d\n", \ 67 _WARN_STR(condition), __FILE__, __LINE__); \ 68 unlikely(__ret); \ 69 }) 70 71 #define WARN_ON_SMP(condition) WARN_ON(condition) 72 73 #define WARN_ON_ONCE(condition) ({ \ 74 static int __warned; \ 75 int __ret = !!(condition); \ 76 if (__ret && !__warned) { \ 77 kprintf("WARNING %s failed at %s:%d\n", \ 78 _WARN_STR(condition), __FILE__, __LINE__); \ 79 __warned = 1; \ 80 } \ 81 unlikely(__ret); \ 82 }) 83 84 #undef ALIGN 85 #define ALIGN(x, y) roundup2((x), (y)) 86 #define DIV_ROUND_UP howmany 87 #define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N)) 88 89 #define printk(X...) kprintf(X) 90 #define pr_debug(fmt, ...) printk(KERN_DEBUG # fmt, ##__VA_ARGS__) 91 #define udelay(t) DELAY(t) 92 93 #ifndef pr_fmt 94 #define pr_fmt(fmt) fmt 95 #endif 96 97 /* 98 * Print a one-time message (analogous to WARN_ONCE() et al): 99 */ 100 #define printk_once(x...) ({ \ 101 static bool __print_once; \ 102 \ 103 if (!__print_once) { \ 104 __print_once = true; \ 105 printk(x); \ 106 } \ 107 }) 108 109 110 111 #define pr_emerg(fmt, ...) \ 112 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 113 #define pr_alert(fmt, ...) \ 114 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 115 #define pr_crit(fmt, ...) \ 116 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 117 #define pr_err(fmt, ...) \ 118 kprintf(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 119 #define pr_warning(fmt, ...) \ 120 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 121 #define pr_warn pr_warning 122 #define pr_notice(fmt, ...) \ 123 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 124 #define pr_info(fmt, ...) \ 125 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 126 #define pr_cont(fmt, ...) \ 127 printk(KERN_CONT fmt, ##__VA_ARGS__) 128 129 /* pr_devel() should produce zero code unless DEBUG is defined */ 130 #ifdef DEBUG 131 #define pr_devel(fmt, ...) \ 132 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 133 #else 134 #define pr_devel(fmt, ...) \ 135 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 136 #endif 137 138 #ifndef WARN 139 #define WARN(condition, format...) ({ \ 140 int __ret_warn_on = !!(condition); \ 141 if (unlikely(__ret_warn_on)) \ 142 pr_warning(format); \ 143 unlikely(__ret_warn_on); \ 144 }) 145 #endif 146 147 #define WARN_ONCE(condition, format...) ({ \ 148 static bool __warned_once; \ 149 int __ret = !!(condition); \ 150 \ 151 if ((condition) && !__warned_once) { \ 152 WARN(condition, format); \ 153 __warned_once = true; \ 154 } \ 155 unlikely(__ret); \ 156 }) 157 158 #define container_of(ptr, type, member) \ 159 ({ \ 160 __typeof(((type *)0)->member) *_p = (ptr); \ 161 (type *)((char *)_p - offsetof(type, member)); \ 162 }) 163 164 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 165 166 #define simple_strtoul strtoul 167 #define simple_strtol strtol 168 169 #define min(x, y) ((x) < (y) ? (x) : (y)) 170 #define max(x, y) ((x) > (y) ? (x) : (y)) 171 172 #define min3(a, b, c) min(a, min(b,c)) 173 #define max3(a, b, c) max(a, max(b,c)) 174 175 #define min_t(type, _x, _y) ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y)) 176 #define max_t(type, _x, _y) ((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y)) 177 178 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 179 #define clamp(x, lo, hi) min( max(x,lo), hi) 180 181 /* 182 * This looks more complex than it should be. But we need to 183 * get the type for the ~ right in round_down (it needs to be 184 * as wide as the result!), and we want to evaluate the macro 185 * arguments just once each. 186 */ 187 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 188 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 189 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 190 191 #define num_possible_cpus() mp_ncpus 192 193 typedef struct pm_message { 194 int event; 195 } pm_message_t; 196 197 /* Swap values of a and b */ 198 #define swap(a, b) \ 199 ({ \ 200 typeof(a) _swap_tmp = a; \ 201 a = b; \ 202 b = _swap_tmp; \ 203 }) 204 205 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) /2)) / (divisor)) 206 207 static inline uintmax_t 208 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 209 { 210 uintmax_t q = (x / divisor); 211 uintmax_t r = (x % divisor); 212 213 return ((q * multiplier) + ((r * multiplier) / divisor)); 214 } 215 216 static inline int64_t abs64(int64_t x) 217 { 218 return (x < 0 ? -x : x); 219 } 220 221 /* simplified version of kvasnrprintf() for drm needs. */ 222 char *drm_vasprintf(int flags, const char *format, __va_list ap) __printflike(2, 0); 223 char *drm_asprintf(int flags, const char *format, ...) __printflike(2, 3); 224 225 #define DIV_ROUND_CLOSEST_ULL(ll, d) \ 226 ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; }) 227 228 #endif /* _LINUX_KERNEL_H_ */ 229