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