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