xref: /dflybsd-src/sys/dev/drm/include/linux/kernel.h (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1be7d0317SFrançois Tigeot /*
2c4962bc9SFrançois Tigeot  * Copyright (c) 2010 Isilon Systems, Inc.
3c4962bc9SFrançois Tigeot  * Copyright (c) 2010 iX Systems, Inc.
4c4962bc9SFrançois Tigeot  * Copyright (c) 2010 Panasas, Inc.
5bd04c403SFrançois Tigeot  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
636c92a32SFrançois Tigeot  * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
7c4962bc9SFrançois Tigeot  * All rights reserved.
8c4962bc9SFrançois Tigeot  *
9c4962bc9SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
10c4962bc9SFrançois Tigeot  * modification, are permitted provided that the following conditions
11c4962bc9SFrançois Tigeot  * are met:
12c4962bc9SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
13c4962bc9SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
14c4962bc9SFrançois Tigeot  *    disclaimer.
15c4962bc9SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
16c4962bc9SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
17c4962bc9SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
18c4962bc9SFrançois Tigeot  *
19c4962bc9SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20c4962bc9SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21c4962bc9SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22c4962bc9SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23c4962bc9SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24c4962bc9SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25c4962bc9SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26c4962bc9SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27c4962bc9SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28c4962bc9SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29c4962bc9SFrançois Tigeot  */
30c4962bc9SFrançois Tigeot #ifndef	_LINUX_KERNEL_H_
31c4962bc9SFrançois Tigeot #define	_LINUX_KERNEL_H_
32c4962bc9SFrançois Tigeot 
339286b91eSSascha Wildner #include <sys/stdarg.h>
34a8601baeSFrançois Tigeot #include <linux/stddef.h>
35d6aa1cc5SFrançois Tigeot #include <linux/types.h>
36d6aa1cc5SFrançois Tigeot #include <linux/compiler.h>
37d6aa1cc5SFrançois Tigeot #include <linux/bitops.h>
38d6aa1cc5SFrançois Tigeot #include <linux/log2.h>
392cecdd68SFrançois Tigeot #include <linux/typecheck.h>
40d6aa1cc5SFrançois Tigeot #include <linux/printk.h>
41d6aa1cc5SFrançois Tigeot 
42c4962bc9SFrançois Tigeot #include <sys/systm.h>
43c4962bc9SFrançois Tigeot #include <sys/param.h>
44c4962bc9SFrançois Tigeot #include <sys/libkern.h>
45c4962bc9SFrançois Tigeot #include <sys/stat.h>
463dc5a57bSzrj #include <sys/endian.h>
47c4962bc9SFrançois Tigeot 
48a85cb24fSFrançois Tigeot #define U8_MAX		((u8)~0U)
493f2dd94aSFrançois Tigeot #define U16_MAX		((u16)~0U)
50a85cb24fSFrançois Tigeot #define U32_MAX		((u32)~0U)
51173d94baSFrançois Tigeot #define U64_MAX		((u64)~0ULL)
52*78973132SSergey Zigachev #define S64_MAX		((s64)(U64_MAX>>1))
53173d94baSFrançois Tigeot 
54647fa573SFrançois Tigeot #include <machine/limits.h>	/* LONG_MAX etc... */
55647fa573SFrançois Tigeot 
56c4962bc9SFrançois Tigeot #undef	ALIGN
57c4962bc9SFrançois Tigeot #define	ALIGN(x, y)		roundup2((x), (y))
581837f0f6Szrj #define	IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
59c4962bc9SFrançois Tigeot #define	DIV_ROUND_UP		howmany
60d7b47d24SFrançois Tigeot #define DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))
61c4962bc9SFrançois Tigeot 
62c4962bc9SFrançois Tigeot #define udelay(t)       	DELAY(t)
63c4962bc9SFrançois Tigeot 
64c4962bc9SFrançois Tigeot #define container_of(ptr, type, member)				\
65c4962bc9SFrançois Tigeot ({								\
66c4962bc9SFrançois Tigeot 	__typeof(((type *)0)->member) *_p = (ptr);		\
67c4962bc9SFrançois Tigeot 	(type *)((char *)_p - offsetof(type, member));		\
68c4962bc9SFrançois Tigeot })
69c4962bc9SFrançois Tigeot 
70c4962bc9SFrançois Tigeot #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
71c4962bc9SFrançois Tigeot 
72c4962bc9SFrançois Tigeot #define	simple_strtoul	strtoul
73c4962bc9SFrançois Tigeot #define	simple_strtol	strtol
74c4962bc9SFrançois Tigeot 
75cb2667bdSSascha Wildner #define min(x, y)			((x) < (y) ? (x) : (y))
76cb2667bdSSascha Wildner #define max(x, y)			((x) > (y) ? (x) : (y))
77d7b47d24SFrançois Tigeot 
7895de0cb6SFrançois Tigeot #define min3(a, b, c)			min(a, min(b,c))
79d7b47d24SFrançois Tigeot #define max3(a, b, c)			max(a, max(b,c))
80d7b47d24SFrançois Tigeot 
812d227569SSascha Wildner #define min_t(type, _x, _y)		((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
822d227569SSascha Wildner #define max_t(type, _x, _y)		((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
83c4962bc9SFrançois Tigeot 
842d227569SSascha Wildner #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
8524edb884SFrançois Tigeot #define clamp(x, lo, hi)		min( max(x,lo), hi)
86bd04c403SFrançois Tigeot #define clamp_val(val, lo, hi)		clamp_t(typeof(val), val, lo, hi)
8724edb884SFrançois Tigeot 
88c4962bc9SFrançois Tigeot /*
89c4962bc9SFrançois Tigeot  * This looks more complex than it should be. But we need to
90c4962bc9SFrançois Tigeot  * get the type for the ~ right in round_down (it needs to be
91c4962bc9SFrançois Tigeot  * as wide as the result!), and we want to evaluate the macro
92c4962bc9SFrançois Tigeot  * arguments just once each.
93c4962bc9SFrançois Tigeot  */
94c4962bc9SFrançois Tigeot #define __round_mask(x, y) ((__typeof__(x))((y)-1))
95c4962bc9SFrançois Tigeot #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
96c4962bc9SFrançois Tigeot #define round_down(x, y) ((x) & ~__round_mask(x, y))
97c4962bc9SFrançois Tigeot 
98c4962bc9SFrançois Tigeot #define	num_possible_cpus()	mp_ncpus
99c4962bc9SFrançois Tigeot 
100c4962bc9SFrançois Tigeot typedef struct pm_message {
101c4962bc9SFrançois Tigeot         int event;
102c4962bc9SFrançois Tigeot } pm_message_t;
103c4962bc9SFrançois Tigeot 
10495bc7f26SFrançois Tigeot /* Swap values of a and b */
10595bc7f26SFrançois Tigeot #define swap(a, b)			\
10695bc7f26SFrançois Tigeot ({					\
10795bc7f26SFrançois Tigeot 	typeof(a) _swap_tmp = a;	\
10895bc7f26SFrançois Tigeot 	a = b;				\
10995bc7f26SFrançois Tigeot 	b = _swap_tmp;			\
11095bc7f26SFrançois Tigeot })
11195bc7f26SFrançois Tigeot 
11204d865a6SFrançois Tigeot #define DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) /2)) / (divisor))
11304d865a6SFrançois Tigeot 
114057bf798SFrançois Tigeot static inline uintmax_t
mult_frac(uintmax_t x,uintmax_t multiplier,uintmax_t divisor)115057bf798SFrançois Tigeot mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
116057bf798SFrançois Tigeot {
117057bf798SFrançois Tigeot 	uintmax_t q = (x / divisor);
118057bf798SFrançois Tigeot 	uintmax_t r = (x % divisor);
119057bf798SFrançois Tigeot 
120057bf798SFrançois Tigeot 	return ((q * multiplier) + ((r * multiplier) / divisor));
121057bf798SFrançois Tigeot }
122057bf798SFrançois Tigeot 
abs64(int64_t x)123b3278239SFrançois Tigeot static inline int64_t abs64(int64_t x)
124b3278239SFrançois Tigeot {
125b3278239SFrançois Tigeot 	return (x < 0 ? -x : x);
126b3278239SFrançois Tigeot }
127b3278239SFrançois Tigeot 
128477eb7f9SFrançois Tigeot #define DIV_ROUND_CLOSEST_ULL(ll, d)	\
129477eb7f9SFrançois Tigeot  ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
130477eb7f9SFrançois Tigeot 
13104b45e6fSzrj #define	upper_32_bits(n)	((u32)(((n) >> 16) >> 16))
1323dc5a57bSzrj #define	lower_32_bits(n)	((u32)(n))
1333dc5a57bSzrj 
1343dc5a57bSzrj /* Byteorder compat layer */
1353dc5a57bSzrj #if _BYTE_ORDER == _BIG_ENDIAN
1363dc5a57bSzrj #define	__BIG_ENDIAN 4321
1373dc5a57bSzrj #else
1383dc5a57bSzrj #define	__LITTLE_ENDIAN 1234
1393dc5a57bSzrj #endif
1403dc5a57bSzrj 
1413dc5a57bSzrj #define	cpu_to_le16(x)	htole16(x)
1423dc5a57bSzrj #define	le16_to_cpu(x)	le16toh(x)
1433dc5a57bSzrj #define	cpu_to_le32(x)	htole32(x)
1443dc5a57bSzrj #define	le32_to_cpu(x)	le32toh(x)
1453dc5a57bSzrj #define	le32_to_cpup(x)	le32toh(*x)
1463dc5a57bSzrj 
1473dc5a57bSzrj #define	cpu_to_be16(x)	htobe16(x)
1483dc5a57bSzrj #define	be16_to_cpu(x)	be16toh(x)
1493dc5a57bSzrj #define	cpu_to_be32(x)	htobe32(x)
1503dc5a57bSzrj #define	be32_to_cpu(x)	be32toh(x)
1513dc5a57bSzrj #define	be32_to_cpup(x)	be32toh(*x)
1523dc5a57bSzrj 
1536a895867SFrançois Tigeot static inline int __must_check
kstrtouint(const char * s,unsigned int base,unsigned int * res)1546a895867SFrançois Tigeot kstrtouint(const char *s, unsigned int base, unsigned int *res)
1556a895867SFrançois Tigeot {
1566a895867SFrançois Tigeot 	*(res) = strtol(s,0,base);
1576a895867SFrançois Tigeot 
1586a895867SFrançois Tigeot 	return 0;
1596a895867SFrançois Tigeot }
1606a895867SFrançois Tigeot 
1611dedbd3bSFrançois Tigeot char *kvasprintf(int flags, const char *format, va_list ap);
1621dedbd3bSFrançois Tigeot char *kasprintf(int flags, const char *format, ...);
1639b497556SFrançois Tigeot 
164c1ccb710SFrançois Tigeot static inline void __user *
u64_to_user_ptr(u64 address)165c1ccb710SFrançois Tigeot u64_to_user_ptr(u64 address)
166c1ccb710SFrançois Tigeot {
167c1ccb710SFrançois Tigeot 	return (void __user *)(uintptr_t)address;
168c1ccb710SFrançois Tigeot }
169c1ccb710SFrançois Tigeot 
170be7d0317SFrançois Tigeot static inline void
might_sleep(void)171be7d0317SFrançois Tigeot might_sleep(void)
172be7d0317SFrançois Tigeot {
173be7d0317SFrançois Tigeot }
174be7d0317SFrançois Tigeot 
1752cecdd68SFrançois Tigeot #define might_sleep_if(cond)
1762cecdd68SFrançois Tigeot 
17767bcf553SFrançois Tigeot #define snprintf	ksnprintf
178a85cb24fSFrançois Tigeot #define sprintf		ksprintf
179be7d0317SFrançois Tigeot 
180ebbede87SSascha Wildner static inline int __printf(3, 0)
vsnprintf(char * buf,size_t size,const char * fmt,va_list args)1819286b91eSSascha Wildner vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18267bcf553SFrançois Tigeot {
18367bcf553SFrançois Tigeot 	return kvsnprintf(buf, size, fmt, args);
18467bcf553SFrançois Tigeot }
18567bcf553SFrançois Tigeot 
186ebbede87SSascha Wildner static inline int __printf(3, 0)
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)18767bcf553SFrançois Tigeot vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
18867bcf553SFrançois Tigeot {
18967bcf553SFrançois Tigeot 	int ret;
19067bcf553SFrançois Tigeot 
19167bcf553SFrançois Tigeot 	if (size == 0)
192be7d0317SFrançois Tigeot 		return 0;
19367bcf553SFrançois Tigeot 
19467bcf553SFrançois Tigeot 	ret = vsnprintf(buf, size, fmt, args);
19567bcf553SFrançois Tigeot 	if (ret < size)
19667bcf553SFrançois Tigeot 		return ret;
19767bcf553SFrançois Tigeot 
19867bcf553SFrançois Tigeot 	return size - 1;
199be7d0317SFrançois Tigeot }
200be7d0317SFrançois Tigeot 
201ebbede87SSascha Wildner static inline int __printf(3, 4)
scnprintf(char * buf,size_t size,const char * fmt,...)202a4e0d8b7SFrançois Tigeot scnprintf(char *buf, size_t size, const char *fmt, ...)
203a4e0d8b7SFrançois Tigeot {
204a4e0d8b7SFrançois Tigeot 	va_list args;
205a4e0d8b7SFrançois Tigeot 	int i;
206a4e0d8b7SFrançois Tigeot 
207a4e0d8b7SFrançois Tigeot 	va_start(args, fmt);
208a4e0d8b7SFrançois Tigeot 	i = vscnprintf(buf, size, fmt, args);
209a4e0d8b7SFrançois Tigeot 	va_end(args);
210a4e0d8b7SFrançois Tigeot 
211a4e0d8b7SFrançois Tigeot 	return (i);
212a4e0d8b7SFrançois Tigeot }
213a4e0d8b7SFrançois Tigeot 
21428bc5924SFrançois Tigeot static inline int
kstrtol(const char * cp,unsigned int base,long * res)21528bc5924SFrançois Tigeot kstrtol(const char *cp, unsigned int base, long *res)
21628bc5924SFrançois Tigeot {
21728bc5924SFrançois Tigeot 	char *end;
21828bc5924SFrançois Tigeot 
21928bc5924SFrançois Tigeot 	*res = strtol(cp, &end, base);
22028bc5924SFrançois Tigeot 
22128bc5924SFrançois Tigeot 	/* skip newline character, if any */
22228bc5924SFrançois Tigeot 	if (*end == '\n')
22328bc5924SFrançois Tigeot 		end++;
22428bc5924SFrançois Tigeot 	if (*cp == 0 || *end != 0)
22528bc5924SFrançois Tigeot 		return (-EINVAL);
22628bc5924SFrançois Tigeot 	return (0);
22728bc5924SFrançois Tigeot }
22828bc5924SFrançois Tigeot 
22936c92a32SFrançois Tigeot #define oops_in_progress	(panicstr != NULL)
23036c92a32SFrançois Tigeot 
231c7707484SFrançois Tigeot enum lockdep_ok {
232c7707484SFrançois Tigeot 	LOCKDEP_STILL_OK,
233c7707484SFrançois Tigeot 	LOCKDEP_NOW_UNRELIABLE
234c7707484SFrançois Tigeot };
235c7707484SFrançois Tigeot 
236c7707484SFrançois Tigeot #define TAINT_MACHINE_CHECK	4
237c7707484SFrançois Tigeot 
238c7707484SFrançois Tigeot static inline void
add_taint(unsigned flag,enum lockdep_ok lo)239c7707484SFrançois Tigeot add_taint(unsigned flag, enum lockdep_ok lo)
240c7707484SFrançois Tigeot {
241c7707484SFrançois Tigeot }
242c7707484SFrançois Tigeot 
243*78973132SSergey Zigachev #define STUB() do { kprintf("%s: stub\n", __func__); } while(0);
244*78973132SSergey Zigachev 
245c4962bc9SFrançois Tigeot #endif	/* _LINUX_KERNEL_H_ */
246