drm: Remove useless else block
[cascardo/linux.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41
42 #include "drm_crtc_internal.h"
43 #include "drm_internal.h"
44
45 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
46                                                         struct drm_mode_fb_cmd2 *r,
47                                                         struct drm_file *file_priv);
48
49 /* Avoid boilerplate.  I'm tired of typing. */
50 #define DRM_ENUM_NAME_FN(fnname, list)                          \
51         const char *fnname(int val)                             \
52         {                                                       \
53                 int i;                                          \
54                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
55                         if (list[i].type == val)                \
56                                 return list[i].name;            \
57                 }                                               \
58                 return "(unknown)";                             \
59         }
60
61 /*
62  * Global properties
63  */
64 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
65         { DRM_MODE_DPMS_ON, "On" },
66         { DRM_MODE_DPMS_STANDBY, "Standby" },
67         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
68         { DRM_MODE_DPMS_OFF, "Off" }
69 };
70
71 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
72
73 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
74         { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
75         { DRM_PLANE_TYPE_PRIMARY, "Primary" },
76         { DRM_PLANE_TYPE_CURSOR, "Cursor" },
77 };
78
79 /*
80  * Optional properties
81  */
82 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
83         { DRM_MODE_SCALE_NONE, "None" },
84         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
85         { DRM_MODE_SCALE_CENTER, "Center" },
86         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
87 };
88
89 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
90         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
91         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
92         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
93 };
94
95 /*
96  * Non-global properties, but "required" for certain connectors.
97  */
98 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
99         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
100         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
101         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
102 };
103
104 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
105
106 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
107         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
108         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
109         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
110 };
111
112 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
113                  drm_dvi_i_subconnector_enum_list)
114
115 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
116         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
117         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
118         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
119         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
120         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
121 };
122
123 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
124
125 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
126         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
127         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
128         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
129         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
130         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
131 };
132
133 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
134                  drm_tv_subconnector_enum_list)
135
136 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
137         { DRM_MODE_DIRTY_OFF,      "Off"      },
138         { DRM_MODE_DIRTY_ON,       "On"       },
139         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
140 };
141
142 struct drm_conn_prop_enum_list {
143         int type;
144         const char *name;
145         struct ida ida;
146 };
147
148 /*
149  * Connector and encoder types.
150  */
151 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
152         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
153         { DRM_MODE_CONNECTOR_VGA, "VGA" },
154         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
155         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
156         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
157         { DRM_MODE_CONNECTOR_Composite, "Composite" },
158         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
159         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
160         { DRM_MODE_CONNECTOR_Component, "Component" },
161         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
162         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
163         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
164         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
165         { DRM_MODE_CONNECTOR_TV, "TV" },
166         { DRM_MODE_CONNECTOR_eDP, "eDP" },
167         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
168         { DRM_MODE_CONNECTOR_DSI, "DSI" },
169 };
170
171 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
172         { DRM_MODE_ENCODER_NONE, "None" },
173         { DRM_MODE_ENCODER_DAC, "DAC" },
174         { DRM_MODE_ENCODER_TMDS, "TMDS" },
175         { DRM_MODE_ENCODER_LVDS, "LVDS" },
176         { DRM_MODE_ENCODER_TVDAC, "TV" },
177         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
178         { DRM_MODE_ENCODER_DSI, "DSI" },
179         { DRM_MODE_ENCODER_DPMST, "DP MST" },
180 };
181
182 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
183         { SubPixelUnknown, "Unknown" },
184         { SubPixelHorizontalRGB, "Horizontal RGB" },
185         { SubPixelHorizontalBGR, "Horizontal BGR" },
186         { SubPixelVerticalRGB, "Vertical RGB" },
187         { SubPixelVerticalBGR, "Vertical BGR" },
188         { SubPixelNone, "None" },
189 };
190
191 void drm_connector_ida_init(void)
192 {
193         int i;
194
195         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
196                 ida_init(&drm_connector_enum_list[i].ida);
197 }
198
199 void drm_connector_ida_destroy(void)
200 {
201         int i;
202
203         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
204                 ida_destroy(&drm_connector_enum_list[i].ida);
205 }
206
207 /**
208  * drm_get_connector_status_name - return a string for connector status
209  * @status: connector status to compute name of
210  *
211  * In contrast to the other drm_get_*_name functions this one here returns a
212  * const pointer and hence is threadsafe.
213  */
214 const char *drm_get_connector_status_name(enum drm_connector_status status)
215 {
216         if (status == connector_status_connected)
217                 return "connected";
218         else if (status == connector_status_disconnected)
219                 return "disconnected";
220         else
221                 return "unknown";
222 }
223 EXPORT_SYMBOL(drm_get_connector_status_name);
224
225 /**
226  * drm_get_subpixel_order_name - return a string for a given subpixel enum
227  * @order: enum of subpixel_order
228  *
229  * Note you could abuse this and return something out of bounds, but that
230  * would be a caller error.  No unscrubbed user data should make it here.
231  */
232 const char *drm_get_subpixel_order_name(enum subpixel_order order)
233 {
234         return drm_subpixel_enum_list[order].name;
235 }
236 EXPORT_SYMBOL(drm_get_subpixel_order_name);
237
238 static char printable_char(int c)
239 {
240         return isascii(c) && isprint(c) ? c : '?';
241 }
242
243 /**
244  * drm_get_format_name - return a string for drm fourcc format
245  * @format: format to compute name of
246  *
247  * Note that the buffer used by this function is globally shared and owned by
248  * the function itself.
249  *
250  * FIXME: This isn't really multithreading safe.
251  */
252 const char *drm_get_format_name(uint32_t format)
253 {
254         static char buf[32];
255
256         snprintf(buf, sizeof(buf),
257                  "%c%c%c%c %s-endian (0x%08x)",
258                  printable_char(format & 0xff),
259                  printable_char((format >> 8) & 0xff),
260                  printable_char((format >> 16) & 0xff),
261                  printable_char((format >> 24) & 0x7f),
262                  format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
263                  format);
264
265         return buf;
266 }
267 EXPORT_SYMBOL(drm_get_format_name);
268
269 /*
270  * Internal function to assign a slot in the object idr and optionally
271  * register the object into the idr.
272  */
273 static int drm_mode_object_get_reg(struct drm_device *dev,
274                                    struct drm_mode_object *obj,
275                                    uint32_t obj_type,
276                                    bool register_obj)
277 {
278         int ret;
279
280         mutex_lock(&dev->mode_config.idr_mutex);
281         ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
282         if (ret >= 0) {
283                 /*
284                  * Set up the object linking under the protection of the idr
285                  * lock so that other users can't see inconsistent state.
286                  */
287                 obj->id = ret;
288                 obj->type = obj_type;
289         }
290         mutex_unlock(&dev->mode_config.idr_mutex);
291
292         return ret < 0 ? ret : 0;
293 }
294
295 /**
296  * drm_mode_object_get - allocate a new modeset identifier
297  * @dev: DRM device
298  * @obj: object pointer, used to generate unique ID
299  * @obj_type: object type
300  *
301  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
302  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
303  * modeset identifiers are _not_ reference counted. Hence don't use this for
304  * reference counted modeset objects like framebuffers.
305  *
306  * Returns:
307  * New unique (relative to other objects in @dev) integer identifier for the
308  * object.
309  */
310 int drm_mode_object_get(struct drm_device *dev,
311                         struct drm_mode_object *obj, uint32_t obj_type)
312 {
313         return drm_mode_object_get_reg(dev, obj, obj_type, true);
314 }
315
316 static void drm_mode_object_register(struct drm_device *dev,
317                                      struct drm_mode_object *obj)
318 {
319         mutex_lock(&dev->mode_config.idr_mutex);
320         idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
321         mutex_unlock(&dev->mode_config.idr_mutex);
322 }
323
324 /**
325  * drm_mode_object_put - free a modeset identifer
326  * @dev: DRM device
327  * @object: object to free
328  *
329  * Free @id from @dev's unique identifier pool. Note that despite the _get
330  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
331  * for reference counted modeset objects like framebuffers.
332  */
333 void drm_mode_object_put(struct drm_device *dev,
334                          struct drm_mode_object *object)
335 {
336         mutex_lock(&dev->mode_config.idr_mutex);
337         idr_remove(&dev->mode_config.crtc_idr, object->id);
338         mutex_unlock(&dev->mode_config.idr_mutex);
339 }
340
341 static struct drm_mode_object *_object_find(struct drm_device *dev,
342                 uint32_t id, uint32_t type)
343 {
344         struct drm_mode_object *obj = NULL;
345
346         mutex_lock(&dev->mode_config.idr_mutex);
347         obj = idr_find(&dev->mode_config.crtc_idr, id);
348         if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
349                 obj = NULL;
350         if (obj && obj->id != id)
351                 obj = NULL;
352         /* don't leak out unref'd fb's */
353         if (obj && (obj->type == DRM_MODE_OBJECT_FB))
354                 obj = NULL;
355         mutex_unlock(&dev->mode_config.idr_mutex);
356
357         return obj;
358 }
359
360 /**
361  * drm_mode_object_find - look up a drm object with static lifetime
362  * @dev: drm device
363  * @id: id of the mode object
364  * @type: type of the mode object
365  *
366  * Note that framebuffers cannot be looked up with this functions - since those
367  * are reference counted, they need special treatment.  Even with
368  * DRM_MODE_OBJECT_ANY (although that will simply return NULL
369  * rather than WARN_ON()).
370  */
371 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
372                 uint32_t id, uint32_t type)
373 {
374         struct drm_mode_object *obj = NULL;
375
376         /* Framebuffers are reference counted and need their own lookup
377          * function.*/
378         WARN_ON(type == DRM_MODE_OBJECT_FB);
379         obj = _object_find(dev, id, type);
380         return obj;
381 }
382 EXPORT_SYMBOL(drm_mode_object_find);
383
384 /**
385  * drm_framebuffer_init - initialize a framebuffer
386  * @dev: DRM device
387  * @fb: framebuffer to be initialized
388  * @funcs: ... with these functions
389  *
390  * Allocates an ID for the framebuffer's parent mode object, sets its mode
391  * functions & device file and adds it to the master fd list.
392  *
393  * IMPORTANT:
394  * This functions publishes the fb and makes it available for concurrent access
395  * by other users. Which means by this point the fb _must_ be fully set up -
396  * since all the fb attributes are invariant over its lifetime, no further
397  * locking but only correct reference counting is required.
398  *
399  * Returns:
400  * Zero on success, error code on failure.
401  */
402 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
403                          const struct drm_framebuffer_funcs *funcs)
404 {
405         int ret;
406
407         mutex_lock(&dev->mode_config.fb_lock);
408         kref_init(&fb->refcount);
409         INIT_LIST_HEAD(&fb->filp_head);
410         fb->dev = dev;
411         fb->funcs = funcs;
412
413         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
414         if (ret)
415                 goto out;
416
417         dev->mode_config.num_fb++;
418         list_add(&fb->head, &dev->mode_config.fb_list);
419 out:
420         mutex_unlock(&dev->mode_config.fb_lock);
421
422         return 0;
423 }
424 EXPORT_SYMBOL(drm_framebuffer_init);
425
426 /* dev->mode_config.fb_lock must be held! */
427 static void __drm_framebuffer_unregister(struct drm_device *dev,
428                                          struct drm_framebuffer *fb)
429 {
430         mutex_lock(&dev->mode_config.idr_mutex);
431         idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
432         mutex_unlock(&dev->mode_config.idr_mutex);
433
434         fb->base.id = 0;
435 }
436
437 static void drm_framebuffer_free(struct kref *kref)
438 {
439         struct drm_framebuffer *fb =
440                         container_of(kref, struct drm_framebuffer, refcount);
441         struct drm_device *dev = fb->dev;
442
443         /*
444          * The lookup idr holds a weak reference, which has not necessarily been
445          * removed at this point. Check for that.
446          */
447         mutex_lock(&dev->mode_config.fb_lock);
448         if (fb->base.id) {
449                 /* Mark fb as reaped and drop idr ref. */
450                 __drm_framebuffer_unregister(dev, fb);
451         }
452         mutex_unlock(&dev->mode_config.fb_lock);
453
454         fb->funcs->destroy(fb);
455 }
456
457 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
458                                                         uint32_t id)
459 {
460         struct drm_mode_object *obj = NULL;
461         struct drm_framebuffer *fb;
462
463         mutex_lock(&dev->mode_config.idr_mutex);
464         obj = idr_find(&dev->mode_config.crtc_idr, id);
465         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
466                 fb = NULL;
467         else
468                 fb = obj_to_fb(obj);
469         mutex_unlock(&dev->mode_config.idr_mutex);
470
471         return fb;
472 }
473
474 /**
475  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
476  * @dev: drm device
477  * @id: id of the fb object
478  *
479  * If successful, this grabs an additional reference to the framebuffer -
480  * callers need to make sure to eventually unreference the returned framebuffer
481  * again, using @drm_framebuffer_unreference.
482  */
483 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
484                                                uint32_t id)
485 {
486         struct drm_framebuffer *fb;
487
488         mutex_lock(&dev->mode_config.fb_lock);
489         fb = __drm_framebuffer_lookup(dev, id);
490         if (fb) {
491                 if (!kref_get_unless_zero(&fb->refcount))
492                         fb = NULL;
493         }
494         mutex_unlock(&dev->mode_config.fb_lock);
495
496         return fb;
497 }
498 EXPORT_SYMBOL(drm_framebuffer_lookup);
499
500 /**
501  * drm_framebuffer_unreference - unref a framebuffer
502  * @fb: framebuffer to unref
503  *
504  * This functions decrements the fb's refcount and frees it if it drops to zero.
505  */
506 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
507 {
508         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
509         kref_put(&fb->refcount, drm_framebuffer_free);
510 }
511 EXPORT_SYMBOL(drm_framebuffer_unreference);
512
513 /**
514  * drm_framebuffer_reference - incr the fb refcnt
515  * @fb: framebuffer
516  *
517  * This functions increments the fb's refcount.
518  */
519 void drm_framebuffer_reference(struct drm_framebuffer *fb)
520 {
521         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
522         kref_get(&fb->refcount);
523 }
524 EXPORT_SYMBOL(drm_framebuffer_reference);
525
526 static void drm_framebuffer_free_bug(struct kref *kref)
527 {
528         BUG();
529 }
530
531 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
532 {
533         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
534         kref_put(&fb->refcount, drm_framebuffer_free_bug);
535 }
536
537 /**
538  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
539  * @fb: fb to unregister
540  *
541  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
542  * those used for fbdev. Note that the caller must hold a reference of it's own,
543  * i.e. the object may not be destroyed through this call (since it'll lead to a
544  * locking inversion).
545  */
546 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
547 {
548         struct drm_device *dev = fb->dev;
549
550         mutex_lock(&dev->mode_config.fb_lock);
551         /* Mark fb as reaped and drop idr ref. */
552         __drm_framebuffer_unregister(dev, fb);
553         mutex_unlock(&dev->mode_config.fb_lock);
554 }
555 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
556
557 /**
558  * drm_framebuffer_cleanup - remove a framebuffer object
559  * @fb: framebuffer to remove
560  *
561  * Cleanup framebuffer. This function is intended to be used from the drivers
562  * ->destroy callback. It can also be used to clean up driver private
563  *  framebuffers embedded into a larger structure.
564  *
565  * Note that this function does not remove the fb from active usuage - if it is
566  * still used anywhere, hilarity can ensue since userspace could call getfb on
567  * the id and get back -EINVAL. Obviously no concern at driver unload time.
568  *
569  * Also, the framebuffer will not be removed from the lookup idr - for
570  * user-created framebuffers this will happen in in the rmfb ioctl. For
571  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
572  * drm_framebuffer_unregister_private.
573  */
574 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
575 {
576         struct drm_device *dev = fb->dev;
577
578         mutex_lock(&dev->mode_config.fb_lock);
579         list_del(&fb->head);
580         dev->mode_config.num_fb--;
581         mutex_unlock(&dev->mode_config.fb_lock);
582 }
583 EXPORT_SYMBOL(drm_framebuffer_cleanup);
584
585 /**
586  * drm_framebuffer_remove - remove and unreference a framebuffer object
587  * @fb: framebuffer to remove
588  *
589  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
590  * using @fb, removes it, setting it to NULL. Then drops the reference to the
591  * passed-in framebuffer. Might take the modeset locks.
592  *
593  * Note that this function optimizes the cleanup away if the caller holds the
594  * last reference to the framebuffer. It is also guaranteed to not take the
595  * modeset locks in this case.
596  */
597 void drm_framebuffer_remove(struct drm_framebuffer *fb)
598 {
599         struct drm_device *dev = fb->dev;
600         struct drm_crtc *crtc;
601         struct drm_plane *plane;
602         struct drm_mode_set set;
603         int ret;
604
605         WARN_ON(!list_empty(&fb->filp_head));
606
607         /*
608          * drm ABI mandates that we remove any deleted framebuffers from active
609          * useage. But since most sane clients only remove framebuffers they no
610          * longer need, try to optimize this away.
611          *
612          * Since we're holding a reference ourselves, observing a refcount of 1
613          * means that we're the last holder and can skip it. Also, the refcount
614          * can never increase from 1 again, so we don't need any barriers or
615          * locks.
616          *
617          * Note that userspace could try to race with use and instate a new
618          * usage _after_ we've cleared all current ones. End result will be an
619          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
620          * in this manner.
621          */
622         if (atomic_read(&fb->refcount.refcount) > 1) {
623                 drm_modeset_lock_all(dev);
624                 /* remove from any CRTC */
625                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
626                         if (crtc->primary->fb == fb) {
627                                 /* should turn off the crtc */
628                                 memset(&set, 0, sizeof(struct drm_mode_set));
629                                 set.crtc = crtc;
630                                 set.fb = NULL;
631                                 ret = drm_mode_set_config_internal(&set);
632                                 if (ret)
633                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
634                         }
635                 }
636
637                 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
638                         if (plane->fb == fb)
639                                 drm_plane_force_disable(plane);
640                 }
641                 drm_modeset_unlock_all(dev);
642         }
643
644         drm_framebuffer_unreference(fb);
645 }
646 EXPORT_SYMBOL(drm_framebuffer_remove);
647
648 DEFINE_WW_CLASS(crtc_ww_class);
649
650 /**
651  * drm_crtc_init_with_planes - Initialise a new CRTC object with
652  *    specified primary and cursor planes.
653  * @dev: DRM device
654  * @crtc: CRTC object to init
655  * @primary: Primary plane for CRTC
656  * @cursor: Cursor plane for CRTC
657  * @funcs: callbacks for the new CRTC
658  *
659  * Inits a new object created as base part of a driver crtc object.
660  *
661  * Returns:
662  * Zero on success, error code on failure.
663  */
664 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
665                               struct drm_plane *primary,
666                               struct drm_plane *cursor,
667                               const struct drm_crtc_funcs *funcs)
668 {
669         struct drm_mode_config *config = &dev->mode_config;
670         int ret;
671
672         crtc->dev = dev;
673         crtc->funcs = funcs;
674         crtc->invert_dimensions = false;
675
676         drm_modeset_lock_init(&crtc->mutex);
677         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
678         if (ret)
679                 return ret;
680
681         crtc->base.properties = &crtc->properties;
682
683         list_add_tail(&crtc->head, &config->crtc_list);
684         config->num_crtc++;
685
686         crtc->primary = primary;
687         crtc->cursor = cursor;
688         if (primary)
689                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
690         if (cursor)
691                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
692
693         return 0;
694 }
695 EXPORT_SYMBOL(drm_crtc_init_with_planes);
696
697 /**
698  * drm_crtc_cleanup - Clean up the core crtc usage
699  * @crtc: CRTC to cleanup
700  *
701  * This function cleans up @crtc and removes it from the DRM mode setting
702  * core. Note that the function does *not* free the crtc structure itself,
703  * this is the responsibility of the caller.
704  */
705 void drm_crtc_cleanup(struct drm_crtc *crtc)
706 {
707         struct drm_device *dev = crtc->dev;
708
709         kfree(crtc->gamma_store);
710         crtc->gamma_store = NULL;
711
712         drm_modeset_lock_fini(&crtc->mutex);
713
714         drm_mode_object_put(dev, &crtc->base);
715         list_del(&crtc->head);
716         dev->mode_config.num_crtc--;
717
718         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
719         if (crtc->state && crtc->funcs->atomic_destroy_state)
720                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
721
722         memset(crtc, 0, sizeof(*crtc));
723 }
724 EXPORT_SYMBOL(drm_crtc_cleanup);
725
726 /**
727  * drm_crtc_index - find the index of a registered CRTC
728  * @crtc: CRTC to find index for
729  *
730  * Given a registered CRTC, return the index of that CRTC within a DRM
731  * device's list of CRTCs.
732  */
733 unsigned int drm_crtc_index(struct drm_crtc *crtc)
734 {
735         unsigned int index = 0;
736         struct drm_crtc *tmp;
737
738         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
739                 if (tmp == crtc)
740                         return index;
741
742                 index++;
743         }
744
745         BUG();
746 }
747 EXPORT_SYMBOL(drm_crtc_index);
748
749 /*
750  * drm_mode_remove - remove and free a mode
751  * @connector: connector list to modify
752  * @mode: mode to remove
753  *
754  * Remove @mode from @connector's mode list, then free it.
755  */
756 static void drm_mode_remove(struct drm_connector *connector,
757                             struct drm_display_mode *mode)
758 {
759         list_del(&mode->head);
760         drm_mode_destroy(connector->dev, mode);
761 }
762
763 /**
764  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
765  * @connector: connector to quwery
766  *
767  * The kernel supports per-connector configration of its consoles through
768  * use of the video= parameter. This function parses that option and
769  * extracts the user's specified mode (or enable/disable status) for a
770  * particular connector. This is typically only used during the early fbdev
771  * setup.
772  */
773 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
774 {
775         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
776         char *option = NULL;
777
778         if (fb_get_options(connector->name, &option))
779                 return;
780
781         if (!drm_mode_parse_command_line_for_connector(option,
782                                                        connector,
783                                                        mode))
784                 return;
785
786         if (mode->force) {
787                 const char *s;
788
789                 switch (mode->force) {
790                 case DRM_FORCE_OFF:
791                         s = "OFF";
792                         break;
793                 case DRM_FORCE_ON_DIGITAL:
794                         s = "ON - dig";
795                         break;
796                 default:
797                 case DRM_FORCE_ON:
798                         s = "ON";
799                         break;
800                 }
801
802                 DRM_INFO("forcing %s connector %s\n", connector->name, s);
803                 connector->force = mode->force;
804         }
805
806         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
807                       connector->name,
808                       mode->xres, mode->yres,
809                       mode->refresh_specified ? mode->refresh : 60,
810                       mode->rb ? " reduced blanking" : "",
811                       mode->margins ? " with margins" : "",
812                       mode->interlace ?  " interlaced" : "");
813 }
814
815 /**
816  * drm_connector_init - Init a preallocated connector
817  * @dev: DRM device
818  * @connector: the connector to init
819  * @funcs: callbacks for this connector
820  * @connector_type: user visible type of the connector
821  *
822  * Initialises a preallocated connector. Connectors should be
823  * subclassed as part of driver connector objects.
824  *
825  * Returns:
826  * Zero on success, error code on failure.
827  */
828 int drm_connector_init(struct drm_device *dev,
829                        struct drm_connector *connector,
830                        const struct drm_connector_funcs *funcs,
831                        int connector_type)
832 {
833         int ret;
834         struct ida *connector_ida =
835                 &drm_connector_enum_list[connector_type].ida;
836
837         drm_modeset_lock_all(dev);
838
839         ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
840         if (ret)
841                 goto out_unlock;
842
843         connector->base.properties = &connector->properties;
844         connector->dev = dev;
845         connector->funcs = funcs;
846         connector->connector_type = connector_type;
847         connector->connector_type_id =
848                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
849         if (connector->connector_type_id < 0) {
850                 ret = connector->connector_type_id;
851                 goto out_put;
852         }
853         connector->name =
854                 kasprintf(GFP_KERNEL, "%s-%d",
855                           drm_connector_enum_list[connector_type].name,
856                           connector->connector_type_id);
857         if (!connector->name) {
858                 ret = -ENOMEM;
859                 goto out_put;
860         }
861
862         INIT_LIST_HEAD(&connector->probed_modes);
863         INIT_LIST_HEAD(&connector->modes);
864         connector->edid_blob_ptr = NULL;
865         connector->status = connector_status_unknown;
866
867         drm_connector_get_cmdline_mode(connector);
868
869         /* We should add connectors at the end to avoid upsetting the connector
870          * index too much. */
871         list_add_tail(&connector->head, &dev->mode_config.connector_list);
872         dev->mode_config.num_connector++;
873
874         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
875                 drm_object_attach_property(&connector->base,
876                                               dev->mode_config.edid_property,
877                                               0);
878
879         drm_object_attach_property(&connector->base,
880                                       dev->mode_config.dpms_property, 0);
881
882         connector->debugfs_entry = NULL;
883
884 out_put:
885         if (ret)
886                 drm_mode_object_put(dev, &connector->base);
887
888 out_unlock:
889         drm_modeset_unlock_all(dev);
890
891         return ret;
892 }
893 EXPORT_SYMBOL(drm_connector_init);
894
895 /**
896  * drm_connector_cleanup - cleans up an initialised connector
897  * @connector: connector to cleanup
898  *
899  * Cleans up the connector but doesn't free the object.
900  */
901 void drm_connector_cleanup(struct drm_connector *connector)
902 {
903         struct drm_device *dev = connector->dev;
904         struct drm_display_mode *mode, *t;
905
906         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
907                 drm_mode_remove(connector, mode);
908
909         list_for_each_entry_safe(mode, t, &connector->modes, head)
910                 drm_mode_remove(connector, mode);
911
912         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
913                    connector->connector_type_id);
914
915         drm_mode_object_put(dev, &connector->base);
916         kfree(connector->name);
917         connector->name = NULL;
918         list_del(&connector->head);
919         dev->mode_config.num_connector--;
920
921         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
922         if (connector->state && connector->funcs->atomic_destroy_state)
923                 connector->funcs->atomic_destroy_state(connector,
924                                                        connector->state);
925
926         memset(connector, 0, sizeof(*connector));
927 }
928 EXPORT_SYMBOL(drm_connector_cleanup);
929
930 /**
931  * drm_connector_index - find the index of a registered connector
932  * @connector: connector to find index for
933  *
934  * Given a registered connector, return the index of that connector within a DRM
935  * device's list of connectors.
936  */
937 unsigned int drm_connector_index(struct drm_connector *connector)
938 {
939         unsigned int index = 0;
940         struct drm_connector *tmp;
941         struct drm_mode_config *config = &connector->dev->mode_config;
942
943         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
944
945         list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
946                 if (tmp == connector)
947                         return index;
948
949                 index++;
950         }
951
952         BUG();
953 }
954 EXPORT_SYMBOL(drm_connector_index);
955
956 /**
957  * drm_connector_register - register a connector
958  * @connector: the connector to register
959  *
960  * Register userspace interfaces for a connector
961  *
962  * Returns:
963  * Zero on success, error code on failure.
964  */
965 int drm_connector_register(struct drm_connector *connector)
966 {
967         int ret;
968
969         drm_mode_object_register(connector->dev, &connector->base);
970
971         ret = drm_sysfs_connector_add(connector);
972         if (ret)
973                 return ret;
974
975         ret = drm_debugfs_connector_add(connector);
976         if (ret) {
977                 drm_sysfs_connector_remove(connector);
978                 return ret;
979         }
980
981         return 0;
982 }
983 EXPORT_SYMBOL(drm_connector_register);
984
985 /**
986  * drm_connector_unregister - unregister a connector
987  * @connector: the connector to unregister
988  *
989  * Unregister userspace interfaces for a connector
990  */
991 void drm_connector_unregister(struct drm_connector *connector)
992 {
993         drm_sysfs_connector_remove(connector);
994         drm_debugfs_connector_remove(connector);
995 }
996 EXPORT_SYMBOL(drm_connector_unregister);
997
998
999 /**
1000  * drm_connector_unplug_all - unregister connector userspace interfaces
1001  * @dev: drm device
1002  *
1003  * This function unregisters all connector userspace interfaces in sysfs. Should
1004  * be call when the device is disconnected, e.g. from an usb driver's
1005  * ->disconnect callback.
1006  */
1007 void drm_connector_unplug_all(struct drm_device *dev)
1008 {
1009         struct drm_connector *connector;
1010
1011         /* taking the mode config mutex ends up in a clash with sysfs */
1012         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1013                 drm_connector_unregister(connector);
1014
1015 }
1016 EXPORT_SYMBOL(drm_connector_unplug_all);
1017
1018 /**
1019  * drm_bridge_init - initialize a drm transcoder/bridge
1020  * @dev: drm device
1021  * @bridge: transcoder/bridge to set up
1022  * @funcs: bridge function table
1023  *
1024  * Initialises a preallocated bridge. Bridges should be
1025  * subclassed as part of driver connector objects.
1026  *
1027  * Returns:
1028  * Zero on success, error code on failure.
1029  */
1030 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1031                 const struct drm_bridge_funcs *funcs)
1032 {
1033         int ret;
1034
1035         drm_modeset_lock_all(dev);
1036
1037         ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1038         if (ret)
1039                 goto out;
1040
1041         bridge->dev = dev;
1042         bridge->funcs = funcs;
1043
1044         list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1045         dev->mode_config.num_bridge++;
1046
1047  out:
1048         drm_modeset_unlock_all(dev);
1049         return ret;
1050 }
1051 EXPORT_SYMBOL(drm_bridge_init);
1052
1053 /**
1054  * drm_bridge_cleanup - cleans up an initialised bridge
1055  * @bridge: bridge to cleanup
1056  *
1057  * Cleans up the bridge but doesn't free the object.
1058  */
1059 void drm_bridge_cleanup(struct drm_bridge *bridge)
1060 {
1061         struct drm_device *dev = bridge->dev;
1062
1063         drm_modeset_lock_all(dev);
1064         drm_mode_object_put(dev, &bridge->base);
1065         list_del(&bridge->head);
1066         dev->mode_config.num_bridge--;
1067         drm_modeset_unlock_all(dev);
1068
1069         memset(bridge, 0, sizeof(*bridge));
1070 }
1071 EXPORT_SYMBOL(drm_bridge_cleanup);
1072
1073 /**
1074  * drm_encoder_init - Init a preallocated encoder
1075  * @dev: drm device
1076  * @encoder: the encoder to init
1077  * @funcs: callbacks for this encoder
1078  * @encoder_type: user visible type of the encoder
1079  *
1080  * Initialises a preallocated encoder. Encoder should be
1081  * subclassed as part of driver encoder objects.
1082  *
1083  * Returns:
1084  * Zero on success, error code on failure.
1085  */
1086 int drm_encoder_init(struct drm_device *dev,
1087                       struct drm_encoder *encoder,
1088                       const struct drm_encoder_funcs *funcs,
1089                       int encoder_type)
1090 {
1091         int ret;
1092
1093         drm_modeset_lock_all(dev);
1094
1095         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1096         if (ret)
1097                 goto out_unlock;
1098
1099         encoder->dev = dev;
1100         encoder->encoder_type = encoder_type;
1101         encoder->funcs = funcs;
1102         encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1103                                   drm_encoder_enum_list[encoder_type].name,
1104                                   encoder->base.id);
1105         if (!encoder->name) {
1106                 ret = -ENOMEM;
1107                 goto out_put;
1108         }
1109
1110         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1111         dev->mode_config.num_encoder++;
1112
1113 out_put:
1114         if (ret)
1115                 drm_mode_object_put(dev, &encoder->base);
1116
1117 out_unlock:
1118         drm_modeset_unlock_all(dev);
1119
1120         return ret;
1121 }
1122 EXPORT_SYMBOL(drm_encoder_init);
1123
1124 /**
1125  * drm_encoder_cleanup - cleans up an initialised encoder
1126  * @encoder: encoder to cleanup
1127  *
1128  * Cleans up the encoder but doesn't free the object.
1129  */
1130 void drm_encoder_cleanup(struct drm_encoder *encoder)
1131 {
1132         struct drm_device *dev = encoder->dev;
1133
1134         drm_modeset_lock_all(dev);
1135         drm_mode_object_put(dev, &encoder->base);
1136         kfree(encoder->name);
1137         list_del(&encoder->head);
1138         dev->mode_config.num_encoder--;
1139         drm_modeset_unlock_all(dev);
1140
1141         memset(encoder, 0, sizeof(*encoder));
1142 }
1143 EXPORT_SYMBOL(drm_encoder_cleanup);
1144
1145 /**
1146  * drm_universal_plane_init - Initialize a new universal plane object
1147  * @dev: DRM device
1148  * @plane: plane object to init
1149  * @possible_crtcs: bitmask of possible CRTCs
1150  * @funcs: callbacks for the new plane
1151  * @formats: array of supported formats (%DRM_FORMAT_*)
1152  * @format_count: number of elements in @formats
1153  * @type: type of plane (overlay, primary, cursor)
1154  *
1155  * Initializes a plane object of type @type.
1156  *
1157  * Returns:
1158  * Zero on success, error code on failure.
1159  */
1160 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1161                              unsigned long possible_crtcs,
1162                              const struct drm_plane_funcs *funcs,
1163                              const uint32_t *formats, uint32_t format_count,
1164                              enum drm_plane_type type)
1165 {
1166         int ret;
1167
1168         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1169         if (ret)
1170                 return ret;
1171
1172         drm_modeset_lock_init(&plane->mutex);
1173
1174         plane->base.properties = &plane->properties;
1175         plane->dev = dev;
1176         plane->funcs = funcs;
1177         plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1178                                             GFP_KERNEL);
1179         if (!plane->format_types) {
1180                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1181                 drm_mode_object_put(dev, &plane->base);
1182                 return -ENOMEM;
1183         }
1184
1185         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1186         plane->format_count = format_count;
1187         plane->possible_crtcs = possible_crtcs;
1188         plane->type = type;
1189
1190         list_add_tail(&plane->head, &dev->mode_config.plane_list);
1191         dev->mode_config.num_total_plane++;
1192         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1193                 dev->mode_config.num_overlay_plane++;
1194
1195         drm_object_attach_property(&plane->base,
1196                                    dev->mode_config.plane_type_property,
1197                                    plane->type);
1198
1199         return 0;
1200 }
1201 EXPORT_SYMBOL(drm_universal_plane_init);
1202
1203 /**
1204  * drm_plane_init - Initialize a legacy plane
1205  * @dev: DRM device
1206  * @plane: plane object to init
1207  * @possible_crtcs: bitmask of possible CRTCs
1208  * @funcs: callbacks for the new plane
1209  * @formats: array of supported formats (%DRM_FORMAT_*)
1210  * @format_count: number of elements in @formats
1211  * @is_primary: plane type (primary vs overlay)
1212  *
1213  * Legacy API to initialize a DRM plane.
1214  *
1215  * New drivers should call drm_universal_plane_init() instead.
1216  *
1217  * Returns:
1218  * Zero on success, error code on failure.
1219  */
1220 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1221                    unsigned long possible_crtcs,
1222                    const struct drm_plane_funcs *funcs,
1223                    const uint32_t *formats, uint32_t format_count,
1224                    bool is_primary)
1225 {
1226         enum drm_plane_type type;
1227
1228         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1229         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1230                                         formats, format_count, type);
1231 }
1232 EXPORT_SYMBOL(drm_plane_init);
1233
1234 /**
1235  * drm_plane_cleanup - Clean up the core plane usage
1236  * @plane: plane to cleanup
1237  *
1238  * This function cleans up @plane and removes it from the DRM mode setting
1239  * core. Note that the function does *not* free the plane structure itself,
1240  * this is the responsibility of the caller.
1241  */
1242 void drm_plane_cleanup(struct drm_plane *plane)
1243 {
1244         struct drm_device *dev = plane->dev;
1245
1246         drm_modeset_lock_all(dev);
1247         kfree(plane->format_types);
1248         drm_mode_object_put(dev, &plane->base);
1249
1250         BUG_ON(list_empty(&plane->head));
1251
1252         list_del(&plane->head);
1253         dev->mode_config.num_total_plane--;
1254         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1255                 dev->mode_config.num_overlay_plane--;
1256         drm_modeset_unlock_all(dev);
1257
1258         WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1259         if (plane->state && plane->funcs->atomic_destroy_state)
1260                 plane->funcs->atomic_destroy_state(plane, plane->state);
1261
1262         memset(plane, 0, sizeof(*plane));
1263 }
1264 EXPORT_SYMBOL(drm_plane_cleanup);
1265
1266 /**
1267  * drm_plane_index - find the index of a registered plane
1268  * @plane: plane to find index for
1269  *
1270  * Given a registered plane, return the index of that CRTC within a DRM
1271  * device's list of planes.
1272  */
1273 unsigned int drm_plane_index(struct drm_plane *plane)
1274 {
1275         unsigned int index = 0;
1276         struct drm_plane *tmp;
1277
1278         list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1279                 if (tmp == plane)
1280                         return index;
1281
1282                 index++;
1283         }
1284
1285         BUG();
1286 }
1287 EXPORT_SYMBOL(drm_plane_index);
1288
1289 /**
1290  * drm_plane_force_disable - Forcibly disable a plane
1291  * @plane: plane to disable
1292  *
1293  * Forces the plane to be disabled.
1294  *
1295  * Used when the plane's current framebuffer is destroyed,
1296  * and when restoring fbdev mode.
1297  */
1298 void drm_plane_force_disable(struct drm_plane *plane)
1299 {
1300         int ret;
1301
1302         if (!plane->fb)
1303                 return;
1304
1305         plane->old_fb = plane->fb;
1306         ret = plane->funcs->disable_plane(plane);
1307         if (ret) {
1308                 DRM_ERROR("failed to disable plane with busy fb\n");
1309                 plane->old_fb = NULL;
1310                 return;
1311         }
1312         /* disconnect the plane from the fb and crtc: */
1313         __drm_framebuffer_unreference(plane->old_fb);
1314         plane->old_fb = NULL;
1315         plane->fb = NULL;
1316         plane->crtc = NULL;
1317 }
1318 EXPORT_SYMBOL(drm_plane_force_disable);
1319
1320 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1321 {
1322         struct drm_property *edid;
1323         struct drm_property *dpms;
1324         struct drm_property *dev_path;
1325
1326         /*
1327          * Standard properties (apply to all connectors)
1328          */
1329         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1330                                    DRM_MODE_PROP_IMMUTABLE,
1331                                    "EDID", 0);
1332         dev->mode_config.edid_property = edid;
1333
1334         dpms = drm_property_create_enum(dev, 0,
1335                                    "DPMS", drm_dpms_enum_list,
1336                                    ARRAY_SIZE(drm_dpms_enum_list));
1337         dev->mode_config.dpms_property = dpms;
1338
1339         dev_path = drm_property_create(dev,
1340                                        DRM_MODE_PROP_BLOB |
1341                                        DRM_MODE_PROP_IMMUTABLE,
1342                                        "PATH", 0);
1343         dev->mode_config.path_property = dev_path;
1344
1345         return 0;
1346 }
1347
1348 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1349 {
1350         struct drm_property *type;
1351
1352         /*
1353          * Standard properties (apply to all planes)
1354          */
1355         type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1356                                         "type", drm_plane_type_enum_list,
1357                                         ARRAY_SIZE(drm_plane_type_enum_list));
1358         dev->mode_config.plane_type_property = type;
1359
1360         return 0;
1361 }
1362
1363 /**
1364  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1365  * @dev: DRM device
1366  *
1367  * Called by a driver the first time a DVI-I connector is made.
1368  */
1369 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1370 {
1371         struct drm_property *dvi_i_selector;
1372         struct drm_property *dvi_i_subconnector;
1373
1374         if (dev->mode_config.dvi_i_select_subconnector_property)
1375                 return 0;
1376
1377         dvi_i_selector =
1378                 drm_property_create_enum(dev, 0,
1379                                     "select subconnector",
1380                                     drm_dvi_i_select_enum_list,
1381                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
1382         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1383
1384         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1385                                     "subconnector",
1386                                     drm_dvi_i_subconnector_enum_list,
1387                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1388         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1389
1390         return 0;
1391 }
1392 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1393
1394 /**
1395  * drm_create_tv_properties - create TV specific connector properties
1396  * @dev: DRM device
1397  * @num_modes: number of different TV formats (modes) supported
1398  * @modes: array of pointers to strings containing name of each format
1399  *
1400  * Called by a driver's TV initialization routine, this function creates
1401  * the TV specific connector properties for a given device.  Caller is
1402  * responsible for allocating a list of format names and passing them to
1403  * this routine.
1404  */
1405 int drm_mode_create_tv_properties(struct drm_device *dev,
1406                                   unsigned int num_modes,
1407                                   char *modes[])
1408 {
1409         struct drm_property *tv_selector;
1410         struct drm_property *tv_subconnector;
1411         unsigned int i;
1412
1413         if (dev->mode_config.tv_select_subconnector_property)
1414                 return 0;
1415
1416         /*
1417          * Basic connector properties
1418          */
1419         tv_selector = drm_property_create_enum(dev, 0,
1420                                           "select subconnector",
1421                                           drm_tv_select_enum_list,
1422                                           ARRAY_SIZE(drm_tv_select_enum_list));
1423         dev->mode_config.tv_select_subconnector_property = tv_selector;
1424
1425         tv_subconnector =
1426                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1427                                     "subconnector",
1428                                     drm_tv_subconnector_enum_list,
1429                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1430         dev->mode_config.tv_subconnector_property = tv_subconnector;
1431
1432         /*
1433          * Other, TV specific properties: margins & TV modes.
1434          */
1435         dev->mode_config.tv_left_margin_property =
1436                 drm_property_create_range(dev, 0, "left margin", 0, 100);
1437
1438         dev->mode_config.tv_right_margin_property =
1439                 drm_property_create_range(dev, 0, "right margin", 0, 100);
1440
1441         dev->mode_config.tv_top_margin_property =
1442                 drm_property_create_range(dev, 0, "top margin", 0, 100);
1443
1444         dev->mode_config.tv_bottom_margin_property =
1445                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1446
1447         dev->mode_config.tv_mode_property =
1448                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1449                                     "mode", num_modes);
1450         for (i = 0; i < num_modes; i++)
1451                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1452                                       i, modes[i]);
1453
1454         dev->mode_config.tv_brightness_property =
1455                 drm_property_create_range(dev, 0, "brightness", 0, 100);
1456
1457         dev->mode_config.tv_contrast_property =
1458                 drm_property_create_range(dev, 0, "contrast", 0, 100);
1459
1460         dev->mode_config.tv_flicker_reduction_property =
1461                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1462
1463         dev->mode_config.tv_overscan_property =
1464                 drm_property_create_range(dev, 0, "overscan", 0, 100);
1465
1466         dev->mode_config.tv_saturation_property =
1467                 drm_property_create_range(dev, 0, "saturation", 0, 100);
1468
1469         dev->mode_config.tv_hue_property =
1470                 drm_property_create_range(dev, 0, "hue", 0, 100);
1471
1472         return 0;
1473 }
1474 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1475
1476 /**
1477  * drm_mode_create_scaling_mode_property - create scaling mode property
1478  * @dev: DRM device
1479  *
1480  * Called by a driver the first time it's needed, must be attached to desired
1481  * connectors.
1482  */
1483 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1484 {
1485         struct drm_property *scaling_mode;
1486
1487         if (dev->mode_config.scaling_mode_property)
1488                 return 0;
1489
1490         scaling_mode =
1491                 drm_property_create_enum(dev, 0, "scaling mode",
1492                                 drm_scaling_mode_enum_list,
1493                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1494
1495         dev->mode_config.scaling_mode_property = scaling_mode;
1496
1497         return 0;
1498 }
1499 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1500
1501 /**
1502  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1503  * @dev: DRM device
1504  *
1505  * Called by a driver the first time it's needed, must be attached to desired
1506  * connectors.
1507  *
1508  * Returns:
1509  * Zero on success, negative errno on failure.
1510  */
1511 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1512 {
1513         if (dev->mode_config.aspect_ratio_property)
1514                 return 0;
1515
1516         dev->mode_config.aspect_ratio_property =
1517                 drm_property_create_enum(dev, 0, "aspect ratio",
1518                                 drm_aspect_ratio_enum_list,
1519                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1520
1521         if (dev->mode_config.aspect_ratio_property == NULL)
1522                 return -ENOMEM;
1523
1524         return 0;
1525 }
1526 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1527
1528 /**
1529  * drm_mode_create_dirty_property - create dirty property
1530  * @dev: DRM device
1531  *
1532  * Called by a driver the first time it's needed, must be attached to desired
1533  * connectors.
1534  */
1535 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1536 {
1537         struct drm_property *dirty_info;
1538
1539         if (dev->mode_config.dirty_info_property)
1540                 return 0;
1541
1542         dirty_info =
1543                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1544                                     "dirty",
1545                                     drm_dirty_info_enum_list,
1546                                     ARRAY_SIZE(drm_dirty_info_enum_list));
1547         dev->mode_config.dirty_info_property = dirty_info;
1548
1549         return 0;
1550 }
1551 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1552
1553 /**
1554  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1555  * @dev: DRM device
1556  *
1557  * Create the the suggested x/y offset property for connectors.
1558  */
1559 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1560 {
1561         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1562                 return 0;
1563
1564         dev->mode_config.suggested_x_property =
1565                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1566
1567         dev->mode_config.suggested_y_property =
1568                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1569
1570         if (dev->mode_config.suggested_x_property == NULL ||
1571             dev->mode_config.suggested_y_property == NULL)
1572                 return -ENOMEM;
1573         return 0;
1574 }
1575 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1576
1577 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1578 {
1579         uint32_t total_objects = 0;
1580
1581         total_objects += dev->mode_config.num_crtc;
1582         total_objects += dev->mode_config.num_connector;
1583         total_objects += dev->mode_config.num_encoder;
1584         total_objects += dev->mode_config.num_bridge;
1585
1586         group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
1587         if (!group->id_list)
1588                 return -ENOMEM;
1589
1590         group->num_crtcs = 0;
1591         group->num_connectors = 0;
1592         group->num_encoders = 0;
1593         group->num_bridges = 0;
1594         return 0;
1595 }
1596
1597 void drm_mode_group_destroy(struct drm_mode_group *group)
1598 {
1599         kfree(group->id_list);
1600         group->id_list = NULL;
1601 }
1602
1603 /*
1604  * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1605  * the drm core's responsibility to set up mode control groups.
1606  */
1607 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1608                                      struct drm_mode_group *group)
1609 {
1610         struct drm_crtc *crtc;
1611         struct drm_encoder *encoder;
1612         struct drm_connector *connector;
1613         struct drm_bridge *bridge;
1614         int ret;
1615
1616         ret = drm_mode_group_init(dev, group);
1617         if (ret)
1618                 return ret;
1619
1620         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1621                 group->id_list[group->num_crtcs++] = crtc->base.id;
1622
1623         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1624                 group->id_list[group->num_crtcs + group->num_encoders++] =
1625                 encoder->base.id;
1626
1627         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1628                 group->id_list[group->num_crtcs + group->num_encoders +
1629                                group->num_connectors++] = connector->base.id;
1630
1631         list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1632                 group->id_list[group->num_crtcs + group->num_encoders +
1633                                group->num_connectors + group->num_bridges++] =
1634                                         bridge->base.id;
1635
1636         return 0;
1637 }
1638 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1639
1640 void drm_reinit_primary_mode_group(struct drm_device *dev)
1641 {
1642         drm_modeset_lock_all(dev);
1643         drm_mode_group_destroy(&dev->primary->mode_group);
1644         drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1645         drm_modeset_unlock_all(dev);
1646 }
1647 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1648
1649 /**
1650  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1651  * @out: drm_mode_modeinfo struct to return to the user
1652  * @in: drm_display_mode to use
1653  *
1654  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1655  * the user.
1656  */
1657 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1658                                       const struct drm_display_mode *in)
1659 {
1660         WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1661              in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1662              in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1663              in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1664              in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1665              "timing values too large for mode info\n");
1666
1667         out->clock = in->clock;
1668         out->hdisplay = in->hdisplay;
1669         out->hsync_start = in->hsync_start;
1670         out->hsync_end = in->hsync_end;
1671         out->htotal = in->htotal;
1672         out->hskew = in->hskew;
1673         out->vdisplay = in->vdisplay;
1674         out->vsync_start = in->vsync_start;
1675         out->vsync_end = in->vsync_end;
1676         out->vtotal = in->vtotal;
1677         out->vscan = in->vscan;
1678         out->vrefresh = in->vrefresh;
1679         out->flags = in->flags;
1680         out->type = in->type;
1681         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1682         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1683 }
1684
1685 /**
1686  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1687  * @out: drm_display_mode to return to the user
1688  * @in: drm_mode_modeinfo to use
1689  *
1690  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1691  * the caller.
1692  *
1693  * Returns:
1694  * Zero on success, negative errno on failure.
1695  */
1696 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1697                                   const struct drm_mode_modeinfo *in)
1698 {
1699         if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1700                 return -ERANGE;
1701
1702         if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1703                 return -EINVAL;
1704
1705         out->clock = in->clock;
1706         out->hdisplay = in->hdisplay;
1707         out->hsync_start = in->hsync_start;
1708         out->hsync_end = in->hsync_end;
1709         out->htotal = in->htotal;
1710         out->hskew = in->hskew;
1711         out->vdisplay = in->vdisplay;
1712         out->vsync_start = in->vsync_start;
1713         out->vsync_end = in->vsync_end;
1714         out->vtotal = in->vtotal;
1715         out->vscan = in->vscan;
1716         out->vrefresh = in->vrefresh;
1717         out->flags = in->flags;
1718         out->type = in->type;
1719         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1720         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1721
1722         return 0;
1723 }
1724
1725 /**
1726  * drm_mode_getresources - get graphics configuration
1727  * @dev: drm device for the ioctl
1728  * @data: data pointer for the ioctl
1729  * @file_priv: drm file for the ioctl call
1730  *
1731  * Construct a set of configuration description structures and return
1732  * them to the user, including CRTC, connector and framebuffer configuration.
1733  *
1734  * Called by the user via ioctl.
1735  *
1736  * Returns:
1737  * Zero on success, negative errno on failure.
1738  */
1739 int drm_mode_getresources(struct drm_device *dev, void *data,
1740                           struct drm_file *file_priv)
1741 {
1742         struct drm_mode_card_res *card_res = data;
1743         struct list_head *lh;
1744         struct drm_framebuffer *fb;
1745         struct drm_connector *connector;
1746         struct drm_crtc *crtc;
1747         struct drm_encoder *encoder;
1748         int ret = 0;
1749         int connector_count = 0;
1750         int crtc_count = 0;
1751         int fb_count = 0;
1752         int encoder_count = 0;
1753         int copied = 0, i;
1754         uint32_t __user *fb_id;
1755         uint32_t __user *crtc_id;
1756         uint32_t __user *connector_id;
1757         uint32_t __user *encoder_id;
1758         struct drm_mode_group *mode_group;
1759
1760         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761                 return -EINVAL;
1762
1763
1764         mutex_lock(&file_priv->fbs_lock);
1765         /*
1766          * For the non-control nodes we need to limit the list of resources
1767          * by IDs in the group list for this node
1768          */
1769         list_for_each(lh, &file_priv->fbs)
1770                 fb_count++;
1771
1772         /* handle this in 4 parts */
1773         /* FBs */
1774         if (card_res->count_fbs >= fb_count) {
1775                 copied = 0;
1776                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1777                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1778                         if (put_user(fb->base.id, fb_id + copied)) {
1779                                 mutex_unlock(&file_priv->fbs_lock);
1780                                 return -EFAULT;
1781                         }
1782                         copied++;
1783                 }
1784         }
1785         card_res->count_fbs = fb_count;
1786         mutex_unlock(&file_priv->fbs_lock);
1787
1788         /* mode_config.mutex protects the connector list against e.g. DP MST
1789          * connector hot-adding. CRTC/Plane lists are invariant. */
1790         mutex_lock(&dev->mode_config.mutex);
1791         if (!drm_is_primary_client(file_priv)) {
1792
1793                 mode_group = NULL;
1794                 list_for_each(lh, &dev->mode_config.crtc_list)
1795                         crtc_count++;
1796
1797                 list_for_each(lh, &dev->mode_config.connector_list)
1798                         connector_count++;
1799
1800                 list_for_each(lh, &dev->mode_config.encoder_list)
1801                         encoder_count++;
1802         } else {
1803
1804                 mode_group = &file_priv->master->minor->mode_group;
1805                 crtc_count = mode_group->num_crtcs;
1806                 connector_count = mode_group->num_connectors;
1807                 encoder_count = mode_group->num_encoders;
1808         }
1809
1810         card_res->max_height = dev->mode_config.max_height;
1811         card_res->min_height = dev->mode_config.min_height;
1812         card_res->max_width = dev->mode_config.max_width;
1813         card_res->min_width = dev->mode_config.min_width;
1814
1815         /* CRTCs */
1816         if (card_res->count_crtcs >= crtc_count) {
1817                 copied = 0;
1818                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1819                 if (!mode_group) {
1820                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1821                                             head) {
1822                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1823                                 if (put_user(crtc->base.id, crtc_id + copied)) {
1824                                         ret = -EFAULT;
1825                                         goto out;
1826                                 }
1827                                 copied++;
1828                         }
1829                 } else {
1830                         for (i = 0; i < mode_group->num_crtcs; i++) {
1831                                 if (put_user(mode_group->id_list[i],
1832                                              crtc_id + copied)) {
1833                                         ret = -EFAULT;
1834                                         goto out;
1835                                 }
1836                                 copied++;
1837                         }
1838                 }
1839         }
1840         card_res->count_crtcs = crtc_count;
1841
1842         /* Encoders */
1843         if (card_res->count_encoders >= encoder_count) {
1844                 copied = 0;
1845                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1846                 if (!mode_group) {
1847                         list_for_each_entry(encoder,
1848                                             &dev->mode_config.encoder_list,
1849                                             head) {
1850                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1851                                                 encoder->name);
1852                                 if (put_user(encoder->base.id, encoder_id +
1853                                              copied)) {
1854                                         ret = -EFAULT;
1855                                         goto out;
1856                                 }
1857                                 copied++;
1858                         }
1859                 } else {
1860                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1861                                 if (put_user(mode_group->id_list[i],
1862                                              encoder_id + copied)) {
1863                                         ret = -EFAULT;
1864                                         goto out;
1865                                 }
1866                                 copied++;
1867                         }
1868
1869                 }
1870         }
1871         card_res->count_encoders = encoder_count;
1872
1873         /* Connectors */
1874         if (card_res->count_connectors >= connector_count) {
1875                 copied = 0;
1876                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1877                 if (!mode_group) {
1878                         list_for_each_entry(connector,
1879                                             &dev->mode_config.connector_list,
1880                                             head) {
1881                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1882                                         connector->base.id,
1883                                         connector->name);
1884                                 if (put_user(connector->base.id,
1885                                              connector_id + copied)) {
1886                                         ret = -EFAULT;
1887                                         goto out;
1888                                 }
1889                                 copied++;
1890                         }
1891                 } else {
1892                         int start = mode_group->num_crtcs +
1893                                 mode_group->num_encoders;
1894                         for (i = start; i < start + mode_group->num_connectors; i++) {
1895                                 if (put_user(mode_group->id_list[i],
1896                                              connector_id + copied)) {
1897                                         ret = -EFAULT;
1898                                         goto out;
1899                                 }
1900                                 copied++;
1901                         }
1902                 }
1903         }
1904         card_res->count_connectors = connector_count;
1905
1906         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1907                   card_res->count_connectors, card_res->count_encoders);
1908
1909 out:
1910         mutex_unlock(&dev->mode_config.mutex);
1911         return ret;
1912 }
1913
1914 /**
1915  * drm_mode_getcrtc - get CRTC configuration
1916  * @dev: drm device for the ioctl
1917  * @data: data pointer for the ioctl
1918  * @file_priv: drm file for the ioctl call
1919  *
1920  * Construct a CRTC configuration structure to return to the user.
1921  *
1922  * Called by the user via ioctl.
1923  *
1924  * Returns:
1925  * Zero on success, negative errno on failure.
1926  */
1927 int drm_mode_getcrtc(struct drm_device *dev,
1928                      void *data, struct drm_file *file_priv)
1929 {
1930         struct drm_mode_crtc *crtc_resp = data;
1931         struct drm_crtc *crtc;
1932
1933         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1934                 return -EINVAL;
1935
1936         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1937         if (!crtc)
1938                 return -ENOENT;
1939
1940         drm_modeset_lock_crtc(crtc, crtc->primary);
1941         crtc_resp->x = crtc->x;
1942         crtc_resp->y = crtc->y;
1943         crtc_resp->gamma_size = crtc->gamma_size;
1944         if (crtc->primary->fb)
1945                 crtc_resp->fb_id = crtc->primary->fb->base.id;
1946         else
1947                 crtc_resp->fb_id = 0;
1948
1949         if (crtc->enabled) {
1950
1951                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1952                 crtc_resp->mode_valid = 1;
1953
1954         } else {
1955                 crtc_resp->mode_valid = 0;
1956         }
1957         drm_modeset_unlock_crtc(crtc);
1958
1959         return 0;
1960 }
1961
1962 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1963                                          const struct drm_file *file_priv)
1964 {
1965         /*
1966          * If user-space hasn't configured the driver to expose the stereo 3D
1967          * modes, don't expose them.
1968          */
1969         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1970                 return false;
1971
1972         return true;
1973 }
1974
1975 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1976 {
1977         /* For atomic drivers only state objects are synchronously updated and
1978          * protected by modeset locks, so check those first. */
1979         if (connector->state)
1980                 return connector->state->best_encoder;
1981         return connector->encoder;
1982 }
1983
1984 /**
1985  * drm_mode_getconnector - get connector configuration
1986  * @dev: drm device for the ioctl
1987  * @data: data pointer for the ioctl
1988  * @file_priv: drm file for the ioctl call
1989  *
1990  * Construct a connector configuration structure to return to the user.
1991  *
1992  * Called by the user via ioctl.
1993  *
1994  * Returns:
1995  * Zero on success, negative errno on failure.
1996  */
1997 int drm_mode_getconnector(struct drm_device *dev, void *data,
1998                           struct drm_file *file_priv)
1999 {
2000         struct drm_mode_get_connector *out_resp = data;
2001         struct drm_connector *connector;
2002         struct drm_encoder *encoder;
2003         struct drm_display_mode *mode;
2004         int mode_count = 0;
2005         int props_count = 0;
2006         int encoders_count = 0;
2007         int ret = 0;
2008         int copied = 0;
2009         int i;
2010         struct drm_mode_modeinfo u_mode;
2011         struct drm_mode_modeinfo __user *mode_ptr;
2012         uint32_t __user *prop_ptr;
2013         uint64_t __user *prop_values;
2014         uint32_t __user *encoder_ptr;
2015
2016         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2017                 return -EINVAL;
2018
2019         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2020
2021         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2022
2023         mutex_lock(&dev->mode_config.mutex);
2024
2025         connector = drm_connector_find(dev, out_resp->connector_id);
2026         if (!connector) {
2027                 ret = -ENOENT;
2028                 goto out;
2029         }
2030
2031         props_count = connector->properties.count;
2032
2033         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2034                 if (connector->encoder_ids[i] != 0)
2035                         encoders_count++;
2036
2037         if (out_resp->count_modes == 0) {
2038                 connector->funcs->fill_modes(connector,
2039                                              dev->mode_config.max_width,
2040                                              dev->mode_config.max_height);
2041         }
2042
2043         /* delayed so we get modes regardless of pre-fill_modes state */
2044         list_for_each_entry(mode, &connector->modes, head)
2045                 if (drm_mode_expose_to_userspace(mode, file_priv))
2046                         mode_count++;
2047
2048         out_resp->connector_id = connector->base.id;
2049         out_resp->connector_type = connector->connector_type;
2050         out_resp->connector_type_id = connector->connector_type_id;
2051         out_resp->mm_width = connector->display_info.width_mm;
2052         out_resp->mm_height = connector->display_info.height_mm;
2053         out_resp->subpixel = connector->display_info.subpixel_order;
2054         out_resp->connection = connector->status;
2055         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2056
2057         encoder = drm_connector_get_encoder(connector);
2058         if (encoder)
2059                 out_resp->encoder_id = encoder->base.id;
2060         else
2061                 out_resp->encoder_id = 0;
2062         drm_modeset_unlock(&dev->mode_config.connection_mutex);
2063
2064         /*
2065          * This ioctl is called twice, once to determine how much space is
2066          * needed, and the 2nd time to fill it.
2067          */
2068         if ((out_resp->count_modes >= mode_count) && mode_count) {
2069                 copied = 0;
2070                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2071                 list_for_each_entry(mode, &connector->modes, head) {
2072                         if (!drm_mode_expose_to_userspace(mode, file_priv))
2073                                 continue;
2074
2075                         drm_crtc_convert_to_umode(&u_mode, mode);
2076                         if (copy_to_user(mode_ptr + copied,
2077                                          &u_mode, sizeof(u_mode))) {
2078                                 ret = -EFAULT;
2079                                 goto out;
2080                         }
2081                         copied++;
2082                 }
2083         }
2084         out_resp->count_modes = mode_count;
2085
2086         if ((out_resp->count_props >= props_count) && props_count) {
2087                 copied = 0;
2088                 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2089                 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
2090                 for (i = 0; i < connector->properties.count; i++) {
2091                         if (put_user(connector->properties.ids[i],
2092                                      prop_ptr + copied)) {
2093                                 ret = -EFAULT;
2094                                 goto out;
2095                         }
2096
2097                         if (put_user(connector->properties.values[i],
2098                                      prop_values + copied)) {
2099                                 ret = -EFAULT;
2100                                 goto out;
2101                         }
2102                         copied++;
2103                 }
2104         }
2105         out_resp->count_props = props_count;
2106
2107         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2108                 copied = 0;
2109                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2110                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2111                         if (connector->encoder_ids[i] != 0) {
2112                                 if (put_user(connector->encoder_ids[i],
2113                                              encoder_ptr + copied)) {
2114                                         ret = -EFAULT;
2115                                         goto out;
2116                                 }
2117                                 copied++;
2118                         }
2119                 }
2120         }
2121         out_resp->count_encoders = encoders_count;
2122
2123 out:
2124         mutex_unlock(&dev->mode_config.mutex);
2125
2126         return ret;
2127 }
2128
2129 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2130 {
2131         struct drm_connector *connector;
2132         struct drm_device *dev = encoder->dev;
2133         bool uses_atomic = false;
2134
2135         /* For atomic drivers only state objects are synchronously updated and
2136          * protected by modeset locks, so check those first. */
2137         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2138                 if (!connector->state)
2139                         continue;
2140
2141                 uses_atomic = true;
2142
2143                 if (connector->state->best_encoder != encoder)
2144                         continue;
2145
2146                 return connector->state->crtc;
2147         }
2148
2149         /* Don't return stale data (e.g. pending async disable). */
2150         if (uses_atomic)
2151                 return NULL;
2152
2153         return encoder->crtc;
2154 }
2155
2156 /**
2157  * drm_mode_getencoder - get encoder configuration
2158  * @dev: drm device for the ioctl
2159  * @data: data pointer for the ioctl
2160  * @file_priv: drm file for the ioctl call
2161  *
2162  * Construct a encoder configuration structure to return to the user.
2163  *
2164  * Called by the user via ioctl.
2165  *
2166  * Returns:
2167  * Zero on success, negative errno on failure.
2168  */
2169 int drm_mode_getencoder(struct drm_device *dev, void *data,
2170                         struct drm_file *file_priv)
2171 {
2172         struct drm_mode_get_encoder *enc_resp = data;
2173         struct drm_encoder *encoder;
2174         struct drm_crtc *crtc;
2175
2176         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2177                 return -EINVAL;
2178
2179         encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2180         if (!encoder)
2181                 return -ENOENT;
2182
2183         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2184         crtc = drm_encoder_get_crtc(encoder);
2185         if (crtc)
2186                 enc_resp->crtc_id = crtc->base.id;
2187         else if (encoder->crtc)
2188                 enc_resp->crtc_id = encoder->crtc->base.id;
2189         else
2190                 enc_resp->crtc_id = 0;
2191         drm_modeset_unlock(&dev->mode_config.connection_mutex);
2192
2193         enc_resp->encoder_type = encoder->encoder_type;
2194         enc_resp->encoder_id = encoder->base.id;
2195         enc_resp->possible_crtcs = encoder->possible_crtcs;
2196         enc_resp->possible_clones = encoder->possible_clones;
2197
2198         return 0;
2199 }
2200
2201 /**
2202  * drm_mode_getplane_res - enumerate all plane resources
2203  * @dev: DRM device
2204  * @data: ioctl data
2205  * @file_priv: DRM file info
2206  *
2207  * Construct a list of plane ids to return to the user.
2208  *
2209  * Called by the user via ioctl.
2210  *
2211  * Returns:
2212  * Zero on success, negative errno on failure.
2213  */
2214 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2215                           struct drm_file *file_priv)
2216 {
2217         struct drm_mode_get_plane_res *plane_resp = data;
2218         struct drm_mode_config *config;
2219         struct drm_plane *plane;
2220         uint32_t __user *plane_ptr;
2221         int copied = 0;
2222         unsigned num_planes;
2223
2224         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2225                 return -EINVAL;
2226
2227         config = &dev->mode_config;
2228
2229         if (file_priv->universal_planes)
2230                 num_planes = config->num_total_plane;
2231         else
2232                 num_planes = config->num_overlay_plane;
2233
2234         /*
2235          * This ioctl is called twice, once to determine how much space is
2236          * needed, and the 2nd time to fill it.
2237          */
2238         if (num_planes &&
2239             (plane_resp->count_planes >= num_planes)) {
2240                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2241
2242                 /* Plane lists are invariant, no locking needed. */
2243                 list_for_each_entry(plane, &config->plane_list, head) {
2244                         /*
2245                          * Unless userspace set the 'universal planes'
2246                          * capability bit, only advertise overlays.
2247                          */
2248                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2249                             !file_priv->universal_planes)
2250                                 continue;
2251
2252                         if (put_user(plane->base.id, plane_ptr + copied))
2253                                 return -EFAULT;
2254                         copied++;
2255                 }
2256         }
2257         plane_resp->count_planes = num_planes;
2258
2259         return 0;
2260 }
2261
2262 /**
2263  * drm_mode_getplane - get plane configuration
2264  * @dev: DRM device
2265  * @data: ioctl data
2266  * @file_priv: DRM file info
2267  *
2268  * Construct a plane configuration structure to return to the user.
2269  *
2270  * Called by the user via ioctl.
2271  *
2272  * Returns:
2273  * Zero on success, negative errno on failure.
2274  */
2275 int drm_mode_getplane(struct drm_device *dev, void *data,
2276                       struct drm_file *file_priv)
2277 {
2278         struct drm_mode_get_plane *plane_resp = data;
2279         struct drm_plane *plane;
2280         uint32_t __user *format_ptr;
2281
2282         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283                 return -EINVAL;
2284
2285         plane = drm_plane_find(dev, plane_resp->plane_id);
2286         if (!plane)
2287                 return -ENOENT;
2288
2289         drm_modeset_lock(&plane->mutex, NULL);
2290         if (plane->crtc)
2291                 plane_resp->crtc_id = plane->crtc->base.id;
2292         else
2293                 plane_resp->crtc_id = 0;
2294
2295         if (plane->fb)
2296                 plane_resp->fb_id = plane->fb->base.id;
2297         else
2298                 plane_resp->fb_id = 0;
2299         drm_modeset_unlock(&plane->mutex);
2300
2301         plane_resp->plane_id = plane->base.id;
2302         plane_resp->possible_crtcs = plane->possible_crtcs;
2303         plane_resp->gamma_size = 0;
2304
2305         /*
2306          * This ioctl is called twice, once to determine how much space is
2307          * needed, and the 2nd time to fill it.
2308          */
2309         if (plane->format_count &&
2310             (plane_resp->count_format_types >= plane->format_count)) {
2311                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2312                 if (copy_to_user(format_ptr,
2313                                  plane->format_types,
2314                                  sizeof(uint32_t) * plane->format_count)) {
2315                         return -EFAULT;
2316                 }
2317         }
2318         plane_resp->count_format_types = plane->format_count;
2319
2320         return 0;
2321 }
2322
2323 /*
2324  * setplane_internal - setplane handler for internal callers
2325  *
2326  * Note that we assume an extra reference has already been taken on fb.  If the
2327  * update fails, this reference will be dropped before return; if it succeeds,
2328  * the previous framebuffer (if any) will be unreferenced instead.
2329  *
2330  * src_{x,y,w,h} are provided in 16.16 fixed point format
2331  */
2332 static int __setplane_internal(struct drm_plane *plane,
2333                                struct drm_crtc *crtc,
2334                                struct drm_framebuffer *fb,
2335                                int32_t crtc_x, int32_t crtc_y,
2336                                uint32_t crtc_w, uint32_t crtc_h,
2337                                /* src_{x,y,w,h} values are 16.16 fixed point */
2338                                uint32_t src_x, uint32_t src_y,
2339                                uint32_t src_w, uint32_t src_h)
2340 {
2341         int ret = 0;
2342         unsigned int fb_width, fb_height;
2343         unsigned int i;
2344
2345         /* No fb means shut it down */
2346         if (!fb) {
2347                 plane->old_fb = plane->fb;
2348                 ret = plane->funcs->disable_plane(plane);
2349                 if (!ret) {
2350                         plane->crtc = NULL;
2351                         plane->fb = NULL;
2352                 } else {
2353                         plane->old_fb = NULL;
2354                 }
2355                 goto out;
2356         }
2357
2358         /* Check whether this plane is usable on this CRTC */
2359         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2360                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2361                 ret = -EINVAL;
2362                 goto out;
2363         }
2364
2365         /* Check whether this plane supports the fb pixel format. */
2366         for (i = 0; i < plane->format_count; i++)
2367                 if (fb->pixel_format == plane->format_types[i])
2368                         break;
2369         if (i == plane->format_count) {
2370                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2371                               drm_get_format_name(fb->pixel_format));
2372                 ret = -EINVAL;
2373                 goto out;
2374         }
2375
2376         fb_width = fb->width << 16;
2377         fb_height = fb->height << 16;
2378
2379         /* Make sure source coordinates are inside the fb. */
2380         if (src_w > fb_width ||
2381             src_x > fb_width - src_w ||
2382             src_h > fb_height ||
2383             src_y > fb_height - src_h) {
2384                 DRM_DEBUG_KMS("Invalid source coordinates "
2385                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2386                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2387                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2388                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2389                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2390                 ret = -ENOSPC;
2391                 goto out;
2392         }
2393
2394         plane->old_fb = plane->fb;
2395         ret = plane->funcs->update_plane(plane, crtc, fb,
2396                                          crtc_x, crtc_y, crtc_w, crtc_h,
2397                                          src_x, src_y, src_w, src_h);
2398         if (!ret) {
2399                 plane->crtc = crtc;
2400                 plane->fb = fb;
2401                 fb = NULL;
2402         } else {
2403                 plane->old_fb = NULL;
2404         }
2405
2406 out:
2407         if (fb)
2408                 drm_framebuffer_unreference(fb);
2409         if (plane->old_fb)
2410                 drm_framebuffer_unreference(plane->old_fb);
2411         plane->old_fb = NULL;
2412
2413         return ret;
2414 }
2415
2416 static int setplane_internal(struct drm_plane *plane,
2417                              struct drm_crtc *crtc,
2418                              struct drm_framebuffer *fb,
2419                              int32_t crtc_x, int32_t crtc_y,
2420                              uint32_t crtc_w, uint32_t crtc_h,
2421                              /* src_{x,y,w,h} values are 16.16 fixed point */
2422                              uint32_t src_x, uint32_t src_y,
2423                              uint32_t src_w, uint32_t src_h)
2424 {
2425         int ret;
2426
2427         drm_modeset_lock_all(plane->dev);
2428         ret = __setplane_internal(plane, crtc, fb,
2429                                   crtc_x, crtc_y, crtc_w, crtc_h,
2430                                   src_x, src_y, src_w, src_h);
2431         drm_modeset_unlock_all(plane->dev);
2432
2433         return ret;
2434 }
2435
2436 /**
2437  * drm_mode_setplane - configure a plane's configuration
2438  * @dev: DRM device
2439  * @data: ioctl data*
2440  * @file_priv: DRM file info
2441  *
2442  * Set plane configuration, including placement, fb, scaling, and other factors.
2443  * Or pass a NULL fb to disable (planes may be disabled without providing a
2444  * valid crtc).
2445  *
2446  * Returns:
2447  * Zero on success, negative errno on failure.
2448  */
2449 int drm_mode_setplane(struct drm_device *dev, void *data,
2450                       struct drm_file *file_priv)
2451 {
2452         struct drm_mode_set_plane *plane_req = data;
2453         struct drm_plane *plane;
2454         struct drm_crtc *crtc = NULL;
2455         struct drm_framebuffer *fb = NULL;
2456
2457         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2458                 return -EINVAL;
2459
2460         /* Give drivers some help against integer overflows */
2461         if (plane_req->crtc_w > INT_MAX ||
2462             plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2463             plane_req->crtc_h > INT_MAX ||
2464             plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2465                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2466                               plane_req->crtc_w, plane_req->crtc_h,
2467                               plane_req->crtc_x, plane_req->crtc_y);
2468                 return -ERANGE;
2469         }
2470
2471         /*
2472          * First, find the plane, crtc, and fb objects.  If not available,
2473          * we don't bother to call the driver.
2474          */
2475         plane = drm_plane_find(dev, plane_req->plane_id);
2476         if (!plane) {
2477                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2478                               plane_req->plane_id);
2479                 return -ENOENT;
2480         }
2481
2482         if (plane_req->fb_id) {
2483                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2484                 if (!fb) {
2485                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2486                                       plane_req->fb_id);
2487                         return -ENOENT;
2488                 }
2489
2490                 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2491                 if (!crtc) {
2492                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2493                                       plane_req->crtc_id);
2494                         return -ENOENT;
2495                 }
2496         }
2497
2498         /*
2499          * setplane_internal will take care of deref'ing either the old or new
2500          * framebuffer depending on success.
2501          */
2502         return setplane_internal(plane, crtc, fb,
2503                                  plane_req->crtc_x, plane_req->crtc_y,
2504                                  plane_req->crtc_w, plane_req->crtc_h,
2505                                  plane_req->src_x, plane_req->src_y,
2506                                  plane_req->src_w, plane_req->src_h);
2507 }
2508
2509 /**
2510  * drm_mode_set_config_internal - helper to call ->set_config
2511  * @set: modeset config to set
2512  *
2513  * This is a little helper to wrap internal calls to the ->set_config driver
2514  * interface. The only thing it adds is correct refcounting dance.
2515  *
2516  * Returns:
2517  * Zero on success, negative errno on failure.
2518  */
2519 int drm_mode_set_config_internal(struct drm_mode_set *set)
2520 {
2521         struct drm_crtc *crtc = set->crtc;
2522         struct drm_framebuffer *fb;
2523         struct drm_crtc *tmp;
2524         int ret;
2525
2526         /*
2527          * NOTE: ->set_config can also disable other crtcs (if we steal all
2528          * connectors from it), hence we need to refcount the fbs across all
2529          * crtcs. Atomic modeset will have saner semantics ...
2530          */
2531         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2532                 tmp->primary->old_fb = tmp->primary->fb;
2533
2534         fb = set->fb;
2535
2536         ret = crtc->funcs->set_config(set);
2537         if (ret == 0) {
2538                 crtc->primary->crtc = crtc;
2539                 crtc->primary->fb = fb;
2540         }
2541
2542         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2543                 if (tmp->primary->fb)
2544                         drm_framebuffer_reference(tmp->primary->fb);
2545                 if (tmp->primary->old_fb)
2546                         drm_framebuffer_unreference(tmp->primary->old_fb);
2547                 tmp->primary->old_fb = NULL;
2548         }
2549
2550         return ret;
2551 }
2552 EXPORT_SYMBOL(drm_mode_set_config_internal);
2553
2554 /**
2555  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2556  *     CRTC viewport
2557  * @crtc: CRTC that framebuffer will be displayed on
2558  * @x: x panning
2559  * @y: y panning
2560  * @mode: mode that framebuffer will be displayed under
2561  * @fb: framebuffer to check size of
2562  */
2563 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2564                             int x, int y,
2565                             const struct drm_display_mode *mode,
2566                             const struct drm_framebuffer *fb)
2567
2568 {
2569         int hdisplay, vdisplay;
2570
2571         hdisplay = mode->hdisplay;
2572         vdisplay = mode->vdisplay;
2573
2574         if (drm_mode_is_stereo(mode)) {
2575                 struct drm_display_mode adjusted = *mode;
2576
2577                 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2578                 hdisplay = adjusted.crtc_hdisplay;
2579                 vdisplay = adjusted.crtc_vdisplay;
2580         }
2581
2582         if (crtc->invert_dimensions)
2583                 swap(hdisplay, vdisplay);
2584
2585         if (hdisplay > fb->width ||
2586             vdisplay > fb->height ||
2587             x > fb->width - hdisplay ||
2588             y > fb->height - vdisplay) {
2589                 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2590                               fb->width, fb->height, hdisplay, vdisplay, x, y,
2591                               crtc->invert_dimensions ? " (inverted)" : "");
2592                 return -ENOSPC;
2593         }
2594
2595         return 0;
2596 }
2597 EXPORT_SYMBOL(drm_crtc_check_viewport);
2598
2599 /**
2600  * drm_mode_setcrtc - set CRTC configuration
2601  * @dev: drm device for the ioctl
2602  * @data: data pointer for the ioctl
2603  * @file_priv: drm file for the ioctl call
2604  *
2605  * Build a new CRTC configuration based on user request.
2606  *
2607  * Called by the user via ioctl.
2608  *
2609  * Returns:
2610  * Zero on success, negative errno on failure.
2611  */
2612 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2613                      struct drm_file *file_priv)
2614 {
2615         struct drm_mode_config *config = &dev->mode_config;
2616         struct drm_mode_crtc *crtc_req = data;
2617         struct drm_crtc *crtc;
2618         struct drm_connector **connector_set = NULL, *connector;
2619         struct drm_framebuffer *fb = NULL;
2620         struct drm_display_mode *mode = NULL;
2621         struct drm_mode_set set;
2622         uint32_t __user *set_connectors_ptr;
2623         int ret;
2624         int i;
2625
2626         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2627                 return -EINVAL;
2628
2629         /* For some reason crtc x/y offsets are signed internally. */
2630         if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2631                 return -ERANGE;
2632
2633         drm_modeset_lock_all(dev);
2634         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2635         if (!crtc) {
2636                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2637                 ret = -ENOENT;
2638                 goto out;
2639         }
2640         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2641
2642         if (crtc_req->mode_valid) {
2643                 /* If we have a mode we need a framebuffer. */
2644                 /* If we pass -1, set the mode with the currently bound fb */
2645                 if (crtc_req->fb_id == -1) {
2646                         if (!crtc->primary->fb) {
2647                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2648                                 ret = -EINVAL;
2649                                 goto out;
2650                         }
2651                         fb = crtc->primary->fb;
2652                         /* Make refcounting symmetric with the lookup path. */
2653                         drm_framebuffer_reference(fb);
2654                 } else {
2655                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2656                         if (!fb) {
2657                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2658                                                 crtc_req->fb_id);
2659                                 ret = -ENOENT;
2660                                 goto out;
2661                         }
2662                 }
2663
2664                 mode = drm_mode_create(dev);
2665                 if (!mode) {
2666                         ret = -ENOMEM;
2667                         goto out;
2668                 }
2669
2670                 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2671                 if (ret) {
2672                         DRM_DEBUG_KMS("Invalid mode\n");
2673                         goto out;
2674                 }
2675
2676                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2677
2678                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2679                                               mode, fb);
2680                 if (ret)
2681                         goto out;
2682
2683         }
2684
2685         if (crtc_req->count_connectors == 0 && mode) {
2686                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2687                 ret = -EINVAL;
2688                 goto out;
2689         }
2690
2691         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2692                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2693                           crtc_req->count_connectors);
2694                 ret = -EINVAL;
2695                 goto out;
2696         }
2697
2698         if (crtc_req->count_connectors > 0) {
2699                 u32 out_id;
2700
2701                 /* Avoid unbounded kernel memory allocation */
2702                 if (crtc_req->count_connectors > config->num_connector) {
2703                         ret = -EINVAL;
2704                         goto out;
2705                 }
2706
2707                 connector_set = kmalloc_array(crtc_req->count_connectors,
2708                                               sizeof(struct drm_connector *),
2709                                               GFP_KERNEL);
2710                 if (!connector_set) {
2711                         ret = -ENOMEM;
2712                         goto out;
2713                 }
2714
2715                 for (i = 0; i < crtc_req->count_connectors; i++) {
2716                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2717                         if (get_user(out_id, &set_connectors_ptr[i])) {
2718                                 ret = -EFAULT;
2719                                 goto out;
2720                         }
2721
2722                         connector = drm_connector_find(dev, out_id);
2723                         if (!connector) {
2724                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
2725                                                 out_id);
2726                                 ret = -ENOENT;
2727                                 goto out;
2728                         }
2729                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2730                                         connector->base.id,
2731                                         connector->name);
2732
2733                         connector_set[i] = connector;
2734                 }
2735         }
2736
2737         set.crtc = crtc;
2738         set.x = crtc_req->x;
2739         set.y = crtc_req->y;
2740         set.mode = mode;
2741         set.connectors = connector_set;
2742         set.num_connectors = crtc_req->count_connectors;
2743         set.fb = fb;
2744         ret = drm_mode_set_config_internal(&set);
2745
2746 out:
2747         if (fb)
2748                 drm_framebuffer_unreference(fb);
2749
2750         kfree(connector_set);
2751         drm_mode_destroy(dev, mode);
2752         drm_modeset_unlock_all(dev);
2753         return ret;
2754 }
2755
2756 /**
2757  * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2758  *     universal plane handler call
2759  * @crtc: crtc to update cursor for
2760  * @req: data pointer for the ioctl
2761  * @file_priv: drm file for the ioctl call
2762  *
2763  * Legacy cursor ioctl's work directly with driver buffer handles.  To
2764  * translate legacy ioctl calls into universal plane handler calls, we need to
2765  * wrap the native buffer handle in a drm_framebuffer.
2766  *
2767  * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2768  * buffer with a pitch of 4*width; the universal plane interface should be used
2769  * directly in cases where the hardware can support other buffer settings and
2770  * userspace wants to make use of these capabilities.
2771  *
2772  * Returns:
2773  * Zero on success, negative errno on failure.
2774  */
2775 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2776                                      struct drm_mode_cursor2 *req,
2777                                      struct drm_file *file_priv)
2778 {
2779         struct drm_device *dev = crtc->dev;
2780         struct drm_framebuffer *fb = NULL;
2781         struct drm_mode_fb_cmd2 fbreq = {
2782                 .width = req->width,
2783                 .height = req->height,
2784                 .pixel_format = DRM_FORMAT_ARGB8888,
2785                 .pitches = { req->width * 4 },
2786                 .handles = { req->handle },
2787         };
2788         int32_t crtc_x, crtc_y;
2789         uint32_t crtc_w = 0, crtc_h = 0;
2790         uint32_t src_w = 0, src_h = 0;
2791         int ret = 0;
2792
2793         BUG_ON(!crtc->cursor);
2794         WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2795
2796         /*
2797          * Obtain fb we'll be using (either new or existing) and take an extra
2798          * reference to it if fb != null.  setplane will take care of dropping
2799          * the reference if the plane update fails.
2800          */
2801         if (req->flags & DRM_MODE_CURSOR_BO) {
2802                 if (req->handle) {
2803                         fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2804                         if (IS_ERR(fb)) {
2805                                 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2806                                 return PTR_ERR(fb);
2807                         }
2808
2809                         drm_framebuffer_reference(fb);
2810                 } else {
2811                         fb = NULL;
2812                 }
2813         } else {
2814                 fb = crtc->cursor->fb;
2815                 if (fb)
2816                         drm_framebuffer_reference(fb);
2817         }
2818
2819         if (req->flags & DRM_MODE_CURSOR_MOVE) {
2820                 crtc_x = req->x;
2821                 crtc_y = req->y;
2822         } else {
2823                 crtc_x = crtc->cursor_x;
2824                 crtc_y = crtc->cursor_y;
2825         }
2826
2827         if (fb) {
2828                 crtc_w = fb->width;
2829                 crtc_h = fb->height;
2830                 src_w = fb->width << 16;
2831                 src_h = fb->height << 16;
2832         }
2833
2834         /*
2835          * setplane_internal will take care of deref'ing either the old or new
2836          * framebuffer depending on success.
2837          */
2838         ret = __setplane_internal(crtc->cursor, crtc, fb,
2839                                 crtc_x, crtc_y, crtc_w, crtc_h,
2840                                 0, 0, src_w, src_h);
2841
2842         /* Update successful; save new cursor position, if necessary */
2843         if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2844                 crtc->cursor_x = req->x;
2845                 crtc->cursor_y = req->y;
2846         }
2847
2848         return ret;
2849 }
2850
2851 static int drm_mode_cursor_common(struct drm_device *dev,
2852                                   struct drm_mode_cursor2 *req,
2853                                   struct drm_file *file_priv)
2854 {
2855         struct drm_crtc *crtc;
2856         int ret = 0;
2857
2858         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2859                 return -EINVAL;
2860
2861         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2862                 return -EINVAL;
2863
2864         crtc = drm_crtc_find(dev, req->crtc_id);
2865         if (!crtc) {
2866                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2867                 return -ENOENT;
2868         }
2869
2870         /*
2871          * If this crtc has a universal cursor plane, call that plane's update
2872          * handler rather than using legacy cursor handlers.
2873          */
2874         drm_modeset_lock_crtc(crtc, crtc->cursor);
2875         if (crtc->cursor) {
2876                 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2877                 goto out;
2878         }
2879
2880         if (req->flags & DRM_MODE_CURSOR_BO) {
2881                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2882                         ret = -ENXIO;
2883                         goto out;
2884                 }
2885                 /* Turns off the cursor if handle is 0 */
2886                 if (crtc->funcs->cursor_set2)
2887                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2888                                                       req->width, req->height, req->hot_x, req->hot_y);
2889                 else
2890                         ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2891                                                       req->width, req->height);
2892         }
2893
2894         if (req->flags & DRM_MODE_CURSOR_MOVE) {
2895                 if (crtc->funcs->cursor_move) {
2896                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2897                 } else {
2898                         ret = -EFAULT;
2899                         goto out;
2900                 }
2901         }
2902 out:
2903         drm_modeset_unlock_crtc(crtc);
2904
2905         return ret;
2906
2907 }
2908
2909
2910 /**
2911  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2912  * @dev: drm device for the ioctl
2913  * @data: data pointer for the ioctl
2914  * @file_priv: drm file for the ioctl call
2915  *
2916  * Set the cursor configuration based on user request.
2917  *
2918  * Called by the user via ioctl.
2919  *
2920  * Returns:
2921  * Zero on success, negative errno on failure.
2922  */
2923 int drm_mode_cursor_ioctl(struct drm_device *dev,
2924                           void *data, struct drm_file *file_priv)
2925 {
2926         struct drm_mode_cursor *req = data;
2927         struct drm_mode_cursor2 new_req;
2928
2929         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2930         new_req.hot_x = new_req.hot_y = 0;
2931
2932         return drm_mode_cursor_common(dev, &new_req, file_priv);
2933 }
2934
2935 /**
2936  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2937  * @dev: drm device for the ioctl
2938  * @data: data pointer for the ioctl
2939  * @file_priv: drm file for the ioctl call
2940  *
2941  * Set the cursor configuration based on user request. This implements the 2nd
2942  * version of the cursor ioctl, which allows userspace to additionally specify
2943  * the hotspot of the pointer.
2944  *
2945  * Called by the user via ioctl.
2946  *
2947  * Returns:
2948  * Zero on success, negative errno on failure.
2949  */
2950 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2951                            void *data, struct drm_file *file_priv)
2952 {
2953         struct drm_mode_cursor2 *req = data;
2954
2955         return drm_mode_cursor_common(dev, req, file_priv);
2956 }
2957
2958 /**
2959  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2960  * @bpp: bits per pixels
2961  * @depth: bit depth per pixel
2962  *
2963  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2964  * Useful in fbdev emulation code, since that deals in those values.
2965  */
2966 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2967 {
2968         uint32_t fmt;
2969
2970         switch (bpp) {
2971         case 8:
2972                 fmt = DRM_FORMAT_C8;
2973                 break;
2974         case 16:
2975                 if (depth == 15)
2976                         fmt = DRM_FORMAT_XRGB1555;
2977                 else
2978                         fmt = DRM_FORMAT_RGB565;
2979                 break;
2980         case 24:
2981                 fmt = DRM_FORMAT_RGB888;
2982                 break;
2983         case 32:
2984                 if (depth == 24)
2985                         fmt = DRM_FORMAT_XRGB8888;
2986                 else if (depth == 30)
2987                         fmt = DRM_FORMAT_XRGB2101010;
2988                 else
2989                         fmt = DRM_FORMAT_ARGB8888;
2990                 break;
2991         default:
2992                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2993                 fmt = DRM_FORMAT_XRGB8888;
2994                 break;
2995         }
2996
2997         return fmt;
2998 }
2999 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3000
3001 /**
3002  * drm_mode_addfb - add an FB to the graphics configuration
3003  * @dev: drm device for the ioctl
3004  * @data: data pointer for the ioctl
3005  * @file_priv: drm file for the ioctl call
3006  *
3007  * Add a new FB to the specified CRTC, given a user request. This is the
3008  * original addfb ioctl which only supported RGB formats.
3009  *
3010  * Called by the user via ioctl.
3011  *
3012  * Returns:
3013  * Zero on success, negative errno on failure.
3014  */
3015 int drm_mode_addfb(struct drm_device *dev,
3016                    void *data, struct drm_file *file_priv)
3017 {
3018         struct drm_mode_fb_cmd *or = data;
3019         struct drm_mode_fb_cmd2 r = {};
3020         int ret;
3021
3022         /* convert to new format and call new ioctl */
3023         r.fb_id = or->fb_id;
3024         r.width = or->width;
3025         r.height = or->height;
3026         r.pitches[0] = or->pitch;
3027         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3028         r.handles[0] = or->handle;
3029
3030         ret = drm_mode_addfb2(dev, &r, file_priv);
3031         if (ret)
3032                 return ret;
3033
3034         or->fb_id = r.fb_id;
3035
3036         return 0;
3037 }
3038
3039 static int format_check(const struct drm_mode_fb_cmd2 *r)
3040 {
3041         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3042
3043         switch (format) {
3044         case DRM_FORMAT_C8:
3045         case DRM_FORMAT_RGB332:
3046         case DRM_FORMAT_BGR233:
3047         case DRM_FORMAT_XRGB4444:
3048         case DRM_FORMAT_XBGR4444:
3049         case DRM_FORMAT_RGBX4444:
3050         case DRM_FORMAT_BGRX4444:
3051         case DRM_FORMAT_ARGB4444:
3052         case DRM_FORMAT_ABGR4444:
3053         case DRM_FORMAT_RGBA4444:
3054         case DRM_FORMAT_BGRA4444:
3055         case DRM_FORMAT_XRGB1555:
3056         case DRM_FORMAT_XBGR1555:
3057         case DRM_FORMAT_RGBX5551:
3058         case DRM_FORMAT_BGRX5551:
3059         case DRM_FORMAT_ARGB1555:
3060         case DRM_FORMAT_ABGR1555:
3061         case DRM_FORMAT_RGBA5551:
3062         case DRM_FORMAT_BGRA5551:
3063         case DRM_FORMAT_RGB565:
3064         case DRM_FORMAT_BGR565:
3065         case DRM_FORMAT_RGB888:
3066         case DRM_FORMAT_BGR888:
3067         case DRM_FORMAT_XRGB8888:
3068         case DRM_FORMAT_XBGR8888:
3069         case DRM_FORMAT_RGBX8888:
3070         case DRM_FORMAT_BGRX8888:
3071         case DRM_FORMAT_ARGB8888:
3072         case DRM_FORMAT_ABGR8888:
3073         case DRM_FORMAT_RGBA8888:
3074         case DRM_FORMAT_BGRA8888:
3075         case DRM_FORMAT_XRGB2101010:
3076         case DRM_FORMAT_XBGR2101010:
3077         case DRM_FORMAT_RGBX1010102:
3078         case DRM_FORMAT_BGRX1010102:
3079         case DRM_FORMAT_ARGB2101010:
3080         case DRM_FORMAT_ABGR2101010:
3081         case DRM_FORMAT_RGBA1010102:
3082         case DRM_FORMAT_BGRA1010102:
3083         case DRM_FORMAT_YUYV:
3084         case DRM_FORMAT_YVYU:
3085         case DRM_FORMAT_UYVY:
3086         case DRM_FORMAT_VYUY:
3087         case DRM_FORMAT_AYUV:
3088         case DRM_FORMAT_NV12:
3089         case DRM_FORMAT_NV21:
3090         case DRM_FORMAT_NV16:
3091         case DRM_FORMAT_NV61:
3092         case DRM_FORMAT_NV24:
3093         case DRM_FORMAT_NV42:
3094         case DRM_FORMAT_YUV410:
3095         case DRM_FORMAT_YVU410:
3096         case DRM_FORMAT_YUV411:
3097         case DRM_FORMAT_YVU411:
3098         case DRM_FORMAT_YUV420:
3099         case DRM_FORMAT_YVU420:
3100         case DRM_FORMAT_YUV422:
3101         case DRM_FORMAT_YVU422:
3102         case DRM_FORMAT_YUV444:
3103         case DRM_FORMAT_YVU444:
3104                 return 0;
3105         default:
3106                 DRM_DEBUG_KMS("invalid pixel format %s\n",
3107                               drm_get_format_name(r->pixel_format));
3108                 return -EINVAL;
3109         }
3110 }
3111
3112 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3113 {
3114         int ret, hsub, vsub, num_planes, i;
3115
3116         ret = format_check(r);
3117         if (ret) {
3118                 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3119                               drm_get_format_name(r->pixel_format));
3120                 return ret;
3121         }
3122
3123         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3124         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3125         num_planes = drm_format_num_planes(r->pixel_format);
3126
3127         if (r->width == 0 || r->width % hsub) {
3128                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3129                 return -EINVAL;
3130         }
3131
3132         if (r->height == 0 || r->height % vsub) {
3133                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3134                 return -EINVAL;
3135         }
3136
3137         for (i = 0; i < num_planes; i++) {
3138                 unsigned int width = r->width / (i != 0 ? hsub : 1);
3139                 unsigned int height = r->height / (i != 0 ? vsub : 1);
3140                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3141
3142                 if (!r->handles[i]) {
3143                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3144                         return -EINVAL;
3145                 }
3146
3147                 if ((uint64_t) width * cpp > UINT_MAX)
3148                         return -ERANGE;
3149
3150                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3151                         return -ERANGE;
3152
3153                 if (r->pitches[i] < width * cpp) {
3154                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3155                         return -EINVAL;
3156                 }
3157         }
3158
3159         return 0;
3160 }
3161
3162 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3163                                                         struct drm_mode_fb_cmd2 *r,
3164                                                         struct drm_file *file_priv)
3165 {
3166         struct drm_mode_config *config = &dev->mode_config;
3167         struct drm_framebuffer *fb;
3168         int ret;
3169
3170         if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3171                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3172                 return ERR_PTR(-EINVAL);
3173         }
3174
3175         if ((config->min_width > r->width) || (r->width > config->max_width)) {
3176                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3177                           r->width, config->min_width, config->max_width);
3178                 return ERR_PTR(-EINVAL);
3179         }
3180         if ((config->min_height > r->height) || (r->height > config->max_height)) {
3181                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3182                           r->height, config->min_height, config->max_height);
3183                 return ERR_PTR(-EINVAL);
3184         }
3185
3186         ret = framebuffer_check(r);
3187         if (ret)
3188                 return ERR_PTR(ret);
3189
3190         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3191         if (IS_ERR(fb)) {
3192                 DRM_DEBUG_KMS("could not create framebuffer\n");
3193                 return fb;
3194         }
3195
3196         mutex_lock(&file_priv->fbs_lock);
3197         r->fb_id = fb->base.id;
3198         list_add(&fb->filp_head, &file_priv->fbs);
3199         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3200         mutex_unlock(&file_priv->fbs_lock);
3201
3202         return fb;
3203 }
3204
3205 /**
3206  * drm_mode_addfb2 - add an FB to the graphics configuration
3207  * @dev: drm device for the ioctl
3208  * @data: data pointer for the ioctl
3209  * @file_priv: drm file for the ioctl call
3210  *
3211  * Add a new FB to the specified CRTC, given a user request with format. This is
3212  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3213  * and uses fourcc codes as pixel format specifiers.
3214  *
3215  * Called by the user via ioctl.
3216  *
3217  * Returns:
3218  * Zero on success, negative errno on failure.
3219  */
3220 int drm_mode_addfb2(struct drm_device *dev,
3221                     void *data, struct drm_file *file_priv)
3222 {
3223         struct drm_framebuffer *fb;
3224
3225         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3226                 return -EINVAL;
3227
3228         fb = add_framebuffer_internal(dev, data, file_priv);
3229         if (IS_ERR(fb))
3230                 return PTR_ERR(fb);
3231
3232         return 0;
3233 }
3234
3235 /**
3236  * drm_mode_rmfb - remove an FB from the configuration
3237  * @dev: drm device for the ioctl
3238  * @data: data pointer for the ioctl
3239  * @file_priv: drm file for the ioctl call
3240  *
3241  * Remove the FB specified by the user.
3242  *
3243  * Called by the user via ioctl.
3244  *
3245  * Returns:
3246  * Zero on success, negative errno on failure.
3247  */
3248 int drm_mode_rmfb(struct drm_device *dev,
3249                    void *data, struct drm_file *file_priv)
3250 {
3251         struct drm_framebuffer *fb = NULL;
3252         struct drm_framebuffer *fbl = NULL;
3253         uint32_t *id = data;
3254         int found = 0;
3255
3256         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3257                 return -EINVAL;
3258
3259         mutex_lock(&file_priv->fbs_lock);
3260         mutex_lock(&dev->mode_config.fb_lock);
3261         fb = __drm_framebuffer_lookup(dev, *id);
3262         if (!fb)
3263                 goto fail_lookup;
3264
3265         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3266                 if (fb == fbl)
3267                         found = 1;
3268         if (!found)
3269                 goto fail_lookup;
3270
3271         /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3272         __drm_framebuffer_unregister(dev, fb);
3273
3274         list_del_init(&fb->filp_head);
3275         mutex_unlock(&dev->mode_config.fb_lock);
3276         mutex_unlock(&file_priv->fbs_lock);
3277
3278         drm_framebuffer_remove(fb);
3279
3280         return 0;
3281
3282 fail_lookup:
3283         mutex_unlock(&dev->mode_config.fb_lock);
3284         mutex_unlock(&file_priv->fbs_lock);
3285
3286         return -ENOENT;
3287 }
3288
3289 /**
3290  * drm_mode_getfb - get FB info
3291  * @dev: drm device for the ioctl
3292  * @data: data pointer for the ioctl
3293  * @file_priv: drm file for the ioctl call
3294  *
3295  * Lookup the FB given its ID and return info about it.
3296  *
3297  * Called by the user via ioctl.
3298  *
3299  * Returns:
3300  * Zero on success, negative errno on failure.
3301  */
3302 int drm_mode_getfb(struct drm_device *dev,
3303                    void *data, struct drm_file *file_priv)
3304 {
3305         struct drm_mode_fb_cmd *r = data;
3306         struct drm_framebuffer *fb;
3307         int ret;
3308
3309         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3310                 return -EINVAL;
3311
3312         fb = drm_framebuffer_lookup(dev, r->fb_id);
3313         if (!fb)
3314                 return -ENOENT;
3315
3316         r->height = fb->height;
3317         r->width = fb->width;
3318         r->depth = fb->depth;
3319         r->bpp = fb->bits_per_pixel;
3320         r->pitch = fb->pitches[0];
3321         if (fb->funcs->create_handle) {
3322                 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3323                     drm_is_control_client(file_priv)) {
3324                         ret = fb->funcs->create_handle(fb, file_priv,
3325                                                        &r->handle);
3326                 } else {
3327                         /* GET_FB() is an unprivileged ioctl so we must not
3328                          * return a buffer-handle to non-master processes! For
3329                          * backwards-compatibility reasons, we cannot make
3330                          * GET_FB() privileged, so just return an invalid handle
3331                          * for non-masters. */
3332                         r->handle = 0;
3333                         ret = 0;
3334                 }
3335         } else {
3336                 ret = -ENODEV;
3337         }
3338
3339         drm_framebuffer_unreference(fb);
3340
3341         return ret;
3342 }
3343
3344 /**
3345  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3346  * @dev: drm device for the ioctl
3347  * @data: data pointer for the ioctl
3348  * @file_priv: drm file for the ioctl call
3349  *
3350  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3351  * rectangle list. Generic userspace which does frontbuffer rendering must call
3352  * this ioctl to flush out the changes on manual-update display outputs, e.g.
3353  * usb display-link, mipi manual update panels or edp panel self refresh modes.
3354  *
3355  * Modesetting drivers which always update the frontbuffer do not need to
3356  * implement the corresponding ->dirty framebuffer callback.
3357  *
3358  * Called by the user via ioctl.
3359  *
3360  * Returns:
3361  * Zero on success, negative errno on failure.
3362  */
3363 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3364                            void *data, struct drm_file *file_priv)
3365 {
3366         struct drm_clip_rect __user *clips_ptr;
3367         struct drm_clip_rect *clips = NULL;
3368         struct drm_mode_fb_dirty_cmd *r = data;
3369         struct drm_framebuffer *fb;
3370         unsigned flags;
3371         int num_clips;
3372         int ret;
3373
3374         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3375                 return -EINVAL;
3376
3377         fb = drm_framebuffer_lookup(dev, r->fb_id);
3378         if (!fb)
3379                 return -ENOENT;
3380
3381         num_clips = r->num_clips;
3382         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3383
3384         if (!num_clips != !clips_ptr) {
3385                 ret = -EINVAL;
3386                 goto out_err1;
3387         }
3388
3389         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3390
3391         /* If userspace annotates copy, clips must come in pairs */
3392         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3393                 ret = -EINVAL;
3394                 goto out_err1;
3395         }
3396
3397         if (num_clips && clips_ptr) {
3398                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3399                         ret = -EINVAL;
3400                         goto out_err1;
3401                 }
3402                 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3403                 if (!clips) {
3404                         ret = -ENOMEM;
3405                         goto out_err1;
3406                 }
3407
3408                 ret = copy_from_user(clips, clips_ptr,
3409                                      num_clips * sizeof(*clips));
3410                 if (ret) {
3411                         ret = -EFAULT;
3412                         goto out_err2;
3413                 }
3414         }
3415
3416         if (fb->funcs->dirty) {
3417                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3418                                        clips, num_clips);
3419         } else {
3420                 ret = -ENOSYS;
3421         }
3422
3423 out_err2:
3424         kfree(clips);
3425 out_err1:
3426         drm_framebuffer_unreference(fb);
3427
3428         return ret;
3429 }
3430
3431
3432 /**
3433  * drm_fb_release - remove and free the FBs on this file
3434  * @priv: drm file for the ioctl
3435  *
3436  * Destroy all the FBs associated with @filp.
3437  *
3438  * Called by the user via ioctl.
3439  *
3440  * Returns:
3441  * Zero on success, negative errno on failure.
3442  */
3443 void drm_fb_release(struct drm_file *priv)
3444 {
3445         struct drm_device *dev = priv->minor->dev;
3446         struct drm_framebuffer *fb, *tfb;
3447
3448         /*
3449          * When the file gets released that means no one else can access the fb
3450          * list any more, so no need to grab fpriv->fbs_lock. And we need to
3451          * avoid upsetting lockdep since the universal cursor code adds a
3452          * framebuffer while holding mutex locks.
3453          *
3454          * Note that a real deadlock between fpriv->fbs_lock and the modeset
3455          * locks is impossible here since no one else but this function can get
3456          * at it any more.
3457          */
3458         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3459
3460                 mutex_lock(&dev->mode_config.fb_lock);
3461                 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3462                 __drm_framebuffer_unregister(dev, fb);
3463                 mutex_unlock(&dev->mode_config.fb_lock);
3464
3465                 list_del_init(&fb->filp_head);
3466
3467                 /* This will also drop the fpriv->fbs reference. */
3468                 drm_framebuffer_remove(fb);
3469         }
3470 }
3471
3472 /**
3473  * drm_property_create - create a new property type
3474  * @dev: drm device
3475  * @flags: flags specifying the property type
3476  * @name: name of the property
3477  * @num_values: number of pre-defined values
3478  *
3479  * This creates a new generic drm property which can then be attached to a drm
3480  * object with drm_object_attach_property. The returned property object must be
3481  * freed with drm_property_destroy.
3482  *
3483  * Note that the DRM core keeps a per-device list of properties and that, if
3484  * drm_mode_config_cleanup() is called, it will destroy all properties created
3485  * by the driver.
3486  *
3487  * Returns:
3488  * A pointer to the newly created property on success, NULL on failure.
3489  */
3490 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3491                                          const char *name, int num_values)
3492 {
3493         struct drm_property *property = NULL;
3494         int ret;
3495
3496         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3497         if (!property)
3498                 return NULL;
3499
3500         property->dev = dev;
3501
3502         if (num_values) {
3503                 property->values = kcalloc(num_values, sizeof(uint64_t),
3504                                            GFP_KERNEL);
3505                 if (!property->values)
3506                         goto fail;
3507         }
3508
3509         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3510         if (ret)
3511                 goto fail;
3512
3513         property->flags = flags;
3514         property->num_values = num_values;
3515         INIT_LIST_HEAD(&property->enum_list);
3516
3517         if (name) {
3518                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3519                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3520         }
3521
3522         list_add_tail(&property->head, &dev->mode_config.property_list);
3523
3524         WARN_ON(!drm_property_type_valid(property));
3525
3526         return property;
3527 fail:
3528         kfree(property->values);
3529         kfree(property);
3530         return NULL;
3531 }
3532 EXPORT_SYMBOL(drm_property_create);
3533
3534 /**
3535  * drm_property_create_enum - create a new enumeration property type
3536  * @dev: drm device
3537  * @flags: flags specifying the property type
3538  * @name: name of the property
3539  * @props: enumeration lists with property values
3540  * @num_values: number of pre-defined values
3541  *
3542  * This creates a new generic drm property which can then be attached to a drm
3543  * object with drm_object_attach_property. The returned property object must be
3544  * freed with drm_property_destroy.
3545  *
3546  * Userspace is only allowed to set one of the predefined values for enumeration
3547  * properties.
3548  *
3549  * Returns:
3550  * A pointer to the newly created property on success, NULL on failure.
3551  */
3552 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3553                                          const char *name,
3554                                          const struct drm_prop_enum_list *props,
3555                                          int num_values)
3556 {
3557         struct drm_property *property;
3558         int i, ret;
3559
3560         flags |= DRM_MODE_PROP_ENUM;
3561
3562         property = drm_property_create(dev, flags, name, num_values);
3563         if (!property)
3564                 return NULL;
3565
3566         for (i = 0; i < num_values; i++) {
3567                 ret = drm_property_add_enum(property, i,
3568                                       props[i].type,
3569                                       props[i].name);
3570                 if (ret) {
3571                         drm_property_destroy(dev, property);
3572                         return NULL;
3573                 }
3574         }
3575
3576         return property;
3577 }
3578 EXPORT_SYMBOL(drm_property_create_enum);
3579
3580 /**
3581  * drm_property_create_bitmask - create a new bitmask property type
3582  * @dev: drm device
3583  * @flags: flags specifying the property type
3584  * @name: name of the property
3585  * @props: enumeration lists with property bitflags
3586  * @num_props: size of the @props array
3587  * @supported_bits: bitmask of all supported enumeration values
3588  *
3589  * This creates a new bitmask drm property which can then be attached to a drm
3590  * object with drm_object_attach_property. The returned property object must be
3591  * freed with drm_property_destroy.
3592  *
3593  * Compared to plain enumeration properties userspace is allowed to set any
3594  * or'ed together combination of the predefined property bitflag values
3595  *
3596  * Returns:
3597  * A pointer to the newly created property on success, NULL on failure.
3598  */
3599 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3600                                          int flags, const char *name,
3601                                          const struct drm_prop_enum_list *props,
3602                                          int num_props,
3603                                          uint64_t supported_bits)
3604 {
3605         struct drm_property *property;
3606         int i, ret, index = 0;
3607         int num_values = hweight64(supported_bits);
3608
3609         flags |= DRM_MODE_PROP_BITMASK;
3610
3611         property = drm_property_create(dev, flags, name, num_values);
3612         if (!property)
3613                 return NULL;
3614         for (i = 0; i < num_props; i++) {
3615                 if (!(supported_bits & (1ULL << props[i].type)))
3616                         continue;
3617
3618                 if (WARN_ON(index >= num_values)) {
3619                         drm_property_destroy(dev, property);
3620                         return NULL;
3621                 }
3622
3623                 ret = drm_property_add_enum(property, index++,
3624                                       props[i].type,
3625                                       props[i].name);
3626                 if (ret) {
3627                         drm_property_destroy(dev, property);
3628                         return NULL;
3629                 }
3630         }
3631
3632         return property;
3633 }
3634 EXPORT_SYMBOL(drm_property_create_bitmask);
3635
3636 static struct drm_property *property_create_range(struct drm_device *dev,
3637                                          int flags, const char *name,
3638                                          uint64_t min, uint64_t max)
3639 {
3640         struct drm_property *property;
3641
3642         property = drm_property_create(dev, flags, name, 2);
3643         if (!property)
3644                 return NULL;
3645
3646         property->values[0] = min;
3647         property->values[1] = max;
3648
3649         return property;
3650 }
3651
3652 /**
3653  * drm_property_create_range - create a new ranged property type
3654  * @dev: drm device
3655  * @flags: flags specifying the property type
3656  * @name: name of the property
3657  * @min: minimum value of the property
3658  * @max: maximum value of the property
3659  *
3660  * This creates a new generic drm property which can then be attached to a drm
3661  * object with drm_object_attach_property. The returned property object must be
3662  * freed with drm_property_destroy.
3663  *
3664  * Userspace is allowed to set any integer value in the (min, max) range
3665  * inclusive.
3666  *
3667  * Returns:
3668  * A pointer to the newly created property on success, NULL on failure.
3669  */
3670 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3671                                          const char *name,
3672                                          uint64_t min, uint64_t max)
3673 {
3674         return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3675                         name, min, max);
3676 }
3677 EXPORT_SYMBOL(drm_property_create_range);
3678
3679 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3680                                          int flags, const char *name,
3681                                          int64_t min, int64_t max)
3682 {
3683         return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3684                         name, I642U64(min), I642U64(max));
3685 }
3686 EXPORT_SYMBOL(drm_property_create_signed_range);
3687
3688 struct drm_property *drm_property_create_object(struct drm_device *dev,
3689                                          int flags, const char *name, uint32_t type)
3690 {
3691         struct drm_property *property;
3692
3693         flags |= DRM_MODE_PROP_OBJECT;
3694
3695         property = drm_property_create(dev, flags, name, 1);
3696         if (!property)
3697                 return NULL;
3698
3699         property->values[0] = type;
3700
3701         return property;
3702 }
3703 EXPORT_SYMBOL(drm_property_create_object);
3704
3705 /**
3706  * drm_property_add_enum - add a possible value to an enumeration property
3707  * @property: enumeration property to change
3708  * @index: index of the new enumeration
3709  * @value: value of the new enumeration
3710  * @name: symbolic name of the new enumeration
3711  *
3712  * This functions adds enumerations to a property.
3713  *
3714  * It's use is deprecated, drivers should use one of the more specific helpers
3715  * to directly create the property with all enumerations already attached.
3716  *
3717  * Returns:
3718  * Zero on success, error code on failure.
3719  */
3720 int drm_property_add_enum(struct drm_property *property, int index,
3721                           uint64_t value, const char *name)
3722 {
3723         struct drm_property_enum *prop_enum;
3724
3725         if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3726                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3727                 return -EINVAL;
3728
3729         /*
3730          * Bitmask enum properties have the additional constraint of values
3731          * from 0 to 63
3732          */
3733         if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3734                         (value > 63))
3735                 return -EINVAL;
3736
3737         if (!list_empty(&property->enum_list)) {
3738                 list_for_each_entry(prop_enum, &property->enum_list, head) {
3739                         if (prop_enum->value == value) {
3740                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3741                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3742                                 return 0;
3743                         }
3744                 }
3745         }
3746
3747         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3748         if (!prop_enum)
3749                 return -ENOMEM;
3750
3751         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3752         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3753         prop_enum->value = value;
3754
3755         property->values[index] = value;
3756         list_add_tail(&prop_enum->head, &property->enum_list);
3757         return 0;
3758 }
3759 EXPORT_SYMBOL(drm_property_add_enum);
3760
3761 /**
3762  * drm_property_destroy - destroy a drm property
3763  * @dev: drm device
3764  * @property: property to destry
3765  *
3766  * This function frees a property including any attached resources like
3767  * enumeration values.
3768  */
3769 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3770 {
3771         struct drm_property_enum *prop_enum, *pt;
3772
3773         list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3774                 list_del(&prop_enum->head);
3775                 kfree(prop_enum);
3776         }
3777
3778         if (property->num_values)
3779                 kfree(property->values);
3780         drm_mode_object_put(dev, &property->base);
3781         list_del(&property->head);
3782         kfree(property);
3783 }
3784 EXPORT_SYMBOL(drm_property_destroy);
3785
3786 /**
3787  * drm_object_attach_property - attach a property to a modeset object
3788  * @obj: drm modeset object
3789  * @property: property to attach
3790  * @init_val: initial value of the property
3791  *
3792  * This attaches the given property to the modeset object with the given initial
3793  * value. Currently this function cannot fail since the properties are stored in
3794  * a statically sized array.
3795  */
3796 void drm_object_attach_property(struct drm_mode_object *obj,
3797                                 struct drm_property *property,
3798                                 uint64_t init_val)
3799 {
3800         int count = obj->properties->count;
3801
3802         if (count == DRM_OBJECT_MAX_PROPERTY) {
3803                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3804                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3805                         "you see this message on the same object type.\n",
3806                         obj->type);
3807                 return;
3808         }
3809
3810         obj->properties->ids[count] = property->base.id;
3811         obj->properties->values[count] = init_val;
3812         obj->properties->count++;
3813 }
3814 EXPORT_SYMBOL(drm_object_attach_property);
3815
3816 /**
3817  * drm_object_property_set_value - set the value of a property
3818  * @obj: drm mode object to set property value for
3819  * @property: property to set
3820  * @val: value the property should be set to
3821  *
3822  * This functions sets a given property on a given object. This function only
3823  * changes the software state of the property, it does not call into the
3824  * driver's ->set_property callback.
3825  *
3826  * Returns:
3827  * Zero on success, error code on failure.
3828  */
3829 int drm_object_property_set_value(struct drm_mode_object *obj,
3830                                   struct drm_property *property, uint64_t val)
3831 {
3832         int i;
3833
3834         for (i = 0; i < obj->properties->count; i++) {
3835                 if (obj->properties->ids[i] == property->base.id) {
3836                         obj->properties->values[i] = val;
3837                         return 0;
3838                 }
3839         }
3840
3841         return -EINVAL;
3842 }
3843 EXPORT_SYMBOL(drm_object_property_set_value);
3844
3845 /**
3846  * drm_object_property_get_value - retrieve the value of a property
3847  * @obj: drm mode object to get property value from
3848  * @property: property to retrieve
3849  * @val: storage for the property value
3850  *
3851  * This function retrieves the softare state of the given property for the given
3852  * property. Since there is no driver callback to retrieve the current property
3853  * value this might be out of sync with the hardware, depending upon the driver
3854  * and property.
3855  *
3856  * Returns:
3857  * Zero on success, error code on failure.
3858  */
3859 int drm_object_property_get_value(struct drm_mode_object *obj,
3860                                   struct drm_property *property, uint64_t *val)
3861 {
3862         int i;
3863
3864         for (i = 0; i < obj->properties->count; i++) {
3865                 if (obj->properties->ids[i] == property->base.id) {
3866                         *val = obj->properties->values[i];
3867                         return 0;
3868                 }
3869         }
3870
3871         return -EINVAL;
3872 }
3873 EXPORT_SYMBOL(drm_object_property_get_value);
3874
3875 /**
3876  * drm_mode_getproperty_ioctl - get the property metadata
3877  * @dev: DRM device
3878  * @data: ioctl data
3879  * @file_priv: DRM file info
3880  *
3881  * This function retrieves the metadata for a given property, like the different
3882  * possible values for an enum property or the limits for a range property.
3883  *
3884  * Blob properties are special
3885  *
3886  * Called by the user via ioctl.
3887  *
3888  * Returns:
3889  * Zero on success, negative errno on failure.
3890  */
3891 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3892                                void *data, struct drm_file *file_priv)
3893 {
3894         struct drm_mode_get_property *out_resp = data;
3895         struct drm_property *property;
3896         int enum_count = 0;
3897         int value_count = 0;
3898         int ret = 0, i;
3899         int copied;
3900         struct drm_property_enum *prop_enum;
3901         struct drm_mode_property_enum __user *enum_ptr;
3902         uint64_t __user *values_ptr;
3903
3904         if (!drm_core_check_feature(dev, DRIVER_MODESET))
3905                 return -EINVAL;
3906
3907         drm_modeset_lock_all(dev);
3908         property = drm_property_find(dev, out_resp->prop_id);
3909         if (!property) {
3910                 ret = -ENOENT;
3911                 goto done;
3912         }
3913
3914         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3915                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3916                 list_for_each_entry(prop_enum, &property->enum_list, head)
3917                         enum_count++;
3918         }
3919
3920         value_count = property->num_values;
3921
3922         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3923         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3924         out_resp->flags = property->flags;
3925
3926         if ((out_resp->count_values >= value_count) && value_count) {
3927                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3928                 for (i = 0; i < value_count; i++) {
3929                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3930                                 ret = -EFAULT;
3931                                 goto done;
3932                         }
3933                 }
3934         }
3935         out_resp->count_values = value_count;
3936
3937         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3938                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3939                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3940                         copied = 0;
3941                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3942                         list_for_each_entry(prop_enum, &property->enum_list, head) {
3943
3944                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3945                                         ret = -EFAULT;
3946                                         goto done;
3947                                 }
3948
3949                                 if (copy_to_user(&enum_ptr[copied].name,
3950                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
3951                                         ret = -EFAULT;
3952                                         goto done;
3953                                 }
3954                                 copied++;
3955                         }
3956                 }
3957                 out_resp->count_enum_blobs = enum_count;
3958         }
3959
3960         /*
3961          * NOTE: The idea seems to have been to use this to read all the blob
3962          * property values. But nothing ever added them to the corresponding
3963          * list, userspace always used the special-purpose get_blob ioctl to
3964          * read the value for a blob property. It also doesn't make a lot of
3965          * sense to return values here when everything else is just metadata for
3966          * the property itself.
3967          */
3968         if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3969                 out_resp->count_enum_blobs = 0;
3970 done:
3971         drm_modeset_unlock_all(dev);
3972         return ret;
3973 }
3974
3975 static struct drm_property_blob *
3976 drm_property_create_blob(struct drm_device *dev, size_t length,
3977                          const void *data)
3978 {
3979         struct drm_property_blob *blob;
3980         int ret;
3981
3982         if (!length || !data)
3983                 return NULL;
3984
3985         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3986         if (!blob)
3987                 return NULL;
3988
3989         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3990         if (ret) {
3991                 kfree(blob);
3992                 return NULL;
3993         }
3994
3995         blob->length = length;
3996
3997         memcpy(blob->data, data, length);
3998
3999         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4000         return blob;
4001 }
4002
4003 static void drm_property_destroy_blob(struct drm_device *dev,
4004                                struct drm_property_blob *blob)
4005 {
4006         drm_mode_object_put(dev, &blob->base);
4007         list_del(&blob->head);
4008         kfree(blob);
4009 }
4010
4011 /**
4012  * drm_mode_getblob_ioctl - get the contents of a blob property value
4013  * @dev: DRM device
4014  * @data: ioctl data
4015  * @file_priv: DRM file info
4016  *
4017  * This function retrieves the contents of a blob property. The value stored in
4018  * an object's blob property is just a normal modeset object id.
4019  *
4020  * Called by the user via ioctl.
4021  *
4022  * Returns:
4023  * Zero on success, negative errno on failure.
4024  */
4025 int drm_mode_getblob_ioctl(struct drm_device *dev,
4026                            void *data, struct drm_file *file_priv)
4027 {
4028         struct drm_mode_get_blob *out_resp = data;
4029         struct drm_property_blob *blob;
4030         int ret = 0;
4031         void __user *blob_ptr;
4032
4033         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4034                 return -EINVAL;
4035
4036         drm_modeset_lock_all(dev);
4037         blob = drm_property_blob_find(dev, out_resp->blob_id);
4038         if (!blob) {
4039                 ret = -ENOENT;
4040                 goto done;
4041         }
4042
4043         if (out_resp->length == blob->length) {
4044                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4045                 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4046                         ret = -EFAULT;
4047                         goto done;
4048                 }
4049         }
4050         out_resp->length = blob->length;
4051
4052 done:
4053         drm_modeset_unlock_all(dev);
4054         return ret;
4055 }
4056
4057 /**
4058  * drm_mode_connector_set_path_property - set tile property on connector
4059  * @connector: connector to set property on.
4060  * @path: path to use for property.
4061  *
4062  * This creates a property to expose to userspace to specify a
4063  * connector path. This is mainly used for DisplayPort MST where
4064  * connectors have a topology and we want to allow userspace to give
4065  * them more meaningful names.
4066  *
4067  * Returns:
4068  * Zero on success, negative errno on failure.
4069  */
4070 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4071                                          const char *path)
4072 {
4073         struct drm_device *dev = connector->dev;
4074         size_t size = strlen(path) + 1;
4075         int ret;
4076
4077         connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4078                                                             size, path);
4079         if (!connector->path_blob_ptr)
4080                 return -EINVAL;
4081
4082         ret = drm_object_property_set_value(&connector->base,
4083                                             dev->mode_config.path_property,
4084                                             connector->path_blob_ptr->base.id);
4085         return ret;
4086 }
4087 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4088
4089 /**
4090  * drm_mode_connector_update_edid_property - update the edid property of a connector
4091  * @connector: drm connector
4092  * @edid: new value of the edid property
4093  *
4094  * This function creates a new blob modeset object and assigns its id to the
4095  * connector's edid property.
4096  *
4097  * Returns:
4098  * Zero on success, negative errno on failure.
4099  */
4100 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4101                                             const struct edid *edid)
4102 {
4103         struct drm_device *dev = connector->dev;
4104         size_t size;
4105         int ret;
4106
4107         /* ignore requests to set edid when overridden */
4108         if (connector->override_edid)
4109                 return 0;
4110
4111         if (connector->edid_blob_ptr)
4112                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4113
4114         /* Delete edid, when there is none. */
4115         if (!edid) {
4116                 connector->edid_blob_ptr = NULL;
4117                 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
4118                 return ret;
4119         }
4120
4121         size = EDID_LENGTH * (1 + edid->extensions);
4122         connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4123                                                             size, edid);
4124         if (!connector->edid_blob_ptr)
4125                 return -EINVAL;
4126
4127         ret = drm_object_property_set_value(&connector->base,
4128                                                dev->mode_config.edid_property,
4129                                                connector->edid_blob_ptr->base.id);
4130
4131         return ret;
4132 }
4133 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4134
4135 static bool drm_property_change_is_valid(struct drm_property *property,
4136                                          uint64_t value)
4137 {
4138         int i;
4139
4140         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4141                 return false;
4142
4143         if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4144                 if (value < property->values[0] || value > property->values[1])
4145                         return false;
4146                 return true;
4147         } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4148                 int64_t svalue = U642I64(value);
4149
4150                 if (svalue < U642I64(property->values[0]) ||
4151                                 svalue > U642I64(property->values[1]))
4152                         return false;
4153                 return true;
4154         } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4155                 uint64_t valid_mask = 0;
4156
4157                 for (i = 0; i < property->num_values; i++)
4158                         valid_mask |= (1ULL << property->values[i]);
4159                 return !(value & ~valid_mask);
4160         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4161                 /* Only the driver knows */
4162                 return true;
4163         } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4164                 struct drm_mode_object *obj;
4165
4166                 /* a zero value for an object property translates to null: */
4167                 if (value == 0)
4168                         return true;
4169                 /*
4170                  * NOTE: use _object_find() directly to bypass restriction on
4171                  * looking up refcnt'd objects (ie. fb's).  For a refcnt'd
4172                  * object this could race against object finalization, so it
4173                  * simply tells us that the object *was* valid.  Which is good
4174                  * enough.
4175                  */
4176                 obj = _object_find(property->dev, value, property->values[0]);
4177                 return obj != NULL;
4178         }
4179
4180         for (i = 0; i < property->num_values; i++)
4181                 if (property->values[i] == value)
4182                         return true;
4183         return false;
4184 }
4185
4186 /**
4187  * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4188  * @dev: DRM device
4189  * @data: ioctl data
4190  * @file_priv: DRM file info
4191  *
4192  * This function sets the current value for a connectors's property. It also
4193  * calls into a driver's ->set_property callback to update the hardware state
4194  *
4195  * Called by the user via ioctl.
4196  *
4197  * Returns:
4198  * Zero on success, negative errno on failure.
4199  */
4200 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4201                                        void *data, struct drm_file *file_priv)
4202 {
4203         struct drm_mode_connector_set_property *conn_set_prop = data;
4204         struct drm_mode_obj_set_property obj_set_prop = {
4205                 .value = conn_set_prop->value,
4206                 .prop_id = conn_set_prop->prop_id,
4207                 .obj_id = conn_set_prop->connector_id,
4208                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4209         };
4210
4211         /* It does all the locking and checking we need */
4212         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4213 }
4214
4215 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4216                                            struct drm_property *property,
4217                                            uint64_t value)
4218 {
4219         int ret = -EINVAL;
4220         struct drm_connector *connector = obj_to_connector(obj);
4221
4222         /* Do DPMS ourselves */
4223         if (property == connector->dev->mode_config.dpms_property) {
4224                 if (connector->funcs->dpms)
4225                         (*connector->funcs->dpms)(connector, (int)value);
4226                 ret = 0;
4227         } else if (connector->funcs->set_property)
4228                 ret = connector->funcs->set_property(connector, property, value);
4229
4230         /* store the property value if successful */
4231         if (!ret)
4232                 drm_object_property_set_value(&connector->base, property, value);
4233         return ret;
4234 }
4235
4236 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4237                                       struct drm_property *property,
4238                                       uint64_t value)
4239 {
4240         int ret = -EINVAL;
4241         struct drm_crtc *crtc = obj_to_crtc(obj);
4242
4243         if (crtc->funcs->set_property)
4244                 ret = crtc->funcs->set_property(crtc, property, value);
4245         if (!ret)
4246                 drm_object_property_set_value(obj, property, value);
4247
4248         return ret;
4249 }
4250
4251 /**
4252  * drm_mode_plane_set_obj_prop - set the value of a property
4253  * @plane: drm plane object to set property value for
4254  * @property: property to set
4255  * @value: value the property should be set to
4256  *
4257  * This functions sets a given property on a given plane object. This function
4258  * calls the driver's ->set_property callback and changes the software state of
4259  * the property if the callback succeeds.
4260  *
4261  * Returns:
4262  * Zero on success, error code on failure.
4263  */
4264 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4265                                 struct drm_property *property,
4266                                 uint64_t value)
4267 {
4268         int ret = -EINVAL;
4269         struct drm_mode_object *obj = &plane->base;
4270
4271         if (plane->funcs->set_property)
4272                 ret = plane->funcs->set_property(plane, property, value);
4273         if (!ret)
4274                 drm_object_property_set_value(obj, property, value);
4275
4276         return ret;
4277 }
4278 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4279
4280 /**
4281  * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4282  * @dev: DRM device
4283  * @data: ioctl data
4284  * @file_priv: DRM file info
4285  *
4286  * This function retrieves the current value for an object's property. Compared
4287  * to the connector specific ioctl this one is extended to also work on crtc and
4288  * plane objects.
4289  *
4290  * Called by the user via ioctl.
4291  *
4292  * Returns:
4293  * Zero on success, negative errno on failure.
4294  */
4295 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4296                                       struct drm_file *file_priv)
4297 {
4298         struct drm_mode_obj_get_properties *arg = data;
4299         struct drm_mode_object *obj;
4300         int ret = 0;
4301         int i;
4302         int copied = 0;
4303         int props_count = 0;
4304         uint32_t __user *props_ptr;
4305         uint64_t __user *prop_values_ptr;
4306
4307         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4308                 return -EINVAL;
4309
4310         drm_modeset_lock_all(dev);
4311
4312         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4313         if (!obj) {
4314                 ret = -ENOENT;
4315                 goto out;
4316         }
4317         if (!obj->properties) {
4318                 ret = -EINVAL;
4319                 goto out;
4320         }
4321
4322         props_count = obj->properties->count;
4323
4324         /* This ioctl is called twice, once to determine how much space is
4325          * needed, and the 2nd time to fill it. */
4326         if ((arg->count_props >= props_count) && props_count) {
4327                 copied = 0;
4328                 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4329                 prop_values_ptr = (uint64_t __user *)(unsigned long)
4330                                   (arg->prop_values_ptr);
4331                 for (i = 0; i < props_count; i++) {
4332                         if (put_user(obj->properties->ids[i],
4333                                      props_ptr + copied)) {
4334                                 ret = -EFAULT;
4335                                 goto out;
4336                         }
4337                         if (put_user(obj->properties->values[i],
4338                                      prop_values_ptr + copied)) {
4339                                 ret = -EFAULT;
4340                                 goto out;
4341                         }
4342                         copied++;
4343                 }
4344         }
4345         arg->count_props = props_count;
4346 out:
4347         drm_modeset_unlock_all(dev);
4348         return ret;
4349 }
4350
4351 /**
4352  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4353  * @dev: DRM device
4354  * @data: ioctl data
4355  * @file_priv: DRM file info
4356  *
4357  * This function sets the current value for an object's property. It also calls
4358  * into a driver's ->set_property callback to update the hardware state.
4359  * Compared to the connector specific ioctl this one is extended to also work on
4360  * crtc and plane objects.
4361  *
4362  * Called by the user via ioctl.
4363  *
4364  * Returns:
4365  * Zero on success, negative errno on failure.
4366  */
4367 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4368                                     struct drm_file *file_priv)
4369 {
4370         struct drm_mode_obj_set_property *arg = data;
4371         struct drm_mode_object *arg_obj;
4372         struct drm_mode_object *prop_obj;
4373         struct drm_property *property;
4374         int ret = -EINVAL;
4375         int i;
4376
4377         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4378                 return -EINVAL;
4379
4380         drm_modeset_lock_all(dev);
4381
4382         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4383         if (!arg_obj) {
4384                 ret = -ENOENT;
4385                 goto out;
4386         }
4387         if (!arg_obj->properties)
4388                 goto out;
4389
4390         for (i = 0; i < arg_obj->properties->count; i++)
4391                 if (arg_obj->properties->ids[i] == arg->prop_id)
4392                         break;
4393
4394         if (i == arg_obj->properties->count)
4395                 goto out;
4396
4397         prop_obj = drm_mode_object_find(dev, arg->prop_id,
4398                                         DRM_MODE_OBJECT_PROPERTY);
4399         if (!prop_obj) {
4400                 ret = -ENOENT;
4401                 goto out;
4402         }
4403         property = obj_to_property(prop_obj);
4404
4405         if (!drm_property_change_is_valid(property, arg->value))
4406                 goto out;
4407
4408         switch (arg_obj->type) {
4409         case DRM_MODE_OBJECT_CONNECTOR:
4410                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4411                                                       arg->value);
4412                 break;
4413         case DRM_MODE_OBJECT_CRTC:
4414                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4415                 break;
4416         case DRM_MODE_OBJECT_PLANE:
4417                 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4418                                                   property, arg->value);
4419                 break;
4420         }
4421
4422 out:
4423         drm_modeset_unlock_all(dev);
4424         return ret;
4425 }
4426
4427 /**
4428  * drm_mode_connector_attach_encoder - attach a connector to an encoder
4429  * @connector: connector to attach
4430  * @encoder: encoder to attach @connector to
4431  *
4432  * This function links up a connector to an encoder. Note that the routing
4433  * restrictions between encoders and crtcs are exposed to userspace through the
4434  * possible_clones and possible_crtcs bitmasks.
4435  *
4436  * Returns:
4437  * Zero on success, negative errno on failure.
4438  */
4439 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4440                                       struct drm_encoder *encoder)
4441 {
4442         int i;
4443
4444         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4445                 if (connector->encoder_ids[i] == 0) {
4446                         connector->encoder_ids[i] = encoder->base.id;
4447                         return 0;
4448                 }
4449         }
4450         return -ENOMEM;
4451 }
4452 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4453
4454 /**
4455  * drm_mode_crtc_set_gamma_size - set the gamma table size
4456  * @crtc: CRTC to set the gamma table size for
4457  * @gamma_size: size of the gamma table
4458  *
4459  * Drivers which support gamma tables should set this to the supported gamma
4460  * table size when initializing the CRTC. Currently the drm core only supports a
4461  * fixed gamma table size.
4462  *
4463  * Returns:
4464  * Zero on success, negative errno on failure.
4465  */
4466 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4467                                  int gamma_size)
4468 {
4469         crtc->gamma_size = gamma_size;
4470
4471         crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4472                                     GFP_KERNEL);
4473         if (!crtc->gamma_store) {
4474                 crtc->gamma_size = 0;
4475                 return -ENOMEM;
4476         }
4477
4478         return 0;
4479 }
4480 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4481
4482 /**
4483  * drm_mode_gamma_set_ioctl - set the gamma table
4484  * @dev: DRM device
4485  * @data: ioctl data
4486  * @file_priv: DRM file info
4487  *
4488  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4489  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4490  *
4491  * Called by the user via ioctl.
4492  *
4493  * Returns:
4494  * Zero on success, negative errno on failure.
4495  */
4496 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4497                              void *data, struct drm_file *file_priv)
4498 {
4499         struct drm_mode_crtc_lut *crtc_lut = data;
4500         struct drm_crtc *crtc;
4501         void *r_base, *g_base, *b_base;
4502         int size;
4503         int ret = 0;
4504
4505         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4506                 return -EINVAL;
4507
4508         drm_modeset_lock_all(dev);
4509         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4510         if (!crtc) {
4511                 ret = -ENOENT;
4512                 goto out;
4513         }
4514
4515         if (crtc->funcs->gamma_set == NULL) {
4516                 ret = -ENOSYS;
4517                 goto out;
4518         }
4519
4520         /* memcpy into gamma store */
4521         if (crtc_lut->gamma_size != crtc->gamma_size) {
4522                 ret = -EINVAL;
4523                 goto out;
4524         }
4525
4526         size = crtc_lut->gamma_size * (sizeof(uint16_t));
4527         r_base = crtc->gamma_store;
4528         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4529                 ret = -EFAULT;
4530                 goto out;
4531         }
4532
4533         g_base = r_base + size;
4534         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4535                 ret = -EFAULT;
4536                 goto out;
4537         }
4538
4539         b_base = g_base + size;
4540         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4541                 ret = -EFAULT;
4542                 goto out;
4543         }
4544
4545         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4546
4547 out:
4548         drm_modeset_unlock_all(dev);
4549         return ret;
4550
4551 }
4552
4553 /**
4554  * drm_mode_gamma_get_ioctl - get the gamma table
4555  * @dev: DRM device
4556  * @data: ioctl data
4557  * @file_priv: DRM file info
4558  *
4559  * Copy the current gamma table into the storage provided. This also provides
4560  * the gamma table size the driver expects, which can be used to size the
4561  * allocated storage.
4562  *
4563  * Called by the user via ioctl.
4564  *
4565  * Returns:
4566  * Zero on success, negative errno on failure.
4567  */
4568 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4569                              void *data, struct drm_file *file_priv)
4570 {
4571         struct drm_mode_crtc_lut *crtc_lut = data;
4572         struct drm_crtc *crtc;
4573         void *r_base, *g_base, *b_base;
4574         int size;
4575         int ret = 0;
4576
4577         if (!drm_core_check_feature(dev, DRIVER_MODESET))
4578                 return -EINVAL;
4579
4580         drm_modeset_lock_all(dev);
4581         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4582         if (!crtc) {
4583                 ret = -ENOENT;
4584                 goto out;
4585         }
4586
4587         /* memcpy into gamma store */
4588         if (crtc_lut->gamma_size != crtc->gamma_size) {
4589                 ret = -EINVAL;
4590                 goto out;
4591         }
4592
4593         size = crtc_lut->gamma_size * (sizeof(uint16_t));
4594         r_base = crtc->gamma_store;
4595         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4596                 ret = -EFAULT;
4597                 goto out;
4598         }
4599
4600         g_base = r_base + size;
4601         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4602                 ret = -EFAULT;
4603                 goto out;
4604         }
4605
4606         b_base = g_base + size;
4607         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4608                 ret = -EFAULT;
4609                 goto out;
4610         }
4611 out:
4612         drm_modeset_unlock_all(dev);
4613         return ret;
4614 }
4615
4616 /**
4617  * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4618  * @dev: DRM device
4619  * @data: ioctl data
4620  * @file_priv: DRM file info
4621  *
4622  * This schedules an asynchronous update on a given CRTC, called page flip.
4623  * Optionally a drm event is generated to signal the completion of the event.
4624  * Generic drivers cannot assume that a pageflip with changed framebuffer
4625  * properties (including driver specific metadata like tiling layout) will work,
4626  * but some drivers support e.g. pixel format changes through the pageflip
4627  * ioctl.
4628  *
4629  * Called by the user via ioctl.
4630  *
4631  * Returns:
4632  * Zero on success, negative errno on failure.
4633  */
4634 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4635                              void *data, struct drm_file *file_priv)
4636 {
4637         struct drm_mode_crtc_page_flip *page_flip = data;
4638         struct drm_crtc *crtc;
4639         struct drm_framebuffer *fb = NULL;
4640         struct drm_pending_vblank_event *e = NULL;
4641         unsigned long flags;
4642         int ret = -EINVAL;
4643
4644         if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4645             page_flip->reserved != 0)
4646                 return -EINVAL;
4647
4648         if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4649                 return -EINVAL;
4650
4651         crtc = drm_crtc_find(dev, page_flip->crtc_id);
4652         if (!crtc)
4653                 return -ENOENT;
4654
4655         drm_modeset_lock_crtc(crtc, crtc->primary);
4656         if (crtc->primary->fb == NULL) {
4657                 /* The framebuffer is currently unbound, presumably
4658                  * due to a hotplug event, that userspace has not
4659                  * yet discovered.
4660                  */
4661                 ret = -EBUSY;
4662                 goto out;
4663         }
4664
4665         if (crtc->funcs->page_flip == NULL)
4666                 goto out;
4667
4668         fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4669         if (!fb) {
4670                 ret = -ENOENT;
4671                 goto out;
4672         }
4673
4674         ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4675         if (ret)
4676                 goto out;
4677
4678         if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4679                 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4680                 ret = -EINVAL;
4681                 goto out;
4682         }
4683
4684         if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4685                 ret = -ENOMEM;
4686                 spin_lock_irqsave(&dev->event_lock, flags);
4687                 if (file_priv->event_space < sizeof e->event) {
4688                         spin_unlock_irqrestore(&dev->event_lock, flags);
4689                         goto out;
4690                 }
4691                 file_priv->event_space -= sizeof e->event;
4692                 spin_unlock_irqrestore(&dev->event_lock, flags);
4693
4694                 e = kzalloc(sizeof *e, GFP_KERNEL);
4695                 if (e == NULL) {
4696                         spin_lock_irqsave(&dev->event_lock, flags);
4697                         file_priv->event_space += sizeof e->event;
4698                         spin_unlock_irqrestore(&dev->event_lock, flags);
4699                         goto out;
4700                 }
4701
4702                 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4703                 e->event.base.length = sizeof e->event;
4704                 e->event.user_data = page_flip->user_data;
4705                 e->base.event = &e->event.base;
4706                 e->base.file_priv = file_priv;
4707                 e->base.destroy =
4708                         (void (*) (struct drm_pending_event *)) kfree;
4709         }
4710
4711         crtc->primary->old_fb = crtc->primary->fb;
4712         ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4713         if (ret) {
4714                 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4715                         spin_lock_irqsave(&dev->event_lock, flags);
4716                         file_priv->event_space += sizeof e->event;
4717                         spin_unlock_irqrestore(&dev->event_lock, flags);
4718                         kfree(e);
4719                 }
4720                 /* Keep the old fb, don't unref it. */
4721                 crtc->primary->old_fb = NULL;
4722         } else {
4723                 /*
4724                  * Warn if the driver hasn't properly updated the crtc->fb
4725                  * field to reflect that the new framebuffer is now used.
4726                  * Failing to do so will screw with the reference counting
4727                  * on framebuffers.
4728                  */
4729                 WARN_ON(crtc->primary->fb != fb);
4730                 /* Unref only the old framebuffer. */
4731                 fb = NULL;
4732         }
4733
4734 out:
4735         if (fb)
4736                 drm_framebuffer_unreference(fb);
4737         if (crtc->primary->old_fb)
4738                 drm_framebuffer_unreference(crtc->primary->old_fb);
4739         crtc->primary->old_fb = NULL;
4740         drm_modeset_unlock_crtc(crtc);
4741
4742         return ret;
4743 }
4744
4745 /**
4746  * drm_mode_config_reset - call ->reset callbacks
4747  * @dev: drm device
4748  *
4749  * This functions calls all the crtc's, encoder's and connector's ->reset
4750  * callback. Drivers can use this in e.g. their driver load or resume code to
4751  * reset hardware and software state.
4752  */
4753 void drm_mode_config_reset(struct drm_device *dev)
4754 {
4755         struct drm_crtc *crtc;
4756         struct drm_plane *plane;
4757         struct drm_encoder *encoder;
4758         struct drm_connector *connector;
4759
4760         list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4761                 if (plane->funcs->reset)
4762                         plane->funcs->reset(plane);
4763
4764         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4765                 if (crtc->funcs->reset)
4766                         crtc->funcs->reset(crtc);
4767
4768         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4769                 if (encoder->funcs->reset)
4770                         encoder->funcs->reset(encoder);
4771
4772         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4773                 connector->status = connector_status_unknown;
4774
4775                 if (connector->funcs->reset)
4776                         connector->funcs->reset(connector);
4777         }
4778 }
4779 EXPORT_SYMBOL(drm_mode_config_reset);
4780
4781 /**
4782  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4783  * @dev: DRM device
4784  * @data: ioctl data
4785  * @file_priv: DRM file info
4786  *
4787  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4788  * TTM or something else entirely) and returns the resulting buffer handle. This
4789  * handle can then be wrapped up into a framebuffer modeset object.
4790  *
4791  * Note that userspace is not allowed to use such objects for render
4792  * acceleration - drivers must create their own private ioctls for such a use
4793  * case.
4794  *
4795  * Called by the user via ioctl.
4796  *
4797  * Returns:
4798  * Zero on success, negative errno on failure.
4799  */
4800 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4801                                void *data, struct drm_file *file_priv)
4802 {
4803         struct drm_mode_create_dumb *args = data;
4804         u32 cpp, stride, size;
4805
4806         if (!dev->driver->dumb_create)
4807                 return -ENOSYS;
4808         if (!args->width || !args->height || !args->bpp)
4809                 return -EINVAL;
4810
4811         /* overflow checks for 32bit size calculations */
4812         /* NOTE: DIV_ROUND_UP() can overflow */
4813         cpp = DIV_ROUND_UP(args->bpp, 8);
4814         if (!cpp || cpp > 0xffffffffU / args->width)
4815                 return -EINVAL;
4816         stride = cpp * args->width;
4817         if (args->height > 0xffffffffU / stride)
4818                 return -EINVAL;
4819
4820         /* test for wrap-around */
4821         size = args->height * stride;
4822         if (PAGE_ALIGN(size) == 0)
4823                 return -EINVAL;
4824
4825         /*
4826          * handle, pitch and size are output parameters. Zero them out to
4827          * prevent drivers from accidentally using uninitialized data. Since
4828          * not all existing userspace is clearing these fields properly we
4829          * cannot reject IOCTL with garbage in them.
4830          */
4831         args->handle = 0;
4832         args->pitch = 0;
4833         args->size = 0;
4834
4835         return dev->driver->dumb_create(file_priv, dev, args);
4836 }
4837
4838 /**
4839  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4840  * @dev: DRM device
4841  * @data: ioctl data
4842  * @file_priv: DRM file info
4843  *
4844  * Allocate an offset in the drm device node's address space to be able to
4845  * memory map a dumb buffer.
4846  *
4847  * Called by the user via ioctl.
4848  *
4849  * Returns:
4850  * Zero on success, negative errno on failure.
4851  */
4852 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4853                              void *data, struct drm_file *file_priv)
4854 {
4855         struct drm_mode_map_dumb *args = data;
4856
4857         /* call driver ioctl to get mmap offset */
4858         if (!dev->driver->dumb_map_offset)
4859                 return -ENOSYS;
4860
4861         return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4862 }
4863
4864 /**
4865  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4866  * @dev: DRM device
4867  * @data: ioctl data
4868  * @file_priv: DRM file info
4869  *
4870  * This destroys the userspace handle for the given dumb backing storage buffer.
4871  * Since buffer objects must be reference counted in the kernel a buffer object
4872  * won't be immediately freed if a framebuffer modeset object still uses it.
4873  *
4874  * Called by the user via ioctl.
4875  *
4876  * Returns:
4877  * Zero on success, negative errno on failure.
4878  */
4879 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4880                                 void *data, struct drm_file *file_priv)
4881 {
4882         struct drm_mode_destroy_dumb *args = data;
4883
4884         if (!dev->driver->dumb_destroy)
4885                 return -ENOSYS;
4886
4887         return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4888 }
4889
4890 /**
4891  * drm_fb_get_bpp_depth - get the bpp/depth values for format
4892  * @format: pixel format (DRM_FORMAT_*)
4893  * @depth: storage for the depth value
4894  * @bpp: storage for the bpp value
4895  *
4896  * This only supports RGB formats here for compat with code that doesn't use
4897  * pixel formats directly yet.
4898  */
4899 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4900                           int *bpp)
4901 {
4902         switch (format) {
4903         case DRM_FORMAT_C8:
4904         case DRM_FORMAT_RGB332:
4905         case DRM_FORMAT_BGR233:
4906                 *depth = 8;
4907                 *bpp = 8;
4908                 break;
4909         case DRM_FORMAT_XRGB1555:
4910         case DRM_FORMAT_XBGR1555:
4911         case DRM_FORMAT_RGBX5551:
4912         case DRM_FORMAT_BGRX5551:
4913         case DRM_FORMAT_ARGB1555:
4914         case DRM_FORMAT_ABGR1555:
4915         case DRM_FORMAT_RGBA5551:
4916         case DRM_FORMAT_BGRA5551:
4917                 *depth = 15;
4918                 *bpp = 16;
4919                 break;
4920         case DRM_FORMAT_RGB565:
4921         case DRM_FORMAT_BGR565:
4922                 *depth = 16;
4923                 *bpp = 16;
4924                 break;
4925         case DRM_FORMAT_RGB888:
4926         case DRM_FORMAT_BGR888:
4927                 *depth = 24;
4928                 *bpp = 24;
4929                 break;
4930         case DRM_FORMAT_XRGB8888:
4931         case DRM_FORMAT_XBGR8888:
4932         case DRM_FORMAT_RGBX8888:
4933         case DRM_FORMAT_BGRX8888:
4934                 *depth = 24;
4935                 *bpp = 32;
4936                 break;
4937         case DRM_FORMAT_XRGB2101010:
4938         case DRM_FORMAT_XBGR2101010:
4939         case DRM_FORMAT_RGBX1010102:
4940         case DRM_FORMAT_BGRX1010102:
4941         case DRM_FORMAT_ARGB2101010:
4942         case DRM_FORMAT_ABGR2101010:
4943         case DRM_FORMAT_RGBA1010102:
4944         case DRM_FORMAT_BGRA1010102:
4945                 *depth = 30;
4946                 *bpp = 32;
4947                 break;
4948         case DRM_FORMAT_ARGB8888:
4949         case DRM_FORMAT_ABGR8888:
4950         case DRM_FORMAT_RGBA8888:
4951         case DRM_FORMAT_BGRA8888:
4952                 *depth = 32;
4953                 *bpp = 32;
4954                 break;
4955         default:
4956                 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4957                               drm_get_format_name(format));
4958                 *depth = 0;
4959                 *bpp = 0;
4960                 break;
4961         }
4962 }
4963 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4964
4965 /**
4966  * drm_format_num_planes - get the number of planes for format
4967  * @format: pixel format (DRM_FORMAT_*)
4968  *
4969  * Returns:
4970  * The number of planes used by the specified pixel format.
4971  */
4972 int drm_format_num_planes(uint32_t format)
4973 {
4974         switch (format) {
4975         case DRM_FORMAT_YUV410:
4976         case DRM_FORMAT_YVU410:
4977         case DRM_FORMAT_YUV411:
4978         case DRM_FORMAT_YVU411:
4979         case DRM_FORMAT_YUV420:
4980         case DRM_FORMAT_YVU420:
4981         case DRM_FORMAT_YUV422:
4982         case DRM_FORMAT_YVU422:
4983         case DRM_FORMAT_YUV444:
4984         case DRM_FORMAT_YVU444:
4985                 return 3;
4986         case DRM_FORMAT_NV12:
4987         case DRM_FORMAT_NV21:
4988         case DRM_FORMAT_NV16:
4989         case DRM_FORMAT_NV61:
4990         case DRM_FORMAT_NV24:
4991         case DRM_FORMAT_NV42:
4992                 return 2;
4993         default:
4994                 return 1;
4995         }
4996 }
4997 EXPORT_SYMBOL(drm_format_num_planes);
4998
4999 /**
5000  * drm_format_plane_cpp - determine the bytes per pixel value
5001  * @format: pixel format (DRM_FORMAT_*)
5002  * @plane: plane index
5003  *
5004  * Returns:
5005  * The bytes per pixel value for the specified plane.
5006  */
5007 int drm_format_plane_cpp(uint32_t format, int plane)
5008 {
5009         unsigned int depth;
5010         int bpp;
5011
5012         if (plane >= drm_format_num_planes(format))
5013                 return 0;
5014
5015         switch (format) {
5016         case DRM_FORMAT_YUYV:
5017         case DRM_FORMAT_YVYU:
5018         case DRM_FORMAT_UYVY:
5019         case DRM_FORMAT_VYUY:
5020                 return 2;
5021         case DRM_FORMAT_NV12:
5022         case DRM_FORMAT_NV21:
5023         case DRM_FORMAT_NV16:
5024         case DRM_FORMAT_NV61:
5025         case DRM_FORMAT_NV24:
5026         case DRM_FORMAT_NV42:
5027                 return plane ? 2 : 1;
5028         case DRM_FORMAT_YUV410:
5029         case DRM_FORMAT_YVU410:
5030         case DRM_FORMAT_YUV411:
5031         case DRM_FORMAT_YVU411:
5032         case DRM_FORMAT_YUV420:
5033         case DRM_FORMAT_YVU420:
5034         case DRM_FORMAT_YUV422:
5035         case DRM_FORMAT_YVU422:
5036         case DRM_FORMAT_YUV444:
5037         case DRM_FORMAT_YVU444:
5038                 return 1;
5039         default:
5040                 drm_fb_get_bpp_depth(format, &depth, &bpp);
5041                 return bpp >> 3;
5042         }
5043 }
5044 EXPORT_SYMBOL(drm_format_plane_cpp);
5045
5046 /**
5047  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5048  * @format: pixel format (DRM_FORMAT_*)
5049  *
5050  * Returns:
5051  * The horizontal chroma subsampling factor for the
5052  * specified pixel format.
5053  */
5054 int drm_format_horz_chroma_subsampling(uint32_t format)
5055 {
5056         switch (format) {
5057         case DRM_FORMAT_YUV411:
5058         case DRM_FORMAT_YVU411:
5059         case DRM_FORMAT_YUV410:
5060         case DRM_FORMAT_YVU410:
5061                 return 4;
5062         case DRM_FORMAT_YUYV:
5063         case DRM_FORMAT_YVYU:
5064         case DRM_FORMAT_UYVY:
5065         case DRM_FORMAT_VYUY:
5066         case DRM_FORMAT_NV12:
5067         case DRM_FORMAT_NV21:
5068         case DRM_FORMAT_NV16:
5069         case DRM_FORMAT_NV61:
5070         case DRM_FORMAT_YUV422:
5071         case DRM_FORMAT_YVU422:
5072         case DRM_FORMAT_YUV420:
5073         case DRM_FORMAT_YVU420:
5074                 return 2;
5075         default:
5076                 return 1;
5077         }
5078 }
5079 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5080
5081 /**
5082  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5083  * @format: pixel format (DRM_FORMAT_*)
5084  *
5085  * Returns:
5086  * The vertical chroma subsampling factor for the
5087  * specified pixel format.
5088  */
5089 int drm_format_vert_chroma_subsampling(uint32_t format)
5090 {
5091         switch (format) {
5092         case DRM_FORMAT_YUV410:
5093         case DRM_FORMAT_YVU410:
5094                 return 4;
5095         case DRM_FORMAT_YUV420:
5096         case DRM_FORMAT_YVU420:
5097         case DRM_FORMAT_NV12:
5098         case DRM_FORMAT_NV21:
5099                 return 2;
5100         default:
5101                 return 1;
5102         }
5103 }
5104 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5105
5106 /**
5107  * drm_rotation_simplify() - Try to simplify the rotation
5108  * @rotation: Rotation to be simplified
5109  * @supported_rotations: Supported rotations
5110  *
5111  * Attempt to simplify the rotation to a form that is supported.
5112  * Eg. if the hardware supports everything except DRM_REFLECT_X
5113  * one could call this function like this:
5114  *
5115  * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5116  *                       BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5117  *                       BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5118  *
5119  * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5120  * transforms the hardware supports, this function may not
5121  * be able to produce a supported transform, so the caller should
5122  * check the result afterwards.
5123  */
5124 unsigned int drm_rotation_simplify(unsigned int rotation,
5125                                    unsigned int supported_rotations)
5126 {
5127         if (rotation & ~supported_rotations) {
5128                 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5129                 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5130         }
5131
5132         return rotation;
5133 }
5134 EXPORT_SYMBOL(drm_rotation_simplify);
5135
5136 /**
5137  * drm_mode_config_init - initialize DRM mode_configuration structure
5138  * @dev: DRM device
5139  *
5140  * Initialize @dev's mode_config structure, used for tracking the graphics
5141  * configuration of @dev.
5142  *
5143  * Since this initializes the modeset locks, no locking is possible. Which is no
5144  * problem, since this should happen single threaded at init time. It is the
5145  * driver's problem to ensure this guarantee.
5146  *
5147  */
5148 void drm_mode_config_init(struct drm_device *dev)
5149 {
5150         mutex_init(&dev->mode_config.mutex);
5151         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5152         mutex_init(&dev->mode_config.idr_mutex);
5153         mutex_init(&dev->mode_config.fb_lock);
5154         INIT_LIST_HEAD(&dev->mode_config.fb_list);
5155         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5156         INIT_LIST_HEAD(&dev->mode_config.connector_list);
5157         INIT_LIST_HEAD(&dev->mode_config.bridge_list);
5158         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5159         INIT_LIST_HEAD(&dev->mode_config.property_list);
5160         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5161         INIT_LIST_HEAD(&dev->mode_config.plane_list);
5162         idr_init(&dev->mode_config.crtc_idr);
5163
5164         drm_modeset_lock_all(dev);
5165         drm_mode_create_standard_connector_properties(dev);
5166         drm_mode_create_standard_plane_properties(dev);
5167         drm_modeset_unlock_all(dev);
5168
5169         /* Just to be sure */
5170         dev->mode_config.num_fb = 0;
5171         dev->mode_config.num_connector = 0;
5172         dev->mode_config.num_crtc = 0;
5173         dev->mode_config.num_encoder = 0;
5174         dev->mode_config.num_overlay_plane = 0;
5175         dev->mode_config.num_total_plane = 0;
5176 }
5177 EXPORT_SYMBOL(drm_mode_config_init);
5178
5179 /**
5180  * drm_mode_config_cleanup - free up DRM mode_config info
5181  * @dev: DRM device
5182  *
5183  * Free up all the connectors and CRTCs associated with this DRM device, then
5184  * free up the framebuffers and associated buffer objects.
5185  *
5186  * Note that since this /should/ happen single-threaded at driver/device
5187  * teardown time, no locking is required. It's the driver's job to ensure that
5188  * this guarantee actually holds true.
5189  *
5190  * FIXME: cleanup any dangling user buffer objects too
5191  */
5192 void drm_mode_config_cleanup(struct drm_device *dev)
5193 {
5194         struct drm_connector *connector, *ot;
5195         struct drm_crtc *crtc, *ct;
5196         struct drm_encoder *encoder, *enct;
5197         struct drm_bridge *bridge, *brt;
5198         struct drm_framebuffer *fb, *fbt;
5199         struct drm_property *property, *pt;
5200         struct drm_property_blob *blob, *bt;
5201         struct drm_plane *plane, *plt;
5202
5203         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5204                                  head) {
5205                 encoder->funcs->destroy(encoder);
5206         }
5207
5208         list_for_each_entry_safe(bridge, brt,
5209                                  &dev->mode_config.bridge_list, head) {
5210                 bridge->funcs->destroy(bridge);
5211         }
5212
5213         list_for_each_entry_safe(connector, ot,
5214                                  &dev->mode_config.connector_list, head) {
5215                 connector->funcs->destroy(connector);
5216         }
5217
5218         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5219                                  head) {
5220                 drm_property_destroy(dev, property);
5221         }
5222
5223         list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5224                                  head) {
5225                 drm_property_destroy_blob(dev, blob);
5226         }
5227
5228         /*
5229          * Single-threaded teardown context, so it's not required to grab the
5230          * fb_lock to protect against concurrent fb_list access. Contrary, it
5231          * would actually deadlock with the drm_framebuffer_cleanup function.
5232          *
5233          * Also, if there are any framebuffers left, that's a driver leak now,
5234          * so politely WARN about this.
5235          */
5236         WARN_ON(!list_empty(&dev->mode_config.fb_list));
5237         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5238                 drm_framebuffer_remove(fb);
5239         }
5240
5241         list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5242                                  head) {
5243                 plane->funcs->destroy(plane);
5244         }
5245
5246         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5247                 crtc->funcs->destroy(crtc);
5248         }
5249
5250         idr_destroy(&dev->mode_config.crtc_idr);
5251         drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5252 }
5253 EXPORT_SYMBOL(drm_mode_config_cleanup);
5254
5255 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5256                                                        unsigned int supported_rotations)
5257 {
5258         static const struct drm_prop_enum_list props[] = {
5259                 { DRM_ROTATE_0,   "rotate-0" },
5260                 { DRM_ROTATE_90,  "rotate-90" },
5261                 { DRM_ROTATE_180, "rotate-180" },
5262                 { DRM_ROTATE_270, "rotate-270" },
5263                 { DRM_REFLECT_X,  "reflect-x" },
5264                 { DRM_REFLECT_Y,  "reflect-y" },
5265         };
5266
5267         return drm_property_create_bitmask(dev, 0, "rotation",
5268                                            props, ARRAY_SIZE(props),
5269                                            supported_rotations);
5270 }
5271 EXPORT_SYMBOL(drm_mode_create_rotation_property);