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