xref: /dragonfly/sys/dev/drm/include/linux/compiler.h (revision da0d35cf)
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 #define ACCESS_ONCE(x)	(*(volatile typeof(x) *)&(x))
77 
78 #ifdef _KERNEL		/* This file is included by kdump(1) */
79 
80 #include <sys/param.h>
81 
82 /*
83  * The READ_ONCE() and WRITE_ONCE() macros force volatile accesses to
84  * various data types.
85  * Their complexity is caused by the necessity to work-around
86  * compiler cleverness and bugs.
87  * Some GCC versions drop the volatile modifier if the variable used
88  * is not of a scalar type.
89  */
90 static inline void
91 __volatile_read(const volatile void *x, int size, void *result)
92 {
93 	switch(size) {
94 	case 8:
95 		*(uint64_t *)result = *(const volatile uint64_t *)x;
96 		break;
97 	case 4:
98 		*(uint32_t *)result = *(const volatile uint32_t *)x;
99 		break;
100 	case 2:
101 		*(uint16_t *)result = *(const volatile uint16_t *)x;
102 		break;
103 	case 1:
104 		*(uint8_t *)result = *(const volatile uint8_t *)x;
105 		break;
106 	default:
107 		panic("__volatile_read called with size %d\n", size);
108 	}
109 }
110 
111 static inline void
112 __volatile_write(volatile void *var, int size, void *value)
113 {
114 	switch(size) {
115 	case 8:
116 		*(volatile uint64_t *)var = *(uint64_t *)value;
117 		break;
118 	case 4:
119 		*(volatile uint32_t *)var = *(uint32_t *)value;
120 		break;
121 	case 2:
122 		*(volatile uint16_t *)var = *(uint16_t *)value;
123 		break;
124 	case 1:
125 		*(volatile uint8_t *)var = *(uint8_t *)value;
126 		break;
127 	default:
128 		panic("__volatile_write called with size %d\n", size);
129 	}
130 
131 }
132 
133 #define READ_ONCE(x) ({						\
134 	union {							\
135 		__typeof(x) initial_type;			\
136 		uint8_t nc_type;				\
137 	} result;						\
138 								\
139 	result.nc_type = 0;					\
140 	__volatile_read(&(x), sizeof(x), &result.nc_type);	\
141 	result.initial_type;					\
142 })
143 
144 #define WRITE_ONCE(var, value) ({				\
145 	union {							\
146 		__typeof(var) initial_type;			\
147 		uint8_t nc_type;				\
148 	} result;						\
149 								\
150 	result.initial_type = value;				\
151 	__volatile_write(&(var), sizeof(var), &result.nc_type);	\
152 	result.initial_type;					\
153 })
154 
155 #define __rcu
156 
157 #endif	/* __KERNEL__ */
158 
159 #define GCC_VERSION	\
160 	(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
161 
162 #endif	/* _LINUX_COMPILER_H_ */
163