A few changes for recent redhat.
[platform/upstream/libdrm.git] / linux-core / drm_os_linux.h
1 /**
2  * \file drm_os_linux.h
3  * OS abstraction macros.
4  */
5
6 #define __NO_VERSION__
7
8 #include <linux/interrupt.h>    /* For task queue support */
9 #include <linux/delay.h>
10
11 /** File pointer type */
12 #define DRMFILE                         struct file *
13 /** Ioctl arguments */
14 #define DRM_IOCTL_ARGS                  struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data
15 #define DRM_ERR(d)                      -(d)
16 /** Current process ID */
17 #define DRM_CURRENTPID                  current->pid
18 #define DRM_UDELAY(d)                   udelay(d)
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 /** Read memory barrier */
32 #define DRM_READMEMORYBARRIER()         rmb()
33 /** Write memory barrier */
34 #define DRM_WRITEMEMORYBARRIER()        wmb()
35 /** Read/write memory barrier */
36 #define DRM_MEMORYBARRIER()             mb()
37 /** DRM device local declaration */
38 #define DRM_DEVICE      drm_file_t      *priv   = filp->private_data; \
39                         drm_device_t    *dev    = priv->dev
40
41 /** IRQ handler arguments and return type and values */
42 #define DRM_IRQ_ARGS            int irq, void *arg, struct pt_regs *regs
43 /** backwards compatibility with old irq return values */
44 #ifndef IRQ_HANDLED
45 typedef void irqreturn_t;
46 #define IRQ_HANDLED   /* nothing */
47 #define IRQ_NONE      /* nothing */
48 #endif
49
50 /** AGP types */
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
59 /** Task queue handler arguments */
60 #define DRM_TASKQUEUE_ARGS      void *arg
61
62 /** For data going into the kernel through the ioctl argument */
63 #define DRM_COPY_FROM_USER_IOCTL(arg1, arg2, arg3)      \
64         if ( copy_from_user(&arg1, arg2, arg3) )        \
65                 return -EFAULT
66 /** For data going from the kernel through the ioctl argument */
67 #define DRM_COPY_TO_USER_IOCTL(arg1, arg2, arg3)        \
68         if ( copy_to_user(arg1, &arg2, arg3) )          \
69                 return -EFAULT
70 /** Other copying of data to kernel space */
71 #define DRM_COPY_FROM_USER(arg1, arg2, arg3)            \
72         copy_from_user(arg1, arg2, arg3)
73 /** Other copying of data from kernel space */
74 #define DRM_COPY_TO_USER(arg1, arg2, arg3)              \
75         copy_to_user(arg1, arg2, arg3)
76 /* Macros for copyfrom user, but checking readability only once */
77 #define DRM_VERIFYAREA_READ( uaddr, size )              \
78         verify_area( VERIFY_READ, uaddr, size )
79 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
80         __copy_from_user(arg1, arg2, arg3)
81 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
82         __copy_to_user(arg1, arg2, arg3)
83 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
84         __get_user(val, uaddr)
85 #define DRM_PUT_USER_UNCHECKED(uaddr, val)              \
86         __put_user(val, uaddr)
87
88
89 /** 'malloc' without the overhead of DRM(alloc)() */
90 #define DRM_MALLOC(x) kmalloc(x, GFP_KERNEL)
91 /** 'free' without the overhead of DRM(free)() */
92 #define DRM_FREE(x,size) kfree(x)
93
94 #define DRM_GET_PRIV_WITH_RETURN(_priv, _filp) _priv = _filp->private_data
95
96 /** 
97  * Get the pointer to the SAREA.
98  *
99  * Searches the SAREA on the mapping lists and points drm_device::sarea to it.
100  */
101 #define DRM_GETSAREA()                                                   \
102 do {                                                                     \
103         drm_map_list_t *entry;                                           \
104         list_for_each_entry( entry, &dev->maplist->head, head ) {        \
105                 if ( entry->map &&                                       \
106                      entry->map->type == _DRM_SHM &&                     \
107                      (entry->map->flags & _DRM_CONTAINS_LOCK) ) {        \
108                         dev_priv->sarea = entry->map;                    \
109                         break;                                           \
110                 }                                                        \
111         }                                                                \
112 } while (0)
113
114 #define DRM_HZ HZ
115
116 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
117 do {                                                            \
118         DECLARE_WAITQUEUE(entry, current);                      \
119         unsigned long end = jiffies + (timeout);                \
120         add_wait_queue(&(queue), &entry);                       \
121                                                                 \
122         for (;;) {                                              \
123                 current->state = TASK_INTERRUPTIBLE;            \
124                 if (condition)                                  \
125                         break;                                  \
126                 if (time_after_eq(jiffies, end)) {              \
127                         ret = -EBUSY;                           \
128                         break;                                  \
129                 }                                               \
130                 schedule_timeout((HZ/100 > 1) ? HZ/100 : 1);    \
131                 if (signal_pending(current)) {                  \
132                         ret = -EINTR;                           \
133                         break;                                  \
134                 }                                               \
135         }                                                       \
136         current->state = TASK_RUNNING;                          \
137         remove_wait_queue(&(queue), &entry);                    \
138 } while (0)
139
140
141 #define DRM_WAKEUP( queue ) wake_up_interruptible( queue )
142 #define DRM_INIT_WAITQUEUE( queue ) init_waitqueue_head( queue )
143