xref: /freebsd/sys/dev/drm2/drm_os_freebsd.h (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /**
2  * \file drm_os_freebsd.h
3  * OS abstraction macros.
4  */
5 
6 #include <sys/cdefs.h>
7 __FBSDID("$FreeBSD$");
8 
9 #ifndef _DRM_OS_FREEBSD_H_
10 #define	_DRM_OS_FREEBSD_H_
11 
12 #include <sys/fbio.h>
13 
14 #if _BYTE_ORDER == _BIG_ENDIAN
15 #define	__BIG_ENDIAN 4321
16 #else
17 #define	__LITTLE_ENDIAN 1234
18 #endif
19 
20 #ifdef __LP64__
21 #define	BITS_PER_LONG	64
22 #else
23 #define	BITS_PER_LONG	32
24 #endif
25 
26 #ifndef __user
27 #define __user
28 #endif
29 #ifndef __iomem
30 #define __iomem
31 #endif
32 
33 #define	cpu_to_le16(x)	htole16(x)
34 #define	le16_to_cpu(x)	le16toh(x)
35 #define	cpu_to_le32(x)	htole32(x)
36 #define	le32_to_cpu(x)	le32toh(x)
37 
38 #define	cpu_to_be16(x)	htobe16(x)
39 #define	be16_to_cpu(x)	be16toh(x)
40 #define	cpu_to_be32(x)	htobe32(x)
41 #define	be32_to_cpu(x)	be32toh(x)
42 #define	be32_to_cpup(x)	be32toh(*x)
43 
44 typedef vm_paddr_t dma_addr_t;
45 typedef vm_paddr_t resource_size_t;
46 #define	wait_queue_head_t atomic_t
47 
48 typedef uint64_t u64;
49 typedef uint32_t u32;
50 typedef uint16_t u16;
51 typedef uint8_t  u8;
52 typedef int64_t s64;
53 typedef int32_t s32;
54 typedef int16_t s16;
55 typedef int8_t  s8;
56 typedef uint16_t __le16;
57 typedef uint32_t __le32;
58 typedef uint64_t __le64;
59 typedef uint16_t __be16;
60 typedef uint32_t __be32;
61 typedef uint64_t __be64;
62 
63 #define	DRM_IRQ_ARGS		void *arg
64 typedef void			irqreturn_t;
65 #define	IRQ_HANDLED		/* nothing */
66 #define	IRQ_NONE		/* nothing */
67 
68 #define	__init
69 #define	__exit
70 #define	__read_mostly
71 
72 #define	WARN_ON(cond)		KASSERT(!(cond), ("WARN ON: " #cond))
73 #define	WARN_ON_SMP(cond)	WARN_ON(cond)
74 #define	BUG_ON(cond)		KASSERT(!(cond), ("BUG ON: " #cond))
75 #define	unlikely(x)            __builtin_expect(!!(x), 0)
76 #define	likely(x)              __builtin_expect(!!(x), 1)
77 #define	container_of(ptr, type, member) ({			\
78 	__typeof( ((type *)0)->member ) *__mptr = (ptr);	\
79 	(type *)( (char *)__mptr - offsetof(type,member) );})
80 
81 #define	KHZ2PICOS(a)	(1000000000UL/(a))
82 
83 #define ARRAY_SIZE(x)		(sizeof(x)/sizeof(x[0]))
84 
85 #define	HZ			hz
86 #define	DRM_HZ			hz
87 #define	DRM_CURRENTPID		curthread->td_proc->p_pid
88 #define	DRM_SUSER(p)		(priv_check(p, PRIV_DRIVER) == 0)
89 #define	udelay(usecs)		DELAY(usecs)
90 #define	mdelay(msecs)		do { int loops = (msecs);		\
91 				  while (loops--) DELAY(1000);		\
92 				} while (0)
93 #define	DRM_UDELAY(udelay)	DELAY(udelay)
94 #define	drm_msleep(x, msg)	pause((msg), ((int64_t)(x)) * hz / 1000)
95 #define	DRM_MSLEEP(msecs)	drm_msleep((msecs), "drm_msleep")
96 
97 #define	DRM_READ8(map, offset)						\
98 	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
99 	    (vm_offset_t)(offset))
100 #define	DRM_READ16(map, offset)						\
101 	le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +	\
102 	    (vm_offset_t)(offset)))
103 #define	DRM_READ32(map, offset)						\
104 	le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +	\
105 	    (vm_offset_t)(offset)))
106 #define	DRM_READ64(map, offset)						\
107 	le64toh(*(volatile u_int64_t *)(((vm_offset_t)(map)->handle) +	\
108 	    (vm_offset_t)(offset)))
109 #define	DRM_WRITE8(map, offset, val)					\
110 	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
111 	    (vm_offset_t)(offset)) = val
112 #define	DRM_WRITE16(map, offset, val)					\
113 	*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +		\
114 	    (vm_offset_t)(offset)) = htole16(val)
115 #define	DRM_WRITE32(map, offset, val)					\
116 	*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +		\
117 	    (vm_offset_t)(offset)) = htole32(val)
118 #define	DRM_WRITE64(map, offset, val)					\
119 	*(volatile u_int64_t *)(((vm_offset_t)(map)->handle) +		\
120 	    (vm_offset_t)(offset)) = htole64(val)
121 
122 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
123  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
124  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
125  */
126 #define	DRM_READMEMORYBARRIER()		rmb()
127 #define	DRM_WRITEMEMORYBARRIER()	wmb()
128 #define	DRM_MEMORYBARRIER()		mb()
129 #define	smp_rmb()			rmb()
130 #define	smp_mb__before_atomic_inc()	mb()
131 #define	smp_mb__after_atomic_inc()	mb()
132 
133 #define	do_div(a, b)		((a) /= (b))
134 #define	div64_u64(a, b)		((a) / (b))
135 #define	lower_32_bits(n)	((u32)(n))
136 
137 #define min_t(type, x, y) ({			\
138 	type __min1 = (x);			\
139 	type __min2 = (y);			\
140 	__min1 < __min2 ? __min1 : __min2; })
141 
142 #define max_t(type, x, y) ({			\
143 	type __max1 = (x);			\
144 	type __max2 = (y);			\
145 	__max1 > __max2 ? __max1 : __max2; })
146 
147 #define	memset_io(a, b, c)	memset((a), (b), (c))
148 #define	memcpy_fromio(a, b, c)	memcpy((a), (b), (c))
149 #define	memcpy_toio(a, b, c)	memcpy((a), (b), (c))
150 
151 /* XXXKIB what is the right code for the FreeBSD ? */
152 /* kib@ used ENXIO here -- dumbbell@ */
153 #define	EREMOTEIO	EIO
154 #define	ERESTARTSYS	512 /* Same value as Linux. */
155 
156 #define	KTR_DRM		KTR_DEV
157 #define	KTR_DRM_REG	KTR_SPARE3
158 
159 #define	DRM_AGP_KERN	struct agp_info
160 #define	DRM_AGP_MEM	void
161 
162 #define	PCI_VENDOR_ID_APPLE		0x106b
163 #define	PCI_VENDOR_ID_ASUSTEK		0x1043
164 #define	PCI_VENDOR_ID_ATI		0x1002
165 #define	PCI_VENDOR_ID_DELL		0x1028
166 #define	PCI_VENDOR_ID_HP		0x103c
167 #define	PCI_VENDOR_ID_IBM		0x1014
168 #define	PCI_VENDOR_ID_INTEL		0x8086
169 #define	PCI_VENDOR_ID_SERVERWORKS	0x1166
170 #define	PCI_VENDOR_ID_SONY		0x104d
171 #define	PCI_VENDOR_ID_VIA		0x1106
172 
173 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
174 #define	hweight32(i)	bitcount32(i)
175 
176 static inline unsigned long
177 roundup_pow_of_two(unsigned long x)
178 {
179 
180 	return (1UL << flsl(x - 1));
181 }
182 
183 /**
184  * ror32 - rotate a 32-bit value right
185  * @word: value to rotate
186  * @shift: bits to roll
187  *
188  * Source: include/linux/bitops.h
189  */
190 static inline uint32_t
191 ror32(uint32_t word, unsigned int shift)
192 {
193 
194 	return (word >> shift) | (word << (32 - shift));
195 }
196 
197 #define	IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
198 #define	get_unaligned(ptr)                                              \
199 	({ __typeof__(*(ptr)) __tmp;                                    \
200 	  memcpy(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })
201 
202 #if _BYTE_ORDER == _LITTLE_ENDIAN
203 /* Taken from linux/include/linux/unaligned/le_struct.h. */
204 struct __una_u32 { u32 x; } __packed;
205 
206 static inline u32
207 __get_unaligned_cpu32(const void *p)
208 {
209 	const struct __una_u32 *ptr = (const struct __una_u32 *)p;
210 
211 	return (ptr->x);
212 }
213 
214 static inline u32
215 get_unaligned_le32(const void *p)
216 {
217 
218 	return (__get_unaligned_cpu32((const u8 *)p));
219 }
220 #else
221 /* Taken from linux/include/linux/unaligned/le_byteshift.h. */
222 static inline u32
223 __get_unaligned_le32(const u8 *p)
224 {
225 
226 	return (p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24);
227 }
228 
229 static inline u32
230 get_unaligned_le32(const void *p)
231 {
232 
233 	return (__get_unaligned_le32((const u8 *)p));
234 }
235 #endif
236 
237 static inline unsigned long
238 ilog2(unsigned long x)
239 {
240 
241 	return (flsl(x) - 1);
242 }
243 
244 static inline int64_t
245 abs64(int64_t x)
246 {
247 
248 	return (x < 0 ? -x : x);
249 }
250 
251 int64_t		timeval_to_ns(const struct timeval *tv);
252 struct timeval	ns_to_timeval(const int64_t nsec);
253 
254 #define PAGE_ALIGN(addr) round_page(addr)
255 
256 #define	drm_get_device_from_kdev(_kdev)	(((struct drm_minor *)(_kdev)->si_drv1)->dev)
257 
258 #define DRM_IOC_VOID		IOC_VOID
259 #define DRM_IOC_READ		IOC_OUT
260 #define DRM_IOC_WRITE		IOC_IN
261 #define DRM_IOC_READWRITE	IOC_INOUT
262 #define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
263 
264 static inline long
265 __copy_to_user(void __user *to, const void *from, unsigned long n)
266 {
267 	return (copyout(from, to, n) != 0 ? n : 0);
268 }
269 #define	copy_to_user(to, from, n) __copy_to_user((to), (from), (n))
270 
271 static inline int
272 __put_user(size_t size, void *ptr, void *x)
273 {
274 
275 	size = copy_to_user(ptr, x, size);
276 
277 	return (size ? -EFAULT : size);
278 }
279 #define	put_user(x, ptr) __put_user(sizeof(*ptr), (ptr), &(x))
280 
281 static inline unsigned long
282 __copy_from_user(void *to, const void __user *from, unsigned long n)
283 {
284 	return ((copyin(__DECONST(void *, from), to, n) != 0 ? n : 0));
285 }
286 #define	copy_from_user(to, from, n) __copy_from_user((to), (from), (n))
287 
288 static inline int
289 __get_user(size_t size, const void *ptr, void *x)
290 {
291 
292 	size = copy_from_user(x, ptr, size);
293 
294 	return (size ? -EFAULT : size);
295 }
296 #define	get_user(x, ptr) __get_user(sizeof(*ptr), (ptr), &(x))
297 
298 #define	sigemptyset(set)	SIGEMPTYSET(set)
299 #define	sigaddset(set, sig)	SIGADDSET(set, sig)
300 
301 #define DRM_LOCK(dev)		sx_xlock(&(dev)->dev_struct_lock)
302 #define DRM_UNLOCK(dev) 	sx_xunlock(&(dev)->dev_struct_lock)
303 
304 #define jiffies			ticks
305 #define	jiffies_to_msecs(x)	(((int64_t)(x)) * 1000 / hz)
306 #define	msecs_to_jiffies(x)	(((int64_t)(x)) * hz / 1000)
307 #define	time_after(a,b)		((long)(b) - (long)(a) < 0)
308 #define	time_after_eq(a,b)	((long)(b) - (long)(a) <= 0)
309 
310 #define	wake_up(queue)			wakeup((void *)queue)
311 #define	wake_up_interruptible(queue)	wakeup((void *)queue)
312 
313 MALLOC_DECLARE(DRM_MEM_DMA);
314 MALLOC_DECLARE(DRM_MEM_SAREA);
315 MALLOC_DECLARE(DRM_MEM_DRIVER);
316 MALLOC_DECLARE(DRM_MEM_MAGIC);
317 MALLOC_DECLARE(DRM_MEM_MINOR);
318 MALLOC_DECLARE(DRM_MEM_IOCTLS);
319 MALLOC_DECLARE(DRM_MEM_MAPS);
320 MALLOC_DECLARE(DRM_MEM_BUFS);
321 MALLOC_DECLARE(DRM_MEM_SEGS);
322 MALLOC_DECLARE(DRM_MEM_PAGES);
323 MALLOC_DECLARE(DRM_MEM_FILES);
324 MALLOC_DECLARE(DRM_MEM_QUEUES);
325 MALLOC_DECLARE(DRM_MEM_CMDS);
326 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
327 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
328 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
329 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
330 MALLOC_DECLARE(DRM_MEM_SGLISTS);
331 MALLOC_DECLARE(DRM_MEM_MM);
332 MALLOC_DECLARE(DRM_MEM_HASHTAB);
333 MALLOC_DECLARE(DRM_MEM_KMS);
334 MALLOC_DECLARE(DRM_MEM_VBLANK);
335 
336 #define	simple_strtol(a, b, c)			strtol((a), (b), (c))
337 
338 typedef struct drm_pci_id_list
339 {
340 	int vendor;
341 	int device;
342 	long driver_private;
343 	char *name;
344 } drm_pci_id_list_t;
345 
346 #ifdef __i386__
347 #define	CONFIG_X86	1
348 #endif
349 #ifdef __amd64__
350 #define	CONFIG_X86	1
351 #define	CONFIG_X86_64	1
352 #endif
353 #ifdef __ia64__
354 #define	CONFIG_IA64	1
355 #endif
356 
357 #if defined(__i386__) || defined(__amd64__)
358 #define	CONFIG_ACPI
359 #endif
360 
361 #define	CONFIG_AGP	1
362 #define	CONFIG_MTRR	1
363 
364 #define	CONFIG_FB	1
365 extern const char *fb_mode_option;
366 
367 #define	EXPORT_SYMBOL(x)
368 #define	MODULE_AUTHOR(author)
369 #define	MODULE_DESCRIPTION(desc)
370 #define	MODULE_LICENSE(license)
371 #define	MODULE_PARM_DESC(name, desc)
372 #define	module_param_named(name, var, type, perm)
373 
374 #define	printk		printf
375 #define	KERN_DEBUG	""
376 
377 struct fb_info *	framebuffer_alloc(void);
378 void			framebuffer_release(struct fb_info *info);
379 
380 #define KIB_NOTYET()							\
381 do {									\
382 	if (drm_debug && drm_notyet)					\
383 		printf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
384 } while (0)
385 
386 #endif /* _DRM_OS_FREEBSD_H_ */
387