xref: /dflybsd-src/sys/dev/drm/include/linux/kernel.h (revision f01491b5c83693b9c1b8fd41379cfe6daf6ba69f)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2014 François Tigeot
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef	_LINUX_KERNEL_H_
30 #define	_LINUX_KERNEL_H_
31 
32 #include <sys/systm.h>
33 #include <sys/param.h>
34 #include <sys/libkern.h>
35 #include <sys/stat.h>
36 
37 #include <linux/bitops.h>
38 #include <linux/compiler.h>
39 #include <linux/log2.h>
40 #include <linux/types.h>
41 
42 #define KERN_CONT       ""
43 #define	KERN_EMERG	"<0>"
44 #define	KERN_ALERT	"<1>"
45 #define	KERN_CRIT	"<2>"
46 #define	KERN_ERR	"<3>"
47 #define	KERN_WARNING	"<4>"
48 #define	KERN_NOTICE	"<5>"
49 #define	KERN_INFO	"<6>"
50 #define	KERN_DEBUG	"<7>"
51 
52 #define BUG()	do				\
53 {						\
54 	panic("BUG in %s at %s:%u",		\
55 		__func__, __FILE__, __LINE__);	\
56 } while (0)
57 
58 #define BUG_ON(condition)	do { if (condition) BUG(); } while(0)
59 
60 #define _WARN_STR(x) #x
61 
62 #define WARN_ON(condition) ({						\
63 	int __ret = !!(condition);					\
64 	if (__ret)							\
65 		kprintf("WARNING %s failed at %s:%d\n",			\
66 		    _WARN_STR(condition), __FILE__, __LINE__);			\
67 	unlikely(__ret);						\
68 })
69 
70 #define WARN_ON_ONCE(condition) ({					\
71 	static int __warned;						\
72 	int __ret = !!(condition);					\
73 	if (__ret && !__warned) {					\
74 		kprintf("WARNING %s failed at %s:%d\n",			\
75 		    _WARN_STR(condition), __FILE__, __LINE__);		\
76 		__warned = 1;						\
77 	}								\
78 	unlikely(__ret);						\
79 })
80 
81 #undef	ALIGN
82 #define	ALIGN(x, y)		roundup2((x), (y))
83 #define	DIV_ROUND_UP		howmany
84 
85 #define	printk(X...)		kprintf(X)
86 #define	pr_debug(fmt, ...)	printk(KERN_DEBUG # fmt, ##__VA_ARGS__)
87 #define udelay(t)       	DELAY(t)
88 
89 #ifndef pr_fmt
90 #define pr_fmt(fmt) fmt
91 #endif
92 
93 /*
94  * Print a one-time message (analogous to WARN_ONCE() et al):
95  */
96 #define printk_once(x...) ({                    \
97         static bool __print_once;               \
98                                                 \
99         if (!__print_once) {                    \
100                 __print_once = true;            \
101                 printk(x);                      \
102         }                                       \
103 })
104 
105 
106 
107 #define pr_emerg(fmt, ...) \
108         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
109 #define pr_alert(fmt, ...) \
110         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
111 #define pr_crit(fmt, ...) \
112         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
113 #define pr_err(fmt, ...) \
114         kprintf(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
115 #define pr_warning(fmt, ...) \
116         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
117 #define pr_warn pr_warning
118 #define pr_notice(fmt, ...) \
119         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
120 #define pr_info(fmt, ...) \
121         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
122 #define pr_cont(fmt, ...) \
123         printk(KERN_CONT fmt, ##__VA_ARGS__)
124 
125 /* pr_devel() should produce zero code unless DEBUG is defined */
126 #ifdef DEBUG
127 #define pr_devel(fmt, ...) \
128         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
129 #else
130 #define pr_devel(fmt, ...) \
131         ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
132 #endif
133 
134 #ifndef WARN
135 #define WARN(condition, format...) ({                                   \
136         int __ret_warn_on = !!(condition);                              \
137         if (unlikely(__ret_warn_on))                                    \
138                 pr_warning(format);                                     \
139         unlikely(__ret_warn_on);                                        \
140 })
141 #endif
142 
143 #define WARN_ONCE(condition, format...)	({	\
144 	static bool __warned_once;		\
145 						\
146 	if ((condition) && !__warned_once) {	\
147 		WARN(condition, format);	\
148 		__warned_once = true;		\
149 	}					\
150 })
151 
152 #define container_of(ptr, type, member)				\
153 ({								\
154 	__typeof(((type *)0)->member) *_p = (ptr);		\
155 	(type *)((char *)_p - offsetof(type, member));		\
156 })
157 
158 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
159 
160 #define	simple_strtoul	strtoul
161 #define	simple_strtol	strtol
162 
163 #define min(x, y)	(x < y ? x : y)
164 #define max(x, y)	(x > y ? x : y)
165 #define min_t(type, _x, _y)	(type)(_x) < (type)(_y) ? (type)(_x) : (_y)
166 #define max_t(type, _x, _y)	(type)(_x) > (type)(_y) ? (type)(_x) : (_y)
167 
168 /*
169  * This looks more complex than it should be. But we need to
170  * get the type for the ~ right in round_down (it needs to be
171  * as wide as the result!), and we want to evaluate the macro
172  * arguments just once each.
173  */
174 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
175 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
176 #define round_down(x, y) ((x) & ~__round_mask(x, y))
177 
178 #define	num_possible_cpus()	mp_ncpus
179 
180 typedef struct pm_message {
181         int event;
182 } pm_message_t;
183 
184 /* Swap values of a and b */
185 #define swap(a, b)			\
186 ({					\
187 	typeof(a) _swap_tmp = a;	\
188 	a = b;				\
189 	b = _swap_tmp;			\
190 })
191 
192 #define DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) /2)) / (divisor))
193 
194 #endif	/* _LINUX_KERNEL_H_ */
195