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