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