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