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