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