xref: /gfx-drm/usr/src/uts/common/drm/drm_linux.h (revision c9e5ece4)
1 /*
2  * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /*
25  * Copyright (c) 2012, 2012 Intel Corporation.  All rights reserved.
26  */
27 
28 #ifndef __DRM_LINUX_H__
29 #define __DRM_LINUX_H__
30 
31 #include <sys/types.h>
32 #include <sys/byteorder.h>
33 #include "drm_atomic.h"
34 
35 #define DRM_MEM_CACHED	0
36 #define DRM_MEM_UNCACHED 	1
37 #define DRM_MEM_WC 		2
38 
39 #ifndef min
40 #define min(a, b) (((a) < (b)) ? (a) : (b))
41 #endif
42 
43 #ifndef max
44 #define max(a, b) (((a) > (b)) ? (a) : (b))
45 #endif
46 
47 #define clamp_int64_t(val)			\
48 	val = min((int64_t)INT_MAX, val);	\
49 	val = max((int64_t)INT_MIN, val);
50 
51 #define ioremap_wc(base,size) drm_sun_ioremap((base), (size), DRM_MEM_WC)
52 #define ioremap(base, size)   drm_sun_ioremap((base), (size), DRM_MEM_UNCACHED)
53 #define iounmap(addr)         drm_sun_iounmap((addr))
54 
55 #define spinlock_t                       kmutex_t
56 #define	spin_lock_init(l)                mutex_init((l), NULL, MUTEX_DRIVER, NULL);
57 #define	spin_lock(l)	                 mutex_enter(l)
58 #define	spin_unlock(u)                   mutex_exit(u)
59 #define	spin_lock_irq(l)		mutex_enter(l)
60 #define	spin_unlock_irq(u)		mutex_exit(u)
61 #ifdef __lint
62 /*
63  * The following is to keep lint happy when it encouters the use of 'flag'.
64  * On Linux, this allows a local variable to be used to retain context,
65  * but is unused on Solaris.  Rather than trying to place LINTED
66  * directives in the source, we actually consue the flag for lint here.
67  */
68 #define	spin_lock_irqsave(l, flag)       flag = 0; mutex_enter(l)
69 #define	spin_unlock_irqrestore(u, flag)  flag &= flag; mutex_exit(u)
70 #else
71 #define	spin_lock_irqsave(l, flag)       mutex_enter(l)
72 #define	spin_unlock_irqrestore(u, flag)  mutex_exit(u)
73 #endif
74 
75 #define mutex_lock(l)			mutex_enter(l)
76 #define mutex_unlock(u)			mutex_exit(u)
77 #define mutex_is_locked(l)		mutex_owned(l)
78 
79 #define assert_spin_locked(l)		ASSERT(MUTEX_HELD(l))
80 
81 #define kmalloc           kmem_alloc
82 #define kzalloc           kmem_zalloc
83 #define kcalloc(x, y, z)  kzalloc((x)*(y), z)
84 #define kfree             kmem_free
85 
86 #define do_gettimeofday   (void) uniqtime
87 #define msleep_interruptible(s)  DRM_UDELAY(s)
88 #define	timeval_to_ns(tvp)	TICK_TO_NSEC(TIMEVAL_TO_TICK(tvp))
89 #define	ns_to_timeval(nsec, tvp)	TICK_TO_TIMEVAL(NSEC_TO_TICK(nsec), tvp)
90 
91 #define GFP_KERNEL KM_SLEEP
92 #define GFP_ATOMIC KM_SLEEP
93 
94 #define	KHZ2PICOS(a)		(1000000000UL/(a))
95 
96 #define udelay			drv_usecwait
97 #define mdelay(x)		udelay((x) * 1000)
98 #define msleep(x)		mdelay((x))
99 #define msecs_to_jiffies(x)	drv_usectohz((x) * 1000)
100 #define jiffies_to_msecs(x)	drv_hztousec(x) / 1000
101 #define time_after(a,b)	((long)(b) - (long)(a) < 0)
102 #define	time_after_eq(a,b)	((long)(a) - (long)(b) >= 0)
103 #define time_before_eq(a,b)	time_after_eq(b,a)
104 #define time_in_range(a,b,c) \
105 	(time_after_eq(a,b) && \
106 	 time_before_eq(a,c))
107 
108 #define	jiffies	ddi_get_lbolt()
109 
110 #ifdef _BIG_ENDIAN
111 #define cpu_to_le16(x) LE_16(x)
112 #define le16_to_cpu(x) LE_16(x)
113 #else
114 #define cpu_to_le16(x) (x)
115 #define le16_to_cpu(x) (x)
116 #endif
117 
118 #define swap(a, b) \
119 	do { int tmp = (a); (a) = (b); (b) = tmp; } while (__lintzero)
120 
121 #define abs(x) ((x < 0) ? -x : x)
122 
123 #define div_u64(x, y) ((unsigned long long)(x))/((unsigned long long)(y))  /* XXX FIXME */
124 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
125 
126 #define put_user(val,ptr) DRM_COPY_TO_USER(ptr,(&val),sizeof(val))
127 #define get_user(x,ptr) DRM_COPY_FROM_USER((&x),ptr,sizeof(x))
128 #define copy_to_user DRM_COPY_TO_USER
129 #define copy_from_user DRM_COPY_FROM_USER
130 #define unlikely(a)  (a)
131 
132 #if 0 /* See sys/agpgart.h */
133 #define AGP_USER_TYPES (1 << 16)
134 #define AGP_USER_MEMORY (AGP_USER_TYPES)
135 #define AGP_USER_CACHED_MEMORY (AGP_USER_TYPES + 1)
136 #endif
137 
138 #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
139 
140 #define page_to_phys(x)	*(uint32_t *)(uintptr_t)(x)
141 #define in_dbg_master()	0
142 
143 #define BITS_PER_BYTE		8
144 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
145 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
146 #define	POS_DIV_ROUND_CLOSEST(x, d)	((x + (d / 2)) / d)
147 #define POS_DIV_ROUND_UP_ULL(x, d)	DIV_ROUND_UP(x,d)
148 
149 typedef unsigned long dma_addr_t;
150 typedef uint64_t	u64;
151 typedef int64_t		s64;
152 typedef uint32_t	u32;
153 typedef int32_t		s32;
154 typedef uint16_t	u16;
155 typedef uint8_t		u8;
156 typedef uint_t		irqreturn_t;
157 
158 typedef int		bool;
159 
160 #define true		(1)
161 #define false		(0)
162 
163 #define __init
164 #define __exit
165 #define __iomem
166 
167 #ifdef _ILP32
168 typedef u32 resource_size_t;
169 #else /* _LP64 */
170 typedef u64 resource_size_t;
171 #endif
172 
173 typedef struct kref {
174 	atomic_t refcount;
175 } kref_t;
176 
177 extern void kref_init(struct kref *kref);
178 extern void kref_get(struct kref *kref);
179 extern void kref_put(struct kref *kref, void (*release)(struct kref *kref));
180 
181 extern unsigned int hweight16(unsigned int w);
182 
183 extern long IS_ERR(const void *ptr);
184 #define	IS_ERR_OR_NULL(ptr)	(!ptr || IS_ERR(ptr))
185 
186 #ifdef __lint
187 /*
188  * The actual code for _wait_for() causes Solaris lint2 to fail, though
189  * by all appearances, the code actually works (may try and peek at
190  * the compiled code to understand why).  So to get around the problem,
191  * we create a special lint version for _wait_for().
192  */
193 #define _wait_for(COND, MS, W) (! (COND))
194 #else /* !__lint */
195 #define _wait_for(COND, MS, W) ({ \
196 	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);	\
197 	int ret__ = 0;							\
198 	while (! (COND)) {						\
199 		if (time_after(jiffies, timeout__)) {			\
200 			ret__ = -ETIMEDOUT;				\
201 			break;						\
202 		}							\
203 		if (W) udelay(W);					\
204 	}								\
205 	ret__;								\
206 })
207 #endif /* __lint */
208 
209 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
210 #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
211 
212 #endif /* __DRM_LINUX_H__ */
213