]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_os_freebsd.h
This commit was generated by cvs2svn to compensate for changes in r97049,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_os_freebsd.h
1 /*
2  * $FreeBSD$
3  */
4
5 #include <sys/param.h>
6 #include <sys/queue.h>
7 #include <sys/lockmgr.h>
8 #include <sys/malloc.h>
9 #include <sys/kernel.h>
10 #include <sys/module.h>
11 #include <sys/systm.h>
12 #include <sys/conf.h>
13 #include <sys/stat.h>
14 #include <sys/proc.h>
15 #include <sys/lock.h>
16 #include <sys/fcntl.h>
17 #include <sys/uio.h>
18 #include <sys/filio.h>
19 #include <sys/sysctl.h>
20 #include <sys/select.h>
21 #include <vm/vm.h>
22 #include <vm/pmap.h>
23 #include <machine/pmap.h>
24 #if __FreeBSD_version >= 500000
25 #include <sys/selinfo.h>
26 #endif
27 #include <sys/bus.h>
28 #if __FreeBSD_version >= 400005
29 #include <sys/taskqueue.h>
30 #endif
31 #if __FreeBSD_version >= 500000
32 #include <sys/mutex.h>
33 #endif
34
35 #if __FreeBSD_version >= 400006
36 #define __REALLY_HAVE_AGP       __HAVE_AGP
37 #endif
38
39 #define __REALLY_HAVE_MTRR      0
40 #define __REALLY_HAVE_SG        0
41
42 #if __REALLY_HAVE_AGP
43 #include <pci/agpvar.h>
44 #endif
45
46 /* Allow setting of debug code enabling from kernel config file */
47 #include <opt_drm.h>
48 #if DRM_DEBUG
49 #undef  DRM_DEBUG_CODE
50 #define DRM_DEBUG_CODE 2
51 #endif
52 #undef DRM_DEBUG
53
54 #define DRM_TIME_SLICE        (hz/20)  /* Time slice for GLXContexts      */
55
56 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
57 #define DRM_DEV_UID     0
58 #define DRM_DEV_GID     0
59
60 #if __FreeBSD_version >= 500000
61 #define DRM_OS_SPINTYPE         struct mtx
62 #define DRM_OS_SPININIT(l,name) mtx_init(&l, name, NULL, MTX_DEF)
63 #define DRM_OS_SPINLOCK(l)      mtx_lock(l)
64 #define DRM_OS_SPINUNLOCK(u)    mtx_unlock(u);
65 #define DRM_OS_CURPROC          curthread
66 #define DRM_OS_STRUCTPROC       struct thread
67 #define DRM_OS_CURRENTPID       curthread->td_proc->p_pid
68 #else
69 #define DRM_OS_CURPROC          curproc
70 #define DRM_OS_STRUCTPROC       struct proc
71 #define DRM_OS_SPINTYPE         struct simplelock
72 #define DRM_OS_SPININIT(l,name) simple_lock_init(&l)
73 #define DRM_OS_SPINLOCK(l)      simple_lock(l)
74 #define DRM_OS_SPINUNLOCK(u)    simple_unlock(u);
75 #define DRM_OS_CURRENTPID       curproc->p_pid
76 #endif
77
78 #define DRM_OS_IOCTL            dev_t kdev, u_long cmd, caddr_t data, int flags, DRM_OS_STRUCTPROC *p
79 #define IOCTL_ARGS_PASS         kdev, cmd, data, flags, p
80 #define DRM_OS_LOCK             lockmgr(&dev->dev_lock, LK_EXCLUSIVE, 0, DRM_OS_CURPROC)
81 #define DRM_OS_UNLOCK           lockmgr(&dev->dev_lock, LK_RELEASE, 0, DRM_OS_CURPROC)
82 #define DRM_OS_CHECKSUSER       suser(p)
83 #define DRM_OS_TASKQUEUE_ARGS   void *dev, int pending
84 #define DRM_OS_IRQ_ARGS         void *device
85 #define DRM_OS_DEVICE           drm_device_t    *dev    = kdev->si_drv1
86 #define DRM_OS_MALLOC(size)     malloc( size, DRM(M_DRM), M_NOWAIT )
87 #define DRM_OS_FREE(pt)         free( pt, DRM(M_DRM) )
88 #define DRM_OS_VTOPHYS(addr)    vtophys(addr)
89
90 #define DRM_OS_PRIV                                     \
91         drm_file_t      *priv   = (drm_file_t *) DRM(find_file_by_proc)(dev, p); \
92         if (!priv) {                                            \
93                 DRM_DEBUG("can't find authenticator\n");        \
94                 return EINVAL;                                  \
95         }
96
97 #define DRM_OS_DELAY( udelay )                                  \
98 do {                                                            \
99         struct timeval tv1, tv2;                                \
100         microtime(&tv1);                                        \
101         do {                                                    \
102                 microtime(&tv2);                                \
103         }                                                       \
104         while (((tv2.tv_sec-tv1.tv_sec)*1000000 + tv2.tv_usec - tv1.tv_usec) < udelay ); \
105 } while (0)
106
107 #define DRM_OS_ERR(v)   v
108
109
110 #define DRM_OS_KRNTOUSR(arg1, arg2, arg3) \
111         *arg1 = arg2
112 #define DRM_OS_KRNFROMUSR(arg1, arg2, arg3) \
113         arg1 = *arg2
114 #define DRM_OS_COPYTOUSR(arg1, arg2, arg3) \
115         copyout(arg2, arg1, arg3)
116 #define DRM_OS_COPYFROMUSR(arg1, arg2, arg3) \
117         copyin(arg2, arg1, arg3)
118
119 #define DRM_OS_READMEMORYBARRIER \
120 {                                                                                               \
121         int xchangeDummy;                                                                       \
122         DRM_DEBUG("%s\n", __FUNCTION__);                                                        \
123         __asm__ volatile(" push %%eax ; xchg %%eax, %0 ; pop %%eax" : : "m" (xchangeDummy));    \
124         __asm__ volatile(" push %%eax ; push %%ebx ; push %%ecx ; push %%edx ;"                 \
125                          " movl $0,%%eax ; cpuid ; pop %%edx ; pop %%ecx ; pop %%ebx ;"         \
126                          " pop %%eax" : /* no outputs */ :  /* no inputs */ );                  \
127 } while (0);
128
129 #define DRM_OS_WRITEMEMORYBARRIER DRM_OS_READMEMORYBARRIER
130
131 #define DRM_OS_WAKEUP(w) wakeup(w)
132 #define DRM_OS_WAKEUP_INT(w) wakeup(w)
133
134 #define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)
135
136 #define malloctype DRM(M_DRM)
137 /* The macros confliced in the MALLOC_DEFINE */
138 MALLOC_DECLARE(malloctype);
139 #undef malloctype
140
141 typedef struct drm_chipinfo
142 {
143         int vendor;
144         int device;
145         int supported;
146         char *name;
147 } drm_chipinfo_t;
148
149 typedef unsigned long atomic_t;
150 typedef u_int32_t cycles_t;
151 typedef u_int32_t spinlock_t;
152 typedef u_int32_t u32;
153 typedef u_int16_t u16;
154 typedef u_int8_t u8;
155 #define atomic_set(p, v)        (*(p) = (v))
156 #define atomic_read(p)          (*(p))
157 #define atomic_inc(p)           atomic_add_long(p, 1)
158 #define atomic_dec(p)           atomic_subtract_long(p, 1)
159 #define atomic_add(n, p)        atomic_add_long(p, n)
160 #define atomic_sub(n, p)        atomic_subtract_long(p, n)
161
162 /* Fake this */
163 static __inline unsigned int
164 test_and_set_bit(int b, volatile unsigned long *p)
165 {
166         int s = splhigh();
167         unsigned int m = 1<<b;
168         unsigned int r = *p & m;
169         *p |= m;
170         splx(s);
171         return r;
172 }
173
174 static __inline void
175 clear_bit(int b, volatile unsigned long *p)
176 {
177     atomic_clear_long(p + (b >> 5), 1 << (b & 0x1f));
178 }
179
180 static __inline void
181 set_bit(int b, volatile unsigned long *p)
182 {
183     atomic_set_long(p + (b >> 5), 1 << (b & 0x1f));
184 }
185
186 static __inline int
187 test_bit(int b, volatile unsigned long *p)
188 {
189     return p[b >> 5] & (1 << (b & 0x1f));
190 }
191
192 static __inline int
193 find_first_zero_bit(volatile unsigned long *p, int max)
194 {
195     int b;
196
197     for (b = 0; b < max; b += 32) {
198         if (p[b >> 5] != ~0) {
199             for (;;) {
200                 if ((p[b >> 5] & (1 << (b & 0x1f))) == 0)
201                     return b;
202                 b++;
203             }
204         }
205     }
206     return max;
207 }
208
209 #define spldrm()                spltty()
210
211 #define memset(p, v, s)         bzero(p, s)
212
213 /*
214  * Fake out the module macros for versions of FreeBSD where they don't
215  * exist.
216  */
217 #if (__FreeBSD_version < 500002 && __FreeBSD_version > 500000) || __FreeBSD_version < 420000
218 /* FIXME: again, what's the exact date? */
219 #define MODULE_VERSION(a,b)             struct __hack
220 #define MODULE_DEPEND(a,b,c,d,e)        struct __hack
221
222 #endif
223
224 #define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock)
225 #define _DRM_CAS(lock,old,new,__ret)                                   \
226         do {                                                           \
227                 int __dummy;    /* Can't mark eax as clobbered */      \
228                 __asm__ __volatile__(                                  \
229                         "lock ; cmpxchg %4,%1\n\t"                     \
230                         "setnz %0"                                     \
231                         : "=d" (__ret),                                \
232                           "=m" (__drm_dummy_lock(lock)),               \
233                           "=a" (__dummy)                               \
234                         : "2" (old),                                   \
235                           "r" (new));                                  \
236         } while (0)
237
238 /* Redefinitions to make templating easy */
239 #define wait_queue_head_t       long
240 #define agp_memory              void
241 #define jiffies                 ticks
242
243                                 /* Macros to make printf easier */
244 #define DRM_ERROR(fmt, arg...) \
245         printf("error: " "[" DRM_NAME ":" __FUNCTION__ "] *ERROR* " fmt , ##arg)
246 #define DRM_MEM_ERROR(area, fmt, arg...) \
247         printf("error: " "[" DRM_NAME ":" __FUNCTION__ ":%s] *ERROR* " fmt , \
248                DRM(mem_stats)[area].name , ##arg)
249 #define DRM_INFO(fmt, arg...)  printf("info: " "[" DRM_NAME "] " fmt , ##arg)
250
251 #if DRM_DEBUG_CODE
252 #define DRM_DEBUG(fmt, arg...)                                            \
253         do {                                                              \
254                 if (DRM(flags) & DRM_FLAG_DEBUG)                                  \
255                         printf("[" DRM_NAME ":" __FUNCTION__ "] " fmt ,   \
256                                ##arg);                                    \
257         } while (0)
258 #else
259 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
260 #endif
261
262 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
263
264 #if (__FreeBSD_version >= 500000) || ((__FreeBSD_version < 500000) && (__FreeBSD_version >= 410002))
265 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
266 #else
267 #define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
268 #endif
269
270 #define DRM_SYSCTL_PRINT(fmt, arg...)           \
271   snprintf(buf, sizeof(buf), fmt, ##arg);       \
272   error = SYSCTL_OUT(req, buf, strlen(buf));    \
273   if (error) return error;
274
275 #define DRM_SYSCTL_PRINT_RET(ret, fmt, arg...)  \
276   snprintf(buf, sizeof(buf), fmt, ##arg);       \
277   error = SYSCTL_OUT(req, buf, strlen(buf));    \
278   if (error) { ret; return error; }
279
280
281 #define DRM_FIND_MAP(dest, o)                                           \
282         do {                                                            \
283                 drm_map_list_entry_t *listentry;                        \
284                 TAILQ_FOREACH(listentry, dev->maplist, link) {          \
285                         if ( listentry->map->offset == o ) {            \
286                                 dest = listentry->map;                  \
287                                 break;                                  \
288                         }                                               \
289                 }                                                       \
290         } while (0)
291
292
293 /* Internal functions */
294
295 /* drm_drv.h */
296 extern d_open_t         DRM(open);
297 extern d_close_t        DRM(close);
298 extern d_read_t         DRM(read);
299 extern d_write_t        DRM(write);
300 extern d_poll_t         DRM(poll);
301 extern d_mmap_t         DRM(mmap);
302 extern int              DRM(open_helper)(dev_t kdev, int flags, int fmt, 
303                                          DRM_OS_STRUCTPROC *p, drm_device_t *dev);
304 extern drm_file_t       *DRM(find_file_by_proc)(drm_device_t *dev, 
305                                          DRM_OS_STRUCTPROC *p);
306
307 /* Memory management support (drm_memory.h) */
308 extern int              DRM(mem_info) DRM_SYSCTL_HANDLER_ARGS;
309
310 /* SysCtl Support (drm_sysctl.h) */
311 extern int              DRM(sysctl_init)(drm_device_t *dev);
312 extern int              DRM(sysctl_cleanup)(drm_device_t *dev);