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