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