1 /**
2 * \file drm_os_linux.h
3 * OS abstraction macros.
4 */
6 #include <linux/interrupt.h> /* For task queue support */
7 #include <linux/delay.h>
9 /** Current process ID */
10 #define DRM_CURRENTPID current->pid
11 #define DRM_SUSER(p) capable(CAP_SYS_ADMIN)
12 #define DRM_UDELAY(d) udelay(d)
13 /** Read a byte from a MMIO region */
14 #define DRM_READ8(map, offset) readb((map)->handle + (offset))
15 /** Read a word from a MMIO region */
16 #define DRM_READ16(map, offset) readw((map)->handle + (offset))
17 /** Read a dword from a MMIO region */
18 #define DRM_READ32(map, offset) readl((map)->handle + (offset))
19 /** Write a byte into a MMIO region */
20 #define DRM_WRITE8(map, offset, val) writeb(val, (map)->handle + (offset))
21 /** Write a word into a MMIO region */
22 #define DRM_WRITE16(map, offset, val) writew(val, (map)->handle + (offset))
23 /** Write a dword into a MMIO region */
24 #define DRM_WRITE32(map, offset, val) writel(val, (map)->handle + (offset))
25 /** Read memory barrier */
26 #define DRM_READMEMORYBARRIER() rmb()
27 /** Write memory barrier */
28 #define DRM_WRITEMEMORYBARRIER() wmb()
29 /** Read/write memory barrier */
30 #define DRM_MEMORYBARRIER() mb()
32 /** IRQ handler arguments and return type and values */
33 #define DRM_IRQ_ARGS int irq, void *arg
34 /** backwards compatibility with old irq return values */
35 #if !defined(IRQ_HANDLED) && LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,29)
36 typedef void irqreturn_t;
37 #define IRQ_HANDLED /* nothing */
38 #define IRQ_NONE /* nothing */
39 #endif
41 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
42 #define dev_set_name(dev, name, ...) ({ \
43 snprintf((dev)->bus_id, BUS_ID_SIZE, (name), __VA_ARGS__); \
44 0; })
45 #endif
47 /** AGP types */
48 #if __OS_HAS_AGP
49 #define DRM_AGP_MEM struct agp_memory
50 #define DRM_AGP_KERN struct agp_kern_info
51 #else
52 /* define some dummy types for non AGP supporting kernels */
53 struct no_agp_kern {
54 unsigned long aper_base;
55 unsigned long aper_size;
56 };
57 #define DRM_AGP_MEM int
58 #define DRM_AGP_KERN struct no_agp_kern
59 #endif
61 #if !(__OS_HAS_MTRR)
62 static __inline__ int mtrr_add(unsigned long base, unsigned long size,
63 unsigned int type, char increment)
64 {
65 return -ENODEV;
66 }
68 static __inline__ int mtrr_del(int reg, unsigned long base, unsigned long size)
69 {
70 return -ENODEV;
71 }
73 #define MTRR_TYPE_WRCOMB 1
74 #endif
76 /** Other copying of data to kernel space */
77 #define DRM_COPY_FROM_USER(arg1, arg2, arg3) \
78 copy_from_user(arg1, arg2, arg3)
79 /** Other copying of data from kernel space */
80 #define DRM_COPY_TO_USER(arg1, arg2, arg3) \
81 copy_to_user(arg1, arg2, arg3)
82 /* Macros for copyfrom user, but checking readability only once */
83 #define DRM_VERIFYAREA_READ( uaddr, size ) \
84 (access_ok( VERIFY_READ, uaddr, size) ? 0 : -EFAULT)
85 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) \
86 __copy_from_user(arg1, arg2, arg3)
87 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3) \
88 __copy_to_user(arg1, arg2, arg3)
89 #define DRM_GET_USER_UNCHECKED(val, uaddr) \
90 __get_user(val, uaddr)
92 #define DRM_HZ HZ
94 #define DRM_WAIT_ON( ret, queue, timeout, condition ) \
95 do { \
96 DECLARE_WAITQUEUE(entry, current); \
97 unsigned long end = jiffies + (timeout); \
98 add_wait_queue(&(queue), &entry); \
99 \
100 for (;;) { \
101 __set_current_state(TASK_INTERRUPTIBLE); \
102 if (condition) \
103 break; \
104 if (time_after_eq(jiffies, end)) { \
105 ret = -EBUSY; \
106 break; \
107 } \
108 schedule_timeout((HZ/100 > 1) ? HZ/100 : 1); \
109 if (signal_pending(current)) { \
110 ret = -EINTR; \
111 break; \
112 } \
113 } \
114 __set_current_state(TASK_RUNNING); \
115 remove_wait_queue(&(queue), &entry); \
116 } while (0)
118 #define DRM_WAKEUP( queue ) wake_up_interruptible( queue )
119 #define DRM_INIT_WAITQUEUE( queue ) init_waitqueue_head( queue )
121 /** Type for the OS's non-sleepable mutex lock */
122 #define DRM_SPINTYPE spinlock_t
123 /**
124 * Initialize the lock for use. name is an optional string describing the
125 * lock
126 */
127 #define DRM_SPININIT(l,name) spin_lock_init(l)
128 #define DRM_SPINUNINIT(l)
129 #define DRM_SPINLOCK(l) spin_lock(l)
130 #define DRM_SPINUNLOCK(l) spin_unlock(l)
131 #define DRM_SPINLOCK_IRQSAVE(l, _flags) spin_lock_irqsave(l, _flags);
132 #define DRM_SPINUNLOCK_IRQRESTORE(l, _flags) spin_unlock_irqrestore(l, _flags);
133 #define DRM_SPINLOCK_ASSERT(l) do {} while (0)
135 #define DRM_PCI_DEV struct pci_dev
136 #define drm_pci_get_bsf(b, s, f) pci_get_bus_and_slot(b, PCI_DEVFN(s, f))
137 #define drm_pci_read_config_dword pci_read_config_dword