xref: /dragonfly/sys/dev/drm/include/linux/kernel.h (revision 655933d6)
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-2020 François Tigeot <ftigeot@wolfpond.org>
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 #ifndef	_LINUX_KERNEL_H_
31 #define	_LINUX_KERNEL_H_
32 
33 #include <sys/stdarg.h>
34 #include <linux/stddef.h>
35 #include <linux/types.h>
36 #include <linux/compiler.h>
37 #include <linux/bitops.h>
38 #include <linux/log2.h>
39 #include <linux/typecheck.h>
40 #include <linux/printk.h>
41 
42 #include <sys/systm.h>
43 #include <sys/param.h>
44 #include <sys/libkern.h>
45 #include <sys/stat.h>
46 #include <sys/endian.h>
47 
48 #define U8_MAX		((u8)~0U)
49 #define U16_MAX		((u16)~0U)
50 #define U32_MAX		((u32)~0U)
51 #define U64_MAX		((u64)~0ULL)
52 #define S64_MAX		((s64)(U64_MAX>>1))
53 
54 #include <machine/limits.h>	/* LONG_MAX etc... */
55 
56 #undef	ALIGN
57 #define	ALIGN(x, y)		roundup2((x), (y))
58 #define	IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
59 #define	DIV_ROUND_UP		howmany
60 #define DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))
61 
62 #define udelay(t)       	DELAY(t)
63 
64 #define container_of(ptr, type, member)				\
65 ({								\
66 	__typeof(((type *)0)->member) *_p = (ptr);		\
67 	(type *)((char *)_p - offsetof(type, member));		\
68 })
69 
70 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
71 
72 #define	simple_strtoul	strtoul
73 #define	simple_strtol	strtol
74 
75 #define min(x, y)			((x) < (y) ? (x) : (y))
76 #define max(x, y)			((x) > (y) ? (x) : (y))
77 
78 #define min3(a, b, c)			min(a, min(b,c))
79 #define max3(a, b, c)			max(a, max(b,c))
80 
81 #define min_t(type, _x, _y)		((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
82 #define max_t(type, _x, _y)		((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
83 
84 #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
85 #define clamp(x, lo, hi)		min( max(x,lo), hi)
86 #define clamp_val(val, lo, hi)		clamp_t(typeof(val), val, lo, hi)
87 
88 /*
89  * This looks more complex than it should be. But we need to
90  * get the type for the ~ right in round_down (it needs to be
91  * as wide as the result!), and we want to evaluate the macro
92  * arguments just once each.
93  */
94 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
95 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
96 #define round_down(x, y) ((x) & ~__round_mask(x, y))
97 
98 #define	num_possible_cpus()	mp_ncpus
99 
100 typedef struct pm_message {
101         int event;
102 } pm_message_t;
103 
104 /* Swap values of a and b */
105 #define swap(a, b)			\
106 ({					\
107 	typeof(a) _swap_tmp = a;	\
108 	a = b;				\
109 	b = _swap_tmp;			\
110 })
111 
112 #define DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) /2)) / (divisor))
113 
114 static inline uintmax_t
115 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
116 {
117 	uintmax_t q = (x / divisor);
118 	uintmax_t r = (x % divisor);
119 
120 	return ((q * multiplier) + ((r * multiplier) / divisor));
121 }
122 
123 static inline int64_t abs64(int64_t x)
124 {
125 	return (x < 0 ? -x : x);
126 }
127 
128 #define DIV_ROUND_CLOSEST_ULL(ll, d)	\
129  ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
130 
131 #define	upper_32_bits(n)	((u32)(((n) >> 16) >> 16))
132 #define	lower_32_bits(n)	((u32)(n))
133 
134 /* Byteorder compat layer */
135 #if _BYTE_ORDER == _BIG_ENDIAN
136 #define	__BIG_ENDIAN 4321
137 #else
138 #define	__LITTLE_ENDIAN 1234
139 #endif
140 
141 #define	cpu_to_le16(x)	htole16(x)
142 #define	le16_to_cpu(x)	le16toh(x)
143 #define	cpu_to_le32(x)	htole32(x)
144 #define	le32_to_cpu(x)	le32toh(x)
145 #define	le32_to_cpup(x)	le32toh(*x)
146 
147 #define	cpu_to_be16(x)	htobe16(x)
148 #define	be16_to_cpu(x)	be16toh(x)
149 #define	cpu_to_be32(x)	htobe32(x)
150 #define	be32_to_cpu(x)	be32toh(x)
151 #define	be32_to_cpup(x)	be32toh(*x)
152 
153 static inline int __must_check
154 kstrtouint(const char *s, unsigned int base, unsigned int *res)
155 {
156 	*(res) = strtol(s,0,base);
157 
158 	return 0;
159 }
160 
161 char *kvasprintf(int flags, const char *format, va_list ap);
162 char *kasprintf(int flags, const char *format, ...);
163 
164 static inline void __user *
165 u64_to_user_ptr(u64 address)
166 {
167 	return (void __user *)(uintptr_t)address;
168 }
169 
170 static inline void
171 might_sleep(void)
172 {
173 }
174 
175 #define might_sleep_if(cond)
176 
177 #define snprintf	ksnprintf
178 #define sprintf		ksprintf
179 
180 static inline int __printf(3, 0)
181 vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
182 {
183 	return kvsnprintf(buf, size, fmt, args);
184 }
185 
186 static inline int __printf(3, 0)
187 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
188 {
189 	int ret;
190 
191 	if (size == 0)
192 		return 0;
193 
194 	ret = vsnprintf(buf, size, fmt, args);
195 	if (ret < size)
196 		return ret;
197 
198 	return size - 1;
199 }
200 
201 static inline int __printf(3, 4)
202 scnprintf(char *buf, size_t size, const char *fmt, ...)
203 {
204 	va_list args;
205 	int i;
206 
207 	va_start(args, fmt);
208 	i = vscnprintf(buf, size, fmt, args);
209 	va_end(args);
210 
211 	return (i);
212 }
213 
214 static inline int
215 kstrtol(const char *cp, unsigned int base, long *res)
216 {
217 	char *end;
218 
219 	*res = strtol(cp, &end, base);
220 
221 	/* skip newline character, if any */
222 	if (*end == '\n')
223 		end++;
224 	if (*cp == 0 || *end != 0)
225 		return (-EINVAL);
226 	return (0);
227 }
228 
229 #define oops_in_progress	(panicstr != NULL)
230 
231 enum lockdep_ok {
232 	LOCKDEP_STILL_OK,
233 	LOCKDEP_NOW_UNRELIABLE
234 };
235 
236 #define TAINT_MACHINE_CHECK	4
237 
238 static inline void
239 add_taint(unsigned flag, enum lockdep_ok lo)
240 {
241 }
242 
243 #define STUB() do { kprintf("%s: stub\n", __func__); } while(0);
244 
245 #endif	/* _LINUX_KERNEL_H_ */
246