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