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