blob: 60403bf7a4ffa9132c88f2a101fac3c02b40b377 [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>
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020043#include <drm/drm_debugfs_crc.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
Daniel Vetter52217192016-08-12 22:48:50 +020057/*
58 * Optional properties
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010059 */
Lukas Wunner6a0d9522016-06-08 18:47:27 +020060/**
61 * drm_crtc_force_disable - Forcibly turn off a CRTC
62 * @crtc: CRTC to turn off
63 *
64 * Returns:
65 * Zero on success, error code on failure.
66 */
67int drm_crtc_force_disable(struct drm_crtc *crtc)
68{
69 struct drm_mode_set set = {
70 .crtc = crtc,
71 };
72
73 return drm_mode_set_config_internal(&set);
74}
75EXPORT_SYMBOL(drm_crtc_force_disable);
76
77/**
78 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79 * @dev: DRM device whose CRTCs to turn off
80 *
81 * Drivers may want to call this on unload to ensure that all displays are
82 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
83 *
84 * Returns:
85 * Zero on success, error code on failure.
86 */
87int drm_crtc_force_disable_all(struct drm_device *dev)
88{
89 struct drm_crtc *crtc;
90 int ret = 0;
91
92 drm_modeset_lock_all(dev);
93 drm_for_each_crtc(crtc, dev)
94 if (crtc->enabled) {
95 ret = drm_crtc_force_disable(crtc);
96 if (ret)
97 goto out;
98 }
99out:
100 drm_modeset_unlock_all(dev);
101 return ret;
102}
103EXPORT_SYMBOL(drm_crtc_force_disable_all);
104
Rob Clark51fd3712013-11-19 12:10:12 -0500105DEFINE_WW_CLASS(crtc_ww_class);
106
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200107static unsigned int drm_num_crtcs(struct drm_device *dev)
108{
109 unsigned int num = 0;
110 struct drm_crtc *tmp;
111
112 drm_for_each_crtc(tmp, dev) {
113 num++;
114 }
115
116 return num;
117}
118
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200119static int drm_crtc_register_all(struct drm_device *dev)
120{
121 struct drm_crtc *crtc;
122 int ret = 0;
123
124 drm_for_each_crtc(crtc, dev) {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200125 if (drm_debugfs_crtc_add(crtc))
126 DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
127 crtc->name);
128
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200129 if (crtc->funcs->late_register)
130 ret = crtc->funcs->late_register(crtc);
131 if (ret)
132 return ret;
133 }
134
135 return 0;
136}
137
138static void drm_crtc_unregister_all(struct drm_device *dev)
139{
140 struct drm_crtc *crtc;
141
142 drm_for_each_crtc(crtc, dev) {
143 if (crtc->funcs->early_unregister)
144 crtc->funcs->early_unregister(crtc);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200145 drm_debugfs_crtc_remove(crtc);
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200146 }
147}
148
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200149static int drm_crtc_crc_init(struct drm_crtc *crtc)
150{
151#ifdef CONFIG_DEBUG_FS
152 spin_lock_init(&crtc->crc.lock);
153 init_waitqueue_head(&crtc->crc.wq);
154 crtc->crc.source = kstrdup("auto", GFP_KERNEL);
155 if (!crtc->crc.source)
156 return -ENOMEM;
157#endif
158 return 0;
159}
160
161static void drm_crtc_crc_fini(struct drm_crtc *crtc)
162{
163#ifdef CONFIG_DEBUG_FS
164 kfree(crtc->crc.source);
165#endif
166}
167
Dave Airlief453ba02008-11-07 14:05:41 -0800168/**
Matt Ropere13161a2014-04-01 15:22:38 -0700169 * drm_crtc_init_with_planes - Initialise a new CRTC object with
170 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800171 * @dev: DRM device
172 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700173 * @primary: Primary plane for CRTC
174 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800175 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200176 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800177 *
Daniel Vetter532b3672016-09-21 10:59:25 +0200178 * Inits a new object created as base part of a driver crtc object. Drivers
179 * should use this function instead of drm_crtc_init(), which is only provided
180 * for backwards compatibility with drivers which do not yet support universal
181 * planes). For really simple hardware which has only 1 plane look at
182 * drm_simple_display_pipe_init() instead.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200183 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100184 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200185 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800186 */
Matt Ropere13161a2014-04-01 15:22:38 -0700187int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
188 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700189 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200190 const struct drm_crtc_funcs *funcs,
191 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800192{
Rob Clark51fd3712013-11-19 12:10:12 -0500193 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200194 int ret;
195
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100196 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
197 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
198
Dave Airlief453ba02008-11-07 14:05:41 -0800199 crtc->dev = dev;
200 crtc->funcs = funcs;
201
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200202 INIT_LIST_HEAD(&crtc->commit_list);
203 spin_lock_init(&crtc->commit_lock);
204
Rob Clark51fd3712013-11-19 12:10:12 -0500205 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200206 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
207 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100208 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800209
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200210 if (name) {
211 va_list ap;
212
213 va_start(ap, name);
214 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
215 va_end(ap);
216 } else {
217 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
218 drm_num_crtcs(dev));
219 }
220 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000221 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200222 return -ENOMEM;
223 }
224
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300225 crtc->base.properties = &crtc->properties;
226
Rob Clark51fd3712013-11-19 12:10:12 -0500227 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100228 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200229
Matt Ropere13161a2014-04-01 15:22:38 -0700230 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700231 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700232 if (primary)
233 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700234 if (cursor)
235 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700236
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200237 ret = drm_crtc_crc_init(crtc);
238 if (ret) {
239 drm_mode_object_unregister(dev, &crtc->base);
240 return ret;
241 }
242
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100243 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
244 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100245 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100246 }
247
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100248 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800249}
Matt Ropere13161a2014-04-01 15:22:38 -0700250EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800251
252/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000253 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800254 * @crtc: CRTC to cleanup
255 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000256 * This function cleans up @crtc and removes it from the DRM mode setting
257 * core. Note that the function does *not* free the crtc structure itself,
258 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800259 */
260void drm_crtc_cleanup(struct drm_crtc *crtc)
261{
262 struct drm_device *dev = crtc->dev;
263
Chris Wilson490d3d12016-05-27 20:05:00 +0100264 /* Note that the crtc_list is considered to be static; should we
265 * remove the drm_crtc at runtime we would have to decrement all
266 * the indices on the drm_crtc after us in the crtc_list.
267 */
268
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200269 drm_crtc_crc_fini(crtc);
270
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000271 kfree(crtc->gamma_store);
272 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800273
Rob Clark51fd3712013-11-19 12:10:12 -0500274 drm_modeset_lock_fini(&crtc->mutex);
275
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000276 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800277 list_del(&crtc->head);
278 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100279
280 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
281 if (crtc->state && crtc->funcs->atomic_destroy_state)
282 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100283
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200284 kfree(crtc->name);
285
Thierry Redinga18c0af2014-12-10 11:38:49 +0100286 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800287}
288EXPORT_SYMBOL(drm_crtc_cleanup);
289
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200290int drm_modeset_register_all(struct drm_device *dev)
291{
292 int ret;
293
294 ret = drm_plane_register_all(dev);
295 if (ret)
296 goto err_plane;
297
298 ret = drm_crtc_register_all(dev);
299 if (ret)
300 goto err_crtc;
301
302 ret = drm_encoder_register_all(dev);
303 if (ret)
304 goto err_encoder;
305
306 ret = drm_connector_register_all(dev);
307 if (ret)
308 goto err_connector;
309
310 return 0;
311
312err_connector:
313 drm_encoder_unregister_all(dev);
314err_encoder:
315 drm_crtc_unregister_all(dev);
316err_crtc:
317 drm_plane_unregister_all(dev);
318err_plane:
319 return ret;
320}
321
322void drm_modeset_unregister_all(struct drm_device *dev)
323{
324 drm_connector_unregister_all(dev);
325 drm_encoder_unregister_all(dev);
326 drm_crtc_unregister_all(dev);
327 drm_plane_unregister_all(dev);
328}
329
Rob Clark6b4959f2014-12-18 16:01:53 -0500330static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800331{
Rob Clark356af0e2014-12-18 16:01:52 -0500332 struct drm_property *prop;
Daniel Vetter52217192016-08-12 22:48:50 +0200333 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800334
Daniel Vetter52217192016-08-12 22:48:50 +0200335 ret = drm_connector_create_standard_properties(dev);
336 if (ret)
337 return ret;
Dave Airlie6f134d72014-10-20 16:30:50 +1000338
Rob Clark6b4959f2014-12-18 16:01:53 -0500339 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -0400340 "type", drm_plane_type_enum_list,
341 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -0500342 if (!prop)
343 return -ENOMEM;
344 dev->mode_config.plane_type_property = prop;
345
346 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347 "SRC_X", 0, UINT_MAX);
348 if (!prop)
349 return -ENOMEM;
350 dev->mode_config.prop_src_x = prop;
351
352 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353 "SRC_Y", 0, UINT_MAX);
354 if (!prop)
355 return -ENOMEM;
356 dev->mode_config.prop_src_y = prop;
357
358 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
359 "SRC_W", 0, UINT_MAX);
360 if (!prop)
361 return -ENOMEM;
362 dev->mode_config.prop_src_w = prop;
363
364 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
365 "SRC_H", 0, UINT_MAX);
366 if (!prop)
367 return -ENOMEM;
368 dev->mode_config.prop_src_h = prop;
369
370 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
371 "CRTC_X", INT_MIN, INT_MAX);
372 if (!prop)
373 return -ENOMEM;
374 dev->mode_config.prop_crtc_x = prop;
375
376 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
377 "CRTC_Y", INT_MIN, INT_MAX);
378 if (!prop)
379 return -ENOMEM;
380 dev->mode_config.prop_crtc_y = prop;
381
382 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383 "CRTC_W", 0, INT_MAX);
384 if (!prop)
385 return -ENOMEM;
386 dev->mode_config.prop_crtc_w = prop;
387
388 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
389 "CRTC_H", 0, INT_MAX);
390 if (!prop)
391 return -ENOMEM;
392 dev->mode_config.prop_crtc_h = prop;
393
394 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
395 "FB_ID", DRM_MODE_OBJECT_FB);
396 if (!prop)
397 return -ENOMEM;
398 dev->mode_config.prop_fb_id = prop;
399
400 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
401 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
402 if (!prop)
403 return -ENOMEM;
404 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -0400405
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100406 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
407 "ACTIVE");
408 if (!prop)
409 return -ENOMEM;
410 dev->mode_config.prop_active = prop;
411
Daniel Stone955f3c32015-05-25 19:11:52 +0100412 prop = drm_property_create(dev,
413 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
414 "MODE_ID", 0);
415 if (!prop)
416 return -ENOMEM;
417 dev->mode_config.prop_mode_id = prop;
418
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000419 prop = drm_property_create(dev,
420 DRM_MODE_PROP_BLOB,
421 "DEGAMMA_LUT", 0);
422 if (!prop)
423 return -ENOMEM;
424 dev->mode_config.degamma_lut_property = prop;
425
426 prop = drm_property_create_range(dev,
427 DRM_MODE_PROP_IMMUTABLE,
428 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
429 if (!prop)
430 return -ENOMEM;
431 dev->mode_config.degamma_lut_size_property = prop;
432
433 prop = drm_property_create(dev,
434 DRM_MODE_PROP_BLOB,
435 "CTM", 0);
436 if (!prop)
437 return -ENOMEM;
438 dev->mode_config.ctm_property = prop;
439
440 prop = drm_property_create(dev,
441 DRM_MODE_PROP_BLOB,
442 "GAMMA_LUT", 0);
443 if (!prop)
444 return -ENOMEM;
445 dev->mode_config.gamma_lut_property = prop;
446
447 prop = drm_property_create_range(dev,
448 DRM_MODE_PROP_IMMUTABLE,
449 "GAMMA_LUT_SIZE", 0, UINT_MAX);
450 if (!prop)
451 return -ENOMEM;
452 dev->mode_config.gamma_lut_size_property = prop;
453
Rob Clark9922ab52014-04-01 20:16:57 -0400454 return 0;
455}
456
Dave Airlief453ba02008-11-07 14:05:41 -0800457/**
Dave Airlief453ba02008-11-07 14:05:41 -0800458 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100459 * @dev: drm device for the ioctl
460 * @data: data pointer for the ioctl
461 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800462 *
Dave Airlief453ba02008-11-07 14:05:41 -0800463 * Construct a set of configuration description structures and return
464 * them to the user, including CRTC, connector and framebuffer configuration.
465 *
466 * Called by the user via ioctl.
467 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100468 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100469 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800470 */
471int drm_mode_getresources(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
473{
474 struct drm_mode_card_res *card_res = data;
475 struct list_head *lh;
476 struct drm_framebuffer *fb;
477 struct drm_connector *connector;
478 struct drm_crtc *crtc;
479 struct drm_encoder *encoder;
480 int ret = 0;
481 int connector_count = 0;
482 int crtc_count = 0;
483 int fb_count = 0;
484 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200485 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800486 uint32_t __user *fb_id;
487 uint32_t __user *crtc_id;
488 uint32_t __user *connector_id;
489 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -0800490
Dave Airliefb3b06c2011-02-08 13:55:21 +1000491 if (!drm_core_check_feature(dev, DRIVER_MODESET))
492 return -EINVAL;
493
Dave Airlief453ba02008-11-07 14:05:41 -0800494
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100495 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800496 /*
497 * For the non-control nodes we need to limit the list of resources
498 * by IDs in the group list for this node
499 */
500 list_for_each(lh, &file_priv->fbs)
501 fb_count++;
502
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100503 /* handle this in 4 parts */
504 /* FBs */
505 if (card_res->count_fbs >= fb_count) {
506 copied = 0;
507 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
508 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
509 if (put_user(fb->base.id, fb_id + copied)) {
510 mutex_unlock(&file_priv->fbs_lock);
511 return -EFAULT;
512 }
513 copied++;
514 }
515 }
516 card_res->count_fbs = fb_count;
517 mutex_unlock(&file_priv->fbs_lock);
518
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100519 /* mode_config.mutex protects the connector list against e.g. DP MST
520 * connector hot-adding. CRTC/Plane lists are invariant. */
521 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200522 drm_for_each_crtc(crtc, dev)
523 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -0800524
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200525 drm_for_each_connector(connector, dev)
526 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -0800527
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200528 drm_for_each_encoder(encoder, dev)
529 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -0800530
531 card_res->max_height = dev->mode_config.max_height;
532 card_res->min_height = dev->mode_config.min_height;
533 card_res->max_width = dev->mode_config.max_width;
534 card_res->min_width = dev->mode_config.min_width;
535
Dave Airlief453ba02008-11-07 14:05:41 -0800536 /* CRTCs */
537 if (card_res->count_crtcs >= crtc_count) {
538 copied = 0;
539 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200540 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200541 if (put_user(crtc->base.id, crtc_id + copied)) {
542 ret = -EFAULT;
543 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800544 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200545 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -0800546 }
547 }
548 card_res->count_crtcs = crtc_count;
549
550 /* Encoders */
551 if (card_res->count_encoders >= encoder_count) {
552 copied = 0;
553 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200554 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200555 if (put_user(encoder->base.id, encoder_id +
556 copied)) {
557 ret = -EFAULT;
558 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800559 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200560 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -0800561 }
562 }
563 card_res->count_encoders = encoder_count;
564
565 /* Connectors */
566 if (card_res->count_connectors >= connector_count) {
567 copied = 0;
568 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200569 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200570 if (put_user(connector->base.id,
571 connector_id + copied)) {
572 ret = -EFAULT;
573 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800574 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +0200575 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -0800576 }
577 }
578 card_res->count_connectors = connector_count;
579
Dave Airlief453ba02008-11-07 14:05:41 -0800580out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100581 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800582 return ret;
583}
584
585/**
586 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100587 * @dev: drm device for the ioctl
588 * @data: data pointer for the ioctl
589 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800590 *
Dave Airlief453ba02008-11-07 14:05:41 -0800591 * Construct a CRTC configuration structure to return to the user.
592 *
593 * Called by the user via ioctl.
594 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100595 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100596 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800597 */
598int drm_mode_getcrtc(struct drm_device *dev,
599 void *data, struct drm_file *file_priv)
600{
601 struct drm_mode_crtc *crtc_resp = data;
602 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -0800603
Dave Airliefb3b06c2011-02-08 13:55:21 +1000604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
605 return -EINVAL;
606
Rob Clarka2b34e22013-10-05 16:36:52 -0400607 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100608 if (!crtc)
609 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800610
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100611 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -0800612 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -0700613 if (crtc->primary->fb)
614 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -0800615 else
616 crtc_resp->fb_id = 0;
617
Daniel Vetter31c946e2015-02-22 12:24:17 +0100618 if (crtc->state) {
619 crtc_resp->x = crtc->primary->state->src_x >> 16;
620 crtc_resp->y = crtc->primary->state->src_y >> 16;
621 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +0100622 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +0100623 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -0800624
Daniel Vetter31c946e2015-02-22 12:24:17 +0100625 } else {
626 crtc_resp->mode_valid = 0;
627 }
Dave Airlief453ba02008-11-07 14:05:41 -0800628 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +0100629 crtc_resp->x = crtc->x;
630 crtc_resp->y = crtc->y;
631 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +0100632 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +0100633 crtc_resp->mode_valid = 1;
634
635 } else {
636 crtc_resp->mode_valid = 0;
637 }
Dave Airlief453ba02008-11-07 14:05:41 -0800638 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100639 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800640
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100641 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800642}
643
Dave Airlief453ba02008-11-07 14:05:41 -0800644/**
Daniel Vetter2d13b672012-12-11 13:47:23 +0100645 * drm_mode_set_config_internal - helper to call ->set_config
646 * @set: modeset config to set
647 *
648 * This is a little helper to wrap internal calls to the ->set_config driver
649 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +0100650 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100651 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100652 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +0100653 */
654int drm_mode_set_config_internal(struct drm_mode_set *set)
655{
656 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200657 struct drm_framebuffer *fb;
658 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100659 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100660
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200661 /*
662 * NOTE: ->set_config can also disable other crtcs (if we steal all
663 * connectors from it), hence we need to refcount the fbs across all
664 * crtcs. Atomic modeset will have saner semantics ...
665 */
Daniel Vettere4f62542015-07-09 23:44:35 +0200666 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +0200667 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200668
Daniel Vetterb0d12322012-12-11 01:07:12 +0100669 fb = set->fb;
670
671 ret = crtc->funcs->set_config(set);
672 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -0700673 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +0200674 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200675 }
Daniel Vettercc85e122013-06-15 00:13:15 +0200676
Daniel Vettere4f62542015-07-09 23:44:35 +0200677 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700678 if (tmp->primary->fb)
679 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +0200680 if (tmp->primary->old_fb)
681 drm_framebuffer_unreference(tmp->primary->old_fb);
682 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100683 }
684
685 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100686}
687EXPORT_SYMBOL(drm_mode_set_config_internal);
688
Matt Roperaf936292014-04-01 15:22:34 -0700689/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800690 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
691 * @mode: mode to query
692 * @hdisplay: hdisplay value to fill in
693 * @vdisplay: vdisplay value to fill in
694 *
695 * The vdisplay value will be doubled if the specified mode is a stereo mode of
696 * the appropriate layout.
697 */
698void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
699 int *hdisplay, int *vdisplay)
700{
701 struct drm_display_mode adjusted;
702
703 drm_mode_copy(&adjusted, mode);
704 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
705 *hdisplay = adjusted.crtc_hdisplay;
706 *vdisplay = adjusted.crtc_vdisplay;
707}
708EXPORT_SYMBOL(drm_crtc_get_hv_timing);
709
710/**
Matt Roperaf936292014-04-01 15:22:34 -0700711 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
712 * CRTC viewport
713 * @crtc: CRTC that framebuffer will be displayed on
714 * @x: x panning
715 * @y: y panning
716 * @mode: mode that framebuffer will be displayed under
717 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +0100718 */
Matt Roperaf936292014-04-01 15:22:34 -0700719int drm_crtc_check_viewport(const struct drm_crtc *crtc,
720 int x, int y,
721 const struct drm_display_mode *mode,
722 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +0100723
724{
725 int hdisplay, vdisplay;
726
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800727 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +0100728
Ville Syrjälä33e0be62015-10-16 18:38:39 +0300729 if (crtc->state &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300730 crtc->primary->state->rotation & (DRM_ROTATE_90 |
731 DRM_ROTATE_270))
Damien Lespiauc11e9282013-09-25 16:45:30 +0100732 swap(hdisplay, vdisplay);
733
Daniel Vetter43968d72016-09-21 10:59:24 +0200734 return drm_framebuffer_check_src_coords(x << 16, y << 16,
735 hdisplay << 16, vdisplay << 16,
736 fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +0100737}
Matt Roperaf936292014-04-01 15:22:34 -0700738EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +0100739
Daniel Vetter2d13b672012-12-11 13:47:23 +0100740/**
Dave Airlief453ba02008-11-07 14:05:41 -0800741 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100742 * @dev: drm device for the ioctl
743 * @data: data pointer for the ioctl
744 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800745 *
Dave Airlief453ba02008-11-07 14:05:41 -0800746 * Build a new CRTC configuration based on user request.
747 *
748 * Called by the user via ioctl.
749 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100750 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100751 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800752 */
753int drm_mode_setcrtc(struct drm_device *dev, void *data,
754 struct drm_file *file_priv)
755{
756 struct drm_mode_config *config = &dev->mode_config;
757 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +0200758 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -0800759 struct drm_connector **connector_set = NULL, *connector;
760 struct drm_framebuffer *fb = NULL;
761 struct drm_display_mode *mode = NULL;
762 struct drm_mode_set set;
763 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200764 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800765 int i;
766
Dave Airliefb3b06c2011-02-08 13:55:21 +1000767 if (!drm_core_check_feature(dev, DRIVER_MODESET))
768 return -EINVAL;
769
Zhao Junwang01447e92015-07-07 17:08:35 +0800770 /*
771 * Universal plane src offsets are only 16.16, prevent havoc for
772 * drivers using universal plane code internally.
773 */
774 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +0200775 return -ERANGE;
776
Daniel Vetter84849902012-12-02 00:28:11 +0100777 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -0400778 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
779 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800780 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +0300781 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800782 goto out;
783 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200784 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800785
786 if (crtc_req->mode_valid) {
787 /* If we have a mode we need a framebuffer. */
788 /* If we pass -1, set the mode with the currently bound fb */
789 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -0700790 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +0200791 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
792 ret = -EINVAL;
793 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800794 }
Matt Roperf4510a22014-04-01 15:22:40 -0700795 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100796 /* Make refcounting symmetric with the lookup path. */
797 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800798 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +0100799 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
800 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800801 DRM_DEBUG_KMS("Unknown FB ID%d\n",
802 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +0300803 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800804 goto out;
805 }
Dave Airlief453ba02008-11-07 14:05:41 -0800806 }
807
808 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200809 if (!mode) {
810 ret = -ENOMEM;
811 goto out;
812 }
813
Daniel Stone934a8a82015-05-22 13:34:48 +0100814 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +0200815 if (ret) {
816 DRM_DEBUG_KMS("Invalid mode\n");
817 goto out;
818 }
819
Laurent Pinchart7eb5f302015-03-09 10:41:07 +0200820 /*
821 * Check whether the primary plane supports the fb pixel format.
822 * Drivers not implementing the universal planes API use a
823 * default formats list provided by the DRM core which doesn't
824 * match real hardware capabilities. Skip the check in that
825 * case.
826 */
827 if (!crtc->primary->format_default) {
828 ret = drm_plane_check_pixel_format(crtc->primary,
829 fb->pixel_format);
830 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +0100831 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +0100832 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
833 kfree(format_name);
Laurent Pinchart7eb5f302015-03-09 10:41:07 +0200834 goto out;
835 }
836 }
837
Damien Lespiauc11e9282013-09-25 16:45:30 +0100838 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
839 mode, fb);
840 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +0200841 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +0100842
Dave Airlief453ba02008-11-07 14:05:41 -0800843 }
844
845 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800846 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800847 ret = -EINVAL;
848 goto out;
849 }
850
Jakob Bornecrantz7781de72009-08-03 13:43:58 +0100851 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800852 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800853 crtc_req->count_connectors);
854 ret = -EINVAL;
855 goto out;
856 }
857
858 if (crtc_req->count_connectors > 0) {
859 u32 out_id;
860
861 /* Avoid unbounded kernel memory allocation */
862 if (crtc_req->count_connectors > config->num_connector) {
863 ret = -EINVAL;
864 goto out;
865 }
866
Thierry Reding2f6c5382014-12-10 13:03:36 +0100867 connector_set = kmalloc_array(crtc_req->count_connectors,
868 sizeof(struct drm_connector *),
869 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800870 if (!connector_set) {
871 ret = -ENOMEM;
872 goto out;
873 }
874
875 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +1000876 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +0200877 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -0800878 if (get_user(out_id, &set_connectors_ptr[i])) {
879 ret = -EFAULT;
880 goto out;
881 }
882
Dave Airlieb164d312016-04-27 11:10:09 +1000883 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -0400884 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800885 DRM_DEBUG_KMS("Connector id %d unknown\n",
886 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +0300887 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800888 goto out;
889 }
Jerome Glisse94401062010-07-15 15:43:25 -0400890 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
891 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300892 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800893
894 connector_set[i] = connector;
895 }
896 }
897
898 set.crtc = crtc;
899 set.x = crtc_req->x;
900 set.y = crtc_req->y;
901 set.mode = mode;
902 set.connectors = connector_set;
903 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000904 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100905 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -0800906
907out:
Daniel Vetterb0d12322012-12-11 01:07:12 +0100908 if (fb)
909 drm_framebuffer_unreference(fb);
910
Dave Airlieb164d312016-04-27 11:10:09 +1000911 if (connector_set) {
912 for (i = 0; i < crtc_req->count_connectors; i++) {
913 if (connector_set[i])
914 drm_connector_unreference(connector_set[i]);
915 }
916 }
Dave Airlief453ba02008-11-07 14:05:41 -0800917 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200918 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +0100919 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800920 return ret;
921}
922
Daniel Vetter949619f2016-08-29 10:27:51 +0200923int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
924 struct drm_property *property,
925 uint64_t value)
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300926{
927 int ret = -EINVAL;
928 struct drm_crtc *crtc = obj_to_crtc(obj);
929
930 if (crtc->funcs->set_property)
931 ret = crtc->funcs->set_property(crtc, property, value);
932 if (!ret)
933 drm_object_property_set_value(obj, property, value);
934
935 return ret;
936}
937
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100938/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100939 * drm_mode_config_reset - call ->reset callbacks
940 * @dev: drm device
941 *
942 * This functions calls all the crtc's, encoder's and connector's ->reset
943 * callback. Drivers can use this in e.g. their driver load or resume code to
944 * reset hardware and software state.
945 */
Chris Wilsoneb033552011-01-24 15:11:08 +0000946void drm_mode_config_reset(struct drm_device *dev)
947{
948 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +0200949 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +0000950 struct drm_encoder *encoder;
951 struct drm_connector *connector;
952
Daniel Vettere4f62542015-07-09 23:44:35 +0200953 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +0200954 if (plane->funcs->reset)
955 plane->funcs->reset(plane);
956
Daniel Vettere4f62542015-07-09 23:44:35 +0200957 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +0000958 if (crtc->funcs->reset)
959 crtc->funcs->reset(crtc);
960
Daniel Vettere4f62542015-07-09 23:44:35 +0200961 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +0000962 if (encoder->funcs->reset)
963 encoder->funcs->reset(encoder);
964
Daniel Vetterf8c2ba32015-07-29 08:32:43 +0200965 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +1000966 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +0000967 if (connector->funcs->reset)
968 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +0200969 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +0000970}
971EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +1000972
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100973/**
974 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
975 * @dev: DRM device
976 * @data: ioctl data
977 * @file_priv: DRM file info
978 *
979 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
980 * TTM or something else entirely) and returns the resulting buffer handle. This
981 * handle can then be wrapped up into a framebuffer modeset object.
982 *
983 * Note that userspace is not allowed to use such objects for render
984 * acceleration - drivers must create their own private ioctls for such a use
985 * case.
986 *
987 * Called by the user via ioctl.
988 *
989 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100990 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100991 */
Dave Airlieff72145b2011-02-07 12:16:14 +1000992int drm_mode_create_dumb_ioctl(struct drm_device *dev,
993 void *data, struct drm_file *file_priv)
994{
995 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +0100996 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +1000997
998 if (!dev->driver->dumb_create)
999 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01001000 if (!args->width || !args->height || !args->bpp)
1001 return -EINVAL;
1002
1003 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02001004 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01001005 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02001006 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01001007 return -EINVAL;
1008 stride = cpp * args->width;
1009 if (args->height > 0xffffffffU / stride)
1010 return -EINVAL;
1011
1012 /* test for wrap-around */
1013 size = args->height * stride;
1014 if (PAGE_ALIGN(size) == 0)
1015 return -EINVAL;
1016
Thierry Redingf6085952014-11-03 11:14:14 +01001017 /*
1018 * handle, pitch and size are output parameters. Zero them out to
1019 * prevent drivers from accidentally using uninitialized data. Since
1020 * not all existing userspace is clearing these fields properly we
1021 * cannot reject IOCTL with garbage in them.
1022 */
1023 args->handle = 0;
1024 args->pitch = 0;
1025 args->size = 0;
1026
Dave Airlieff72145b2011-02-07 12:16:14 +10001027 return dev->driver->dumb_create(file_priv, dev, args);
1028}
1029
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001030/**
1031 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1032 * @dev: DRM device
1033 * @data: ioctl data
1034 * @file_priv: DRM file info
1035 *
1036 * Allocate an offset in the drm device node's address space to be able to
1037 * memory map a dumb buffer.
1038 *
1039 * Called by the user via ioctl.
1040 *
1041 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001042 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001043 */
Dave Airlieff72145b2011-02-07 12:16:14 +10001044int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1045 void *data, struct drm_file *file_priv)
1046{
1047 struct drm_mode_map_dumb *args = data;
1048
1049 /* call driver ioctl to get mmap offset */
1050 if (!dev->driver->dumb_map_offset)
1051 return -ENOSYS;
1052
1053 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
1054}
1055
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001056/**
1057 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1058 * @dev: DRM device
1059 * @data: ioctl data
1060 * @file_priv: DRM file info
1061 *
1062 * This destroys the userspace handle for the given dumb backing storage buffer.
1063 * Since buffer objects must be reference counted in the kernel a buffer object
1064 * won't be immediately freed if a framebuffer modeset object still uses it.
1065 *
1066 * Called by the user via ioctl.
1067 *
1068 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001069 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001070 */
Dave Airlieff72145b2011-02-07 12:16:14 +10001071int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1072 void *data, struct drm_file *file_priv)
1073{
1074 struct drm_mode_destroy_dumb *args = data;
1075
1076 if (!dev->driver->dumb_destroy)
1077 return -ENOSYS;
1078
1079 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1080}
Dave Airlie248dbc22011-11-29 20:02:54 +00001081
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001082/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001083 * drm_mode_config_init - initialize DRM mode_configuration structure
1084 * @dev: DRM device
1085 *
1086 * Initialize @dev's mode_config structure, used for tracking the graphics
1087 * configuration of @dev.
1088 *
1089 * Since this initializes the modeset locks, no locking is possible. Which is no
1090 * problem, since this should happen single threaded at init time. It is the
1091 * driver's problem to ensure this guarantee.
1092 *
1093 */
1094void drm_mode_config_init(struct drm_device *dev)
1095{
1096 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05001097 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001098 mutex_init(&dev->mode_config.idr_mutex);
1099 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01001100 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001101 INIT_LIST_HEAD(&dev->mode_config.fb_list);
1102 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1103 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1104 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1105 INIT_LIST_HEAD(&dev->mode_config.property_list);
1106 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1107 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1108 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10001109 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001110 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001111
1112 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05001113 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001114 drm_modeset_unlock_all(dev);
1115
1116 /* Just to be sure */
1117 dev->mode_config.num_fb = 0;
1118 dev->mode_config.num_connector = 0;
1119 dev->mode_config.num_crtc = 0;
1120 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07001121 dev->mode_config.num_overlay_plane = 0;
1122 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001123}
1124EXPORT_SYMBOL(drm_mode_config_init);
1125
1126/**
1127 * drm_mode_config_cleanup - free up DRM mode_config info
1128 * @dev: DRM device
1129 *
1130 * Free up all the connectors and CRTCs associated with this DRM device, then
1131 * free up the framebuffers and associated buffer objects.
1132 *
1133 * Note that since this /should/ happen single-threaded at driver/device
1134 * teardown time, no locking is required. It's the driver's job to ensure that
1135 * this guarantee actually holds true.
1136 *
1137 * FIXME: cleanup any dangling user buffer objects too
1138 */
1139void drm_mode_config_cleanup(struct drm_device *dev)
1140{
1141 struct drm_connector *connector, *ot;
1142 struct drm_crtc *crtc, *ct;
1143 struct drm_encoder *encoder, *enct;
1144 struct drm_framebuffer *fb, *fbt;
1145 struct drm_property *property, *pt;
1146 struct drm_property_blob *blob, *bt;
1147 struct drm_plane *plane, *plt;
1148
1149 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1150 head) {
1151 encoder->funcs->destroy(encoder);
1152 }
1153
1154 list_for_each_entry_safe(connector, ot,
1155 &dev->mode_config.connector_list, head) {
1156 connector->funcs->destroy(connector);
1157 }
1158
1159 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1160 head) {
1161 drm_property_destroy(dev, property);
1162 }
1163
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01001164 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1165 head) {
1166 plane->funcs->destroy(plane);
1167 }
1168
1169 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1170 crtc->funcs->destroy(crtc);
1171 }
1172
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001173 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01001174 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01001175 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001176 }
1177
1178 /*
1179 * Single-threaded teardown context, so it's not required to grab the
1180 * fb_lock to protect against concurrent fb_list access. Contrary, it
1181 * would actually deadlock with the drm_framebuffer_cleanup function.
1182 *
1183 * Also, if there are any framebuffers left, that's a driver leak now,
1184 * so politely WARN about this.
1185 */
1186 WARN_ON(!list_empty(&dev->mode_config.fb_list));
1187 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10001188 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001189 }
1190
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001191 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10001192 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001193 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05001194 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02001195}
1196EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05301197
Dave Airlie138f9eb2014-10-20 16:17:17 +10001198/**
1199 * DOC: Tile group
1200 *
1201 * Tile groups are used to represent tiled monitors with a unique
1202 * integer identifier. Tiled monitors using DisplayID v1.3 have
1203 * a unique 8-byte handle, we store this in a tile group, so we
1204 * have a common identifier for all tiles in a monitor group.
1205 */
1206static void drm_tile_group_free(struct kref *kref)
1207{
1208 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1209 struct drm_device *dev = tg->dev;
1210 mutex_lock(&dev->mode_config.idr_mutex);
1211 idr_remove(&dev->mode_config.tile_idr, tg->id);
1212 mutex_unlock(&dev->mode_config.idr_mutex);
1213 kfree(tg);
1214}
1215
1216/**
1217 * drm_mode_put_tile_group - drop a reference to a tile group.
1218 * @dev: DRM device
1219 * @tg: tile group to drop reference to.
1220 *
1221 * drop reference to tile group and free if 0.
1222 */
1223void drm_mode_put_tile_group(struct drm_device *dev,
1224 struct drm_tile_group *tg)
1225{
1226 kref_put(&tg->refcount, drm_tile_group_free);
1227}
1228
1229/**
1230 * drm_mode_get_tile_group - get a reference to an existing tile group
1231 * @dev: DRM device
1232 * @topology: 8-bytes unique per monitor.
1233 *
1234 * Use the unique bytes to get a reference to an existing tile group.
1235 *
1236 * RETURNS:
1237 * tile group or NULL if not found.
1238 */
1239struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1240 char topology[8])
1241{
1242 struct drm_tile_group *tg;
1243 int id;
1244 mutex_lock(&dev->mode_config.idr_mutex);
1245 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1246 if (!memcmp(tg->group_data, topology, 8)) {
1247 if (!kref_get_unless_zero(&tg->refcount))
1248 tg = NULL;
1249 mutex_unlock(&dev->mode_config.idr_mutex);
1250 return tg;
1251 }
1252 }
1253 mutex_unlock(&dev->mode_config.idr_mutex);
1254 return NULL;
1255}
Rob Clark81ddd1b2015-03-27 13:01:52 -04001256EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10001257
1258/**
1259 * drm_mode_create_tile_group - create a tile group from a displayid description
1260 * @dev: DRM device
1261 * @topology: 8-bytes unique per monitor.
1262 *
1263 * Create a tile group for the unique monitor, and get a unique
1264 * identifier for the tile group.
1265 *
1266 * RETURNS:
1267 * new tile group or error.
1268 */
1269struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1270 char topology[8])
1271{
1272 struct drm_tile_group *tg;
1273 int ret;
1274
1275 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1276 if (!tg)
1277 return ERR_PTR(-ENOMEM);
1278
1279 kref_init(&tg->refcount);
1280 memcpy(tg->group_data, topology, 8);
1281 tg->dev = dev;
1282
1283 mutex_lock(&dev->mode_config.idr_mutex);
1284 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1285 if (ret >= 0) {
1286 tg->id = ret;
1287 } else {
1288 kfree(tg);
1289 tg = ERR_PTR(ret);
1290 }
1291
1292 mutex_unlock(&dev->mode_config.idr_mutex);
1293 return tg;
1294}
Rob Clark81ddd1b2015-03-27 13:01:52 -04001295EXPORT_SYMBOL(drm_mode_create_tile_group);