]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/kernel.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / kernel.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * Copyright (c) 2014-2015 François Tigeot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 #ifndef _LINUX_KERNEL_H_
33 #define _LINUX_KERNEL_H_
34
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/param.h>
39 #include <sys/libkern.h>
40 #include <sys/stat.h>
41 #include <sys/smp.h>
42 #include <sys/stddef.h>
43 #include <sys/syslog.h>
44 #include <sys/time.h>
45
46 #include <linux/bitops.h>
47 #include <linux/compiler.h>
48 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/types.h>
51 #include <linux/jiffies.h>
52 #include <linux/wait.h>
53 #include <linux/log2.h> 
54 #include <asm/byteorder.h>
55
56 #include <machine/stdarg.h>
57
58 #define KERN_CONT       ""
59 #define KERN_EMERG      "<0>"
60 #define KERN_ALERT      "<1>"
61 #define KERN_CRIT       "<2>"
62 #define KERN_ERR        "<3>"
63 #define KERN_WARNING    "<4>"
64 #define KERN_NOTICE     "<5>"
65 #define KERN_INFO       "<6>"
66 #define KERN_DEBUG      "<7>"
67
68 #define U8_MAX          ((u8)~0U)
69 #define S8_MAX          ((s8)(U8_MAX >> 1))
70 #define S8_MIN          ((s8)(-S8_MAX - 1))
71 #define U16_MAX         ((u16)~0U)
72 #define S16_MAX         ((s16)(U16_MAX >> 1))
73 #define S16_MIN         ((s16)(-S16_MAX - 1))
74 #define U32_MAX         ((u32)~0U)
75 #define S32_MAX         ((s32)(U32_MAX >> 1))
76 #define S32_MIN         ((s32)(-S32_MAX - 1))
77 #define U64_MAX         ((u64)~0ULL)
78 #define S64_MAX         ((s64)(U64_MAX >> 1))
79 #define S64_MIN         ((s64)(-S64_MAX - 1))
80
81 #define S8_C(x)  x
82 #define U8_C(x)  x ## U
83 #define S16_C(x) x
84 #define U16_C(x) x ## U
85 #define S32_C(x) x
86 #define U32_C(x) x ## U
87 #define S64_C(x) x ## LL
88 #define U64_C(x) x ## ULL
89
90 #define BUILD_BUG_ON(x)         CTASSERT(!(x))
91
92 #define BUG()                   panic("BUG at %s:%d", __FILE__, __LINE__)
93 #define BUG_ON(cond)            do {                            \
94         if (cond) {                                             \
95                 panic("BUG ON %s failed at %s:%d",              \
96                     __stringify(cond), __FILE__, __LINE__);     \
97         }                                                       \
98 } while (0)
99
100 #define WARN_ON(cond) ({                                        \
101       bool __ret = (cond);                                      \
102       if (__ret) {                                              \
103                 printf("WARNING %s failed at %s:%d\n",          \
104                     __stringify(cond), __FILE__, __LINE__);     \
105       }                                                         \
106       unlikely(__ret);                                          \
107 })
108
109 #define WARN_ON_SMP(cond)       WARN_ON(cond)
110
111 #define WARN_ON_ONCE(cond) ({                                   \
112       static bool __warn_on_once;                               \
113       bool __ret = (cond);                                      \
114       if (__ret && !__warn_on_once) {                           \
115                 __warn_on_once = 1;                             \
116                 printf("WARNING %s failed at %s:%d\n",          \
117                     __stringify(cond), __FILE__, __LINE__);     \
118       }                                                         \
119       unlikely(__ret);                                          \
120 })
121
122 #undef  ALIGN
123 #define ALIGN(x, y)             roundup2((x), (y))
124 #undef PTR_ALIGN
125 #define PTR_ALIGN(p, a)         ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
126 #define DIV_ROUND_UP(x, n)      howmany(x, n)
127 #define DIV_ROUND_UP_ULL(x, n)  DIV_ROUND_UP((unsigned long long)(x), (n))
128 #define FIELD_SIZEOF(t, f)      sizeof(((t *)0)->f)
129
130 #define printk(...)             printf(__VA_ARGS__)
131 #define vprintk(f, a)           vprintf(f, a)
132
133 struct va_format {
134         const char *fmt;
135         va_list *va;
136 };
137
138 static inline int
139 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
140 {
141         ssize_t ssize = size;
142         int i;
143
144         i = vsnprintf(buf, size, fmt, args);
145
146         return ((i >= ssize) ? (ssize - 1) : i);
147 }
148
149 static inline int
150 scnprintf(char *buf, size_t size, const char *fmt, ...)
151 {
152         va_list args;
153         int i;
154
155         va_start(args, fmt);
156         i = vscnprintf(buf, size, fmt, args);
157         va_end(args);
158
159         return (i);
160 }
161
162 /*
163  * The "pr_debug()" and "pr_devel()" macros should produce zero code
164  * unless DEBUG is defined:
165  */
166 #ifdef DEBUG
167 #define pr_debug(fmt, ...) \
168         log(LOG_DEBUG, fmt, ##__VA_ARGS__)
169 #define pr_devel(fmt, ...) \
170         log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
171 #else
172 #define pr_debug(fmt, ...) \
173         ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
174 #define pr_devel(fmt, ...) \
175         ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
176 #endif
177
178 #ifndef pr_fmt
179 #define pr_fmt(fmt) fmt
180 #endif
181
182 /*
183  * Print a one-time message (analogous to WARN_ONCE() et al):
184  */
185 #define printk_once(...) do {                   \
186         static bool __print_once;               \
187                                                 \
188         if (!__print_once) {                    \
189                 __print_once = true;            \
190                 printk(__VA_ARGS__);            \
191         }                                       \
192 } while (0)
193
194 /*
195  * Log a one-time message (analogous to WARN_ONCE() et al):
196  */
197 #define log_once(level,...) do {                \
198         static bool __log_once;                 \
199                                                 \
200         if (unlikely(!__log_once)) {            \
201                 __log_once = true;              \
202                 log(level, __VA_ARGS__);        \
203         }                                       \
204 } while (0)
205
206 #define pr_emerg(fmt, ...) \
207         log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
208 #define pr_alert(fmt, ...) \
209         log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
210 #define pr_crit(fmt, ...) \
211         log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
212 #define pr_err(fmt, ...) \
213         log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
214 #define pr_warning(fmt, ...) \
215         log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
216 #define pr_warn(...) \
217         pr_warning(__VA_ARGS__)
218 #define pr_warn_once(fmt, ...) \
219         log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
220 #define pr_notice(fmt, ...) \
221         log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
222 #define pr_info(fmt, ...) \
223         log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
224 #define pr_info_once(fmt, ...) \
225         log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
226 #define pr_cont(fmt, ...) \
227         printk(KERN_CONT fmt, ##__VA_ARGS__)
228 #define pr_warn_ratelimited(...) do {           \
229         static linux_ratelimit_t __ratelimited; \
230         if (linux_ratelimited(&__ratelimited))  \
231                 pr_warning(__VA_ARGS__);        \
232 } while (0)
233
234 #ifndef WARN
235 #define WARN(condition, ...) ({                 \
236         bool __ret_warn_on = (condition);       \
237         if (unlikely(__ret_warn_on))            \
238                 pr_warning(__VA_ARGS__);        \
239         unlikely(__ret_warn_on);                \
240 })
241 #endif
242
243 #ifndef WARN_ONCE
244 #define WARN_ONCE(condition, ...) ({            \
245         bool __ret_warn_on = (condition);       \
246         if (unlikely(__ret_warn_on))            \
247                 pr_warn_once(__VA_ARGS__);      \
248         unlikely(__ret_warn_on);                \
249 })
250 #endif
251
252 #define container_of(ptr, type, member)                         \
253 ({                                                              \
254         const __typeof(((type *)0)->member) *__p = (ptr);       \
255         (type *)((uintptr_t)__p - offsetof(type, member));      \
256 })
257   
258 #define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
259
260 static inline unsigned long long
261 simple_strtoull(const char *cp, char **endp, unsigned int base)
262 {
263         return (strtouq(cp, endp, base));
264 }
265
266 static inline long long
267 simple_strtoll(const char *cp, char **endp, unsigned int base)
268 {
269         return (strtoq(cp, endp, base));
270 }
271
272 static inline unsigned long
273 simple_strtoul(const char *cp, char **endp, unsigned int base)
274 {
275         return (strtoul(cp, endp, base));
276 }
277
278 static inline long
279 simple_strtol(const char *cp, char **endp, unsigned int base)
280 {
281         return (strtol(cp, endp, base));
282 }
283
284 static inline int
285 kstrtoul(const char *cp, unsigned int base, unsigned long *res)
286 {
287         char *end;
288
289         *res = strtoul(cp, &end, base);
290
291         if (*cp == 0 || *end != 0)
292                 return (-EINVAL);
293         return (0);
294 }
295
296 static inline int
297 kstrtol(const char *cp, unsigned int base, long *res)
298 {
299         char *end;
300
301         *res = strtol(cp, &end, base);
302
303         if (*cp == 0 || *end != 0)
304                 return (-EINVAL);
305         return (0);
306 }
307
308 static inline int
309 kstrtoint(const char *cp, unsigned int base, int *res)
310 {
311         char *end;
312         long temp;
313
314         *res = temp = strtol(cp, &end, base);
315
316         if (*cp == 0 || *end != 0)
317                 return (-EINVAL);
318         if (temp != (int)temp)
319                 return (-ERANGE);
320         return (0);
321 }
322
323 static inline int
324 kstrtouint(const char *cp, unsigned int base, unsigned int *res)
325 {
326         char *end;
327         unsigned long temp;
328
329         *res = temp = strtoul(cp, &end, base);
330
331         if (*cp == 0 || *end != 0)
332                 return (-EINVAL);
333         if (temp != (unsigned int)temp)
334                 return (-ERANGE);
335         return (0);
336 }
337
338 static inline int
339 kstrtou32(const char *cp, unsigned int base, u32 *res)
340 {
341         char *end;
342         unsigned long temp;
343
344         *res = temp = strtoul(cp, &end, base);
345
346         if (*cp == 0 || *end != 0)
347                 return (-EINVAL);
348         if (temp != (u32)temp)
349                 return (-ERANGE);
350         return (0);
351 }
352
353 #define min(x, y)       ((x) < (y) ? (x) : (y))
354 #define max(x, y)       ((x) > (y) ? (x) : (y))
355
356 #define min3(a, b, c)   min(a, min(b,c))
357 #define max3(a, b, c)   max(a, max(b,c))
358
359 #define min_t(type, x, y) ({                    \
360         type __min1 = (x);                      \
361         type __min2 = (y);                      \
362         __min1 < __min2 ? __min1 : __min2; })
363
364 #define max_t(type, x, y) ({                    \
365         type __max1 = (x);                      \
366         type __max2 = (y);                      \
367         __max1 > __max2 ? __max1 : __max2; })
368
369 #define clamp_t(type, _x, min, max)     min_t(type, max_t(type, _x, min), max)
370 #define clamp(x, lo, hi)                min( max(x,lo), hi)
371 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
372
373 /*
374  * This looks more complex than it should be. But we need to
375  * get the type for the ~ right in round_down (it needs to be
376  * as wide as the result!), and we want to evaluate the macro
377  * arguments just once each.
378  */
379 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
380 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
381 #define round_down(x, y) ((x) & ~__round_mask(x, y))
382
383 #define smp_processor_id()      PCPU_GET(cpuid)
384 #define num_possible_cpus()     mp_ncpus
385 #define num_online_cpus()       mp_ncpus
386
387 #if defined(__i386__) || defined(__amd64__)
388 extern bool linux_cpu_has_clflush;
389 #define cpu_has_clflush         linux_cpu_has_clflush
390 #endif
391
392 typedef struct pm_message {
393         int event;
394 } pm_message_t;
395
396 /* Swap values of a and b */
397 #define swap(a, b) do {                 \
398         typeof(a) _swap_tmp = a;        \
399         a = b;                          \
400         b = _swap_tmp;                  \
401 } while (0)
402
403 #define DIV_ROUND_CLOSEST(x, divisor)   (((x) + ((divisor) / 2)) / (divisor))
404
405 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({            \
406         __typeof(divisor) __d = (divisor);              \
407         unsigned long long __ret = (x) + (__d) / 2;     \
408         __ret /= __d;                                   \
409         __ret;                                          \
410 })
411
412 static inline uintmax_t
413 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
414 {
415         uintmax_t q = (x / divisor);
416         uintmax_t r = (x % divisor);
417
418         return ((q * multiplier) + ((r * multiplier) / divisor));
419 }
420
421 static inline int64_t
422 abs64(int64_t x)
423 {
424         return (x < 0 ? -x : x);
425 }
426
427 typedef struct linux_ratelimit {
428         struct timeval lasttime;
429         int counter;
430 } linux_ratelimit_t;
431
432 static inline bool
433 linux_ratelimited(linux_ratelimit_t *rl)
434 {
435         return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
436 }
437
438 #endif  /* _LINUX_KERNEL_H_ */