bd717fc1423416b7bebb79ec1d587537adc546e4
[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 <linux/highmem.h>
64 #include <sys/sbuf.h>
65 #include <sys/taskqueue.h>
66 #include <sys/tree.h>
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page2.h>
74 #include <vm/vm_pager.h>
75 #include <vm/vm_param.h>
76 #include <machine/param.h>
77 #include <machine/pmap.h>
78 #ifdef __x86_64__
79 #include <machine/specialreg.h>
80 #endif
81 #include <machine/sysarch.h>
82 #include <sys/endian.h>
83 #include <sys/mman.h>
84 #include <sys/rman.h>
85 #include <sys/memrange.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/dma-mapping.h>
97 #include <linux/capability.h>
98 #include <linux/err.h>
99 #include <linux/idr.h>
100 #include <linux/pci.h>
101 #include <linux/jiffies.h>
102 #include <linux/kernel.h>
103 #include <linux/fs.h>
104 #include <linux/kref.h>
105 #include <linux/list.h>
106 #include <linux/mm.h>
107 #include <linux/moduleparam.h>
108 #include <linux/mutex.h>
109 #include <linux/slab.h>
110 #include <linux/scatterlist.h>
111 #include <linux/timer.h>
112 #include <asm/io.h>
113 #include <linux/seq_file.h>
114 #include <linux/types.h>
115 #include <linux/vmalloc.h>
116 #include <linux/wait.h>
117 #include <linux/workqueue.h>
118
119 #include <asm/uaccess.h>
120
121 #include <drm/drm_global.h>
122
123 #include <drm/drm_vma_manager.h>
124
125 struct drm_file;
126 struct drm_device;
127 struct drm_agp_head;
128 struct drm_gem_object;
129
130 struct device_node;
131 struct videomode;
132
133 #include <drm/drm_os_linux.h>
134 #include <drm/drm_hashtab.h>
135 #include <drm/drm_mm.h>
136
137 /*
138  * 4 debug categories are defined:
139  *
140  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
141  *       This is the category used by the DRM_DEBUG() macro.
142  *
143  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
144  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
145  *
146  * KMS: used in the modesetting code.
147  *      This is the category used by the DRM_DEBUG_KMS() macro.
148  *
149  * PRIME: used in the prime code.
150  *        This is the category used by the DRM_DEBUG_PRIME() macro.
151  *
152  * Enabling verbose debug messages is done through the drm.debug parameter,
153  * each category being enabled by a bit.
154  *
155  * drm.debug=0x1 will enable CORE messages
156  * drm.debug=0x2 will enable DRIVER messages
157  * drm.debug=0x3 will enable CORE and DRIVER messages
158  * ...
159  * drm.debug=0xf will enable all messages
160  *
161  * An interesting feature is that it's possible to enable verbose logging at
162  * run-time by echoing the debug value in its sysfs node:
163  *   # echo 0xf > /sys/module/drm/parameters/debug
164  */
165 #define DRM_UT_CORE             0x01
166 #define DRM_UT_DRIVER           0x02
167 #define DRM_UT_KMS              0x04
168 #define DRM_UT_PRIME            0x08
169
170 void drm_ut_debug_printk(const char *function_name,
171                          const char *format, ...);
172
173 void drm_err(const char *func, const char *format, ...);
174
175 /***********************************************************************/
176 /** \name DRM template customization defaults */
177 /*@{*/
178
179 /* driver capabilities and requirements mask */
180 #define DRIVER_USE_AGP     0x1
181 #define DRIVER_REQUIRE_AGP 0x2
182 #define DRIVER_PCI_DMA     0x8
183 #define DRIVER_SG          0x10
184 #define DRIVER_HAVE_DMA    0x20
185 #define DRIVER_HAVE_IRQ    0x40
186 #define DRIVER_IRQ_SHARED  0x80
187 #define DRIVER_DMA_QUEUE   0x200
188 #define DRIVER_GEM         0x1000
189 #define DRIVER_MODESET     0x2000
190 #define DRIVER_PRIME       0x4000
191 #define DRIVER_RENDER      0x8000
192
193 /***********************************************************************/
194 /** \name Begin the DRM... */
195 /*@{*/
196
197 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
198
199 /*@}*/
200
201 /***********************************************************************/
202 /** \name Macros to make printk easier */
203 /*@{*/
204
205 /**
206  * Error output.
207  *
208  * \param fmt printf() like format string.
209  * \param arg arguments
210  */
211 #define DRM_ERROR(fmt, ...) \
212         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
213             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
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|S_IROTH|S_IWOTH)
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 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
305
306 #define DRM_INFO_ONCE(fmt, ...)                         \
307         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
308
309 #define DRM_DEBUGBITS_DEBUG             0x1
310 #define DRM_DEBUGBITS_KMS               0x2
311 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
312 #define DRM_DEBUGBITS_VERBOSE           0x8
313
314 #define DRM_DEBUG(fmt, ...) do {                                        \
315         if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)             \
316                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
317                         __func__ , ##__VA_ARGS__);                      \
318 } while (0)
319
320 #define DRM_DEBUG_VERBOSE(fmt, ...) do {                                \
321         if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)           \
322                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
323                         __func__ , ##__VA_ARGS__);                      \
324 } while (0)
325
326 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
327         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
328                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
329                         __func__ , ##__VA_ARGS__);                      \
330 } while (0)
331
332 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
333         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
334                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
335                         __func__ , ##__VA_ARGS__);                      \
336 } while (0)
337
338 #define DRM_LOG_KMS(fmt, ...) do {                                      \
339         kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,     \
340                         __func__ , ##__VA_ARGS__);                      \
341 } while (0)
342
343 #define dev_dbg(dev, fmt, ...) do {                                     \
344         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
345                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
346         }                                                               \
347 } while (0)
348
349 typedef struct drm_pci_id_list
350 {
351         int vendor;
352         int device;
353         long driver_private;
354         char *name;
355 } drm_pci_id_list_t;
356
357 struct drm_msi_blacklist_entry
358 {
359         int vendor;
360         int device;
361 };
362
363 /**
364  * Ioctl function type.
365  *
366  * \param inode device inode.
367  * \param file_priv DRM file private pointer.
368  * \param cmd command.
369  * \param arg argument.
370  */
371 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
372                         struct drm_file *file_priv);
373
374 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
375                                unsigned long arg);
376
377 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
378
379 #define DRM_AUTH        0x1
380 #define DRM_MASTER      0x2
381 #define DRM_ROOT_ONLY   0x4
382 #define DRM_CONTROL_ALLOW 0x8
383 #define DRM_UNLOCKED    0x10
384 #define DRM_RENDER_ALLOW 0x20
385
386 struct drm_ioctl_desc {
387         unsigned int cmd;
388         int flags;
389         drm_ioctl_t *func;
390         unsigned int cmd_drv;
391         const char *name;
392 };
393
394 /**
395  * Creates a driver or general drm_ioctl_desc array entry for the given
396  * ioctl, for use by drm_ioctl().
397  */
398 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
399         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
400
401 typedef struct drm_magic_entry {
402         struct list_head head;
403         struct drm_hash_item hash_item;
404         struct drm_file        *priv;
405         struct drm_magic_entry *next;
406 } drm_magic_entry_t;
407
408 typedef struct drm_magic_head {
409         struct drm_magic_entry *head;
410         struct drm_magic_entry *tail;
411 } drm_magic_head_t;
412
413 typedef struct drm_buf {
414         int idx;                       /**< Index into master buflist */
415         int total;                     /**< Buffer size */
416         int order;                     /**< log-base-2(total) */
417         int used;                      /**< Amount of buffer in use (for DMA) */
418         unsigned long offset;          /**< Byte offset (used internally) */
419         void *address;                 /**< Address of buffer */
420         unsigned long bus_address;     /**< Bus address of buffer */
421         struct drm_buf *next;          /**< Kernel-only: used for free list */
422         __volatile__ int waiting;      /**< On kernel DMA queue */
423         __volatile__ int pending;      /**< On hardware DMA queue */
424         struct drm_file *file_priv;    /**< Private of holding file descr */
425         int context;                   /**< Kernel queue for this buffer */
426         int while_locked;              /**< Dispatch this buffer while locked */
427         enum {
428                 DRM_LIST_NONE = 0,
429                 DRM_LIST_FREE = 1,
430                 DRM_LIST_WAIT = 2,
431                 DRM_LIST_PEND = 3,
432                 DRM_LIST_PRIO = 4,
433                 DRM_LIST_RECLAIM = 5
434         } list;                        /**< Which list we're on */
435
436         int dev_priv_size;               /**< Size of buffer private storage */
437         void *dev_private;               /**< Per-buffer private storage */
438 } drm_buf_t;
439
440 /** bufs is one longer than it has to be */
441 struct drm_waitlist {
442         int count;                      /**< Number of possible buffers */
443         struct drm_buf **bufs;          /**< List of pointers to buffers */
444         struct drm_buf **rp;                    /**< Read pointer */
445         struct drm_buf **wp;                    /**< Write pointer */
446         struct drm_buf **end;           /**< End pointer */
447         struct spinlock *read_lock;
448         struct spinlock *write_lock;
449 };
450
451 typedef struct drm_dma_handle {
452         void *vaddr;
453         size_t size;
454         bus_addr_t busaddr;
455         bus_dma_tag_t tag;
456         bus_dmamap_t map;
457 } drm_dma_handle_t;
458
459 typedef struct drm_buf_entry {
460         int               buf_size;
461         int               buf_count;
462         drm_buf_t         *buflist;
463         int               seg_count;
464         int               page_order;
465         struct drm_dma_handle **seglist;
466
467         int low_mark;                   /**< Low water mark */
468         int high_mark;                  /**< High water mark */
469 } drm_buf_entry_t;
470
471 /* Event queued up for userspace to read */
472 struct drm_pending_event {
473         struct drm_event *event;
474         struct list_head link;
475         struct drm_file *file_priv;
476         pid_t pid; /* pid of requester, no guarantee it's valid by the time
477                       we deliver the event, for tracing only */
478         void (*destroy)(struct drm_pending_event *event);
479 };
480
481 /* initial implementaton using a linked list - todo hashtab */
482 struct drm_prime_file_private {
483         struct list_head head;
484 #ifdef DUMBBELL_WIP
485         struct mutex lock;
486 #endif /* DUMBBELL_WIP */
487 };
488
489 /** File private data */
490 struct drm_file {
491         int authenticated;
492         struct drm_device *dev;
493         int               master;
494
495         /* true when the client has asked us to expose stereo 3D mode flags */
496         bool stereo_allowed;
497         /*
498          * true if client understands CRTC primary planes and cursor planes
499          * in the plane list
500          */
501         unsigned universal_planes:1;
502
503         pid_t             pid;
504         uid_t             uid;
505         drm_magic_t       magic;
506         unsigned long     ioctl_count;
507         struct list_head lhead;
508         struct kqinfo     dkq;
509
510         /** Mapping of mm object handles to object pointers. */
511         struct idr object_idr;
512         /** Lock for synchronization of access to object_idr. */
513         struct lock table_lock;
514
515         void             *driver_priv;
516
517         int               is_master;
518         struct drm_master *masterp;
519
520         /**
521          * fbs - List of framebuffers associated with this file.
522          *
523          * Protected by fbs_lock. Note that the fbs list holds a reference on
524          * the fb object to prevent it from untimely disappearing.
525          */
526         struct list_head fbs;
527         struct lock fbs_lock;
528
529         wait_queue_head_t event_wait;
530         struct list_head  event_list;
531         int               event_space;
532
533         struct drm_prime_file_private prime;
534 };
535
536 /** Wait queue */
537 struct drm_queue {
538         atomic_t use_count;             /**< Outstanding uses (+1) */
539         atomic_t finalization;          /**< Finalization in progress */
540         atomic_t block_count;           /**< Count of processes waiting */
541         atomic_t block_read;            /**< Queue blocked for reads */
542         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
543         atomic_t block_write;           /**< Queue blocked for writes */
544         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
545         atomic_t total_queued;          /**< Total queued statistic */
546         atomic_t total_flushed;         /**< Total flushes statistic */
547         atomic_t total_locks;           /**< Total locks statistics */
548         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
549         struct drm_waitlist waitlist;   /**< Pending buffers */
550         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
551 };
552
553 /**
554  * Lock data.
555  */
556 struct drm_lock_data {
557         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
558         /** Private of lock holder's file (NULL=kernel) */
559         struct drm_file *file_priv;
560         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
561         unsigned long lock_time;        /**< Time of last lock in jiffies */
562 };
563
564 /* This structure, in the struct drm_device, is always initialized while the
565  * device
566  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
567  * when set marks that no further bufs may be allocated until device teardown
568  * occurs (when the last open of the device has closed).  The high/low
569  * watermarks of bufs are only touched by the X Server, and thus not
570  * concurrently accessed, so no locking is needed.
571  */
572 typedef struct drm_device_dma {
573
574         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
575         int buf_count;                  /**< total number of buffers */
576         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
577         int seg_count;
578         int page_count;                 /**< number of pages */
579         unsigned long *pagelist;        /**< page list */
580         unsigned long byte_count;
581         enum {
582                 _DRM_DMA_USE_AGP = 0x01,
583                 _DRM_DMA_USE_SG = 0x02,
584                 _DRM_DMA_USE_FB = 0x04,
585                 _DRM_DMA_USE_PCI_RO = 0x08
586         } flags;
587
588 } drm_device_dma_t;
589
590 typedef struct drm_sg_mem {
591         vm_offset_t vaddr;
592         vm_paddr_t *busaddr;
593         vm_pindex_t pages;
594 } drm_sg_mem_t;
595
596 /**
597  * Kernel side of a mapping
598  */
599 struct drm_local_map {
600         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
601         unsigned long size;      /**< Requested physical size (bytes) */
602         enum drm_map_type type;  /**< Type of memory to map */
603         enum drm_map_flags flags;        /**< Flags */
604         void *handle;            /**< User-space: "Handle" to pass to mmap() */
605                                  /**< Kernel-space: kernel-virtual address */
606         int mtrr;                /**< MTRR slot used */
607 };
608
609 typedef struct drm_local_map drm_local_map_t;
610
611 /**
612  * Mappings list
613  */
614 struct drm_map_list {
615         struct list_head head;          /**< list head */
616         struct drm_hash_item hash;
617         struct drm_local_map *map;      /**< mapping */
618         uint64_t user_token;
619         struct drm_master *master;
620         struct drm_mm_node *file_offset_node;   /**< fake offset */
621 };
622
623 /**
624  * GEM specific mm private for tracking GEM objects
625  */
626 struct drm_gem_mm {
627         struct drm_vma_offset_manager vma_manager;
628         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
629         struct drm_open_hash offset_hash; /**< User token hash table for maps */
630         struct unrhdr *idxunr;
631 };
632
633
634 #include "drm_crtc.h"
635
636 /**
637  * struct drm_master - drm master structure
638  *
639  * @refcount: Refcount for this master object.
640  * @minor: Link back to minor char device we are master for. Immutable.
641  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
642  * @unique_len: Length of unique field. Protected by drm_global_mutex.
643  * @unique_size: Amount allocated. Protected by drm_global_mutex.
644  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
645  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
646  * @lock: DRI lock information.
647  * @driver_priv: Pointer to driver-private information.
648  */
649 struct drm_master {
650
651         struct kref refcount; /* refcount for this master */
652
653         struct list_head head; /**< each minor contains a list of masters */
654         struct drm_minor *minor; /**< link back to minor we are a master for */
655
656         char *unique;                   /**< Unique identifier: e.g., busid */
657         int unique_len;                 /**< Length of unique field */
658         int unique_size;                /**< amount allocated */
659
660         int blocked;                    /**< Blocked due to VC switch? */
661
662         struct drm_open_hash magiclist;
663         struct list_head magicfree;
664         struct drm_lock_data lock;
665         void *driver_priv;
666 };
667
668 /* Size of ringbuffer for vblank timestamps. Just double-buffer
669  * in initial implementation.
670  */
671 #define DRM_VBLANKTIME_RBSIZE 2
672
673 /* Flags and return codes for get_vblank_timestamp() driver function. */
674 #define DRM_CALLED_FROM_VBLIRQ 1
675 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
676 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
677
678 /* get_scanout_position() return flags */
679 #define DRM_SCANOUTPOS_VALID        (1 << 0)
680 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
681 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
682
683 #ifndef DMA_BIT_MASK
684 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
685 #endif
686
687 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
688
689 /**
690  * DRM driver structure. This structure represent the common code for
691  * a family of cards. There will one drm_device for each card present
692  * in this family
693  */
694 struct drm_driver {
695         int     (*load)(struct drm_device *, unsigned long flags);
696         int     (*use_msi)(struct drm_device *, unsigned long flags);
697         int     (*firstopen)(struct drm_device *);
698         int     (*open)(struct drm_device *, struct drm_file *);
699         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
700         void    (*postclose)(struct drm_device *, struct drm_file *);
701         void    (*lastclose)(struct drm_device *);
702         int     (*unload)(struct drm_device *);
703         void    (*reclaim_buffers_locked)(struct drm_device *,
704                                           struct drm_file *file_priv);
705         int     (*dma_ioctl)(struct drm_device *dev, void *data,
706                              struct drm_file *file_priv);
707         void    (*dma_ready)(struct drm_device *);
708         int     (*dma_quiescent)(struct drm_device *);
709         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
710                                              enum drm_lock_flags flags);
711         int     (*dma_flush_unblock)(struct drm_device *, int context,
712                                      enum drm_lock_flags flags);
713         int     (*context_ctor)(struct drm_device *dev, int context);
714         int     (*context_dtor)(struct drm_device *dev, int context);
715         int     (*kernel_context_switch)(struct drm_device *dev, int old,
716                                          int new);
717         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
718         void    (*irq_preinstall)(struct drm_device *dev);
719         int     (*irq_postinstall)(struct drm_device *dev);
720         void    (*irq_uninstall)(struct drm_device *dev);
721         void    (*irq_handler)(void *arg);
722
723         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
724         int     (*enable_vblank)(struct drm_device *dev, int crtc);
725         void    (*disable_vblank)(struct drm_device *dev, int crtc);
726
727         /**
728          * Called by vblank timestamping code.
729          *
730          * Return the current display scanout position from a crtc, and an
731          * optional accurate ktime_get timestamp of when position was measured.
732          *
733          * \param dev  DRM device.
734          * \param crtc Id of the crtc to query.
735          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
736          * \param *vpos Target location for current vertical scanout position.
737          * \param *hpos Target location for current horizontal scanout position.
738          * \param *stime Target location for timestamp taken immediately before
739          *               scanout position query. Can be NULL to skip timestamp.
740          * \param *etime Target location for timestamp taken immediately after
741          *               scanout position query. Can be NULL to skip timestamp.
742          *
743          * Returns vpos as a positive number while in active scanout area.
744          * Returns vpos as a negative number inside vblank, counting the number
745          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
746          * until start of active scanout / end of vblank."
747          *
748          * \return Flags, or'ed together as follows:
749          *
750          * DRM_SCANOUTPOS_VALID = Query successful.
751          * DRM_SCANOUTPOS_INVBL = Inside vblank.
752          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
753          * this flag means that returned position may be offset by a constant
754          * but unknown small number of scanlines wrt. real scanout position.
755          *
756          */
757         int (*get_scanout_position) (struct drm_device *dev, int crtc,
758                                      unsigned int flags,
759                                      int *vpos, int *hpos, ktime_t *stime,
760                                      ktime_t *etime);
761
762         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
763                     int *max_error, struct timeval *vblank_time,
764                     unsigned flags);
765
766         void    (*gem_free_object)(struct drm_gem_object *obj);
767         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
768         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
769
770         struct cdev_pager_ops *gem_pager_ops;
771
772         int     (*dumb_create)(struct drm_file *file_priv,
773                     struct drm_device *dev, struct drm_mode_create_dumb *args);
774         int     (*dumb_map_offset)(struct drm_file *file_priv,
775                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
776         int     (*dumb_destroy)(struct drm_file *file_priv,
777                     struct drm_device *dev, uint32_t handle);
778
779         int     (*sysctl_init)(struct drm_device *dev,
780                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
781         void    (*sysctl_cleanup)(struct drm_device *dev);
782
783         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
784
785         /**
786          * Called by \c drm_device_is_agp.  Typically used to determine if a
787          * card is really attached to AGP or not.
788          *
789          * \param dev  DRM device handle
790          *
791          * \returns
792          * One of three values is returned depending on whether or not the
793          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
794          * (return of 1), or may or may not be AGP (return of 2).
795          */
796         int     (*device_is_agp) (struct drm_device * dev);
797
798         int     major;
799         int     minor;
800         int     patchlevel;
801         const char *name;               /* Simple driver name              */
802         const char *desc;               /* Longer driver name              */
803         const char *date;               /* Date of last major changes.     */
804
805         u32 driver_features;
806         int dev_priv_size;
807         const struct drm_ioctl_desc *ioctls;
808         int num_ioctls;
809 };
810
811 /**
812  * Info file list entry. This structure represents a debugfs or proc file to
813  * be created by the drm core
814  */
815 struct drm_info_list {
816         const char *name; /** file name */
817         int (*show)(struct seq_file*, void*); /** show callback */
818         u32 driver_features; /**< Required driver features for this entry */
819         void *data;
820 };
821
822 /**
823  * debugfs node structure. This structure represents a debugfs file.
824  */
825 struct drm_info_node {
826         struct list_head list;
827         struct drm_minor *minor;
828         const struct drm_info_list *info_ent;
829         struct dentry *dent;
830 };
831
832 /**
833  * DRM minor structure. This structure represents a drm minor number.
834  */
835 struct drm_minor {
836         int index;                      /**< Minor device number */
837         int type;                       /**< Control or render */
838         device_t kdev;                  /**< OS device */
839         struct drm_device *dev;
840
841         struct drm_master *master; /* currently active master for this node */
842         struct list_head master_list;
843         struct drm_mode_group mode_group;
844 };
845
846 struct drm_pending_vblank_event {
847         struct drm_pending_event base;
848         int pipe;
849         struct drm_event_vblank event;
850 };
851
852 struct drm_sysctl_info {
853         struct sysctl_ctx_list ctx;
854         char   name[2];
855 };
856
857 /* Length for the array of resource pointers for drm_get_resource_*. */
858 #define DRM_MAX_PCI_RESOURCE    6
859
860 struct drm_vblank_crtc {
861         struct drm_device *dev;         /* pointer to the drm_device */
862         wait_queue_head_t queue;        /**< VBLANK wait queue */
863         struct timeval time[DRM_VBLANKTIME_RBSIZE];     /**< timestamp of current count */
864         struct timer_list disable_timer;                /* delayed disable timer */
865         atomic_t count;                 /**< number of VBLANK interrupts */
866         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
867         u32 last;                       /* protected by dev->vbl_lock, used */
868                                         /* for wraparound handling */
869         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
870         unsigned int inmodeset;         /* Display driver is setting mode */
871         int crtc;                       /* crtc index */
872         bool enabled;                   /* so we don't call enable more than
873                                            once per disable */
874 };
875
876 /**
877  * DRM device structure. This structure represent a complete card that
878  * may contain multiple heads.
879  */
880 struct drm_device {
881         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
882
883         uint16_t pci_subdevice;         /* PCI subsystem device id */
884         uint16_t pci_subvendor;         /* PCI subsystem vendor id */
885
886         char              *unique;      /* Unique identifier: e.g., busid  */
887         int               unique_len;   /* Length of unique field          */
888         struct cdev       *devnode;     /* Device number for mknod         */
889         int               if_version;   /* Highest interface version set */
890
891         int               flags;        /* Flags to open(2)                */
892
893                                 /* Locks */
894         struct spinlock   dma_lock;     /* protects dev->dma */
895         struct lwkt_serialize irq_lock; /* protects irq condition checks */
896         struct lock       dev_lock;     /* protects everything else */
897
898         /** \name Locks */
899         /*@{ */
900         struct lock struct_mutex;       /**< For others */
901         struct lock master_mutex;       /**< For drm_minor::master */
902         /*@} */
903
904         /** \name Usage Counters */
905         /*@{ */
906         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
907         struct spinlock buf_lock;       /**< For drm_device::buf_use and a few other things. */
908         int buf_use;                    /**< Buffers in use -- cannot alloc */
909         atomic_t buf_alloc;             /**< Buffer allocation in progress */
910         /*@} */
911
912
913         /** \name Performance counters */
914         /*@{ */
915         unsigned long     counters;
916         enum drm_stat_type      types[15];
917         atomic_t          counts[15];
918         /*@} */
919
920                                 /* Authentication */
921         struct drm_open_hash magiclist; /**< magic hash table */
922         struct list_head magicfree;
923
924         struct list_head filelist;
925
926         /** \name Memory management */
927         /*@{ */
928         struct list_head maplist;       /**< Linked list of regions */
929         int map_count;                  /**< Number of mappable regions */
930         struct drm_open_hash map_hash;  /**< User token hash table for maps */
931
932         /** \name Context handle management */
933         /*@{ */
934         struct list_head ctxlist;       /**< Linked list of context handles */
935         int ctx_count;                  /**< Number of context handles */
936         struct lock ctxlist_mutex;      /**< For ctxlist */
937
938         struct idr ctx_idr;
939
940         /*@} */
941
942         struct drm_lock_data lock;      /* Information on hardware lock    */
943
944                                 /* DMA queues (contexts) */
945         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
946
947         int               irq;          /* Interrupt used by board         */
948         int               irq_type;     /* IRQ type (MSI enabled or not) */
949         int               irqrid;       /* Interrupt used by board */
950         struct resource   *irqr;        /* Resource for interrupt used by board    */
951         void              *irqh;        /* Handle from bus_setup_intr      */
952
953         /* Storage of resource pointers for drm_get_resource_* */
954         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
955         int               pcirid[DRM_MAX_PCI_RESOURCE];
956
957         int               pci_domain;
958         int               pci_bus;
959         int               pci_slot;
960         int               pci_func;
961
962         /** \name Context support */
963         /*@{ */
964         int irq_enabled;                /**< True if irq handler is enabled */
965         __volatile__ long context_flag; /**< Context swapping flag */
966         __volatile__ long interrupt_flag; /**< Interruption handler flag */
967         __volatile__ long dma_flag;     /**< DMA dispatch flag */
968         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
969         int last_checked;               /**< Last context checked for DMA */
970         int last_context;               /**< Last current context */
971         unsigned long last_switch;      /**< jiffies at last context switch */
972         /*@} */
973
974         /** \name VBLANK IRQ support */
975         /*@{ */
976
977         /*
978          * At load time, disabling the vblank interrupt won't be allowed since
979          * old clients may not call the modeset ioctl and therefore misbehave.
980          * Once the modeset ioctl *has* been called though, we can safely
981          * disable them when unused.
982          */
983         bool vblank_disable_allowed;
984
985         /*
986          * If true, vblank interrupt will be disabled immediately when the
987          * refcount drops to zero, as opposed to via the vblank disable
988          * timer.
989          * This can be set to true it the hardware has a working vblank
990          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
991          * appropriately.
992          */
993         bool vblank_disable_immediate;
994
995         /* array of size num_crtcs */
996         struct drm_vblank_crtc *vblank;
997
998         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
999         struct lock vbl_lock;
1000         struct timer_list vblank_disable_timer;
1001
1002         u32 max_vblank_count;           /**< size of vblank counter register */
1003
1004         /**
1005          * List of events
1006          */
1007         struct list_head vblank_event_list;
1008         struct lock event_lock;
1009
1010         /*@} */
1011
1012         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1013
1014                                 /* Sysctl support */
1015         struct drm_sysctl_info *sysctl;
1016
1017
1018         drm_sg_mem_t      *sg;  /* Scatter gather memory */
1019         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1020
1021         unsigned long     *ctx_bitmap;
1022         void              *dev_private;
1023
1024         void              *drm_ttm_bdev;
1025
1026         /*@} */
1027
1028         struct drm_agp_head *agp;       /**< AGP data */
1029
1030         struct device *dev;             /**< Device structure */
1031         struct pci_dev *pdev;           /**< PCI device structure */
1032         int pci_vendor;                 /**< PCI vendor id */
1033         int pci_device;                 /**< PCI device id */
1034
1035         struct drm_driver *driver;
1036         struct drm_local_map *agp_buffer_map;
1037         unsigned int agp_buffer_token;
1038         struct drm_minor *control;              /**< Control node for card */
1039         struct drm_minor *primary;              /**< render type primary screen head */
1040
1041         struct drm_mode_config mode_config;     /**< Current mode config */
1042
1043         /** \name GEM information */
1044         /*@{ */
1045         struct lock object_name_lock;
1046         struct idr object_name_idr;
1047         /*@} */
1048         void             *mm_private;
1049
1050         void *sysctl_private;
1051         char busid_str[128];
1052         int modesetting;
1053
1054         int switch_power_state;
1055
1056         atomic_t unplugged; /* device has been unplugged or gone away */
1057 };
1058
1059 #define DRM_SWITCH_POWER_ON 0
1060 #define DRM_SWITCH_POWER_OFF 1
1061 #define DRM_SWITCH_POWER_CHANGING 2
1062 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1063
1064 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1065                                              int feature)
1066 {
1067         return ((dev->driver->driver_features & feature) ? 1 : 0);
1068 }
1069
1070 static inline void drm_device_set_unplugged(struct drm_device *dev)
1071 {
1072         smp_wmb();
1073         atomic_set(&dev->unplugged, 1);
1074 }
1075
1076 static inline int drm_device_is_unplugged(struct drm_device *dev)
1077 {
1078         int ret = atomic_read(&dev->unplugged);
1079         smp_rmb();
1080         return ret;
1081 }
1082
1083 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1084 {
1085         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1086 }
1087
1088 /**
1089  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1090  * @file: DRM open-file context
1091  *
1092  * This checks whether a DRM open-file context is owner of the master context
1093  * attached to it. If a file owns a master context, it's called DRM-Master.
1094  * Per DRM device, only one such file can be DRM-Master at a time.
1095  *
1096  * Returns: True if the file is DRM-Master, otherwise false.
1097  */
1098 static inline bool drm_is_master(const struct drm_file *file)
1099 {
1100 #if 0
1101         return file->master && file->master == file->minor->master;
1102 #else
1103         return true;
1104 #endif
1105 }
1106
1107 /******************************************************************/
1108 /** \name Internal function definitions */
1109 /*@{*/
1110
1111 #if __OS_HAS_AGP
1112 static inline int drm_core_has_AGP(struct drm_device *dev)
1113 {
1114         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1115 }
1116 #else
1117 #define drm_core_has_AGP(dev) (0)
1118 #endif
1119
1120 extern int      drm_notyet_flag;
1121 extern unsigned int drm_rnodes;
1122 extern unsigned int drm_universal_planes;
1123
1124 extern int drm_vblank_offdelay;
1125 extern unsigned int drm_timestamp_precision;
1126 extern unsigned int drm_timestamp_monotonic;
1127
1128                                 /* Driver support (drm_drv.h) */
1129 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1130 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1131 int     drm_create_cdevs(device_t kdev);
1132 d_ioctl_t drm_ioctl;
1133 extern int drm_lastclose(struct drm_device *dev);
1134 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1135
1136                                 /* Device support (drm_fops.h) */
1137 extern struct lock drm_global_mutex;
1138 d_open_t drm_open;
1139 d_close_t drm_close;
1140 d_read_t drm_read;
1141 d_kqfilter_t drm_kqfilter;
1142 int drm_release(device_t kdev);
1143
1144 d_mmap_t drm_mmap;
1145 d_mmap_single_t drm_mmap_single;
1146
1147 void drm_cdevpriv_dtor(void *cd);
1148
1149 int drm_add_busid_modesetting(struct drm_device *dev,
1150     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1151
1152 /* File operations helpers (drm_fops.c) */
1153 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1154                                          DRM_STRUCTPROC *p,
1155                                         struct drm_device *dev,
1156                                         struct file *fp);
1157 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1158                                         DRM_STRUCTPROC *p);
1159
1160 #ifdef DUMBBELL_WIP
1161 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1162                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1163                 int *prime_fd);
1164 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1165                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1166
1167 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1168                                         struct drm_file *file_priv);
1169 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1170                                         struct drm_file *file_priv);
1171
1172 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1173                                             dma_addr_t *addrs, int max_pages);
1174 #endif /* DUMBBELL_WIP */
1175 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1176 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1177
1178 #ifdef DUMBBELL_WIP
1179 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1180 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1181 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1182 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1183 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1184
1185 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1186 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1187                          struct drm_gem_object **obj);
1188 #endif /* DUMBBELL_WIP */
1189
1190 void    drm_mem_init(void);
1191 void    drm_mem_uninit(void);
1192 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1193 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1194 void    drm_ioremapfree(drm_local_map_t *map);
1195 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1196 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1197
1198 /* Locking IOCTL support (drm_lock.c) */
1199 int     drm_lock_take(struct drm_lock_data *lock_data,
1200                       unsigned int context);
1201 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1202                           unsigned int context);
1203 int     drm_lock_free(struct drm_lock_data *lock_data,
1204                       unsigned int context);
1205
1206 /*
1207  * These are exported to drivers so that they can implement fencing using
1208  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1209  */
1210
1211 unsigned long drm_get_resource_start(struct drm_device *dev,
1212                                      unsigned int resource);
1213 unsigned long drm_get_resource_len(struct drm_device *dev,
1214                                    unsigned int resource);
1215
1216                                 /* IRQ support (drm_irq.h) */
1217 extern int drm_irq_install(struct drm_device *dev, int irq);
1218 int     drm_irq_uninstall(struct drm_device *dev);
1219 void    drm_driver_irq_preinstall(struct drm_device *dev);
1220 void    drm_driver_irq_postinstall(struct drm_device *dev);
1221 void    drm_driver_irq_uninstall(struct drm_device *dev);
1222
1223 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1224 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1225                            struct drm_file *filp);
1226 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1227 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1228                                      struct timeval *vblanktime);
1229 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1230                                      struct drm_pending_vblank_event *e);
1231 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1232 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1233 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1234 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1235 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1236 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1237 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1238 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1239 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1240 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1241 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1242 extern void drm_vblank_cleanup(struct drm_device *dev);
1243
1244 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1245                                                  int crtc, int *max_error,
1246                                                  struct timeval *vblank_time,
1247                                                  unsigned flags,
1248                                                  const struct drm_crtc *refcrtc,
1249                                                  const struct drm_display_mode *mode);
1250 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1251                                             const struct drm_display_mode *mode);
1252
1253 /**
1254  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1255  * @crtc: which CRTC's vblank waitqueue to retrieve
1256  *
1257  * This function returns a pointer to the vblank waitqueue for the CRTC.
1258  * Drivers can use this to implement vblank waits using wait_event() & co.
1259  */
1260 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1261 {
1262        return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1263 }
1264
1265 /* Modesetting support */
1266 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1267 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1268 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1269                            struct drm_file *file_priv);
1270
1271                                 /* AGP/GART support (drm_agpsupport.h) */
1272
1273 #include <drm/drm_agpsupport.h>
1274
1275 /* sysctl support (drm_sysctl.h) */
1276 extern int              drm_sysctl_init(struct drm_device *dev);
1277 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1278
1279 /* Cache management (drm_memory.c) */
1280 void    drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1281
1282 /* Locking IOCTL support (drm_drv.c) */
1283 int     drm_lock(struct drm_device *dev, void *data,
1284                  struct drm_file *file_priv);
1285 int     drm_unlock(struct drm_device *dev, void *data,
1286                    struct drm_file *file_priv);
1287 int     drm_setversion(struct drm_device *dev, void *data,
1288                        struct drm_file *file_priv);
1289
1290 /*
1291  * These are exported to drivers so that they can implement fencing using
1292  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1293  */
1294
1295 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1296
1297 /* Misc. IOCTL support (drm_ioctl.c) */
1298 int     drm_irq_by_busid(struct drm_device *dev, void *data,
1299                          struct drm_file *file_priv);
1300 int     drm_getunique(struct drm_device *dev, void *data,
1301                       struct drm_file *file_priv);
1302 int     drm_setunique(struct drm_device *dev, void *data,
1303                       struct drm_file *file_priv);
1304 int     drm_getmap(struct drm_device *dev, void *data,
1305                    struct drm_file *file_priv);
1306 int     drm_getclient(struct drm_device *dev, void *data,
1307                       struct drm_file *file_priv);
1308 int     drm_getstats(struct drm_device *dev, void *data,
1309                      struct drm_file *file_priv);
1310 int     drm_getcap(struct drm_device *dev, void *data,
1311                      struct drm_file *file_priv);
1312 extern int drm_setclientcap(struct drm_device *dev, void *data,
1313                             struct drm_file *file_priv);
1314 int     drm_noop(struct drm_device *dev, void *data,
1315                  struct drm_file *file_priv);
1316
1317 /* Authentication IOCTL support (drm_auth.c) */
1318 int     drm_getmagic(struct drm_device *dev, void *data,
1319                      struct drm_file *file_priv);
1320 int     drm_authmagic(struct drm_device *dev, void *data,
1321                       struct drm_file *file_priv);
1322
1323 /* Cache management (drm_cache.c) */
1324 void drm_clflush_virt_range(void *addr, unsigned long length);
1325
1326 /* DMA support (drm_dma.c) */
1327 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1328
1329 /* IRQ support (drm_irq.c) */
1330 int     drm_control(struct drm_device *dev, void *data,
1331                     struct drm_file *file_priv);
1332
1333                                 /* Stub support (drm_stub.h) */
1334 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1335                                struct drm_file *file_priv);
1336 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1337                                 struct drm_file *file_priv);
1338
1339 extern void drm_put_dev(struct drm_device *dev);
1340 extern void drm_unplug_dev(struct drm_device *dev);
1341 extern unsigned int drm_debug;
1342
1343 /* Scatter Gather Support (drm_scatter.c) */
1344 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1345                            struct drm_file *file_priv);
1346 int     drm_sg_free(struct drm_device *dev, void *data,
1347                     struct drm_file *file_priv);
1348
1349 /* consistent PCI memory functions (drm_pci.c) */
1350 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1351                                        size_t align);
1352 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1353
1354                                /* sysfs support (drm_sysfs.c) */
1355 struct drm_sysfs_class;
1356 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1357 extern void drm_sysfs_destroy(void);
1358 extern int drm_sysfs_device_add(struct drm_minor *minor);
1359 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1360 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1361 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1362 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1363
1364 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1365                         struct drm_file *file_priv);
1366 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1367                         struct drm_file *file_priv);
1368 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1369                        struct drm_file *file_priv);
1370 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1371 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1372
1373 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1374     vm_size_t size, struct vm_object **obj_res, int nprot);
1375 void drm_gem_pager_dtr(void *obj);
1376
1377 struct ttm_bo_device;
1378 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1379     vm_size_t size, struct vm_object **obj_res, int nprot);
1380 struct ttm_buffer_object;
1381 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1382
1383 void drm_device_lock_mtx(struct drm_device *dev);
1384 void drm_device_unlock_mtx(struct drm_device *dev);
1385 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1386     const char *msg, int timeout);
1387 void drm_device_assert_mtx_locked(struct drm_device *dev);
1388 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1389
1390 void drm_device_lock_struct(struct drm_device *dev);
1391 void drm_device_unlock_struct(struct drm_device *dev);
1392 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1393     const char *msg, int timeout);
1394 void drm_device_assert_struct_locked(struct drm_device *dev);
1395 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1396
1397 void drm_compat_locking_init(struct drm_device *dev);
1398 void drm_sleep_locking_init(struct drm_device *dev);
1399
1400 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1401 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1402 void drm_fini_pdev(struct pci_dev **pdev);
1403
1404 /* Inline replacements for drm_alloc and friends */
1405 static __inline__ void *
1406 drm_alloc(size_t size, struct malloc_type *area)
1407 {
1408         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1409 }
1410
1411 static __inline__ void *
1412 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1413 {
1414         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1415 }
1416
1417 static __inline__ void
1418 drm_free(void *pt, struct malloc_type *area)
1419 {
1420         /* kfree is special!!! */
1421         if (pt != NULL)
1422                 (kfree)(pt, area);
1423 }
1424
1425 static __inline__ void
1426 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1427 {
1428         map->handle = drm_ioremap_wc(dev, map);
1429 }
1430 static __inline__ void
1431 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1432 {
1433         map->handle = drm_ioremap(dev, map);
1434 }
1435 static __inline__ void
1436 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1437 {
1438         if ( map->handle && map->size )
1439                 drm_ioremapfree(map);
1440 }
1441
1442 static __inline__ struct drm_local_map *
1443 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1444 {
1445         struct drm_map_list *_entry;
1446
1447         list_for_each_entry(_entry, &dev->maplist, head) {
1448                 if (offset == (unsigned long)_entry->map->handle)
1449                         return _entry->map;
1450         }
1451         return NULL;
1452 }
1453
1454 static __inline__ void drm_core_dropmap(struct drm_map *map)
1455 {
1456 }
1457
1458 #include <drm/drm_mem_util.h>
1459
1460 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1461                                  struct device *parent);
1462 void drm_dev_ref(struct drm_device *dev);
1463 void drm_dev_unref(struct drm_device *dev);
1464 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1465 void drm_dev_unregister(struct drm_device *dev);
1466 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1467
1468 /* FreeBSD compatibility macros */
1469 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1470 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1471
1472 #define DRM_PCIE_SPEED_25 1
1473 #define DRM_PCIE_SPEED_50 2
1474 #define DRM_PCIE_SPEED_80 4
1475
1476 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1477
1478 /* XXX bad */
1479 #define drm_can_sleep() (HZ & 1)
1480
1481 #endif /* __KERNEL__ */
1482 #endif /* _DRM_P_H_ */