xref: /openbsd-src/sys/dev/pci/drm/include/linux/kernel.h (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /* Public domain. */
2 
3 #ifndef _LINUX_KERNEL_H
4 #define _LINUX_KERNEL_H
5 
6 #include <sys/stdint.h>
7 #include <sys/param.h>
8 #include <sys/systm.h>
9 #include <sys/stdarg.h>
10 #include <sys/malloc.h>
11 
12 #include <linux/types.h>
13 #include <linux/compiler.h>
14 #include <linux/bitops.h>
15 #include <linux/log2.h>
16 #include <linux/linkage.h>
17 #include <linux/printk.h>
18 #include <linux/typecheck.h>
19 #include <linux/container_of.h>
20 #include <asm/byteorder.h>
21 
22 #define swap(a, b) \
23 	do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0)
24 
25 #define offsetofend(s, e) (offsetof(s, e) + sizeof((((s *)0)->e)))
26 
27 #define S8_MAX		INT8_MAX
28 #define S16_MAX		INT16_MAX
29 #define S32_MAX		INT32_MAX
30 #define S64_MAX		INT64_MAX
31 
32 #define U8_MAX		UINT8_MAX
33 #define U16_MAX		UINT16_MAX
34 #define U32_MAX		UINT32_MAX
35 #define U64_C(x)	UINT64_C(x)
36 #define U64_MAX		UINT64_MAX
37 
38 #define ARRAY_SIZE nitems
39 
40 #define lower_32_bits(n)	((u32)(n))
41 #define upper_32_bits(_val)	((u32)(((_val) >> 16) >> 16))
42 
43 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg)
44 
45 #define min_t(t, a, b) ({ \
46 	t __min_a = (a); \
47 	t __min_b = (b); \
48 	__min_a < __min_b ? __min_a : __min_b; })
49 
50 #define max_t(t, a, b) ({ \
51 	t __max_a = (a); \
52 	t __max_b = (b); \
53 	__max_a > __max_b ? __max_a : __max_b; })
54 
55 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b)
56 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b)
57 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b)
58 
59 #define min(a, b) MIN(a, b)
60 #define max(a, b) MAX(a, b)
61 #define min3(x, y, z) MIN(x, MIN(y, z))
62 #define max3(x, y, z) MAX(x, MAX(y, z))
63 
64 #define mult_frac(x, n, d) (((x) * (n)) / (d))
65 
66 #define roundup2(x, y) (((x) + ((y) - 1)) & (~((__typeof(x))(y) - 1)))
67 #define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
68 #define round_down(x, y) (((x) / (y)) * (y)) /* y is power of two */
69 #define rounddown(x, y) (((x) / (y)) * (y)) /* arbitrary y */
70 #define DIV_ROUND_UP(x, y)	(((x) + ((y) - 1)) / (y))
71 #define DIV_ROUND_UP_ULL(x, y)	DIV_ROUND_UP(x, y)
72 #define DIV_ROUND_DOWN(x, y)	((x) / (y))
73 #define DIV_ROUND_DOWN_ULL(x, y)	DIV_ROUND_DOWN(x, y)
74 #define DIV_ROUND_CLOSEST(x, y)	(((x) + ((y) / 2)) / (y))
75 #define DIV_ROUND_CLOSEST_ULL(x, y)	DIV_ROUND_CLOSEST(x, y)
76 
77 #define IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
78 #define PTR_ALIGN(x, y)		((__typeof(x))roundup2((unsigned long)(x), (y)))
79 
80 static inline char *
81 kvasprintf(int flags, const char *fmt, va_list ap)
82 {
83 	char *buf;
84 	size_t len;
85 	va_list vl;
86 
87 	va_copy(vl, ap);
88 	len = vsnprintf(NULL, 0, fmt, vl);
89 	va_end(vl);
90 
91 	buf = malloc(len + 1, M_DRM, flags);
92 	if (buf) {
93 		vsnprintf(buf, len + 1, fmt, ap);
94 	}
95 
96 	return buf;
97 }
98 
99 static inline char *
100 kasprintf(int flags, const char *fmt, ...)
101 {
102 	char *buf;
103 	va_list ap;
104 
105 	va_start(ap, fmt);
106 	buf = kvasprintf(flags, fmt, ap);
107 	va_end(ap);
108 
109 	return buf;
110 }
111 
112 static inline int
113 vscnprintf(char *buf, size_t size, const char *fmt, va_list ap)
114 {
115 	int nc;
116 
117 	nc = vsnprintf(buf, size, fmt, ap);
118 	if (nc > (size - 1))
119 		return (size - 1);
120 	else
121 		return nc;
122 }
123 
124 static inline int
125 _in_dbg_master(void)
126 {
127 #ifdef DDB
128 	return (db_active);
129 #endif
130 	return (0);
131 }
132 
133 #define oops_in_progress _in_dbg_master()
134 
135 #define might_sleep()		assertwaitok()
136 #define might_sleep_if(x)	do {	\
137 	if (x)				\
138 		assertwaitok();		\
139 } while (0)
140 
141 #define add_taint(x, y)
142 #define TAINT_MACHINE_CHECK	0
143 #define TAINT_WARN		1
144 #define LOCKDEP_STILL_OK	0
145 
146 #define u64_to_user_ptr(x)	((void *)(uintptr_t)(x))
147 
148 #define _RET_IP_		__builtin_return_address(0)
149 
150 #define STUB() do { printf("%s: stub\n", __func__); } while(0)
151 
152 #endif
153