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