xref: /dragonfly/sys/dev/drm/include/linux/compiler.h (revision 655933d6)
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) 2015-2020 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 
31 #ifndef	_LINUX_COMPILER_H_
32 #define	_LINUX_COMPILER_H_
33 
34 #include <sys/cdefs.h>
35 
36 #define __user
37 #define __kernel
38 #define __safe
39 #define __force
40 #define __nocast
41 #define __iomem
42 #define __chk_user_ptr(x)		0
43 #define __chk_io_ptr(x)			0
44 #define __builtin_warning(x, y...)	(1)
45 #define __acquires(x)
46 #define __releases(x)
47 #define __acquire(x)			0
48 #define __release(x)			0
49 #define __cond_lock(x,c)		(c)
50 #define	__bitwise
51 #define __devinitdata
52 #define __init
53 #define	__devinit
54 #define	__devexit
55 #define __exit
56 #define	__stringify(x)			#x
57 #define	__attribute_const__		__attribute__((__const__))
58 #undef __always_inline
59 #define	__always_inline			inline
60 #define noinline			__attribute__((noinline))
61 
62 #define	likely(x)			__builtin_expect(!!(x), 1)
63 #define	unlikely(x)			__builtin_expect(!!(x), 0)
64 #define typeof(x)			__typeof(x)
65 
66 #define __maybe_unused			__unused
67 #define __always_unused			__unused
68 #define __malloc
69 #define __must_check			__heedresult
70 
71 #define __printf(a,b)			__printflike(a,b)
72 
73 
74 #define barrier()	cpu_ccfence()
75 
76 #ifdef _KERNEL		/* This file is included by kdump(1) */
77 
78 #include <sys/param.h>
79 
80 /*
81  * The READ_ONCE() and WRITE_ONCE() macros force volatile accesses to
82  * various data types.
83  * Their complexity is caused by the necessity to work-around
84  * compiler cleverness and bugs.
85  * Some GCC versions drop the volatile modifier if the variable used
86  * is not of a scalar type.
87  */
88 static inline void
89 __volatile_read(const volatile void *x, int size, void *result)
90 {
91 	switch(size) {
92 	case 8:
93 		*(uint64_t *)result = *(const volatile uint64_t *)x;
94 		break;
95 	case 4:
96 		*(uint32_t *)result = *(const volatile uint32_t *)x;
97 		break;
98 	case 2:
99 		*(uint16_t *)result = *(const volatile uint16_t *)x;
100 		break;
101 	case 1:
102 		*(uint8_t *)result = *(const volatile uint8_t *)x;
103 		break;
104 	default:
105 		panic("__volatile_read called with size %d\n", size);
106 	}
107 }
108 
109 static inline void
110 __volatile_write(volatile void *var, int size, void *value)
111 {
112 	switch(size) {
113 	case 8:
114 		*(volatile uint64_t *)var = *(uint64_t *)value;
115 		break;
116 	case 4:
117 		*(volatile uint32_t *)var = *(uint32_t *)value;
118 		break;
119 	case 2:
120 		*(volatile uint16_t *)var = *(uint16_t *)value;
121 		break;
122 	case 1:
123 		*(volatile uint8_t *)var = *(uint8_t *)value;
124 		break;
125 	default:
126 		panic("__volatile_write called with size %d\n", size);
127 	}
128 
129 }
130 
131 #define READ_ONCE(x) ({						\
132 	union {							\
133 		__typeof(x) initial_type;			\
134 		uint8_t nc_type;				\
135 	} result;						\
136 								\
137 	result.nc_type = 0;					\
138 	__volatile_read(&(x), sizeof(x), &result.nc_type);	\
139 	result.initial_type;					\
140 })
141 
142 #define WRITE_ONCE(var, value) ({				\
143 	union {							\
144 		__typeof(var) initial_type;			\
145 		uint8_t nc_type;				\
146 	} result;						\
147 								\
148 	result.initial_type = value;				\
149 	__volatile_write(&(var), sizeof(var), &result.nc_type);	\
150 	result.initial_type;					\
151 })
152 
153 #define __rcu
154 
155 /* Workaround to protect from the 'DEBUG' kernel config option */
156 #undef DEBUG
157 
158 #endif	/* __KERNEL__ */
159 
160 #define GCC_VERSION	\
161 	(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
162 
163 #ifndef unreachable
164 #define unreachable()			\
165 do {					\
166 	__asm __volatile("");		\
167 	__builtin_unreachable();	\
168 } while (0)
169 #endif
170 
171 #endif	/* _LINUX_COMPILER_H_ */
172