GNU Linux-libre 4.4.299-gnu1
[releases.git] / include / drm / drmP.h
1 /*
2  * Internal Header for the Direct Rendering Manager
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * Copyright (c) 2009-2010, Code Aurora Forum.
7  * All rights reserved.
8  *
9  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10  * Author: Gareth Hughes <gareth@valinux.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the next
20  * paragraph) shall be included in all copies or substantial portions of the
21  * Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  */
31
32 #ifndef _DRM_P_H_
33 #define _DRM_P_H_
34
35 #include <linux/agp_backend.h>
36 #include <linux/cdev.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/file.h>
39 #include <linux/fs.h>
40 #include <linux/highmem.h>
41 #include <linux/idr.h>
42 #include <linux/init.h>
43 #include <linux/io.h>
44 #include <linux/jiffies.h>
45 #include <linux/kernel.h>
46 #include <linux/kref.h>
47 #include <linux/miscdevice.h>
48 #include <linux/mm.h>
49 #include <linux/mutex.h>
50 #include <linux/pci.h>
51 #include <linux/platform_device.h>
52 #include <linux/poll.h>
53 #include <linux/ratelimit.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/vmalloc.h>
58 #include <linux/workqueue.h>
59
60 #include <asm/mman.h>
61 #include <asm/pgalloc.h>
62 #include <asm/uaccess.h>
63
64 #include <uapi/drm/drm.h>
65 #include <uapi/drm/drm_mode.h>
66
67 #include <drm/drm_agpsupport.h>
68 #include <drm/drm_crtc.h>
69 #include <drm/drm_global.h>
70 #include <drm/drm_hashtab.h>
71 #include <drm/drm_mem_util.h>
72 #include <drm/drm_mm.h>
73 #include <drm/drm_os_linux.h>
74 #include <drm/drm_sarea.h>
75 #include <drm/drm_vma_manager.h>
76
77 struct module;
78
79 struct drm_file;
80 struct drm_device;
81 struct drm_agp_head;
82 struct drm_local_map;
83 struct drm_device_dma;
84 struct drm_dma_handle;
85 struct drm_gem_object;
86
87 struct device_node;
88 struct videomode;
89 struct reservation_object;
90 struct dma_buf_attachment;
91
92 /*
93  * 4 debug categories are defined:
94  *
95  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
96  *       This is the category used by the DRM_DEBUG() macro.
97  *
98  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
99  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
100  *
101  * KMS: used in the modesetting code.
102  *      This is the category used by the DRM_DEBUG_KMS() macro.
103  *
104  * PRIME: used in the prime code.
105  *        This is the category used by the DRM_DEBUG_PRIME() macro.
106  *
107  * ATOMIC: used in the atomic code.
108  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
109  *
110  * VBL: used for verbose debug message in the vblank code
111  *        This is the category used by the DRM_DEBUG_VBL() macro.
112  *
113  * Enabling verbose debug messages is done through the drm.debug parameter,
114  * each category being enabled by a bit.
115  *
116  * drm.debug=0x1 will enable CORE messages
117  * drm.debug=0x2 will enable DRIVER messages
118  * drm.debug=0x3 will enable CORE and DRIVER messages
119  * ...
120  * drm.debug=0x3f will enable all messages
121  *
122  * An interesting feature is that it's possible to enable verbose logging at
123  * run-time by echoing the debug value in its sysfs node:
124  *   # echo 0xf > /sys/module/drm/parameters/debug
125  */
126 #define DRM_UT_CORE             0x01
127 #define DRM_UT_DRIVER           0x02
128 #define DRM_UT_KMS              0x04
129 #define DRM_UT_PRIME            0x08
130 #define DRM_UT_ATOMIC           0x10
131 #define DRM_UT_VBL              0x20
132
133 extern __printf(2, 3)
134 void drm_ut_debug_printk(const char *function_name,
135                          const char *format, ...);
136 extern __printf(1, 2)
137 void drm_err(const char *format, ...);
138
139 /***********************************************************************/
140 /** \name DRM template customization defaults */
141 /*@{*/
142
143 /* driver capabilities and requirements mask */
144 #define DRIVER_USE_AGP                  0x1
145 #define DRIVER_PCI_DMA                  0x8
146 #define DRIVER_SG                       0x10
147 #define DRIVER_HAVE_DMA                 0x20
148 #define DRIVER_HAVE_IRQ                 0x40
149 #define DRIVER_IRQ_SHARED               0x80
150 #define DRIVER_GEM                      0x1000
151 #define DRIVER_MODESET                  0x2000
152 #define DRIVER_PRIME                    0x4000
153 #define DRIVER_RENDER                   0x8000
154 #define DRIVER_ATOMIC                   0x10000
155 #define DRIVER_KMS_LEGACY_CONTEXT       0x20000
156
157 /***********************************************************************/
158 /** \name Macros to make printk easier */
159 /*@{*/
160
161 #define _DRM_PRINTK(once, level, fmt, ...)                              \
162         do {                                                            \
163                 printk##once(KERN_##level "[" DRM_NAME "] " fmt,        \
164                              ##__VA_ARGS__);                            \
165         } while (0)
166
167 #define DRM_INFO(fmt, ...)                                              \
168         _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
169 #define DRM_NOTE(fmt, ...)                                              \
170         _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
171 #define DRM_WARN(fmt, ...)                                              \
172         _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
173
174 #define DRM_INFO_ONCE(fmt, ...)                                         \
175         _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
176 #define DRM_NOTE_ONCE(fmt, ...)                                         \
177         _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
178 #define DRM_WARN_ONCE(fmt, ...)                                         \
179         _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
180
181 /**
182  * Error output.
183  *
184  * \param fmt printf() like format string.
185  * \param arg arguments
186  */
187 #define DRM_ERROR(fmt, ...)                             \
188         drm_err(fmt, ##__VA_ARGS__)
189
190 /**
191  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
192  *
193  * \param fmt printf() like format string.
194  * \param arg arguments
195  */
196 #define DRM_ERROR_RATELIMITED(fmt, ...)                         \
197 ({                                                                      \
198         static DEFINE_RATELIMIT_STATE(_rs,                              \
199                                       DEFAULT_RATELIMIT_INTERVAL,       \
200                                       DEFAULT_RATELIMIT_BURST);         \
201                                                                         \
202         if (__ratelimit(&_rs))                                          \
203                 drm_err(fmt, ##__VA_ARGS__);                            \
204 })
205
206 /**
207  * Debug output.
208  *
209  * \param fmt printf() like format string.
210  * \param arg arguments
211  */
212 #define DRM_DEBUG(fmt, args...)                                         \
213         do {                                                            \
214                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
215                         drm_ut_debug_printk(__func__, fmt, ##args);     \
216         } while (0)
217
218 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
219         do {                                                            \
220                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
221                         drm_ut_debug_printk(__func__, fmt, ##args);     \
222         } while (0)
223 #define DRM_DEBUG_KMS(fmt, args...)                                     \
224         do {                                                            \
225                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
226                         drm_ut_debug_printk(__func__, fmt, ##args);     \
227         } while (0)
228 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
229         do {                                                            \
230                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
231                         drm_ut_debug_printk(__func__, fmt, ##args);     \
232         } while (0)
233 #define DRM_DEBUG_ATOMIC(fmt, args...)                                  \
234         do {                                                            \
235                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                \
236                         drm_ut_debug_printk(__func__, fmt, ##args);     \
237         } while (0)
238 #define DRM_DEBUG_VBL(fmt, args...)                                     \
239         do {                                                            \
240                 if (unlikely(drm_debug & DRM_UT_VBL))                   \
241                         drm_ut_debug_printk(__func__, fmt, ##args);     \
242         } while (0)
243
244 /*@}*/
245
246 /***********************************************************************/
247 /** \name Internal types and structures */
248 /*@{*/
249
250 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
251
252 /**
253  * Ioctl function type.
254  *
255  * \param inode device inode.
256  * \param file_priv DRM file private pointer.
257  * \param cmd command.
258  * \param arg argument.
259  */
260 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
261                         struct drm_file *file_priv);
262
263 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
264                                unsigned long arg);
265
266 #define DRM_IOCTL_NR(n)                _IOC_NR(n)
267 #define DRM_MAJOR       226
268
269 #define DRM_AUTH        0x1
270 #define DRM_MASTER      0x2
271 #define DRM_ROOT_ONLY   0x4
272 #define DRM_CONTROL_ALLOW 0x8
273 #define DRM_UNLOCKED    0x10
274 #define DRM_RENDER_ALLOW 0x20
275
276 struct drm_ioctl_desc {
277         unsigned int cmd;
278         int flags;
279         drm_ioctl_t *func;
280         const char *name;
281 };
282
283 /**
284  * Creates a driver or general drm_ioctl_desc array entry for the given
285  * ioctl, for use by drm_ioctl().
286  */
287
288 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                         \
289         [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = {        \
290                 .cmd = DRM_IOCTL_##ioctl,                               \
291                 .func = _func,                                          \
292                 .flags = _flags,                                        \
293                 .name = #ioctl                                          \
294          }
295
296 /* Event queued up for userspace to read */
297 struct drm_pending_event {
298         struct drm_event *event;
299         struct list_head link;
300         struct drm_file *file_priv;
301         pid_t pid; /* pid of requester, no guarantee it's valid by the time
302                       we deliver the event, for tracing only */
303         void (*destroy)(struct drm_pending_event *event);
304 };
305
306 /* initial implementaton using a linked list - todo hashtab */
307 struct drm_prime_file_private {
308         struct list_head head;
309         struct mutex lock;
310 };
311
312 /** File private data */
313 struct drm_file {
314         unsigned authenticated :1;
315         /* Whether we're master for a minor. Protected by master_mutex */
316         unsigned is_master :1;
317         /* true when the client has asked us to expose stereo 3D mode flags */
318         unsigned stereo_allowed :1;
319         /*
320          * true if client understands CRTC primary planes and cursor planes
321          * in the plane list
322          */
323         unsigned universal_planes:1;
324         /* true if client understands atomic properties */
325         unsigned atomic:1;
326         /*
327          * This client is allowed to gain master privileges for @master.
328          * Protected by struct drm_device::master_mutex.
329          */
330         unsigned allowed_master:1;
331
332         struct pid *pid;
333         kuid_t uid;
334         drm_magic_t magic;
335         struct list_head lhead;
336         struct drm_minor *minor;
337         unsigned long lock_count;
338
339         /** Mapping of mm object handles to object pointers. */
340         struct idr object_idr;
341         /** Lock for synchronization of access to object_idr. */
342         spinlock_t table_lock;
343
344         struct file *filp;
345         void *driver_priv;
346
347         struct drm_master *master; /* master this node is currently associated with
348                                       N.B. not always minor->master */
349         /**
350          * fbs - List of framebuffers associated with this file.
351          *
352          * Protected by fbs_lock. Note that the fbs list holds a reference on
353          * the fb object to prevent it from untimely disappearing.
354          */
355         struct list_head fbs;
356         struct mutex fbs_lock;
357
358         /** User-created blob properties; this retains a reference on the
359          *  property. */
360         struct list_head blobs;
361
362         wait_queue_head_t event_wait;
363         struct list_head event_list;
364         int event_space;
365
366         struct drm_prime_file_private prime;
367 };
368
369 /**
370  * Lock data.
371  */
372 struct drm_lock_data {
373         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
374         /** Private of lock holder's file (NULL=kernel) */
375         struct drm_file *file_priv;
376         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
377         unsigned long lock_time;        /**< Time of last lock in jiffies */
378         spinlock_t spinlock;
379         uint32_t kernel_waiters;
380         uint32_t user_waiters;
381         int idle_has_lock;
382 };
383
384 /**
385  * struct drm_master - drm master structure
386  *
387  * @refcount: Refcount for this master object.
388  * @minor: Link back to minor char device we are master for. Immutable.
389  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
390  * @unique_len: Length of unique field. Protected by drm_global_mutex.
391  * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
392  * @lock: DRI lock information.
393  * @driver_priv: Pointer to driver-private information.
394  */
395 struct drm_master {
396         struct kref refcount;
397         struct drm_minor *minor;
398         char *unique;
399         int unique_len;
400         struct idr magic_map;
401         struct drm_lock_data lock;
402         void *driver_priv;
403 };
404
405 /* Size of ringbuffer for vblank timestamps. Just double-buffer
406  * in initial implementation.
407  */
408 #define DRM_VBLANKTIME_RBSIZE 2
409
410 /* Flags and return codes for get_vblank_timestamp() driver function. */
411 #define DRM_CALLED_FROM_VBLIRQ 1
412 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
413 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
414
415 /* get_scanout_position() return flags */
416 #define DRM_SCANOUTPOS_VALID        (1 << 0)
417 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
418 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
419
420 /**
421  * DRM driver structure. This structure represent the common code for
422  * a family of cards. There will one drm_device for each card present
423  * in this family
424  */
425 struct drm_driver {
426         int (*load) (struct drm_device *, unsigned long flags);
427         int (*firstopen) (struct drm_device *);
428         int (*open) (struct drm_device *, struct drm_file *);
429         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
430         void (*postclose) (struct drm_device *, struct drm_file *);
431         void (*lastclose) (struct drm_device *);
432         int (*unload) (struct drm_device *);
433         int (*suspend) (struct drm_device *, pm_message_t state);
434         int (*resume) (struct drm_device *);
435         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
436         int (*dma_quiescent) (struct drm_device *);
437         int (*context_dtor) (struct drm_device *dev, int context);
438         int (*set_busid)(struct drm_device *dev, struct drm_master *master);
439
440         /**
441          * get_vblank_counter - get raw hardware vblank counter
442          * @dev: DRM device
443          * @pipe: counter to fetch
444          *
445          * Driver callback for fetching a raw hardware vblank counter for @crtc.
446          * If a device doesn't have a hardware counter, the driver can simply
447          * return the value of drm_vblank_count. The DRM core will account for
448          * missed vblank events while interrupts where disabled based on system
449          * timestamps.
450          *
451          * Wraparound handling and loss of events due to modesetting is dealt
452          * with in the DRM core code.
453          *
454          * RETURNS
455          * Raw vblank counter value.
456          */
457         u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
458
459         /**
460          * enable_vblank - enable vblank interrupt events
461          * @dev: DRM device
462          * @pipe: which irq to enable
463          *
464          * Enable vblank interrupts for @crtc.  If the device doesn't have
465          * a hardware vblank counter, this routine should be a no-op, since
466          * interrupts will have to stay on to keep the count accurate.
467          *
468          * RETURNS
469          * Zero on success, appropriate errno if the given @crtc's vblank
470          * interrupt cannot be enabled.
471          */
472         int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
473
474         /**
475          * disable_vblank - disable vblank interrupt events
476          * @dev: DRM device
477          * @pipe: which irq to enable
478          *
479          * Disable vblank interrupts for @crtc.  If the device doesn't have
480          * a hardware vblank counter, this routine should be a no-op, since
481          * interrupts will have to stay on to keep the count accurate.
482          */
483         void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
484
485         /**
486          * Called by \c drm_device_is_agp.  Typically used to determine if a
487          * card is really attached to AGP or not.
488          *
489          * \param dev  DRM device handle
490          *
491          * \returns
492          * One of three values is returned depending on whether or not the
493          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
494          * (return of 1), or may or may not be AGP (return of 2).
495          */
496         int (*device_is_agp) (struct drm_device *dev);
497
498         /**
499          * Called by vblank timestamping code.
500          *
501          * Return the current display scanout position from a crtc, and an
502          * optional accurate ktime_get timestamp of when position was measured.
503          *
504          * \param dev  DRM device.
505          * \param pipe Id of the crtc to query.
506          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
507          * \param *vpos Target location for current vertical scanout position.
508          * \param *hpos Target location for current horizontal scanout position.
509          * \param *stime Target location for timestamp taken immediately before
510          *               scanout position query. Can be NULL to skip timestamp.
511          * \param *etime Target location for timestamp taken immediately after
512          *               scanout position query. Can be NULL to skip timestamp.
513          * \param mode Current display timings.
514          *
515          * Returns vpos as a positive number while in active scanout area.
516          * Returns vpos as a negative number inside vblank, counting the number
517          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
518          * until start of active scanout / end of vblank."
519          *
520          * \return Flags, or'ed together as follows:
521          *
522          * DRM_SCANOUTPOS_VALID = Query successful.
523          * DRM_SCANOUTPOS_INVBL = Inside vblank.
524          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
525          * this flag means that returned position may be offset by a constant
526          * but unknown small number of scanlines wrt. real scanout position.
527          *
528          */
529         int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
530                                      unsigned int flags, int *vpos, int *hpos,
531                                      ktime_t *stime, ktime_t *etime,
532                                      const struct drm_display_mode *mode);
533
534         /**
535          * Called by \c drm_get_last_vbltimestamp. Should return a precise
536          * timestamp when the most recent VBLANK interval ended or will end.
537          *
538          * Specifically, the timestamp in @vblank_time should correspond as
539          * closely as possible to the time when the first video scanline of
540          * the video frame after the end of VBLANK will start scanning out,
541          * the time immediately after end of the VBLANK interval. If the
542          * @crtc is currently inside VBLANK, this will be a time in the future.
543          * If the @crtc is currently scanning out a frame, this will be the
544          * past start time of the current scanout. This is meant to adhere
545          * to the OpenML OML_sync_control extension specification.
546          *
547          * \param dev dev DRM device handle.
548          * \param pipe crtc for which timestamp should be returned.
549          * \param *max_error Maximum allowable timestamp error in nanoseconds.
550          *                   Implementation should strive to provide timestamp
551          *                   with an error of at most *max_error nanoseconds.
552          *                   Returns true upper bound on error for timestamp.
553          * \param *vblank_time Target location for returned vblank timestamp.
554          * \param flags 0 = Defaults, no special treatment needed.
555          * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
556          *              irq handler. Some drivers need to apply some workarounds
557          *              for gpu-specific vblank irq quirks if flag is set.
558          *
559          * \returns
560          * Zero if timestamping isn't supported in current display mode or a
561          * negative number on failure. A positive status code on success,
562          * which describes how the vblank_time timestamp was computed.
563          */
564         int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
565                                      int *max_error,
566                                      struct timeval *vblank_time,
567                                      unsigned flags);
568
569         /* these have to be filled in */
570
571         irqreturn_t(*irq_handler) (int irq, void *arg);
572         void (*irq_preinstall) (struct drm_device *dev);
573         int (*irq_postinstall) (struct drm_device *dev);
574         void (*irq_uninstall) (struct drm_device *dev);
575
576         /* Master routines */
577         int (*master_create)(struct drm_device *dev, struct drm_master *master);
578         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
579         /**
580          * master_set is called whenever the minor master is set.
581          * master_drop is called whenever the minor master is dropped.
582          */
583
584         int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
585                           bool from_open);
586         void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
587                             bool from_release);
588
589         int (*debugfs_init)(struct drm_minor *minor);
590         void (*debugfs_cleanup)(struct drm_minor *minor);
591
592         /**
593          * Driver-specific constructor for drm_gem_objects, to set up
594          * obj->driver_private.
595          *
596          * Returns 0 on success.
597          */
598         void (*gem_free_object) (struct drm_gem_object *obj);
599         int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
600         void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
601
602         /* prime: */
603         /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
604         int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
605                                 uint32_t handle, uint32_t flags, int *prime_fd);
606         /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
607         int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
608                                 int prime_fd, uint32_t *handle);
609         /* export GEM -> dmabuf */
610         struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
611                                 struct drm_gem_object *obj, int flags);
612         /* import dmabuf -> GEM */
613         struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
614                                 struct dma_buf *dma_buf);
615         /* low-level interface used by drm_gem_prime_{import,export} */
616         int (*gem_prime_pin)(struct drm_gem_object *obj);
617         void (*gem_prime_unpin)(struct drm_gem_object *obj);
618         struct reservation_object * (*gem_prime_res_obj)(
619                                 struct drm_gem_object *obj);
620         struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
621         struct drm_gem_object *(*gem_prime_import_sg_table)(
622                                 struct drm_device *dev,
623                                 struct dma_buf_attachment *attach,
624                                 struct sg_table *sgt);
625         void *(*gem_prime_vmap)(struct drm_gem_object *obj);
626         void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
627         int (*gem_prime_mmap)(struct drm_gem_object *obj,
628                                 struct vm_area_struct *vma);
629
630         /* vga arb irq handler */
631         void (*vgaarb_irq)(struct drm_device *dev, bool state);
632
633         /* dumb alloc support */
634         int (*dumb_create)(struct drm_file *file_priv,
635                            struct drm_device *dev,
636                            struct drm_mode_create_dumb *args);
637         int (*dumb_map_offset)(struct drm_file *file_priv,
638                                struct drm_device *dev, uint32_t handle,
639                                uint64_t *offset);
640         int (*dumb_destroy)(struct drm_file *file_priv,
641                             struct drm_device *dev,
642                             uint32_t handle);
643
644         /* Driver private ops for this object */
645         const struct vm_operations_struct *gem_vm_ops;
646
647         int major;
648         int minor;
649         int patchlevel;
650         char *name;
651         char *desc;
652         char *date;
653
654         u32 driver_features;
655         int dev_priv_size;
656         const struct drm_ioctl_desc *ioctls;
657         int num_ioctls;
658         const struct file_operations *fops;
659
660         /* List of devices hanging off this driver with stealth attach. */
661         struct list_head legacy_dev_list;
662 };
663
664 enum drm_minor_type {
665         DRM_MINOR_LEGACY,
666         DRM_MINOR_CONTROL,
667         DRM_MINOR_RENDER,
668         DRM_MINOR_CNT,
669 };
670
671 /**
672  * Info file list entry. This structure represents a debugfs or proc file to
673  * be created by the drm core
674  */
675 struct drm_info_list {
676         const char *name; /** file name */
677         int (*show)(struct seq_file*, void*); /** show callback */
678         u32 driver_features; /**< Required driver features for this entry */
679         void *data;
680 };
681
682 /**
683  * debugfs node structure. This structure represents a debugfs file.
684  */
685 struct drm_info_node {
686         struct list_head list;
687         struct drm_minor *minor;
688         const struct drm_info_list *info_ent;
689         struct dentry *dent;
690 };
691
692 /**
693  * DRM minor structure. This structure represents a drm minor number.
694  */
695 struct drm_minor {
696         int index;                      /**< Minor device number */
697         int type;                       /**< Control or render */
698         struct device *kdev;            /**< Linux device */
699         struct drm_device *dev;
700
701         struct dentry *debugfs_root;
702
703         struct list_head debugfs_list;
704         struct mutex debugfs_lock; /* Protects debugfs_list. */
705
706         /* currently active master for this node. Protected by master_mutex */
707         struct drm_master *master;
708 };
709
710
711 struct drm_pending_vblank_event {
712         struct drm_pending_event base;
713         unsigned int pipe;
714         struct drm_event_vblank event;
715 };
716
717 struct drm_vblank_crtc {
718         struct drm_device *dev;         /* pointer to the drm_device */
719         wait_queue_head_t queue;        /**< VBLANK wait queue */
720         struct timer_list disable_timer;                /* delayed disable timer */
721
722         /* vblank counter, protected by dev->vblank_time_lock for writes */
723         u32 count;
724         /* vblank timestamps, protected by dev->vblank_time_lock for writes */
725         struct timeval time[DRM_VBLANKTIME_RBSIZE];
726
727         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
728         u32 last;                       /* protected by dev->vbl_lock, used */
729                                         /* for wraparound handling */
730         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
731         unsigned int inmodeset;         /* Display driver is setting mode */
732         unsigned int pipe;              /* crtc index */
733         int framedur_ns;                /* frame/field duration in ns */
734         int linedur_ns;                 /* line duration in ns */
735         bool enabled;                   /* so we don't call enable more than
736                                            once per disable */
737 };
738
739 /**
740  * DRM device structure. This structure represent a complete card that
741  * may contain multiple heads.
742  */
743 struct drm_device {
744         struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
745         int if_version;                 /**< Highest interface version set */
746
747         /** \name Lifetime Management */
748         /*@{ */
749         struct kref ref;                /**< Object ref-count */
750         struct device *dev;             /**< Device structure of bus-device */
751         struct drm_driver *driver;      /**< DRM driver managing the device */
752         void *dev_private;              /**< DRM driver private data */
753         struct drm_minor *control;              /**< Control node */
754         struct drm_minor *primary;              /**< Primary node */
755         struct drm_minor *render;               /**< Render node */
756         atomic_t unplugged;                     /**< Flag whether dev is dead */
757         struct inode *anon_inode;               /**< inode for private address-space */
758         char *unique;                           /**< unique name of the device */
759         /*@} */
760
761         /** \name Locks */
762         /*@{ */
763         struct mutex struct_mutex;      /**< For others */
764         struct mutex master_mutex;      /**< For drm_minor::master and drm_file::is_master */
765         /*@} */
766
767         /** \name Usage Counters */
768         /*@{ */
769         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
770         spinlock_t buf_lock;            /**< For drm_device::buf_use and a few other things. */
771         int buf_use;                    /**< Buffers in use -- cannot alloc */
772         atomic_t buf_alloc;             /**< Buffer allocation in progress */
773         /*@} */
774
775         struct list_head filelist;
776
777         /** \name Memory management */
778         /*@{ */
779         struct list_head maplist;       /**< Linked list of regions */
780         struct drm_open_hash map_hash;  /**< User token hash table for maps */
781
782         /** \name Context handle management */
783         /*@{ */
784         struct list_head ctxlist;       /**< Linked list of context handles */
785         struct mutex ctxlist_mutex;     /**< For ctxlist */
786
787         struct idr ctx_idr;
788
789         struct list_head vmalist;       /**< List of vmas (for debugging) */
790
791         /*@} */
792
793         /** \name DMA support */
794         /*@{ */
795         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
796         /*@} */
797
798         /** \name Context support */
799         /*@{ */
800
801         __volatile__ long context_flag; /**< Context swapping flag */
802         int last_context;               /**< Last current context */
803         /*@} */
804
805         /** \name VBLANK IRQ support */
806         /*@{ */
807         bool irq_enabled;
808         int irq;
809
810         /*
811          * At load time, disabling the vblank interrupt won't be allowed since
812          * old clients may not call the modeset ioctl and therefore misbehave.
813          * Once the modeset ioctl *has* been called though, we can safely
814          * disable them when unused.
815          */
816         bool vblank_disable_allowed;
817
818         /*
819          * If true, vblank interrupt will be disabled immediately when the
820          * refcount drops to zero, as opposed to via the vblank disable
821          * timer.
822          * This can be set to true it the hardware has a working vblank
823          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
824          * appropriately.
825          */
826         bool vblank_disable_immediate;
827
828         /* array of size num_crtcs */
829         struct drm_vblank_crtc *vblank;
830
831         spinlock_t vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
832         spinlock_t vbl_lock;
833
834         u32 max_vblank_count;           /**< size of vblank counter register */
835
836         /**
837          * List of events
838          */
839         struct list_head vblank_event_list;
840         spinlock_t event_lock;
841
842         /*@} */
843
844         struct drm_agp_head *agp;       /**< AGP data */
845
846         struct pci_dev *pdev;           /**< PCI device structure */
847 #ifdef __alpha__
848         struct pci_controller *hose;
849 #endif
850
851         struct platform_device *platformdev; /**< Platform device struture */
852         struct virtio_device *virtdev;
853
854         struct drm_sg_mem *sg;  /**< Scatter gather memory */
855         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
856
857         struct {
858                 int context;
859                 struct drm_hw_lock *lock;
860         } sigdata;
861
862         struct drm_local_map *agp_buffer_map;
863         unsigned int agp_buffer_token;
864
865         struct drm_mode_config mode_config;     /**< Current mode config */
866
867         /** \name GEM information */
868         /*@{ */
869         struct mutex object_name_lock;
870         struct idr object_name_idr;
871         struct drm_vma_offset_manager *vma_offset_manager;
872         /*@} */
873         int switch_power_state;
874 };
875
876 #define DRM_SWITCH_POWER_ON 0
877 #define DRM_SWITCH_POWER_OFF 1
878 #define DRM_SWITCH_POWER_CHANGING 2
879 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
880
881 static __inline__ int drm_core_check_feature(struct drm_device *dev,
882                                              int feature)
883 {
884         return ((dev->driver->driver_features & feature) ? 1 : 0);
885 }
886
887 static inline void drm_device_set_unplugged(struct drm_device *dev)
888 {
889         smp_wmb();
890         atomic_set(&dev->unplugged, 1);
891 }
892
893 static inline int drm_device_is_unplugged(struct drm_device *dev)
894 {
895         int ret = atomic_read(&dev->unplugged);
896         smp_rmb();
897         return ret;
898 }
899
900 static inline bool drm_is_render_client(const struct drm_file *file_priv)
901 {
902         return file_priv->minor->type == DRM_MINOR_RENDER;
903 }
904
905 static inline bool drm_is_control_client(const struct drm_file *file_priv)
906 {
907         return file_priv->minor->type == DRM_MINOR_CONTROL;
908 }
909
910 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
911 {
912         return file_priv->minor->type == DRM_MINOR_LEGACY;
913 }
914
915 /******************************************************************/
916 /** \name Internal function definitions */
917 /*@{*/
918
919                                 /* Driver support (drm_drv.h) */
920 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
921 extern long drm_ioctl(struct file *filp,
922                       unsigned int cmd, unsigned long arg);
923 extern long drm_compat_ioctl(struct file *filp,
924                              unsigned int cmd, unsigned long arg);
925 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
926
927                                 /* Device support (drm_fops.h) */
928 extern int drm_open(struct inode *inode, struct file *filp);
929 extern ssize_t drm_read(struct file *filp, char __user *buffer,
930                         size_t count, loff_t *offset);
931 extern int drm_release(struct inode *inode, struct file *filp);
932 extern int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
933
934                                 /* Mapping support (drm_vm.h) */
935 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
936
937 /* Misc. IOCTL support (drm_ioctl.c) */
938 int drm_noop(struct drm_device *dev, void *data,
939              struct drm_file *file_priv);
940 int drm_invalid_op(struct drm_device *dev, void *data,
941                    struct drm_file *file_priv);
942
943 /* Cache management (drm_cache.c) */
944 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
945 void drm_clflush_sg(struct sg_table *st);
946 void drm_clflush_virt_range(void *addr, unsigned long length);
947
948 /*
949  * These are exported to drivers so that they can implement fencing using
950  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
951  */
952
953                                 /* IRQ support (drm_irq.h) */
954 extern int drm_irq_install(struct drm_device *dev, int irq);
955 extern int drm_irq_uninstall(struct drm_device *dev);
956
957 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
958 extern int drm_wait_vblank(struct drm_device *dev, void *data,
959                            struct drm_file *filp);
960 extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
961 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
962 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
963                                      struct timeval *vblanktime);
964 extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
965                                           struct timeval *vblanktime);
966 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
967                                   struct drm_pending_vblank_event *e);
968 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
969                                        struct drm_pending_vblank_event *e);
970 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
971                                  struct drm_pending_vblank_event *e);
972 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
973                                       struct drm_pending_vblank_event *e);
974 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
975 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
976 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
977 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
978 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
979 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
980 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
981 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
982 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
983 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
984 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
985 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
986 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
987 extern void drm_vblank_cleanup(struct drm_device *dev);
988 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
989
990 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
991                                                  unsigned int pipe, int *max_error,
992                                                  struct timeval *vblank_time,
993                                                  unsigned flags,
994                                                  const struct drm_display_mode *mode);
995 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
996                                             const struct drm_display_mode *mode);
997
998 /**
999  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1000  * @crtc: which CRTC's vblank waitqueue to retrieve
1001  *
1002  * This function returns a pointer to the vblank waitqueue for the CRTC.
1003  * Drivers can use this to implement vblank waits using wait_event() & co.
1004  */
1005 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1006 {
1007         return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1008 }
1009
1010 /* Modesetting support */
1011 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1012 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1013
1014                                 /* Stub support (drm_stub.h) */
1015 extern struct drm_master *drm_master_get(struct drm_master *master);
1016 extern void drm_master_put(struct drm_master **master);
1017
1018 extern void drm_put_dev(struct drm_device *dev);
1019 extern void drm_unplug_dev(struct drm_device *dev);
1020 extern unsigned int drm_debug;
1021 extern bool drm_atomic;
1022
1023                                 /* Debugfs support */
1024 #if defined(CONFIG_DEBUG_FS)
1025 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1026                                     int count, struct dentry *root,
1027                                     struct drm_minor *minor);
1028 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1029                                     int count, struct drm_minor *minor);
1030 #else
1031 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1032                                            int count, struct dentry *root,
1033                                            struct drm_minor *minor)
1034 {
1035         return 0;
1036 }
1037
1038 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1039                                            int count, struct drm_minor *minor)
1040 {
1041         return 0;
1042 }
1043 #endif
1044
1045 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1046                                             struct drm_gem_object *obj,
1047                                             int flags);
1048 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1049                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1050                 int *prime_fd);
1051 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1052                 struct dma_buf *dma_buf);
1053 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1054                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1055 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1056
1057 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1058                                             dma_addr_t *addrs, int max_pages);
1059 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
1060 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1061
1062
1063 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1064                                             size_t align);
1065 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1066
1067                                /* sysfs support (drm_sysfs.c) */
1068 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1069
1070
1071 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1072                                  struct device *parent);
1073 void drm_dev_ref(struct drm_device *dev);
1074 void drm_dev_unref(struct drm_device *dev);
1075 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1076 void drm_dev_unregister(struct drm_device *dev);
1077 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1078
1079 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1080 void drm_minor_release(struct drm_minor *minor);
1081
1082 /*@}*/
1083
1084 /* PCI section */
1085 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1086 {
1087         if (dev->driver->device_is_agp != NULL) {
1088                 int err = (*dev->driver->device_is_agp) (dev);
1089
1090                 if (err != 2) {
1091                         return err;
1092                 }
1093         }
1094
1095         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1096 }
1097 void drm_pci_agp_destroy(struct drm_device *dev);
1098
1099 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1100 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1101 #ifdef CONFIG_PCI
1102 extern int drm_get_pci_dev(struct pci_dev *pdev,
1103                            const struct pci_device_id *ent,
1104                            struct drm_driver *driver);
1105 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1106 #else
1107 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1108                                   const struct pci_device_id *ent,
1109                                   struct drm_driver *driver)
1110 {
1111         return -ENOSYS;
1112 }
1113
1114 static inline int drm_pci_set_busid(struct drm_device *dev,
1115                                     struct drm_master *master)
1116 {
1117         return -ENOSYS;
1118 }
1119 #endif
1120
1121 #define DRM_PCIE_SPEED_25 1
1122 #define DRM_PCIE_SPEED_50 2
1123 #define DRM_PCIE_SPEED_80 4
1124
1125 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1126
1127 /* platform section */
1128 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1129 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1130
1131 /* returns true if currently okay to sleep */
1132 static __inline__ bool drm_can_sleep(void)
1133 {
1134         if (in_atomic() || in_dbg_master() || irqs_disabled())
1135                 return false;
1136         return true;
1137 }
1138
1139 #endif