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