Port the VIA DRM to FreeBSD. Original patch by Jake, with some cleanup by
[platform/upstream/libdrm.git] / linux-core / drm_os_linux.h
1 /**
2  * \file drm_os_linux.h
3  * OS abstraction macros.
4  */
5
6 #include <linux/interrupt.h>    /* For task queue support */
7 #include <linux/delay.h>
8
9 /** File pointer type */
10 #define DRMFILE                         struct file *
11 /** Ioctl arguments */
12 #define DRM_IOCTL_ARGS                  struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data
13 #define DRM_ERR(d)                      -(d)
14 /** Current process ID */
15 #define DRM_CURRENTPID                  current->pid
16 #define DRM_SUSER(p)                    capable(CAP_SYS_ADMIN)
17 #define DRM_UDELAY(d)                   udelay(d)
18 #if LINUX_VERSION_CODE <= 0x020608      /* KERNEL_VERSION(2,6,8) */
19 /** Read a byte from a MMIO region */
20 #define DRM_READ8(map, offset)          readb(((unsigned long)(map)->handle) + (offset))
21 /** Read a word from a MMIO region */
22 #define DRM_READ16(map, offset)         readw(((unsigned long)(map)->handle) + (offset))
23 /** Read a dword from a MMIO region */
24 #define DRM_READ32(map, offset)         readl(((unsigned long)(map)->handle) + (offset))
25 /** Write a byte into a MMIO region */
26 #define DRM_WRITE8(map, offset, val)    writeb(val, ((unsigned long)(map)->handle) + (offset))
27 /** Write a word into a MMIO region */
28 #define DRM_WRITE16(map, offset, val)   writew(val, ((unsigned long)(map)->handle) + (offset))
29 /** Write a dword into a MMIO region */
30 #define DRM_WRITE32(map, offset, val)   writel(val, ((unsigned long)(map)->handle) + (offset))
31 #else
32 /** Read a byte from a MMIO region */
33 #define DRM_READ8(map, offset)          readb((map)->handle + (offset))
34 /** Read a word from a MMIO region */
35 #define DRM_READ16(map, offset)         readw((map)->handle + (offset))
36 /** Read a dword from a MMIO region */
37 #define DRM_READ32(map, offset)         readl((map)->handle + (offset))
38 /** Write a byte into a MMIO region */
39 #define DRM_WRITE8(map, offset, val)    writeb(val, (map)->handle + (offset))
40 /** Write a word into a MMIO region */
41 #define DRM_WRITE16(map, offset, val)   writew(val, (map)->handle + (offset))
42 /** Write a dword into a MMIO region */
43 #define DRM_WRITE32(map, offset, val)   writel(val, (map)->handle + (offset))
44 #endif
45 /** Read memory barrier */
46 #define DRM_READMEMORYBARRIER()         rmb()
47 /** Write memory barrier */
48 #define DRM_WRITEMEMORYBARRIER()        wmb()
49 /** Read/write memory barrier */
50 #define DRM_MEMORYBARRIER()             mb()
51 /** DRM device local declaration */
52 #define DRM_DEVICE      drm_file_t      *priv   = filp->private_data; \
53                         drm_device_t    *dev    = priv->head->dev
54
55 /** IRQ handler arguments and return type and values */
56 #define DRM_IRQ_ARGS            int irq, void *arg, struct pt_regs *regs
57 /** backwards compatibility with old irq return values */
58 #ifndef IRQ_HANDLED
59 typedef void irqreturn_t;
60 #define IRQ_HANDLED             /* nothing */
61 #define IRQ_NONE                /* nothing */
62 #endif
63
64 /** AGP types */
65 #if __OS_HAS_AGP
66 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,70)
67 #define DRM_AGP_MEM             agp_memory
68 #define DRM_AGP_KERN            agp_kern_info
69 #else
70 #define DRM_AGP_MEM             struct agp_memory
71 #define DRM_AGP_KERN            struct agp_kern_info
72 #endif
73 #else
74 /* define some dummy types for non AGP supporting kernels */
75 struct no_agp_kern {
76         unsigned long aper_base;
77         unsigned long aper_size;
78 };
79 #define DRM_AGP_MEM             int
80 #define DRM_AGP_KERN            struct no_agp_kern
81 #endif
82
83 #if !(__OS_HAS_MTRR)
84 static __inline__ int mtrr_add(unsigned long base, unsigned long size,
85                                unsigned int type, char increment)
86 {
87         return -ENODEV;
88 }
89
90 static __inline__ int mtrr_del(int reg, unsigned long base, unsigned long size)
91 {
92         return -ENODEV;
93 }
94
95 #define MTRR_TYPE_WRCOMB     1
96 #endif
97
98 /** Task queue handler arguments */
99 #define DRM_TASKQUEUE_ARGS      void *arg
100
101 /** For data going into the kernel through the ioctl argument */
102 #define DRM_COPY_FROM_USER_IOCTL(arg1, arg2, arg3)      \
103         if ( copy_from_user(&arg1, arg2, arg3) )        \
104                 return -EFAULT
105 /** For data going from the kernel through the ioctl argument */
106 #define DRM_COPY_TO_USER_IOCTL(arg1, arg2, arg3)        \
107         if ( copy_to_user(arg1, &arg2, arg3) )          \
108                 return -EFAULT
109 /** Other copying of data to kernel space */
110 #define DRM_COPY_FROM_USER(arg1, arg2, arg3)            \
111         copy_from_user(arg1, arg2, arg3)
112 /** Other copying of data from kernel space */
113 #define DRM_COPY_TO_USER(arg1, arg2, arg3)              \
114         copy_to_user(arg1, arg2, arg3)
115 /* Macros for copyfrom user, but checking readability only once */
116 #define DRM_VERIFYAREA_READ( uaddr, size )              \
117         (access_ok( VERIFY_READ, uaddr, size) ? 0 : -EFAULT)
118 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
119         __copy_from_user(arg1, arg2, arg3)
120 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
121         __copy_to_user(arg1, arg2, arg3)
122 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
123         __get_user(val, uaddr)
124
125 #define DRM_GET_PRIV_WITH_RETURN(_priv, _filp) _priv = _filp->private_data
126
127 /**
128  * Get the pointer to the SAREA.
129  *
130  * Searches the SAREA on the mapping lists and points drm_device::sarea to it.
131  */
132 #define DRM_GETSAREA()                                                   \
133 do {                                                                     \
134         drm_map_list_t *entry;                                           \
135         list_for_each_entry( entry, &dev->maplist->head, head ) {        \
136                 if ( entry->map &&                                       \
137                      entry->map->type == _DRM_SHM &&                     \
138                      (entry->map->flags & _DRM_CONTAINS_LOCK) ) {        \
139                         dev_priv->sarea = entry->map;                    \
140                         break;                                           \
141                 }                                                        \
142         }                                                                \
143 } while (0)
144
145 #define DRM_HZ HZ
146
147 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
148 do {                                                            \
149         DECLARE_WAITQUEUE(entry, current);                      \
150         unsigned long end = jiffies + (timeout);                \
151         add_wait_queue(&(queue), &entry);                       \
152                                                                 \
153         for (;;) {                                              \
154                 __set_current_state(TASK_INTERRUPTIBLE);        \
155                 if (condition)                                  \
156                         break;                                  \
157                 if (time_after_eq(jiffies, end)) {              \
158                         ret = -EBUSY;                           \
159                         break;                                  \
160                 }                                               \
161                 schedule_timeout((HZ/100 > 1) ? HZ/100 : 1);    \
162                 if (signal_pending(current)) {                  \
163                         ret = -EINTR;                           \
164                         break;                                  \
165                 }                                               \
166         }                                                       \
167         __set_current_state(TASK_RUNNING);                      \
168         remove_wait_queue(&(queue), &entry);                    \
169 } while (0)
170
171 #define DRM_WAKEUP( queue ) wake_up_interruptible( queue )
172 #define DRM_INIT_WAITQUEUE( queue ) init_waitqueue_head( queue )