xref: /dflybsd-src/sys/dev/drm/include/drm/drm_os_linux.h (revision 10cf3bfcde2ee9c50d77a153397b93d8026b03e1)
1 /**
2  * \file drm_os_freebsd.h
3  * OS abstraction macros.
4  *
5  * $FreeBSD: head/sys/dev/drm2/drm_os_freebsd.h 254858 2013-08-25 14:27:14Z dumbbell $
6  */
7 
8 #define DRM_READ8(map, offset)						\
9 	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
10 	    (vm_offset_t)(offset))
11 #define DRM_READ16(map, offset)						\
12 	le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +	\
13 	    (vm_offset_t)(offset)))
14 #define DRM_READ32(map, offset)						\
15 	le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +	\
16 	    (vm_offset_t)(offset)))
17 #define DRM_READ64(map, offset)						\
18 	le64toh(*(volatile u_int64_t *)(((vm_offset_t)(map)->handle) +	\
19 	    (vm_offset_t)(offset)))
20 #define DRM_WRITE8(map, offset, val)					\
21 	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
22 	    (vm_offset_t)(offset)) = val
23 #define DRM_WRITE16(map, offset, val)					\
24 	*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +		\
25 	    (vm_offset_t)(offset)) = htole16(val)
26 
27 #define DRM_WRITE32(map, offset, val)					\
28 	*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +		\
29 	    (vm_offset_t)(offset)) = htole32(val)
30 
31 #define DRM_WRITE64(map, offset, val)					\
32 	*(volatile u_int64_t *)(((vm_offset_t)(map)->handle) +		\
33 	    (vm_offset_t)(offset)) = htole64(val)
34 
35 
36 #if _BYTE_ORDER == _BIG_ENDIAN
37 #define	__BIG_ENDIAN 4321
38 #else
39 #define	__LITTLE_ENDIAN 1234
40 #endif
41 
42 #ifdef __LP64__
43 #define	BITS_PER_LONG	64
44 #else
45 #define	BITS_PER_LONG	32
46 #endif
47 
48 #define	cpu_to_le16(x)	htole16(x)
49 #define	le16_to_cpu(x)	le16toh(x)
50 #define	cpu_to_le32(x)	htole32(x)
51 #define	le32_to_cpu(x)	le32toh(x)
52 
53 #define	cpu_to_be16(x)	htobe16(x)
54 #define	be16_to_cpu(x)	be16toh(x)
55 #define	cpu_to_be32(x)	htobe32(x)
56 #define	be32_to_cpu(x)	be32toh(x)
57 #define	be32_to_cpup(x)	be32toh(*x)
58 
59 #define	unlikely(x)            __builtin_expect(!!(x), 0)
60 #define	likely(x)              __builtin_expect(!!(x), 1)
61 
62 #define DRM_HZ			hz
63 #define DRM_UDELAY(udelay)	DELAY(udelay)
64 #define DRM_MDELAY(msecs)	do { int loops = (msecs);		\
65 				  while (loops--) DELAY(1000);		\
66 				} while (0)
67 #define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
68 
69 #define	lower_32_bits(n)	((u32)(n))
70 
71 #define	memset_io(a, b, c)	memset((a), (b), (c))
72 #define	memcpy_fromio(a, b, c)	memcpy((a), (b), (c))
73 #define	memcpy_toio(a, b, c)	memcpy((a), (b), (c))
74 
75 /* XXXKIB what is the right code for the FreeBSD ? */
76 /* kib@ used ENXIO here -- dumbbell@ */
77 #define	EREMOTEIO	EIO
78 
79 #define	KTR_DRM		KTR_DEV
80 #define	KTR_DRM_REG	KTR_SPARE3
81 
82 #define	hweight32(i)	bitcount32(i)
83 
84 /**
85  * ror32 - rotate a 32-bit value right
86  * @word: value to rotate
87  * @shift: bits to roll
88  *
89  * Source: include/linux/bitops.h
90  */
91 static inline uint32_t ror32(uint32_t word, unsigned int shift)
92 {
93 	return (word >> shift) | (word << (32 - shift));
94 }
95 
96 #define	IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
97 #define	get_unaligned(ptr)                                              \
98 	({ __typeof__(*(ptr)) __tmp;                                    \
99 	  memcpy(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })
100 
101 #if _BYTE_ORDER == _LITTLE_ENDIAN
102 /* Taken from linux/include/linux/unaligned/le_struct.h. */
103 struct __una_u32 { u32 x; } __packed;
104 
105 static inline u32 __get_unaligned_cpu32(const void *p)
106 {
107 	const struct __una_u32 *ptr = (const struct __una_u32 *)p;
108 	return ptr->x;
109 }
110 
111 static inline u32 get_unaligned_le32(const void *p)
112 {
113 	return __get_unaligned_cpu32((const u8 *)p);
114 }
115 #else
116 /* Taken from linux/include/linux/unaligned/le_byteshift.h. */
117 static inline u32 __get_unaligned_le32(const u8 *p)
118 {
119 	return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
120 }
121 
122 static inline u32 get_unaligned_le32(const void *p)
123 {
124 	return __get_unaligned_le32((const u8 *)p);
125 }
126 #endif
127 
128 #define KIB_NOTYET()							\
129 do {									\
130 	if (drm_debug && drm_notyet_flag)				\
131 		kprintf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
132 } while (0)
133