Deleted Added
full compact
drmP.h (164033) drmP.h (173573)
1/* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2 * Created: Mon Jan 4 10:05:05 1999 by faith@precisioninsight.com
3 */
4/*-
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Rickard E. (Rik) Faith <faith@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
31 *
32 */
33
34#include <sys/cdefs.h>
1/* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2 * Created: Mon Jan 4 10:05:05 1999 by faith@precisioninsight.com
3 */
4/*-
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Rickard E. (Rik) Faith <faith@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
31 *
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/drm/drmP.h 164033 2006-11-06 13:42:10Z rwatson $");
35__FBSDID("$FreeBSD: head/sys/dev/drm/drmP.h 173573 2007-11-12 21:51:38Z jhb $");
36
37#ifndef _DRM_P_H_
38#define _DRM_P_H_
39
40#if defined(_KERNEL) || defined(__KERNEL__)
41
42typedef struct drm_device drm_device_t;
43typedef struct drm_file drm_file_t;
44
45#include <sys/param.h>
46#include <sys/queue.h>
47#include <sys/malloc.h>
48#include <sys/kernel.h>
49#include <sys/module.h>
50#include <sys/systm.h>
51#include <sys/conf.h>
52#include <sys/stat.h>
53#if __FreeBSD_version >= 700000
54#include <sys/priv.h>
55#endif
56#include <sys/proc.h>
57#include <sys/lock.h>
58#include <sys/fcntl.h>
59#include <sys/uio.h>
60#include <sys/filio.h>
61#include <sys/sysctl.h>
62#include <sys/bus.h>
63#include <sys/signalvar.h>
64#include <sys/poll.h>
65#include <vm/vm.h>
66#include <vm/pmap.h>
67#include <vm/vm_extern.h>
68#include <vm/vm_map.h>
69#include <vm/vm_param.h>
70#include <machine/param.h>
71#include <machine/pmap.h>
72#include <machine/bus.h>
73#include <machine/resource.h>
74#include <machine/sysarch.h>
75#include <sys/endian.h>
76#include <sys/mman.h>
77#if defined(__FreeBSD__)
78#include <sys/rman.h>
79#include <sys/memrange.h>
36
37#ifndef _DRM_P_H_
38#define _DRM_P_H_
39
40#if defined(_KERNEL) || defined(__KERNEL__)
41
42typedef struct drm_device drm_device_t;
43typedef struct drm_file drm_file_t;
44
45#include <sys/param.h>
46#include <sys/queue.h>
47#include <sys/malloc.h>
48#include <sys/kernel.h>
49#include <sys/module.h>
50#include <sys/systm.h>
51#include <sys/conf.h>
52#include <sys/stat.h>
53#if __FreeBSD_version >= 700000
54#include <sys/priv.h>
55#endif
56#include <sys/proc.h>
57#include <sys/lock.h>
58#include <sys/fcntl.h>
59#include <sys/uio.h>
60#include <sys/filio.h>
61#include <sys/sysctl.h>
62#include <sys/bus.h>
63#include <sys/signalvar.h>
64#include <sys/poll.h>
65#include <vm/vm.h>
66#include <vm/pmap.h>
67#include <vm/vm_extern.h>
68#include <vm/vm_map.h>
69#include <vm/vm_param.h>
70#include <machine/param.h>
71#include <machine/pmap.h>
72#include <machine/bus.h>
73#include <machine/resource.h>
74#include <machine/sysarch.h>
75#include <sys/endian.h>
76#include <sys/mman.h>
77#if defined(__FreeBSD__)
78#include <sys/rman.h>
79#include <sys/memrange.h>
80#if __FreeBSD_version >= 800004
81#include <dev/agp/agpvar.h>
82#else
80#include <pci/agpvar.h>
83#include <pci/agpvar.h>
84#endif
81#include <sys/agpio.h>
82#if __FreeBSD_version >= 500000
83#include <sys/mutex.h>
84#include <dev/pci/pcivar.h>
85#include <sys/selinfo.h>
86#else /* __FreeBSD_version >= 500000 */
87#include <pci/pcivar.h>
88#include <sys/select.h>
89#endif /* __FreeBSD_version < 500000 */
90#elif defined(__NetBSD__)
91#include <machine/mtrr.h>
92#include <sys/vnode.h>
93#include <sys/select.h>
94#include <sys/device.h>
95#include <sys/resourcevar.h>
96#include <sys/lkm.h>
97#include <sys/agpio.h>
98#include <sys/ttycom.h>
99#include <uvm/uvm.h>
100#include <dev/pci/pcireg.h>
101#include <dev/pci/pcivar.h>
102#include <dev/pci/agpvar.h>
103#elif defined(__OpenBSD__)
104#include <sys/lkm.h>
105#include <uvm/uvm.h>
106#endif
107#include <sys/bus.h>
108
109#include "dev/drm/drm.h"
110#include "dev/drm/drm_linux_list.h"
111#include "dev/drm/drm_atomic.h"
112
113#ifdef __FreeBSD__
114#include <opt_drm.h>
115#ifdef DRM_DEBUG
116#undef DRM_DEBUG
117#define DRM_DEBUG_DEFAULT_ON 1
118#endif /* DRM_DEBUG */
119#endif
120
121#if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
122#include <sys/file.h>
123#include <sys/proc.h>
124#include <machine/../linux/linux.h>
125#include <machine/../linux/linux_proto.h>
126#else
127/* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
128 * supported on this OS yet.
129 */
130#undef DRM_LINUX
131#define DRM_LINUX 0
132#endif
133
134#define DRM_HASH_SIZE 16 /* Size of key hash table */
135#define DRM_KERNEL_CONTEXT 0 /* Change drm_resctx if changed */
136#define DRM_RESERVED_CONTEXTS 1 /* Change drm_resctx if changed */
137
138#define DRM_MEM_DMA 0
139#define DRM_MEM_SAREA 1
140#define DRM_MEM_DRIVER 2
141#define DRM_MEM_MAGIC 3
142#define DRM_MEM_IOCTLS 4
143#define DRM_MEM_MAPS 5
144#define DRM_MEM_BUFS 6
145#define DRM_MEM_SEGS 7
146#define DRM_MEM_PAGES 8
147#define DRM_MEM_FILES 9
148#define DRM_MEM_QUEUES 10
149#define DRM_MEM_CMDS 11
150#define DRM_MEM_MAPPINGS 12
151#define DRM_MEM_BUFLISTS 13
152#define DRM_MEM_AGPLISTS 14
153#define DRM_MEM_TOTALAGP 15
154#define DRM_MEM_BOUNDAGP 16
155#define DRM_MEM_CTXBITMAP 17
156#define DRM_MEM_STUB 18
157#define DRM_MEM_SGLISTS 19
158
159#define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
160
161 /* Internal types and structures */
162#define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
163#define DRM_MIN(a,b) ((a)<(b)?(a):(b))
164#define DRM_MAX(a,b) ((a)>(b)?(a):(b))
165
166#define DRM_IF_VERSION(maj, min) (maj << 16 | min)
167
168MALLOC_DECLARE(M_DRM);
169
170#define __OS_HAS_AGP 1
171
172#define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
173#define DRM_DEV_UID 0
174#define DRM_DEV_GID 0
175
176#define wait_queue_head_t atomic_t
177#define DRM_WAKEUP(w) wakeup((void *)w)
178#define DRM_WAKEUP_INT(w) wakeup(w)
179#define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
180
181#if defined(__FreeBSD__) && __FreeBSD_version < 502109
182#define bus_alloc_resource_any(dev, type, rid, flags) \
183 bus_alloc_resource(dev, type, rid, 0ul, ~0ul, 1, flags)
184#endif
185
186#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
187#define DRM_CURPROC curthread
188#define DRM_STRUCTPROC struct thread
189#define DRM_SPINTYPE struct mtx
190#define DRM_SPININIT(l,name) mtx_init(&l, name, NULL, MTX_DEF)
191#define DRM_SPINUNINIT(l) mtx_destroy(&l)
192#define DRM_SPINLOCK(l) mtx_lock(l)
193#define DRM_SPINUNLOCK(u) mtx_unlock(u);
194#define DRM_SPINLOCK_ASSERT(l) mtx_assert(l, MA_OWNED)
195#define DRM_CURRENTPID curthread->td_proc->p_pid
196#define DRM_LOCK() mtx_lock(&dev->dev_lock)
197#define DRM_UNLOCK() mtx_unlock(&dev->dev_lock)
198#define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
199#else /* __FreeBSD__ && __FreeBSD_version >= 500000 */
200#define DRM_CURPROC curproc
201#define DRM_STRUCTPROC struct proc
202#define DRM_SPINTYPE struct simplelock
203#define DRM_SPININIT(l,name)
204#define DRM_SPINUNINIT(l)
205#define DRM_SPINLOCK(l)
206#define DRM_SPINUNLOCK(u)
207#define DRM_SPINLOCK_ASSERT(l)
208#define DRM_CURRENTPID curproc->p_pid
209#define DRM_LOCK()
210#define DRM_UNLOCK()
211#define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
212#define spldrm() spltty()
213#endif /* __NetBSD__ || __OpenBSD__ */
214
215/* Currently our DRMFILE (filp) is a void * which is actually the pid
216 * of the current process. It should be a per-open unique pointer, but
217 * code for that is not yet written */
218#define DRMFILE void *
219#define DRM_IRQ_ARGS void *arg
220typedef void irqreturn_t;
221#define IRQ_HANDLED /* nothing */
222#define IRQ_NONE /* nothing */
223
224enum {
225 DRM_IS_NOT_AGP,
226 DRM_IS_AGP,
227 DRM_MIGHT_BE_AGP
228};
229#define DRM_AGP_MEM struct agp_memory_info
230
231#if defined(__FreeBSD__)
232#define DRM_DEVICE \
233 drm_device_t *dev = kdev->si_drv1
234#define DRM_IOCTL_ARGS struct cdev *kdev, u_long cmd, caddr_t data, \
235 int flags, DRM_STRUCTPROC *p, DRMFILE filp
236
237#define PAGE_ALIGN(addr) round_page(addr)
238/* DRM_SUSER returns true if the user is superuser */
239#if __FreeBSD_version >= 700000
240#define DRM_SUSER(p) (priv_check(p, PRIV_DRIVER) == 0)
241#else
242#define DRM_SUSER(p) (suser(p) == 0)
243#endif
244#define DRM_AGP_FIND_DEVICE() agp_find_device()
245#define DRM_MTRR_WC MDF_WRITECOMBINE
246#define jiffies ticks
247
248#else /* __FreeBSD__ */
249
250#if defined(__NetBSD__)
251#define DRM_DEVICE \
252 drm_device_t *dev = device_lookup(&drm_cd, minor(kdev))
253#elif defined(__OpenBSD__)
254#define DRM_DEVICE \
255 drm_device_t *dev = (device_lookup(&drm_cd, \
256 minor(kdev)))->dv_cfdata->cf_driver->cd_devs[minor(kdev)]
257#endif /* __OpenBSD__ */
258#define DRM_IOCTL_ARGS dev_t kdev, u_long cmd, caddr_t data, \
259 int flags, DRM_STRUCTPROC *p, DRMFILE filp
260
261#define CDEV_MAJOR 34
262#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
263/* DRM_SUSER returns true if the user is superuser */
264#define DRM_SUSER(p) (suser(p->p_ucred, &p->p_acflag) == 0)
265#define DRM_AGP_FIND_DEVICE() agp_find_device(0)
266#define DRM_MTRR_WC MTRR_TYPE_WC
267#define jiffies hardclock_ticks
268
269typedef drm_device_t *device_t;
270extern struct cfdriver drm_cd;
271#endif /* !__FreeBSD__ */
272
273/* Capabilities taken from src/sys/dev/pci/pcireg.h. */
274#ifndef PCIY_AGP
275#define PCIY_AGP 0x02
276#endif
277
278#ifndef PCIY_EXPRESS
279#define PCIY_EXPRESS 0x10
280#endif
281
282typedef unsigned long dma_addr_t;
283typedef u_int64_t u64;
284typedef u_int32_t u32;
285typedef u_int16_t u16;
286typedef u_int8_t u8;
287
288/* DRM_READMEMORYBARRIER() prevents reordering of reads.
289 * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
290 * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
291 */
292#if defined(__i386__)
293#define DRM_READMEMORYBARRIER() __asm __volatile( \
294 "lock; addl $0,0(%%esp)" : : : "memory");
295#define DRM_WRITEMEMORYBARRIER() __asm __volatile("" : : : "memory");
296#define DRM_MEMORYBARRIER() __asm __volatile( \
297 "lock; addl $0,0(%%esp)" : : : "memory");
298#elif defined(__alpha__)
299#define DRM_READMEMORYBARRIER() alpha_mb();
300#define DRM_WRITEMEMORYBARRIER() alpha_wmb();
301#define DRM_MEMORYBARRIER() alpha_mb();
302#elif defined(__amd64__)
303#define DRM_READMEMORYBARRIER() __asm __volatile( \
304 "lock; addl $0,0(%%rsp)" : : : "memory");
305#define DRM_WRITEMEMORYBARRIER() __asm __volatile("" : : : "memory");
306#define DRM_MEMORYBARRIER() __asm __volatile( \
307 "lock; addl $0,0(%%rsp)" : : : "memory");
308#endif
309
310#ifdef __FreeBSD__
311#define DRM_READ8(map, offset) \
312 *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
313#define DRM_READ16(map, offset) \
314 *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
315#define DRM_READ32(map, offset) \
316 *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
317#define DRM_WRITE8(map, offset, val) \
318 *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
319#define DRM_WRITE16(map, offset, val) \
320 *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
321#define DRM_WRITE32(map, offset, val) \
322 *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
323
324#define DRM_VERIFYAREA_READ( uaddr, size ) \
325 (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
326
327#else /* __FreeBSD__ */
328
329typedef vaddr_t vm_offset_t;
330
331#define DRM_READ8(map, offset) \
332 bus_space_read_1( (map)->bst, (map)->bsh, (offset))
333#define DRM_READ16(map, offset) \
334 bus_space_read_2( (map)->bst, (map)->bsh, (offset))
335#define DRM_READ32(map, offset) \
336 bus_space_read_4( (map)->bst, (map)->bsh, (offset))
337#define DRM_WRITE8(map, offset, val) \
338 bus_space_write_1((map)->bst, (map)->bsh, (offset), (val))
339#define DRM_WRITE16(map, offset, val) \
340 bus_space_write_2((map)->bst, (map)->bsh, (offset), (val))
341#define DRM_WRITE32(map, offset, val) \
342 bus_space_write_4((map)->bst, (map)->bsh, (offset), (val))
343
344#define DRM_VERIFYAREA_READ( uaddr, size ) \
345 (!uvm_useracc((caddr_t)uaddr, size, VM_PROT_READ))
346#endif /* !__FreeBSD__ */
347
348#define DRM_COPY_TO_USER_IOCTL(user, kern, size) \
349 if ( IOCPARM_LEN(cmd) != size) \
350 return EINVAL; \
351 *user = kern;
352#define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
353 if ( IOCPARM_LEN(cmd) != size) \
354 return EINVAL; \
355 kern = *user;
356#define DRM_COPY_TO_USER(user, kern, size) \
357 copyout(kern, user, size)
358#define DRM_COPY_FROM_USER(kern, user, size) \
359 copyin(user, kern, size)
360#define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) \
361 copyin(arg2, arg1, arg3)
362#define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3) \
363 copyout(arg2, arg1, arg3)
364#if __FreeBSD_version > 500000
365#define DRM_GET_USER_UNCHECKED(val, uaddr) \
366 ((val) = fuword32(uaddr), 0)
367#else
368#define DRM_GET_USER_UNCHECKED(val, uaddr) \
369 ((val) = fuword(uaddr), 0)
370#endif
371
372#define cpu_to_le32(x) htole32(x)
373#define le32_to_cpu(x) le32toh(x)
374
375#define DRM_ERR(v) v
376#define DRM_HZ hz
377#define DRM_UDELAY(udelay) DELAY(udelay)
378#define DRM_TIME_SLICE (hz/20) /* Time slice for GLXContexts */
379
380#define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do { \
381 (_map) = (_dev)->context_sareas[_ctx]; \
382} while(0)
383
384#define DRM_GET_PRIV_WITH_RETURN(_priv, _filp) \
385do { \
386 if (_filp != (DRMFILE)(intptr_t)DRM_CURRENTPID) { \
387 DRM_ERROR("filp doesn't match curproc\n"); \
388 return EINVAL; \
389 } \
390 _priv = drm_find_file_by_proc(dev, DRM_CURPROC); \
391 if (_priv == NULL) { \
392 DRM_ERROR("can't find authenticator\n"); \
393 return EINVAL; \
394 } \
395} while (0)
396
397#define LOCK_TEST_WITH_RETURN(dev, filp) \
398do { \
399 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || \
400 dev->lock.filp != filp) { \
401 DRM_ERROR("%s called without lock held\n", \
402 __FUNCTION__); \
403 return EINVAL; \
404 } \
405} while (0)
406
407#define DRM_GETSAREA() \
408do { \
409 drm_local_map_t *map; \
410 DRM_SPINLOCK_ASSERT(&dev->dev_lock); \
411 TAILQ_FOREACH(map, &dev->maplist, link) { \
412 if (map->type == _DRM_SHM && \
413 map->flags & _DRM_CONTAINS_LOCK) { \
414 dev_priv->sarea = map; \
415 break; \
416 } \
417 } \
418} while (0)
419
420#if defined(__FreeBSD__) && __FreeBSD_version > 500000
421#define DRM_WAIT_ON( ret, queue, timeout, condition ) \
422for ( ret = 0 ; !ret && !(condition) ; ) { \
423 DRM_UNLOCK(); \
424 mtx_lock(&dev->irq_lock); \
425 if (!(condition)) \
426 ret = msleep(&(queue), &dev->irq_lock, \
427 PZERO | PCATCH, "drmwtq", (timeout)); \
428 mtx_unlock(&dev->irq_lock); \
429 DRM_LOCK(); \
430}
431#else
432#define DRM_WAIT_ON( ret, queue, timeout, condition ) \
433for ( ret = 0 ; !ret && !(condition) ; ) { \
434 int s = spldrm(); \
435 if (!(condition)) \
436 ret = tsleep( &(queue), PZERO | PCATCH, \
437 "drmwtq", (timeout) ); \
438 splx(s); \
439}
440#endif
441
442#define DRM_ERROR(fmt, arg...) \
443 printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt, \
444 DRM_CURRENTPID, __func__ , ## arg)
445
446#define DRM_INFO(fmt, arg...) printf("info: [" DRM_NAME "] " fmt , ## arg)
447
448#define DRM_DEBUG(fmt, arg...) do { \
449 if (drm_debug_flag) \
450 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
451 __func__ , ## arg); \
452} while (0)
453
454typedef struct drm_pci_id_list
455{
456 int vendor;
457 int device;
458 long driver_private;
459 char *name;
460} drm_pci_id_list_t;
461
462#define DRM_AUTH 0x1
463#define DRM_MASTER 0x2
464#define DRM_ROOT_ONLY 0x4
465typedef struct drm_ioctl_desc {
466 int (*func)(DRM_IOCTL_ARGS);
467 int flags;
468} drm_ioctl_desc_t;
469
470typedef struct drm_magic_entry {
471 drm_magic_t magic;
472 struct drm_file *priv;
473 struct drm_magic_entry *next;
474} drm_magic_entry_t;
475
476typedef struct drm_magic_head {
477 struct drm_magic_entry *head;
478 struct drm_magic_entry *tail;
479} drm_magic_head_t;
480
481typedef struct drm_buf {
482 int idx; /* Index into master buflist */
483 int total; /* Buffer size */
484 int order; /* log-base-2(total) */
485 int used; /* Amount of buffer in use (for DMA) */
486 unsigned long offset; /* Byte offset (used internally) */
487 void *address; /* Address of buffer */
488 unsigned long bus_address; /* Bus address of buffer */
489 struct drm_buf *next; /* Kernel-only: used for free list */
490 __volatile__ int pending; /* On hardware DMA queue */
491 DRMFILE filp; /* Unique identifier of holding process */
492 int context; /* Kernel queue for this buffer */
493 enum {
494 DRM_LIST_NONE = 0,
495 DRM_LIST_FREE = 1,
496 DRM_LIST_WAIT = 2,
497 DRM_LIST_PEND = 3,
498 DRM_LIST_PRIO = 4,
499 DRM_LIST_RECLAIM = 5
500 } list; /* Which list we're on */
501
502 int dev_priv_size; /* Size of buffer private stoarge */
503 void *dev_private; /* Per-buffer private storage */
504} drm_buf_t;
505
506typedef struct drm_freelist {
507 int initialized; /* Freelist in use */
508 atomic_t count; /* Number of free buffers */
509 drm_buf_t *next; /* End pointer */
510
511 int low_mark; /* Low water mark */
512 int high_mark; /* High water mark */
513} drm_freelist_t;
514
515typedef struct drm_dma_handle {
516 void *vaddr;
517 bus_addr_t busaddr;
518#if defined(__FreeBSD__)
519 bus_dma_tag_t tag;
520 bus_dmamap_t map;
521#elif defined(__NetBSD__)
522 bus_dma_segment_t seg;
523#endif
524} drm_dma_handle_t;
525
526typedef struct drm_buf_entry {
527 int buf_size;
528 int buf_count;
529 drm_buf_t *buflist;
530 int seg_count;
531 drm_dma_handle_t **seglist;
532 int page_order;
533
534 drm_freelist_t freelist;
535} drm_buf_entry_t;
536
537typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
538struct drm_file {
539 TAILQ_ENTRY(drm_file) link;
540 int authenticated;
541 int master;
542 int minor;
543 pid_t pid;
544 uid_t uid;
545 int refs;
546 drm_magic_t magic;
547 unsigned long ioctl_count;
548 void *driver_priv;
549};
550
551typedef struct drm_lock_data {
552 drm_hw_lock_t *hw_lock; /* Hardware lock */
553 DRMFILE filp; /* Unique identifier of holding process (NULL is kernel)*/
554 int lock_queue; /* Queue of blocked processes */
555 unsigned long lock_time; /* Time of last lock in jiffies */
556} drm_lock_data_t;
557
558/* This structure, in the drm_device_t, is always initialized while the device
559 * is open. dev->dma_lock protects the incrementing of dev->buf_use, which
560 * when set marks that no further bufs may be allocated until device teardown
561 * occurs (when the last open of the device has closed). The high/low
562 * watermarks of bufs are only touched by the X Server, and thus not
563 * concurrently accessed, so no locking is needed.
564 */
565typedef struct drm_device_dma {
566 drm_buf_entry_t bufs[DRM_MAX_ORDER+1];
567 int buf_count;
568 drm_buf_t **buflist; /* Vector of pointers info bufs */
569 int seg_count;
570 int page_count;
571 unsigned long *pagelist;
572 unsigned long byte_count;
573 enum {
574 _DRM_DMA_USE_AGP = 0x01,
575 _DRM_DMA_USE_SG = 0x02
576 } flags;
577} drm_device_dma_t;
578
579typedef struct drm_agp_mem {
580 void *handle;
581 unsigned long bound; /* address */
582 int pages;
583 struct drm_agp_mem *prev;
584 struct drm_agp_mem *next;
585} drm_agp_mem_t;
586
587typedef struct drm_agp_head {
588 device_t agpdev;
589 struct agp_info info;
590 const char *chipset;
591 drm_agp_mem_t *memory;
592 unsigned long mode;
593 int enabled;
594 int acquired;
595 unsigned long base;
596 int mtrr;
597 int cant_use_aperture;
598 unsigned long page_mask;
599} drm_agp_head_t;
600
601typedef struct drm_sg_mem {
602 unsigned long handle;
603 void *virtual;
604 int pages;
605 dma_addr_t *busaddr;
606 drm_dma_handle_t *dmah; /* Handle to PCI memory for ATI PCIGART table */
607} drm_sg_mem_t;
608
609typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
610
611typedef struct drm_local_map {
612 unsigned long offset; /* Physical address (0 for SAREA)*/
613 unsigned long size; /* Physical size (bytes) */
614 drm_map_type_t type; /* Type of memory mapped */
615 drm_map_flags_t flags; /* Flags */
616 void *handle; /* User-space: "Handle" to pass to mmap */
617 /* Kernel-space: kernel-virtual address */
618 int mtrr; /* Boolean: MTRR used */
619 /* Private data */
620 int rid; /* PCI resource ID for bus_space */
621 struct resource *bsr;
622 bus_space_tag_t bst;
623 bus_space_handle_t bsh;
624 drm_dma_handle_t *dmah;
625 TAILQ_ENTRY(drm_local_map) link;
626} drm_local_map_t;
627
628TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
629typedef struct drm_vbl_sig {
630 TAILQ_ENTRY(drm_vbl_sig) link;
631 unsigned int sequence;
632 int signo;
633 int pid;
634} drm_vbl_sig_t;
635
636/* location of GART table */
637#define DRM_ATI_GART_MAIN 1
638#define DRM_ATI_GART_FB 2
639
640typedef struct ati_pcigart_info {
641 int gart_table_location;
642 int is_pcie;
643 void *addr;
644 dma_addr_t bus_addr;
645 drm_local_map_t mapping;
646} drm_ati_pcigart_info;
647
648struct drm_driver_info {
649 int (*load)(struct drm_device *, unsigned long flags);
650 int (*firstopen)(struct drm_device *);
651 int (*open)(struct drm_device *, drm_file_t *);
652 void (*preclose)(struct drm_device *, void *filp);
653 void (*postclose)(struct drm_device *, drm_file_t *);
654 void (*lastclose)(struct drm_device *);
655 int (*unload)(struct drm_device *);
656 void (*reclaim_buffers_locked)(struct drm_device *, void *filp);
657 int (*dma_ioctl)(DRM_IOCTL_ARGS);
658 void (*dma_ready)(struct drm_device *);
659 int (*dma_quiescent)(struct drm_device *);
660 int (*dma_flush_block_and_flush)(struct drm_device *, int context,
661 drm_lock_flags_t flags);
662 int (*dma_flush_unblock)(struct drm_device *, int context,
663 drm_lock_flags_t flags);
664 int (*context_ctor)(struct drm_device *dev, int context);
665 int (*context_dtor)(struct drm_device *dev, int context);
666 int (*kernel_context_switch)(struct drm_device *dev, int old,
667 int new);
668 int (*kernel_context_switch_unlock)(struct drm_device *dev);
669 void (*irq_preinstall)(drm_device_t *dev);
670 void (*irq_postinstall)(drm_device_t *dev);
671 void (*irq_uninstall)(drm_device_t *dev);
672 void (*irq_handler)(DRM_IRQ_ARGS);
673 int (*vblank_wait)(drm_device_t *dev, unsigned int *sequence);
674
675 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
676
677 /**
678 * Called by \c drm_device_is_agp. Typically used to determine if a
679 * card is really attached to AGP or not.
680 *
681 * \param dev DRM device handle
682 *
683 * \returns
684 * One of three values is returned depending on whether or not the
685 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
686 * (return of 1), or may or may not be AGP (return of 2).
687 */
688 int (*device_is_agp) (struct drm_device * dev);
689
690 drm_ioctl_desc_t *ioctls;
691 int max_ioctl;
692
693 int buf_priv_size;
694
695 int major;
696 int minor;
697 int patchlevel;
698 const char *name; /* Simple driver name */
699 const char *desc; /* Longer driver name */
700 const char *date; /* Date of last major changes. */
701
702 unsigned use_agp :1;
703 unsigned require_agp :1;
704 unsigned use_sg :1;
705 unsigned use_dma :1;
706 unsigned use_pci_dma :1;
707 unsigned use_dma_queue :1;
708 unsigned use_irq :1;
709 unsigned use_vbl_irq :1;
710 unsigned use_mtrr :1;
711};
712
713/* Length for the array of resource pointers for drm_get_resource_*. */
714#define DRM_MAX_PCI_RESOURCE 3
715
716/**
717 * DRM device functions structure
718 */
719struct drm_device {
720#if defined(__NetBSD__) || defined(__OpenBSD__)
721 struct device device; /* softc is an extension of struct device */
722#endif
723
724 struct drm_driver_info driver;
725 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
726
727 u_int16_t pci_device; /* PCI device id */
728 u_int16_t pci_vendor; /* PCI vendor id */
729
730 char *unique; /* Unique identifier: e.g., busid */
731 int unique_len; /* Length of unique field */
732#ifdef __FreeBSD__
733 device_t device; /* Device instance from newbus */
734#endif
735 struct cdev *devnode; /* Device number for mknod */
736 int if_version; /* Highest interface version set */
737
738 int flags; /* Flags to open(2) */
739
740 /* Locks */
741#if defined(__FreeBSD__) && __FreeBSD_version > 500000
742 struct mtx dma_lock; /* protects dev->dma */
743 struct mtx irq_lock; /* protects irq condition checks */
744 struct mtx dev_lock; /* protects everything else */
745#endif
746 /* Usage Counters */
747 int open_count; /* Outstanding files open */
748 int buf_use; /* Buffers in use -- cannot alloc */
749
750 /* Performance counters */
751 unsigned long counters;
752 drm_stat_type_t types[15];
753 atomic_t counts[15];
754
755 /* Authentication */
756 drm_file_list_t files;
757 drm_magic_head_t magiclist[DRM_HASH_SIZE];
758
759 /* Linked list of mappable regions. Protected by dev_lock */
760 drm_map_list_t maplist;
761
762 drm_local_map_t **context_sareas;
763 int max_context;
764
765 drm_lock_data_t lock; /* Information on hardware lock */
766
767 /* DMA queues (contexts) */
768 drm_device_dma_t *dma; /* Optional pointer for DMA support */
769
770 /* Context support */
771 int irq; /* Interrupt used by board */
772 int irq_enabled; /* True if the irq handler is enabled */
773#ifdef __FreeBSD__
774 int irqrid; /* Interrupt used by board */
775 struct resource *irqr; /* Resource for interrupt used by board */
776#elif defined(__NetBSD__) || defined(__OpenBSD__)
777 struct pci_attach_args pa;
778#endif
779 void *irqh; /* Handle from bus_setup_intr */
780
781 /* Storage of resource pointers for drm_get_resource_* */
782 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
783 int pcirid[DRM_MAX_PCI_RESOURCE];
784
785 int pci_domain;
786 int pci_bus;
787 int pci_slot;
788 int pci_func;
789
790 atomic_t context_flag; /* Context swapping flag */
791 int last_context; /* Last current context */
792 int vbl_queue; /* vbl wait channel */
793 atomic_t vbl_received;
794
795#ifdef __FreeBSD__
796 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
797#elif defined(__NetBSD__)
798 pid_t buf_pgid;
799#endif
800
801 /* Sysctl support */
802 struct drm_sysctl_info *sysctl;
803
804 drm_agp_head_t *agp;
805 drm_sg_mem_t *sg; /* Scatter gather memory */
806 atomic_t *ctx_bitmap;
807 void *dev_private;
808 unsigned int agp_buffer_token;
809 drm_local_map_t *agp_buffer_map;
810};
811
812extern int drm_debug_flag;
813
814/* Device setup support (drm_drv.c) */
815#ifdef __FreeBSD__
816int drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
817int drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
818int drm_detach(device_t nbdev);
819d_ioctl_t drm_ioctl;
820d_open_t drm_open;
821d_close_t drm_close;
822d_read_t drm_read;
823d_poll_t drm_poll;
824d_mmap_t drm_mmap;
825#elif defined(__NetBSD__) || defined(__OpenBSD__)
826int drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
827int drm_attach(struct pci_attach_args *pa, dev_t kdev, drm_pci_id_list_t *idlist);
828dev_type_ioctl(drm_ioctl);
829dev_type_open(drm_open);
830dev_type_close(drm_close);
831dev_type_read(drm_read);
832dev_type_poll(drm_poll);
833dev_type_mmap(drm_mmap);
834#endif
835
836/* File operations helpers (drm_fops.c) */
837#ifdef __FreeBSD__
838extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
839 DRM_STRUCTPROC *p, drm_device_t *dev);
840extern drm_file_t *drm_find_file_by_proc(drm_device_t *dev,
841 DRM_STRUCTPROC *p);
842#elif defined(__NetBSD__) || defined(__OpenBSD__)
843extern int drm_open_helper(dev_t kdev, int flags, int fmt,
844 DRM_STRUCTPROC *p, drm_device_t *dev);
845extern drm_file_t *drm_find_file_by_proc(drm_device_t *dev,
846 DRM_STRUCTPROC *p);
847#endif /* __NetBSD__ || __OpenBSD__ */
848
849/* Memory management support (drm_memory.c) */
850void drm_mem_init(void);
851void drm_mem_uninit(void);
852void *drm_alloc(size_t size, int area);
853void *drm_calloc(size_t nmemb, size_t size, int area);
854void *drm_realloc(void *oldpt, size_t oldsize, size_t size,
855 int area);
856void drm_free(void *pt, size_t size, int area);
857void *drm_ioremap(drm_device_t *dev, drm_local_map_t *map);
858void drm_ioremapfree(drm_local_map_t *map);
859int drm_mtrr_add(unsigned long offset, size_t size, int flags);
860int drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
861
862int drm_context_switch(drm_device_t *dev, int old, int new);
863int drm_context_switch_complete(drm_device_t *dev, int new);
864
865int drm_ctxbitmap_init(drm_device_t *dev);
866void drm_ctxbitmap_cleanup(drm_device_t *dev);
867void drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle);
868int drm_ctxbitmap_next(drm_device_t *dev);
869
870/* Locking IOCTL support (drm_lock.c) */
871int drm_lock_take(__volatile__ unsigned int *lock,
872 unsigned int context);
873int drm_lock_transfer(drm_device_t *dev,
874 __volatile__ unsigned int *lock,
875 unsigned int context);
876int drm_lock_free(drm_device_t *dev,
877 __volatile__ unsigned int *lock,
878 unsigned int context);
879
880/* Buffer management support (drm_bufs.c) */
881unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource);
882unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource);
883void drm_rmmap(drm_device_t *dev, drm_local_map_t *map);
884int drm_order(unsigned long size);
885int drm_addmap(drm_device_t * dev, unsigned long offset, unsigned long size,
886 drm_map_type_t type, drm_map_flags_t flags,
887 drm_local_map_t **map_ptr);
888int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request);
889int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request);
890int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request);
891
892/* DMA support (drm_dma.c) */
893int drm_dma_setup(drm_device_t *dev);
894void drm_dma_takedown(drm_device_t *dev);
895void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf);
896void drm_reclaim_buffers(drm_device_t *dev, DRMFILE filp);
897#define drm_core_reclaim_buffers drm_reclaim_buffers
898
899/* IRQ support (drm_irq.c) */
900int drm_irq_install(drm_device_t *dev);
901int drm_irq_uninstall(drm_device_t *dev);
902irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
903void drm_driver_irq_preinstall(drm_device_t *dev);
904void drm_driver_irq_postinstall(drm_device_t *dev);
905void drm_driver_irq_uninstall(drm_device_t *dev);
906int drm_vblank_wait(drm_device_t *dev, unsigned int *vbl_seq);
907void drm_vbl_send_signals(drm_device_t *dev);
908
909/* AGP/PCI Express/GART support (drm_agpsupport.c) */
910int drm_device_is_agp(drm_device_t *dev);
911int drm_device_is_pcie(drm_device_t *dev);
912drm_agp_head_t *drm_agp_init(void);
913int drm_agp_acquire(drm_device_t *dev);
914int drm_agp_release(drm_device_t *dev);
915int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info);
916int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode);
917void *drm_agp_allocate_memory(size_t pages, u32 type);
918int drm_agp_free_memory(void *handle);
919int drm_agp_bind_memory(void *handle, off_t start);
920int drm_agp_unbind_memory(void *handle);
921int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request);
922int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request);
923int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request);
924int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request);
925
926/* Scatter Gather Support (drm_scatter.c) */
927void drm_sg_cleanup(drm_sg_mem_t *entry);
928
929#ifdef __FreeBSD__
930/* sysctl support (drm_sysctl.h) */
931extern int drm_sysctl_init(drm_device_t *dev);
932extern int drm_sysctl_cleanup(drm_device_t *dev);
933#endif /* __FreeBSD__ */
934
935/* ATI PCIGART support (ati_pcigart.c) */
936int drm_ati_pcigart_init(drm_device_t *dev,
937 drm_ati_pcigart_info *gart_info);
938int drm_ati_pcigart_cleanup(drm_device_t *dev,
939 drm_ati_pcigart_info *gart_info);
940
941/* Locking IOCTL support (drm_drv.c) */
942int drm_lock(DRM_IOCTL_ARGS);
943int drm_unlock(DRM_IOCTL_ARGS);
944int drm_version(DRM_IOCTL_ARGS);
945int drm_setversion(DRM_IOCTL_ARGS);
946
947/* Misc. IOCTL support (drm_ioctl.c) */
948int drm_irq_by_busid(DRM_IOCTL_ARGS);
949int drm_getunique(DRM_IOCTL_ARGS);
950int drm_setunique(DRM_IOCTL_ARGS);
951int drm_getmap(DRM_IOCTL_ARGS);
952int drm_getclient(DRM_IOCTL_ARGS);
953int drm_getstats(DRM_IOCTL_ARGS);
954int drm_noop(DRM_IOCTL_ARGS);
955
956/* Context IOCTL support (drm_context.c) */
957int drm_resctx(DRM_IOCTL_ARGS);
958int drm_addctx(DRM_IOCTL_ARGS);
959int drm_modctx(DRM_IOCTL_ARGS);
960int drm_getctx(DRM_IOCTL_ARGS);
961int drm_switchctx(DRM_IOCTL_ARGS);
962int drm_newctx(DRM_IOCTL_ARGS);
963int drm_rmctx(DRM_IOCTL_ARGS);
964int drm_setsareactx(DRM_IOCTL_ARGS);
965int drm_getsareactx(DRM_IOCTL_ARGS);
966
967/* Drawable IOCTL support (drm_drawable.c) */
968int drm_adddraw(DRM_IOCTL_ARGS);
969int drm_rmdraw(DRM_IOCTL_ARGS);
970
971/* Authentication IOCTL support (drm_auth.c) */
972int drm_getmagic(DRM_IOCTL_ARGS);
973int drm_authmagic(DRM_IOCTL_ARGS);
974
975/* Buffer management support (drm_bufs.c) */
976int drm_addmap_ioctl(DRM_IOCTL_ARGS);
977int drm_rmmap_ioctl(DRM_IOCTL_ARGS);
978int drm_addbufs_ioctl(DRM_IOCTL_ARGS);
979int drm_infobufs(DRM_IOCTL_ARGS);
980int drm_markbufs(DRM_IOCTL_ARGS);
981int drm_freebufs(DRM_IOCTL_ARGS);
982int drm_mapbufs(DRM_IOCTL_ARGS);
983
984/* DMA support (drm_dma.c) */
985int drm_dma(DRM_IOCTL_ARGS);
986
987/* IRQ support (drm_irq.c) */
988int drm_control(DRM_IOCTL_ARGS);
989int drm_wait_vblank(DRM_IOCTL_ARGS);
990
991/* AGP/GART support (drm_agpsupport.c) */
992int drm_agp_acquire_ioctl(DRM_IOCTL_ARGS);
993int drm_agp_release_ioctl(DRM_IOCTL_ARGS);
994int drm_agp_enable_ioctl(DRM_IOCTL_ARGS);
995int drm_agp_info_ioctl(DRM_IOCTL_ARGS);
996int drm_agp_alloc_ioctl(DRM_IOCTL_ARGS);
997int drm_agp_free_ioctl(DRM_IOCTL_ARGS);
998int drm_agp_unbind_ioctl(DRM_IOCTL_ARGS);
999int drm_agp_bind_ioctl(DRM_IOCTL_ARGS);
1000
1001/* Scatter Gather Support (drm_scatter.c) */
1002int drm_sg_alloc(DRM_IOCTL_ARGS);
1003int drm_sg_free(DRM_IOCTL_ARGS);
1004
1005/* consistent PCI memory functions (drm_pci.c) */
1006drm_dma_handle_t *drm_pci_alloc(drm_device_t *dev, size_t size, size_t align,
1007 dma_addr_t maxaddr);
1008void drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah);
1009
1010/* Inline replacements for DRM_IOREMAP macros */
1011static __inline__ void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1012{
1013 map->handle = drm_ioremap(dev, map);
1014}
1015static __inline__ void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1016{
1017 if ( map->handle && map->size )
1018 drm_ioremapfree(map);
1019}
1020
1021static __inline__ struct drm_local_map *drm_core_findmap(struct drm_device *dev, unsigned long offset)
1022{
1023 drm_local_map_t *map;
1024
1025 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1026 TAILQ_FOREACH(map, &dev->maplist, link) {
1027 if (map->offset == offset)
1028 return map;
1029 }
1030 return NULL;
1031}
1032
1033static __inline__ void drm_core_dropmap(struct drm_map *map)
1034{
1035}
1036
1037#endif /* __KERNEL__ */
1038#endif /* _DRM_P_H_ */
85#include <sys/agpio.h>
86#if __FreeBSD_version >= 500000
87#include <sys/mutex.h>
88#include <dev/pci/pcivar.h>
89#include <sys/selinfo.h>
90#else /* __FreeBSD_version >= 500000 */
91#include <pci/pcivar.h>
92#include <sys/select.h>
93#endif /* __FreeBSD_version < 500000 */
94#elif defined(__NetBSD__)
95#include <machine/mtrr.h>
96#include <sys/vnode.h>
97#include <sys/select.h>
98#include <sys/device.h>
99#include <sys/resourcevar.h>
100#include <sys/lkm.h>
101#include <sys/agpio.h>
102#include <sys/ttycom.h>
103#include <uvm/uvm.h>
104#include <dev/pci/pcireg.h>
105#include <dev/pci/pcivar.h>
106#include <dev/pci/agpvar.h>
107#elif defined(__OpenBSD__)
108#include <sys/lkm.h>
109#include <uvm/uvm.h>
110#endif
111#include <sys/bus.h>
112
113#include "dev/drm/drm.h"
114#include "dev/drm/drm_linux_list.h"
115#include "dev/drm/drm_atomic.h"
116
117#ifdef __FreeBSD__
118#include <opt_drm.h>
119#ifdef DRM_DEBUG
120#undef DRM_DEBUG
121#define DRM_DEBUG_DEFAULT_ON 1
122#endif /* DRM_DEBUG */
123#endif
124
125#if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
126#include <sys/file.h>
127#include <sys/proc.h>
128#include <machine/../linux/linux.h>
129#include <machine/../linux/linux_proto.h>
130#else
131/* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
132 * supported on this OS yet.
133 */
134#undef DRM_LINUX
135#define DRM_LINUX 0
136#endif
137
138#define DRM_HASH_SIZE 16 /* Size of key hash table */
139#define DRM_KERNEL_CONTEXT 0 /* Change drm_resctx if changed */
140#define DRM_RESERVED_CONTEXTS 1 /* Change drm_resctx if changed */
141
142#define DRM_MEM_DMA 0
143#define DRM_MEM_SAREA 1
144#define DRM_MEM_DRIVER 2
145#define DRM_MEM_MAGIC 3
146#define DRM_MEM_IOCTLS 4
147#define DRM_MEM_MAPS 5
148#define DRM_MEM_BUFS 6
149#define DRM_MEM_SEGS 7
150#define DRM_MEM_PAGES 8
151#define DRM_MEM_FILES 9
152#define DRM_MEM_QUEUES 10
153#define DRM_MEM_CMDS 11
154#define DRM_MEM_MAPPINGS 12
155#define DRM_MEM_BUFLISTS 13
156#define DRM_MEM_AGPLISTS 14
157#define DRM_MEM_TOTALAGP 15
158#define DRM_MEM_BOUNDAGP 16
159#define DRM_MEM_CTXBITMAP 17
160#define DRM_MEM_STUB 18
161#define DRM_MEM_SGLISTS 19
162
163#define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
164
165 /* Internal types and structures */
166#define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
167#define DRM_MIN(a,b) ((a)<(b)?(a):(b))
168#define DRM_MAX(a,b) ((a)>(b)?(a):(b))
169
170#define DRM_IF_VERSION(maj, min) (maj << 16 | min)
171
172MALLOC_DECLARE(M_DRM);
173
174#define __OS_HAS_AGP 1
175
176#define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
177#define DRM_DEV_UID 0
178#define DRM_DEV_GID 0
179
180#define wait_queue_head_t atomic_t
181#define DRM_WAKEUP(w) wakeup((void *)w)
182#define DRM_WAKEUP_INT(w) wakeup(w)
183#define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
184
185#if defined(__FreeBSD__) && __FreeBSD_version < 502109
186#define bus_alloc_resource_any(dev, type, rid, flags) \
187 bus_alloc_resource(dev, type, rid, 0ul, ~0ul, 1, flags)
188#endif
189
190#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
191#define DRM_CURPROC curthread
192#define DRM_STRUCTPROC struct thread
193#define DRM_SPINTYPE struct mtx
194#define DRM_SPININIT(l,name) mtx_init(&l, name, NULL, MTX_DEF)
195#define DRM_SPINUNINIT(l) mtx_destroy(&l)
196#define DRM_SPINLOCK(l) mtx_lock(l)
197#define DRM_SPINUNLOCK(u) mtx_unlock(u);
198#define DRM_SPINLOCK_ASSERT(l) mtx_assert(l, MA_OWNED)
199#define DRM_CURRENTPID curthread->td_proc->p_pid
200#define DRM_LOCK() mtx_lock(&dev->dev_lock)
201#define DRM_UNLOCK() mtx_unlock(&dev->dev_lock)
202#define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
203#else /* __FreeBSD__ && __FreeBSD_version >= 500000 */
204#define DRM_CURPROC curproc
205#define DRM_STRUCTPROC struct proc
206#define DRM_SPINTYPE struct simplelock
207#define DRM_SPININIT(l,name)
208#define DRM_SPINUNINIT(l)
209#define DRM_SPINLOCK(l)
210#define DRM_SPINUNLOCK(u)
211#define DRM_SPINLOCK_ASSERT(l)
212#define DRM_CURRENTPID curproc->p_pid
213#define DRM_LOCK()
214#define DRM_UNLOCK()
215#define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
216#define spldrm() spltty()
217#endif /* __NetBSD__ || __OpenBSD__ */
218
219/* Currently our DRMFILE (filp) is a void * which is actually the pid
220 * of the current process. It should be a per-open unique pointer, but
221 * code for that is not yet written */
222#define DRMFILE void *
223#define DRM_IRQ_ARGS void *arg
224typedef void irqreturn_t;
225#define IRQ_HANDLED /* nothing */
226#define IRQ_NONE /* nothing */
227
228enum {
229 DRM_IS_NOT_AGP,
230 DRM_IS_AGP,
231 DRM_MIGHT_BE_AGP
232};
233#define DRM_AGP_MEM struct agp_memory_info
234
235#if defined(__FreeBSD__)
236#define DRM_DEVICE \
237 drm_device_t *dev = kdev->si_drv1
238#define DRM_IOCTL_ARGS struct cdev *kdev, u_long cmd, caddr_t data, \
239 int flags, DRM_STRUCTPROC *p, DRMFILE filp
240
241#define PAGE_ALIGN(addr) round_page(addr)
242/* DRM_SUSER returns true if the user is superuser */
243#if __FreeBSD_version >= 700000
244#define DRM_SUSER(p) (priv_check(p, PRIV_DRIVER) == 0)
245#else
246#define DRM_SUSER(p) (suser(p) == 0)
247#endif
248#define DRM_AGP_FIND_DEVICE() agp_find_device()
249#define DRM_MTRR_WC MDF_WRITECOMBINE
250#define jiffies ticks
251
252#else /* __FreeBSD__ */
253
254#if defined(__NetBSD__)
255#define DRM_DEVICE \
256 drm_device_t *dev = device_lookup(&drm_cd, minor(kdev))
257#elif defined(__OpenBSD__)
258#define DRM_DEVICE \
259 drm_device_t *dev = (device_lookup(&drm_cd, \
260 minor(kdev)))->dv_cfdata->cf_driver->cd_devs[minor(kdev)]
261#endif /* __OpenBSD__ */
262#define DRM_IOCTL_ARGS dev_t kdev, u_long cmd, caddr_t data, \
263 int flags, DRM_STRUCTPROC *p, DRMFILE filp
264
265#define CDEV_MAJOR 34
266#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
267/* DRM_SUSER returns true if the user is superuser */
268#define DRM_SUSER(p) (suser(p->p_ucred, &p->p_acflag) == 0)
269#define DRM_AGP_FIND_DEVICE() agp_find_device(0)
270#define DRM_MTRR_WC MTRR_TYPE_WC
271#define jiffies hardclock_ticks
272
273typedef drm_device_t *device_t;
274extern struct cfdriver drm_cd;
275#endif /* !__FreeBSD__ */
276
277/* Capabilities taken from src/sys/dev/pci/pcireg.h. */
278#ifndef PCIY_AGP
279#define PCIY_AGP 0x02
280#endif
281
282#ifndef PCIY_EXPRESS
283#define PCIY_EXPRESS 0x10
284#endif
285
286typedef unsigned long dma_addr_t;
287typedef u_int64_t u64;
288typedef u_int32_t u32;
289typedef u_int16_t u16;
290typedef u_int8_t u8;
291
292/* DRM_READMEMORYBARRIER() prevents reordering of reads.
293 * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
294 * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
295 */
296#if defined(__i386__)
297#define DRM_READMEMORYBARRIER() __asm __volatile( \
298 "lock; addl $0,0(%%esp)" : : : "memory");
299#define DRM_WRITEMEMORYBARRIER() __asm __volatile("" : : : "memory");
300#define DRM_MEMORYBARRIER() __asm __volatile( \
301 "lock; addl $0,0(%%esp)" : : : "memory");
302#elif defined(__alpha__)
303#define DRM_READMEMORYBARRIER() alpha_mb();
304#define DRM_WRITEMEMORYBARRIER() alpha_wmb();
305#define DRM_MEMORYBARRIER() alpha_mb();
306#elif defined(__amd64__)
307#define DRM_READMEMORYBARRIER() __asm __volatile( \
308 "lock; addl $0,0(%%rsp)" : : : "memory");
309#define DRM_WRITEMEMORYBARRIER() __asm __volatile("" : : : "memory");
310#define DRM_MEMORYBARRIER() __asm __volatile( \
311 "lock; addl $0,0(%%rsp)" : : : "memory");
312#endif
313
314#ifdef __FreeBSD__
315#define DRM_READ8(map, offset) \
316 *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
317#define DRM_READ16(map, offset) \
318 *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
319#define DRM_READ32(map, offset) \
320 *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
321#define DRM_WRITE8(map, offset, val) \
322 *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
323#define DRM_WRITE16(map, offset, val) \
324 *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
325#define DRM_WRITE32(map, offset, val) \
326 *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
327
328#define DRM_VERIFYAREA_READ( uaddr, size ) \
329 (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
330
331#else /* __FreeBSD__ */
332
333typedef vaddr_t vm_offset_t;
334
335#define DRM_READ8(map, offset) \
336 bus_space_read_1( (map)->bst, (map)->bsh, (offset))
337#define DRM_READ16(map, offset) \
338 bus_space_read_2( (map)->bst, (map)->bsh, (offset))
339#define DRM_READ32(map, offset) \
340 bus_space_read_4( (map)->bst, (map)->bsh, (offset))
341#define DRM_WRITE8(map, offset, val) \
342 bus_space_write_1((map)->bst, (map)->bsh, (offset), (val))
343#define DRM_WRITE16(map, offset, val) \
344 bus_space_write_2((map)->bst, (map)->bsh, (offset), (val))
345#define DRM_WRITE32(map, offset, val) \
346 bus_space_write_4((map)->bst, (map)->bsh, (offset), (val))
347
348#define DRM_VERIFYAREA_READ( uaddr, size ) \
349 (!uvm_useracc((caddr_t)uaddr, size, VM_PROT_READ))
350#endif /* !__FreeBSD__ */
351
352#define DRM_COPY_TO_USER_IOCTL(user, kern, size) \
353 if ( IOCPARM_LEN(cmd) != size) \
354 return EINVAL; \
355 *user = kern;
356#define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
357 if ( IOCPARM_LEN(cmd) != size) \
358 return EINVAL; \
359 kern = *user;
360#define DRM_COPY_TO_USER(user, kern, size) \
361 copyout(kern, user, size)
362#define DRM_COPY_FROM_USER(kern, user, size) \
363 copyin(user, kern, size)
364#define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) \
365 copyin(arg2, arg1, arg3)
366#define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3) \
367 copyout(arg2, arg1, arg3)
368#if __FreeBSD_version > 500000
369#define DRM_GET_USER_UNCHECKED(val, uaddr) \
370 ((val) = fuword32(uaddr), 0)
371#else
372#define DRM_GET_USER_UNCHECKED(val, uaddr) \
373 ((val) = fuword(uaddr), 0)
374#endif
375
376#define cpu_to_le32(x) htole32(x)
377#define le32_to_cpu(x) le32toh(x)
378
379#define DRM_ERR(v) v
380#define DRM_HZ hz
381#define DRM_UDELAY(udelay) DELAY(udelay)
382#define DRM_TIME_SLICE (hz/20) /* Time slice for GLXContexts */
383
384#define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do { \
385 (_map) = (_dev)->context_sareas[_ctx]; \
386} while(0)
387
388#define DRM_GET_PRIV_WITH_RETURN(_priv, _filp) \
389do { \
390 if (_filp != (DRMFILE)(intptr_t)DRM_CURRENTPID) { \
391 DRM_ERROR("filp doesn't match curproc\n"); \
392 return EINVAL; \
393 } \
394 _priv = drm_find_file_by_proc(dev, DRM_CURPROC); \
395 if (_priv == NULL) { \
396 DRM_ERROR("can't find authenticator\n"); \
397 return EINVAL; \
398 } \
399} while (0)
400
401#define LOCK_TEST_WITH_RETURN(dev, filp) \
402do { \
403 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || \
404 dev->lock.filp != filp) { \
405 DRM_ERROR("%s called without lock held\n", \
406 __FUNCTION__); \
407 return EINVAL; \
408 } \
409} while (0)
410
411#define DRM_GETSAREA() \
412do { \
413 drm_local_map_t *map; \
414 DRM_SPINLOCK_ASSERT(&dev->dev_lock); \
415 TAILQ_FOREACH(map, &dev->maplist, link) { \
416 if (map->type == _DRM_SHM && \
417 map->flags & _DRM_CONTAINS_LOCK) { \
418 dev_priv->sarea = map; \
419 break; \
420 } \
421 } \
422} while (0)
423
424#if defined(__FreeBSD__) && __FreeBSD_version > 500000
425#define DRM_WAIT_ON( ret, queue, timeout, condition ) \
426for ( ret = 0 ; !ret && !(condition) ; ) { \
427 DRM_UNLOCK(); \
428 mtx_lock(&dev->irq_lock); \
429 if (!(condition)) \
430 ret = msleep(&(queue), &dev->irq_lock, \
431 PZERO | PCATCH, "drmwtq", (timeout)); \
432 mtx_unlock(&dev->irq_lock); \
433 DRM_LOCK(); \
434}
435#else
436#define DRM_WAIT_ON( ret, queue, timeout, condition ) \
437for ( ret = 0 ; !ret && !(condition) ; ) { \
438 int s = spldrm(); \
439 if (!(condition)) \
440 ret = tsleep( &(queue), PZERO | PCATCH, \
441 "drmwtq", (timeout) ); \
442 splx(s); \
443}
444#endif
445
446#define DRM_ERROR(fmt, arg...) \
447 printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt, \
448 DRM_CURRENTPID, __func__ , ## arg)
449
450#define DRM_INFO(fmt, arg...) printf("info: [" DRM_NAME "] " fmt , ## arg)
451
452#define DRM_DEBUG(fmt, arg...) do { \
453 if (drm_debug_flag) \
454 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
455 __func__ , ## arg); \
456} while (0)
457
458typedef struct drm_pci_id_list
459{
460 int vendor;
461 int device;
462 long driver_private;
463 char *name;
464} drm_pci_id_list_t;
465
466#define DRM_AUTH 0x1
467#define DRM_MASTER 0x2
468#define DRM_ROOT_ONLY 0x4
469typedef struct drm_ioctl_desc {
470 int (*func)(DRM_IOCTL_ARGS);
471 int flags;
472} drm_ioctl_desc_t;
473
474typedef struct drm_magic_entry {
475 drm_magic_t magic;
476 struct drm_file *priv;
477 struct drm_magic_entry *next;
478} drm_magic_entry_t;
479
480typedef struct drm_magic_head {
481 struct drm_magic_entry *head;
482 struct drm_magic_entry *tail;
483} drm_magic_head_t;
484
485typedef struct drm_buf {
486 int idx; /* Index into master buflist */
487 int total; /* Buffer size */
488 int order; /* log-base-2(total) */
489 int used; /* Amount of buffer in use (for DMA) */
490 unsigned long offset; /* Byte offset (used internally) */
491 void *address; /* Address of buffer */
492 unsigned long bus_address; /* Bus address of buffer */
493 struct drm_buf *next; /* Kernel-only: used for free list */
494 __volatile__ int pending; /* On hardware DMA queue */
495 DRMFILE filp; /* Unique identifier of holding process */
496 int context; /* Kernel queue for this buffer */
497 enum {
498 DRM_LIST_NONE = 0,
499 DRM_LIST_FREE = 1,
500 DRM_LIST_WAIT = 2,
501 DRM_LIST_PEND = 3,
502 DRM_LIST_PRIO = 4,
503 DRM_LIST_RECLAIM = 5
504 } list; /* Which list we're on */
505
506 int dev_priv_size; /* Size of buffer private stoarge */
507 void *dev_private; /* Per-buffer private storage */
508} drm_buf_t;
509
510typedef struct drm_freelist {
511 int initialized; /* Freelist in use */
512 atomic_t count; /* Number of free buffers */
513 drm_buf_t *next; /* End pointer */
514
515 int low_mark; /* Low water mark */
516 int high_mark; /* High water mark */
517} drm_freelist_t;
518
519typedef struct drm_dma_handle {
520 void *vaddr;
521 bus_addr_t busaddr;
522#if defined(__FreeBSD__)
523 bus_dma_tag_t tag;
524 bus_dmamap_t map;
525#elif defined(__NetBSD__)
526 bus_dma_segment_t seg;
527#endif
528} drm_dma_handle_t;
529
530typedef struct drm_buf_entry {
531 int buf_size;
532 int buf_count;
533 drm_buf_t *buflist;
534 int seg_count;
535 drm_dma_handle_t **seglist;
536 int page_order;
537
538 drm_freelist_t freelist;
539} drm_buf_entry_t;
540
541typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
542struct drm_file {
543 TAILQ_ENTRY(drm_file) link;
544 int authenticated;
545 int master;
546 int minor;
547 pid_t pid;
548 uid_t uid;
549 int refs;
550 drm_magic_t magic;
551 unsigned long ioctl_count;
552 void *driver_priv;
553};
554
555typedef struct drm_lock_data {
556 drm_hw_lock_t *hw_lock; /* Hardware lock */
557 DRMFILE filp; /* Unique identifier of holding process (NULL is kernel)*/
558 int lock_queue; /* Queue of blocked processes */
559 unsigned long lock_time; /* Time of last lock in jiffies */
560} drm_lock_data_t;
561
562/* This structure, in the drm_device_t, is always initialized while the device
563 * is open. dev->dma_lock protects the incrementing of dev->buf_use, which
564 * when set marks that no further bufs may be allocated until device teardown
565 * occurs (when the last open of the device has closed). The high/low
566 * watermarks of bufs are only touched by the X Server, and thus not
567 * concurrently accessed, so no locking is needed.
568 */
569typedef struct drm_device_dma {
570 drm_buf_entry_t bufs[DRM_MAX_ORDER+1];
571 int buf_count;
572 drm_buf_t **buflist; /* Vector of pointers info bufs */
573 int seg_count;
574 int page_count;
575 unsigned long *pagelist;
576 unsigned long byte_count;
577 enum {
578 _DRM_DMA_USE_AGP = 0x01,
579 _DRM_DMA_USE_SG = 0x02
580 } flags;
581} drm_device_dma_t;
582
583typedef struct drm_agp_mem {
584 void *handle;
585 unsigned long bound; /* address */
586 int pages;
587 struct drm_agp_mem *prev;
588 struct drm_agp_mem *next;
589} drm_agp_mem_t;
590
591typedef struct drm_agp_head {
592 device_t agpdev;
593 struct agp_info info;
594 const char *chipset;
595 drm_agp_mem_t *memory;
596 unsigned long mode;
597 int enabled;
598 int acquired;
599 unsigned long base;
600 int mtrr;
601 int cant_use_aperture;
602 unsigned long page_mask;
603} drm_agp_head_t;
604
605typedef struct drm_sg_mem {
606 unsigned long handle;
607 void *virtual;
608 int pages;
609 dma_addr_t *busaddr;
610 drm_dma_handle_t *dmah; /* Handle to PCI memory for ATI PCIGART table */
611} drm_sg_mem_t;
612
613typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
614
615typedef struct drm_local_map {
616 unsigned long offset; /* Physical address (0 for SAREA)*/
617 unsigned long size; /* Physical size (bytes) */
618 drm_map_type_t type; /* Type of memory mapped */
619 drm_map_flags_t flags; /* Flags */
620 void *handle; /* User-space: "Handle" to pass to mmap */
621 /* Kernel-space: kernel-virtual address */
622 int mtrr; /* Boolean: MTRR used */
623 /* Private data */
624 int rid; /* PCI resource ID for bus_space */
625 struct resource *bsr;
626 bus_space_tag_t bst;
627 bus_space_handle_t bsh;
628 drm_dma_handle_t *dmah;
629 TAILQ_ENTRY(drm_local_map) link;
630} drm_local_map_t;
631
632TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
633typedef struct drm_vbl_sig {
634 TAILQ_ENTRY(drm_vbl_sig) link;
635 unsigned int sequence;
636 int signo;
637 int pid;
638} drm_vbl_sig_t;
639
640/* location of GART table */
641#define DRM_ATI_GART_MAIN 1
642#define DRM_ATI_GART_FB 2
643
644typedef struct ati_pcigart_info {
645 int gart_table_location;
646 int is_pcie;
647 void *addr;
648 dma_addr_t bus_addr;
649 drm_local_map_t mapping;
650} drm_ati_pcigart_info;
651
652struct drm_driver_info {
653 int (*load)(struct drm_device *, unsigned long flags);
654 int (*firstopen)(struct drm_device *);
655 int (*open)(struct drm_device *, drm_file_t *);
656 void (*preclose)(struct drm_device *, void *filp);
657 void (*postclose)(struct drm_device *, drm_file_t *);
658 void (*lastclose)(struct drm_device *);
659 int (*unload)(struct drm_device *);
660 void (*reclaim_buffers_locked)(struct drm_device *, void *filp);
661 int (*dma_ioctl)(DRM_IOCTL_ARGS);
662 void (*dma_ready)(struct drm_device *);
663 int (*dma_quiescent)(struct drm_device *);
664 int (*dma_flush_block_and_flush)(struct drm_device *, int context,
665 drm_lock_flags_t flags);
666 int (*dma_flush_unblock)(struct drm_device *, int context,
667 drm_lock_flags_t flags);
668 int (*context_ctor)(struct drm_device *dev, int context);
669 int (*context_dtor)(struct drm_device *dev, int context);
670 int (*kernel_context_switch)(struct drm_device *dev, int old,
671 int new);
672 int (*kernel_context_switch_unlock)(struct drm_device *dev);
673 void (*irq_preinstall)(drm_device_t *dev);
674 void (*irq_postinstall)(drm_device_t *dev);
675 void (*irq_uninstall)(drm_device_t *dev);
676 void (*irq_handler)(DRM_IRQ_ARGS);
677 int (*vblank_wait)(drm_device_t *dev, unsigned int *sequence);
678
679 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
680
681 /**
682 * Called by \c drm_device_is_agp. Typically used to determine if a
683 * card is really attached to AGP or not.
684 *
685 * \param dev DRM device handle
686 *
687 * \returns
688 * One of three values is returned depending on whether or not the
689 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
690 * (return of 1), or may or may not be AGP (return of 2).
691 */
692 int (*device_is_agp) (struct drm_device * dev);
693
694 drm_ioctl_desc_t *ioctls;
695 int max_ioctl;
696
697 int buf_priv_size;
698
699 int major;
700 int minor;
701 int patchlevel;
702 const char *name; /* Simple driver name */
703 const char *desc; /* Longer driver name */
704 const char *date; /* Date of last major changes. */
705
706 unsigned use_agp :1;
707 unsigned require_agp :1;
708 unsigned use_sg :1;
709 unsigned use_dma :1;
710 unsigned use_pci_dma :1;
711 unsigned use_dma_queue :1;
712 unsigned use_irq :1;
713 unsigned use_vbl_irq :1;
714 unsigned use_mtrr :1;
715};
716
717/* Length for the array of resource pointers for drm_get_resource_*. */
718#define DRM_MAX_PCI_RESOURCE 3
719
720/**
721 * DRM device functions structure
722 */
723struct drm_device {
724#if defined(__NetBSD__) || defined(__OpenBSD__)
725 struct device device; /* softc is an extension of struct device */
726#endif
727
728 struct drm_driver_info driver;
729 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
730
731 u_int16_t pci_device; /* PCI device id */
732 u_int16_t pci_vendor; /* PCI vendor id */
733
734 char *unique; /* Unique identifier: e.g., busid */
735 int unique_len; /* Length of unique field */
736#ifdef __FreeBSD__
737 device_t device; /* Device instance from newbus */
738#endif
739 struct cdev *devnode; /* Device number for mknod */
740 int if_version; /* Highest interface version set */
741
742 int flags; /* Flags to open(2) */
743
744 /* Locks */
745#if defined(__FreeBSD__) && __FreeBSD_version > 500000
746 struct mtx dma_lock; /* protects dev->dma */
747 struct mtx irq_lock; /* protects irq condition checks */
748 struct mtx dev_lock; /* protects everything else */
749#endif
750 /* Usage Counters */
751 int open_count; /* Outstanding files open */
752 int buf_use; /* Buffers in use -- cannot alloc */
753
754 /* Performance counters */
755 unsigned long counters;
756 drm_stat_type_t types[15];
757 atomic_t counts[15];
758
759 /* Authentication */
760 drm_file_list_t files;
761 drm_magic_head_t magiclist[DRM_HASH_SIZE];
762
763 /* Linked list of mappable regions. Protected by dev_lock */
764 drm_map_list_t maplist;
765
766 drm_local_map_t **context_sareas;
767 int max_context;
768
769 drm_lock_data_t lock; /* Information on hardware lock */
770
771 /* DMA queues (contexts) */
772 drm_device_dma_t *dma; /* Optional pointer for DMA support */
773
774 /* Context support */
775 int irq; /* Interrupt used by board */
776 int irq_enabled; /* True if the irq handler is enabled */
777#ifdef __FreeBSD__
778 int irqrid; /* Interrupt used by board */
779 struct resource *irqr; /* Resource for interrupt used by board */
780#elif defined(__NetBSD__) || defined(__OpenBSD__)
781 struct pci_attach_args pa;
782#endif
783 void *irqh; /* Handle from bus_setup_intr */
784
785 /* Storage of resource pointers for drm_get_resource_* */
786 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
787 int pcirid[DRM_MAX_PCI_RESOURCE];
788
789 int pci_domain;
790 int pci_bus;
791 int pci_slot;
792 int pci_func;
793
794 atomic_t context_flag; /* Context swapping flag */
795 int last_context; /* Last current context */
796 int vbl_queue; /* vbl wait channel */
797 atomic_t vbl_received;
798
799#ifdef __FreeBSD__
800 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
801#elif defined(__NetBSD__)
802 pid_t buf_pgid;
803#endif
804
805 /* Sysctl support */
806 struct drm_sysctl_info *sysctl;
807
808 drm_agp_head_t *agp;
809 drm_sg_mem_t *sg; /* Scatter gather memory */
810 atomic_t *ctx_bitmap;
811 void *dev_private;
812 unsigned int agp_buffer_token;
813 drm_local_map_t *agp_buffer_map;
814};
815
816extern int drm_debug_flag;
817
818/* Device setup support (drm_drv.c) */
819#ifdef __FreeBSD__
820int drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
821int drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
822int drm_detach(device_t nbdev);
823d_ioctl_t drm_ioctl;
824d_open_t drm_open;
825d_close_t drm_close;
826d_read_t drm_read;
827d_poll_t drm_poll;
828d_mmap_t drm_mmap;
829#elif defined(__NetBSD__) || defined(__OpenBSD__)
830int drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
831int drm_attach(struct pci_attach_args *pa, dev_t kdev, drm_pci_id_list_t *idlist);
832dev_type_ioctl(drm_ioctl);
833dev_type_open(drm_open);
834dev_type_close(drm_close);
835dev_type_read(drm_read);
836dev_type_poll(drm_poll);
837dev_type_mmap(drm_mmap);
838#endif
839
840/* File operations helpers (drm_fops.c) */
841#ifdef __FreeBSD__
842extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
843 DRM_STRUCTPROC *p, drm_device_t *dev);
844extern drm_file_t *drm_find_file_by_proc(drm_device_t *dev,
845 DRM_STRUCTPROC *p);
846#elif defined(__NetBSD__) || defined(__OpenBSD__)
847extern int drm_open_helper(dev_t kdev, int flags, int fmt,
848 DRM_STRUCTPROC *p, drm_device_t *dev);
849extern drm_file_t *drm_find_file_by_proc(drm_device_t *dev,
850 DRM_STRUCTPROC *p);
851#endif /* __NetBSD__ || __OpenBSD__ */
852
853/* Memory management support (drm_memory.c) */
854void drm_mem_init(void);
855void drm_mem_uninit(void);
856void *drm_alloc(size_t size, int area);
857void *drm_calloc(size_t nmemb, size_t size, int area);
858void *drm_realloc(void *oldpt, size_t oldsize, size_t size,
859 int area);
860void drm_free(void *pt, size_t size, int area);
861void *drm_ioremap(drm_device_t *dev, drm_local_map_t *map);
862void drm_ioremapfree(drm_local_map_t *map);
863int drm_mtrr_add(unsigned long offset, size_t size, int flags);
864int drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
865
866int drm_context_switch(drm_device_t *dev, int old, int new);
867int drm_context_switch_complete(drm_device_t *dev, int new);
868
869int drm_ctxbitmap_init(drm_device_t *dev);
870void drm_ctxbitmap_cleanup(drm_device_t *dev);
871void drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle);
872int drm_ctxbitmap_next(drm_device_t *dev);
873
874/* Locking IOCTL support (drm_lock.c) */
875int drm_lock_take(__volatile__ unsigned int *lock,
876 unsigned int context);
877int drm_lock_transfer(drm_device_t *dev,
878 __volatile__ unsigned int *lock,
879 unsigned int context);
880int drm_lock_free(drm_device_t *dev,
881 __volatile__ unsigned int *lock,
882 unsigned int context);
883
884/* Buffer management support (drm_bufs.c) */
885unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource);
886unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource);
887void drm_rmmap(drm_device_t *dev, drm_local_map_t *map);
888int drm_order(unsigned long size);
889int drm_addmap(drm_device_t * dev, unsigned long offset, unsigned long size,
890 drm_map_type_t type, drm_map_flags_t flags,
891 drm_local_map_t **map_ptr);
892int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request);
893int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request);
894int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request);
895
896/* DMA support (drm_dma.c) */
897int drm_dma_setup(drm_device_t *dev);
898void drm_dma_takedown(drm_device_t *dev);
899void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf);
900void drm_reclaim_buffers(drm_device_t *dev, DRMFILE filp);
901#define drm_core_reclaim_buffers drm_reclaim_buffers
902
903/* IRQ support (drm_irq.c) */
904int drm_irq_install(drm_device_t *dev);
905int drm_irq_uninstall(drm_device_t *dev);
906irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
907void drm_driver_irq_preinstall(drm_device_t *dev);
908void drm_driver_irq_postinstall(drm_device_t *dev);
909void drm_driver_irq_uninstall(drm_device_t *dev);
910int drm_vblank_wait(drm_device_t *dev, unsigned int *vbl_seq);
911void drm_vbl_send_signals(drm_device_t *dev);
912
913/* AGP/PCI Express/GART support (drm_agpsupport.c) */
914int drm_device_is_agp(drm_device_t *dev);
915int drm_device_is_pcie(drm_device_t *dev);
916drm_agp_head_t *drm_agp_init(void);
917int drm_agp_acquire(drm_device_t *dev);
918int drm_agp_release(drm_device_t *dev);
919int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info);
920int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode);
921void *drm_agp_allocate_memory(size_t pages, u32 type);
922int drm_agp_free_memory(void *handle);
923int drm_agp_bind_memory(void *handle, off_t start);
924int drm_agp_unbind_memory(void *handle);
925int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request);
926int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request);
927int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request);
928int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request);
929
930/* Scatter Gather Support (drm_scatter.c) */
931void drm_sg_cleanup(drm_sg_mem_t *entry);
932
933#ifdef __FreeBSD__
934/* sysctl support (drm_sysctl.h) */
935extern int drm_sysctl_init(drm_device_t *dev);
936extern int drm_sysctl_cleanup(drm_device_t *dev);
937#endif /* __FreeBSD__ */
938
939/* ATI PCIGART support (ati_pcigart.c) */
940int drm_ati_pcigart_init(drm_device_t *dev,
941 drm_ati_pcigart_info *gart_info);
942int drm_ati_pcigart_cleanup(drm_device_t *dev,
943 drm_ati_pcigart_info *gart_info);
944
945/* Locking IOCTL support (drm_drv.c) */
946int drm_lock(DRM_IOCTL_ARGS);
947int drm_unlock(DRM_IOCTL_ARGS);
948int drm_version(DRM_IOCTL_ARGS);
949int drm_setversion(DRM_IOCTL_ARGS);
950
951/* Misc. IOCTL support (drm_ioctl.c) */
952int drm_irq_by_busid(DRM_IOCTL_ARGS);
953int drm_getunique(DRM_IOCTL_ARGS);
954int drm_setunique(DRM_IOCTL_ARGS);
955int drm_getmap(DRM_IOCTL_ARGS);
956int drm_getclient(DRM_IOCTL_ARGS);
957int drm_getstats(DRM_IOCTL_ARGS);
958int drm_noop(DRM_IOCTL_ARGS);
959
960/* Context IOCTL support (drm_context.c) */
961int drm_resctx(DRM_IOCTL_ARGS);
962int drm_addctx(DRM_IOCTL_ARGS);
963int drm_modctx(DRM_IOCTL_ARGS);
964int drm_getctx(DRM_IOCTL_ARGS);
965int drm_switchctx(DRM_IOCTL_ARGS);
966int drm_newctx(DRM_IOCTL_ARGS);
967int drm_rmctx(DRM_IOCTL_ARGS);
968int drm_setsareactx(DRM_IOCTL_ARGS);
969int drm_getsareactx(DRM_IOCTL_ARGS);
970
971/* Drawable IOCTL support (drm_drawable.c) */
972int drm_adddraw(DRM_IOCTL_ARGS);
973int drm_rmdraw(DRM_IOCTL_ARGS);
974
975/* Authentication IOCTL support (drm_auth.c) */
976int drm_getmagic(DRM_IOCTL_ARGS);
977int drm_authmagic(DRM_IOCTL_ARGS);
978
979/* Buffer management support (drm_bufs.c) */
980int drm_addmap_ioctl(DRM_IOCTL_ARGS);
981int drm_rmmap_ioctl(DRM_IOCTL_ARGS);
982int drm_addbufs_ioctl(DRM_IOCTL_ARGS);
983int drm_infobufs(DRM_IOCTL_ARGS);
984int drm_markbufs(DRM_IOCTL_ARGS);
985int drm_freebufs(DRM_IOCTL_ARGS);
986int drm_mapbufs(DRM_IOCTL_ARGS);
987
988/* DMA support (drm_dma.c) */
989int drm_dma(DRM_IOCTL_ARGS);
990
991/* IRQ support (drm_irq.c) */
992int drm_control(DRM_IOCTL_ARGS);
993int drm_wait_vblank(DRM_IOCTL_ARGS);
994
995/* AGP/GART support (drm_agpsupport.c) */
996int drm_agp_acquire_ioctl(DRM_IOCTL_ARGS);
997int drm_agp_release_ioctl(DRM_IOCTL_ARGS);
998int drm_agp_enable_ioctl(DRM_IOCTL_ARGS);
999int drm_agp_info_ioctl(DRM_IOCTL_ARGS);
1000int drm_agp_alloc_ioctl(DRM_IOCTL_ARGS);
1001int drm_agp_free_ioctl(DRM_IOCTL_ARGS);
1002int drm_agp_unbind_ioctl(DRM_IOCTL_ARGS);
1003int drm_agp_bind_ioctl(DRM_IOCTL_ARGS);
1004
1005/* Scatter Gather Support (drm_scatter.c) */
1006int drm_sg_alloc(DRM_IOCTL_ARGS);
1007int drm_sg_free(DRM_IOCTL_ARGS);
1008
1009/* consistent PCI memory functions (drm_pci.c) */
1010drm_dma_handle_t *drm_pci_alloc(drm_device_t *dev, size_t size, size_t align,
1011 dma_addr_t maxaddr);
1012void drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah);
1013
1014/* Inline replacements for DRM_IOREMAP macros */
1015static __inline__ void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1016{
1017 map->handle = drm_ioremap(dev, map);
1018}
1019static __inline__ void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1020{
1021 if ( map->handle && map->size )
1022 drm_ioremapfree(map);
1023}
1024
1025static __inline__ struct drm_local_map *drm_core_findmap(struct drm_device *dev, unsigned long offset)
1026{
1027 drm_local_map_t *map;
1028
1029 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1030 TAILQ_FOREACH(map, &dev->maplist, link) {
1031 if (map->offset == offset)
1032 return map;
1033 }
1034 return NULL;
1035}
1036
1037static __inline__ void drm_core_dropmap(struct drm_map *map)
1038{
1039}
1040
1041#endif /* __KERNEL__ */
1042#endif /* _DRM_P_H_ */