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