drm: Make the hlist API compatible with Linux 3.17
[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
35 #ifndef _DRM_P_H_
36 #define _DRM_P_H_
37
38 #if defined(_KERNEL) || defined(__KERNEL__)
39
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/sglist.h>
49 #include <sys/stat.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/spinlock.h>
54 #include <sys/spinlock2.h>
55 #include <sys/fcntl.h>
56 #include <sys/uio.h>
57 #include <sys/filio.h>
58 #include <sys/sysctl.h>
59 #include <sys/bus.h>
60 #include <sys/queue.h>
61 #include <sys/signalvar.h>
62 #include <sys/poll.h>
63 #include <sys/sbuf.h>
64 #include <sys/taskqueue.h>
65 #include <sys/tree.h>
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page2.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_param.h>
75 #include <machine/param.h>
76 #include <machine/pmap.h>
77 #ifdef __x86_64__
78 #include <machine/specialreg.h>
79 #endif
80 #include <machine/sysarch.h>
81 #include <sys/endian.h>
82 #include <sys/mman.h>
83 #include <sys/rman.h>
84 #include <sys/memrange.h>
85 #include <dev/agp/agpvar.h>
86 #include <sys/agpio.h>
87 #include <sys/mutex.h>
88
89 MALLOC_DECLARE(M_DRM);
90
91 #include <uapi_drm/drm.h>
92 #include <uapi_drm/drm_sarea.h>
93
94 #include <linux/atomic.h>
95 #include <linux/bug.h>
96 #include <linux/capability.h>
97 #include <linux/err.h>
98 #include <linux/idr.h>
99 #include <linux/pci.h>
100 #include <linux/jiffies.h>
101 #include <linux/kernel.h>
102 #include <linux/fs.h>
103 #include <linux/kref.h>
104 #include <linux/list.h>
105 #include <linux/mm.h>
106 #include <linux/moduleparam.h>
107 #include <linux/mutex.h>
108 #include <linux/slab.h>
109 #include <linux/scatterlist.h>
110 #include <linux/timer.h>
111 #include <asm/io.h>
112 #include <linux/seq_file.h>
113 #include <linux/types.h>
114 #include <linux/wait.h>
115 #include <linux/workqueue.h>
116
117 #include <asm/uaccess.h>
118
119 #include <drm/drm_vma_manager.h>
120
121 struct drm_file;
122 struct drm_device;
123
124 struct device_node;
125 struct videomode;
126
127 #include <drm/drm_os_linux.h>
128 #include <drm/drm_hashtab.h>
129 #include <drm/drm_mm.h>
130
131 /*
132  * 4 debug categories are defined:
133  *
134  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
135  *       This is the category used by the DRM_DEBUG() macro.
136  *
137  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
138  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
139  *
140  * KMS: used in the modesetting code.
141  *      This is the category used by the DRM_DEBUG_KMS() macro.
142  *
143  * PRIME: used in the prime code.
144  *        This is the category used by the DRM_DEBUG_PRIME() macro.
145  *
146  * Enabling verbose debug messages is done through the drm.debug parameter,
147  * each category being enabled by a bit.
148  *
149  * drm.debug=0x1 will enable CORE messages
150  * drm.debug=0x2 will enable DRIVER messages
151  * drm.debug=0x3 will enable CORE and DRIVER messages
152  * ...
153  * drm.debug=0xf will enable all messages
154  *
155  * An interesting feature is that it's possible to enable verbose logging at
156  * run-time by echoing the debug value in its sysfs node:
157  *   # echo 0xf > /sys/module/drm/parameters/debug
158  */
159 #define DRM_UT_CORE             0x01
160 #define DRM_UT_DRIVER           0x02
161 #define DRM_UT_KMS              0x04
162 #define DRM_UT_PRIME            0x08
163
164 #ifdef DRM_DEBUG
165 #  if DRM_DEBUG>1
166 #    define DRM_DEBUG_DEFAULT_ON 2
167 #  else
168 #    define DRM_DEBUG_DEFAULT_ON 1
169 #  endif
170 #undef DRM_DEBUG
171 #endif /* DRM_DEBUG */
172
173 #define DRM_DEBUGBITS_DEBUG             0x1
174 #define DRM_DEBUGBITS_KMS               0x2
175 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
176 #define DRM_DEBUGBITS_VERBOSE           0x8
177
178 void drm_ut_debug_printk(unsigned int request_level,
179                          const char *prefix,
180                          const char *function_name,
181                          const char *format, ...);
182
183 int drm_err(const char *func, const char *format, ...);
184
185 /***********************************************************************/
186 /** \name DRM template customization defaults */
187 /*@{*/
188
189 /* driver capabilities and requirements mask */
190 #define DRIVER_USE_AGP     0x1
191 #define DRIVER_REQUIRE_AGP 0x2
192 #define DRIVER_PCI_DMA     0x8
193 #define DRIVER_SG          0x10
194 #define DRIVER_HAVE_DMA    0x20
195 #define DRIVER_HAVE_IRQ    0x40
196 #define DRIVER_IRQ_SHARED  0x80
197 #define DRIVER_DMA_QUEUE   0x200
198 #define DRIVER_GEM         0x1000
199 #define DRIVER_MODESET     0x2000
200 #define DRIVER_PRIME       0x4000
201 #define DRIVER_RENDER      0x8000
202
203 /***********************************************************************/
204 /** \name Begin the DRM... */
205 /*@{*/
206
207 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
208                                      also include looping detection. */
209
210 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
211 #define DRM_KERNEL_CONTEXT    0  /**< Change drm_resctx if changed */
212 #define DRM_RESERVED_CONTEXTS 1  /**< Change drm_resctx if changed */
213
214 #define DRM_MAP_HASH_OFFSET 0x10000000
215
216 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
217 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
218 #define DRM_GEM_MAX_IDX         0x3fffff
219 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
220 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
221 #define DRM_GEM_MAPPING_MAPOFF(o) \
222     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
223
224 SYSCTL_DECL(_hw_drm);
225
226 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
227
228 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
229
230 #define __OS_HAS_AGP    1
231
232 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
233 #define DRM_DEV_UID     0
234 #define DRM_DEV_GID     0
235
236 #define DRM_CURPROC             curthread
237 #define DRM_STRUCTPROC          struct thread
238 #define DRM_CURRENTPID          (curproc != NULL ? curproc->p_pid : -1)
239 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
240 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
241 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
242     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
243 #if defined(INVARIANTS)
244 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
245 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
246 #else
247 #define DRM_LOCK_ASSERT(d)
248 #define DRM_UNLOCK_ASSERT(d)
249 #endif
250
251 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
252
253 typedef void                    irqreturn_t;
254 #define IRQ_HANDLED             /* nothing */
255 #define IRQ_NONE                /* nothing */
256
257 enum {
258         DRM_IS_NOT_AGP,
259         DRM_IS_AGP,
260         DRM_MIGHT_BE_AGP
261 };
262 #define DRM_AGP_MEM             struct agp_memory_info
263
264 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
265
266 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
267 #define DRM_MTRR_WC             MDF_WRITECOMBINE
268
269 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
270 do {                                                                    \
271         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
272              dev->lock.file_priv != file_priv) {                        \
273                 DRM_ERROR("%s called without lock held\n",              \
274                            __FUNCTION__);                               \
275                 return EINVAL;                                          \
276         }                                                               \
277 } while (0)
278
279 /* Returns -errno to shared code */
280 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
281 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
282         DRM_UNLOCK(dev);                                        \
283         lwkt_serialize_enter(&dev->irq_lock);                   \
284         if (!(condition)) {                                     \
285             tsleep_interlock(&(queue), PCATCH);                 \
286             lwkt_serialize_exit(&dev->irq_lock);                \
287             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,      \
288                           "drmwtq", (timeout));                 \
289         } else {                                                \
290                 lwkt_serialize_exit(&dev->irq_lock);            \
291         }                                                       \
292         DRM_LOCK(dev);                                          \
293 }
294
295 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
296     vm_memattr_t memattr);
297 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
298 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
299
300 /***********************************************************************/
301 /** \name Macros to make printk easier */
302 /*@{*/
303
304 /**
305  * Error output.
306  *
307  * \param fmt printf() like format string.
308  * \param arg arguments
309  */
310 #define DRM_ERROR(fmt, ...) \
311         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
312             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
313
314 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
315
316 #define DRM_INFO_ONCE(fmt, ...)                         \
317         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
318
319 #define DRM_DEBUG(fmt, ...) do {                                        \
320         if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)             \
321                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
322                         __func__ , ##__VA_ARGS__);                      \
323 } while (0)
324
325 #define DRM_DEBUG_VERBOSE(fmt, ...) do {                                \
326         if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)           \
327                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
328                         __func__ , ##__VA_ARGS__);                      \
329 } while (0)
330
331 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
332         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
333                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
334                         __func__ , ##__VA_ARGS__);                      \
335 } while (0)
336
337 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
338         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
339                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
340                         __func__ , ##__VA_ARGS__);                      \
341 } while (0)
342
343 #define DRM_LOG_KMS(fmt, ...) do {                                      \
344         kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,     \
345                         __func__ , ##__VA_ARGS__);                      \
346 } while (0)
347
348 #define dev_dbg(dev, fmt, ...) do {                                     \
349         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
350                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
351         }                                                               \
352 } while (0)
353
354 typedef struct drm_pci_id_list
355 {
356         int vendor;
357         int device;
358         long driver_private;
359         char *name;
360 } drm_pci_id_list_t;
361
362 struct drm_msi_blacklist_entry
363 {
364         int vendor;
365         int device;
366 };
367
368 /**
369  * Ioctl function type.
370  *
371  * \param inode device inode.
372  * \param file_priv DRM file private pointer.
373  * \param cmd command.
374  * \param arg argument.
375  */
376 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
377                         struct drm_file *file_priv);
378
379 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
380                                unsigned long arg);
381
382 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
383
384 #define DRM_AUTH        0x1
385 #define DRM_MASTER      0x2
386 #define DRM_ROOT_ONLY   0x4
387 #define DRM_CONTROL_ALLOW 0x8
388 #define DRM_UNLOCKED    0x10
389 #define DRM_RENDER_ALLOW 0x20
390
391 struct drm_ioctl_desc {
392         unsigned int cmd;
393         int flags;
394         drm_ioctl_t *func;
395         unsigned int cmd_drv;
396         const char *name;
397 };
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_DRV(ioctl, _func, _flags)                 \
404         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
405
406 typedef struct drm_magic_entry {
407         struct list_head head;
408         struct drm_hash_item hash_item;
409         struct drm_file        *priv;
410         struct drm_magic_entry *next;
411 } drm_magic_entry_t;
412
413 typedef struct drm_magic_head {
414         struct drm_magic_entry *head;
415         struct drm_magic_entry *tail;
416 } drm_magic_head_t;
417
418 typedef struct drm_buf {
419         int idx;                       /**< Index into master buflist */
420         int total;                     /**< Buffer size */
421         int order;                     /**< log-base-2(total) */
422         int used;                      /**< Amount of buffer in use (for DMA) */
423         unsigned long offset;          /**< Byte offset (used internally) */
424         void *address;                 /**< Address of buffer */
425         unsigned long bus_address;     /**< Bus address of buffer */
426         struct drm_buf *next;          /**< Kernel-only: used for free list */
427         __volatile__ int waiting;      /**< On kernel DMA queue */
428         __volatile__ int pending;      /**< On hardware DMA queue */
429         struct drm_file *file_priv;    /**< Private of holding file descr */
430         int context;                   /**< Kernel queue for this buffer */
431         int while_locked;              /**< Dispatch this buffer while locked */
432         enum {
433                 DRM_LIST_NONE = 0,
434                 DRM_LIST_FREE = 1,
435                 DRM_LIST_WAIT = 2,
436                 DRM_LIST_PEND = 3,
437                 DRM_LIST_PRIO = 4,
438                 DRM_LIST_RECLAIM = 5
439         } list;                        /**< Which list we're on */
440
441         int dev_priv_size;               /**< Size of buffer private storage */
442         void *dev_private;               /**< Per-buffer private storage */
443 } drm_buf_t;
444
445 /** bufs is one longer than it has to be */
446 struct drm_waitlist {
447         int count;                      /**< Number of possible buffers */
448         struct drm_buf **bufs;          /**< List of pointers to buffers */
449         struct drm_buf **rp;                    /**< Read pointer */
450         struct drm_buf **wp;                    /**< Write pointer */
451         struct drm_buf **end;           /**< End pointer */
452         spinlock_t read_lock;
453         spinlock_t write_lock;
454 };
455
456 struct drm_freelist {
457         int               initialized; /* Freelist in use                  */
458         atomic_t          count;       /* Number of free buffers           */
459         drm_buf_t         *next;       /* End pointer                      */
460
461         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
462         int               low_mark;    /* Low water mark                   */
463         int               high_mark;   /* High water mark                  */
464 };
465
466 typedef struct drm_dma_handle {
467         void *vaddr;
468         size_t size;
469         bus_addr_t busaddr;
470         bus_dma_tag_t tag;
471         bus_dmamap_t map;
472 } drm_dma_handle_t;
473
474 typedef struct drm_buf_entry {
475         int               buf_size;
476         int               buf_count;
477         drm_buf_t         *buflist;
478         int               seg_count;
479         drm_dma_handle_t  **seglist;
480         int               page_order;
481
482         struct drm_freelist freelist;
483 } drm_buf_entry_t;
484
485 /* Event queued up for userspace to read */
486 struct drm_pending_event {
487         struct drm_event *event;
488         struct list_head link;
489         struct drm_file *file_priv;
490         pid_t pid; /* pid of requester, no guarantee it's valid by the time
491                       we deliver the event, for tracing only */
492         void (*destroy)(struct drm_pending_event *event);
493 };
494
495 /* initial implementaton using a linked list - todo hashtab */
496 struct drm_prime_file_private {
497         struct list_head head;
498 #ifdef DUMBBELL_WIP
499         struct mutex lock;
500 #endif /* DUMBBELL_WIP */
501 };
502
503 /** File private data */
504 struct drm_file {
505         int authenticated;
506         struct drm_device *dev;
507         int               master;
508
509         /* true when the client has asked us to expose stereo 3D mode flags */
510         bool stereo_allowed;
511         /*
512          * true if client understands CRTC primary planes and cursor planes
513          * in the plane list
514          */
515         unsigned universal_planes:1;
516
517         pid_t             pid;
518         uid_t             uid;
519         drm_magic_t       magic;
520         unsigned long     ioctl_count;
521         struct list_head lhead;
522         struct kqinfo     dkq;
523
524         /** Mapping of mm object handles to object pointers. */
525         struct idr object_idr;
526         /** Lock for synchronization of access to object_idr. */
527         struct lock table_lock;
528
529         void             *driver_priv;
530
531         int               is_master;
532         struct drm_master *masterp;
533
534         /**
535          * fbs - List of framebuffers associated with this file.
536          *
537          * Protected by fbs_lock. Note that the fbs list holds a reference on
538          * the fb object to prevent it from untimely disappearing.
539          */
540         struct list_head fbs;
541         struct lock fbs_lock;
542
543         wait_queue_head_t event_wait;
544         struct list_head  event_list;
545         int               event_space;
546
547         struct drm_prime_file_private prime;
548 };
549
550 /** Wait queue */
551 struct drm_queue {
552         atomic_t use_count;             /**< Outstanding uses (+1) */
553         atomic_t finalization;          /**< Finalization in progress */
554         atomic_t block_count;           /**< Count of processes waiting */
555         atomic_t block_read;            /**< Queue blocked for reads */
556         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
557         atomic_t block_write;           /**< Queue blocked for writes */
558         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
559         atomic_t total_queued;          /**< Total queued statistic */
560         atomic_t total_flushed;         /**< Total flushes statistic */
561         atomic_t total_locks;           /**< Total locks statistics */
562         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
563         struct drm_waitlist waitlist;   /**< Pending buffers */
564         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
565 };
566
567 /**
568  * Lock data.
569  */
570 struct drm_lock_data {
571         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
572         /** Private of lock holder's file (NULL=kernel) */
573         struct drm_file *file_priv;
574         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
575         unsigned long lock_time;        /**< Time of last lock in jiffies */
576 };
577
578 /* This structure, in the struct drm_device, is always initialized while the
579  * device
580  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
581  * when set marks that no further bufs may be allocated until device teardown
582  * occurs (when the last open of the device has closed).  The high/low
583  * watermarks of bufs are only touched by the X Server, and thus not
584  * concurrently accessed, so no locking is needed.
585  */
586 typedef struct drm_device_dma {
587
588         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
589         int buf_count;                  /**< total number of buffers */
590         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
591         int seg_count;
592         int page_count;                 /**< number of pages */
593         unsigned long *pagelist;        /**< page list */
594         unsigned long byte_count;
595         enum {
596                 _DRM_DMA_USE_AGP = 0x01,
597                 _DRM_DMA_USE_SG = 0x02,
598                 _DRM_DMA_USE_FB = 0x04,
599                 _DRM_DMA_USE_PCI_RO = 0x08
600         } flags;
601
602 } drm_device_dma_t;
603
604 typedef struct drm_agp_mem {
605         void               *handle;
606         unsigned long      bound; /* address */
607         int                pages;
608         struct drm_agp_mem *prev;
609         struct drm_agp_mem *next;
610 } drm_agp_mem_t;
611
612 typedef struct drm_agp_head {
613         device_t           agpdev;
614         struct agp_info    agp_info;
615         const char         *chipset;
616         drm_agp_mem_t      *memory;
617         unsigned long      mode;
618         int                enabled;
619         int                acquired;
620         unsigned long      base;
621         int                agp_mtrr;
622         int                cant_use_aperture;
623         unsigned long      page_mask;
624 } drm_agp_head_t;
625
626 typedef struct drm_sg_mem {
627         vm_offset_t vaddr;
628         vm_paddr_t *busaddr;
629         vm_pindex_t pages;
630 } drm_sg_mem_t;
631
632 /**
633  * Kernel side of a mapping
634  */
635 struct drm_local_map {
636         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
637         unsigned long size;      /**< Requested physical size (bytes) */
638         enum drm_map_type type;  /**< Type of memory to map */
639         enum drm_map_flags flags;        /**< Flags */
640         void *handle;            /**< User-space: "Handle" to pass to mmap() */
641                                  /**< Kernel-space: kernel-virtual address */
642         int mtrr;                /**< MTRR slot used */
643 };
644
645 typedef struct drm_local_map drm_local_map_t;
646
647 /**
648  * Mappings list
649  */
650 struct drm_map_list {
651         struct list_head head;          /**< list head */
652         struct drm_hash_item hash;
653         struct drm_local_map *map;      /**< mapping */
654         uint64_t user_token;
655         struct drm_master *master;
656         struct drm_mm_node *file_offset_node;   /**< fake offset */
657 };
658
659 /**
660  * Context handle list
661  */
662 struct drm_ctx_list {
663         struct list_head head;          /**< list head */
664         drm_context_t handle;           /**< context handle */
665         struct drm_file *tag;           /**< associated fd private data */
666 };
667
668 /* location of GART table */
669 #define DRM_ATI_GART_MAIN 1
670 #define DRM_ATI_GART_FB   2
671
672 #define DRM_ATI_GART_PCI 1
673 #define DRM_ATI_GART_PCIE 2
674 #define DRM_ATI_GART_IGP 3
675
676 struct drm_ati_pcigart_info {
677         int gart_table_location;
678         int gart_reg_if;
679         void *addr;
680         dma_addr_t bus_addr;
681         dma_addr_t table_mask;
682         dma_addr_t member_mask;
683         struct drm_dma_handle *table_handle;
684         drm_local_map_t mapping;
685         int table_size;
686         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
687 };
688
689 /**
690  * GEM specific mm private for tracking GEM objects
691  */
692 struct drm_gem_mm {
693         struct drm_vma_offset_manager vma_manager;
694         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
695         struct drm_open_hash offset_hash; /**< User token hash table for maps */
696         struct unrhdr *idxunr;
697 };
698
699 struct drm_gem_object {
700         /** Reference count of this object */
701         struct kref refcount;
702
703         /**
704          * handle_count - gem file_priv handle count of this object
705          *
706          * Each handle also holds a reference. Note that when the handle_count
707          * drops to 0 any global names (e.g. the id in the flink namespace) will
708          * be cleared.
709          *
710          * Protected by dev->object_name_lock.
711          * */
712         unsigned handle_count;
713
714         /** Related drm device */
715         struct drm_device *dev;
716
717         /** File representing the shmem storage: filp in Linux parlance */
718         vm_object_t vm_obj;
719
720         /* Mapping info for this object */
721         struct drm_vma_offset_node vma_node;
722         bool on_map;
723         struct drm_hash_item map_list;
724
725         /**
726          * Size of the object, in bytes.  Immutable over the object's
727          * lifetime.
728          */
729         size_t size;
730
731         /**
732          * Global name for this object, starts at 1. 0 means unnamed.
733          * Access is covered by the object_name_lock in the related drm_device
734          */
735         int name;
736
737         /**
738          * Memory domains. These monitor which caches contain read/write data
739          * related to the object. When transitioning from one set of domains
740          * to another, the driver is called to ensure that caches are suitably
741          * flushed and invalidated
742          */
743         uint32_t read_domains;
744         uint32_t write_domain;
745
746         /**
747          * While validating an exec operation, the
748          * new read/write domain values are computed here.
749          * They will be transferred to the above values
750          * at the point that any cache flushing occurs
751          */
752         uint32_t pending_read_domains;
753         uint32_t pending_write_domain;
754
755 #ifdef DUMBBELL_WIP
756         /* dma buf exported from this GEM object */
757         struct dma_buf *export_dma_buf;
758
759         /* dma buf attachment backing this object */
760         struct dma_buf_attachment *import_attach;
761 #endif /* DUMBBELL_WIP */
762 };
763
764 #include "drm_crtc.h"
765
766 /**
767  * struct drm_master - drm master structure
768  *
769  * @refcount: Refcount for this master object.
770  * @minor: Link back to minor char device we are master for. Immutable.
771  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
772  * @unique_len: Length of unique field. Protected by drm_global_mutex.
773  * @unique_size: Amount allocated. Protected by drm_global_mutex.
774  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
775  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
776  * @lock: DRI lock information.
777  * @driver_priv: Pointer to driver-private information.
778  */
779 struct drm_master {
780
781         struct kref refcount; /* refcount for this master */
782
783         struct list_head head; /**< each minor contains a list of masters */
784         struct drm_minor *minor; /**< link back to minor we are a master for */
785
786         char *unique;                   /**< Unique identifier: e.g., busid */
787         int unique_len;                 /**< Length of unique field */
788         int unique_size;                /**< amount allocated */
789
790         int blocked;                    /**< Blocked due to VC switch? */
791
792         struct drm_open_hash magiclist;
793         struct list_head magicfree;
794         struct drm_lock_data lock;
795         void *driver_priv;
796 };
797
798 /* Size of ringbuffer for vblank timestamps. Just double-buffer
799  * in initial implementation.
800  */
801 #define DRM_VBLANKTIME_RBSIZE 2
802
803 /* Flags and return codes for get_vblank_timestamp() driver function. */
804 #define DRM_CALLED_FROM_VBLIRQ 1
805 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
806 #define DRM_VBLANKTIME_INVBL             (1 << 1)
807
808 /* get_scanout_position() return flags */
809 #define DRM_SCANOUTPOS_VALID        (1 << 0)
810 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
811 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
812
813 #ifndef DMA_BIT_MASK
814 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
815 #endif
816
817 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
818
819 /**
820  * DRM driver structure. This structure represent the common code for
821  * a family of cards. There will one drm_device for each card present
822  * in this family
823  */
824 struct drm_driver {
825         int     (*load)(struct drm_device *, unsigned long flags);
826         int     (*use_msi)(struct drm_device *, unsigned long flags);
827         int     (*firstopen)(struct drm_device *);
828         int     (*open)(struct drm_device *, struct drm_file *);
829         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
830         void    (*postclose)(struct drm_device *, struct drm_file *);
831         void    (*lastclose)(struct drm_device *);
832         int     (*unload)(struct drm_device *);
833         void    (*reclaim_buffers_locked)(struct drm_device *,
834                                           struct drm_file *file_priv);
835         int     (*dma_ioctl)(struct drm_device *dev, void *data,
836                              struct drm_file *file_priv);
837         void    (*dma_ready)(struct drm_device *);
838         int     (*dma_quiescent)(struct drm_device *);
839         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
840                                              enum drm_lock_flags flags);
841         int     (*dma_flush_unblock)(struct drm_device *, int context,
842                                      enum drm_lock_flags flags);
843         int     (*context_ctor)(struct drm_device *dev, int context);
844         int     (*context_dtor)(struct drm_device *dev, int context);
845         int     (*kernel_context_switch)(struct drm_device *dev, int old,
846                                          int new);
847         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
848         void    (*irq_preinstall)(struct drm_device *dev);
849         int     (*irq_postinstall)(struct drm_device *dev);
850         void    (*irq_uninstall)(struct drm_device *dev);
851         void    (*irq_handler)(void *arg);
852
853         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
854         int     (*enable_vblank)(struct drm_device *dev, int crtc);
855         void    (*disable_vblank)(struct drm_device *dev, int crtc);
856
857         /**
858          * Called by vblank timestamping code.
859          *
860          * Return the current display scanout position from a crtc, and an
861          * optional accurate ktime_get timestamp of when position was measured.
862          *
863          * \param dev  DRM device.
864          * \param crtc Id of the crtc to query.
865          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
866          * \param *vpos Target location for current vertical scanout position.
867          * \param *hpos Target location for current horizontal scanout position.
868          * \param *stime Target location for timestamp taken immediately before
869          *               scanout position query. Can be NULL to skip timestamp.
870          * \param *etime Target location for timestamp taken immediately after
871          *               scanout position query. Can be NULL to skip timestamp.
872          *
873          * Returns vpos as a positive number while in active scanout area.
874          * Returns vpos as a negative number inside vblank, counting the number
875          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
876          * until start of active scanout / end of vblank."
877          *
878          * \return Flags, or'ed together as follows:
879          *
880          * DRM_SCANOUTPOS_VALID = Query successful.
881          * DRM_SCANOUTPOS_INVBL = Inside vblank.
882          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
883          * this flag means that returned position may be offset by a constant
884          * but unknown small number of scanlines wrt. real scanout position.
885          *
886          */
887         int (*get_scanout_position) (struct drm_device *dev, int crtc,
888                                      unsigned int flags,
889                                      int *vpos, int *hpos, ktime_t *stime,
890                                      ktime_t *etime);
891
892         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
893                     int *max_error, struct timeval *vblank_time,
894                     unsigned flags);
895
896         void    (*gem_free_object)(struct drm_gem_object *obj);
897         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
898         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
899
900         struct cdev_pager_ops *gem_pager_ops;
901
902         int     (*dumb_create)(struct drm_file *file_priv,
903                     struct drm_device *dev, struct drm_mode_create_dumb *args);
904         int     (*dumb_map_offset)(struct drm_file *file_priv,
905                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
906         int     (*dumb_destroy)(struct drm_file *file_priv,
907                     struct drm_device *dev, uint32_t handle);
908
909         int     (*sysctl_init)(struct drm_device *dev,
910                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
911         void    (*sysctl_cleanup)(struct drm_device *dev);
912
913         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
914
915         /**
916          * Called by \c drm_device_is_agp.  Typically used to determine if a
917          * card is really attached to AGP or not.
918          *
919          * \param dev  DRM device handle
920          *
921          * \returns
922          * One of three values is returned depending on whether or not the
923          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
924          * (return of 1), or may or may not be AGP (return of 2).
925          */
926         int     (*device_is_agp) (struct drm_device * dev);
927
928         int     major;
929         int     minor;
930         int     patchlevel;
931         const char *name;               /* Simple driver name              */
932         const char *desc;               /* Longer driver name              */
933         const char *date;               /* Date of last major changes.     */
934
935         u32 driver_features;
936         int dev_priv_size;
937         const struct drm_ioctl_desc *ioctls;
938         int num_ioctls;
939 };
940
941 /**
942  * Info file list entry. This structure represents a debugfs or proc file to
943  * be created by the drm core
944  */
945 struct drm_info_list {
946         const char *name; /** file name */
947         int (*show)(struct seq_file*, void*); /** show callback */
948         u32 driver_features; /**< Required driver features for this entry */
949         void *data;
950 };
951
952 /**
953  * debugfs node structure. This structure represents a debugfs file.
954  */
955 struct drm_info_node {
956         struct list_head list;
957         struct drm_minor *minor;
958         const struct drm_info_list *info_ent;
959         struct dentry *dent;
960 };
961
962 /**
963  * DRM minor structure. This structure represents a drm minor number.
964  */
965 struct drm_minor {
966         int index;                      /**< Minor device number */
967         int type;                       /**< Control or render */
968         device_t kdev;                  /**< OS device */
969         struct drm_device *dev;
970
971         struct drm_master *master; /* currently active master for this node */
972         struct list_head master_list;
973         struct drm_mode_group mode_group;
974 };
975
976 struct drm_pending_vblank_event {
977         struct drm_pending_event base;
978         int pipe;
979         struct drm_event_vblank event;
980 };
981
982 struct drm_sysctl_info {
983         struct sysctl_ctx_list ctx;
984         char   name[2];
985 };
986
987 /* Length for the array of resource pointers for drm_get_resource_*. */
988 #define DRM_MAX_PCI_RESOURCE    6
989
990 struct drm_vblank_crtc {
991         struct drm_device *dev;         /* pointer to the drm_device */
992         wait_queue_head_t queue;        /**< VBLANK wait queue */
993         struct timeval time[DRM_VBLANKTIME_RBSIZE];     /**< timestamp of current count */
994         struct timer_list disable_timer;                /* delayed disable timer */
995         atomic_t count;                 /**< number of VBLANK interrupts */
996         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
997         u32 last;                       /* protected by dev->vbl_lock, used */
998                                         /* for wraparound handling */
999         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
1000         unsigned int inmodeset;         /* Display driver is setting mode */
1001         int crtc;                       /* crtc index */
1002         bool enabled;                   /* so we don't call enable more than
1003                                            once per disable */
1004 };
1005
1006 /**
1007  * DRM device structure. This structure represent a complete card that
1008  * may contain multiple heads.
1009  */
1010 struct drm_device {
1011         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
1012
1013         uint16_t pci_subdevice;         /* PCI subsystem device id */
1014         uint16_t pci_subvendor;         /* PCI subsystem vendor id */
1015
1016         char              *unique;      /* Unique identifier: e.g., busid  */
1017         int               unique_len;   /* Length of unique field          */
1018         struct cdev       *devnode;     /* Device number for mknod         */
1019         int               if_version;   /* Highest interface version set */
1020
1021         int               flags;        /* Flags to open(2)                */
1022
1023                                 /* Locks */
1024         struct spinlock   dma_lock;     /* protects dev->dma */
1025         struct lwkt_serialize irq_lock; /* protects irq condition checks */
1026         struct lock       dev_lock;     /* protects everything else */
1027
1028         /** \name Locks */
1029         /*@{ */
1030         struct lock struct_mutex;       /**< For others */
1031         /*@} */
1032
1033         /** \name Usage Counters */
1034         /*@{ */
1035         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
1036         int               buf_use;      /* Buffers in use -- cannot alloc  */
1037         /*@} */
1038
1039
1040         /** \name Performance counters */
1041         /*@{ */
1042         unsigned long     counters;
1043         enum drm_stat_type      types[15];
1044         atomic_t          counts[15];
1045         /*@} */
1046
1047                                 /* Authentication */
1048         struct drm_open_hash magiclist; /**< magic hash table */
1049         struct list_head magicfree;
1050
1051         struct list_head filelist;
1052
1053         /** \name Memory management */
1054         /*@{ */
1055         struct list_head maplist;       /**< Linked list of regions */
1056         int map_count;                  /**< Number of mappable regions */
1057         struct drm_open_hash map_hash;  /**< User token hash table for maps */
1058
1059         /** \name Context handle management */
1060         /*@{ */
1061         struct list_head ctxlist;       /**< Linked list of context handles */
1062         int ctx_count;                  /**< Number of context handles */
1063         struct lock ctxlist_mutex;      /**< For ctxlist */
1064
1065         struct idr ctx_idr;
1066
1067         /*@} */
1068
1069         struct drm_lock_data lock;      /* Information on hardware lock    */
1070
1071                                 /* DMA queues (contexts) */
1072         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
1073
1074         int               irq;          /* Interrupt used by board         */
1075         int               irq_type;     /* IRQ type (MSI enabled or not) */
1076         int               irqrid;       /* Interrupt used by board */
1077         struct resource   *irqr;        /* Resource for interrupt used by board    */
1078         void              *irqh;        /* Handle from bus_setup_intr      */
1079
1080         /* Storage of resource pointers for drm_get_resource_* */
1081         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
1082         int               pcirid[DRM_MAX_PCI_RESOURCE];
1083
1084         int               pci_domain;
1085         int               pci_bus;
1086         int               pci_slot;
1087         int               pci_func;
1088
1089         /** \name Context support */
1090         /*@{ */
1091         int irq_enabled;                /**< True if irq handler is enabled */
1092         __volatile__ long context_flag; /**< Context swapping flag */
1093         __volatile__ long interrupt_flag; /**< Interruption handler flag */
1094         __volatile__ long dma_flag;     /**< DMA dispatch flag */
1095         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
1096         int last_checked;               /**< Last context checked for DMA */
1097         int last_context;               /**< Last current context */
1098         unsigned long last_switch;      /**< jiffies at last context switch */
1099         /*@} */
1100
1101         /** \name VBLANK IRQ support */
1102         /*@{ */
1103
1104         /*
1105          * At load time, disabling the vblank interrupt won't be allowed since
1106          * old clients may not call the modeset ioctl and therefore misbehave.
1107          * Once the modeset ioctl *has* been called though, we can safely
1108          * disable them when unused.
1109          */
1110         bool vblank_disable_allowed;
1111
1112         /* array of size num_crtcs */
1113         struct drm_vblank_crtc *vblank;
1114
1115         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
1116         struct lock vbl_lock;
1117         struct timer_list vblank_disable_timer;
1118
1119         u32 max_vblank_count;           /**< size of vblank counter register */
1120
1121         /**
1122          * List of events
1123          */
1124         struct list_head vblank_event_list;
1125         struct lock event_lock;
1126
1127         /*@} */
1128
1129         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1130
1131                                 /* Sysctl support */
1132         struct drm_sysctl_info *sysctl;
1133         int               sysctl_node_idx;
1134
1135
1136         drm_sg_mem_t      *sg;  /* Scatter gather memory */
1137         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1138
1139         unsigned long     *ctx_bitmap;
1140         void              *dev_private;
1141
1142         void              *drm_ttm_bdev;
1143
1144         /*@} */
1145
1146         struct drm_agp_head *agp;       /**< AGP data */
1147
1148         struct device *dev;             /**< Device structure */
1149         struct pci_dev *pdev;           /**< PCI device structure */
1150         int pci_vendor;                 /**< PCI vendor id */
1151         int pci_device;                 /**< PCI device id */
1152
1153         struct drm_driver *driver;
1154         struct drm_local_map *agp_buffer_map;
1155         unsigned int agp_buffer_token;
1156         struct drm_minor *control;              /**< Control node for card */
1157         struct drm_minor *primary;              /**< render type primary screen head */
1158
1159         struct drm_mode_config mode_config;     /**< Current mode config */
1160
1161         /** \name GEM information */
1162         /*@{ */
1163         struct lock object_name_lock;
1164         struct idr object_name_idr;
1165         /*@} */
1166         void             *mm_private;
1167
1168         void *sysctl_private;
1169         char busid_str[128];
1170         int modesetting;
1171
1172         int switch_power_state;
1173
1174         atomic_t unplugged; /* device has been unplugged or gone away */
1175 };
1176
1177 #define DRM_SWITCH_POWER_ON 0
1178 #define DRM_SWITCH_POWER_OFF 1
1179 #define DRM_SWITCH_POWER_CHANGING 2
1180 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1181
1182 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1183                                              int feature)
1184 {
1185         return ((dev->driver->driver_features & feature) ? 1 : 0);
1186 }
1187
1188 static inline int drm_device_is_unplugged(struct drm_device *dev)
1189 {
1190         int ret = atomic_read(&dev->unplugged);
1191         smp_rmb();
1192         return ret;
1193 }
1194
1195 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1196 {
1197         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1198 }
1199
1200 #if __OS_HAS_AGP
1201 static inline int drm_core_has_AGP(struct drm_device *dev)
1202 {
1203         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1204 }
1205 #else
1206 #define drm_core_has_AGP(dev) (0)
1207 #endif
1208
1209 extern int      drm_notyet_flag;
1210 extern unsigned int drm_debug;
1211 extern unsigned int drm_rnodes;
1212 extern unsigned int drm_universal_planes;
1213
1214 extern unsigned int drm_vblank_offdelay;
1215 extern unsigned int drm_timestamp_precision;
1216 extern unsigned int drm_timestamp_monotonic;
1217
1218 /* Device setup support (drm_drv.c) */
1219 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1220 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1221 int     drm_create_cdevs(device_t kdev);
1222
1223 d_ioctl_t drm_ioctl;
1224 extern int drm_lastclose(struct drm_device *dev);
1225
1226                                 /* Device support (drm_fops.h) */
1227 extern struct lock drm_global_mutex;
1228 d_open_t drm_open;
1229 d_close_t drm_close;
1230 d_read_t drm_read;
1231 d_kqfilter_t drm_kqfilter;
1232 int drm_release(device_t kdev);
1233
1234 d_mmap_t drm_mmap;
1235 d_mmap_single_t drm_mmap_single;
1236 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
1237
1238 void drm_cdevpriv_dtor(void *cd);
1239
1240 void drm_event_wakeup(struct drm_pending_event *e);
1241
1242 int drm_add_busid_modesetting(struct drm_device *dev,
1243     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1244
1245 /* File operations helpers (drm_fops.c) */
1246 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1247                                          DRM_STRUCTPROC *p,
1248                                         struct drm_device *dev,
1249                                         struct file *fp);
1250 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1251                                         DRM_STRUCTPROC *p);
1252
1253 #ifdef DUMBBELL_WIP
1254 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1255                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1256                 int *prime_fd);
1257 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1258                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1259
1260 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1261                                         struct drm_file *file_priv);
1262 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1263                                         struct drm_file *file_priv);
1264
1265 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1266                                             dma_addr_t *addrs, int max_pages);
1267 #endif /* DUMBBELL_WIP */
1268 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1269 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1270
1271 int drm_gem_dumb_destroy(struct drm_file *file,
1272                          struct drm_device *dev,
1273                          uint32_t handle);
1274
1275 #ifdef DUMBBELL_WIP
1276 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1277 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1278 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1279 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1280 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1281
1282 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1283 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1284                          struct drm_gem_object **obj);
1285 #endif /* DUMBBELL_WIP */
1286
1287                                 /* Memory management support (drm_memory.h) */
1288 #include <drm/drm_memory.h>
1289 void    drm_mem_init(void);
1290 void    drm_mem_uninit(void);
1291 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1292 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1293 void    drm_ioremapfree(drm_local_map_t *map);
1294 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1295 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1296
1297 int     drm_ctxbitmap_init(struct drm_device *dev);
1298 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
1299 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1300
1301 /* Locking IOCTL support (drm_lock.c) */
1302 int     drm_lock_take(struct drm_lock_data *lock_data,
1303                       unsigned int context);
1304 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1305                           unsigned int context);
1306 int     drm_lock_free(struct drm_lock_data *lock_data,
1307                       unsigned int context);
1308
1309 /*
1310  * These are exported to drivers so that they can implement fencing using
1311  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1312  */
1313
1314 unsigned long drm_get_resource_start(struct drm_device *dev,
1315                                      unsigned int resource);
1316 unsigned long drm_get_resource_len(struct drm_device *dev,
1317                                    unsigned int resource);
1318 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1319 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1320 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1321 extern int drm_addmap(struct drm_device *dev, resource_size_t offset,
1322                       unsigned int size, enum drm_map_type type,
1323                       enum drm_map_flags flags, struct drm_local_map **map_ptr);
1324
1325 /* DMA support (drm_dma.c) */
1326 int     drm_dma_setup(struct drm_device *dev);
1327 void    drm_dma_takedown(struct drm_device *dev);
1328 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1329 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1330 #define drm_core_reclaim_buffers drm_reclaim_buffers
1331
1332                                 /* IRQ support (drm_irq.h) */
1333 extern int drm_irq_install(struct drm_device *dev, int irq);
1334 int     drm_irq_uninstall(struct drm_device *dev);
1335 void    drm_driver_irq_preinstall(struct drm_device *dev);
1336 void    drm_driver_irq_postinstall(struct drm_device *dev);
1337 void    drm_driver_irq_uninstall(struct drm_device *dev);
1338
1339 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1340 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1341                            struct drm_file *filp);
1342 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1343 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1344                                      struct timeval *vblanktime);
1345 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1346                                      struct drm_pending_vblank_event *e);
1347 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1348 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1349 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1350 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1351 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1352 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1353 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1354 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1355 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1356 extern void drm_vblank_cleanup(struct drm_device *dev);
1357
1358 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1359                                      struct timeval *tvblank, unsigned flags);
1360 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1361                                                  int crtc, int *max_error,
1362                                                  struct timeval *vblank_time,
1363                                                  unsigned flags,
1364                                                  const struct drm_crtc *refcrtc,
1365                                                  const struct drm_display_mode *mode);
1366 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1367                                             const struct drm_display_mode *mode);
1368
1369 /* Modesetting support */
1370 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1371 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1372 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1373                            struct drm_file *file_priv);
1374
1375 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1376 int     drm_device_is_agp(struct drm_device *dev);
1377 int     drm_device_is_pcie(struct drm_device *dev);
1378 drm_agp_head_t *drm_agp_init(void);
1379 int     drm_agp_acquire(struct drm_device *dev);
1380 int     drm_agp_release(struct drm_device *dev);
1381 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1382 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1383 void    *drm_agp_allocate_memory(size_t pages, u32 type);
1384 int     drm_agp_free_memory(void *handle);
1385 int     drm_agp_bind_memory(void *handle, off_t start);
1386 int     drm_agp_unbind_memory(void *handle);
1387 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1388 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1389 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1390 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1391
1392                                 /* Scatter Gather Support (drm_scatter.h) */
1393 extern void drm_legacy_sg_cleanup(struct drm_device *dev);
1394 extern int drm_sg_alloc(struct drm_device *dev, void *data,
1395                         struct drm_file *file_priv);
1396
1397 /* Scatter Gather Support (drm_scatter.c) */
1398 void    drm_sg_cleanup(drm_sg_mem_t *entry);
1399
1400 /* sysctl support (drm_sysctl.h) */
1401 extern int              drm_sysctl_init(struct drm_device *dev);
1402 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1403
1404 /* ATI PCIGART support (ati_pcigart.c) */
1405 int     drm_ati_pcigart_init(struct drm_device *dev,
1406                                 struct drm_ati_pcigart_info *gart_info);
1407 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
1408                                 struct drm_ati_pcigart_info *gart_info);
1409
1410 /* Cache management (drm_memory.c) */
1411 void    drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1412
1413 /* Locking IOCTL support (drm_drv.c) */
1414 int     drm_lock(struct drm_device *dev, void *data,
1415                  struct drm_file *file_priv);
1416 int     drm_unlock(struct drm_device *dev, void *data,
1417                    struct drm_file *file_priv);
1418 int     drm_version(struct drm_device *dev, void *data,
1419                     struct drm_file *file_priv);
1420 int     drm_setversion(struct drm_device *dev, void *data,
1421                        struct drm_file *file_priv);
1422
1423 /* Misc. IOCTL support (drm_ioctl.c) */
1424 int     drm_irq_by_busid(struct drm_device *dev, void *data,
1425                          struct drm_file *file_priv);
1426 int     drm_getunique(struct drm_device *dev, void *data,
1427                       struct drm_file *file_priv);
1428 int     drm_setunique(struct drm_device *dev, void *data,
1429                       struct drm_file *file_priv);
1430 int     drm_getmap(struct drm_device *dev, void *data,
1431                    struct drm_file *file_priv);
1432 int     drm_getclient(struct drm_device *dev, void *data,
1433                       struct drm_file *file_priv);
1434 int     drm_getstats(struct drm_device *dev, void *data,
1435                      struct drm_file *file_priv);
1436 int     drm_getcap(struct drm_device *dev, void *data,
1437                      struct drm_file *file_priv);
1438 extern int drm_setclientcap(struct drm_device *dev, void *data,
1439                             struct drm_file *file_priv);
1440 int     drm_noop(struct drm_device *dev, void *data,
1441                  struct drm_file *file_priv);
1442
1443 /* Context IOCTL support (drm_context.c) */
1444 int     drm_resctx(struct drm_device *dev, void *data,
1445                    struct drm_file *file_priv);
1446 int     drm_addctx(struct drm_device *dev, void *data,
1447                    struct drm_file *file_priv);
1448 int     drm_getctx(struct drm_device *dev, void *data,
1449                    struct drm_file *file_priv);
1450 int     drm_switchctx(struct drm_device *dev, void *data,
1451                       struct drm_file *file_priv);
1452 int     drm_newctx(struct drm_device *dev, void *data,
1453                    struct drm_file *file_priv);
1454 int     drm_rmctx(struct drm_device *dev, void *data,
1455                   struct drm_file *file_priv);
1456 int     drm_setsareactx(struct drm_device *dev, void *data,
1457                         struct drm_file *file_priv);
1458 int     drm_getsareactx(struct drm_device *dev, void *data,
1459                         struct drm_file *file_priv);
1460
1461 /* Authentication IOCTL support (drm_auth.c) */
1462 int     drm_getmagic(struct drm_device *dev, void *data,
1463                      struct drm_file *file_priv);
1464 int     drm_authmagic(struct drm_device *dev, void *data,
1465                       struct drm_file *file_priv);
1466
1467 /* Cache management (drm_cache.c) */
1468 void drm_clflush_virt_range(void *addr, unsigned long length);
1469
1470 /* Buffer management support (drm_bufs.c) */
1471 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
1472                          struct drm_file *file_priv);
1473 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
1474                         struct drm_file *file_priv);
1475 int     drm_addbufs(struct drm_device *dev, void *data,
1476                     struct drm_file *file_priv);
1477 int     drm_infobufs(struct drm_device *dev, void *data,
1478                      struct drm_file *file_priv);
1479 int     drm_markbufs(struct drm_device *dev, void *data,
1480                      struct drm_file *file_priv);
1481 int     drm_freebufs(struct drm_device *dev, void *data,
1482                      struct drm_file *file_priv);
1483 int     drm_mapbufs(struct drm_device *dev, void *data,
1484                     struct drm_file *file_priv);
1485
1486 /* DMA support (drm_dma.c) */
1487 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1488
1489 /* IRQ support (drm_irq.c) */
1490 int     drm_control(struct drm_device *dev, void *data,
1491                     struct drm_file *file_priv);
1492
1493 /* AGP/GART support (drm_agpsupport.c) */
1494 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1495                               struct drm_file *file_priv);
1496 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
1497                               struct drm_file *file_priv);
1498 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1499                              struct drm_file *file_priv);
1500 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
1501                            struct drm_file *file_priv);
1502 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1503                             struct drm_file *file_priv);
1504 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
1505                            struct drm_file *file_priv);
1506 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1507                              struct drm_file *file_priv);
1508 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1509                            struct drm_file *file_priv);
1510
1511                                 /* Stub support (drm_stub.h) */
1512 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1513                                struct drm_file *file_priv);
1514 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1515                                 struct drm_file *file_priv);
1516
1517 /* Scatter Gather Support (drm_scatter.c) */
1518 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1519                            struct drm_file *file_priv);
1520 int     drm_sg_free(struct drm_device *dev, void *data,
1521                     struct drm_file *file_priv);
1522
1523 /* consistent PCI memory functions (drm_pci.c) */
1524 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1525                                        size_t align);
1526 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1527
1528                                /* sysfs support (drm_sysfs.c) */
1529 struct drm_sysfs_class;
1530 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1531 extern void drm_sysfs_destroy(void);
1532 extern int drm_sysfs_device_add(struct drm_minor *minor);
1533 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1534 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1535 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1536 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1537
1538 /* Graphics Execution Manager library functions (drm_gem.c) */
1539 int drm_gem_init(struct drm_device *dev);
1540 void drm_gem_destroy(struct drm_device *dev);
1541 void drm_gem_object_release(struct drm_gem_object *obj);
1542 void drm_gem_object_free(struct kref *kref);
1543 int drm_gem_object_init(struct drm_device *dev,
1544                         struct drm_gem_object *obj, size_t size);
1545 void drm_gem_private_object_init(struct drm_device *dev,
1546                                  struct drm_gem_object *obj, size_t size);
1547 void drm_gem_vm_open(struct vm_area_struct *vma);
1548 void drm_gem_vm_close(struct vm_area_struct *vma);
1549 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1550                      struct vm_area_struct *vma);
1551 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
1552
1553 int i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
1554     vm_ooffset_t foff, struct ucred *cred, u_short *color);
1555 void i915_gem_pager_dtor(void * handle);
1556
1557 #include <drm/drm_global.h>
1558
1559 static inline void
1560 drm_gem_object_reference(struct drm_gem_object *obj)
1561 {
1562         kref_get(&obj->refcount);
1563 }
1564
1565 static inline void
1566 drm_gem_object_unreference(struct drm_gem_object *obj)
1567 {
1568         if (obj != NULL)
1569                 kref_put(&obj->refcount, drm_gem_object_free);
1570 }
1571
1572 static inline void
1573 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1574 {
1575         if (obj != NULL) {
1576                 struct drm_device *dev = obj->dev;
1577                 DRM_LOCK(dev);
1578                 kref_put(&obj->refcount, drm_gem_object_free);
1579                 DRM_UNLOCK(dev);
1580         }
1581 }
1582
1583 int drm_gem_handle_create_tail(struct drm_file *file_priv,
1584                                struct drm_gem_object *obj,
1585                                u32 *handlep);
1586 int drm_gem_handle_create(struct drm_file *file_priv,
1587                           struct drm_gem_object *obj,
1588                           u32 *handlep);
1589 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1590
1591 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1592                                              struct drm_file *filp,
1593                                              u32 handle);
1594 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1595                         struct drm_file *file_priv);
1596 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1597                         struct drm_file *file_priv);
1598 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1599                        struct drm_file *file_priv);
1600 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1601 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1602
1603 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1604 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1605 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1606
1607 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1608     vm_size_t size, struct vm_object **obj_res, int nprot);
1609 void drm_gem_pager_dtr(void *obj);
1610
1611 struct ttm_bo_device;
1612 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1613     vm_size_t size, struct vm_object **obj_res, int nprot);
1614 struct ttm_buffer_object;
1615 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1616
1617 void drm_device_lock_mtx(struct drm_device *dev);
1618 void drm_device_unlock_mtx(struct drm_device *dev);
1619 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1620     const char *msg, int timeout);
1621 void drm_device_assert_mtx_locked(struct drm_device *dev);
1622 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1623
1624 void drm_device_lock_struct(struct drm_device *dev);
1625 void drm_device_unlock_struct(struct drm_device *dev);
1626 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1627     const char *msg, int timeout);
1628 void drm_device_assert_struct_locked(struct drm_device *dev);
1629 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1630
1631 void drm_compat_locking_init(struct drm_device *dev);
1632 void drm_sleep_locking_init(struct drm_device *dev);
1633
1634 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1635 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1636 void drm_fini_pdev(struct pci_dev **pdev);
1637
1638 /* Inline replacements for drm_alloc and friends */
1639 static __inline__ void *
1640 drm_alloc(size_t size, struct malloc_type *area)
1641 {
1642         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1643 }
1644
1645 static __inline__ void *
1646 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1647 {
1648         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1649 }
1650
1651 static __inline__ void
1652 drm_free(void *pt, struct malloc_type *area)
1653 {
1654         kfree(pt);
1655 }
1656
1657 static __inline__ void
1658 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1659 {
1660         map->handle = drm_ioremap_wc(dev, map);
1661 }
1662 static __inline__ void
1663 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1664 {
1665         map->handle = drm_ioremap(dev, map);
1666 }
1667 static __inline__ void
1668 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1669 {
1670         if ( map->handle && map->size )
1671                 drm_ioremapfree(map);
1672 }
1673
1674 static __inline__ struct drm_local_map *
1675 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1676 {
1677         struct drm_map_list *_entry;
1678
1679         list_for_each_entry(_entry, &dev->maplist, head) {
1680                 if (offset == (unsigned long)_entry->map->handle)
1681                         return _entry->map;
1682         }
1683         return NULL;
1684 }
1685
1686 static __inline__ void drm_core_dropmap(struct drm_map *map)
1687 {
1688 }
1689
1690 #include <drm/drm_mem_util.h>
1691
1692 /* FreeBSD compatibility macros */
1693 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1694 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1695
1696 #define DRM_PCIE_SPEED_25 1
1697 #define DRM_PCIE_SPEED_50 2
1698 #define DRM_PCIE_SPEED_80 4
1699
1700 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1701
1702 /* XXX bad */
1703 #define drm_can_sleep() (HZ & 1)
1704
1705 #endif /* __KERNEL__ */
1706 #endif /* _DRM_P_H_ */