]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drmP.h
Import Concurrency Kit in the kernel.
[FreeBSD/FreeBSD.git] / sys / dev / drm / drmP.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$");
36
37 #ifndef _DRM_P_H_
38 #define _DRM_P_H_
39
40 #if defined(_KERNEL) || defined(__KERNEL__)
41
42 struct drm_device;
43 struct drm_file;
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/sglist.h>
53 #include <sys/stat.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/lock.h>
57 #include <sys/fcntl.h>
58 #include <sys/uio.h>
59 #include <sys/filio.h>
60 #include <sys/rwlock.h>
61 #include <sys/sysctl.h>
62 #include <sys/bus.h>
63 #include <sys/queue.h>
64 #include <sys/signalvar.h>
65 #include <sys/poll.h>
66 #include <sys/taskqueue.h>
67 #include <sys/tree.h>
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_phys.h>
78 #include <machine/bus.h>
79 #include <machine/resource.h>
80 #if defined(__i386__) || defined(__amd64__)
81 #include <machine/specialreg.h>
82 #endif
83 #include <machine/sysarch.h>
84 #include <sys/endian.h>
85 #if _BYTE_ORDER == _BIG_ENDIAN
86 #define __BIG_ENDIAN 1
87 #else
88 #define __LITTLE_ENDIAN 1
89 #endif
90 #include <sys/mman.h>
91 #include <sys/rman.h>
92 #include <sys/memrange.h>
93 #include <dev/agp/agpvar.h>
94 #include <sys/agpio.h>
95 #include <sys/mutex.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/pci/pcireg.h>
98 #include <sys/selinfo.h>
99 #include <sys/bus.h>
100
101 #include "dev/drm/drm.h"
102 #include "dev/drm/drm_atomic.h"
103 #include "dev/drm/drm_internal.h"
104 #include "dev/drm/drm_linux_list.h"
105
106 #include <opt_drm.h>
107 #ifdef DRM_DEBUG
108 #undef DRM_DEBUG
109 #define DRM_DEBUG_DEFAULT_ON 1
110 #endif /* DRM_DEBUG */
111
112 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
113 #include <sys/file.h>
114 #include <sys/proc.h>
115 #include <machine/../linux/linux.h>
116 #include <machine/../linux/linux_proto.h>
117 #else
118 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
119  * supported on this OS yet.
120  */
121 #undef DRM_LINUX
122 #define DRM_LINUX 0
123 #endif
124
125 /* driver capabilities and requirements mask */
126 #define DRIVER_USE_AGP     0x1
127 #define DRIVER_REQUIRE_AGP 0x2
128 #define DRIVER_USE_MTRR    0x4
129 #define DRIVER_PCI_DMA     0x8
130 #define DRIVER_SG          0x10
131 #define DRIVER_HAVE_DMA    0x20
132 #define DRIVER_HAVE_IRQ    0x40
133 #define DRIVER_DMA_QUEUE   0x100
134
135
136 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
137 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
138 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
139
140 MALLOC_DECLARE(DRM_MEM_DMA);
141 MALLOC_DECLARE(DRM_MEM_SAREA);
142 MALLOC_DECLARE(DRM_MEM_DRIVER);
143 MALLOC_DECLARE(DRM_MEM_MAGIC);
144 MALLOC_DECLARE(DRM_MEM_IOCTLS);
145 MALLOC_DECLARE(DRM_MEM_MAPS);
146 MALLOC_DECLARE(DRM_MEM_BUFS);
147 MALLOC_DECLARE(DRM_MEM_SEGS);
148 MALLOC_DECLARE(DRM_MEM_PAGES);
149 MALLOC_DECLARE(DRM_MEM_FILES);
150 MALLOC_DECLARE(DRM_MEM_QUEUES);
151 MALLOC_DECLARE(DRM_MEM_CMDS);
152 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
153 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
154 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
155 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
156 MALLOC_DECLARE(DRM_MEM_SGLISTS);
157 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
158 MALLOC_DECLARE(DRM_MEM_MM);
159 MALLOC_DECLARE(DRM_MEM_HASHTAB);
160
161 SYSCTL_DECL(_hw_drm);
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
172 #define __OS_HAS_AGP    1
173
174 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
175 #define DRM_DEV_UID     UID_ROOT
176 #define DRM_DEV_GID     GID_VIDEO
177
178 #define wait_queue_head_t       atomic_t
179 #define DRM_WAKEUP(w)           wakeup((void *)w)
180 #define DRM_WAKEUP_INT(w)       wakeup(w)
181 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
182
183 #define DRM_CURPROC             curthread
184 #define DRM_STRUCTPROC          struct thread
185 #define DRM_SPINTYPE            struct mtx
186 #define DRM_SPININIT(l,name)    mtx_init(l, name, NULL, MTX_DEF)
187 #define DRM_SPINUNINIT(l)       mtx_destroy(l)
188 #define DRM_SPINLOCK(l)         mtx_lock(l)
189 #define DRM_SPINUNLOCK(u)       mtx_unlock(u)
190 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {          \
191         mtx_lock(l);                                    \
192         (void)irqflags;                                 \
193 } while (0)
194 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
195 #define DRM_SPINLOCK_ASSERT(l)  mtx_assert(l, MA_OWNED)
196 #define DRM_CURRENTPID          curthread->td_proc->p_pid
197 #define DRM_LOCK()              mtx_lock(&dev->dev_lock)
198 #define DRM_UNLOCK()            mtx_unlock(&dev->dev_lock)
199 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
200
201 #define DRM_IRQ_ARGS            void *arg
202 typedef void                    irqreturn_t;
203 #define IRQ_HANDLED             /* nothing */
204 #define IRQ_NONE                /* nothing */
205
206 #define unlikely(x)            __builtin_expect(!!(x), 0)
207 #define container_of(ptr, type, member) ({                      \
208         __typeof( ((type *)0)->member ) *__mptr = (ptr);        \
209         (type *)( (char *)__mptr - offsetof(type,member) );})
210
211 enum {
212         DRM_IS_NOT_AGP,
213         DRM_IS_AGP,
214         DRM_MIGHT_BE_AGP
215 };
216 #define DRM_AGP_MEM             struct agp_memory_info
217
218 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
219
220 #define PAGE_ALIGN(addr) round_page(addr)
221 /* DRM_SUSER returns true if the user is superuser */
222 #define DRM_SUSER(p)            (priv_check(p, PRIV_DRIVER) == 0)
223 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
224 #define DRM_MTRR_WC             MDF_WRITECOMBINE
225 #define jiffies                 ticks
226
227 typedef vm_paddr_t dma_addr_t;
228 typedef u_int64_t u64;
229 typedef u_int32_t u32;
230 typedef u_int16_t u16;
231 typedef u_int8_t u8;
232
233 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
234  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
235  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
236  */
237 #define DRM_READMEMORYBARRIER()         rmb()
238 #define DRM_WRITEMEMORYBARRIER()        wmb()
239 #define DRM_MEMORYBARRIER()             mb()
240
241 #define DRM_READ8(map, offset)                                          \
242         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
243             (vm_offset_t)(offset))
244 #define DRM_READ16(map, offset)                                         \
245         le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) + \
246             (vm_offset_t)(offset)))
247 #define DRM_READ32(map, offset)                                         \
248         le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) + \
249             (vm_offset_t)(offset)))
250 #define DRM_WRITE8(map, offset, val)                                    \
251         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
252             (vm_offset_t)(offset)) = val
253 #define DRM_WRITE16(map, offset, val)                                   \
254         *(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +         \
255             (vm_offset_t)(offset)) = htole16(val)
256 #define DRM_WRITE32(map, offset, val)                                   \
257         *(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +         \
258             (vm_offset_t)(offset)) = htole32(val)
259
260 #define DRM_VERIFYAREA_READ( uaddr, size )              \
261         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
262
263 #define DRM_COPY_TO_USER(user, kern, size) \
264         copyout(kern, user, size)
265 #define DRM_COPY_FROM_USER(kern, user, size) \
266         copyin(user, kern, size)
267 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
268         copyin(arg2, arg1, arg3)
269 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
270         copyout(arg2, arg1, arg3)
271 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
272         ((val) = fuword32(uaddr), 0)
273
274 #define cpu_to_le32(x) htole32(x)
275 #define le32_to_cpu(x) le32toh(x)
276
277 #define DRM_HZ                  hz
278 #define DRM_UDELAY(udelay)      DELAY(udelay)
279 #define DRM_TIME_SLICE          (hz/20)  /* Time slice for GLXContexts    */
280
281 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
282         (_map) = (_dev)->context_sareas[_ctx];          \
283 } while(0)
284
285 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
286 do {                                                                    \
287         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
288              dev->lock.file_priv != file_priv) {                        \
289                 DRM_ERROR("%s called without lock held\n",              \
290                            __FUNCTION__);                               \
291                 return EINVAL;                                          \
292         }                                                               \
293 } while (0)
294
295 /* Returns -errno to shared code */
296 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
297 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
298         DRM_UNLOCK();                                           \
299         mtx_lock(&dev->irq_lock);                               \
300         if (!(condition))                                       \
301             ret = -mtx_sleep(&(queue), &dev->irq_lock,          \
302                 PCATCH, "drmwtq", (timeout));                   \
303         mtx_unlock(&dev->irq_lock);                             \
304         DRM_LOCK();                                             \
305 }
306
307 #define DRM_ERROR(fmt, ...) \
308         printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,           \
309             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
310
311 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
312
313 #define DRM_DEBUG(fmt, ...) do {                                        \
314         if (drm_debug_flag)                                             \
315                 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,  \
316                         __func__ , ##__VA_ARGS__);                      \
317 } while (0)
318
319 typedef struct drm_pci_id_list
320 {
321         int vendor;
322         int device;
323         long driver_private;
324         char *name;
325 } drm_pci_id_list_t;
326
327 struct drm_msi_blacklist_entry
328 {
329         int vendor;
330         int device;
331 };
332
333 #define DRM_AUTH        0x1
334 #define DRM_MASTER      0x2
335 #define DRM_ROOT_ONLY   0x4
336 typedef struct drm_ioctl_desc {
337         unsigned long cmd;
338         int (*func)(struct drm_device *dev, void *data,
339                     struct drm_file *file_priv);
340         int flags;
341 } drm_ioctl_desc_t;
342 /**
343  * Creates a driver or general drm_ioctl_desc array entry for the given
344  * ioctl, for use by drm_ioctl().
345  */
346 #define DRM_IOCTL_DEF(ioctl, func, flags) \
347         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
348
349 typedef struct drm_magic_entry {
350         drm_magic_t            magic;
351         struct drm_file        *priv;
352         struct drm_magic_entry *next;
353 } drm_magic_entry_t;
354
355 typedef struct drm_magic_head {
356         struct drm_magic_entry *head;
357         struct drm_magic_entry *tail;
358 } drm_magic_head_t;
359
360 typedef struct drm_buf {
361         int               idx;         /* Index into master buflist          */
362         int               total;       /* Buffer size                        */
363         int               order;       /* log-base-2(total)                  */
364         int               used;        /* Amount of buffer in use (for DMA)  */
365         unsigned long     offset;      /* Byte offset (used internally)      */
366         void              *address;    /* Address of buffer                  */
367         unsigned long     bus_address; /* Bus address of buffer              */
368         struct drm_buf    *next;       /* Kernel-only: used for free list    */
369         __volatile__ int  pending;     /* On hardware DMA queue              */
370         struct drm_file   *file_priv;  /* Unique identifier of holding process */
371         int               context;     /* Kernel queue for this buffer       */
372         enum {
373                 DRM_LIST_NONE    = 0,
374                 DRM_LIST_FREE    = 1,
375                 DRM_LIST_WAIT    = 2,
376                 DRM_LIST_PEND    = 3,
377                 DRM_LIST_PRIO    = 4,
378                 DRM_LIST_RECLAIM = 5
379         }                 list;        /* Which list we're on                */
380
381         int               dev_priv_size; /* Size of buffer private stoarge   */
382         void              *dev_private;  /* Per-buffer private storage       */
383 } drm_buf_t;
384
385 typedef struct drm_freelist {
386         int               initialized; /* Freelist in use                  */
387         atomic_t          count;       /* Number of free buffers           */
388         drm_buf_t         *next;       /* End pointer                      */
389
390         int               low_mark;    /* Low water mark                   */
391         int               high_mark;   /* High water mark                  */
392 } drm_freelist_t;
393
394 typedef struct drm_dma_handle {
395         void *vaddr;
396         bus_addr_t busaddr;
397         bus_dma_tag_t tag;
398         bus_dmamap_t map;
399 } drm_dma_handle_t;
400
401 typedef struct drm_buf_entry {
402         int               buf_size;
403         int               buf_count;
404         drm_buf_t         *buflist;
405         int               seg_count;
406         drm_dma_handle_t  **seglist;
407         int               page_order;
408
409         drm_freelist_t    freelist;
410 } drm_buf_entry_t;
411
412 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
413 struct drm_file {
414         TAILQ_ENTRY(drm_file) link;
415         struct drm_device *dev;
416         int               authenticated;
417         int               master;
418         pid_t             pid;
419         uid_t             uid;
420         drm_magic_t       magic;
421         unsigned long     ioctl_count;
422         void             *driver_priv;
423 };
424
425 typedef struct drm_lock_data {
426         struct drm_hw_lock      *hw_lock;       /* Hardware lock                   */
427         struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
428         int               lock_queue;   /* Queue of blocked processes      */
429         unsigned long     lock_time;    /* Time of last lock in jiffies    */
430 } drm_lock_data_t;
431
432 /* This structure, in the struct drm_device, is always initialized while the
433  * device
434  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
435  * when set marks that no further bufs may be allocated until device teardown
436  * occurs (when the last open of the device has closed).  The high/low
437  * watermarks of bufs are only touched by the X Server, and thus not
438  * concurrently accessed, so no locking is needed.
439  */
440 typedef struct drm_device_dma {
441         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
442         int               buf_count;
443         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
444         int               seg_count;
445         int               page_count;
446         unsigned long     *pagelist;
447         unsigned long     byte_count;
448         enum {
449                 _DRM_DMA_USE_AGP = 0x01,
450                 _DRM_DMA_USE_SG  = 0x02
451         } flags;
452 } drm_device_dma_t;
453
454 typedef struct drm_agp_mem {
455         void               *handle;
456         unsigned long      bound; /* address */
457         int                pages;
458         struct drm_agp_mem *prev;
459         struct drm_agp_mem *next;
460 } drm_agp_mem_t;
461
462 typedef struct drm_agp_head {
463         device_t           agpdev;
464         struct agp_info    info;
465         const char         *chipset;
466         drm_agp_mem_t      *memory;
467         unsigned long      mode;
468         int                enabled;
469         int                acquired;
470         unsigned long      base;
471         int                mtrr;
472         int                cant_use_aperture;
473         unsigned long      page_mask;
474 } drm_agp_head_t;
475
476 typedef struct drm_sg_mem {
477         vm_offset_t vaddr;
478         vm_paddr_t *busaddr;
479         vm_pindex_t pages;
480 } drm_sg_mem_t;
481
482 #define DRM_MAP_HANDLE_BITS     (sizeof(void *) == 4 ? 4 : 24)
483 #define DRM_MAP_HANDLE_SHIFT    (sizeof(void *) * 8 - DRM_MAP_HANDLE_BITS)
484 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
485
486 typedef struct drm_local_map {
487         unsigned long offset;     /* Physical address (0 for SAREA)       */
488         unsigned long size;       /* Physical size (bytes)                */
489         enum drm_map_type type;   /* Type of memory mapped                */
490         enum drm_map_flags flags; /* Flags                                */
491         void *handle;             /* User-space: "Handle" to pass to mmap */
492                                   /* Kernel-space: kernel-virtual address */
493         int mtrr;                 /* Boolean: MTRR used                   */
494                                   /* Private data                         */
495         int rid;                  /* PCI resource ID for bus_space        */
496         void *virtual;            /* Kernel-space: kernel-virtual address */
497         struct resource *bsr;
498         bus_space_tag_t bst;
499         bus_space_handle_t bsh;
500         drm_dma_handle_t *dmah;
501         TAILQ_ENTRY(drm_local_map) link;
502 } drm_local_map_t;
503
504 struct drm_vblank_info {
505         wait_queue_head_t queue;        /* vblank wait queue */
506         atomic_t count;                 /* number of VBLANK interrupts */
507                                         /* (driver must alloc the right number of counters) */
508         atomic_t refcount;              /* number of users of vblank interrupts */
509         u32 last;                       /* protected by dev->vbl_lock, used */
510                                         /* for wraparound handling */
511         int enabled;                    /* so we don't call enable more than */
512                                         /* once per disable */
513         int inmodeset;                  /* Display driver is setting mode */
514 };
515
516 /* location of GART table */
517 #define DRM_ATI_GART_MAIN 1
518 #define DRM_ATI_GART_FB   2
519
520 #define DRM_ATI_GART_PCI  1
521 #define DRM_ATI_GART_PCIE 2
522 #define DRM_ATI_GART_IGP  3
523
524 struct drm_ati_pcigart_info {
525         int gart_table_location;
526         int gart_reg_if;
527         void *addr;
528         dma_addr_t bus_addr;
529         dma_addr_t table_mask;
530         dma_addr_t member_mask;
531         struct drm_dma_handle *table_handle;
532         drm_local_map_t mapping;
533         int table_size;
534         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
535 };
536
537 #ifndef DMA_BIT_MASK
538 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
539 #endif
540
541 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
542
543 struct drm_driver_info {
544         int     (*load)(struct drm_device *, unsigned long flags);
545         int     (*firstopen)(struct drm_device *);
546         int     (*open)(struct drm_device *, struct drm_file *);
547         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
548         void    (*postclose)(struct drm_device *, struct drm_file *);
549         void    (*lastclose)(struct drm_device *);
550         int     (*unload)(struct drm_device *);
551         void    (*reclaim_buffers_locked)(struct drm_device *,
552                                           struct drm_file *file_priv);
553         int     (*dma_ioctl)(struct drm_device *dev, void *data,
554                              struct drm_file *file_priv);
555         void    (*dma_ready)(struct drm_device *);
556         int     (*dma_quiescent)(struct drm_device *);
557         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
558                                              enum drm_lock_flags flags);
559         int     (*dma_flush_unblock)(struct drm_device *, int context,
560                                      enum drm_lock_flags flags);
561         int     (*context_ctor)(struct drm_device *dev, int context);
562         int     (*context_dtor)(struct drm_device *dev, int context);
563         int     (*kernel_context_switch)(struct drm_device *dev, int old,
564                                          int new);
565         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
566         void    (*irq_preinstall)(struct drm_device *dev);
567         int     (*irq_postinstall)(struct drm_device *dev);
568         void    (*irq_uninstall)(struct drm_device *dev);
569         void    (*irq_handler)(DRM_IRQ_ARGS);
570         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
571         int     (*enable_vblank)(struct drm_device *dev, int crtc);
572         void    (*disable_vblank)(struct drm_device *dev, int crtc);
573
574         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
575
576         /**
577          * Called by \c drm_device_is_agp.  Typically used to determine if a
578          * card is really attached to AGP or not.
579          *
580          * \param dev  DRM device handle
581          *
582          * \returns 
583          * One of three values is returned depending on whether or not the
584          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
585          * (return of 1), or may or may not be AGP (return of 2).
586          */
587         int     (*device_is_agp) (struct drm_device * dev);
588
589         drm_ioctl_desc_t *ioctls;
590         int     max_ioctl;
591
592         int     buf_priv_size;
593
594         int     major;
595         int     minor;
596         int     patchlevel;
597         const char *name;               /* Simple driver name              */
598         const char *desc;               /* Longer driver name              */
599         const char *date;               /* Date of last major changes.     */
600
601         u32 driver_features;
602 };
603
604 /* Length for the array of resource pointers for drm_get_resource_*. */
605 #define DRM_MAX_PCI_RESOURCE    6
606
607 /** 
608  * DRM device functions structure
609  */
610 struct drm_device {
611         struct drm_driver_info *driver;
612         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
613
614         u_int16_t pci_device;           /* PCI device id */
615         u_int16_t pci_vendor;           /* PCI vendor id */
616
617         char              *unique;      /* Unique identifier: e.g., busid  */
618         int               unique_len;   /* Length of unique field          */
619         device_t          device;       /* Device instance from newbus     */
620         struct cdev       *devnode;     /* Device number for mknod         */
621         int               if_version;   /* Highest interface version set */
622
623         int               flags;        /* Flags to open(2)                */
624
625                                 /* Locks */
626         struct mtx        vbl_lock;     /* protects vblank operations */
627         struct mtx        dma_lock;     /* protects dev->dma */
628         struct mtx        irq_lock;     /* protects irq condition checks */
629         struct mtx        dev_lock;     /* protects everything else */
630         DRM_SPINTYPE      drw_lock;
631
632                                 /* Usage Counters */
633         int               open_count;   /* Outstanding files open          */
634         int               buf_use;      /* Buffers in use -- cannot alloc  */
635
636                                 /* Performance counters */
637         unsigned long     counters;
638         enum drm_stat_type      types[15];
639         atomic_t          counts[15];
640
641                                 /* Authentication */
642         drm_file_list_t   files;
643         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
644
645         /* Linked list of mappable regions. Protected by dev_lock */
646         drm_map_list_t    maplist;
647         struct unrhdr     *map_unrhdr;
648
649         drm_local_map_t   **context_sareas;
650         int               max_context;
651
652         drm_lock_data_t   lock;         /* Information on hardware lock    */
653
654                                 /* DMA queues (contexts) */
655         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
656
657                                 /* Context support */
658         int               irq;          /* Interrupt used by board         */
659         int               irq_enabled;  /* True if the irq handler is enabled */
660         int               msi_enabled;  /* MSI enabled */
661         int               irqrid;       /* Interrupt used by board */
662         struct resource   *irqr;        /* Resource for interrupt used by board    */
663         void              *irqh;        /* Handle from bus_setup_intr      */
664
665         /* Storage of resource pointers for drm_get_resource_* */
666         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
667         int               pcirid[DRM_MAX_PCI_RESOURCE];
668
669         int               pci_domain;
670         int               pci_bus;
671         int               pci_slot;
672         int               pci_func;
673
674         atomic_t          context_flag; /* Context swapping flag           */
675         int               last_context; /* Last current context            */
676
677         int               vblank_disable_allowed;
678         struct callout    vblank_disable_timer;
679         u32               max_vblank_count;     /* size of vblank counter register */
680         struct drm_vblank_info *vblank;         /* per crtc vblank info */
681         int               num_crtcs;
682
683         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
684
685                                 /* Sysctl support */
686         struct drm_sysctl_info *sysctl;
687
688         drm_agp_head_t    *agp;
689         drm_sg_mem_t      *sg;  /* Scatter gather memory */
690         atomic_t          *ctx_bitmap;
691         void              *dev_private;
692         unsigned int      agp_buffer_token;
693         drm_local_map_t   *agp_buffer_map;
694
695         struct unrhdr     *drw_unrhdr;
696         /* RB tree of drawable infos */
697         RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
698 };
699
700 static __inline__ int drm_core_check_feature(struct drm_device *dev,
701                                              int feature)
702 {
703         return ((dev->driver->driver_features & feature) ? 1 : 0);
704 }
705
706 #if __OS_HAS_AGP
707 static inline int drm_core_has_AGP(struct drm_device *dev)
708 {
709         return drm_core_check_feature(dev, DRIVER_USE_AGP);
710 }
711 #else
712 #define drm_core_has_AGP(dev) (0)
713 #endif
714
715 extern int      drm_debug_flag;
716
717 /* Device setup support (drm_drv.c) */
718 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
719 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
720 void    drm_close(void *data);
721 int     drm_detach(device_t kdev);
722 d_ioctl_t drm_ioctl;
723 d_open_t drm_open;
724 d_read_t drm_read;
725 d_poll_t drm_poll;
726 d_mmap_t drm_mmap;
727 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
728
729 /* File operations helpers (drm_fops.c) */
730 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
731                                          DRM_STRUCTPROC *p,
732                                         struct drm_device *dev);
733
734 /* Memory management support (drm_memory.c) */
735 void    drm_mem_init(void);
736 void    drm_mem_uninit(void);
737 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
738 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
739 void    drm_ioremapfree(drm_local_map_t *map);
740 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
741 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
742
743 int     drm_context_switch(struct drm_device *dev, int old, int new);
744 int     drm_context_switch_complete(struct drm_device *dev, int new);
745
746 int     drm_ctxbitmap_init(struct drm_device *dev);
747 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
748 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
749 int     drm_ctxbitmap_next(struct drm_device *dev);
750
751 /* Locking IOCTL support (drm_lock.c) */
752 int     drm_lock_take(struct drm_lock_data *lock_data,
753                       unsigned int context);
754 int     drm_lock_transfer(struct drm_lock_data *lock_data,
755                           unsigned int context);
756 int     drm_lock_free(struct drm_lock_data *lock_data,
757                       unsigned int context);
758
759 /* Buffer management support (drm_bufs.c) */
760 unsigned long drm_get_resource_start(struct drm_device *dev,
761                                      unsigned int resource);
762 unsigned long drm_get_resource_len(struct drm_device *dev,
763                                    unsigned int resource);
764 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
765 int     drm_order(unsigned long size);
766 int     drm_addmap(struct drm_device *dev, unsigned long offset,
767                    unsigned long size,
768                    enum drm_map_type type, enum drm_map_flags flags,
769                    drm_local_map_t **map_ptr);
770 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
771 int     drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
772 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
773
774 /* DMA support (drm_dma.c) */
775 int     drm_dma_setup(struct drm_device *dev);
776 void    drm_dma_takedown(struct drm_device *dev);
777 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
778 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
779 #define drm_core_reclaim_buffers drm_reclaim_buffers
780
781 /* IRQ support (drm_irq.c) */
782 int     drm_irq_install(struct drm_device *dev);
783 int     drm_irq_uninstall(struct drm_device *dev);
784 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
785 void    drm_driver_irq_preinstall(struct drm_device *dev);
786 void    drm_driver_irq_postinstall(struct drm_device *dev);
787 void    drm_driver_irq_uninstall(struct drm_device *dev);
788 void    drm_handle_vblank(struct drm_device *dev, int crtc);
789 u32     drm_vblank_count(struct drm_device *dev, int crtc);
790 int     drm_vblank_get(struct drm_device *dev, int crtc);
791 void    drm_vblank_put(struct drm_device *dev, int crtc);
792 void    drm_vblank_cleanup(struct drm_device *dev);
793 int     drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
794 int     drm_vblank_init(struct drm_device *dev, int num_crtcs);
795 int     drm_modeset_ctl(struct drm_device *dev, void *data,
796                         struct drm_file *file_priv);
797
798 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
799 int     drm_device_is_agp(struct drm_device *dev);
800 int     drm_device_is_pcie(struct drm_device *dev);
801 drm_agp_head_t *drm_agp_init(void);
802 int     drm_agp_acquire(struct drm_device *dev);
803 int     drm_agp_release(struct drm_device *dev);
804 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
805 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
806 void    *drm_agp_allocate_memory(size_t pages, u32 type);
807 int     drm_agp_free_memory(void *handle);
808 int     drm_agp_bind_memory(void *handle, off_t start);
809 int     drm_agp_unbind_memory(void *handle);
810 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
811 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
812 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
813 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
814
815 /* Scatter Gather Support (drm_scatter.c) */
816 void    drm_sg_cleanup(drm_sg_mem_t *entry);
817 int     drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
818
819 /* sysctl support (drm_sysctl.h) */
820 extern int              drm_sysctl_init(struct drm_device *dev);
821 extern int              drm_sysctl_cleanup(struct drm_device *dev);
822
823 /* ATI PCIGART support (ati_pcigart.c) */
824 int     drm_ati_pcigart_init(struct drm_device *dev,
825                                 struct drm_ati_pcigart_info *gart_info);
826 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
827                                 struct drm_ati_pcigart_info *gart_info);
828
829 /* Locking IOCTL support (drm_drv.c) */
830 int     drm_lock(struct drm_device *dev, void *data,
831                  struct drm_file *file_priv);
832 int     drm_unlock(struct drm_device *dev, void *data,
833                    struct drm_file *file_priv);
834 int     drm_version(struct drm_device *dev, void *data,
835                     struct drm_file *file_priv);
836 int     drm_setversion(struct drm_device *dev, void *data,
837                        struct drm_file *file_priv);
838
839 /* Misc. IOCTL support (drm_ioctl.c) */
840 int     drm_irq_by_busid(struct drm_device *dev, void *data,
841                          struct drm_file *file_priv);
842 int     drm_getunique(struct drm_device *dev, void *data,
843                       struct drm_file *file_priv);
844 int     drm_setunique(struct drm_device *dev, void *data,
845                       struct drm_file *file_priv);
846 int     drm_getmap(struct drm_device *dev, void *data,
847                    struct drm_file *file_priv);
848 int     drm_getclient(struct drm_device *dev, void *data,
849                       struct drm_file *file_priv);
850 int     drm_getstats(struct drm_device *dev, void *data,
851                      struct drm_file *file_priv);
852 int     drm_noop(struct drm_device *dev, void *data,
853                  struct drm_file *file_priv);
854
855 /* Context IOCTL support (drm_context.c) */
856 int     drm_resctx(struct drm_device *dev, void *data,
857                    struct drm_file *file_priv);
858 int     drm_addctx(struct drm_device *dev, void *data,
859                    struct drm_file *file_priv);
860 int     drm_modctx(struct drm_device *dev, void *data,
861                    struct drm_file *file_priv);
862 int     drm_getctx(struct drm_device *dev, void *data,
863                    struct drm_file *file_priv);
864 int     drm_switchctx(struct drm_device *dev, void *data,
865                       struct drm_file *file_priv);
866 int     drm_newctx(struct drm_device *dev, void *data,
867                    struct drm_file *file_priv);
868 int     drm_rmctx(struct drm_device *dev, void *data,
869                   struct drm_file *file_priv);
870 int     drm_setsareactx(struct drm_device *dev, void *data,
871                         struct drm_file *file_priv);
872 int     drm_getsareactx(struct drm_device *dev, void *data,
873                         struct drm_file *file_priv);
874
875 /* Drawable IOCTL support (drm_drawable.c) */
876 int     drm_adddraw(struct drm_device *dev, void *data,
877                     struct drm_file *file_priv);
878 int     drm_rmdraw(struct drm_device *dev, void *data,
879                    struct drm_file *file_priv);
880 int     drm_update_draw(struct drm_device *dev, void *data,
881                         struct drm_file *file_priv);
882 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
883                                                 int handle);
884
885 /* Drawable support (drm_drawable.c) */
886 void drm_drawable_free_all(struct drm_device *dev);
887
888 /* Authentication IOCTL support (drm_auth.c) */
889 int     drm_getmagic(struct drm_device *dev, void *data,
890                      struct drm_file *file_priv);
891 int     drm_authmagic(struct drm_device *dev, void *data,
892                       struct drm_file *file_priv);
893
894 /* Buffer management support (drm_bufs.c) */
895 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
896                          struct drm_file *file_priv);
897 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
898                         struct drm_file *file_priv);
899 int     drm_addbufs(struct drm_device *dev, void *data,
900                     struct drm_file *file_priv);
901 int     drm_infobufs(struct drm_device *dev, void *data,
902                      struct drm_file *file_priv);
903 int     drm_markbufs(struct drm_device *dev, void *data,
904                      struct drm_file *file_priv);
905 int     drm_freebufs(struct drm_device *dev, void *data,
906                      struct drm_file *file_priv);
907 int     drm_mapbufs(struct drm_device *dev, void *data,
908                     struct drm_file *file_priv);
909
910 /* DMA support (drm_dma.c) */
911 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
912
913 /* IRQ support (drm_irq.c) */
914 int     drm_control(struct drm_device *dev, void *data,
915                     struct drm_file *file_priv);
916 int     drm_wait_vblank(struct drm_device *dev, void *data,
917                         struct drm_file *file_priv);
918
919 /* AGP/GART support (drm_agpsupport.c) */
920 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
921                               struct drm_file *file_priv);
922 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
923                               struct drm_file *file_priv);
924 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
925                              struct drm_file *file_priv);
926 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
927                            struct drm_file *file_priv);
928 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
929                             struct drm_file *file_priv);
930 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
931                            struct drm_file *file_priv);
932 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
933                              struct drm_file *file_priv);
934 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
935                            struct drm_file *file_priv);
936
937 /* Scatter Gather Support (drm_scatter.c) */
938 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
939                            struct drm_file *file_priv);
940 int     drm_sg_free(struct drm_device *dev, void *data,
941                     struct drm_file *file_priv);
942
943 /* consistent PCI memory functions (drm_pci.c) */
944 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
945                                 size_t align, dma_addr_t maxaddr);
946 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
947
948 /* Inline replacements for drm_alloc and friends */
949 static __inline__ void *
950 drm_alloc(size_t size, struct malloc_type *area)
951 {
952         return malloc(size, area, M_NOWAIT);
953 }
954
955 static __inline__ void *
956 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
957 {
958         return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
959 }
960
961 static __inline__ void *
962 drm_realloc(void *oldpt, size_t oldsize, size_t size,
963     struct malloc_type *area)
964 {
965         return reallocf(oldpt, size, area, M_NOWAIT);
966 }
967
968 static __inline__ void
969 drm_free(void *pt, size_t size, struct malloc_type *area)
970 {
971         free(pt, area);
972 }
973
974 /* Inline replacements for DRM_IOREMAP macros */
975 static __inline__ void
976 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
977 {
978         map->virtual = drm_ioremap_wc(dev, map);
979 }
980 static __inline__ void
981 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
982 {
983         map->virtual = drm_ioremap(dev, map);
984 }
985 static __inline__ void
986 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
987 {
988         if ( map->virtual && map->size )
989                 drm_ioremapfree(map);
990 }
991
992 static __inline__ struct drm_local_map *
993 drm_core_findmap(struct drm_device *dev, unsigned long offset)
994 {
995         drm_local_map_t *map;
996
997         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
998         TAILQ_FOREACH(map, &dev->maplist, link) {
999                 if (offset == (unsigned long)map->handle)
1000                         return map;
1001         }
1002         return NULL;
1003 }
1004
1005 static __inline__ void drm_core_dropmap(struct drm_map *map)
1006 {
1007 }
1008
1009 #endif /* __KERNEL__ */
1010 #endif /* _DRM_P_H_ */