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