2 * $FreeBSD: src/sys/dev/drm/drm_os_freebsd.h,v 1.10.2.1 2003/04/26 07:05:28 anholt Exp $
6 #include <sys/malloc.h>
7 #include <sys/kernel.h>
8 #include <sys/module.h>
14 #include <sys/fcntl.h>
16 #include <sys/filio.h>
17 #include <sys/sysctl.h>
19 #include <sys/signalvar.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>
32 #include <sys/memrange.h>
33 #include <pci/pcivar.h>
34 #if __FreeBSD_version >= 500000
35 #include <sys/selinfo.h>
37 #include <sys/select.h>
40 #if __FreeBSD_version >= 400005
41 #include <sys/taskqueue.h>
43 #if __FreeBSD_version >= 500000
44 #include <sys/mutex.h>
47 #if __FreeBSD_version >= 400006
48 #define __REALLY_HAVE_AGP __HAVE_AGP
52 #define __REALLY_HAVE_MTRR (__HAVE_MTRR) && (__FreeBSD_version >= 500000)
54 #define __REALLY_HAVE_MTRR 0
56 #define __REALLY_HAVE_SG (__HAVE_SG)
59 #include <pci/agpvar.h>
60 #include <sys/agpio.h>
66 #define DRM_DEBUG_CODE 2
73 #include <machine/../linux/linux.h>
74 #include <machine/../linux/linux_proto.h>
77 #define DRM_TIME_SLICE (hz/20) /* Time slice for GLXContexts */
79 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
82 #define CDEV_MAJOR 145
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
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
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)
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
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) )
130 #define DRM_AGP_FIND_DEVICE() agp_find_device()
134 drm_file_t *priv = (drm_file_t *) DRM(find_file_by_proc)(dev, p); \
136 DRM_DEBUG("can't find authenticator\n"); \
140 #define LOCK_TEST_WITH_RETURN(dev, filp) \
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", \
150 #define DRM_UDELAY( udelay ) \
152 struct timeval tv1, tv2; \
157 while (((tv2.tv_sec-tv1.tv_sec)*1000000 + tv2.tv_usec - tv1.tv_usec) < udelay ); \
160 #define DRM_GETSAREA() \
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; \
175 #define DRM_WAIT_ON( ret, queue, timeout, condition ) \
176 while (!condition) { \
177 ret = tsleep( &(queue), PZERO | PCATCH, "drmwtq", (timeout) ); \
182 #define DRM_WAKEUP( queue ) wakeup( queue )
183 #define DRM_WAKEUP_INT( queue ) wakeup( queue )
184 #define DRM_INIT_WAITQUEUE( queue ) do {} while (0)
186 #define DRM_COPY_TO_USER_IOCTL(user, kern, size) \
187 if ( IOCPARM_LEN(cmd) != size) \
190 #define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
191 if ( IOCPARM_LEN(cmd) != size) \
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.
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)
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);
214 #define PAGE_ALIGN(addr) round_page(addr)
216 #ifndef M_WAITOK /* M_WAITOK (=0) name removed in -current */
220 #define malloctype DRM(M_DRM)
221 /* The macros conflicted in the MALLOC_DEFINE */
222 MALLOC_DECLARE(malloctype);
225 typedef struct drm_chipinfo
233 #define cpu_to_le32(x) (x) /* FIXME */
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;
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)
249 #if __FreeBSD_version < 500000
250 /* The extra atomic functions from 5.0 haven't been merged to 4.x */
252 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
262 "# atomic_cmpset_int"
263 : "+a" (res) /* 0 (result) */
272 static __inline atomic_t
273 test_and_set_bit(int b, volatile void *p)
276 unsigned int m = 1<<b;
277 unsigned int r = *(volatile int *)p & m;
278 *(volatile int *)p |= m;
284 clear_bit(int b, volatile void *p)
286 atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
290 set_bit(int b, volatile void *p)
292 atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
296 test_bit(int b, volatile void *p)
298 return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
302 find_first_zero_bit(volatile void *p, int max)
306 for (b = 0; b < max; b += 32) {
307 if (((volatile int *)p)[b >> 5] != ~0) {
309 if ((((volatile int *)p)[b >> 5] & (1 << (b & 0x1f))) == 0)
318 #define spldrm() spltty()
320 #define memset(p, v, s) bzero(p, s)
323 * Fake out the module macros for versions of FreeBSD where they don't
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
331 /* Redefinitions to make templating easy */
332 #define wait_queue_head_t atomic_t
333 #define agp_memory void
334 #define jiffies ticks
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)
345 #define DRM_DEBUG(fmt, arg...) \
347 if (DRM(flags) & DRM_FLAG_DEBUG) \
348 printf("[" DRM_NAME ":%s] " fmt , __func__ , ## arg); \
351 #define DRM_DEBUG(fmt, arg...) do { } while (0)
354 #if (__FreeBSD_version >= 500000) || ((__FreeBSD_version < 500000) && (__FreeBSD_version >= 410002))
355 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
357 #define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
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;
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; }
371 #define DRM_FIND_MAP(dest, o) \
373 drm_map_list_entry_t *listentry; \
374 TAILQ_FOREACH(listentry, dev->maplist, link) { \
375 if ( listentry->map->offset == o ) { \
376 dest = listentry->map; \
383 /* Internal functions */
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,
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);
401 /* Memory info sysctl (drm_memory.h) */
402 extern int DRM(mem_info) DRM_SYSCTL_HANDLER_ARGS;