drm: Improve drm_cache.c
[dragonfly.git] / sys / dev / drm / include / drm / drmP.h
1 /*
2  * Internal Header for the Direct Rendering Manager
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * Copyright (c) 2009-2010, Code Aurora Forum.
7  * All rights reserved.
8  *
9  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10  * Author: Gareth Hughes <gareth@valinux.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the next
20  * paragraph) shall be included in all copies or substantial portions of the
21  * Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  */
31
32 #ifndef _DRM_P_H_
33 #define _DRM_P_H_
34
35 #if defined(_KERNEL) || defined(__KERNEL__)
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/module.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/sglist.h>
46 #include <sys/stat.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/lock.h>
50 #include <sys/spinlock.h>
51 #include <sys/spinlock2.h>
52 #include <sys/fcntl.h>
53 #include <sys/uio.h>
54 #include <sys/filio.h>
55 #include <sys/sysctl.h>
56 #include <sys/bus.h>
57 #include <sys/queue.h>
58 #include <sys/signalvar.h>
59 #include <sys/poll.h>
60 #include <linux/highmem.h>
61 #include <sys/sbuf.h>
62 #include <sys/taskqueue.h>
63 #include <sys/tree.h>
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page2.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_param.h>
73 #include <machine/param.h>
74 #include <machine/pmap.h>
75 #ifdef __x86_64__
76 #include <machine/specialreg.h>
77 #endif
78 #include <machine/sysarch.h>
79 #include <sys/endian.h>
80 #include <sys/mman.h>
81 #include <sys/rman.h>
82 #include <sys/memrange.h>
83 #include <sys/agpio.h>
84 #include <sys/mutex.h>
85
86 MALLOC_DECLARE(M_DRM);
87
88 #include <uapi_drm/drm.h>
89 #include <uapi_drm/drm_sarea.h>
90
91 #include <linux/atomic.h>
92 #include <linux/bug.h>
93 #include <linux/dma-mapping.h>
94 #include <linux/capability.h>
95 #include <linux/err.h>
96 #include <linux/idr.h>
97 #include <linux/pci.h>
98 #include <linux/jiffies.h>
99 #include <linux/kernel.h>
100 #include <linux/fs.h>
101 #include <linux/kref.h>
102 #include <linux/list.h>
103 #include <linux/mm.h>
104 #include <linux/moduleparam.h>
105 #include <linux/mutex.h>
106 #include <linux/slab.h>
107 #include <linux/scatterlist.h>
108 #include <linux/timer.h>
109 #include <asm/io.h>
110 #include <linux/seq_file.h>
111 #include <linux/types.h>
112 #include <linux/vmalloc.h>
113 #include <linux/wait.h>
114 #include <linux/workqueue.h>
115
116 #include <asm/uaccess.h>
117
118 #include <drm/drm_global.h>
119
120 #include <drm/drm_vma_manager.h>
121
122 #include <drm/drm_os_linux.h>
123 #include <drm/drm_hashtab.h>
124 #include <drm/drm_mm.h>
125
126 struct drm_file;
127 struct drm_device;
128 struct drm_agp_head;
129 struct drm_gem_object;
130
131 struct device_node;
132 struct videomode;
133 struct reservation_object;
134 struct dma_buf_attachment;
135
136 /*
137  * 4 debug categories are defined:
138  *
139  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
140  *       This is the category used by the DRM_DEBUG() macro.
141  *
142  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
143  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
144  *
145  * KMS: used in the modesetting code.
146  *      This is the category used by the DRM_DEBUG_KMS() macro.
147  *
148  * PRIME: used in the prime code.
149  *        This is the category used by the DRM_DEBUG_PRIME() macro.
150  *
151  * ATOMIC: used in the atomic code.
152  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
153  *
154  * Enabling verbose debug messages is done through the drm.debug parameter,
155  * each category being enabled by a bit.
156  *
157  * drm.debug=0x1 will enable CORE messages
158  * drm.debug=0x2 will enable DRIVER messages
159  * drm.debug=0x3 will enable CORE and DRIVER messages
160  * ...
161  * drm.debug=0xf will enable all messages
162  *
163  * An interesting feature is that it's possible to enable verbose logging at
164  * run-time by echoing the debug value in its sysfs node:
165  *   # echo 0xf > /sys/module/drm/parameters/debug
166  */
167 #define DRM_UT_CORE             0x01
168 #define DRM_UT_DRIVER           0x02
169 #define DRM_UT_KMS              0x04
170 #define DRM_UT_PRIME            0x08
171 #define DRM_UT_ATOMIC           0x10
172
173 void drm_ut_debug_printk(const char *function_name,
174                          const char *format, ...);
175
176 void drm_err(const char *format, ...);
177
178 /***********************************************************************/
179 /** \name DRM template customization defaults */
180 /*@{*/
181
182 /* driver capabilities and requirements mask */
183 #define DRIVER_USE_AGP     0x1
184 #define DRIVER_REQUIRE_AGP 0x2
185 #define DRIVER_PCI_DMA     0x8
186 #define DRIVER_SG          0x10
187 #define DRIVER_HAVE_DMA    0x20
188 #define DRIVER_HAVE_IRQ    0x40
189 #define DRIVER_IRQ_SHARED  0x80
190 #define DRIVER_DMA_QUEUE   0x200
191 #define DRIVER_GEM         0x1000
192 #define DRIVER_MODESET     0x2000
193 #define DRIVER_PRIME       0x4000
194 #define DRIVER_RENDER      0x8000
195 #define DRIVER_ATOMIC      0x10000
196
197 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
198
199 /***********************************************************************/
200 /** \name Macros to make printk easier */
201 /*@{*/
202
203 /**
204  * Error output.
205  *
206  * \param fmt printf() like format string.
207  * \param arg arguments
208  */
209 #define DRM_ERROR(fmt, ...) \
210         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
211             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
212
213 /**
214  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
215  *
216  * \param fmt printf() like format string.
217  * \param arg arguments
218  */
219
220 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
221
222 #define DRM_INFO_ONCE(fmt, ...)                         \
223         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
224
225 /**
226  * Debug output.
227  *
228  * \param fmt printf() like format string.
229  * \param arg arguments
230  */
231
232 #define DRM_DEBUGBITS_DEBUG             0x1
233 #define DRM_DEBUGBITS_KMS               0x2
234 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
235 #define DRM_DEBUGBITS_VERBOSE           0x8
236
237 #define DRM_DEBUG(fmt, ...) do {                                        \
238         if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)             \
239                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
240                         __func__ , ##__VA_ARGS__);                      \
241 } while (0)
242
243 #define DRM_DEBUG_VERBOSE(fmt, ...) do {                                \
244         if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)           \
245                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
246                         __func__ , ##__VA_ARGS__);                      \
247 } while (0)
248
249 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
250         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
251                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
252                         __func__ , ##__VA_ARGS__);                      \
253 } while (0)
254
255 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
256         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
257                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
258                         __func__ , ##__VA_ARGS__);                      \
259 } while (0)
260
261 #define DRM_DEBUG_ATOMIC(fmt, args...)                                  \
262         do {                                                            \
263                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                \
264                         drm_ut_debug_printk(__func__, fmt, ##args);     \
265         } while (0)
266
267 /*@}*/
268
269 /***********************************************************************/
270 /** \name Internal types and structures */
271 /*@{*/
272
273 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
274 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
275 #define DRM_GEM_MAX_IDX         0x3fffff
276 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
277 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
278 #define DRM_GEM_MAPPING_MAPOFF(o) \
279     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
280
281 SYSCTL_DECL(_hw_drm);
282
283 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
284
285 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
286
287 #define __OS_HAS_AGP    1
288
289 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
290 #define DRM_DEV_UID     UID_ROOT
291 #define DRM_DEV_GID     GID_WHEEL
292
293 #define DRM_CURPROC             curthread
294 #define DRM_STRUCTPROC          struct thread
295 #define DRM_CURRENTPID          (curproc != NULL ? curproc->p_pid : -1)
296 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
297 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
298 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
299     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
300 #if defined(INVARIANTS)
301 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
302 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
303 #else
304 #define DRM_LOCK_ASSERT(d)
305 #define DRM_UNLOCK_ASSERT(d)
306 #endif
307
308 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
309
310 typedef void                    irqreturn_t;
311 #define IRQ_HANDLED             /* nothing */
312 #define IRQ_NONE                /* nothing */
313
314 enum {
315         DRM_IS_NOT_AGP,
316         DRM_IS_AGP,
317         DRM_MIGHT_BE_AGP
318 };
319 #define DRM_AGP_MEM             struct agp_memory_info
320
321 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
322
323 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
324 #define DRM_MTRR_WC             MDF_WRITECOMBINE
325
326 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
327 do {                                                                    \
328         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
329              dev->lock.file_priv != file_priv) {                        \
330                 DRM_ERROR("%s called without lock held\n",              \
331                            __FUNCTION__);                               \
332                 return EINVAL;                                          \
333         }                                                               \
334 } while (0)
335
336 /* Returns -errno to shared code */
337 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
338 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
339         lwkt_serialize_enter(&dev->irq_lock);                   \
340         if (!(condition)) {                                     \
341             tsleep_interlock(&(queue), PCATCH);                 \
342             lwkt_serialize_exit(&dev->irq_lock);                \
343             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,      \
344                           "drmwtq", (timeout));                 \
345         } else {                                                \
346                 lwkt_serialize_exit(&dev->irq_lock);            \
347         }                                                       \
348 }
349
350 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
351     vm_memattr_t memattr);
352 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
353 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
354
355
356 #define dev_dbg(dev, fmt, ...) do {                                     \
357         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
358                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
359         }                                                               \
360 } while (0)
361
362 typedef struct drm_pci_id_list
363 {
364         int vendor;
365         int device;
366         long driver_private;
367         char *name;
368 } drm_pci_id_list_t;
369
370 /**
371  * Ioctl function type.
372  *
373  * \param inode device inode.
374  * \param file_priv DRM file private pointer.
375  * \param cmd command.
376  * \param arg argument.
377  */
378 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
379                         struct drm_file *file_priv);
380
381 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
382                                unsigned long arg);
383
384 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
385
386 #define DRM_AUTH        0x1
387 #define DRM_MASTER      0x2
388 #define DRM_ROOT_ONLY   0x4
389 #define DRM_CONTROL_ALLOW 0x8
390 #define DRM_UNLOCKED    0x10
391 #define DRM_RENDER_ALLOW 0x20
392
393 struct drm_ioctl_desc {
394         unsigned int cmd;
395         int flags;
396         drm_ioctl_t *func;
397         unsigned int cmd_drv;
398         const char *name;
399 };
400
401 /**
402  * Creates a driver or general drm_ioctl_desc array entry for the given
403  * ioctl, for use by drm_ioctl().
404  */
405 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
406         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
407
408 typedef struct drm_magic_entry {
409         struct list_head head;
410         struct drm_hash_item hash_item;
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 /** bufs is one longer than it has to be */
448 struct drm_waitlist {
449         int count;                      /**< Number of possible buffers */
450         struct drm_buf **bufs;          /**< List of pointers to buffers */
451         struct drm_buf **rp;                    /**< Read pointer */
452         struct drm_buf **wp;                    /**< Write pointer */
453         struct drm_buf **end;           /**< End pointer */
454         struct spinlock *read_lock;
455         struct spinlock *write_lock;
456 };
457
458 typedef struct drm_dma_handle {
459         void *vaddr;
460         size_t size;
461         bus_addr_t busaddr;
462         bus_dma_tag_t tag;
463         bus_dmamap_t map;
464 } drm_dma_handle_t;
465
466 typedef struct drm_buf_entry {
467         int               buf_size;
468         int               buf_count;
469         drm_buf_t         *buflist;
470         int               seg_count;
471         int               page_order;
472         struct drm_dma_handle **seglist;
473
474         int low_mark;                   /**< Low water mark */
475         int high_mark;                  /**< High water mark */
476 } drm_buf_entry_t;
477
478 /* Event queued up for userspace to read */
479 struct drm_pending_event {
480         struct drm_event *event;
481         struct list_head link;
482         struct drm_file *file_priv;
483         pid_t pid; /* pid of requester, no guarantee it's valid by the time
484                       we deliver the event, for tracing only */
485         void (*destroy)(struct drm_pending_event *event);
486 };
487
488 /* initial implementaton using a linked list - todo hashtab */
489 struct drm_prime_file_private {
490         struct list_head head;
491 #ifdef DUMBBELL_WIP
492         struct mutex lock;
493 #endif /* DUMBBELL_WIP */
494 };
495
496 /** File private data */
497 struct drm_file {
498         int authenticated;
499         struct drm_device *dev;
500         int               master;
501
502         /* true when the client has asked us to expose stereo 3D mode flags */
503         bool stereo_allowed;
504         /*
505          * true if client understands CRTC primary planes and cursor planes
506          * in the plane list
507          */
508         unsigned universal_planes:1;
509         /* true if client understands atomic properties */
510         unsigned atomic:1;
511
512         pid_t             pid;
513         uid_t             uid;
514         drm_magic_t       magic;
515         unsigned long     ioctl_count;
516         struct list_head lhead;
517         struct kqinfo     dkq;
518
519         /** Mapping of mm object handles to object pointers. */
520         struct idr object_idr;
521         /** Lock for synchronization of access to object_idr. */
522         struct lock table_lock;
523
524         void             *driver_priv;
525
526         int               is_master;
527         struct drm_master *masterp;
528
529         /**
530          * fbs - List of framebuffers associated with this file.
531          *
532          * Protected by fbs_lock. Note that the fbs list holds a reference on
533          * the fb object to prevent it from untimely disappearing.
534          */
535         struct list_head fbs;
536         struct lock fbs_lock;
537
538         /** User-created blob properties; this retains a reference on the
539          *  property. */
540         struct list_head blobs;
541
542         wait_queue_head_t event_wait;
543         struct list_head  event_list;
544         int               event_space;
545
546         struct drm_prime_file_private prime;
547 };
548
549 /**
550  * Lock data.
551  */
552 struct drm_lock_data {
553         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
554         /** Private of lock holder's file (NULL=kernel) */
555         struct drm_file *file_priv;
556         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
557         unsigned long lock_time;        /**< Time of last lock in jiffies */
558 };
559
560 /* This structure, in the struct drm_device, is always initialized while the
561  * device
562  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
563  * when set marks that no further bufs may be allocated until device teardown
564  * occurs (when the last open of the device has closed).  The high/low
565  * watermarks of bufs are only touched by the X Server, and thus not
566  * concurrently accessed, so no locking is needed.
567  */
568 typedef struct drm_device_dma {
569
570         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
571         int buf_count;                  /**< total number of buffers */
572         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
573         int seg_count;
574         int page_count;                 /**< number of pages */
575         unsigned long *pagelist;        /**< page list */
576         unsigned long byte_count;
577         enum {
578                 _DRM_DMA_USE_AGP = 0x01,
579                 _DRM_DMA_USE_SG = 0x02,
580                 _DRM_DMA_USE_FB = 0x04,
581                 _DRM_DMA_USE_PCI_RO = 0x08
582         } flags;
583
584 } drm_device_dma_t;
585
586 typedef struct drm_sg_mem {
587         vm_offset_t vaddr;
588         vm_paddr_t *busaddr;
589         vm_pindex_t pages;
590 } drm_sg_mem_t;
591
592 /**
593  * Kernel side of a mapping
594  */
595 struct drm_local_map {
596         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
597         unsigned long size;      /**< Requested physical size (bytes) */
598         enum drm_map_type type;  /**< Type of memory to map */
599         enum drm_map_flags flags;        /**< Flags */
600         void *handle;            /**< User-space: "Handle" to pass to mmap() */
601                                  /**< Kernel-space: kernel-virtual address */
602         int mtrr;                /**< MTRR slot used */
603 };
604
605 typedef struct drm_local_map drm_local_map_t;
606
607 /**
608  * Mappings list
609  */
610 struct drm_map_list {
611         struct list_head head;          /**< list head */
612         struct drm_hash_item hash;
613         struct drm_local_map *map;      /**< mapping */
614         uint64_t user_token;
615         struct drm_master *master;
616         struct drm_mm_node *file_offset_node;   /**< fake offset */
617 };
618
619 /**
620  * GEM specific mm private for tracking GEM objects
621  */
622 struct drm_gem_mm {
623         struct drm_vma_offset_manager vma_manager;
624         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
625         struct drm_open_hash offset_hash; /**< User token hash table for maps */
626         struct unrhdr *idxunr;
627 };
628
629
630 #include "drm_crtc.h"
631
632 /**
633  * struct drm_master - drm master structure
634  *
635  * @refcount: Refcount for this master object.
636  * @minor: Link back to minor char device we are master for. Immutable.
637  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
638  * @unique_len: Length of unique field. Protected by drm_global_mutex.
639  * @unique_size: Amount allocated. Protected by drm_global_mutex.
640  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
641  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
642  * @lock: DRI lock information.
643  * @driver_priv: Pointer to driver-private information.
644  */
645 struct drm_master {
646
647         struct kref refcount; /* refcount for this master */
648
649         struct list_head head; /**< each minor contains a list of masters */
650         struct drm_minor *minor; /**< link back to minor we are a master for */
651
652         char *unique;                   /**< Unique identifier: e.g., busid */
653         int unique_len;                 /**< Length of unique field */
654         int unique_size;                /**< amount allocated */
655
656         int blocked;                    /**< Blocked due to VC switch? */
657
658         struct drm_open_hash magiclist;
659         struct list_head magicfree;
660         struct drm_lock_data lock;
661         void *driver_priv;
662 };
663
664 /* Size of ringbuffer for vblank timestamps. Just double-buffer
665  * in initial implementation.
666  */
667 #define DRM_VBLANKTIME_RBSIZE 2
668
669 /* Flags and return codes for get_vblank_timestamp() driver function. */
670 #define DRM_CALLED_FROM_VBLIRQ 1
671 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
672 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
673
674 /* get_scanout_position() return flags */
675 #define DRM_SCANOUTPOS_VALID        (1 << 0)
676 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
677 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
678
679 #ifndef DMA_BIT_MASK
680 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
681 #endif
682
683 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
684
685 /**
686  * DRM driver structure. This structure represent the common code for
687  * a family of cards. There will one drm_device for each card present
688  * in this family
689  */
690 struct drm_driver {
691         int     (*load)(struct drm_device *, unsigned long flags);
692         int     (*use_msi)(struct drm_device *, unsigned long flags);
693         int     (*firstopen)(struct drm_device *);
694         int     (*open)(struct drm_device *, struct drm_file *);
695         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
696         void    (*postclose)(struct drm_device *, struct drm_file *);
697         void    (*lastclose)(struct drm_device *);
698         int     (*unload)(struct drm_device *);
699         void    (*reclaim_buffers_locked)(struct drm_device *,
700                                           struct drm_file *file_priv);
701         int     (*dma_ioctl)(struct drm_device *dev, void *data,
702                              struct drm_file *file_priv);
703         void    (*dma_ready)(struct drm_device *);
704         int     (*dma_quiescent)(struct drm_device *);
705         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
706                                              enum drm_lock_flags flags);
707         int     (*dma_flush_unblock)(struct drm_device *, int context,
708                                      enum drm_lock_flags flags);
709         int     (*context_ctor)(struct drm_device *dev, int context);
710         int     (*context_dtor)(struct drm_device *dev, int context);
711         int     (*kernel_context_switch)(struct drm_device *dev, int old,
712                                          int new);
713         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
714         void    (*irq_preinstall)(struct drm_device *dev);
715         int     (*irq_postinstall)(struct drm_device *dev);
716         void    (*irq_uninstall)(struct drm_device *dev);
717         void    (*irq_handler)(void *arg);
718
719         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
720         int     (*enable_vblank)(struct drm_device *dev, int crtc);
721         void    (*disable_vblank)(struct drm_device *dev, int crtc);
722
723         /**
724          * Called by vblank timestamping code.
725          *
726          * Return the current display scanout position from a crtc, and an
727          * optional accurate ktime_get timestamp of when position was measured.
728          *
729          * \param dev  DRM device.
730          * \param crtc Id of the crtc to query.
731          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
732          * \param *vpos Target location for current vertical scanout position.
733          * \param *hpos Target location for current horizontal scanout position.
734          * \param *stime Target location for timestamp taken immediately before
735          *               scanout position query. Can be NULL to skip timestamp.
736          * \param *etime Target location for timestamp taken immediately after
737          *               scanout position query. Can be NULL to skip timestamp.
738          *
739          * Returns vpos as a positive number while in active scanout area.
740          * Returns vpos as a negative number inside vblank, counting the number
741          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
742          * until start of active scanout / end of vblank."
743          *
744          * \return Flags, or'ed together as follows:
745          *
746          * DRM_SCANOUTPOS_VALID = Query successful.
747          * DRM_SCANOUTPOS_INVBL = Inside vblank.
748          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
749          * this flag means that returned position may be offset by a constant
750          * but unknown small number of scanlines wrt. real scanout position.
751          *
752          */
753         int (*get_scanout_position) (struct drm_device *dev, int crtc,
754                                      unsigned int flags,
755                                      int *vpos, int *hpos, ktime_t *stime,
756                                      ktime_t *etime);
757
758         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
759                     int *max_error, struct timeval *vblank_time,
760                     unsigned flags);
761
762         void    (*gem_free_object)(struct drm_gem_object *obj);
763         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
764         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
765
766         struct cdev_pager_ops *gem_pager_ops;
767
768         int     (*dumb_create)(struct drm_file *file_priv,
769                     struct drm_device *dev, struct drm_mode_create_dumb *args);
770         int     (*dumb_map_offset)(struct drm_file *file_priv,
771                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
772         int     (*dumb_destroy)(struct drm_file *file_priv,
773                     struct drm_device *dev, uint32_t handle);
774
775         int     (*sysctl_init)(struct drm_device *dev,
776                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
777         void    (*sysctl_cleanup)(struct drm_device *dev);
778
779         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
780
781         /**
782          * Called by \c drm_device_is_agp.  Typically used to determine if a
783          * card is really attached to AGP or not.
784          *
785          * \param dev  DRM device handle
786          *
787          * \returns
788          * One of three values is returned depending on whether or not the
789          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
790          * (return of 1), or may or may not be AGP (return of 2).
791          */
792         int     (*device_is_agp) (struct drm_device * dev);
793
794         int     major;
795         int     minor;
796         int     patchlevel;
797         const char *name;               /* Simple driver name              */
798         const char *desc;               /* Longer driver name              */
799         const char *date;               /* Date of last major changes.     */
800
801         u32 driver_features;
802         int dev_priv_size;
803         const struct drm_ioctl_desc *ioctls;
804         int num_ioctls;
805 };
806
807 /**
808  * Info file list entry. This structure represents a debugfs or proc file to
809  * be created by the drm core
810  */
811 struct drm_info_list {
812         const char *name; /** file name */
813         int (*show)(struct seq_file*, void*); /** show callback */
814         u32 driver_features; /**< Required driver features for this entry */
815         void *data;
816 };
817
818 /**
819  * debugfs node structure. This structure represents a debugfs file.
820  */
821 struct drm_info_node {
822         struct list_head list;
823         struct drm_minor *minor;
824         const struct drm_info_list *info_ent;
825         struct dentry *dent;
826 };
827
828 /**
829  * DRM minor structure. This structure represents a drm minor number.
830  */
831 struct drm_minor {
832         int index;                      /**< Minor device number */
833         int type;                       /**< Control or render */
834         device_t kdev;                  /**< OS device */
835         struct drm_device *dev;
836
837         struct drm_master *master; /* currently active master for this node */
838         struct list_head master_list;
839         struct drm_mode_group mode_group;
840 };
841
842 struct drm_pending_vblank_event {
843         struct drm_pending_event base;
844         int pipe;
845         struct drm_event_vblank event;
846 };
847
848 struct drm_sysctl_info {
849         struct sysctl_ctx_list ctx;
850         char   name[2];
851 };
852
853 /* Length for the array of resource pointers for drm_get_resource_*. */
854 #define DRM_MAX_PCI_RESOURCE    6
855
856 struct drm_vblank_crtc {
857         struct drm_device *dev;         /* pointer to the drm_device */
858         wait_queue_head_t queue;        /**< VBLANK wait queue */
859         struct timer_list disable_timer;                /* delayed disable timer */
860
861         /* vblank counter, protected by dev->vblank_time_lock for writes */
862         unsigned long count;
863         /* vblank timestamps, protected by dev->vblank_time_lock for writes */
864         struct timeval time[DRM_VBLANKTIME_RBSIZE];
865
866         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
867         u32 last;                       /* protected by dev->vbl_lock, used */
868                                         /* for wraparound handling */
869         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
870         unsigned int inmodeset;         /* Display driver is setting mode */
871         int crtc;                       /* crtc index */
872         bool enabled;                   /* so we don't call enable more than
873                                            once per disable */
874 };
875
876 /**
877  * DRM device structure. This structure represent a complete card that
878  * may contain multiple heads.
879  */
880 struct drm_device {
881         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
882
883         char              *unique;      /* Unique identifier: e.g., busid  */
884         int               unique_len;   /* Length of unique field          */
885         struct cdev       *devnode;     /* Device number for mknod         */
886         int               if_version;   /* Highest interface version set */
887
888         int               flags;        /* Flags to open(2)                */
889
890                                 /* Locks */
891         struct spinlock   dma_lock;     /* protects dev->dma */
892         struct lwkt_serialize irq_lock; /* protects irq condition checks */
893         struct lock       dev_lock;     /* protects everything else */
894
895         /** \name Locks */
896         /*@{ */
897         struct lock struct_mutex;       /**< For others */
898         struct lock master_mutex;       /**< For drm_minor::master */
899         /*@} */
900
901         /** \name Usage Counters */
902         /*@{ */
903         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
904         struct spinlock buf_lock;       /**< For drm_device::buf_use and a few other things. */
905         int buf_use;                    /**< Buffers in use -- cannot alloc */
906         atomic_t buf_alloc;             /**< Buffer allocation in progress */
907         /*@} */
908
909
910         /** \name Performance counters */
911         /*@{ */
912         unsigned long     counters;
913         enum drm_stat_type      types[15];
914         atomic_t          counts[15];
915         /*@} */
916
917                                 /* Authentication */
918         struct drm_open_hash magiclist; /**< magic hash table */
919         struct list_head magicfree;
920
921         struct list_head filelist;
922
923         /** \name Memory management */
924         /*@{ */
925         struct list_head maplist;       /**< Linked list of regions */
926         int map_count;                  /**< Number of mappable regions */
927         struct drm_open_hash map_hash;  /**< User token hash table for maps */
928
929         /** \name Context handle management */
930         /*@{ */
931         struct list_head ctxlist;       /**< Linked list of context handles */
932         int ctx_count;                  /**< Number of context handles */
933         struct lock ctxlist_mutex;      /**< For ctxlist */
934
935         struct idr ctx_idr;
936
937         /*@} */
938
939         struct drm_lock_data lock;      /* Information on hardware lock    */
940
941                                 /* DMA queues (contexts) */
942         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
943
944         int               irq;          /* Interrupt used by board         */
945         int               irq_type;     /* IRQ type (MSI enabled or not) */
946         int               irqrid;       /* Interrupt used by board */
947         struct resource   *irqr;        /* Resource for interrupt used by board    */
948         void              *irqh;        /* Handle from bus_setup_intr      */
949
950         /* Storage of resource pointers for drm_get_resource_* */
951         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
952         int               pcirid[DRM_MAX_PCI_RESOURCE];
953
954         int               pci_domain;
955         int               pci_bus;
956         int               pci_slot;
957         int               pci_func;
958
959         /** \name Context support */
960         /*@{ */
961         int irq_enabled;                /**< True if irq handler is enabled */
962         __volatile__ long context_flag; /**< Context swapping flag */
963         __volatile__ long interrupt_flag; /**< Interruption handler flag */
964         __volatile__ long dma_flag;     /**< DMA dispatch flag */
965         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
966         int last_checked;               /**< Last context checked for DMA */
967         int last_context;               /**< Last current context */
968         unsigned long last_switch;      /**< jiffies at last context switch */
969         /*@} */
970
971         /** \name VBLANK IRQ support */
972         /*@{ */
973
974         /*
975          * At load time, disabling the vblank interrupt won't be allowed since
976          * old clients may not call the modeset ioctl and therefore misbehave.
977          * Once the modeset ioctl *has* been called though, we can safely
978          * disable them when unused.
979          */
980         bool vblank_disable_allowed;
981
982         /*
983          * If true, vblank interrupt will be disabled immediately when the
984          * refcount drops to zero, as opposed to via the vblank disable
985          * timer.
986          * This can be set to true it the hardware has a working vblank
987          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
988          * appropriately.
989          */
990         bool vblank_disable_immediate;
991
992         /* array of size num_crtcs */
993         struct drm_vblank_crtc *vblank;
994
995         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
996         struct lock vbl_lock;
997         struct timer_list vblank_disable_timer;
998
999         u32 max_vblank_count;           /**< size of vblank counter register */
1000
1001         /**
1002          * List of events
1003          */
1004         struct list_head vblank_event_list;
1005         struct lock event_lock;
1006
1007         /*@} */
1008
1009         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1010
1011                                 /* Sysctl support */
1012         struct drm_sysctl_info *sysctl;
1013
1014
1015         drm_sg_mem_t      *sg;  /* Scatter gather memory */
1016         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1017
1018         unsigned long     *ctx_bitmap;
1019         void              *dev_private;
1020
1021         void              *drm_ttm_bdev;
1022
1023         /*@} */
1024
1025         struct drm_agp_head *agp;       /**< AGP data */
1026
1027         struct device *dev;             /**< Device structure */
1028         struct pci_dev *pdev;           /**< PCI device structure */
1029
1030         struct drm_driver *driver;
1031         struct drm_local_map *agp_buffer_map;
1032         unsigned int agp_buffer_token;
1033         struct drm_minor *control;              /**< Control node for card */
1034         struct drm_minor *primary;              /**< render type primary screen head */
1035
1036         struct drm_mode_config mode_config;     /**< Current mode config */
1037
1038         /** \name GEM information */
1039         /*@{ */
1040         struct lock object_name_lock;
1041         struct idr object_name_idr;
1042         /*@} */
1043         void             *mm_private;
1044
1045         void *sysctl_private;
1046         char busid_str[128];
1047         int modesetting;
1048
1049         int switch_power_state;
1050
1051         atomic_t unplugged; /* device has been unplugged or gone away */
1052 };
1053
1054 #define DRM_SWITCH_POWER_ON 0
1055 #define DRM_SWITCH_POWER_OFF 1
1056 #define DRM_SWITCH_POWER_CHANGING 2
1057 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1058
1059 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1060                                              int feature)
1061 {
1062         return ((dev->driver->driver_features & feature) ? 1 : 0);
1063 }
1064
1065 static inline void drm_device_set_unplugged(struct drm_device *dev)
1066 {
1067         smp_wmb();
1068         atomic_set(&dev->unplugged, 1);
1069 }
1070
1071 static inline int drm_device_is_unplugged(struct drm_device *dev)
1072 {
1073         int ret = atomic_read(&dev->unplugged);
1074         smp_rmb();
1075         return ret;
1076 }
1077
1078 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1079 {
1080         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1081 }
1082
1083 /**
1084  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1085  * @file: DRM open-file context
1086  *
1087  * This checks whether a DRM open-file context is owner of the master context
1088  * attached to it. If a file owns a master context, it's called DRM-Master.
1089  * Per DRM device, only one such file can be DRM-Master at a time.
1090  *
1091  * Returns: True if the file is DRM-Master, otherwise false.
1092  */
1093 static inline bool drm_is_master(const struct drm_file *file)
1094 {
1095 #if 0
1096         return file->master && file->master == file->minor->master;
1097 #else
1098         return true;
1099 #endif
1100 }
1101
1102 /******************************************************************/
1103 /** \name Internal function definitions */
1104 /*@{*/
1105
1106 #if __OS_HAS_AGP
1107 static inline int drm_core_has_AGP(struct drm_device *dev)
1108 {
1109         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1110 }
1111 #else
1112 #define drm_core_has_AGP(dev) (0)
1113 #endif
1114
1115 extern int      drm_notyet_flag;
1116 extern unsigned int drm_rnodes;
1117 extern unsigned int drm_universal_planes;
1118
1119 extern int drm_vblank_offdelay;
1120 extern unsigned int drm_timestamp_precision;
1121
1122                                 /* Driver support (drm_drv.h) */
1123 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1124 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1125 int     drm_create_cdevs(device_t kdev);
1126 d_ioctl_t drm_ioctl;
1127 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1128
1129                                 /* Device support (drm_fops.h) */
1130 d_open_t drm_open;
1131 d_close_t drm_close;
1132 d_read_t drm_read;
1133 d_kqfilter_t drm_kqfilter;
1134 int drm_release(device_t kdev);
1135
1136 d_mmap_t drm_mmap;
1137 d_mmap_single_t drm_mmap_single;
1138
1139 void drm_cdevpriv_dtor(void *cd);
1140
1141 int drm_add_busid_modesetting(struct drm_device *dev,
1142     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1143
1144 /* File operations helpers (drm_fops.c) */
1145 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1146                                          DRM_STRUCTPROC *p,
1147                                         struct drm_device *dev,
1148                                         struct file *fp);
1149 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1150                                         DRM_STRUCTPROC *p);
1151
1152 #ifdef DUMBBELL_WIP
1153 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1154                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1155                 int *prime_fd);
1156 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1157                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1158
1159 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1160                                         struct drm_file *file_priv);
1161 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1162                                         struct drm_file *file_priv);
1163
1164 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1165                                             dma_addr_t *addrs, int max_pages);
1166 #endif /* DUMBBELL_WIP */
1167 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1168 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1169
1170 #ifdef DUMBBELL_WIP
1171 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1172 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1173 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1174 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1175 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1176
1177 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1178 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1179                          struct drm_gem_object **obj);
1180 #endif /* DUMBBELL_WIP */
1181
1182 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1183 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1184
1185 /* Locking IOCTL support (drm_lock.c) */
1186 int     drm_lock_take(struct drm_lock_data *lock_data,
1187                       unsigned int context);
1188 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1189                           unsigned int context);
1190 int     drm_lock_free(struct drm_lock_data *lock_data,
1191                       unsigned int context);
1192
1193 /*
1194  * These are exported to drivers so that they can implement fencing using
1195  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1196  */
1197
1198 unsigned long drm_get_resource_start(struct drm_device *dev,
1199                                      unsigned int resource);
1200 unsigned long drm_get_resource_len(struct drm_device *dev,
1201                                    unsigned int resource);
1202
1203                                 /* IRQ support (drm_irq.h) */
1204 extern int drm_irq_install(struct drm_device *dev, int irq);
1205 int     drm_irq_uninstall(struct drm_device *dev);
1206 void    drm_driver_irq_preinstall(struct drm_device *dev);
1207 void    drm_driver_irq_postinstall(struct drm_device *dev);
1208 void    drm_driver_irq_uninstall(struct drm_device *dev);
1209
1210 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1211 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1212                            struct drm_file *filp);
1213 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1214 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1215 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1216                                      struct timeval *vblanktime);
1217 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1218                                      struct drm_pending_vblank_event *e);
1219 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1220                                        struct drm_pending_vblank_event *e);
1221 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1222 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1223 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1224 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1225 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1226 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1227 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1228 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1229 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1230 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1231 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1232 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1233 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1234 extern void drm_vblank_cleanup(struct drm_device *dev);
1235
1236 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1237                                                  int crtc, int *max_error,
1238                                                  struct timeval *vblank_time,
1239                                                  unsigned flags,
1240                                                  const struct drm_crtc *refcrtc,
1241                                                  const struct drm_display_mode *mode);
1242 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1243                                             const struct drm_display_mode *mode);
1244
1245 /**
1246  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1247  * @crtc: which CRTC's vblank waitqueue to retrieve
1248  *
1249  * This function returns a pointer to the vblank waitqueue for the CRTC.
1250  * Drivers can use this to implement vblank waits using wait_event() & co.
1251  */
1252 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1253 {
1254        return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1255 }
1256
1257 /* Modesetting support */
1258 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1259 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1260
1261                                 /* AGP/GART support (drm_agpsupport.h) */
1262
1263 #include <drm/drm_agpsupport.h>
1264
1265 /* sysctl support (drm_sysctl.h) */
1266 extern int              drm_sysctl_init(struct drm_device *dev);
1267 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1268
1269 /* Locking IOCTL support (drm_drv.c) */
1270 int     drm_lock(struct drm_device *dev, void *data,
1271                  struct drm_file *file_priv);
1272 int     drm_unlock(struct drm_device *dev, void *data,
1273                    struct drm_file *file_priv);
1274 int     drm_setversion(struct drm_device *dev, void *data,
1275                        struct drm_file *file_priv);
1276
1277 /*
1278  * These are exported to drivers so that they can implement fencing using
1279  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1280  */
1281
1282 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1283
1284 /* Misc. IOCTL support (drm_ioctl.c) */
1285 int     drm_noop(struct drm_device *dev, void *data,
1286                  struct drm_file *file_priv);
1287
1288 /* Cache management (drm_cache.c) */
1289 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1290 void drm_clflush_sg(struct sg_table *st);
1291 void drm_clflush_virt_range(void *addr, unsigned long length);
1292
1293 /* DMA support (drm_dma.c) */
1294 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1295
1296                                 /* Stub support (drm_stub.h) */
1297
1298 extern void drm_put_dev(struct drm_device *dev);
1299 extern void drm_unplug_dev(struct drm_device *dev);
1300 extern unsigned int drm_debug;
1301 extern bool drm_atomic;
1302
1303 /* Scatter Gather Support (drm_scatter.c) */
1304 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1305                            struct drm_file *file_priv);
1306 int     drm_sg_free(struct drm_device *dev, void *data,
1307                     struct drm_file *file_priv);
1308
1309 /* consistent PCI memory functions (drm_pci.c) */
1310 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1311                                        size_t align);
1312 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1313
1314                                /* sysfs support (drm_sysfs.c) */
1315 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1316
1317 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1318     vm_size_t size, struct vm_object **obj_res, int nprot);
1319 void drm_gem_pager_dtr(void *obj);
1320
1321 struct ttm_bo_device;
1322 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1323     vm_size_t size, struct vm_object **obj_res, int nprot);
1324 struct ttm_buffer_object;
1325 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1326
1327 void drm_device_lock_mtx(struct drm_device *dev);
1328 void drm_device_unlock_mtx(struct drm_device *dev);
1329 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1330     const char *msg, int timeout);
1331 void drm_device_assert_mtx_locked(struct drm_device *dev);
1332 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1333
1334 void drm_device_lock_struct(struct drm_device *dev);
1335 void drm_device_unlock_struct(struct drm_device *dev);
1336 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1337     const char *msg, int timeout);
1338 void drm_device_assert_struct_locked(struct drm_device *dev);
1339 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1340
1341 void drm_compat_locking_init(struct drm_device *dev);
1342 void drm_sleep_locking_init(struct drm_device *dev);
1343
1344 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1345 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1346 void drm_fini_pdev(struct pci_dev **pdev);
1347
1348 /* Inline replacements for drm_alloc and friends */
1349 static __inline__ void *
1350 drm_alloc(size_t size, struct malloc_type *area)
1351 {
1352         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1353 }
1354
1355 static __inline__ void *
1356 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1357 {
1358         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1359 }
1360
1361 static __inline__ void
1362 drm_free(void *pt, struct malloc_type *area)
1363 {
1364         /* kfree is special!!! */
1365         if (pt != NULL)
1366                 (kfree)(pt, area);
1367 }
1368
1369 static __inline__ struct drm_local_map *
1370 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1371 {
1372         struct drm_map_list *_entry;
1373
1374         list_for_each_entry(_entry, &dev->maplist, head) {
1375                 if (offset == (unsigned long)_entry->map->handle)
1376                         return _entry->map;
1377         }
1378         return NULL;
1379 }
1380
1381 static __inline__ void drm_core_dropmap(struct drm_map *map)
1382 {
1383 }
1384
1385 #include <drm/drm_mem_util.h>
1386
1387 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1388                                  struct device *parent);
1389 void drm_dev_ref(struct drm_device *dev);
1390 void drm_dev_unref(struct drm_device *dev);
1391 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1392 void drm_dev_unregister(struct drm_device *dev);
1393 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1394
1395 /* FreeBSD compatibility macros */
1396 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1397 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1398
1399 #define DRM_PCIE_SPEED_25 1
1400 #define DRM_PCIE_SPEED_50 2
1401 #define DRM_PCIE_SPEED_80 4
1402
1403 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1404
1405 /* XXX bad */
1406 #define drm_can_sleep() (HZ & 1)
1407
1408 #endif /* __KERNEL__ */
1409 #endif /* _DRM_P_H_ */