Replace filp in ioctl arguments with drm_file *file_priv.
[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 /** Ioctl arguments */
10 #define DRM_IOCTL_ARGS                  struct inode *inode, struct drm_file *file_priv, unsigned int cmd, unsigned long data
11 /** Current process ID */
12 #define DRM_CURRENTPID                  current->pid
13 #define DRM_SUSER(p)                    capable(CAP_SYS_ADMIN)
14 #define DRM_UDELAY(d)                   udelay(d)
15 #if LINUX_VERSION_CODE <= 0x020608      /* KERNEL_VERSION(2,6,8) */
16 #ifndef __iomem
17 #define __iomem
18 #endif
19 /** Read a byte from a MMIO region */
20 #define DRM_READ8(map, offset)          readb(((void __iomem *)(map)->handle) + (offset))
21 /** Read a word from a MMIO region */
22 #define DRM_READ16(map, offset)         readw(((void __iomem *)(map)->handle) + (offset))
23 /** Read a dword from a MMIO region */
24 #define DRM_READ32(map, offset)         readl(((void __iomem *)(map)->handle) + (offset))
25 /** Write a byte into a MMIO region */
26 #define DRM_WRITE8(map, offset, val)    writeb(val, ((void __iomem *)(map)->handle) + (offset))
27 /** Write a word into a MMIO region */
28 #define DRM_WRITE16(map, offset, val)   writew(val, ((void __iomem *)(map)->handle) + (offset))
29 /** Write a dword into a MMIO region */
30 #define DRM_WRITE32(map, offset, val)   writel(val, ((void __iomem *)(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      struct drm_device *dev  = file_priv->head->dev
53
54 /** IRQ handler arguments and return type and values */
55 #define DRM_IRQ_ARGS            int irq, void *arg
56 /** backwards compatibility with old irq return values */
57 #ifndef IRQ_HANDLED
58 typedef void irqreturn_t;
59 #define IRQ_HANDLED             /* nothing */
60 #define IRQ_NONE                /* nothing */
61 #endif
62
63 /** AGP types */
64 #if __OS_HAS_AGP
65 #define DRM_AGP_MEM             struct agp_memory
66 #define DRM_AGP_KERN            struct agp_kern_info
67 #else
68 /* define some dummy types for non AGP supporting kernels */
69 struct no_agp_kern {
70         unsigned long aper_base;
71         unsigned long aper_size;
72 };
73 #define DRM_AGP_MEM             int
74 #define DRM_AGP_KERN            struct no_agp_kern
75 #endif
76
77 #if !(__OS_HAS_MTRR)
78 static __inline__ int mtrr_add(unsigned long base, unsigned long size,
79                                unsigned int type, char increment)
80 {
81         return -ENODEV;
82 }
83
84 static __inline__ int mtrr_del(int reg, unsigned long base, unsigned long size)
85 {
86         return -ENODEV;
87 }
88
89 #define MTRR_TYPE_WRCOMB     1
90 #endif
91
92 /** For data going into the kernel through the ioctl argument */
93 #define DRM_COPY_FROM_USER_IOCTL(arg1, arg2, arg3)      \
94         if ( copy_from_user(&arg1, arg2, arg3) )        \
95                 return -EFAULT
96 /** For data going from the kernel through the ioctl argument */
97 #define DRM_COPY_TO_USER_IOCTL(arg1, arg2, arg3)        \
98         if ( copy_to_user(arg1, &arg2, arg3) )          \
99                 return -EFAULT
100 /** Other copying of data to kernel space */
101 #define DRM_COPY_FROM_USER(arg1, arg2, arg3)            \
102         copy_from_user(arg1, arg2, arg3)
103 /** Other copying of data from kernel space */
104 #define DRM_COPY_TO_USER(arg1, arg2, arg3)              \
105         copy_to_user(arg1, arg2, arg3)
106 /* Macros for copyfrom user, but checking readability only once */
107 #define DRM_VERIFYAREA_READ( uaddr, size )              \
108         (access_ok( VERIFY_READ, uaddr, size) ? 0 : -EFAULT)
109 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
110         __copy_from_user(arg1, arg2, arg3)
111 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
112         __copy_to_user(arg1, arg2, arg3)
113 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
114         __get_user(val, uaddr)
115
116 #define DRM_HZ HZ
117
118 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
119 do {                                                            \
120         DECLARE_WAITQUEUE(entry, current);                      \
121         unsigned long end = jiffies + (timeout);                \
122         add_wait_queue(&(queue), &entry);                       \
123                                                                 \
124         for (;;) {                                              \
125                 __set_current_state(TASK_INTERRUPTIBLE);        \
126                 if (condition)                                  \
127                         break;                                  \
128                 if (time_after_eq(jiffies, end)) {              \
129                         ret = -EBUSY;                           \
130                         break;                                  \
131                 }                                               \
132                 schedule_timeout((HZ/100 > 1) ? HZ/100 : 1);    \
133                 if (signal_pending(current)) {                  \
134                         ret = -EINTR;                           \
135                         break;                                  \
136                 }                                               \
137         }                                                       \
138         __set_current_state(TASK_RUNNING);                      \
139         remove_wait_queue(&(queue), &entry);                    \
140 } while (0)
141
142 #define DRM_WAKEUP( queue ) wake_up_interruptible( queue )
143 #define DRM_INIT_WAITQUEUE( queue ) init_waitqueue_head( queue )