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