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