xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kernel.h (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
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 
45 #include <linux/bitops.h>
46 #include <linux/compiler.h>
47 #include <linux/errno.h>
48 #include <linux/kthread.h>
49 #include <linux/types.h>
50 #include <linux/jiffies.h>
51 #include <linux/wait.h>
52 #include <linux/log2.h>
53 #include <asm/byteorder.h>
54 
55 #include <machine/stdarg.h>
56 
57 #define KERN_CONT       ""
58 #define	KERN_EMERG	"<0>"
59 #define	KERN_ALERT	"<1>"
60 #define	KERN_CRIT	"<2>"
61 #define	KERN_ERR	"<3>"
62 #define	KERN_WARNING	"<4>"
63 #define	KERN_NOTICE	"<5>"
64 #define	KERN_INFO	"<6>"
65 #define	KERN_DEBUG	"<7>"
66 
67 #define	U8_MAX		((u8)~0U)
68 #define	S8_MAX		((s8)(U8_MAX >> 1))
69 #define	S8_MIN		((s8)(-S8_MAX - 1))
70 #define	U16_MAX		((u16)~0U)
71 #define	S16_MAX		((s16)(U16_MAX >> 1))
72 #define	S16_MIN		((s16)(-S16_MAX - 1))
73 #define	U32_MAX		((u32)~0U)
74 #define	S32_MAX		((s32)(U32_MAX >> 1))
75 #define	S32_MIN		((s32)(-S32_MAX - 1))
76 #define	U64_MAX		((u64)~0ULL)
77 #define	S64_MAX		((s64)(U64_MAX >> 1))
78 #define	S64_MIN		((s64)(-S64_MAX - 1))
79 
80 #define	S8_C(x)  x
81 #define	U8_C(x)  x ## U
82 #define	S16_C(x) x
83 #define	U16_C(x) x ## U
84 #define	S32_C(x) x
85 #define	U32_C(x) x ## U
86 #define	S64_C(x) x ## LL
87 #define	U64_C(x) x ## ULL
88 
89 #define	BUILD_BUG_ON(x)		CTASSERT(!(x))
90 
91 #define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
92 #define	BUG_ON(cond)		do {				\
93 	if (cond) {						\
94 		panic("BUG ON %s failed at %s:%d",		\
95 		    __stringify(cond), __FILE__, __LINE__);	\
96 	}							\
97 } while (0)
98 
99 #define	WARN_ON(cond) ({					\
100       bool __ret = (cond);					\
101       if (__ret) {						\
102 		printf("WARNING %s failed at %s:%d\n",		\
103 		    __stringify(cond), __FILE__, __LINE__);	\
104       }								\
105       unlikely(__ret);						\
106 })
107 
108 #define	WARN_ON_SMP(cond)	WARN_ON(cond)
109 
110 #define	WARN_ON_ONCE(cond) ({					\
111       static bool __warn_on_once;				\
112       bool __ret = (cond);					\
113       if (__ret && !__warn_on_once) {				\
114 		__warn_on_once = 1;				\
115 		printf("WARNING %s failed at %s:%d\n",		\
116 		    __stringify(cond), __FILE__, __LINE__);	\
117       }								\
118       unlikely(__ret);						\
119 })
120 
121 #undef	ALIGN
122 #define	ALIGN(x, y)		roundup2((x), (y))
123 #undef PTR_ALIGN
124 #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
125 #define	DIV_ROUND_UP(x, n)	howmany(x, n)
126 #define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
127 #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
128 
129 #define	printk(...)		printf(__VA_ARGS__)
130 #define	vprintk(f, a)		vprintf(f, a)
131 
132 struct va_format {
133 	const char *fmt;
134 	va_list *va;
135 };
136 
137 static inline int
138 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
139 {
140 	ssize_t ssize = size;
141 	int i;
142 
143 	i = vsnprintf(buf, size, fmt, args);
144 
145 	return ((i >= ssize) ? (ssize - 1) : i);
146 }
147 
148 static inline int
149 scnprintf(char *buf, size_t size, const char *fmt, ...)
150 {
151 	va_list args;
152 	int i;
153 
154 	va_start(args, fmt);
155 	i = vscnprintf(buf, size, fmt, args);
156 	va_end(args);
157 
158 	return (i);
159 }
160 
161 /*
162  * The "pr_debug()" and "pr_devel()" macros should produce zero code
163  * unless DEBUG is defined:
164  */
165 #ifdef DEBUG
166 #define pr_debug(fmt, ...) \
167         log(LOG_DEBUG, fmt, ##__VA_ARGS__)
168 #define pr_devel(fmt, ...) \
169 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
170 #else
171 #define pr_debug(fmt, ...) \
172         ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
173 #define pr_devel(fmt, ...) \
174 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
175 #endif
176 
177 #ifndef pr_fmt
178 #define pr_fmt(fmt) fmt
179 #endif
180 
181 /*
182  * Print a one-time message (analogous to WARN_ONCE() et al):
183  */
184 #define printk_once(...) do {			\
185 	static bool __print_once;		\
186 						\
187 	if (!__print_once) {			\
188 		__print_once = true;		\
189 		printk(__VA_ARGS__);		\
190 	}					\
191 } while (0)
192 
193 /*
194  * Log a one-time message (analogous to WARN_ONCE() et al):
195  */
196 #define log_once(level,...) do {		\
197 	static bool __log_once;			\
198 						\
199 	if (unlikely(!__log_once)) {		\
200 		__log_once = true;		\
201 		log(level, __VA_ARGS__);	\
202 	}					\
203 } while (0)
204 
205 #define pr_emerg(fmt, ...) \
206 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
207 #define pr_alert(fmt, ...) \
208 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
209 #define pr_crit(fmt, ...) \
210 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
211 #define pr_err(fmt, ...) \
212 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
213 #define pr_warning(fmt, ...) \
214 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
215 #define pr_warn(...) \
216 	pr_warning(__VA_ARGS__)
217 #define pr_warn_once(fmt, ...) \
218 	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
219 #define pr_notice(fmt, ...) \
220 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
221 #define pr_info(fmt, ...) \
222 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
223 #define pr_info_once(fmt, ...) \
224 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
225 #define pr_cont(fmt, ...) \
226 	printk(KERN_CONT fmt, ##__VA_ARGS__)
227 
228 #ifndef WARN
229 #define	WARN(condition, ...) ({			\
230         bool __ret_warn_on = (condition);	\
231         if (unlikely(__ret_warn_on))		\
232                 pr_warning(__VA_ARGS__);	\
233         unlikely(__ret_warn_on);		\
234 })
235 #endif
236 
237 #ifndef WARN_ONCE
238 #define	WARN_ONCE(condition, ...) ({		\
239         bool __ret_warn_on = (condition);	\
240         if (unlikely(__ret_warn_on))		\
241                 pr_warn_once(__VA_ARGS__);	\
242         unlikely(__ret_warn_on);		\
243 })
244 #endif
245 
246 #define container_of(ptr, type, member)				\
247 ({								\
248 	__typeof(((type *)0)->member) *_p = (ptr);		\
249 	(type *)((char *)_p - offsetof(type, member));		\
250 })
251 
252 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
253 
254 #define	simple_strtoul(...) strtoul(__VA_ARGS__)
255 #define	simple_strtol(...) strtol(__VA_ARGS__)
256 #define	kstrtol(a,b,c) ({*(c) = strtol(a,0,b); 0;})
257 #define	kstrtoint(a,b,c) ({*(c) = strtol(a,0,b); 0;})
258 #define	kstrtouint(a,b,c) ({*(c) = strtol(a,0,b); 0;})
259 
260 #define min(x, y)	((x) < (y) ? (x) : (y))
261 #define max(x, y)	((x) > (y) ? (x) : (y))
262 
263 #define min3(a, b, c)	min(a, min(b,c))
264 #define max3(a, b, c)	max(a, max(b,c))
265 
266 #define	min_t(type, x, y) ({			\
267 	type __min1 = (x);			\
268 	type __min2 = (y);			\
269 	__min1 < __min2 ? __min1 : __min2; })
270 
271 #define	max_t(type, x, y) ({			\
272 	type __max1 = (x);			\
273 	type __max2 = (y);			\
274 	__max1 > __max2 ? __max1 : __max2; })
275 
276 #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
277 #define clamp(x, lo, hi)		min( max(x,lo), hi)
278 #define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
279 
280 /*
281  * This looks more complex than it should be. But we need to
282  * get the type for the ~ right in round_down (it needs to be
283  * as wide as the result!), and we want to evaluate the macro
284  * arguments just once each.
285  */
286 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
287 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
288 #define round_down(x, y) ((x) & ~__round_mask(x, y))
289 
290 #define	smp_processor_id()	PCPU_GET(cpuid)
291 #define	num_possible_cpus()	mp_ncpus
292 #define	num_online_cpus()	mp_ncpus
293 
294 #if defined(__i386__) || defined(__amd64__)
295 extern bool linux_cpu_has_clflush;
296 #define	cpu_has_clflush		linux_cpu_has_clflush
297 #endif
298 
299 typedef struct pm_message {
300         int event;
301 } pm_message_t;
302 
303 /* Swap values of a and b */
304 #define swap(a, b) do {			\
305 	typeof(a) _swap_tmp = a;	\
306 	a = b;				\
307 	b = _swap_tmp;			\
308 } while (0)
309 
310 #define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
311 
312 #define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
313 	__typeof(divisor) __d = (divisor);		\
314 	unsigned long long __ret = (x) + (__d) / 2;	\
315 	__ret /= __d;					\
316 	__ret;						\
317 })
318 
319 static inline uintmax_t
320 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
321 {
322 	uintmax_t q = (x / divisor);
323 	uintmax_t r = (x % divisor);
324 
325 	return ((q * multiplier) + ((r * multiplier) / divisor));
326 }
327 
328 static inline int64_t
329 abs64(int64_t x)
330 {
331 	return (x < 0 ? -x : x);
332 }
333 
334 #endif	/* _LINUX_KERNEL_H_ */
335