1/**
2 * \file drm_os_freebsd.h
3 * OS abstraction macros.
4 */
5
6#include <sys/cdefs.h>
7__FBSDID("$FreeBSD$");
8
9#if _BYTE_ORDER == _BIG_ENDIAN
10#define	__BIG_ENDIAN 4321
11#else
12#define	__LITTLE_ENDIAN 1234
13#endif
14
15#define	cpu_to_le16(x)	htole16(x)
16#define	le16_to_cpu(x)	le16toh(x)
17#define	cpu_to_le32(x)	htole32(x)
18#define	le32_to_cpu(x)	le32toh(x)
19
20#define	cpu_to_be16(x)	htobe16(x)
21#define	be16_to_cpu(x)	be16toh(x)
22#define	cpu_to_be32(x)	htobe32(x)
23#define	be32_to_cpu(x)	be32toh(x)
24#define	be32_to_cpup(x)	be32toh(*x)
25
26typedef vm_paddr_t dma_addr_t;
27typedef uint64_t u64;
28typedef uint32_t u32;
29typedef uint16_t u16;
30typedef uint8_t u8;
31typedef int64_t s64;
32typedef int32_t s32;
33typedef int16_t s16;
34typedef int8_t s8;
35typedef int32_t __be32;
36
37#define	unlikely(x)            __builtin_expect(!!(x), 0)
38#define	likely(x)              __builtin_expect(!!(x), 1)
39#define	container_of(ptr, type, member) ({			\
40	__typeof( ((type *)0)->member ) *__mptr = (ptr);	\
41	(type *)( (char *)__mptr - offsetof(type,member) );})
42
43#define DRM_HZ			hz
44#define DRM_UDELAY(udelay)	DELAY(udelay)
45#define DRM_MDELAY(msecs)	do { int loops = (msecs);		\
46				  while (loops--) DELAY(1000);		\
47				} while (0)
48#define DRM_MSLEEP(msecs)	drm_msleep((msecs), "drm_msleep")
49#define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
50
51#define	do_div(a, b)		((a) /= (b))
52#define	lower_32_bits(n)	((u32)(n))
53
54#define min_t(type, x, y) ({			\
55	type __min1 = (x);			\
56	type __min2 = (y);			\
57	__min1 < __min2 ? __min1 : __min2; })
58
59#define max_t(type, x, y) ({			\
60	type __max1 = (x);			\
61	type __max2 = (y);			\
62	__max1 > __max2 ? __max1 : __max2; })
63
64#define	memset_io(a, b, c)	memset((a), (b), (c))
65#define	memcpy_fromio(a, b, c)	memcpy((a), (b), (c))
66#define	memcpy_toio(a, b, c)	memcpy((a), (b), (c))
67
68/* XXXKIB what is the right code for the FreeBSD ? */
69/* kib@ used ENXIO here -- dumbbell@ */
70#define	EREMOTEIO	EIO
71#define	ERESTARTSYS	ERESTART
72
73#define	KTR_DRM		KTR_DEV
74#define	KTR_DRM_REG	KTR_SPARE3
75
76#define	PCI_VENDOR_ID_APPLE		0x106b
77#define	PCI_VENDOR_ID_ASUSTEK		0x1043
78#define	PCI_VENDOR_ID_ATI		0x1002
79#define	PCI_VENDOR_ID_DELL		0x1028
80#define	PCI_VENDOR_ID_HP		0x103c
81#define	PCI_VENDOR_ID_IBM		0x1014
82#define	PCI_VENDOR_ID_INTEL		0x8086
83#define	PCI_VENDOR_ID_SERVERWORKS	0x1166
84#define	PCI_VENDOR_ID_SONY		0x104d
85#define	PCI_VENDOR_ID_VIA		0x1106
86
87#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
88#define	hweight32(i)	bitcount32(i)
89
90static inline unsigned long
91roundup_pow_of_two(unsigned long x)
92{
93	return (1UL << flsl(x - 1));
94}
95
96/**
97 * ror32 - rotate a 32-bit value right
98 * @word: value to rotate
99 * @shift: bits to roll
100 *
101 * Source: include/linux/bitops.h
102 */
103static inline uint32_t ror32(uint32_t word, unsigned int shift)
104{
105	return (word >> shift) | (word << (32 - shift));
106}
107
108#define	IS_ALIGNED(x, y)	(((x) & ((y) - 1)) == 0)
109#define	get_unaligned(ptr)                                              \
110	({ __typeof__(*(ptr)) __tmp;                                    \
111	  memcpy(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })
112
113#if _BYTE_ORDER == _LITTLE_ENDIAN
114/* Taken from linux/include/linux/unaligned/le_struct.h. */
115struct __una_u32 { u32 x; } __packed;
116
117static inline u32 __get_unaligned_cpu32(const void *p)
118{
119	const struct __una_u32 *ptr = (const struct __una_u32 *)p;
120	return ptr->x;
121}
122
123static inline u32 get_unaligned_le32(const void *p)
124{
125	return __get_unaligned_cpu32((const u8 *)p);
126}
127#else
128/* Taken from linux/include/linux/unaligned/le_byteshift.h. */
129static inline u32 __get_unaligned_le32(const u8 *p)
130{
131	return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
132}
133
134static inline u32 get_unaligned_le32(const void *p)
135{
136	return __get_unaligned_le32((const u8 *)p);
137}
138#endif
139
140#define KIB_NOTYET()							\
141do {									\
142	if (drm_debug_flag && drm_notyet_flag)				\
143		printf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
144} while (0)
145