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