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