Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / drm / drm_os_freebsd.h
1 /*
2  * $FreeBSD: src/sys/dev/drm/drm_os_freebsd.h,v 1.10.2.1 2003/04/26 07:05:28 anholt Exp $
3  */
4 #include <sys/param.h>
5 #include <sys/queue.h>
6 #include <sys/malloc.h>
7 #include <sys/kernel.h>
8 #include <sys/module.h>
9 #include <sys/systm.h>
10 #include <sys/conf.h>
11 #include <sys/stat.h>
12 #include <sys/proc.h>
13 #include <sys/lock.h>
14 #include <sys/fcntl.h>
15 #include <sys/uio.h>
16 #include <sys/filio.h>
17 #include <sys/sysctl.h>
18 #include <sys/bus.h>
19 #include <sys/signalvar.h>
20 #include <sys/poll.h>
21 #include <vm/vm.h>
22 #include <vm/pmap.h>
23 #include <vm/vm_extern.h>
24 #include <vm/vm_map.h>
25 #include <vm/vm_param.h>
26 #include <machine/param.h>
27 #include <machine/pmap.h>
28 #include <machine/bus.h>
29 #include <machine/resource.h>
30 #include <sys/mman.h>
31 #include <sys/rman.h>
32 #include <sys/memrange.h>
33 #include <pci/pcivar.h>
34 #if __FreeBSD_version >= 500000
35 #include <sys/selinfo.h>
36 #else
37 #include <sys/select.h>
38 #endif
39 #include <sys/bus.h>
40 #if __FreeBSD_version >= 400005
41 #include <sys/taskqueue.h>
42 #endif
43 #if __FreeBSD_version >= 500000
44 #include <sys/mutex.h>
45 #endif
46
47 #if __FreeBSD_version >= 400006
48 #define __REALLY_HAVE_AGP       __HAVE_AGP
49 #endif
50
51 #ifdef __i386__
52 #define __REALLY_HAVE_MTRR      (__HAVE_MTRR) && (__FreeBSD_version >= 500000)
53 #else
54 #define __REALLY_HAVE_MTRR      0
55 #endif
56 #define __REALLY_HAVE_SG        (__HAVE_SG)
57
58 #if __REALLY_HAVE_AGP
59 #include <pci/agpvar.h>
60 #include <sys/agpio.h>
61 #endif
62
63 #include <opt_drm.h>
64 #if DRM_DEBUG
65 #undef  DRM_DEBUG_CODE
66 #define DRM_DEBUG_CODE 2
67 #endif
68 #undef DRM_DEBUG
69
70 #if DRM_LINUX
71 #include <sys/file.h>
72 #include <sys/proc.h>
73 #include <machine/../linux/linux.h>
74 #include <machine/../linux/linux_proto.h>
75 #endif
76
77 #define DRM_TIME_SLICE        (hz/20)  /* Time slice for GLXContexts      */
78
79 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
80 #define DRM_DEV_UID     0
81 #define DRM_DEV_GID     0
82 #define CDEV_MAJOR      145
83
84 #if __FreeBSD_version >= 500000
85 #define DRM_CURPROC             curthread
86 #define DRM_STRUCTPROC          struct thread
87 #define DRM_SPINTYPE            struct mtx
88 #define DRM_SPININIT(l,name)    mtx_init(&l, name, NULL, MTX_DEF)
89 #define DRM_SPINUNINIT(l)       mtx_destroy(&l)
90 #define DRM_SPINLOCK(l)         mtx_lock(l)
91 #define DRM_SPINUNLOCK(u)       mtx_unlock(u);
92 #define DRM_CURRENTPID          curthread->td_proc->p_pid
93 #else
94 #define DRM_CURPROC             curproc
95 #define DRM_STRUCTPROC          struct proc
96 #define DRM_SPINTYPE            struct simplelock
97 #define DRM_SPININIT(l,name)    simple_lock_init(&l)
98 #define DRM_SPINUNINIT(l)
99 #define DRM_SPINLOCK(l)         simple_lock(l)
100 #define DRM_SPINUNLOCK(u)       simple_unlock(u);
101 #define DRM_CURRENTPID          curproc->p_pid
102 #endif
103
104 /* Currently our DRMFILE (filp) is a void * which is actually the pid
105  * of the current process.  It should be a per-open unique pointer, but
106  * code for that is not yet written */
107 #define DRMFILE                 void *
108 #define DRM_IOCTL_ARGS          dev_t kdev, u_long cmd, caddr_t data, int flags, DRM_STRUCTPROC *p, DRMFILE filp
109 #define DRM_LOCK                lockmgr(&dev->dev_lock, LK_EXCLUSIVE, 0, DRM_CURPROC)
110 #define DRM_UNLOCK              lockmgr(&dev->dev_lock, LK_RELEASE, 0, DRM_CURPROC)
111 #define DRM_SUSER(p)            suser(p)
112 #define DRM_TASKQUEUE_ARGS      void *arg, int pending
113 #define DRM_IRQ_ARGS            void *arg
114 #define DRM_DEVICE              drm_device_t    *dev    = kdev->si_drv1
115 #define DRM_MALLOC(size)        malloc( size, DRM(M_DRM), M_NOWAIT )
116 #define DRM_FREE(pt,size)               free( pt, DRM(M_DRM) )
117 #define DRM_VTOPHYS(addr)       vtophys(addr)
118
119 /* Read/write from bus space, with byteswapping to le if necessary */
120 #define DRM_READ8(map, offset)          *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
121 #define DRM_READ32(map, offset)         *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
122 #define DRM_WRITE8(map, offset, val)    *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
123 #define DRM_WRITE32(map, offset, val)   *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
124 /*
125 #define DRM_READ8(map, offset)          bus_space_read_1(  (map)->iot, (map)->ioh, (offset) )
126 #define DRM_READ32(map, offset)         bus_space_read_4(  (map)->iot, (map)->ioh, (offset) )
127 #define DRM_WRITE8(map, offset, val)    bus_space_write_1( (map)->iot, (map)->ioh, (offset), (val) )
128 #define DRM_WRITE32(map, offset, val)   bus_space_write_4( (map)->iot, (map)->ioh, (offset), (val) )
129 */
130 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
131 #define DRM_ERR(v)              v
132
133 #define DRM_PRIV                                        \
134         drm_file_t      *priv   = (drm_file_t *) DRM(find_file_by_proc)(dev, p); \
135         if (!priv) {                                            \
136                 DRM_DEBUG("can't find authenticator\n");        \
137                 return EINVAL;                                  \
138         }
139
140 #define LOCK_TEST_WITH_RETURN(dev, filp)                                \
141 do {                                                                    \
142         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
143              dev->lock.filp != filp) {                                  \
144                 DRM_ERROR("%s called without lock held\n",              \
145                            __FUNCTION__);                               \
146                 return EINVAL;                                          \
147         }                                                               \
148 } while (0)
149
150 #define DRM_UDELAY( udelay )                                    \
151 do {                                                            \
152         struct timeval tv1, tv2;                                \
153         microtime(&tv1);                                        \
154         do {                                                    \
155                 microtime(&tv2);                                \
156         }                                                       \
157         while (((tv2.tv_sec-tv1.tv_sec)*1000000 + tv2.tv_usec - tv1.tv_usec) < udelay ); \
158 } while (0)
159
160 #define DRM_GETSAREA()                                  \
161 do {                                                            \
162         drm_map_list_entry_t *listentry;                        \
163         TAILQ_FOREACH(listentry, dev->maplist, link) {          \
164                 drm_local_map_t *map = listentry->map;          \
165                 if (map->type == _DRM_SHM &&                    \
166                         map->flags & _DRM_CONTAINS_LOCK) {      \
167                         dev_priv->sarea = map;                  \
168                         break;                                  \
169                 }                                               \
170         }                                                       \
171 } while (0)
172
173 #define DRM_HZ hz
174
175 #define DRM_WAIT_ON( ret, queue, timeout, condition )                   \
176 while (!condition) {                                                    \
177         ret = tsleep( &(queue), PZERO | PCATCH, "drmwtq", (timeout) );  \
178         if ( ret )                                                      \
179                 return ret;                                             \
180 }
181
182 #define DRM_WAKEUP( queue ) wakeup( queue )
183 #define DRM_WAKEUP_INT( queue ) wakeup( queue )
184 #define DRM_INIT_WAITQUEUE( queue )  do {} while (0)
185
186 #define DRM_COPY_TO_USER_IOCTL(user, kern, size)        \
187         if ( IOCPARM_LEN(cmd) != size)                  \
188                 return EINVAL;                          \
189         *user = kern;
190 #define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
191         if ( IOCPARM_LEN(cmd) != size)                  \
192                 return EINVAL;                          \
193         kern = *user;
194 #define DRM_COPY_TO_USER(user, kern, size) \
195         copyout(kern, user, size)
196 #define DRM_COPY_FROM_USER(kern, user, size) \
197         copyin(user, kern, size)
198 /* Macros for userspace access with checking readability once */
199 /* FIXME: can't find equivalent functionality for nocheck yet.
200  * It'll be slower than linux, but should be correct.
201  */
202 #define DRM_VERIFYAREA_READ( uaddr, size )              \
203         (!useracc((caddr_t)uaddr, size, VM_PROT_READ))
204 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
205         copyin(arg2, arg1, arg3)
206 #define DRM_GET_USER_UNCHECKED(val, uaddr)                      \
207         ((val) = fuword(uaddr), 0)
208
209 #define DRM_WRITEMEMORYBARRIER( map )                                   \
210         bus_space_barrier((map)->iot, (map)->ioh, 0, (map)->size, 0);
211 #define DRM_READMEMORYBARRIER( map )                                    \
212         bus_space_barrier((map)->iot, (map)->ioh, 0, (map)->size, BUS_SPACE_BARRIER_READ);
213
214 #define PAGE_ALIGN(addr) round_page(addr)
215
216 #ifndef M_WAITOK                /* M_WAITOK (=0) name removed in -current */
217 #define M_WAITOK 0
218 #endif
219
220 #define malloctype DRM(M_DRM)
221 /* The macros conflicted in the MALLOC_DEFINE */
222 MALLOC_DECLARE(malloctype);
223 #undef malloctype
224
225 typedef struct drm_chipinfo
226 {
227         int vendor;
228         int device;
229         int supported;
230         char *name;
231 } drm_chipinfo_t;
232
233 #define cpu_to_le32(x) (x)      /* FIXME */
234
235 typedef unsigned long dma_addr_t;
236 typedef u_int32_t atomic_t;
237 typedef u_int32_t u32;
238 typedef u_int16_t u16;
239 typedef u_int8_t u8;
240 #define atomic_set(p, v)        (*(p) = (v))
241 #define atomic_read(p)          (*(p))
242 #define atomic_inc(p)           atomic_add_int(p, 1)
243 #define atomic_dec(p)           atomic_subtract_int(p, 1)
244 #define atomic_add(n, p)        atomic_add_int(p, n)
245 #define atomic_sub(n, p)        atomic_subtract_int(p, n)
246
247 /* Fake this */
248
249 #if __FreeBSD_version < 500000
250 /* The extra atomic functions from 5.0 haven't been merged to 4.x */
251 static __inline int
252 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
253 {
254         int res = exp;
255
256         __asm __volatile (
257         "       lock ;                  "
258         "       cmpxchgl %1,%2 ;        "
259         "       setz    %%al ;          "
260         "       movzbl  %%al,%0 ;       "
261         "1:                             "
262         "# atomic_cmpset_int"
263         : "+a" (res)                    /* 0 (result) */
264         : "r" (src),                    /* 1 */
265           "m" (*(dst))                  /* 2 */
266         : "memory");                             
267
268         return (res);
269 }
270 #endif
271
272 static __inline atomic_t
273 test_and_set_bit(int b, volatile void *p)
274 {
275         int s = splhigh();
276         unsigned int m = 1<<b;
277         unsigned int r = *(volatile int *)p & m;
278         *(volatile int *)p |= m;
279         splx(s);
280         return r;
281 }
282
283 static __inline void
284 clear_bit(int b, volatile void *p)
285 {
286     atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
287 }
288
289 static __inline void
290 set_bit(int b, volatile void *p)
291 {
292     atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
293 }
294
295 static __inline int
296 test_bit(int b, volatile void *p)
297 {
298     return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
299 }
300
301 static __inline int
302 find_first_zero_bit(volatile void *p, int max)
303 {
304     int b;
305
306     for (b = 0; b < max; b += 32) {
307         if (((volatile int *)p)[b >> 5] != ~0) {
308             for (;;) {
309                 if ((((volatile int *)p)[b >> 5] & (1 << (b & 0x1f))) == 0)
310                     return b;
311                 b++;
312             }
313         }
314     }
315     return max;
316 }
317
318 #define spldrm()                spltty()
319
320 #define memset(p, v, s)         bzero(p, s)
321
322 /*
323  * Fake out the module macros for versions of FreeBSD where they don't
324  * exist.
325  */
326 #if (__FreeBSD_version < 500002 && __FreeBSD_version > 500000) || __FreeBSD_version < 420000
327 #define MODULE_VERSION(a,b)             struct __hack
328 #define MODULE_DEPEND(a,b,c,d,e)        struct __hack
329 #endif
330
331 /* Redefinitions to make templating easy */
332 #define wait_queue_head_t       atomic_t
333 #define agp_memory              void
334 #define jiffies                 ticks
335
336                                 /* Macros to make printf easier */
337 #define DRM_ERROR(fmt, arg...) \
338         printf("error: " "[" DRM_NAME ":%s] *ERROR* " fmt , __func__ , ## arg)
339 #define DRM_MEM_ERROR(area, fmt, arg...) \
340         printf("error: " "[" DRM_NAME ":%s:%s] *ERROR* " fmt , \
341                 __func__, DRM(mem_stats)[area].name , ##arg)
342 #define DRM_INFO(fmt, arg...)  printf("info: " "[" DRM_NAME "] " fmt , ## arg)
343
344 #if DRM_DEBUG_CODE
345 #define DRM_DEBUG(fmt, arg...)                                            \
346         do {                                                              \
347                 if (DRM(flags) & DRM_FLAG_DEBUG)                          \
348                         printf("[" DRM_NAME ":%s] " fmt , __func__ , ## arg); \
349         } while (0)
350 #else
351 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
352 #endif
353
354 #if (__FreeBSD_version >= 500000) || ((__FreeBSD_version < 500000) && (__FreeBSD_version >= 410002))
355 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
356 #else
357 #define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
358 #endif
359
360 #define DRM_SYSCTL_PRINT(fmt, arg...)           \
361   snprintf(buf, sizeof(buf), fmt, ##arg);       \
362   error = SYSCTL_OUT(req, buf, strlen(buf));    \
363   if (error) return error;
364
365 #define DRM_SYSCTL_PRINT_RET(ret, fmt, arg...)  \
366   snprintf(buf, sizeof(buf), fmt, ##arg);       \
367   error = SYSCTL_OUT(req, buf, strlen(buf));    \
368   if (error) { ret; return error; }
369
370
371 #define DRM_FIND_MAP(dest, o)                                           \
372         do {                                                            \
373                 drm_map_list_entry_t *listentry;                        \
374                 TAILQ_FOREACH(listentry, dev->maplist, link) {          \
375                         if ( listentry->map->offset == o ) {            \
376                                 dest = listentry->map;                  \
377                                 break;                                  \
378                         }                                               \
379                 }                                                       \
380         } while (0)
381
382
383 /* Internal functions */
384
385 /* drm_drv.h */
386 extern d_ioctl_t        DRM(ioctl);
387 extern d_open_t         DRM(open);
388 extern d_close_t        DRM(close);
389 extern d_read_t         DRM(read);
390 extern d_poll_t         DRM(poll);
391 extern d_mmap_t         DRM(mmap);
392 extern int              DRM(open_helper)(dev_t kdev, int flags, int fmt, 
393                                          DRM_STRUCTPROC *p, drm_device_t *dev);
394 extern drm_file_t       *DRM(find_file_by_proc)(drm_device_t *dev, 
395                                          DRM_STRUCTPROC *p);
396
397 /* sysctl support (drm_sysctl.h) */
398 extern int              DRM(sysctl_init)(drm_device_t *dev);
399 extern int              DRM(sysctl_cleanup)(drm_device_t *dev);
400
401 /* Memory info sysctl (drm_memory.h) */
402 extern int              DRM(mem_info) DRM_SYSCTL_HANDLER_ARGS;