blob: 13f7b13a6f49caa580f397b24c225b41e2a22742 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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 */
Ville Syrjälä6ba6d032013-06-10 11:15:10 +030032#include <linux/ctype.h>
Dave Airlief453ba02008-11-07 14:05:41 -080033#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040035#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
Rob Clark51fd3712013-11-19 12:10:12 -050040#include <drm/drm_modeset_lock.h>
Rob Clark88a48e22014-12-18 16:01:50 -050041#include <drm/drm_atomic.h>
Daniel Vetter3b96a0b2016-06-21 10:54:22 +020042#include <drm/drm_auth.h>
Daniel Vetter7520a272016-08-15 16:07:02 +020043#include <drm/drm_framebuffer.h>
Dave Airlief453ba02008-11-07 14:05:41 -080044
Daniel Vetter8bd441b2014-01-23 15:35:24 +010045#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020046#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010047
Dave Airlief453ba02008-11-07 14:05:41 -080048/*
49 * Global properties
50 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010051static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040052 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
55};
56
Thierry Reding4dfd9092014-12-10 13:03:34 +010057static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
58 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -080059 { DRM_MODE_ENCODER_DAC, "DAC" },
60 { DRM_MODE_ENCODER_TMDS, "TMDS" },
61 { DRM_MODE_ENCODER_LVDS, "LVDS" },
62 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +020063 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +030064 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +100065 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Eric Anholt0b27c022016-03-18 12:34:59 -070066 { DRM_MODE_ENCODER_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -080067};
68
Daniel Vetter52217192016-08-12 22:48:50 +020069/*
70 * Optional properties
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010071 */
Dave Airlie2ee39452014-07-24 11:53:45 +100072/*
73 * Internal function to assign a slot in the object idr and optionally
74 * register the object into the idr.
75 */
Daniel Vetter7520a272016-08-15 16:07:02 +020076int drm_mode_object_get_reg(struct drm_device *dev,
77 struct drm_mode_object *obj,
78 uint32_t obj_type,
79 bool register_obj,
80 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +100081{
82 int ret;
83
84 mutex_lock(&dev->mode_config.idr_mutex);
85 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
86 if (ret >= 0) {
87 /*
88 * Set up the object linking under the protection of the idr
89 * lock so that other users can't see inconsistent state.
90 */
91 obj->id = ret;
92 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +100093 if (obj_free_cb) {
94 obj->free_cb = obj_free_cb;
95 kref_init(&obj->refcount);
96 }
Dave Airlie2ee39452014-07-24 11:53:45 +100097 }
98 mutex_unlock(&dev->mode_config.idr_mutex);
99
100 return ret < 0 ? ret : 0;
101}
102
Dave Airlief453ba02008-11-07 14:05:41 -0800103/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100104 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800105 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100106 * @obj: object pointer, used to generate unique ID
107 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800108 *
Dave Airlief453ba02008-11-07 14:05:41 -0800109 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100110 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
111 * modeset identifiers are _not_ reference counted. Hence don't use this for
112 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800113 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100114 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200115 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800116 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100117int drm_mode_object_get(struct drm_device *dev,
118 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800119{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000120 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000121}
Dave Airlief453ba02008-11-07 14:05:41 -0800122
Daniel Vetter7520a272016-08-15 16:07:02 +0200123void drm_mode_object_register(struct drm_device *dev,
124 struct drm_mode_object *obj)
Dave Airlie2ee39452014-07-24 11:53:45 +1000125{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000126 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000127 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000128 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800129}
130
131/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000132 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800133 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100134 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800135 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000136 * Free @id from @dev's unique identifier pool.
137 * This function can be called multiple times, and guards against
138 * multiple removals.
139 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100140 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800141 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000142void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100143 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800144{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000145 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000146 if (object->id) {
147 idr_remove(&dev->mode_config.crtc_idr, object->id);
148 object->id = 0;
149 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000150 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800151}
152
Daniel Vetter7520a272016-08-15 16:07:02 +0200153struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
154 uint32_t id, uint32_t type)
Rob Clark98f75de2014-05-30 11:37:03 -0400155{
156 struct drm_mode_object *obj = NULL;
157
158 mutex_lock(&dev->mode_config.idr_mutex);
159 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200160 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
161 obj = NULL;
162 if (obj && obj->id != id)
163 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000164
165 if (obj && obj->free_cb) {
166 if (!kref_get_unless_zero(&obj->refcount))
167 obj = NULL;
168 }
Rob Clark98f75de2014-05-30 11:37:03 -0400169 mutex_unlock(&dev->mode_config.idr_mutex);
170
171 return obj;
172}
173
Daniel Vetter786b99e2012-12-02 21:53:40 +0100174/**
175 * drm_mode_object_find - look up a drm object with static lifetime
176 * @dev: drm device
177 * @id: id of the mode object
178 * @type: type of the mode object
179 *
Daniel Vetter059814222016-04-22 22:10:27 +0200180 * This function is used to look up a modeset object. It will acquire a
181 * reference for reference counted objects. This reference must be dropped again
182 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100183 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200184struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
185 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800186{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000187 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800188
Daniel Vetter7520a272016-08-15 16:07:02 +0200189 obj = __drm_mode_object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800190 return obj;
191}
192EXPORT_SYMBOL(drm_mode_object_find);
193
Daniel Vetter059814222016-04-22 22:10:27 +0200194/**
195 * drm_mode_object_unreference - decr the object refcnt
196 * @obj: mode_object
197 *
198 * This functions decrements the object's refcount if it is a refcounted modeset
199 * object. It is a no-op on any other object. This is used to drop references
200 * acquired with drm_mode_object_reference().
201 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000202void drm_mode_object_unreference(struct drm_mode_object *obj)
203{
204 if (obj->free_cb) {
205 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
206 kref_put(&obj->refcount, obj->free_cb);
207 }
208}
209EXPORT_SYMBOL(drm_mode_object_unreference);
210
211/**
Daniel Vetter059814222016-04-22 22:10:27 +0200212 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000213 * @obj: mode_object
214 *
Daniel Vetter059814222016-04-22 22:10:27 +0200215 * This functions increments the object's refcount if it is a refcounted modeset
216 * object. It is a no-op on any other object. References should be dropped again
217 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000218 */
219void drm_mode_object_reference(struct drm_mode_object *obj)
220{
221 if (obj->free_cb) {
222 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
223 kref_get(&obj->refcount);
224 }
225}
226EXPORT_SYMBOL(drm_mode_object_reference);
227
Lukas Wunner6a0d9522016-06-08 18:47:27 +0200228/**
229 * drm_crtc_force_disable - Forcibly turn off a CRTC
230 * @crtc: CRTC to turn off
231 *
232 * Returns:
233 * Zero on success, error code on failure.
234 */
235int drm_crtc_force_disable(struct drm_crtc *crtc)
236{
237 struct drm_mode_set set = {
238 .crtc = crtc,
239 };
240
241 return drm_mode_set_config_internal(&set);
242}
243EXPORT_SYMBOL(drm_crtc_force_disable);
244
245/**
246 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
247 * @dev: DRM device whose CRTCs to turn off
248 *
249 * Drivers may want to call this on unload to ensure that all displays are
250 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
251 *
252 * Returns:
253 * Zero on success, error code on failure.
254 */
255int drm_crtc_force_disable_all(struct drm_device *dev)
256{
257 struct drm_crtc *crtc;
258 int ret = 0;
259
260 drm_modeset_lock_all(dev);
261 drm_for_each_crtc(crtc, dev)
262 if (crtc->enabled) {
263 ret = drm_crtc_force_disable(crtc);
264 if (ret)
265 goto out;
266 }
267out:
268 drm_modeset_unlock_all(dev);
269 return ret;
270}
271EXPORT_SYMBOL(drm_crtc_force_disable_all);
272
Rob Clark51fd3712013-11-19 12:10:12 -0500273DEFINE_WW_CLASS(crtc_ww_class);
274
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200275static unsigned int drm_num_crtcs(struct drm_device *dev)
276{
277 unsigned int num = 0;
278 struct drm_crtc *tmp;
279
280 drm_for_each_crtc(tmp, dev) {
281 num++;
282 }
283
284 return num;
285}
286
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200287static int drm_crtc_register_all(struct drm_device *dev)
288{
289 struct drm_crtc *crtc;
290 int ret = 0;
291
292 drm_for_each_crtc(crtc, dev) {
293 if (crtc->funcs->late_register)
294 ret = crtc->funcs->late_register(crtc);
295 if (ret)
296 return ret;
297 }
298
299 return 0;
300}
301
302static void drm_crtc_unregister_all(struct drm_device *dev)
303{
304 struct drm_crtc *crtc;
305
306 drm_for_each_crtc(crtc, dev) {
307 if (crtc->funcs->early_unregister)
308 crtc->funcs->early_unregister(crtc);
309 }
310}
311
Dave Airlief453ba02008-11-07 14:05:41 -0800312/**
Matt Ropere13161a2014-04-01 15:22:38 -0700313 * drm_crtc_init_with_planes - Initialise a new CRTC object with
314 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800315 * @dev: DRM device
316 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700317 * @primary: Primary plane for CRTC
318 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800319 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200320 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800321 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000322 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200323 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100324 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200325 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800326 */
Matt Ropere13161a2014-04-01 15:22:38 -0700327int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
328 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700329 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200330 const struct drm_crtc_funcs *funcs,
331 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800332{
Rob Clark51fd3712013-11-19 12:10:12 -0500333 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200334 int ret;
335
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100336 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
337 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
338
Dave Airlief453ba02008-11-07 14:05:41 -0800339 crtc->dev = dev;
340 crtc->funcs = funcs;
341
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200342 INIT_LIST_HEAD(&crtc->commit_list);
343 spin_lock_init(&crtc->commit_lock);
344
Rob Clark51fd3712013-11-19 12:10:12 -0500345 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200346 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
347 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100348 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800349
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200350 if (name) {
351 va_list ap;
352
353 va_start(ap, name);
354 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
355 va_end(ap);
356 } else {
357 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
358 drm_num_crtcs(dev));
359 }
360 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000361 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200362 return -ENOMEM;
363 }
364
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300365 crtc->base.properties = &crtc->properties;
366
Rob Clark51fd3712013-11-19 12:10:12 -0500367 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100368 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200369
Matt Ropere13161a2014-04-01 15:22:38 -0700370 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700371 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700372 if (primary)
373 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700374 if (cursor)
375 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700376
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100377 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
378 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100379 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100380 }
381
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100382 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800383}
Matt Ropere13161a2014-04-01 15:22:38 -0700384EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800385
386/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000387 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800388 * @crtc: CRTC to cleanup
389 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000390 * This function cleans up @crtc and removes it from the DRM mode setting
391 * core. Note that the function does *not* free the crtc structure itself,
392 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800393 */
394void drm_crtc_cleanup(struct drm_crtc *crtc)
395{
396 struct drm_device *dev = crtc->dev;
397
Chris Wilson490d3d12016-05-27 20:05:00 +0100398 /* Note that the crtc_list is considered to be static; should we
399 * remove the drm_crtc at runtime we would have to decrement all
400 * the indices on the drm_crtc after us in the crtc_list.
401 */
402
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000403 kfree(crtc->gamma_store);
404 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800405
Rob Clark51fd3712013-11-19 12:10:12 -0500406 drm_modeset_lock_fini(&crtc->mutex);
407
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000408 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800409 list_del(&crtc->head);
410 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100411
412 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
413 if (crtc->state && crtc->funcs->atomic_destroy_state)
414 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100415
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200416 kfree(crtc->name);
417
Thierry Redinga18c0af2014-12-10 11:38:49 +0100418 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800419}
420EXPORT_SYMBOL(drm_crtc_cleanup);
421
Dave Airlief453ba02008-11-07 14:05:41 -0800422/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200423 * drm_display_info_set_bus_formats - set the supported bus formats
424 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100425 * @formats: array containing the supported bus formats
426 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200427 *
428 * Store the supported bus formats in display info structure.
429 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
430 * a full list of available formats.
431 */
432int drm_display_info_set_bus_formats(struct drm_display_info *info,
433 const u32 *formats,
434 unsigned int num_formats)
435{
436 u32 *fmts = NULL;
437
438 if (!formats && num_formats)
439 return -EINVAL;
440
441 if (formats && num_formats) {
442 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
443 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300444 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200445 return -ENOMEM;
446 }
447
448 kfree(info->bus_formats);
449 info->bus_formats = fmts;
450 info->num_bus_formats = num_formats;
451
452 return 0;
453}
454EXPORT_SYMBOL(drm_display_info_set_bus_formats);
455
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200456static int drm_encoder_register_all(struct drm_device *dev)
457{
458 struct drm_encoder *encoder;
459 int ret = 0;
460
461 drm_for_each_encoder(encoder, dev) {
462 if (encoder->funcs->late_register)
463 ret = encoder->funcs->late_register(encoder);
464 if (ret)
465 return ret;
466 }
467
468 return 0;
469}
470
471static void drm_encoder_unregister_all(struct drm_device *dev)
472{
473 struct drm_encoder *encoder;
474
475 drm_for_each_encoder(encoder, dev) {
476 if (encoder->funcs->early_unregister)
477 encoder->funcs->early_unregister(encoder);
478 }
479}
480
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100481/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100482 * drm_encoder_init - Init a preallocated encoder
483 * @dev: drm device
484 * @encoder: the encoder to init
485 * @funcs: callbacks for this encoder
486 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200487 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100488 *
489 * Initialises a preallocated encoder. Encoder should be
490 * subclassed as part of driver encoder objects.
491 *
492 * Returns:
493 * Zero on success, error code on failure.
494 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200495int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000496 struct drm_encoder *encoder,
497 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200498 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800499{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200500 int ret;
501
Daniel Vetter84849902012-12-02 00:28:11 +0100502 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800503
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200504 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
505 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +0300506 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800507
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200508 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800509 encoder->encoder_type = encoder_type;
510 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +0200511 if (name) {
512 va_list ap;
513
514 va_start(ap, name);
515 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
516 va_end(ap);
517 } else {
518 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
519 drm_encoder_enum_list[encoder_type].name,
520 encoder->base.id);
521 }
Jani Nikulae5748942014-05-14 16:58:20 +0300522 if (!encoder->name) {
523 ret = -ENOMEM;
524 goto out_put;
525 }
Dave Airlief453ba02008-11-07 14:05:41 -0800526
527 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100528 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -0800529
Jani Nikulae5748942014-05-14 16:58:20 +0300530out_put:
531 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000532 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +0300533
534out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100535 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200536
537 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800538}
539EXPORT_SYMBOL(drm_encoder_init);
540
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100541/**
542 * drm_encoder_cleanup - cleans up an initialised encoder
543 * @encoder: encoder to cleanup
544 *
545 * Cleans up the encoder but doesn't free the object.
546 */
Dave Airlief453ba02008-11-07 14:05:41 -0800547void drm_encoder_cleanup(struct drm_encoder *encoder)
548{
549 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +0100550
Chris Wilson490d3d12016-05-27 20:05:00 +0100551 /* Note that the encoder_list is considered to be static; should we
552 * remove the drm_encoder at runtime we would have to decrement all
553 * the indices on the drm_encoder after us in the encoder_list.
554 */
555
Daniel Vetter84849902012-12-02 00:28:11 +0100556 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000557 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +0300558 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800559 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000560 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100561 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100562
563 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -0800564}
565EXPORT_SYMBOL(drm_encoder_cleanup);
566
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200567static unsigned int drm_num_planes(struct drm_device *dev)
568{
569 unsigned int num = 0;
570 struct drm_plane *tmp;
571
572 drm_for_each_plane(tmp, dev) {
573 num++;
574 }
575
576 return num;
577}
578
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300579/**
Matt Roperdc415ff2014-04-01 15:22:36 -0700580 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300581 * @dev: DRM device
582 * @plane: plane object to init
583 * @possible_crtcs: bitmask of possible CRTCs
584 * @funcs: callbacks for the new plane
Daniel Vetter62cacc72016-08-12 22:48:37 +0200585 * @formats: array of supported formats (DRM_FORMAT\_\*)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300586 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -0700587 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200588 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300589 *
Matt Roperdc415ff2014-04-01 15:22:36 -0700590 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300591 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100592 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300593 * Zero on success, error code on failure.
594 */
Matt Roperdc415ff2014-04-01 15:22:36 -0700595int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
596 unsigned long possible_crtcs,
597 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +0200598 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200599 enum drm_plane_type type,
600 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800601{
Rob Clark6b4959f2014-12-18 16:01:53 -0500602 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200603 int ret;
604
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200605 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
606 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100607 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200608
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100609 drm_modeset_lock_init(&plane->mutex);
610
Rob Clark4d939142012-05-17 02:23:27 -0600611 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800612 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800613 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +0100614 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
615 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800616 if (!plane->format_types) {
617 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000618 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100619 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800620 }
621
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200622 if (name) {
623 va_list ap;
624
625 va_start(ap, name);
626 plane->name = kvasprintf(GFP_KERNEL, name, ap);
627 va_end(ap);
628 } else {
629 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
630 drm_num_planes(dev));
631 }
632 if (!plane->name) {
633 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000634 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200635 return -ENOMEM;
636 }
637
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800638 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800639 plane->format_count = format_count;
640 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -0700641 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642
Rob Clark6b4959f2014-12-18 16:01:53 -0500643 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100644 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -0700645 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -0500646 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800647
Rob Clark9922ab52014-04-01 20:16:57 -0400648 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -0500649 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -0400650 plane->type);
651
Rob Clark6b4959f2014-12-18 16:01:53 -0500652 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
653 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
654 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
655 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
656 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
657 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
658 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
659 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
660 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
661 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
662 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
663 }
664
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100665 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800666}
Matt Roperdc415ff2014-04-01 15:22:36 -0700667EXPORT_SYMBOL(drm_universal_plane_init);
668
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200669static int drm_plane_register_all(struct drm_device *dev)
670{
671 struct drm_plane *plane;
672 int ret = 0;
673
674 drm_for_each_plane(plane, dev) {
675 if (plane->funcs->late_register)
676 ret = plane->funcs->late_register(plane);
677 if (ret)
678 return ret;
679 }
680
681 return 0;
682}
683
684static void drm_plane_unregister_all(struct drm_device *dev)
685{
686 struct drm_plane *plane;
687
688 drm_for_each_plane(plane, dev) {
689 if (plane->funcs->early_unregister)
690 plane->funcs->early_unregister(plane);
691 }
692}
693
Matt Roperdc415ff2014-04-01 15:22:36 -0700694/**
695 * drm_plane_init - Initialize a legacy plane
696 * @dev: DRM device
697 * @plane: plane object to init
698 * @possible_crtcs: bitmask of possible CRTCs
699 * @funcs: callbacks for the new plane
Daniel Vetter62cacc72016-08-12 22:48:37 +0200700 * @formats: array of supported formats (DRM_FORMAT\_\*)
Matt Roperdc415ff2014-04-01 15:22:36 -0700701 * @format_count: number of elements in @formats
702 * @is_primary: plane type (primary vs overlay)
703 *
704 * Legacy API to initialize a DRM plane.
705 *
706 * New drivers should call drm_universal_plane_init() instead.
707 *
708 * Returns:
709 * Zero on success, error code on failure.
710 */
711int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
712 unsigned long possible_crtcs,
713 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +0200714 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -0700715 bool is_primary)
716{
717 enum drm_plane_type type;
718
719 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
720 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +0200721 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -0700722}
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800723EXPORT_SYMBOL(drm_plane_init);
724
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300725/**
726 * drm_plane_cleanup - Clean up the core plane usage
727 * @plane: plane to cleanup
728 *
729 * This function cleans up @plane and removes it from the DRM mode setting
730 * core. Note that the function does *not* free the plane structure itself,
731 * this is the responsibility of the caller.
732 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800733void drm_plane_cleanup(struct drm_plane *plane)
734{
735 struct drm_device *dev = plane->dev;
736
Daniel Vetter84849902012-12-02 00:28:11 +0100737 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800738 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000739 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -0700740
741 BUG_ON(list_empty(&plane->head));
742
Chris Wilson490d3d12016-05-27 20:05:00 +0100743 /* Note that the plane_list is considered to be static; should we
744 * remove the drm_plane at runtime we would have to decrement all
745 * the indices on the drm_plane after us in the plane_list.
746 */
747
Matt Roperdc415ff2014-04-01 15:22:36 -0700748 list_del(&plane->head);
749 dev->mode_config.num_total_plane--;
750 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
751 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +0100752 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +0100753
754 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
755 if (plane->state && plane->funcs->atomic_destroy_state)
756 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100757
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200758 kfree(plane->name);
759
Thierry Redinga18c0af2014-12-10 11:38:49 +0100760 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800761}
762EXPORT_SYMBOL(drm_plane_cleanup);
763
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300764/**
Chandra Konduruf81338a2015-04-09 17:36:21 -0700765 * drm_plane_from_index - find the registered plane at an index
766 * @dev: DRM device
767 * @idx: index of registered plane to find for
768 *
769 * Given a plane index, return the registered plane from DRM device's
770 * list of planes with matching index.
771 */
772struct drm_plane *
773drm_plane_from_index(struct drm_device *dev, int idx)
774{
775 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -0700776
Chris Wilson490d3d12016-05-27 20:05:00 +0100777 drm_for_each_plane(plane, dev)
778 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -0700779 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +0100780
Chandra Konduruf81338a2015-04-09 17:36:21 -0700781 return NULL;
782}
783EXPORT_SYMBOL(drm_plane_from_index);
784
785/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300786 * drm_plane_force_disable - Forcibly disable a plane
787 * @plane: plane to disable
788 *
789 * Forces the plane to be disabled.
790 *
791 * Used when the plane's current framebuffer is destroyed,
792 * and when restoring fbdev mode.
793 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300794void drm_plane_force_disable(struct drm_plane *plane)
795{
796 int ret;
797
Daniel Vetter3d30a592014-07-27 13:42:42 +0200798 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +0300799 return;
800
Daniel Vetter3d30a592014-07-27 13:42:42 +0200801 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +0300802 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +0200803 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300804 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +0200805 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +0200806 return;
807 }
Ville Syrjälä9125e612013-06-03 16:10:40 +0300808 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +0100809 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +0200810 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +0300811 plane->fb = NULL;
812 plane->crtc = NULL;
813}
814EXPORT_SYMBOL(drm_plane_force_disable);
815
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200816int drm_modeset_register_all(struct drm_device *dev)
817{
818 int ret;
819
820 ret = drm_plane_register_all(dev);
821 if (ret)
822 goto err_plane;
823
824 ret = drm_crtc_register_all(dev);
825 if (ret)
826 goto err_crtc;
827
828 ret = drm_encoder_register_all(dev);
829 if (ret)
830 goto err_encoder;
831
832 ret = drm_connector_register_all(dev);
833 if (ret)
834 goto err_connector;
835
836 return 0;
837
838err_connector:
839 drm_encoder_unregister_all(dev);
840err_encoder:
841 drm_crtc_unregister_all(dev);
842err_crtc:
843 drm_plane_unregister_all(dev);
844err_plane:
845 return ret;
846}
847
848void drm_modeset_unregister_all(struct drm_device *dev)
849{
850 drm_connector_unregister_all(dev);
851 drm_encoder_unregister_all(dev);
852 drm_crtc_unregister_all(dev);
853 drm_plane_unregister_all(dev);
854}
855
Rob Clark6b4959f2014-12-18 16:01:53 -0500856static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800857{
Rob Clark356af0e2014-12-18 16:01:52 -0500858 struct drm_property *prop;
Daniel Vetter52217192016-08-12 22:48:50 +0200859 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800860
Daniel Vetter52217192016-08-12 22:48:50 +0200861 ret = drm_connector_create_standard_properties(dev);
862 if (ret)
863 return ret;
Dave Airlie6f134d72014-10-20 16:30:50 +1000864
Rob Clark6b4959f2014-12-18 16:01:53 -0500865 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -0400866 "type", drm_plane_type_enum_list,
867 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -0500868 if (!prop)
869 return -ENOMEM;
870 dev->mode_config.plane_type_property = prop;
871
872 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
873 "SRC_X", 0, UINT_MAX);
874 if (!prop)
875 return -ENOMEM;
876 dev->mode_config.prop_src_x = prop;
877
878 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
879 "SRC_Y", 0, UINT_MAX);
880 if (!prop)
881 return -ENOMEM;
882 dev->mode_config.prop_src_y = prop;
883
884 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
885 "SRC_W", 0, UINT_MAX);
886 if (!prop)
887 return -ENOMEM;
888 dev->mode_config.prop_src_w = prop;
889
890 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
891 "SRC_H", 0, UINT_MAX);
892 if (!prop)
893 return -ENOMEM;
894 dev->mode_config.prop_src_h = prop;
895
896 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
897 "CRTC_X", INT_MIN, INT_MAX);
898 if (!prop)
899 return -ENOMEM;
900 dev->mode_config.prop_crtc_x = prop;
901
902 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
903 "CRTC_Y", INT_MIN, INT_MAX);
904 if (!prop)
905 return -ENOMEM;
906 dev->mode_config.prop_crtc_y = prop;
907
908 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
909 "CRTC_W", 0, INT_MAX);
910 if (!prop)
911 return -ENOMEM;
912 dev->mode_config.prop_crtc_w = prop;
913
914 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
915 "CRTC_H", 0, INT_MAX);
916 if (!prop)
917 return -ENOMEM;
918 dev->mode_config.prop_crtc_h = prop;
919
920 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
921 "FB_ID", DRM_MODE_OBJECT_FB);
922 if (!prop)
923 return -ENOMEM;
924 dev->mode_config.prop_fb_id = prop;
925
926 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
927 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
928 if (!prop)
929 return -ENOMEM;
930 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -0400931
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100932 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
933 "ACTIVE");
934 if (!prop)
935 return -ENOMEM;
936 dev->mode_config.prop_active = prop;
937
Daniel Stone955f3c32015-05-25 19:11:52 +0100938 prop = drm_property_create(dev,
939 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
940 "MODE_ID", 0);
941 if (!prop)
942 return -ENOMEM;
943 dev->mode_config.prop_mode_id = prop;
944
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000945 prop = drm_property_create(dev,
946 DRM_MODE_PROP_BLOB,
947 "DEGAMMA_LUT", 0);
948 if (!prop)
949 return -ENOMEM;
950 dev->mode_config.degamma_lut_property = prop;
951
952 prop = drm_property_create_range(dev,
953 DRM_MODE_PROP_IMMUTABLE,
954 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
955 if (!prop)
956 return -ENOMEM;
957 dev->mode_config.degamma_lut_size_property = prop;
958
959 prop = drm_property_create(dev,
960 DRM_MODE_PROP_BLOB,
961 "CTM", 0);
962 if (!prop)
963 return -ENOMEM;
964 dev->mode_config.ctm_property = prop;
965
966 prop = drm_property_create(dev,
967 DRM_MODE_PROP_BLOB,
968 "GAMMA_LUT", 0);
969 if (!prop)
970 return -ENOMEM;
971 dev->mode_config.gamma_lut_property = prop;
972
973 prop = drm_property_create_range(dev,
974 DRM_MODE_PROP_IMMUTABLE,
975 "GAMMA_LUT_SIZE", 0, UINT_MAX);
976 if (!prop)
977 return -ENOMEM;
978 dev->mode_config.gamma_lut_size_property = prop;
979
Rob Clark9922ab52014-04-01 20:16:57 -0400980 return 0;
981}
982
Dave Airlief453ba02008-11-07 14:05:41 -0800983/**
Dave Airlief453ba02008-11-07 14:05:41 -0800984 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100985 * @dev: drm device for the ioctl
986 * @data: data pointer for the ioctl
987 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800988 *
Dave Airlief453ba02008-11-07 14:05:41 -0800989 * Construct a set of configuration description structures and return
990 * them to the user, including CRTC, connector and framebuffer configuration.
991 *
992 * Called by the user via ioctl.
993 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100994 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100995 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800996 */
997int drm_mode_getresources(struct drm_device *dev, void *data,
998 struct drm_file *file_priv)
999{
1000 struct drm_mode_card_res *card_res = data;
1001 struct list_head *lh;
1002 struct drm_framebuffer *fb;
1003 struct drm_connector *connector;
1004 struct drm_crtc *crtc;
1005 struct drm_encoder *encoder;
1006 int ret = 0;
1007 int connector_count = 0;
1008 int crtc_count = 0;
1009 int fb_count = 0;
1010 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001011 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001012 uint32_t __user *fb_id;
1013 uint32_t __user *crtc_id;
1014 uint32_t __user *connector_id;
1015 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001016
Dave Airliefb3b06c2011-02-08 13:55:21 +10001017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1018 return -EINVAL;
1019
Dave Airlief453ba02008-11-07 14:05:41 -08001020
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001021 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001022 /*
1023 * For the non-control nodes we need to limit the list of resources
1024 * by IDs in the group list for this node
1025 */
1026 list_for_each(lh, &file_priv->fbs)
1027 fb_count++;
1028
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001029 /* handle this in 4 parts */
1030 /* FBs */
1031 if (card_res->count_fbs >= fb_count) {
1032 copied = 0;
1033 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1034 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1035 if (put_user(fb->base.id, fb_id + copied)) {
1036 mutex_unlock(&file_priv->fbs_lock);
1037 return -EFAULT;
1038 }
1039 copied++;
1040 }
1041 }
1042 card_res->count_fbs = fb_count;
1043 mutex_unlock(&file_priv->fbs_lock);
1044
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001045 /* mode_config.mutex protects the connector list against e.g. DP MST
1046 * connector hot-adding. CRTC/Plane lists are invariant. */
1047 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001048 drm_for_each_crtc(crtc, dev)
1049 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001050
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001051 drm_for_each_connector(connector, dev)
1052 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001053
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001054 drm_for_each_encoder(encoder, dev)
1055 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001056
1057 card_res->max_height = dev->mode_config.max_height;
1058 card_res->min_height = dev->mode_config.min_height;
1059 card_res->max_width = dev->mode_config.max_width;
1060 card_res->min_width = dev->mode_config.min_width;
1061
Dave Airlief453ba02008-11-07 14:05:41 -08001062 /* CRTCs */
1063 if (card_res->count_crtcs >= crtc_count) {
1064 copied = 0;
1065 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001066 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001067 if (put_user(crtc->base.id, crtc_id + copied)) {
1068 ret = -EFAULT;
1069 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001070 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001071 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001072 }
1073 }
1074 card_res->count_crtcs = crtc_count;
1075
1076 /* Encoders */
1077 if (card_res->count_encoders >= encoder_count) {
1078 copied = 0;
1079 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001080 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001081 if (put_user(encoder->base.id, encoder_id +
1082 copied)) {
1083 ret = -EFAULT;
1084 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001085 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001086 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001087 }
1088 }
1089 card_res->count_encoders = encoder_count;
1090
1091 /* Connectors */
1092 if (card_res->count_connectors >= connector_count) {
1093 copied = 0;
1094 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001095 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001096 if (put_user(connector->base.id,
1097 connector_id + copied)) {
1098 ret = -EFAULT;
1099 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001100 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001101 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001102 }
1103 }
1104 card_res->count_connectors = connector_count;
1105
Dave Airlief453ba02008-11-07 14:05:41 -08001106out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001107 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001108 return ret;
1109}
1110
1111/**
1112 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001113 * @dev: drm device for the ioctl
1114 * @data: data pointer for the ioctl
1115 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001116 *
Dave Airlief453ba02008-11-07 14:05:41 -08001117 * Construct a CRTC configuration structure to return to the user.
1118 *
1119 * Called by the user via ioctl.
1120 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001121 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001122 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001123 */
1124int drm_mode_getcrtc(struct drm_device *dev,
1125 void *data, struct drm_file *file_priv)
1126{
1127 struct drm_mode_crtc *crtc_resp = data;
1128 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001129
Dave Airliefb3b06c2011-02-08 13:55:21 +10001130 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1131 return -EINVAL;
1132
Rob Clarka2b34e22013-10-05 16:36:52 -04001133 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001134 if (!crtc)
1135 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001136
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001137 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001138 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001139 if (crtc->primary->fb)
1140 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001141 else
1142 crtc_resp->fb_id = 0;
1143
Daniel Vetter31c946e2015-02-22 12:24:17 +01001144 if (crtc->state) {
1145 crtc_resp->x = crtc->primary->state->src_x >> 16;
1146 crtc_resp->y = crtc->primary->state->src_y >> 16;
1147 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001148 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001149 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08001150
Daniel Vetter31c946e2015-02-22 12:24:17 +01001151 } else {
1152 crtc_resp->mode_valid = 0;
1153 }
Dave Airlief453ba02008-11-07 14:05:41 -08001154 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01001155 crtc_resp->x = crtc->x;
1156 crtc_resp->y = crtc->y;
1157 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001158 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001159 crtc_resp->mode_valid = 1;
1160
1161 } else {
1162 crtc_resp->mode_valid = 0;
1163 }
Dave Airlief453ba02008-11-07 14:05:41 -08001164 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001165 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001166
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001167 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001168}
1169
Rob Clark95cbf112014-12-18 16:01:49 -05001170/* helper for getconnector and getproperties ioctls */
Daniel Vetter52217192016-08-12 22:48:50 +02001171int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
1172 uint32_t __user *prop_ptr,
1173 uint64_t __user *prop_values,
1174 uint32_t *arg_count_props)
Rob Clark95cbf112014-12-18 16:01:49 -05001175{
Rob Clark88a48e22014-12-18 16:01:50 -05001176 int props_count;
1177 int i, ret, copied;
1178
1179 props_count = obj->properties->count;
1180 if (!atomic)
1181 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05001182
1183 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05001184 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05001185 struct drm_property *prop = obj->properties->properties[i];
1186 uint64_t val;
1187
Rob Clark88a48e22014-12-18 16:01:50 -05001188 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
1189 continue;
1190
Rob Clark95cbf112014-12-18 16:01:49 -05001191 ret = drm_object_property_get_value(obj, prop, &val);
1192 if (ret)
1193 return ret;
1194
1195 if (put_user(prop->base.id, prop_ptr + copied))
1196 return -EFAULT;
1197
1198 if (put_user(val, prop_values + copied))
1199 return -EFAULT;
1200
1201 copied++;
1202 }
1203 }
1204 *arg_count_props = props_count;
1205
1206 return 0;
1207}
1208
Daniel Vetterabd69c52014-11-25 23:50:05 +01001209static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
1210{
1211 struct drm_connector *connector;
1212 struct drm_device *dev = encoder->dev;
1213 bool uses_atomic = false;
1214
1215 /* For atomic drivers only state objects are synchronously updated and
1216 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02001217 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01001218 if (!connector->state)
1219 continue;
1220
1221 uses_atomic = true;
1222
1223 if (connector->state->best_encoder != encoder)
1224 continue;
1225
1226 return connector->state->crtc;
1227 }
1228
1229 /* Don't return stale data (e.g. pending async disable). */
1230 if (uses_atomic)
1231 return NULL;
1232
1233 return encoder->crtc;
1234}
1235
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001236/**
1237 * drm_mode_getencoder - get encoder configuration
1238 * @dev: drm device for the ioctl
1239 * @data: data pointer for the ioctl
1240 * @file_priv: drm file for the ioctl call
1241 *
1242 * Construct a encoder configuration structure to return to the user.
1243 *
1244 * Called by the user via ioctl.
1245 *
1246 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001247 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001248 */
Dave Airlief453ba02008-11-07 14:05:41 -08001249int drm_mode_getencoder(struct drm_device *dev, void *data,
1250 struct drm_file *file_priv)
1251{
1252 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001253 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01001254 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001255
Dave Airliefb3b06c2011-02-08 13:55:21 +10001256 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1257 return -EINVAL;
1258
Rob Clarka2b34e22013-10-05 16:36:52 -04001259 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001260 if (!encoder)
1261 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001262
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001263 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01001264 crtc = drm_encoder_get_crtc(encoder);
1265 if (crtc)
1266 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001267 else
1268 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001269 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1270
Dave Airlief453ba02008-11-07 14:05:41 -08001271 enc_resp->encoder_type = encoder->encoder_type;
1272 enc_resp->encoder_id = encoder->base.id;
1273 enc_resp->possible_crtcs = encoder->possible_crtcs;
1274 enc_resp->possible_clones = encoder->possible_clones;
1275
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001276 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001277}
1278
1279/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001280 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001281 * @dev: DRM device
1282 * @data: ioctl data
1283 * @file_priv: DRM file info
1284 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001285 * Construct a list of plane ids to return to the user.
1286 *
1287 * Called by the user via ioctl.
1288 *
1289 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001290 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001291 */
1292int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001293 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001294{
1295 struct drm_mode_get_plane_res *plane_resp = data;
1296 struct drm_mode_config *config;
1297 struct drm_plane *plane;
1298 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001299 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07001300 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001301
1302 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1303 return -EINVAL;
1304
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001305 config = &dev->mode_config;
1306
Matt Roper681e7ec2014-04-01 15:22:42 -07001307 if (file_priv->universal_planes)
1308 num_planes = config->num_total_plane;
1309 else
1310 num_planes = config->num_overlay_plane;
1311
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001312 /*
1313 * This ioctl is called twice, once to determine how much space is
1314 * needed, and the 2nd time to fill it.
1315 */
Matt Roper681e7ec2014-04-01 15:22:42 -07001316 if (num_planes &&
1317 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001318 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001319
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001320 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02001321 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07001322 /*
1323 * Unless userspace set the 'universal planes'
1324 * capability bit, only advertise overlays.
1325 */
1326 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1327 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07001328 continue;
1329
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001330 if (put_user(plane->base.id, plane_ptr + copied))
1331 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001332 copied++;
1333 }
1334 }
Matt Roper681e7ec2014-04-01 15:22:42 -07001335 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001336
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001337 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001338}
1339
1340/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001341 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001342 * @dev: DRM device
1343 * @data: ioctl data
1344 * @file_priv: DRM file info
1345 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001346 * Construct a plane configuration structure to return to the user.
1347 *
1348 * Called by the user via ioctl.
1349 *
1350 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001351 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001352 */
1353int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001354 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001355{
1356 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001357 struct drm_plane *plane;
1358 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001359
1360 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1361 return -EINVAL;
1362
Rob Clarka2b34e22013-10-05 16:36:52 -04001363 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001364 if (!plane)
1365 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001366
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001367 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001368 if (plane->crtc)
1369 plane_resp->crtc_id = plane->crtc->base.id;
1370 else
1371 plane_resp->crtc_id = 0;
1372
1373 if (plane->fb)
1374 plane_resp->fb_id = plane->fb->base.id;
1375 else
1376 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001377 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001378
1379 plane_resp->plane_id = plane->base.id;
1380 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001381 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001382
1383 /*
1384 * This ioctl is called twice, once to determine how much space is
1385 * needed, and the 2nd time to fill it.
1386 */
1387 if (plane->format_count &&
1388 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001389 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001390 if (copy_to_user(format_ptr,
1391 plane->format_types,
1392 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001393 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001394 }
1395 }
1396 plane_resp->count_format_types = plane->format_count;
1397
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001398 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001399}
1400
Laurent Pinchartead86102015-03-05 02:25:43 +02001401/**
1402 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
1403 * @plane: plane to check for format support
1404 * @format: the pixel format
1405 *
1406 * Returns:
1407 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
1408 * otherwise.
1409 */
1410int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
1411{
1412 unsigned int i;
1413
1414 for (i = 0; i < plane->format_count; i++) {
1415 if (format == plane->format_types[i])
1416 return 0;
1417 }
1418
1419 return -EINVAL;
1420}
1421
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03001422static int check_src_coords(uint32_t src_x, uint32_t src_y,
1423 uint32_t src_w, uint32_t src_h,
1424 const struct drm_framebuffer *fb)
1425{
1426 unsigned int fb_width, fb_height;
1427
1428 fb_width = fb->width << 16;
1429 fb_height = fb->height << 16;
1430
1431 /* Make sure source coordinates are inside the fb. */
1432 if (src_w > fb_width ||
1433 src_x > fb_width - src_w ||
1434 src_h > fb_height ||
1435 src_y > fb_height - src_h) {
1436 DRM_DEBUG_KMS("Invalid source coordinates "
1437 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1438 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
1439 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
1440 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
1441 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
1442 return -ENOSPC;
1443 }
1444
1445 return 0;
1446}
1447
Matt Roperb36552b2014-06-10 08:28:09 -07001448/*
1449 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001450 *
Matt Roperb36552b2014-06-10 08:28:09 -07001451 * Note that we assume an extra reference has already been taken on fb. If the
1452 * update fails, this reference will be dropped before return; if it succeeds,
1453 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001454 *
Matt Roperb36552b2014-06-10 08:28:09 -07001455 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001456 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02001457static int __setplane_internal(struct drm_plane *plane,
1458 struct drm_crtc *crtc,
1459 struct drm_framebuffer *fb,
1460 int32_t crtc_x, int32_t crtc_y,
1461 uint32_t crtc_w, uint32_t crtc_h,
1462 /* src_{x,y,w,h} values are 16.16 fixed point */
1463 uint32_t src_x, uint32_t src_y,
1464 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001465{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001466 int ret = 0;
1467
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001468 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07001469 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02001470 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02001471 ret = plane->funcs->disable_plane(plane);
1472 if (!ret) {
1473 plane->crtc = NULL;
1474 plane->fb = NULL;
1475 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02001476 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001477 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001478 goto out;
1479 }
1480
Matt Roper7f994f32014-05-29 08:06:51 -07001481 /* Check whether this plane is usable on this CRTC */
1482 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
1483 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1484 ret = -EINVAL;
1485 goto out;
1486 }
1487
Ville Syrjälä62443be2011-12-20 00:06:47 +02001488 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02001489 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1490 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01001491 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01001492 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1493 kfree(format_name);
Ville Syrjälä62443be2011-12-20 00:06:47 +02001494 goto out;
1495 }
1496
Matt Roper3968be92015-04-13 11:06:13 -07001497 /* Give drivers some help against integer overflows */
1498 if (crtc_w > INT_MAX ||
1499 crtc_x > INT_MAX - (int32_t) crtc_w ||
1500 crtc_h > INT_MAX ||
1501 crtc_y > INT_MAX - (int32_t) crtc_h) {
1502 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1503 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03001504 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001505 goto out;
1506 }
1507
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03001508 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1509 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08001510 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001511
Daniel Vetter3d30a592014-07-27 13:42:42 +02001512 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001513 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07001514 crtc_x, crtc_y, crtc_w, crtc_h,
1515 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08001516 if (!ret) {
1517 plane->crtc = crtc;
1518 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001519 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001520 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02001521 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001522 }
1523
1524out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001525 if (fb)
1526 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001527 if (plane->old_fb)
1528 drm_framebuffer_unreference(plane->old_fb);
1529 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001530
1531 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02001532}
Matt Roperb36552b2014-06-10 08:28:09 -07001533
Daniel Vetterf2b50c12014-09-12 17:07:32 +02001534static int setplane_internal(struct drm_plane *plane,
1535 struct drm_crtc *crtc,
1536 struct drm_framebuffer *fb,
1537 int32_t crtc_x, int32_t crtc_y,
1538 uint32_t crtc_w, uint32_t crtc_h,
1539 /* src_{x,y,w,h} values are 16.16 fixed point */
1540 uint32_t src_x, uint32_t src_y,
1541 uint32_t src_w, uint32_t src_h)
1542{
1543 int ret;
1544
1545 drm_modeset_lock_all(plane->dev);
1546 ret = __setplane_internal(plane, crtc, fb,
1547 crtc_x, crtc_y, crtc_w, crtc_h,
1548 src_x, src_y, src_w, src_h);
1549 drm_modeset_unlock_all(plane->dev);
1550
1551 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07001552}
1553
1554/**
1555 * drm_mode_setplane - configure a plane's configuration
1556 * @dev: DRM device
1557 * @data: ioctl data*
1558 * @file_priv: DRM file info
1559 *
1560 * Set plane configuration, including placement, fb, scaling, and other factors.
1561 * Or pass a NULL fb to disable (planes may be disabled without providing a
1562 * valid crtc).
1563 *
1564 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001565 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07001566 */
1567int drm_mode_setplane(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569{
1570 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07001571 struct drm_plane *plane;
1572 struct drm_crtc *crtc = NULL;
1573 struct drm_framebuffer *fb = NULL;
1574
1575 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1576 return -EINVAL;
1577
Matt Roperb36552b2014-06-10 08:28:09 -07001578 /*
1579 * First, find the plane, crtc, and fb objects. If not available,
1580 * we don't bother to call the driver.
1581 */
Rob Clark933f6222014-11-25 20:33:11 -05001582 plane = drm_plane_find(dev, plane_req->plane_id);
1583 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07001584 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1585 plane_req->plane_id);
1586 return -ENOENT;
1587 }
Matt Roperb36552b2014-06-10 08:28:09 -07001588
1589 if (plane_req->fb_id) {
1590 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1591 if (!fb) {
1592 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1593 plane_req->fb_id);
1594 return -ENOENT;
1595 }
1596
Rob Clark933f6222014-11-25 20:33:11 -05001597 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1598 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07001599 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1600 plane_req->crtc_id);
1601 return -ENOENT;
1602 }
Matt Roperb36552b2014-06-10 08:28:09 -07001603 }
1604
Matt Roper161d0dc2014-06-10 08:28:10 -07001605 /*
1606 * setplane_internal will take care of deref'ing either the old or new
1607 * framebuffer depending on success.
1608 */
Chris Wilson17cfd912014-06-13 15:22:28 +01001609 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07001610 plane_req->crtc_x, plane_req->crtc_y,
1611 plane_req->crtc_w, plane_req->crtc_h,
1612 plane_req->src_x, plane_req->src_y,
1613 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08001614}
1615
1616/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001617 * drm_mode_set_config_internal - helper to call ->set_config
1618 * @set: modeset config to set
1619 *
1620 * This is a little helper to wrap internal calls to the ->set_config driver
1621 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01001622 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001623 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001624 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01001625 */
1626int drm_mode_set_config_internal(struct drm_mode_set *set)
1627{
1628 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001629 struct drm_framebuffer *fb;
1630 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001631 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001632
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001633 /*
1634 * NOTE: ->set_config can also disable other crtcs (if we steal all
1635 * connectors from it), hence we need to refcount the fbs across all
1636 * crtcs. Atomic modeset will have saner semantics ...
1637 */
Daniel Vettere4f62542015-07-09 23:44:35 +02001638 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02001639 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001640
Daniel Vetterb0d12322012-12-11 01:07:12 +01001641 fb = set->fb;
1642
1643 ret = crtc->funcs->set_config(set);
1644 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07001645 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001646 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001647 }
Daniel Vettercc85e122013-06-15 00:13:15 +02001648
Daniel Vettere4f62542015-07-09 23:44:35 +02001649 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07001650 if (tmp->primary->fb)
1651 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001652 if (tmp->primary->old_fb)
1653 drm_framebuffer_unreference(tmp->primary->old_fb);
1654 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001655 }
1656
1657 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001658}
1659EXPORT_SYMBOL(drm_mode_set_config_internal);
1660
Matt Roperaf936292014-04-01 15:22:34 -07001661/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08001662 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1663 * @mode: mode to query
1664 * @hdisplay: hdisplay value to fill in
1665 * @vdisplay: vdisplay value to fill in
1666 *
1667 * The vdisplay value will be doubled if the specified mode is a stereo mode of
1668 * the appropriate layout.
1669 */
1670void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1671 int *hdisplay, int *vdisplay)
1672{
1673 struct drm_display_mode adjusted;
1674
1675 drm_mode_copy(&adjusted, mode);
1676 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1677 *hdisplay = adjusted.crtc_hdisplay;
1678 *vdisplay = adjusted.crtc_vdisplay;
1679}
1680EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1681
1682/**
Matt Roperaf936292014-04-01 15:22:34 -07001683 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1684 * CRTC viewport
1685 * @crtc: CRTC that framebuffer will be displayed on
1686 * @x: x panning
1687 * @y: y panning
1688 * @mode: mode that framebuffer will be displayed under
1689 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01001690 */
Matt Roperaf936292014-04-01 15:22:34 -07001691int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1692 int x, int y,
1693 const struct drm_display_mode *mode,
1694 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01001695
1696{
1697 int hdisplay, vdisplay;
1698
Gustavo Padovanecb7e162014-12-01 15:40:09 -08001699 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01001700
Ville Syrjälä33e0be62015-10-16 18:38:39 +03001701 if (crtc->state &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03001702 crtc->primary->state->rotation & (DRM_ROTATE_90 |
1703 DRM_ROTATE_270))
Damien Lespiauc11e9282013-09-25 16:45:30 +01001704 swap(hdisplay, vdisplay);
1705
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03001706 return check_src_coords(x << 16, y << 16,
1707 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01001708}
Matt Roperaf936292014-04-01 15:22:34 -07001709EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01001710
Daniel Vetter2d13b672012-12-11 13:47:23 +01001711/**
Dave Airlief453ba02008-11-07 14:05:41 -08001712 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001713 * @dev: drm device for the ioctl
1714 * @data: data pointer for the ioctl
1715 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001716 *
Dave Airlief453ba02008-11-07 14:05:41 -08001717 * Build a new CRTC configuration based on user request.
1718 *
1719 * Called by the user via ioctl.
1720 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001721 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001722 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001723 */
1724int drm_mode_setcrtc(struct drm_device *dev, void *data,
1725 struct drm_file *file_priv)
1726{
1727 struct drm_mode_config *config = &dev->mode_config;
1728 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001729 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001730 struct drm_connector **connector_set = NULL, *connector;
1731 struct drm_framebuffer *fb = NULL;
1732 struct drm_display_mode *mode = NULL;
1733 struct drm_mode_set set;
1734 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001735 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001736 int i;
1737
Dave Airliefb3b06c2011-02-08 13:55:21 +10001738 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1739 return -EINVAL;
1740
Zhao Junwang01447e92015-07-07 17:08:35 +08001741 /*
1742 * Universal plane src offsets are only 16.16, prevent havoc for
1743 * drivers using universal plane code internally.
1744 */
1745 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001746 return -ERANGE;
1747
Daniel Vetter84849902012-12-02 00:28:11 +01001748 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04001749 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1750 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001751 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001752 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001753 goto out;
1754 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001755 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001756
1757 if (crtc_req->mode_valid) {
1758 /* If we have a mode we need a framebuffer. */
1759 /* If we pass -1, set the mode with the currently bound fb */
1760 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07001761 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001762 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1763 ret = -EINVAL;
1764 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001765 }
Matt Roperf4510a22014-04-01 15:22:40 -07001766 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001767 /* Make refcounting symmetric with the lookup path. */
1768 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001769 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01001770 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1771 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001772 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1773 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03001774 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001775 goto out;
1776 }
Dave Airlief453ba02008-11-07 14:05:41 -08001777 }
1778
1779 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001780 if (!mode) {
1781 ret = -ENOMEM;
1782 goto out;
1783 }
1784
Daniel Stone934a8a82015-05-22 13:34:48 +01001785 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001786 if (ret) {
1787 DRM_DEBUG_KMS("Invalid mode\n");
1788 goto out;
1789 }
1790
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02001791 /*
1792 * Check whether the primary plane supports the fb pixel format.
1793 * Drivers not implementing the universal planes API use a
1794 * default formats list provided by the DRM core which doesn't
1795 * match real hardware capabilities. Skip the check in that
1796 * case.
1797 */
1798 if (!crtc->primary->format_default) {
1799 ret = drm_plane_check_pixel_format(crtc->primary,
1800 fb->pixel_format);
1801 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01001802 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01001803 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1804 kfree(format_name);
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02001805 goto out;
1806 }
1807 }
1808
Damien Lespiauc11e9282013-09-25 16:45:30 +01001809 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1810 mode, fb);
1811 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001812 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01001813
Dave Airlief453ba02008-11-07 14:05:41 -08001814 }
1815
1816 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001817 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001818 ret = -EINVAL;
1819 goto out;
1820 }
1821
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001822 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001823 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001824 crtc_req->count_connectors);
1825 ret = -EINVAL;
1826 goto out;
1827 }
1828
1829 if (crtc_req->count_connectors > 0) {
1830 u32 out_id;
1831
1832 /* Avoid unbounded kernel memory allocation */
1833 if (crtc_req->count_connectors > config->num_connector) {
1834 ret = -EINVAL;
1835 goto out;
1836 }
1837
Thierry Reding2f6c5382014-12-10 13:03:36 +01001838 connector_set = kmalloc_array(crtc_req->count_connectors,
1839 sizeof(struct drm_connector *),
1840 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08001841 if (!connector_set) {
1842 ret = -ENOMEM;
1843 goto out;
1844 }
1845
1846 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10001847 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001848 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001849 if (get_user(out_id, &set_connectors_ptr[i])) {
1850 ret = -EFAULT;
1851 goto out;
1852 }
1853
Dave Airlieb164d312016-04-27 11:10:09 +10001854 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04001855 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001856 DRM_DEBUG_KMS("Connector id %d unknown\n",
1857 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001858 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001859 goto out;
1860 }
Jerome Glisse94401062010-07-15 15:43:25 -04001861 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1862 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001863 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001864
1865 connector_set[i] = connector;
1866 }
1867 }
1868
1869 set.crtc = crtc;
1870 set.x = crtc_req->x;
1871 set.y = crtc_req->y;
1872 set.mode = mode;
1873 set.connectors = connector_set;
1874 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001875 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001876 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08001877
1878out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01001879 if (fb)
1880 drm_framebuffer_unreference(fb);
1881
Dave Airlieb164d312016-04-27 11:10:09 +10001882 if (connector_set) {
1883 for (i = 0; i < crtc_req->count_connectors; i++) {
1884 if (connector_set[i])
1885 drm_connector_unreference(connector_set[i]);
1886 }
1887 }
Dave Airlief453ba02008-11-07 14:05:41 -08001888 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001889 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01001890 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001891 return ret;
1892}
1893
Matt Roper161d0dc2014-06-10 08:28:10 -07001894/**
1895 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1896 * universal plane handler call
1897 * @crtc: crtc to update cursor for
1898 * @req: data pointer for the ioctl
1899 * @file_priv: drm file for the ioctl call
1900 *
1901 * Legacy cursor ioctl's work directly with driver buffer handles. To
1902 * translate legacy ioctl calls into universal plane handler calls, we need to
1903 * wrap the native buffer handle in a drm_framebuffer.
1904 *
1905 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1906 * buffer with a pitch of 4*width; the universal plane interface should be used
1907 * directly in cases where the hardware can support other buffer settings and
1908 * userspace wants to make use of these capabilities.
1909 *
1910 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001911 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07001912 */
1913static int drm_mode_cursor_universal(struct drm_crtc *crtc,
1914 struct drm_mode_cursor2 *req,
1915 struct drm_file *file_priv)
1916{
1917 struct drm_device *dev = crtc->dev;
1918 struct drm_framebuffer *fb = NULL;
1919 struct drm_mode_fb_cmd2 fbreq = {
1920 .width = req->width,
1921 .height = req->height,
1922 .pixel_format = DRM_FORMAT_ARGB8888,
1923 .pitches = { req->width * 4 },
1924 .handles = { req->handle },
1925 };
1926 int32_t crtc_x, crtc_y;
1927 uint32_t crtc_w = 0, crtc_h = 0;
1928 uint32_t src_w = 0, src_h = 0;
1929 int ret = 0;
1930
1931 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02001932 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07001933
1934 /*
1935 * Obtain fb we'll be using (either new or existing) and take an extra
1936 * reference to it if fb != null. setplane will take care of dropping
1937 * the reference if the plane update fails.
1938 */
1939 if (req->flags & DRM_MODE_CURSOR_BO) {
1940 if (req->handle) {
Daniel Vetter7520a272016-08-15 16:07:02 +02001941 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07001942 if (IS_ERR(fb)) {
1943 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1944 return PTR_ERR(fb);
1945 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02001946 fb->hot_x = req->hot_x;
1947 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07001948 } else {
1949 fb = NULL;
1950 }
1951 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07001952 fb = crtc->cursor->fb;
1953 if (fb)
1954 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07001955 }
1956
1957 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1958 crtc_x = req->x;
1959 crtc_y = req->y;
1960 } else {
1961 crtc_x = crtc->cursor_x;
1962 crtc_y = crtc->cursor_y;
1963 }
1964
1965 if (fb) {
1966 crtc_w = fb->width;
1967 crtc_h = fb->height;
1968 src_w = fb->width << 16;
1969 src_h = fb->height << 16;
1970 }
1971
1972 /*
1973 * setplane_internal will take care of deref'ing either the old or new
1974 * framebuffer depending on success.
1975 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02001976 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07001977 crtc_x, crtc_y, crtc_w, crtc_h,
1978 0, 0, src_w, src_h);
1979
1980 /* Update successful; save new cursor position, if necessary */
1981 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1982 crtc->cursor_x = req->x;
1983 crtc->cursor_y = req->y;
1984 }
1985
1986 return ret;
1987}
1988
Dave Airlie4c813d42013-06-20 11:48:52 +10001989static int drm_mode_cursor_common(struct drm_device *dev,
1990 struct drm_mode_cursor2 *req,
1991 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08001992{
Dave Airlief453ba02008-11-07 14:05:41 -08001993 struct drm_crtc *crtc;
1994 int ret = 0;
1995
Dave Airliefb3b06c2011-02-08 13:55:21 +10001996 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1997 return -EINVAL;
1998
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00001999 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002000 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002001
Rob Clarka2b34e22013-10-05 16:36:52 -04002002 crtc = drm_crtc_find(dev, req->crtc_id);
2003 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002004 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002005 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002006 }
Dave Airlief453ba02008-11-07 14:05:41 -08002007
Matt Roper161d0dc2014-06-10 08:28:10 -07002008 /*
2009 * If this crtc has a universal cursor plane, call that plane's update
2010 * handler rather than using legacy cursor handlers.
2011 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002012 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002013 if (crtc->cursor) {
2014 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2015 goto out;
2016 }
2017
Dave Airlief453ba02008-11-07 14:05:41 -08002018 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002019 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002020 ret = -ENXIO;
2021 goto out;
2022 }
2023 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002024 if (crtc->funcs->cursor_set2)
2025 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2026 req->width, req->height, req->hot_x, req->hot_y);
2027 else
2028 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2029 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002030 }
2031
2032 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2033 if (crtc->funcs->cursor_move) {
2034 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2035 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002036 ret = -EFAULT;
2037 goto out;
2038 }
2039 }
2040out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002041 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002042
Dave Airlief453ba02008-11-07 14:05:41 -08002043 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002044
2045}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002046
2047
2048/**
2049 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2050 * @dev: drm device for the ioctl
2051 * @data: data pointer for the ioctl
2052 * @file_priv: drm file for the ioctl call
2053 *
2054 * Set the cursor configuration based on user request.
2055 *
2056 * Called by the user via ioctl.
2057 *
2058 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002059 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002060 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002061int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002062 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002063{
2064 struct drm_mode_cursor *req = data;
2065 struct drm_mode_cursor2 new_req;
2066
2067 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2068 new_req.hot_x = new_req.hot_y = 0;
2069
2070 return drm_mode_cursor_common(dev, &new_req, file_priv);
2071}
2072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002073/**
2074 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2075 * @dev: drm device for the ioctl
2076 * @data: data pointer for the ioctl
2077 * @file_priv: drm file for the ioctl call
2078 *
2079 * Set the cursor configuration based on user request. This implements the 2nd
2080 * version of the cursor ioctl, which allows userspace to additionally specify
2081 * the hotspot of the pointer.
2082 *
2083 * Called by the user via ioctl.
2084 *
2085 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002086 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002087 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002088int drm_mode_cursor2_ioctl(struct drm_device *dev,
2089 void *data, struct drm_file *file_priv)
2090{
2091 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002092
Dave Airlie4c813d42013-06-20 11:48:52 +10002093 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002094}
2095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002096/**
2097 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2098 * @bpp: bits per pixels
2099 * @depth: bit depth per pixel
2100 *
2101 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2102 * Useful in fbdev emulation code, since that deals in those values.
2103 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002104uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2105{
2106 uint32_t fmt;
2107
2108 switch (bpp) {
2109 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002110 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002111 break;
2112 case 16:
2113 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002114 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002115 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002116 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002117 break;
2118 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002119 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002120 break;
2121 case 32:
2122 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002123 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002124 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002125 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002126 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002127 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002128 break;
2129 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002130 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2131 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002132 break;
2133 }
2134
2135 return fmt;
2136}
2137EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2138
Daniel Vetter81065542016-06-21 10:54:13 +02002139static bool drm_property_type_valid(struct drm_property *property)
2140{
2141 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
2142 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2143 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
2144}
2145
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002146/**
2147 * drm_property_create - create a new property type
2148 * @dev: drm device
2149 * @flags: flags specifying the property type
2150 * @name: name of the property
2151 * @num_values: number of pre-defined values
2152 *
2153 * This creates a new generic drm property which can then be attached to a drm
2154 * object with drm_object_attach_property. The returned property object must be
2155 * freed with drm_property_destroy.
2156 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00002157 * Note that the DRM core keeps a per-device list of properties and that, if
2158 * drm_mode_config_cleanup() is called, it will destroy all properties created
2159 * by the driver.
2160 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002161 * Returns:
2162 * A pointer to the newly created property on success, NULL on failure.
2163 */
Dave Airlief453ba02008-11-07 14:05:41 -08002164struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2165 const char *name, int num_values)
2166{
2167 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002168 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002169
2170 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2171 if (!property)
2172 return NULL;
2173
Rob Clark98f75de2014-05-30 11:37:03 -04002174 property->dev = dev;
2175
Dave Airlief453ba02008-11-07 14:05:41 -08002176 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01002177 property->values = kcalloc(num_values, sizeof(uint64_t),
2178 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002179 if (!property->values)
2180 goto fail;
2181 }
2182
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002183 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2184 if (ret)
2185 goto fail;
2186
Dave Airlief453ba02008-11-07 14:05:41 -08002187 property->flags = flags;
2188 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01002189 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08002190
Vinson Lee471dd2e2011-11-10 11:55:40 -08002191 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002192 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002193 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2194 }
Dave Airlief453ba02008-11-07 14:05:41 -08002195
2196 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04002197
2198 WARN_ON(!drm_property_type_valid(property));
2199
Dave Airlief453ba02008-11-07 14:05:41 -08002200 return property;
2201fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002202 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002203 kfree(property);
2204 return NULL;
2205}
2206EXPORT_SYMBOL(drm_property_create);
2207
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002208/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02002209 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002210 * @dev: drm device
2211 * @flags: flags specifying the property type
2212 * @name: name of the property
2213 * @props: enumeration lists with property values
2214 * @num_values: number of pre-defined values
2215 *
2216 * This creates a new generic drm property which can then be attached to a drm
2217 * object with drm_object_attach_property. The returned property object must be
2218 * freed with drm_property_destroy.
2219 *
2220 * Userspace is only allowed to set one of the predefined values for enumeration
2221 * properties.
2222 *
2223 * Returns:
2224 * A pointer to the newly created property on success, NULL on failure.
2225 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01002226struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2227 const char *name,
2228 const struct drm_prop_enum_list *props,
2229 int num_values)
2230{
2231 struct drm_property *property;
2232 int i, ret;
2233
2234 flags |= DRM_MODE_PROP_ENUM;
2235
2236 property = drm_property_create(dev, flags, name, num_values);
2237 if (!property)
2238 return NULL;
2239
2240 for (i = 0; i < num_values; i++) {
2241 ret = drm_property_add_enum(property, i,
2242 props[i].type,
2243 props[i].name);
2244 if (ret) {
2245 drm_property_destroy(dev, property);
2246 return NULL;
2247 }
2248 }
2249
2250 return property;
2251}
2252EXPORT_SYMBOL(drm_property_create_enum);
2253
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002254/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02002255 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002256 * @dev: drm device
2257 * @flags: flags specifying the property type
2258 * @name: name of the property
2259 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02002260 * @num_props: size of the @props array
2261 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002262 *
Daniel Vetter295ee852014-07-30 14:23:44 +02002263 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002264 * object with drm_object_attach_property. The returned property object must be
2265 * freed with drm_property_destroy.
2266 *
2267 * Compared to plain enumeration properties userspace is allowed to set any
2268 * or'ed together combination of the predefined property bitflag values
2269 *
2270 * Returns:
2271 * A pointer to the newly created property on success, NULL on failure.
2272 */
Rob Clark49e27542012-05-17 02:23:26 -06002273struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2274 int flags, const char *name,
2275 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05302276 int num_props,
2277 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06002278{
2279 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05302280 int i, ret, index = 0;
2281 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06002282
2283 flags |= DRM_MODE_PROP_BITMASK;
2284
2285 property = drm_property_create(dev, flags, name, num_values);
2286 if (!property)
2287 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05302288 for (i = 0; i < num_props; i++) {
2289 if (!(supported_bits & (1ULL << props[i].type)))
2290 continue;
Rob Clark49e27542012-05-17 02:23:26 -06002291
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05302292 if (WARN_ON(index >= num_values)) {
2293 drm_property_destroy(dev, property);
2294 return NULL;
2295 }
2296
2297 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06002298 props[i].type,
2299 props[i].name);
2300 if (ret) {
2301 drm_property_destroy(dev, property);
2302 return NULL;
2303 }
2304 }
2305
2306 return property;
2307}
2308EXPORT_SYMBOL(drm_property_create_bitmask);
2309
Rob Clarkebc44cf2012-09-12 22:22:31 -05002310static struct drm_property *property_create_range(struct drm_device *dev,
2311 int flags, const char *name,
2312 uint64_t min, uint64_t max)
2313{
2314 struct drm_property *property;
2315
2316 property = drm_property_create(dev, flags, name, 2);
2317 if (!property)
2318 return NULL;
2319
2320 property->values[0] = min;
2321 property->values[1] = max;
2322
2323 return property;
2324}
2325
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002326/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002327 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002328 * @dev: drm device
2329 * @flags: flags specifying the property type
2330 * @name: name of the property
2331 * @min: minimum value of the property
2332 * @max: maximum value of the property
2333 *
2334 * This creates a new generic drm property which can then be attached to a drm
2335 * object with drm_object_attach_property. The returned property object must be
2336 * freed with drm_property_destroy.
2337 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002338 * Userspace is allowed to set any unsigned integer value in the (min, max)
2339 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002340 *
2341 * Returns:
2342 * A pointer to the newly created property on success, NULL on failure.
2343 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002344struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2345 const char *name,
2346 uint64_t min, uint64_t max)
2347{
Rob Clarkebc44cf2012-09-12 22:22:31 -05002348 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
2349 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002350}
2351EXPORT_SYMBOL(drm_property_create_range);
2352
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002353/**
2354 * drm_property_create_signed_range - create a new signed ranged property type
2355 * @dev: drm device
2356 * @flags: flags specifying the property type
2357 * @name: name of the property
2358 * @min: minimum value of the property
2359 * @max: maximum value of the property
2360 *
2361 * This creates a new generic drm property which can then be attached to a drm
2362 * object with drm_object_attach_property. The returned property object must be
2363 * freed with drm_property_destroy.
2364 *
2365 * Userspace is allowed to set any signed integer value in the (min, max)
2366 * range inclusive.
2367 *
2368 * Returns:
2369 * A pointer to the newly created property on success, NULL on failure.
2370 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05002371struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
2372 int flags, const char *name,
2373 int64_t min, int64_t max)
2374{
2375 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
2376 name, I642U64(min), I642U64(max));
2377}
2378EXPORT_SYMBOL(drm_property_create_signed_range);
2379
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002380/**
2381 * drm_property_create_object - create a new object property type
2382 * @dev: drm device
2383 * @flags: flags specifying the property type
2384 * @name: name of the property
2385 * @type: object type from DRM_MODE_OBJECT_* defines
2386 *
2387 * This creates a new generic drm property which can then be attached to a drm
2388 * object with drm_object_attach_property. The returned property object must be
2389 * freed with drm_property_destroy.
2390 *
2391 * Userspace is only allowed to set this to any property value of the given
2392 * @type. Only useful for atomic properties, which is enforced.
2393 *
2394 * Returns:
2395 * A pointer to the newly created property on success, NULL on failure.
2396 */
Rob Clark98f75de2014-05-30 11:37:03 -04002397struct drm_property *drm_property_create_object(struct drm_device *dev,
2398 int flags, const char *name, uint32_t type)
2399{
2400 struct drm_property *property;
2401
2402 flags |= DRM_MODE_PROP_OBJECT;
2403
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002404 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
2405 return NULL;
2406
Rob Clark98f75de2014-05-30 11:37:03 -04002407 property = drm_property_create(dev, flags, name, 1);
2408 if (!property)
2409 return NULL;
2410
2411 property->values[0] = type;
2412
2413 return property;
2414}
2415EXPORT_SYMBOL(drm_property_create_object);
2416
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002417/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01002418 * drm_property_create_bool - create a new boolean property type
2419 * @dev: drm device
2420 * @flags: flags specifying the property type
2421 * @name: name of the property
2422 *
2423 * This creates a new generic drm property which can then be attached to a drm
2424 * object with drm_object_attach_property. The returned property object must be
2425 * freed with drm_property_destroy.
2426 *
2427 * This is implemented as a ranged property with only {0, 1} as valid values.
2428 *
2429 * Returns:
2430 * A pointer to the newly created property on success, NULL on failure.
2431 */
2432struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
2433 const char *name)
2434{
2435 return drm_property_create_range(dev, flags, name, 0, 1);
2436}
2437EXPORT_SYMBOL(drm_property_create_bool);
2438
2439/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002440 * drm_property_add_enum - add a possible value to an enumeration property
2441 * @property: enumeration property to change
2442 * @index: index of the new enumeration
2443 * @value: value of the new enumeration
2444 * @name: symbolic name of the new enumeration
2445 *
2446 * This functions adds enumerations to a property.
2447 *
2448 * It's use is deprecated, drivers should use one of the more specific helpers
2449 * to directly create the property with all enumerations already attached.
2450 *
2451 * Returns:
2452 * Zero on success, error code on failure.
2453 */
Dave Airlief453ba02008-11-07 14:05:41 -08002454int drm_property_add_enum(struct drm_property *property, int index,
2455 uint64_t value, const char *name)
2456{
2457 struct drm_property_enum *prop_enum;
2458
Rob Clark5ea22f22014-05-30 11:34:01 -04002459 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2460 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06002461 return -EINVAL;
2462
2463 /*
2464 * Bitmask enum properties have the additional constraint of values
2465 * from 0 to 63
2466 */
Rob Clark5ea22f22014-05-30 11:34:01 -04002467 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
2468 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002469 return -EINVAL;
2470
Daniel Vetter3758b342014-11-19 18:38:10 +01002471 if (!list_empty(&property->enum_list)) {
2472 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08002473 if (prop_enum->value == value) {
2474 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2475 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2476 return 0;
2477 }
2478 }
2479 }
2480
2481 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2482 if (!prop_enum)
2483 return -ENOMEM;
2484
2485 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2486 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2487 prop_enum->value = value;
2488
2489 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01002490 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08002491 return 0;
2492}
2493EXPORT_SYMBOL(drm_property_add_enum);
2494
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002495/**
2496 * drm_property_destroy - destroy a drm property
2497 * @dev: drm device
2498 * @property: property to destry
2499 *
2500 * This function frees a property including any attached resources like
2501 * enumeration values.
2502 */
Dave Airlief453ba02008-11-07 14:05:41 -08002503void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2504{
2505 struct drm_property_enum *prop_enum, *pt;
2506
Daniel Vetter3758b342014-11-19 18:38:10 +01002507 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08002508 list_del(&prop_enum->head);
2509 kfree(prop_enum);
2510 }
2511
2512 if (property->num_values)
2513 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10002514 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08002515 list_del(&property->head);
2516 kfree(property);
2517}
2518EXPORT_SYMBOL(drm_property_destroy);
2519
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002520/**
2521 * drm_object_attach_property - attach a property to a modeset object
2522 * @obj: drm modeset object
2523 * @property: property to attach
2524 * @init_val: initial value of the property
2525 *
2526 * This attaches the given property to the modeset object with the given initial
2527 * value. Currently this function cannot fail since the properties are stored in
2528 * a statically sized array.
2529 */
Paulo Zanonic5431882012-05-15 18:09:02 -03002530void drm_object_attach_property(struct drm_mode_object *obj,
2531 struct drm_property *property,
2532 uint64_t init_val)
2533{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002534 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002535
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002536 if (count == DRM_OBJECT_MAX_PROPERTY) {
2537 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2538 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2539 "you see this message on the same object type.\n",
2540 obj->type);
2541 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002542 }
2543
Rob Clarkb17cd752014-12-16 18:05:30 -05002544 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002545 obj->properties->values[count] = init_val;
2546 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05002547 if (property->flags & DRM_MODE_PROP_ATOMIC)
2548 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002549}
2550EXPORT_SYMBOL(drm_object_attach_property);
2551
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002552/**
2553 * drm_object_property_set_value - set the value of a property
2554 * @obj: drm mode object to set property value for
2555 * @property: property to set
2556 * @val: value the property should be set to
2557 *
2558 * This functions sets a given property on a given object. This function only
2559 * changes the software state of the property, it does not call into the
2560 * driver's ->set_property callback.
2561 *
2562 * Returns:
2563 * Zero on success, error code on failure.
2564 */
Paulo Zanonic5431882012-05-15 18:09:02 -03002565int drm_object_property_set_value(struct drm_mode_object *obj,
2566 struct drm_property *property, uint64_t val)
2567{
2568 int i;
2569
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002570 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05002571 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002572 obj->properties->values[i] = val;
2573 return 0;
2574 }
2575 }
2576
2577 return -EINVAL;
2578}
2579EXPORT_SYMBOL(drm_object_property_set_value);
2580
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002581/**
2582 * drm_object_property_get_value - retrieve the value of a property
2583 * @obj: drm mode object to get property value from
2584 * @property: property to retrieve
2585 * @val: storage for the property value
2586 *
2587 * This function retrieves the softare state of the given property for the given
2588 * property. Since there is no driver callback to retrieve the current property
2589 * value this might be out of sync with the hardware, depending upon the driver
2590 * and property.
2591 *
2592 * Returns:
2593 * Zero on success, error code on failure.
2594 */
Paulo Zanonic5431882012-05-15 18:09:02 -03002595int drm_object_property_get_value(struct drm_mode_object *obj,
2596 struct drm_property *property, uint64_t *val)
2597{
2598 int i;
2599
Rob Clark88a48e22014-12-18 16:01:50 -05002600 /* read-only properties bypass atomic mechanism and still store
2601 * their value in obj->properties->values[].. mostly to avoid
2602 * having to deal w/ EDID and similar props in atomic paths:
2603 */
2604 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
2605 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
2606 return drm_atomic_get_property(obj, property, val);
2607
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002608 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05002609 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002610 *val = obj->properties->values[i];
2611 return 0;
2612 }
2613 }
2614
2615 return -EINVAL;
2616}
2617EXPORT_SYMBOL(drm_object_property_get_value);
2618
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002619/**
Daniel Vetter1a498632014-11-19 18:38:09 +01002620 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002621 * @dev: DRM device
2622 * @data: ioctl data
2623 * @file_priv: DRM file info
2624 *
Daniel Vetter1a498632014-11-19 18:38:09 +01002625 * This function retrieves the metadata for a given property, like the different
2626 * possible values for an enum property or the limits for a range property.
2627 *
2628 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002629 *
2630 * Called by the user via ioctl.
2631 *
2632 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002633 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002634 */
Dave Airlief453ba02008-11-07 14:05:41 -08002635int drm_mode_getproperty_ioctl(struct drm_device *dev,
2636 void *data, struct drm_file *file_priv)
2637{
Dave Airlief453ba02008-11-07 14:05:41 -08002638 struct drm_mode_get_property *out_resp = data;
2639 struct drm_property *property;
2640 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002641 int value_count = 0;
2642 int ret = 0, i;
2643 int copied;
2644 struct drm_property_enum *prop_enum;
2645 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002646 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002647
Dave Airliefb3b06c2011-02-08 13:55:21 +10002648 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2649 return -EINVAL;
2650
Daniel Vetter84849902012-12-02 00:28:11 +01002651 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002652 property = drm_property_find(dev, out_resp->prop_id);
2653 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002654 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002655 goto done;
2656 }
Dave Airlief453ba02008-11-07 14:05:41 -08002657
Rob Clark5ea22f22014-05-30 11:34:01 -04002658 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2659 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01002660 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08002661 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002662 }
2663
2664 value_count = property->num_values;
2665
2666 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2667 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2668 out_resp->flags = property->flags;
2669
2670 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002671 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002672 for (i = 0; i < value_count; i++) {
2673 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2674 ret = -EFAULT;
2675 goto done;
2676 }
2677 }
2678 }
2679 out_resp->count_values = value_count;
2680
Rob Clark5ea22f22014-05-30 11:34:01 -04002681 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
2682 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002683 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2684 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002685 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01002686 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08002687
2688 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2689 ret = -EFAULT;
2690 goto done;
2691 }
2692
2693 if (copy_to_user(&enum_ptr[copied].name,
2694 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2695 ret = -EFAULT;
2696 goto done;
2697 }
2698 copied++;
2699 }
2700 }
2701 out_resp->count_enum_blobs = enum_count;
2702 }
2703
Daniel Vetter3758b342014-11-19 18:38:10 +01002704 /*
2705 * NOTE: The idea seems to have been to use this to read all the blob
2706 * property values. But nothing ever added them to the corresponding
2707 * list, userspace always used the special-purpose get_blob ioctl to
2708 * read the value for a blob property. It also doesn't make a lot of
2709 * sense to return values here when everything else is just metadata for
2710 * the property itself.
2711 */
2712 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
2713 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002714done:
Daniel Vetter84849902012-12-02 00:28:11 +01002715 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002716 return ret;
2717}
2718
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002719static void drm_property_free_blob(struct kref *kref)
2720{
2721 struct drm_property_blob *blob =
2722 container_of(kref, struct drm_property_blob, base.refcount);
2723
2724 mutex_lock(&blob->dev->mode_config.blob_lock);
2725 list_del(&blob->head_global);
2726 mutex_unlock(&blob->dev->mode_config.blob_lock);
2727
2728 drm_mode_object_unregister(blob->dev, &blob->base);
2729
2730 kfree(blob);
2731}
2732
Daniel Stone99531d92015-05-22 13:34:49 +01002733/**
2734 * drm_property_create_blob - Create new blob property
2735 *
2736 * Creates a new blob property for a specified DRM device, optionally
2737 * copying data.
2738 *
2739 * @dev: DRM device to create property for
2740 * @length: Length to allocate for blob data
2741 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01002742 *
2743 * Returns:
2744 * New blob property with a single reference on success, or an ERR_PTR
2745 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01002746 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01002747struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02002748drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02002749 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08002750{
2751 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002752 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002753
Dan Carpenter9ac09342015-10-29 16:37:54 +03002754 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01002755 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08002756
2757 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2758 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01002759 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08002760
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002761 /* This must be explicitly initialised, so we can safely call list_del
2762 * on it in the removal handler, even if it isn't in a file list. */
2763 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08002764 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01002765 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08002766
Daniel Stone99531d92015-05-22 13:34:49 +01002767 if (data)
2768 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08002769
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002770 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
2771 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01002772 if (ret) {
2773 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01002774 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01002775 }
2776
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002777 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002778 list_add_tail(&blob->head_global,
2779 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01002780 mutex_unlock(&dev->mode_config.blob_lock);
2781
Dave Airlief453ba02008-11-07 14:05:41 -08002782 return blob;
2783}
Daniel Stone6bcacf52015-04-20 19:22:55 +01002784EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08002785
Daniel Stone6bcacf52015-04-20 19:22:55 +01002786/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01002787 * drm_property_unreference_blob - Unreference a blob property
2788 *
2789 * Drop a reference on a blob property. May free the object.
2790 *
Daniel Stonef102c162015-05-22 13:34:44 +01002791 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01002792 */
2793void drm_property_unreference_blob(struct drm_property_blob *blob)
2794{
Daniel Stone6bcacf52015-04-20 19:22:55 +01002795 if (!blob)
2796 return;
2797
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002798 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01002799}
2800EXPORT_SYMBOL(drm_property_unreference_blob);
2801
2802/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002803 * drm_property_destroy_user_blobs - destroy all blobs created by this client
2804 * @dev: DRM device
2805 * @file_priv: destroy all blobs owned by this file handle
2806 */
2807void drm_property_destroy_user_blobs(struct drm_device *dev,
2808 struct drm_file *file_priv)
2809{
2810 struct drm_property_blob *blob, *bt;
2811
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002812 /*
2813 * When the file gets released that means no one else can access the
2814 * blob list any more, so no need to grab dev->blob_lock.
2815 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002816 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
2817 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002818 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002819 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002820}
2821
2822/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01002823 * drm_property_reference_blob - Take a reference on an existing property
2824 *
2825 * Take a new reference on an existing blob property.
2826 *
Daniel Stonef102c162015-05-22 13:34:44 +01002827 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01002828 */
2829struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
2830{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002831 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01002832 return blob;
2833}
2834EXPORT_SYMBOL(drm_property_reference_blob);
2835
Daniel Stone6bcacf52015-04-20 19:22:55 +01002836/**
2837 * drm_property_lookup_blob - look up a blob property and take a reference
2838 * @dev: drm device
2839 * @id: id of the blob property
2840 *
2841 * If successful, this takes an additional reference to the blob property.
2842 * callers need to make sure to eventually unreference the returned property
2843 * again, using @drm_property_unreference_blob.
2844 */
2845struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
2846 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08002847{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002848 struct drm_mode_object *obj;
2849 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01002850
Daniel Vetter7520a272016-08-15 16:07:02 +02002851 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_BLOB);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002852 if (obj)
2853 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01002854 return blob;
2855}
2856EXPORT_SYMBOL(drm_property_lookup_blob);
2857
2858/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01002859 * drm_property_replace_global_blob - atomically replace existing blob property
2860 * @dev: drm device
2861 * @replace: location of blob property pointer to be replaced
2862 * @length: length of data for new blob, or 0 for no data
2863 * @data: content for new blob, or NULL for no data
2864 * @obj_holds_id: optional object for property holding blob ID
2865 * @prop_holds_id: optional property holding blob ID
2866 * @return 0 on success or error on failure
2867 *
2868 * This function will atomically replace a global property in the blob list,
2869 * optionally updating a property which holds the ID of that property. It is
2870 * guaranteed to be atomic: no caller will be allowed to see intermediate
2871 * results, and either the entire operation will succeed and clean up the
2872 * previous property, or it will fail and the state will be unchanged.
2873 *
2874 * If length is 0 or data is NULL, no new blob will be created, and the holding
2875 * property, if specified, will be set to 0.
2876 *
2877 * Access to the replace pointer is assumed to be protected by the caller, e.g.
2878 * by holding the relevant modesetting object lock for its parent.
2879 *
2880 * For example, a drm_connector has a 'PATH' property, which contains the ID
2881 * of a blob property with the value of the MST path information. Calling this
2882 * function with replace pointing to the connector's path_blob_ptr, length and
2883 * data set for the new path information, obj_holds_id set to the connector's
2884 * base object, and prop_holds_id set to the path property name, will perform
2885 * a completely atomic update. The access to path_blob_ptr is protected by the
2886 * caller holding a lock on the connector.
2887 */
Daniel Vetteradebd6f2016-08-12 22:48:49 +02002888int drm_property_replace_global_blob(struct drm_device *dev,
2889 struct drm_property_blob **replace,
2890 size_t length,
2891 const void *data,
2892 struct drm_mode_object *obj_holds_id,
2893 struct drm_property *prop_holds_id)
Daniel Stoned2ed3432015-04-20 19:22:53 +01002894{
2895 struct drm_property_blob *new_blob = NULL;
2896 struct drm_property_blob *old_blob = NULL;
2897 int ret;
2898
2899 WARN_ON(replace == NULL);
2900
2901 old_blob = *replace;
2902
2903 if (length && data) {
2904 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01002905 if (IS_ERR(new_blob))
2906 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01002907 }
2908
2909 /* This does not need to be synchronised with blob_lock, as the
2910 * get_properties ioctl locks all modesetting objects, and
2911 * obj_holds_id must be locked before calling here, so we cannot
2912 * have its value out of sync with the list membership modified
2913 * below under blob_lock. */
2914 if (obj_holds_id) {
2915 ret = drm_object_property_set_value(obj_holds_id,
2916 prop_holds_id,
2917 new_blob ?
2918 new_blob->base.id : 0);
2919 if (ret != 0)
2920 goto err_created;
2921 }
2922
Markus Elfringec530822015-07-05 21:55:10 +02002923 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01002924 *replace = new_blob;
2925
2926 return 0;
2927
2928err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01002929 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01002930 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002931}
Daniel Vetteradebd6f2016-08-12 22:48:49 +02002932EXPORT_SYMBOL(drm_property_replace_global_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08002933
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002934/**
2935 * drm_mode_getblob_ioctl - get the contents of a blob property value
2936 * @dev: DRM device
2937 * @data: ioctl data
2938 * @file_priv: DRM file info
2939 *
2940 * This function retrieves the contents of a blob property. The value stored in
2941 * an object's blob property is just a normal modeset object id.
2942 *
2943 * Called by the user via ioctl.
2944 *
2945 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002946 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002947 */
Dave Airlief453ba02008-11-07 14:05:41 -08002948int drm_mode_getblob_ioctl(struct drm_device *dev,
2949 void *data, struct drm_file *file_priv)
2950{
Dave Airlief453ba02008-11-07 14:05:41 -08002951 struct drm_mode_get_blob *out_resp = data;
2952 struct drm_property_blob *blob;
2953 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002954 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002955
Dave Airliefb3b06c2011-02-08 13:55:21 +10002956 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2957 return -EINVAL;
2958
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002959 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
2960 if (!blob)
2961 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002962
2963 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002964 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002965 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002966 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002967 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08002968 }
2969 }
2970 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02002971unref:
2972 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08002973
Dave Airlief453ba02008-11-07 14:05:41 -08002974 return ret;
2975}
2976
Dave Airliecc7096f2014-10-22 12:03:04 +10002977/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01002978 * drm_mode_createblob_ioctl - create a new blob property
2979 * @dev: DRM device
2980 * @data: ioctl data
2981 * @file_priv: DRM file info
2982 *
2983 * This function creates a new blob property with user-defined values. In order
2984 * to give us sensible validation and checking when creating, rather than at
2985 * every potential use, we also require a type to be provided upfront.
2986 *
2987 * Called by the user via ioctl.
2988 *
2989 * Returns:
2990 * Zero on success, negative errno on failure.
2991 */
2992int drm_mode_createblob_ioctl(struct drm_device *dev,
2993 void *data, struct drm_file *file_priv)
2994{
2995 struct drm_mode_create_blob *out_resp = data;
2996 struct drm_property_blob *blob;
2997 void __user *blob_ptr;
2998 int ret = 0;
2999
3000 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3001 return -EINVAL;
3002
3003 blob = drm_property_create_blob(dev, out_resp->length, NULL);
3004 if (IS_ERR(blob))
3005 return PTR_ERR(blob);
3006
3007 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3008 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
3009 ret = -EFAULT;
3010 goto out_blob;
3011 }
3012
3013 /* Dropping the lock between create_blob and our access here is safe
3014 * as only the same file_priv can remove the blob; at this point, it is
3015 * not associated with any file_priv. */
3016 mutex_lock(&dev->mode_config.blob_lock);
3017 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04003018 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003019 mutex_unlock(&dev->mode_config.blob_lock);
3020
3021 return 0;
3022
3023out_blob:
3024 drm_property_unreference_blob(blob);
3025 return ret;
3026}
3027
3028/**
3029 * drm_mode_destroyblob_ioctl - destroy a user blob property
3030 * @dev: DRM device
3031 * @data: ioctl data
3032 * @file_priv: DRM file info
3033 *
3034 * Destroy an existing user-defined blob property.
3035 *
3036 * Called by the user via ioctl.
3037 *
3038 * Returns:
3039 * Zero on success, negative errno on failure.
3040 */
3041int drm_mode_destroyblob_ioctl(struct drm_device *dev,
3042 void *data, struct drm_file *file_priv)
3043{
3044 struct drm_mode_destroy_blob *out_resp = data;
3045 struct drm_property_blob *blob = NULL, *bt;
3046 bool found = false;
3047 int ret = 0;
3048
3049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3050 return -EINVAL;
3051
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003052 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
3053 if (!blob)
3054 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003055
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003056 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003057 /* Ensure the property was actually created by this user. */
3058 list_for_each_entry(bt, &file_priv->blobs, head_file) {
3059 if (bt == blob) {
3060 found = true;
3061 break;
3062 }
3063 }
3064
3065 if (!found) {
3066 ret = -EPERM;
3067 goto err;
3068 }
3069
3070 /* We must drop head_file here, because we may not be the last
3071 * reference on the blob. */
3072 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003073 mutex_unlock(&dev->mode_config.blob_lock);
3074
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003075 /* One reference from lookup, and one from the filp. */
3076 drm_property_unreference_blob(blob);
3077 drm_property_unreference_blob(blob);
3078
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003079 return 0;
3080
3081err:
3082 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003083 drm_property_unreference_blob(blob);
3084
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003085 return ret;
3086}
3087
Rob Clark3843e712014-12-16 18:05:29 -05003088/* Some properties could refer to dynamic refcnt'd objects, or things that
3089 * need special locking to handle lifetime issues (ie. to ensure the prop
3090 * value doesn't become invalid part way through the property update due to
3091 * race). The value returned by reference via 'obj' should be passed back
3092 * to drm_property_change_valid_put() after the property is set (and the
3093 * object to which the property is attached has a chance to take it's own
3094 * reference).
3095 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05003096bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05003097 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003098{
Thierry Reding2ca651d2014-12-10 13:03:39 +01003099 int i;
3100
Paulo Zanoni26a34812012-05-15 18:08:59 -03003101 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3102 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003103
Rob Clark3843e712014-12-16 18:05:29 -05003104 *ref = NULL;
3105
Rob Clark5ea22f22014-05-30 11:34:01 -04003106 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003107 if (value < property->values[0] || value > property->values[1])
3108 return false;
3109 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003110 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3111 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01003112
Rob Clarkebc44cf2012-09-12 22:22:31 -05003113 if (svalue < U642I64(property->values[0]) ||
3114 svalue > U642I64(property->values[1]))
3115 return false;
3116 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003117 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003118 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003119
Rob Clark49e27542012-05-17 02:23:26 -06003120 for (i = 0; i < property->num_values; i++)
3121 valid_mask |= (1ULL << property->values[i]);
3122 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003123 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01003124 struct drm_property_blob *blob;
3125
3126 if (value == 0)
3127 return true;
3128
3129 blob = drm_property_lookup_blob(property->dev, value);
3130 if (blob) {
3131 *ref = &blob->base;
3132 return true;
3133 } else {
3134 return false;
3135 }
Rob Clark98f75de2014-05-30 11:37:03 -04003136 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04003137 /* a zero value for an object property translates to null: */
3138 if (value == 0)
3139 return true;
Rob Clark3843e712014-12-16 18:05:29 -05003140
Daniel Vetter7520a272016-08-15 16:07:02 +02003141 *ref = __drm_mode_object_find(property->dev, value,
3142 property->values[0]);
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03003143 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003144 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01003145
3146 for (i = 0; i < property->num_values; i++)
3147 if (property->values[i] == value)
3148 return true;
3149 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003150}
3151
Rob Clarkd34f20d2014-12-18 16:01:56 -05003152void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05003153 struct drm_mode_object *ref)
3154{
3155 if (!ref)
3156 return;
3157
3158 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10003159 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01003160 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3161 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05003162}
3163
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003164static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3165 struct drm_property *property,
3166 uint64_t value)
3167{
3168 int ret = -EINVAL;
3169 struct drm_crtc *crtc = obj_to_crtc(obj);
3170
3171 if (crtc->funcs->set_property)
3172 ret = crtc->funcs->set_property(crtc, property, value);
3173 if (!ret)
3174 drm_object_property_set_value(obj, property, value);
3175
3176 return ret;
3177}
3178
Thomas Wood3a5f87c2014-08-20 14:45:00 +01003179/**
3180 * drm_mode_plane_set_obj_prop - set the value of a property
3181 * @plane: drm plane object to set property value for
3182 * @property: property to set
3183 * @value: value the property should be set to
3184 *
3185 * This functions sets a given property on a given plane object. This function
3186 * calls the driver's ->set_property callback and changes the software state of
3187 * the property if the callback succeeds.
3188 *
3189 * Returns:
3190 * Zero on success, error code on failure.
3191 */
3192int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
3193 struct drm_property *property,
3194 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06003195{
3196 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01003197 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06003198
3199 if (plane->funcs->set_property)
3200 ret = plane->funcs->set_property(plane, property, value);
3201 if (!ret)
3202 drm_object_property_set_value(obj, property, value);
3203
3204 return ret;
3205}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01003206EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06003207
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003208/**
Daniel Vetter1a498632014-11-19 18:38:09 +01003209 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003210 * @dev: DRM device
3211 * @data: ioctl data
3212 * @file_priv: DRM file info
3213 *
3214 * This function retrieves the current value for an object's property. Compared
3215 * to the connector specific ioctl this one is extended to also work on crtc and
3216 * plane objects.
3217 *
3218 * Called by the user via ioctl.
3219 *
3220 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003221 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003222 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003223int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3224 struct drm_file *file_priv)
3225{
3226 struct drm_mode_obj_get_properties *arg = data;
3227 struct drm_mode_object *obj;
3228 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03003229
3230 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3231 return -EINVAL;
3232
Daniel Vetter84849902012-12-02 00:28:11 +01003233 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003234
3235 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3236 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003237 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003238 goto out;
3239 }
3240 if (!obj->properties) {
3241 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02003242 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03003243 }
3244
Daniel Vetter52217192016-08-12 22:48:50 +02003245 ret = drm_mode_object_get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05003246 (uint32_t __user *)(unsigned long)(arg->props_ptr),
3247 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
3248 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03003249
Daniel Vetter1649c332016-04-22 22:10:28 +02003250out_unref:
3251 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03003252out:
Daniel Vetter84849902012-12-02 00:28:11 +01003253 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003254 return ret;
3255}
3256
3257int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3258 struct drm_file *file_priv)
3259{
3260 struct drm_mode_obj_set_property *arg = data;
3261 struct drm_mode_object *arg_obj;
3262 struct drm_mode_object *prop_obj;
3263 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05003264 int i, ret = -EINVAL;
3265 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03003266
3267 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3268 return -EINVAL;
3269
Daniel Vetter84849902012-12-02 00:28:11 +01003270 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003271
3272 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003273 if (!arg_obj) {
3274 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003275 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003276 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003277 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02003278 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03003279
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003280 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05003281 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03003282 break;
3283
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003284 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02003285 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03003286
3287 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3288 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003289 if (!prop_obj) {
3290 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02003291 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003292 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003293 property = obj_to_property(prop_obj);
3294
Rob Clark3843e712014-12-16 18:05:29 -05003295 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10003296 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03003297
3298 switch (arg_obj->type) {
3299 case DRM_MODE_OBJECT_CONNECTOR:
3300 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3301 arg->value);
3302 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003303 case DRM_MODE_OBJECT_CRTC:
3304 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3305 break;
Rob Clark4d939142012-05-17 02:23:27 -06003306 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01003307 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
3308 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06003309 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003310 }
3311
Rob Clark3843e712014-12-16 18:05:29 -05003312 drm_property_change_valid_put(property, ref);
3313
Daniel Vetter1649c332016-04-22 22:10:28 +02003314out_unref:
3315 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03003316out:
Daniel Vetter84849902012-12-02 00:28:11 +01003317 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003318 return ret;
3319}
3320
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003321/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003322 * drm_mode_crtc_set_gamma_size - set the gamma table size
3323 * @crtc: CRTC to set the gamma table size for
3324 * @gamma_size: size of the gamma table
3325 *
3326 * Drivers which support gamma tables should set this to the supported gamma
3327 * table size when initializing the CRTC. Currently the drm core only supports a
3328 * fixed gamma table size.
3329 *
3330 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003331 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003332 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003333int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003334 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08003335{
Daniel Vettercf48e292016-03-30 11:51:16 +02003336 uint16_t *r_base, *g_base, *b_base;
3337 int i;
3338
Dave Airlief453ba02008-11-07 14:05:41 -08003339 crtc->gamma_size = gamma_size;
3340
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003341 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
3342 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003343 if (!crtc->gamma_store) {
3344 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003345 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003346 }
3347
Daniel Vettercf48e292016-03-30 11:51:16 +02003348 r_base = crtc->gamma_store;
3349 g_base = r_base + gamma_size;
3350 b_base = g_base + gamma_size;
3351 for (i = 0; i < gamma_size; i++) {
3352 r_base[i] = i << 8;
3353 g_base[i] = i << 8;
3354 b_base[i] = i << 8;
3355 }
3356
3357
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003358 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003359}
3360EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3361
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003362/**
3363 * drm_mode_gamma_set_ioctl - set the gamma table
3364 * @dev: DRM device
3365 * @data: ioctl data
3366 * @file_priv: DRM file info
3367 *
3368 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3369 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3370 *
3371 * Called by the user via ioctl.
3372 *
3373 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003374 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003375 */
Dave Airlief453ba02008-11-07 14:05:41 -08003376int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3377 void *data, struct drm_file *file_priv)
3378{
3379 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003380 struct drm_crtc *crtc;
3381 void *r_base, *g_base, *b_base;
3382 int size;
3383 int ret = 0;
3384
Dave Airliefb3b06c2011-02-08 13:55:21 +10003385 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3386 return -EINVAL;
3387
Daniel Vetter84849902012-12-02 00:28:11 +01003388 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003389 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
3390 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003391 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003392 goto out;
3393 }
Dave Airlief453ba02008-11-07 14:05:41 -08003394
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003395 if (crtc->funcs->gamma_set == NULL) {
3396 ret = -ENOSYS;
3397 goto out;
3398 }
3399
Dave Airlief453ba02008-11-07 14:05:41 -08003400 /* memcpy into gamma store */
3401 if (crtc_lut->gamma_size != crtc->gamma_size) {
3402 ret = -EINVAL;
3403 goto out;
3404 }
3405
3406 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3407 r_base = crtc->gamma_store;
3408 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3409 ret = -EFAULT;
3410 goto out;
3411 }
3412
3413 g_base = r_base + size;
3414 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3415 ret = -EFAULT;
3416 goto out;
3417 }
3418
3419 b_base = g_base + size;
3420 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3421 ret = -EFAULT;
3422 goto out;
3423 }
3424
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003425 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003426
3427out:
Daniel Vetter84849902012-12-02 00:28:11 +01003428 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003429 return ret;
3430
3431}
3432
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003433/**
3434 * drm_mode_gamma_get_ioctl - get the gamma table
3435 * @dev: DRM device
3436 * @data: ioctl data
3437 * @file_priv: DRM file info
3438 *
3439 * Copy the current gamma table into the storage provided. This also provides
3440 * the gamma table size the driver expects, which can be used to size the
3441 * allocated storage.
3442 *
3443 * Called by the user via ioctl.
3444 *
3445 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003446 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003447 */
Dave Airlief453ba02008-11-07 14:05:41 -08003448int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3449 void *data, struct drm_file *file_priv)
3450{
3451 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003452 struct drm_crtc *crtc;
3453 void *r_base, *g_base, *b_base;
3454 int size;
3455 int ret = 0;
3456
Dave Airliefb3b06c2011-02-08 13:55:21 +10003457 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3458 return -EINVAL;
3459
Daniel Vetter84849902012-12-02 00:28:11 +01003460 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003461 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
3462 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003463 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003464 goto out;
3465 }
Dave Airlief453ba02008-11-07 14:05:41 -08003466
3467 /* memcpy into gamma store */
3468 if (crtc_lut->gamma_size != crtc->gamma_size) {
3469 ret = -EINVAL;
3470 goto out;
3471 }
3472
3473 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3474 r_base = crtc->gamma_store;
3475 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3476 ret = -EFAULT;
3477 goto out;
3478 }
3479
3480 g_base = r_base + size;
3481 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3482 ret = -EFAULT;
3483 goto out;
3484 }
3485
3486 b_base = g_base + size;
3487 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3488 ret = -EFAULT;
3489 goto out;
3490 }
3491out:
Daniel Vetter84849902012-12-02 00:28:11 +01003492 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003493 return ret;
3494}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003495
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003496/**
3497 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
3498 * @dev: DRM device
3499 * @data: ioctl data
3500 * @file_priv: DRM file info
3501 *
3502 * This schedules an asynchronous update on a given CRTC, called page flip.
3503 * Optionally a drm event is generated to signal the completion of the event.
3504 * Generic drivers cannot assume that a pageflip with changed framebuffer
3505 * properties (including driver specific metadata like tiling layout) will work,
3506 * but some drivers support e.g. pixel format changes through the pageflip
3507 * ioctl.
3508 *
3509 * Called by the user via ioctl.
3510 *
3511 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003512 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003513 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003514int drm_mode_page_flip_ioctl(struct drm_device *dev,
3515 void *data, struct drm_file *file_priv)
3516{
3517 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003518 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02003519 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003520 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003521 int ret = -EINVAL;
3522
3523 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3524 page_flip->reserved != 0)
3525 return -EINVAL;
3526
Keith Packard62f21042013-07-22 18:50:00 -07003527 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3528 return -EINVAL;
3529
Rob Clarka2b34e22013-10-05 16:36:52 -04003530 crtc = drm_crtc_find(dev, page_flip->crtc_id);
3531 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003532 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003533
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003534 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07003535 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01003536 /* The framebuffer is currently unbound, presumably
3537 * due to a hotplug event, that userspace has not
3538 * yet discovered.
3539 */
3540 ret = -EBUSY;
3541 goto out;
3542 }
3543
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003544 if (crtc->funcs->page_flip == NULL)
3545 goto out;
3546
Daniel Vetter786b99e2012-12-02 21:53:40 +01003547 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003548 if (!fb) {
3549 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003550 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003551 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003552
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03003553 if (crtc->state) {
3554 const struct drm_plane_state *state = crtc->primary->state;
3555
3556 ret = check_src_coords(state->src_x, state->src_y,
3557 state->src_w, state->src_h, fb);
3558 } else {
3559 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3560 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01003561 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003562 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003563
Matt Roperf4510a22014-04-01 15:22:40 -07003564 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003565 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3566 ret = -EINVAL;
3567 goto out;
3568 }
3569
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003570 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01003571 e = kzalloc(sizeof *e, GFP_KERNEL);
3572 if (!e) {
3573 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003574 goto out;
3575 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003576 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01003577 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003578 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01003579 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
3580 if (ret) {
3581 kfree(e);
3582 goto out;
3583 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003584 }
3585
Daniel Vetter3d30a592014-07-27 13:42:42 +02003586 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07003587 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003588 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01003589 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
3590 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003591 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02003592 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003593 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02003594 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003595 /* Unref only the old framebuffer. */
3596 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003597 }
3598
3599out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003600 if (fb)
3601 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02003602 if (crtc->primary->old_fb)
3603 drm_framebuffer_unreference(crtc->primary->old_fb);
3604 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02003605 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003606
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003607 return ret;
3608}
Chris Wilsoneb033552011-01-24 15:11:08 +00003609
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003610/**
3611 * drm_mode_config_reset - call ->reset callbacks
3612 * @dev: drm device
3613 *
3614 * This functions calls all the crtc's, encoder's and connector's ->reset
3615 * callback. Drivers can use this in e.g. their driver load or resume code to
3616 * reset hardware and software state.
3617 */
Chris Wilsoneb033552011-01-24 15:11:08 +00003618void drm_mode_config_reset(struct drm_device *dev)
3619{
3620 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02003621 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00003622 struct drm_encoder *encoder;
3623 struct drm_connector *connector;
3624
Daniel Vettere4f62542015-07-09 23:44:35 +02003625 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02003626 if (plane->funcs->reset)
3627 plane->funcs->reset(plane);
3628
Daniel Vettere4f62542015-07-09 23:44:35 +02003629 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00003630 if (crtc->funcs->reset)
3631 crtc->funcs->reset(crtc);
3632
Daniel Vettere4f62542015-07-09 23:44:35 +02003633 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00003634 if (encoder->funcs->reset)
3635 encoder->funcs->reset(encoder);
3636
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02003637 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10003638 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00003639 if (connector->funcs->reset)
3640 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02003641 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00003642}
3643EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003644
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003645/**
3646 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
3647 * @dev: DRM device
3648 * @data: ioctl data
3649 * @file_priv: DRM file info
3650 *
3651 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
3652 * TTM or something else entirely) and returns the resulting buffer handle. This
3653 * handle can then be wrapped up into a framebuffer modeset object.
3654 *
3655 * Note that userspace is not allowed to use such objects for render
3656 * acceleration - drivers must create their own private ioctls for such a use
3657 * case.
3658 *
3659 * Called by the user via ioctl.
3660 *
3661 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003662 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003663 */
Dave Airlieff72145b2011-02-07 12:16:14 +10003664int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3665 void *data, struct drm_file *file_priv)
3666{
3667 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01003668 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10003669
3670 if (!dev->driver->dumb_create)
3671 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01003672 if (!args->width || !args->height || !args->bpp)
3673 return -EINVAL;
3674
3675 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02003676 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01003677 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02003678 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01003679 return -EINVAL;
3680 stride = cpp * args->width;
3681 if (args->height > 0xffffffffU / stride)
3682 return -EINVAL;
3683
3684 /* test for wrap-around */
3685 size = args->height * stride;
3686 if (PAGE_ALIGN(size) == 0)
3687 return -EINVAL;
3688
Thierry Redingf6085952014-11-03 11:14:14 +01003689 /*
3690 * handle, pitch and size are output parameters. Zero them out to
3691 * prevent drivers from accidentally using uninitialized data. Since
3692 * not all existing userspace is clearing these fields properly we
3693 * cannot reject IOCTL with garbage in them.
3694 */
3695 args->handle = 0;
3696 args->pitch = 0;
3697 args->size = 0;
3698
Dave Airlieff72145b2011-02-07 12:16:14 +10003699 return dev->driver->dumb_create(file_priv, dev, args);
3700}
3701
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003702/**
3703 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
3704 * @dev: DRM device
3705 * @data: ioctl data
3706 * @file_priv: DRM file info
3707 *
3708 * Allocate an offset in the drm device node's address space to be able to
3709 * memory map a dumb buffer.
3710 *
3711 * Called by the user via ioctl.
3712 *
3713 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003714 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003715 */
Dave Airlieff72145b2011-02-07 12:16:14 +10003716int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3717 void *data, struct drm_file *file_priv)
3718{
3719 struct drm_mode_map_dumb *args = data;
3720
3721 /* call driver ioctl to get mmap offset */
3722 if (!dev->driver->dumb_map_offset)
3723 return -ENOSYS;
3724
3725 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3726}
3727
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003728/**
3729 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
3730 * @dev: DRM device
3731 * @data: ioctl data
3732 * @file_priv: DRM file info
3733 *
3734 * This destroys the userspace handle for the given dumb backing storage buffer.
3735 * Since buffer objects must be reference counted in the kernel a buffer object
3736 * won't be immediately freed if a framebuffer modeset object still uses it.
3737 *
3738 * Called by the user via ioctl.
3739 *
3740 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003741 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003742 */
Dave Airlieff72145b2011-02-07 12:16:14 +10003743int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3744 void *data, struct drm_file *file_priv)
3745{
3746 struct drm_mode_destroy_dumb *args = data;
3747
3748 if (!dev->driver->dumb_destroy)
3749 return -ENOSYS;
3750
3751 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3752}
Dave Airlie248dbc22011-11-29 20:02:54 +00003753
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003754/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05303755 * drm_rotation_simplify() - Try to simplify the rotation
3756 * @rotation: Rotation to be simplified
3757 * @supported_rotations: Supported rotations
3758 *
3759 * Attempt to simplify the rotation to a form that is supported.
3760 * Eg. if the hardware supports everything except DRM_REFLECT_X
3761 * one could call this function like this:
3762 *
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003763 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
3764 * DRM_ROTATE_90 | DRM_ROTATE_180 |
3765 * DRM_ROTATE_270 | DRM_REFLECT_Y);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05303766 *
3767 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
3768 * transforms the hardware supports, this function may not
3769 * be able to produce a supported transform, so the caller should
3770 * check the result afterwards.
3771 */
3772unsigned int drm_rotation_simplify(unsigned int rotation,
3773 unsigned int supported_rotations)
3774{
3775 if (rotation & ~supported_rotations) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003776 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
Joonas Lahtinen14152c82015-10-01 10:00:58 +03003777 rotation = (rotation & DRM_REFLECT_MASK) |
3778 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05303779 }
3780
3781 return rotation;
3782}
3783EXPORT_SYMBOL(drm_rotation_simplify);
3784
3785/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003786 * drm_mode_config_init - initialize DRM mode_configuration structure
3787 * @dev: DRM device
3788 *
3789 * Initialize @dev's mode_config structure, used for tracking the graphics
3790 * configuration of @dev.
3791 *
3792 * Since this initializes the modeset locks, no locking is possible. Which is no
3793 * problem, since this should happen single threaded at init time. It is the
3794 * driver's problem to ensure this guarantee.
3795 *
3796 */
3797void drm_mode_config_init(struct drm_device *dev)
3798{
3799 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05003800 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003801 mutex_init(&dev->mode_config.idr_mutex);
3802 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01003803 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003804 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3805 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3806 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3807 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3808 INIT_LIST_HEAD(&dev->mode_config.property_list);
3809 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3810 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3811 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10003812 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01003813 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003814
3815 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05003816 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003817 drm_modeset_unlock_all(dev);
3818
3819 /* Just to be sure */
3820 dev->mode_config.num_fb = 0;
3821 dev->mode_config.num_connector = 0;
3822 dev->mode_config.num_crtc = 0;
3823 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07003824 dev->mode_config.num_overlay_plane = 0;
3825 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003826}
3827EXPORT_SYMBOL(drm_mode_config_init);
3828
3829/**
3830 * drm_mode_config_cleanup - free up DRM mode_config info
3831 * @dev: DRM device
3832 *
3833 * Free up all the connectors and CRTCs associated with this DRM device, then
3834 * free up the framebuffers and associated buffer objects.
3835 *
3836 * Note that since this /should/ happen single-threaded at driver/device
3837 * teardown time, no locking is required. It's the driver's job to ensure that
3838 * this guarantee actually holds true.
3839 *
3840 * FIXME: cleanup any dangling user buffer objects too
3841 */
3842void drm_mode_config_cleanup(struct drm_device *dev)
3843{
3844 struct drm_connector *connector, *ot;
3845 struct drm_crtc *crtc, *ct;
3846 struct drm_encoder *encoder, *enct;
3847 struct drm_framebuffer *fb, *fbt;
3848 struct drm_property *property, *pt;
3849 struct drm_property_blob *blob, *bt;
3850 struct drm_plane *plane, *plt;
3851
3852 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3853 head) {
3854 encoder->funcs->destroy(encoder);
3855 }
3856
3857 list_for_each_entry_safe(connector, ot,
3858 &dev->mode_config.connector_list, head) {
3859 connector->funcs->destroy(connector);
3860 }
3861
3862 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3863 head) {
3864 drm_property_destroy(dev, property);
3865 }
3866
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01003867 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3868 head) {
3869 plane->funcs->destroy(plane);
3870 }
3871
3872 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3873 crtc->funcs->destroy(crtc);
3874 }
3875
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003876 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003877 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01003878 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003879 }
3880
3881 /*
3882 * Single-threaded teardown context, so it's not required to grab the
3883 * fb_lock to protect against concurrent fb_list access. Contrary, it
3884 * would actually deadlock with the drm_framebuffer_cleanup function.
3885 *
3886 * Also, if there are any framebuffers left, that's a driver leak now,
3887 * so politely WARN about this.
3888 */
3889 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3890 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10003891 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003892 }
3893
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01003894 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10003895 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003896 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05003897 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003898}
3899EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05303900
3901struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
3902 unsigned int supported_rotations)
3903{
3904 static const struct drm_prop_enum_list props[] = {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003905 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
3906 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
3907 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
3908 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
3909 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
3910 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05303911 };
3912
3913 return drm_property_create_bitmask(dev, 0, "rotation",
3914 props, ARRAY_SIZE(props),
3915 supported_rotations);
3916}
3917EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10003918
3919/**
3920 * DOC: Tile group
3921 *
3922 * Tile groups are used to represent tiled monitors with a unique
3923 * integer identifier. Tiled monitors using DisplayID v1.3 have
3924 * a unique 8-byte handle, we store this in a tile group, so we
3925 * have a common identifier for all tiles in a monitor group.
3926 */
3927static void drm_tile_group_free(struct kref *kref)
3928{
3929 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
3930 struct drm_device *dev = tg->dev;
3931 mutex_lock(&dev->mode_config.idr_mutex);
3932 idr_remove(&dev->mode_config.tile_idr, tg->id);
3933 mutex_unlock(&dev->mode_config.idr_mutex);
3934 kfree(tg);
3935}
3936
3937/**
3938 * drm_mode_put_tile_group - drop a reference to a tile group.
3939 * @dev: DRM device
3940 * @tg: tile group to drop reference to.
3941 *
3942 * drop reference to tile group and free if 0.
3943 */
3944void drm_mode_put_tile_group(struct drm_device *dev,
3945 struct drm_tile_group *tg)
3946{
3947 kref_put(&tg->refcount, drm_tile_group_free);
3948}
3949
3950/**
3951 * drm_mode_get_tile_group - get a reference to an existing tile group
3952 * @dev: DRM device
3953 * @topology: 8-bytes unique per monitor.
3954 *
3955 * Use the unique bytes to get a reference to an existing tile group.
3956 *
3957 * RETURNS:
3958 * tile group or NULL if not found.
3959 */
3960struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
3961 char topology[8])
3962{
3963 struct drm_tile_group *tg;
3964 int id;
3965 mutex_lock(&dev->mode_config.idr_mutex);
3966 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
3967 if (!memcmp(tg->group_data, topology, 8)) {
3968 if (!kref_get_unless_zero(&tg->refcount))
3969 tg = NULL;
3970 mutex_unlock(&dev->mode_config.idr_mutex);
3971 return tg;
3972 }
3973 }
3974 mutex_unlock(&dev->mode_config.idr_mutex);
3975 return NULL;
3976}
Rob Clark81ddd1b2015-03-27 13:01:52 -04003977EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10003978
3979/**
3980 * drm_mode_create_tile_group - create a tile group from a displayid description
3981 * @dev: DRM device
3982 * @topology: 8-bytes unique per monitor.
3983 *
3984 * Create a tile group for the unique monitor, and get a unique
3985 * identifier for the tile group.
3986 *
3987 * RETURNS:
3988 * new tile group or error.
3989 */
3990struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
3991 char topology[8])
3992{
3993 struct drm_tile_group *tg;
3994 int ret;
3995
3996 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
3997 if (!tg)
3998 return ERR_PTR(-ENOMEM);
3999
4000 kref_init(&tg->refcount);
4001 memcpy(tg->group_data, topology, 8);
4002 tg->dev = dev;
4003
4004 mutex_lock(&dev->mode_config.idr_mutex);
4005 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
4006 if (ret >= 0) {
4007 tg->id = ret;
4008 } else {
4009 kfree(tg);
4010 tg = ERR_PTR(ret);
4011 }
4012
4013 mutex_unlock(&dev->mode_config.idr_mutex);
4014 return tg;
4015}
Rob Clark81ddd1b2015-03-27 13:01:52 -04004016EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03004017
4018/**
4019 * drm_crtc_enable_color_mgmt - enable color management properties
4020 * @crtc: DRM CRTC
4021 * @degamma_lut_size: the size of the degamma lut (before CSC)
4022 * @has_ctm: whether to attach ctm_property for CSC matrix
4023 * @gamma_lut_size: the size of the gamma lut (after CSC)
4024 *
4025 * This function lets the driver enable the color correction
4026 * properties on a CRTC. This includes 3 degamma, csc and gamma
4027 * properties that userspace can set and 2 size properties to inform
4028 * the userspace of the lut sizes. Each of the properties are
4029 * optional. The gamma and degamma properties are only attached if
4030 * their size is not 0 and ctm_property is only attached if has_ctm is
4031 * true.
4032 */
4033void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
4034 uint degamma_lut_size,
4035 bool has_ctm,
4036 uint gamma_lut_size)
4037{
4038 struct drm_device *dev = crtc->dev;
4039 struct drm_mode_config *config = &dev->mode_config;
4040
4041 if (degamma_lut_size) {
4042 drm_object_attach_property(&crtc->base,
4043 config->degamma_lut_property, 0);
4044 drm_object_attach_property(&crtc->base,
4045 config->degamma_lut_size_property,
4046 degamma_lut_size);
4047 }
4048
4049 if (has_ctm)
4050 drm_object_attach_property(&crtc->base,
4051 config->ctm_property, 0);
4052
4053 if (gamma_lut_size) {
4054 drm_object_attach_property(&crtc->base,
4055 config->gamma_lut_property, 0);
4056 drm_object_attach_property(&crtc->base,
4057 config->gamma_lut_size_property,
4058 gamma_lut_size);
4059 }
4060}
4061EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);