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