drm: Import the ttm memory manager from FreeBSD
[dragonfly.git] / sys / dev / drm2 / 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  * $FreeBSD: head/sys/dev/drm2/drmP.h 248084 2013-03-09 02:32:23Z attilio $
33  */
34
35 #ifndef _DRM_P_H_
36 #define _DRM_P_H_
37
38 #if defined(_KERNEL) || defined(__KERNEL__)
39
40 struct drm_device;
41 struct drm_file;
42
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/module.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/sglist.h>
52 #include <sys/stat.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/lock.h>
56 #include <sys/spinlock.h>
57 #include <sys/spinlock2.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/queue.h>
64 #include <sys/signalvar.h>
65 #include <sys/poll.h>
66 #include <sys/sbuf.h>
67 #include <sys/taskqueue.h>
68 #include <sys/tree.h>
69 #include <vm/vm.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pager.h>
77 #include <vm/vm_param.h>
78 #include <machine/param.h>
79 #include <machine/pmap.h>
80 #include <sys/resource.h>
81 #if defined(__i386__) || defined(__amd64__)
82 #include <machine/specialreg.h>
83 #endif
84 #include <machine/sysarch.h>
85 #include <sys/endian.h>
86 #include <sys/mman.h>
87 #include <sys/rman.h>
88 #include <sys/memrange.h>
89 #include <dev/agp/agpvar.h>
90 #include <sys/agpio.h>
91 #include <sys/mutex.h>
92 #include <bus/pci/pcivar.h>
93 #include <bus/pci/pcireg.h>
94 /* XXX: bool definition */
95 #include <libprop/proplib.h>
96
97 #include <dev/drm2/drm.h>
98 #include <dev/drm2/drm_atomic.h>
99 #include <dev/drm2/drm_internal.h>
100 #include <dev/drm2/drm_linux_list.h>
101 #include <dev/drm2/drm_gem_names.h>
102 #include <dev/drm2/drm_mm.h>
103 #include <dev/drm2/drm_hashtab.h>
104
105 #include "opt_drm.h"
106 #ifdef DRM_DEBUG
107 #undef DRM_DEBUG
108 #define DRM_DEBUG_DEFAULT_ON 1
109 #endif /* DRM_DEBUG */
110
111 #define DRM_DEBUGBITS_DEBUG             0x1
112 #define DRM_DEBUGBITS_KMS               0x2
113 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
114
115 #undef DRM_LINUX
116 #define DRM_LINUX 0
117
118 /* driver capabilities and requirements mask */
119 #define DRIVER_USE_AGP     0x1
120 #define DRIVER_REQUIRE_AGP 0x2
121 #define DRIVER_USE_MTRR    0x4
122 #define DRIVER_PCI_DMA     0x8
123 #define DRIVER_SG          0x10
124 #define DRIVER_HAVE_DMA    0x20
125 #define DRIVER_HAVE_IRQ    0x40
126 #define DRIVER_IRQ_SHARED  0x80
127 #define DRIVER_IRQ_VBL     0x100
128 #define DRIVER_DMA_QUEUE   0x200
129 #define DRIVER_FB_DMA      0x400
130 #define DRIVER_IRQ_VBL2    0x800
131 #define DRIVER_GEM         0x1000
132 #define DRIVER_MODESET     0x2000
133 #define DRIVER_USE_PLATFORM_DEVICE  0x4000
134 #define DRIVER_LOCKLESS_IRQ 0x8000
135
136
137 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
138 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
139 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
140
141 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
142 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
143 #define DRM_GEM_MAX_IDX         0x3fffff
144 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
145 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
146 #define DRM_GEM_MAPPING_MAPOFF(o) \
147     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
148
149 MALLOC_DECLARE(DRM_MEM_DMA);
150 MALLOC_DECLARE(DRM_MEM_SAREA);
151 MALLOC_DECLARE(DRM_MEM_DRIVER);
152 MALLOC_DECLARE(DRM_MEM_MAGIC);
153 MALLOC_DECLARE(DRM_MEM_MAPS);
154 MALLOC_DECLARE(DRM_MEM_BUFS);
155 MALLOC_DECLARE(DRM_MEM_SEGS);
156 MALLOC_DECLARE(DRM_MEM_PAGES);
157 MALLOC_DECLARE(DRM_MEM_FILES);
158 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
159 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
160 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
161 MALLOC_DECLARE(DRM_MEM_SGLISTS);
162 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
163 MALLOC_DECLARE(DRM_MEM_MM);
164 MALLOC_DECLARE(DRM_MEM_HASHTAB);
165 MALLOC_DECLARE(DRM_MEM_KMS);
166
167 SYSCTL_DECL(_hw_drm);
168
169 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
170
171                                 /* Internal types and structures */
172 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
173 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
174 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
175
176 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
177
178 #define __OS_HAS_AGP    1
179
180 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
181 #define DRM_DEV_UID     0
182 #define DRM_DEV_GID     0
183
184 #define wait_queue_head_t       atomic_t
185 #define DRM_WAKEUP(w)           wakeup((void *)w)
186 #define DRM_WAKEUP_INT(w)       wakeup(w)
187 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
188
189 #define DRM_CURPROC             curthread
190 #define DRM_STRUCTPROC          struct thread
191 #define DRM_SPINTYPE            struct spinlock
192 #define DRM_SPININIT(l,name)    spin_init(l)
193 #define DRM_SPINUNINIT(l)       spin_uninit(l)
194 #define DRM_SPINLOCK(l)         spin_lock(l)
195 #define DRM_SPINUNLOCK(u)       spin_unlock(u)
196 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {          \
197         mtx_lock(l);                                    \
198         (void)irqflags;                                 \
199 } while (0)
200 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
201 #define DRM_SPINLOCK_ASSERT(l)  mtx_assert(l, MA_OWNED)
202 #define DRM_CURRENTPID          curthread->td_proc->p_pid
203 #define DRM_LOCK(dev)           lockmgr(&(dev)->dev_struct_lock, LK_EXCLUSIVE);
204 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->dev_struct_lock, LK_RELEASE);
205 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
206     (lksleep((chan), &(dev)->dev_struct_lock, (flags), (msg), (timeout)))
207 #if defined(INVARIANTS)
208 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->dev_struct_lock, curthread) != 0);
209 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->dev_struct_lock, curthread) == 0);
210 #else
211 #define DRM_LOCK_ASSERT(d)
212 #define DRM_UNLOCK_ASSERT(d)
213 #endif
214
215 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
216
217 #define DRM_IRQ_ARGS            void *arg
218 typedef void                    irqreturn_t;
219 #define IRQ_HANDLED             /* nothing */
220 #define IRQ_NONE                /* nothing */
221
222 #define unlikely(x)            __builtin_expect(!!(x), 0)
223 #define likely(x)              __builtin_expect(!!(x), 1)
224 #define container_of(ptr, type, member) ({                      \
225         __typeof( ((type *)0)->member ) *__mptr = (ptr);        \
226         (type *)( (char *)__mptr - offsetof(type,member) );})
227
228 enum {
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 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
236
237 #define PAGE_ALIGN(addr) round_page(addr)
238 /* DRM_SUSER returns true if the user is superuser */
239 #define DRM_SUSER(p)            (priv_check(p, PRIV_DRIVER) == 0)
240 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
241 #define DRM_MTRR_WC             MDF_WRITECOMBINE
242 #define jiffies                 ticks
243 #define jiffies_to_msecs(x)     (((int64_t)(x)) * 1000 / hz)
244 #define msecs_to_jiffies(x)     (((int64_t)(x)) * hz / 1000)
245 #define time_after(a,b)         ((long)(b) - (long)(a) < 0)
246 #define time_after_eq(a,b)      ((long)(b) - (long)(a) <= 0)
247 #define drm_msleep(x, msg)      pause((msg), ((int64_t)(x)) * hz / 1000)
248
249 typedef vm_paddr_t dma_addr_t;
250 typedef uint64_t u64;
251 typedef uint32_t u32;
252 typedef uint16_t u16;
253 typedef uint8_t u8;
254 typedef int64_t s64;
255 typedef int32_t s32;
256 typedef int16_t s16;
257 typedef int8_t s8;
258
259 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
260  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
261  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
262  */
263 #define DRM_READMEMORYBARRIER()         rmb()
264 #define DRM_WRITEMEMORYBARRIER()        wmb()
265 #define DRM_MEMORYBARRIER()             mb()
266
267 #define DRM_READ8(map, offset)                                          \
268         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
269             (vm_offset_t)(offset))
270 #define DRM_READ16(map, offset)                                         \
271         le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) + \
272             (vm_offset_t)(offset)))
273 #define DRM_READ32(map, offset)                                         \
274         le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) + \
275             (vm_offset_t)(offset)))
276 #define DRM_READ64(map, offset)                                         \
277         le64toh(*(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) + \
278             (vm_offset_t)(offset)))
279 #define DRM_WRITE8(map, offset, val)                                    \
280         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
281             (vm_offset_t)(offset)) = val
282 #define DRM_WRITE16(map, offset, val)                                   \
283         *(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +         \
284             (vm_offset_t)(offset)) = htole16(val)
285 #define DRM_WRITE32(map, offset, val)                                   \
286         *(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +         \
287             (vm_offset_t)(offset)) = htole32(val)
288 #define DRM_WRITE64(map, offset, val)                                   \
289         *(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) +         \
290             (vm_offset_t)(offset)) = htole64(val)
291
292 #define DRM_VERIFYAREA_READ( uaddr, size )              \
293         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
294
295 #define DRM_COPY_TO_USER(user, kern, size) \
296         copyout(kern, user, size)
297 #define DRM_COPY_FROM_USER(kern, user, size) \
298         copyin(user, kern, size)
299 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
300         copyin(arg2, arg1, arg3)
301 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
302         copyout(arg2, arg1, arg3)
303 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
304         ((val) = fuword32(uaddr), 0)
305
306 #define cpu_to_le32(x) htole32(x)
307 #define le32_to_cpu(x) le32toh(x)
308
309 #define DRM_HZ                  hz
310 #define DRM_UDELAY(udelay)      DELAY(udelay)
311 #define DRM_TIME_SLICE          (hz/20)  /* Time slice for GLXContexts    */
312
313 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
314         (_map) = (_dev)->context_sareas[_ctx];          \
315 } while(0)
316
317 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
318 do {                                                                    \
319         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
320              dev->lock.file_priv != file_priv) {                        \
321                 DRM_ERROR("%s called without lock held\n",              \
322                            __FUNCTION__);                               \
323                 return EINVAL;                                          \
324         }                                                               \
325 } while (0)
326
327 /* Returns -errno to shared code */
328 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
329 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
330         DRM_UNLOCK(dev);                                        \
331         lwkt_serialize_enter(&dev->irq_lock);                   \
332         if (!(condition))                                       \
333             ret = -mtx_sleep(&(queue), &dev->irq_lock,          \
334                 PCATCH, "drmwtq", (timeout));                   \
335         lwkt_serialize_exit(&dev->irq_lock);                    \
336         DRM_LOCK(dev);                                          \
337 }
338
339 __inline static void
340 vm_page_unhold_pages(vm_page_t *ma, int count)
341 {
342         int i;
343
344         for (i = 0; i < count; i++)
345                 vm_page_unhold(ma[i]);
346 }
347
348 vm_page_t
349 vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
350
351 int
352 vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end, int pat_mode);
353
354
355 #define DRM_ERROR(fmt, ...) \
356         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
357             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
358
359 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
360
361 #define DRM_DEBUG(fmt, ...) do {                                        \
362         if ((drm_debug_flag & DRM_DEBUGBITS_DEBUG) != 0)                \
363                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
364                         __func__ , ##__VA_ARGS__);                      \
365 } while (0)
366
367 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
368         if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)                  \
369                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
370                         __func__ , ##__VA_ARGS__);                      \
371 } while (0)
372
373 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
374         if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)                  \
375                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
376                         __func__ , ##__VA_ARGS__);                      \
377 } while (0)
378
379 typedef struct drm_pci_id_list
380 {
381         int vendor;
382         int device;
383         long driver_private;
384         char *name;
385 } drm_pci_id_list_t;
386
387 struct drm_msi_blacklist_entry
388 {
389         int vendor;
390         int device;
391 };
392
393 #define DRM_AUTH        0x1
394 #define DRM_MASTER      0x2
395 #define DRM_ROOT_ONLY   0x4
396 #define DRM_CONTROL_ALLOW 0x8
397 #define DRM_UNLOCKED    0x10
398
399 typedef struct drm_ioctl_desc {
400         unsigned long cmd;
401         int (*func)(struct drm_device *dev, void *data,
402                     struct drm_file *file_priv);
403         int flags;
404 } drm_ioctl_desc_t;
405 /**
406  * Creates a driver or general drm_ioctl_desc array entry for the given
407  * ioctl, for use by drm_ioctl().
408  */
409 #define DRM_IOCTL_DEF(ioctl, func, flags) \
410         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
411
412 typedef struct drm_magic_entry {
413         drm_magic_t            magic;
414         struct drm_file        *priv;
415         struct drm_magic_entry *next;
416 } drm_magic_entry_t;
417
418 typedef struct drm_magic_head {
419         struct drm_magic_entry *head;
420         struct drm_magic_entry *tail;
421 } drm_magic_head_t;
422
423 typedef struct drm_buf {
424         int               idx;         /* Index into master buflist          */
425         int               total;       /* Buffer size                        */
426         int               order;       /* log-base-2(total)                  */
427         int               used;        /* Amount of buffer in use (for DMA)  */
428         unsigned long     offset;      /* Byte offset (used internally)      */
429         void              *address;    /* Address of buffer                  */
430         unsigned long     bus_address; /* Bus address of buffer              */
431         struct drm_buf    *next;       /* Kernel-only: used for free list    */
432         __volatile__ int  pending;     /* On hardware DMA queue              */
433         struct drm_file   *file_priv;  /* Unique identifier of holding process */
434         int               context;     /* Kernel queue for this buffer       */
435         enum {
436                 DRM_LIST_NONE    = 0,
437                 DRM_LIST_FREE    = 1,
438                 DRM_LIST_WAIT    = 2,
439                 DRM_LIST_PEND    = 3,
440                 DRM_LIST_PRIO    = 4,
441                 DRM_LIST_RECLAIM = 5
442         }                 list;        /* Which list we're on                */
443
444         int               dev_priv_size; /* Size of buffer private stoarge   */
445         void              *dev_private;  /* Per-buffer private storage       */
446 } drm_buf_t;
447
448 typedef struct drm_freelist {
449         int               initialized; /* Freelist in use                  */
450         atomic_t          count;       /* Number of free buffers           */
451         drm_buf_t         *next;       /* End pointer                      */
452
453         int               low_mark;    /* Low water mark                   */
454         int               high_mark;   /* High water mark                  */
455 } drm_freelist_t;
456
457 typedef struct drm_dma_handle {
458         void *vaddr;
459         bus_addr_t busaddr;
460         bus_dma_tag_t tag;
461         bus_dmamap_t map;
462 } drm_dma_handle_t;
463
464 typedef struct drm_buf_entry {
465         int               buf_size;
466         int               buf_count;
467         drm_buf_t         *buflist;
468         int               seg_count;
469         drm_dma_handle_t  **seglist;
470         int               page_order;
471
472         drm_freelist_t    freelist;
473 } drm_buf_entry_t;
474
475 /* Event queued up for userspace to read */
476 struct drm_pending_event {
477         struct drm_event *event;
478         struct list_head link;
479         struct drm_file *file_priv;
480         pid_t pid; /* pid of requester, no guarantee it's valid by the time
481                       we deliver the event, for tracing only */
482         void (*destroy)(struct drm_pending_event *event);
483 };
484
485 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
486 struct drm_file {
487         TAILQ_ENTRY(drm_file) link;
488         struct drm_device *dev;
489         int               authenticated;
490         int               master;
491         pid_t             pid;
492         uid_t             uid;
493         drm_magic_t       magic;
494         unsigned long     ioctl_count;
495
496         void             *driver_priv;
497         struct drm_gem_names object_names;
498
499         int               is_master;
500         struct drm_master *masterp;
501
502         struct list_head  fbs;
503
504         struct list_head  event_list;
505         int               event_space;
506 };
507
508 typedef struct drm_lock_data {
509         struct drm_hw_lock      *hw_lock;       /* Hardware lock                   */
510         struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
511         int               lock_queue;   /* Queue of blocked processes      */
512         unsigned long     lock_time;    /* Time of last lock in jiffies    */
513 } drm_lock_data_t;
514
515 /* This structure, in the struct drm_device, is always initialized while the
516  * device
517  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
518  * when set marks that no further bufs may be allocated until device teardown
519  * occurs (when the last open of the device has closed).  The high/low
520  * watermarks of bufs are only touched by the X Server, and thus not
521  * concurrently accessed, so no locking is needed.
522  */
523 typedef struct drm_device_dma {
524         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
525         int               buf_count;
526         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
527         int               seg_count;
528         int               page_count;
529         unsigned long     *pagelist;
530         unsigned long     byte_count;
531         enum {
532                 _DRM_DMA_USE_AGP = 0x01,
533                 _DRM_DMA_USE_SG  = 0x02
534         } flags;
535 } drm_device_dma_t;
536
537 typedef struct drm_agp_mem {
538         void               *handle;
539         unsigned long      bound; /* address */
540         int                pages;
541         struct drm_agp_mem *prev;
542         struct drm_agp_mem *next;
543 } drm_agp_mem_t;
544
545 typedef struct drm_agp_head {
546         device_t           agpdev;
547         struct agp_info    info;
548         const char         *chipset;
549         drm_agp_mem_t      *memory;
550         unsigned long      mode;
551         int                enabled;
552         int                acquired;
553         unsigned long      base;
554         int                mtrr;
555         int                cant_use_aperture;
556         unsigned long      page_mask;
557 } drm_agp_head_t;
558
559 typedef struct drm_sg_mem {
560         vm_offset_t vaddr;
561         vm_paddr_t *busaddr;
562         vm_pindex_t pages;
563 } drm_sg_mem_t;
564
565 #define DRM_MAP_HANDLE_BITS     (sizeof(void *) == 4 ? 4 : 24)
566 #define DRM_MAP_HANDLE_SHIFT    (sizeof(void *) * 8 - DRM_MAP_HANDLE_BITS)
567 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
568
569 typedef struct drm_local_map {
570         unsigned long offset;     /* Physical address (0 for SAREA)       */
571         unsigned long size;       /* Physical size (bytes)                */
572         enum drm_map_type type;   /* Type of memory mapped                */
573         enum drm_map_flags flags; /* Flags                                */
574         void *handle;             /* User-space: "Handle" to pass to mmap */
575                                   /* Kernel-space: kernel-virtual address */
576         int mtrr;                 /* Boolean: MTRR used                   */
577                                   /* Private data                         */
578         int rid;                  /* PCI resource ID for bus_space        */
579         void *virtual;            /* Kernel-space: kernel-virtual address */
580         struct resource *bsr;
581         bus_space_tag_t bst;
582         bus_space_handle_t bsh;
583         drm_dma_handle_t *dmah;
584         TAILQ_ENTRY(drm_local_map) link;
585 } drm_local_map_t;
586
587 struct drm_vblank_info {
588         wait_queue_head_t queue;        /* vblank wait queue */
589         atomic_t count;                 /* number of VBLANK interrupts */
590                                         /* (driver must alloc the right number of counters) */
591         atomic_t refcount;              /* number of users of vblank interrupts */
592         u32 last;                       /* protected by dev->vbl_lock, used */
593                                         /* for wraparound handling */
594         int enabled;                    /* so we don't call enable more than */
595                                         /* once per disable */
596         int inmodeset;                  /* Display driver is setting mode */
597 };
598
599 /* Size of ringbuffer for vblank timestamps. Just double-buffer
600  * in initial implementation.
601  */
602 #define DRM_VBLANKTIME_RBSIZE 2
603
604 /* Flags and return codes for get_vblank_timestamp() driver function. */
605 #define DRM_CALLED_FROM_VBLIRQ 1
606 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
607 #define DRM_VBLANKTIME_INVBL             (1 << 1)
608
609 /* get_scanout_position() return flags */
610 #define DRM_SCANOUTPOS_VALID        (1 << 0)
611 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
612 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
613
614 /* location of GART table */
615 #define DRM_ATI_GART_MAIN 1
616 #define DRM_ATI_GART_FB   2
617
618 #define DRM_ATI_GART_PCI  1
619 #define DRM_ATI_GART_PCIE 2
620 #define DRM_ATI_GART_IGP  3
621
622 struct drm_ati_pcigart_info {
623         int gart_table_location;
624         int gart_reg_if;
625         void *addr;
626         dma_addr_t bus_addr;
627         dma_addr_t table_mask;
628         dma_addr_t member_mask;
629         struct drm_dma_handle *table_handle;
630         drm_local_map_t mapping;
631         int table_size;
632         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
633 };
634
635 typedef vm_paddr_t resource_size_t;
636
637 /**
638  * GEM specific mm private for tracking GEM objects
639  */
640 struct drm_gem_mm {
641         struct drm_open_hash offset_hash; /**< User token hash table for maps */
642         struct unrhdr *idxunr;
643 };
644
645 struct drm_gem_object {
646         /** Reference count of this object */
647         u_int refcount;
648
649         /** Handle count of this object. Each handle also holds a reference */
650         u_int handle_count; /* number of handles on this object */
651
652         /** Related drm device */
653         struct drm_device *dev;
654
655         /** File representing the shmem storage: filp in Linux parlance */
656         vm_object_t vm_obj;
657
658         bool on_map;
659         struct drm_hash_item map_list;
660
661         /**
662          * Size of the object, in bytes.  Immutable over the object's
663          * lifetime.
664          */
665         size_t size;
666
667         /**
668          * Global name for this object, starts at 1. 0 means unnamed.
669          * Access is covered by the object_name_lock in the related drm_device
670          */
671         int name;
672
673         /**
674          * Memory domains. These monitor which caches contain read/write data
675          * related to the object. When transitioning from one set of domains
676          * to another, the driver is called to ensure that caches are suitably
677          * flushed and invalidated
678          */
679         uint32_t read_domains;
680         uint32_t write_domain;
681
682         /**
683          * While validating an exec operation, the
684          * new read/write domain values are computed here.
685          * They will be transferred to the above values
686          * at the point that any cache flushing occurs
687          */
688         uint32_t pending_read_domains;
689         uint32_t pending_write_domain;
690
691         void *driver_private;
692 };
693
694 #include "drm_crtc.h"
695
696 #ifndef DMA_BIT_MASK
697 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
698 #endif
699
700 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
701
702 struct drm_driver_info {
703         int     (*load)(struct drm_device *, unsigned long flags);
704         int     (*firstopen)(struct drm_device *);
705         int     (*open)(struct drm_device *, struct drm_file *);
706         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
707         void    (*postclose)(struct drm_device *, struct drm_file *);
708         void    (*lastclose)(struct drm_device *);
709         int     (*unload)(struct drm_device *);
710         void    (*reclaim_buffers_locked)(struct drm_device *,
711                                           struct drm_file *file_priv);
712         int     (*dma_ioctl)(struct drm_device *dev, void *data,
713                              struct drm_file *file_priv);
714         void    (*dma_ready)(struct drm_device *);
715         int     (*dma_quiescent)(struct drm_device *);
716         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
717                                              enum drm_lock_flags flags);
718         int     (*dma_flush_unblock)(struct drm_device *, int context,
719                                      enum drm_lock_flags flags);
720         int     (*context_ctor)(struct drm_device *dev, int context);
721         int     (*context_dtor)(struct drm_device *dev, int context);
722         int     (*kernel_context_switch)(struct drm_device *dev, int old,
723                                          int new);
724         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
725         void    (*irq_preinstall)(struct drm_device *dev);
726         int     (*irq_postinstall)(struct drm_device *dev);
727         void    (*irq_uninstall)(struct drm_device *dev);
728         void    (*irq_handler)(DRM_IRQ_ARGS);
729
730         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
731         int     (*enable_vblank)(struct drm_device *dev, int crtc);
732         void    (*disable_vblank)(struct drm_device *dev, int crtc);
733         int     (*get_scanout_position)(struct drm_device *dev, int crtc,
734                     int *vpos, int *hpos);
735
736         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
737                     int *max_error, struct timeval *vblank_time,
738                     unsigned flags);
739
740         int     (*gem_init_object)(struct drm_gem_object *obj);
741         void    (*gem_free_object)(struct drm_gem_object *obj);
742
743         struct cdev_pager_ops *gem_pager_ops;
744
745         int     (*dumb_create)(struct drm_file *file_priv,
746                     struct drm_device *dev, struct drm_mode_create_dumb *args);
747         int     (*dumb_map_offset)(struct drm_file *file_priv,
748                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
749         int     (*dumb_destroy)(struct drm_file *file_priv,
750                     struct drm_device *dev, uint32_t handle);
751
752         int     (*sysctl_init)(struct drm_device *dev,
753                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
754         void    (*sysctl_cleanup)(struct drm_device *dev);
755
756         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
757
758         /**
759          * Called by \c drm_device_is_agp.  Typically used to determine if a
760          * card is really attached to AGP or not.
761          *
762          * \param dev  DRM device handle
763          *
764          * \returns 
765          * One of three values is returned depending on whether or not the
766          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
767          * (return of 1), or may or may not be AGP (return of 2).
768          */
769         int     (*device_is_agp) (struct drm_device * dev);
770
771         drm_ioctl_desc_t *ioctls;
772         int     max_ioctl;
773
774         int     buf_priv_size;
775
776         int     major;
777         int     minor;
778         int     patchlevel;
779         const char *name;               /* Simple driver name              */
780         const char *desc;               /* Longer driver name              */
781         const char *date;               /* Date of last major changes.     */
782
783         u32 driver_features;
784 };
785
786 /**
787  * DRM minor structure. This structure represents a drm minor number.
788  */
789 struct drm_minor {
790         int index;                      /**< Minor device number */
791         int type;                       /**< Control or render */
792         device_t kdev;                  /**< OS device */
793         struct drm_device *dev;
794
795         struct drm_master *master; /* currently active master for this node */
796         struct list_head master_list;
797         struct drm_mode_group mode_group;
798 };
799
800 /* mode specified on the command line */
801 struct drm_cmdline_mode {
802         bool specified;
803         bool refresh_specified;
804         bool bpp_specified;
805         int xres, yres;
806         int bpp;
807         int refresh;
808         bool rb;
809         bool interlace;
810         bool cvt;
811         bool margins;
812         enum drm_connector_force force;
813 };
814
815 struct drm_pending_vblank_event {
816         struct drm_pending_event base;
817         int pipe;
818         struct drm_event_vblank event;
819 };
820
821 /* Length for the array of resource pointers for drm_get_resource_*. */
822 #define DRM_MAX_PCI_RESOURCE    6
823
824 /** 
825  * DRM device functions structure
826  */
827 struct drm_device {
828         struct drm_driver_info *driver;
829         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
830
831         u_int16_t pci_device;           /* PCI device id */
832         u_int16_t pci_vendor;           /* PCI vendor id */
833
834         char              *unique;      /* Unique identifier: e.g., busid  */
835         int               unique_len;   /* Length of unique field          */
836         device_t          device;       /* Device instance from newbus     */
837         struct cdev       *devnode;     /* Device number for mknod         */
838         int               if_version;   /* Highest interface version set */
839
840         int               flags;        /* Flags to open(2)                */
841
842                                 /* Locks */
843         struct spinlock   dma_lock;     /* protects dev->dma */
844         struct lwkt_serialize irq_lock; /* protects irq condition checks */
845         struct lock       dev_lock;     /* protects everything else */
846         struct lock       dev_struct_lock;
847         DRM_SPINTYPE      drw_lock;
848
849                                 /* Usage Counters */
850         int               open_count;   /* Outstanding files open          */
851         int               buf_use;      /* Buffers in use -- cannot alloc  */
852
853                                 /* Performance counters */
854         unsigned long     counters;
855         enum drm_stat_type      types[15];
856         atomic_t          counts[15];
857
858                                 /* Authentication */
859         drm_file_list_t   files;
860         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
861
862         /* Linked list of mappable regions. Protected by dev_lock */
863         drm_map_list_t    maplist;
864         struct unrhdr     *map_unrhdr;
865
866         drm_local_map_t   **context_sareas;
867         int               max_context;
868
869         drm_lock_data_t   lock;         /* Information on hardware lock    */
870
871                                 /* DMA queues (contexts) */
872         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
873
874                                 /* Context support */
875         int               irq;          /* Interrupt used by board         */
876         int               irq_enabled;  /* True if the irq handler is enabled */
877         int               msi_enabled;  /* MSI enabled */
878         int               irqrid;       /* Interrupt used by board */
879         struct resource   *irqr;        /* Resource for interrupt used by board    */
880         void              *irqh;        /* Handle from bus_setup_intr      */
881
882         /* Storage of resource pointers for drm_get_resource_* */
883         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
884         int               pcirid[DRM_MAX_PCI_RESOURCE];
885
886         int               pci_domain;
887         int               pci_bus;
888         int               pci_slot;
889         int               pci_func;
890
891         atomic_t          context_flag; /* Context swapping flag           */
892         int               last_context; /* Last current context            */
893
894         int               num_crtcs;
895
896         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
897
898                                 /* Sysctl support */
899         struct drm_sysctl_info *sysctl;
900         int               sysctl_node_idx;
901
902         drm_agp_head_t    *agp;
903         drm_sg_mem_t      *sg;  /* Scatter gather memory */
904         atomic_t          *ctx_bitmap;
905         void              *dev_private;
906         unsigned int      agp_buffer_token;
907         drm_local_map_t   *agp_buffer_map;
908
909         struct drm_minor *control;              /**< Control node for card */
910         struct drm_minor *primary;              /**< render type primary screen head */
911
912         void              *drm_ttm_bo;
913         struct unrhdr     *drw_unrhdr;
914         /* RB tree of drawable infos */
915         RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
916
917         int vblank_disable_allowed;
918
919         atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
920         struct timeval *_vblank_time;   /**< timestamp of current vblank_count (drivers must alloc right number of fields) */
921         struct lock vblank_time_lock;   /**< Protects vblank count and time updates during vblank enable/disable */
922         struct lock vbl_lock;
923         atomic_t *vblank_refcount;      /* number of users of vblank interruptsper crtc */
924         u32 *last_vblank;               /* protected by dev->vbl_lock, used */
925                                         /* for wraparound handling */
926         int *vblank_enabled;            /* so we don't call enable more than
927                                            once per disable */
928         int *vblank_inmodeset;          /* Display driver is setting mode */
929         u32 *last_vblank_wait;          /* Last vblank seqno waited per CRTC */
930         struct callout vblank_disable_callout;
931
932         u32 max_vblank_count;           /**< size of vblank counter register */
933
934         struct list_head vblank_event_list;
935         struct lock      event_lock;
936
937         struct drm_mode_config mode_config;     /**< Current mode config */
938
939         /* GEM part */
940         struct lock       object_name_lock;
941         struct drm_gem_names object_names;
942         void             *mm_private;
943
944         void *sysctl_private;
945         char busid_str[128];
946         int modesetting;
947 };
948
949 static __inline__ int drm_core_check_feature(struct drm_device *dev,
950                                              int feature)
951 {
952         return ((dev->driver->driver_features & feature) ? 1 : 0);
953 }
954
955 #if __OS_HAS_AGP
956 static inline int drm_core_has_AGP(struct drm_device *dev)
957 {
958         return drm_core_check_feature(dev, DRIVER_USE_AGP);
959 }
960 #else
961 #define drm_core_has_AGP(dev) (0)
962 #endif
963
964 enum dmi_field {
965         DMI_NONE,
966         DMI_BIOS_VENDOR,
967         DMI_BIOS_VERSION,
968         DMI_BIOS_DATE,
969         DMI_SYS_VENDOR,
970         DMI_PRODUCT_NAME,
971         DMI_PRODUCT_VERSION,
972         DMI_PRODUCT_SERIAL,
973         DMI_PRODUCT_UUID,
974         DMI_BOARD_VENDOR,
975         DMI_BOARD_NAME,
976         DMI_BOARD_VERSION,
977         DMI_BOARD_SERIAL,
978         DMI_BOARD_ASSET_TAG,
979         DMI_CHASSIS_VENDOR,
980         DMI_CHASSIS_TYPE,
981         DMI_CHASSIS_VERSION,
982         DMI_CHASSIS_SERIAL,
983         DMI_CHASSIS_ASSET_TAG,
984         DMI_STRING_MAX,
985 };
986
987 struct dmi_strmatch {
988         unsigned char slot;
989         char substr[79];
990 };
991
992 struct dmi_system_id {
993         int (*callback)(const struct dmi_system_id *);
994         const char *ident;
995         struct dmi_strmatch matches[4];
996 };
997 #define DMI_MATCH(a, b) {(a), (b)}
998 bool dmi_check_system(const struct dmi_system_id *);
999
1000 extern int      drm_debug_flag;
1001 extern int      drm_notyet_flag;
1002 extern unsigned int drm_vblank_offdelay;
1003 extern unsigned int drm_timestamp_precision;
1004
1005 /* Device setup support (drm_drv.c) */
1006 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1007 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1008 int     drm_create_cdevs(device_t kdev);
1009 int     drm_detach(device_t kdev);
1010 d_ioctl_t drm_ioctl;
1011 d_open_t drm_open;
1012 d_close_t drm_close;
1013 d_read_t drm_read;
1014 d_kqfilter_t drm_kqfilter;
1015 d_mmap_t drm_mmap;
1016 d_mmap_single_t drm_mmap_single;
1017 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
1018
1019 void drm_event_wakeup(struct drm_pending_event *e);
1020
1021 int drm_add_busid_modesetting(struct drm_device *dev,
1022     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1023
1024 /* File operations helpers (drm_fops.c) */
1025 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1026                                          DRM_STRUCTPROC *p,
1027                                         struct drm_device *dev);
1028 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1029                                         DRM_STRUCTPROC *p);
1030
1031 /* Memory management support (drm_memory.c) */
1032 void    drm_mem_init(void);
1033 void    drm_mem_uninit(void);
1034 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1035 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1036 void    drm_ioremapfree(drm_local_map_t *map);
1037 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1038 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1039
1040 int     drm_context_switch(struct drm_device *dev, int old, int new);
1041 int     drm_context_switch_complete(struct drm_device *dev, int new);
1042
1043 int     drm_ctxbitmap_init(struct drm_device *dev);
1044 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
1045 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1046 int     drm_ctxbitmap_next(struct drm_device *dev);
1047
1048 /* Locking IOCTL support (drm_lock.c) */
1049 int     drm_lock_take(struct drm_lock_data *lock_data,
1050                       unsigned int context);
1051 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1052                           unsigned int context);
1053 int     drm_lock_free(struct drm_lock_data *lock_data,
1054                       unsigned int context);
1055
1056 /* Buffer management support (drm_bufs.c) */
1057 unsigned long drm_get_resource_start(struct drm_device *dev,
1058                                      unsigned int resource);
1059 unsigned long drm_get_resource_len(struct drm_device *dev,
1060                                    unsigned int resource);
1061 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1062 int     drm_order(unsigned long size);
1063 int     drm_addmap(struct drm_device *dev, unsigned long offset,
1064                    unsigned long size,
1065                    enum drm_map_type type, enum drm_map_flags flags,
1066                    drm_local_map_t **map_ptr);
1067 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1068 int     drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
1069 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1070
1071 /* DMA support (drm_dma.c) */
1072 int     drm_dma_setup(struct drm_device *dev);
1073 void    drm_dma_takedown(struct drm_device *dev);
1074 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1075 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1076 #define drm_core_reclaim_buffers drm_reclaim_buffers
1077
1078 /* IRQ support (drm_irq.c) */
1079 int     drm_irq_install(struct drm_device *dev);
1080 int     drm_irq_uninstall(struct drm_device *dev);
1081 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1082 void    drm_driver_irq_preinstall(struct drm_device *dev);
1083 void    drm_driver_irq_postinstall(struct drm_device *dev);
1084 void    drm_driver_irq_uninstall(struct drm_device *dev);
1085
1086 void    drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1087 void    drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1088 int     drm_modeset_ctl(struct drm_device *dev, void *data,
1089                         struct drm_file *file_priv);
1090
1091 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1092 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1093                            struct drm_file *filp);
1094 extern int drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
1095 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1096 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1097                                      struct timeval *vblanktime);
1098 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1099 void drm_handle_vblank_events(struct drm_device *dev, int crtc);
1100 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1101 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1102 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1103 extern void drm_vblank_cleanup(struct drm_device *dev);
1104 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1105                                      struct timeval *tvblank, unsigned flags);
1106 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1107                                                  int crtc, int *max_error,
1108                                                  struct timeval *vblank_time,
1109                                                  unsigned flags,
1110                                                  struct drm_crtc *refcrtc);
1111 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc);
1112
1113 struct timeval ns_to_timeval(const int64_t nsec);
1114 int64_t timeval_to_ns(const struct timeval *tv);
1115
1116 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1117 int     drm_device_is_agp(struct drm_device *dev);
1118 int     drm_device_is_pcie(struct drm_device *dev);
1119 drm_agp_head_t *drm_agp_init(void);
1120 int     drm_agp_acquire(struct drm_device *dev);
1121 int     drm_agp_release(struct drm_device *dev);
1122 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1123 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1124 void    *drm_agp_allocate_memory(size_t pages, u32 type);
1125 int     drm_agp_free_memory(void *handle);
1126 int     drm_agp_bind_memory(void *handle, off_t start);
1127 int     drm_agp_unbind_memory(void *handle);
1128 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1129 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1130 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1131 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1132
1133 /* Scatter Gather Support (drm_scatter.c) */
1134 void    drm_sg_cleanup(drm_sg_mem_t *entry);
1135 int     drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1136
1137 /* sysctl support (drm_sysctl.h) */
1138 extern int              drm_sysctl_init(struct drm_device *dev);
1139 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1140
1141 /* ATI PCIGART support (ati_pcigart.c) */
1142 int     drm_ati_pcigart_init(struct drm_device *dev,
1143                                 struct drm_ati_pcigart_info *gart_info);
1144 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
1145                                 struct drm_ati_pcigart_info *gart_info);
1146
1147 /* Cache management (drm_memory.c) */
1148 void    drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1149
1150 /* Locking IOCTL support (drm_drv.c) */
1151 int     drm_lock(struct drm_device *dev, void *data,
1152                  struct drm_file *file_priv);
1153 int     drm_unlock(struct drm_device *dev, void *data,
1154                    struct drm_file *file_priv);
1155 int     drm_version(struct drm_device *dev, void *data,
1156                     struct drm_file *file_priv);
1157 int     drm_setversion(struct drm_device *dev, void *data,
1158                        struct drm_file *file_priv);
1159
1160 /* Misc. IOCTL support (drm_ioctl.c) */
1161 int     drm_irq_by_busid(struct drm_device *dev, void *data,
1162                          struct drm_file *file_priv);
1163 int     drm_getunique(struct drm_device *dev, void *data,
1164                       struct drm_file *file_priv);
1165 int     drm_setunique(struct drm_device *dev, void *data,
1166                       struct drm_file *file_priv);
1167 int     drm_getmap(struct drm_device *dev, void *data,
1168                    struct drm_file *file_priv);
1169 int     drm_getclient(struct drm_device *dev, void *data,
1170                       struct drm_file *file_priv);
1171 int     drm_getstats(struct drm_device *dev, void *data,
1172                      struct drm_file *file_priv);
1173 int     drm_getcap(struct drm_device *dev, void *data,
1174                      struct drm_file *file_priv);
1175 int     drm_noop(struct drm_device *dev, void *data,
1176                  struct drm_file *file_priv);
1177
1178 /* Context IOCTL support (drm_context.c) */
1179 int     drm_resctx(struct drm_device *dev, void *data,
1180                    struct drm_file *file_priv);
1181 int     drm_addctx(struct drm_device *dev, void *data,
1182                    struct drm_file *file_priv);
1183 int     drm_modctx(struct drm_device *dev, void *data,
1184                    struct drm_file *file_priv);
1185 int     drm_getctx(struct drm_device *dev, void *data,
1186                    struct drm_file *file_priv);
1187 int     drm_switchctx(struct drm_device *dev, void *data,
1188                       struct drm_file *file_priv);
1189 int     drm_newctx(struct drm_device *dev, void *data,
1190                    struct drm_file *file_priv);
1191 int     drm_rmctx(struct drm_device *dev, void *data,
1192                   struct drm_file *file_priv);
1193 int     drm_setsareactx(struct drm_device *dev, void *data,
1194                         struct drm_file *file_priv);
1195 int     drm_getsareactx(struct drm_device *dev, void *data,
1196                         struct drm_file *file_priv);
1197
1198 /* Drawable IOCTL support (drm_drawable.c) */
1199 int     drm_adddraw(struct drm_device *dev, void *data,
1200                     struct drm_file *file_priv);
1201 int     drm_rmdraw(struct drm_device *dev, void *data,
1202                    struct drm_file *file_priv);
1203 int     drm_update_draw(struct drm_device *dev, void *data,
1204                         struct drm_file *file_priv);
1205 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1206                                                 int handle);
1207
1208 /* Drawable support (drm_drawable.c) */
1209 void drm_drawable_free_all(struct drm_device *dev);
1210
1211 /* Authentication IOCTL support (drm_auth.c) */
1212 int     drm_getmagic(struct drm_device *dev, void *data,
1213                      struct drm_file *file_priv);
1214 int     drm_authmagic(struct drm_device *dev, void *data,
1215                       struct drm_file *file_priv);
1216
1217 /* Buffer management support (drm_bufs.c) */
1218 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
1219                          struct drm_file *file_priv);
1220 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
1221                         struct drm_file *file_priv);
1222 int     drm_addbufs(struct drm_device *dev, void *data,
1223                     struct drm_file *file_priv);
1224 int     drm_infobufs(struct drm_device *dev, void *data,
1225                      struct drm_file *file_priv);
1226 int     drm_markbufs(struct drm_device *dev, void *data,
1227                      struct drm_file *file_priv);
1228 int     drm_freebufs(struct drm_device *dev, void *data,
1229                      struct drm_file *file_priv);
1230 int     drm_mapbufs(struct drm_device *dev, void *data,
1231                     struct drm_file *file_priv);
1232
1233 /* DMA support (drm_dma.c) */
1234 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1235
1236 /* IRQ support (drm_irq.c) */
1237 int     drm_control(struct drm_device *dev, void *data,
1238                     struct drm_file *file_priv);
1239
1240 /* AGP/GART support (drm_agpsupport.c) */
1241 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1242                               struct drm_file *file_priv);
1243 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
1244                               struct drm_file *file_priv);
1245 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1246                              struct drm_file *file_priv);
1247 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
1248                            struct drm_file *file_priv);
1249 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1250                             struct drm_file *file_priv);
1251 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
1252                            struct drm_file *file_priv);
1253 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1254                              struct drm_file *file_priv);
1255 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1256                            struct drm_file *file_priv);
1257
1258                                 /* Stub support (drm_stub.h) */
1259 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1260                                struct drm_file *file_priv);
1261 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1262                                 struct drm_file *file_priv);
1263
1264 /* Scatter Gather Support (drm_scatter.c) */
1265 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1266                            struct drm_file *file_priv);
1267 int     drm_sg_free(struct drm_device *dev, void *data,
1268                     struct drm_file *file_priv);
1269
1270 /* consistent PCI memory functions (drm_pci.c) */
1271 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1272                                 size_t align, dma_addr_t maxaddr);
1273 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1274
1275 /* Graphics Execution Manager library functions (drm_gem.c) */
1276 int drm_gem_init(struct drm_device *dev);
1277 void drm_gem_destroy(struct drm_device *dev);
1278
1279 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1280                         struct drm_file *file_priv);
1281 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1282                         struct drm_file *file_priv);
1283 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1284                        struct drm_file *file_priv);
1285 int drm_gem_handle_create(struct drm_file *file_priv,
1286                           struct drm_gem_object *obj,
1287                           u32 *handlep);
1288 int drm_gem_handle_delete(struct drm_file *file_priv, uint32_t handle);
1289 void drm_gem_object_handle_reference(struct drm_gem_object *obj);
1290 void drm_gem_object_handle_unreference(struct drm_gem_object *obj);
1291 void drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj);
1292 void drm_gem_object_handle_free(struct drm_gem_object *obj);
1293 void drm_gem_object_reference(struct drm_gem_object *obj);
1294 void drm_gem_object_unreference(struct drm_gem_object *obj);
1295 void drm_gem_object_unreference_unlocked(struct drm_gem_object *obj);
1296 void drm_gem_object_release(struct drm_gem_object *obj);
1297 void drm_gem_object_free(struct drm_gem_object *obj);
1298 int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,
1299     size_t size);
1300 int drm_gem_private_object_init(struct drm_device *dev,
1301     struct drm_gem_object *obj, size_t size);
1302 struct drm_gem_object *drm_gem_object_alloc(struct drm_device *dev,
1303     size_t size);
1304 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1305     struct drm_file *file_priv, uint32_t handle);
1306
1307 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1308 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1309
1310 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1311 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1312 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1313     vm_size_t size, struct vm_object **obj_res, int nprot);
1314 void drm_gem_pager_dtr(void *obj);
1315
1316 struct ttm_bo_device;
1317 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1318     vm_size_t size, struct vm_object **obj_res, int nprot);
1319
1320 void drm_device_lock_mtx(struct drm_device *dev);
1321 void drm_device_unlock_mtx(struct drm_device *dev);
1322 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1323     const char *msg, int timeout);
1324 void drm_device_assert_mtx_locked(struct drm_device *dev);
1325 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1326
1327 void drm_device_lock_struct(struct drm_device *dev);
1328 void drm_device_unlock_struct(struct drm_device *dev);
1329 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1330     const char *msg, int timeout);
1331 void drm_device_assert_struct_locked(struct drm_device *dev);
1332 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1333
1334 void drm_compat_locking_init(struct drm_device *dev);
1335 void drm_sleep_locking_init(struct drm_device *dev);
1336
1337 /* drm_modes.c */
1338 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1339     struct drm_connector *connector, struct drm_cmdline_mode *mode);
1340 struct drm_display_mode *drm_mode_create_from_cmdline_mode(
1341     struct drm_device *dev, struct drm_cmdline_mode *cmd);
1342
1343 /* drm_edid.c */
1344 u8 *drm_find_cea_extension(struct edid *edid);
1345
1346 /* Inline replacements for drm_alloc and friends */
1347 static __inline__ void *
1348 drm_alloc(size_t size, struct malloc_type *area)
1349 {
1350         return kmalloc(size, area, M_NOWAIT);
1351 }
1352
1353 static __inline__ void *
1354 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1355 {
1356         return kmalloc(size * nmemb, area, M_NOWAIT | M_ZERO);
1357 }
1358
1359 static __inline__ void *
1360 drm_realloc(void *oldpt, size_t oldsize, size_t size,
1361     struct malloc_type *area)
1362 {
1363         void *res;
1364         res = krealloc(oldpt, size, area, M_NOWAIT);
1365         if (res == NULL && oldpt != NULL)
1366                 kfree(oldpt,area);
1367         return res;
1368 }
1369
1370 static __inline__ void
1371 drm_free(void *pt, struct malloc_type *area)
1372 {
1373         if (pt != NULL)
1374                 kfree(pt, area);
1375 }
1376
1377 /* Inline replacements for DRM_IOREMAP macros */
1378 static __inline__ void
1379 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1380 {
1381         map->virtual = drm_ioremap_wc(dev, map);
1382 }
1383 static __inline__ void
1384 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1385 {
1386         map->virtual = drm_ioremap(dev, map);
1387 }
1388 static __inline__ void
1389 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1390 {
1391         if ( map->virtual && map->size )
1392                 drm_ioremapfree(map);
1393 }
1394
1395 static __inline__ struct drm_local_map *
1396 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1397 {
1398         drm_local_map_t *map;
1399
1400         DRM_LOCK_ASSERT(dev);
1401         TAILQ_FOREACH(map, &dev->maplist, link) {
1402                 if (offset == (unsigned long)map->handle)
1403                         return map;
1404         }
1405         return NULL;
1406 }
1407
1408 static __inline__ void drm_core_dropmap(struct drm_map *map)
1409 {
1410 }
1411
1412 #define KIB_NOTYET()                                                    \
1413 do {                                                                    \
1414         if (drm_debug_flag && drm_notyet_flag)                          \
1415                 kprintf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
1416 } while (0)
1417
1418 #define KTR_DRM         KTR_DEV
1419 #define KTR_DRM_REG     KTR_SPARE3
1420
1421
1422 /* FreeBSD compatibility macros */
1423 #define PROC_LOCK(p)
1424 #define PROC_UNLOCK(p)
1425
1426 #define VM_OBJECT_RLOCK(object)         VM_OBJECT_LOCK(object)
1427 #define VM_OBJECT_RUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1428 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1429 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1430
1431 #endif /* __KERNEL__ */
1432 #endif /* _DRM_P_H_ */