drm-memory patch, cleans up alloc/free and makes calloc look more libc like
[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 LINUX_VERSION_CODE < KERNEL_VERSION(2,5,70)
51 #define DRM_AGP_MEM             agp_memory
52 #define DRM_AGP_KERN            agp_kern_info
53 #else
54 #define DRM_AGP_MEM             struct agp_memory
55 #define DRM_AGP_KERN            struct agp_kern_info
56 #endif
57
58 /** Task queue handler arguments */
59 #define DRM_TASKQUEUE_ARGS      void *arg
60
61 /** For data going into the kernel through the ioctl argument */
62 #define DRM_COPY_FROM_USER_IOCTL(arg1, arg2, arg3)      \
63         if ( copy_from_user(&arg1, arg2, arg3) )        \
64                 return -EFAULT
65 /** For data going from the kernel through the ioctl argument */
66 #define DRM_COPY_TO_USER_IOCTL(arg1, arg2, arg3)        \
67         if ( copy_to_user(arg1, &arg2, arg3) )          \
68                 return -EFAULT
69 /** Other copying of data to kernel space */
70 #define DRM_COPY_FROM_USER(arg1, arg2, arg3)            \
71         copy_from_user(arg1, arg2, arg3)
72 /** Other copying of data from kernel space */
73 #define DRM_COPY_TO_USER(arg1, arg2, arg3)              \
74         copy_to_user(arg1, arg2, arg3)
75 /* Macros for copyfrom user, but checking readability only once */
76 #define DRM_VERIFYAREA_READ( uaddr, size )              \
77         verify_area( VERIFY_READ, uaddr, size )
78 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
79         __copy_from_user(arg1, arg2, arg3)
80 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
81         __copy_to_user(arg1, arg2, arg3)
82 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
83         __get_user(val, uaddr)
84 #define DRM_PUT_USER_UNCHECKED(uaddr, val)              \
85         __put_user(val, uaddr)
86
87
88 #define DRM_GET_PRIV_WITH_RETURN(_priv, _filp) _priv = _filp->private_data
89
90 /** 
91  * Get the pointer to the SAREA.
92  *
93  * Searches the SAREA on the mapping lists and points drm_device::sarea to it.
94  */
95 #define DRM_GETSAREA()                                                   \
96 do {                                                                     \
97         drm_map_list_t *entry;                                           \
98         list_for_each_entry( entry, &dev->maplist->head, head ) {        \
99                 if ( entry->map &&                                       \
100                      entry->map->type == _DRM_SHM &&                     \
101                      (entry->map->flags & _DRM_CONTAINS_LOCK) ) {        \
102                         dev_priv->sarea = entry->map;                    \
103                         break;                                           \
104                 }                                                        \
105         }                                                                \
106 } while (0)
107
108 #define DRM_HZ HZ
109
110 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
111 do {                                                            \
112         DECLARE_WAITQUEUE(entry, current);                      \
113         unsigned long end = jiffies + (timeout);                \
114         add_wait_queue(&(queue), &entry);                       \
115                                                                 \
116         for (;;) {                                              \
117                 current->state = TASK_INTERRUPTIBLE;            \
118                 if (condition)                                  \
119                         break;                                  \
120                 if (time_after_eq(jiffies, end)) {              \
121                         ret = -EBUSY;                           \
122                         break;                                  \
123                 }                                               \
124                 schedule_timeout((HZ/100 > 1) ? HZ/100 : 1);    \
125                 if (signal_pending(current)) {                  \
126                         ret = -EINTR;                           \
127                         break;                                  \
128                 }                                               \
129         }                                                       \
130         current->state = TASK_RUNNING;                          \
131         remove_wait_queue(&(queue), &entry);                    \
132 } while (0)
133
134
135 #define DRM_WAKEUP( queue ) wake_up_interruptible( queue )
136 #define DRM_INIT_WAITQUEUE( queue ) init_waitqueue_head( queue )
137