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