blob: 75e0beeb37938a8b7e8b27be180420950a4f65fc [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>
Gustavo Padovan6d6003c2016-11-15 23:37:08 +090036#include <linux/dma-fence.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_edid.h>
40#include <drm/drm_fourcc.h>
Rob Clark51fd3712013-11-19 12:10:12 -050041#include <drm/drm_modeset_lock.h>
Rob Clark88a48e22014-12-18 16:01:50 -050042#include <drm/drm_atomic.h>
Daniel Vetter3b96a0b2016-06-21 10:54:22 +020043#include <drm/drm_auth.h>
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +020044#include <drm/drm_debugfs_crc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080045
Daniel Vetter8bd441b2014-01-23 15:35:24 +010046#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020047#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010048
Lukas Wunner6a0d9522016-06-08 18:47:27 +020049/**
Shawn Guo6d1b81d2016-12-29 20:41:28 +080050 * drm_crtc_from_index - find the registered CRTC at an index
51 * @dev: DRM device
52 * @idx: index of registered CRTC to find for
53 *
54 * Given a CRTC index, return the registered CRTC from DRM device's
55 * list of CRTCs with matching index.
56 */
57struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
58{
59 struct drm_crtc *crtc;
60
61 drm_for_each_crtc(crtc, dev)
62 if (idx == crtc->index)
63 return crtc;
64
65 return NULL;
66}
67EXPORT_SYMBOL(drm_crtc_from_index);
68
69/**
Lukas Wunner6a0d9522016-06-08 18:47:27 +020070 * drm_crtc_force_disable - Forcibly turn off a CRTC
71 * @crtc: CRTC to turn off
72 *
73 * Returns:
74 * Zero on success, error code on failure.
75 */
76int drm_crtc_force_disable(struct drm_crtc *crtc)
77{
78 struct drm_mode_set set = {
79 .crtc = crtc,
80 };
81
82 return drm_mode_set_config_internal(&set);
83}
84EXPORT_SYMBOL(drm_crtc_force_disable);
85
86/**
87 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
88 * @dev: DRM device whose CRTCs to turn off
89 *
90 * Drivers may want to call this on unload to ensure that all displays are
91 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
92 *
93 * Returns:
94 * Zero on success, error code on failure.
95 */
96int drm_crtc_force_disable_all(struct drm_device *dev)
97{
98 struct drm_crtc *crtc;
99 int ret = 0;
100
101 drm_modeset_lock_all(dev);
102 drm_for_each_crtc(crtc, dev)
103 if (crtc->enabled) {
104 ret = drm_crtc_force_disable(crtc);
105 if (ret)
106 goto out;
107 }
108out:
109 drm_modeset_unlock_all(dev);
110 return ret;
111}
112EXPORT_SYMBOL(drm_crtc_force_disable_all);
113
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200114static unsigned int drm_num_crtcs(struct drm_device *dev)
115{
116 unsigned int num = 0;
117 struct drm_crtc *tmp;
118
119 drm_for_each_crtc(tmp, dev) {
120 num++;
121 }
122
123 return num;
124}
125
Daniel Vetter28575f12016-11-14 12:58:23 +0100126int drm_crtc_register_all(struct drm_device *dev)
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200127{
128 struct drm_crtc *crtc;
129 int ret = 0;
130
131 drm_for_each_crtc(crtc, dev) {
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200132 if (drm_debugfs_crtc_add(crtc))
133 DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
134 crtc->name);
135
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200136 if (crtc->funcs->late_register)
137 ret = crtc->funcs->late_register(crtc);
138 if (ret)
139 return ret;
140 }
141
142 return 0;
143}
144
Daniel Vetter28575f12016-11-14 12:58:23 +0100145void drm_crtc_unregister_all(struct drm_device *dev)
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200146{
147 struct drm_crtc *crtc;
148
149 drm_for_each_crtc(crtc, dev) {
150 if (crtc->funcs->early_unregister)
151 crtc->funcs->early_unregister(crtc);
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200152 drm_debugfs_crtc_remove(crtc);
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200153 }
154}
155
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200156static int drm_crtc_crc_init(struct drm_crtc *crtc)
157{
158#ifdef CONFIG_DEBUG_FS
159 spin_lock_init(&crtc->crc.lock);
160 init_waitqueue_head(&crtc->crc.wq);
161 crtc->crc.source = kstrdup("auto", GFP_KERNEL);
162 if (!crtc->crc.source)
163 return -ENOMEM;
164#endif
165 return 0;
166}
167
168static void drm_crtc_crc_fini(struct drm_crtc *crtc)
169{
170#ifdef CONFIG_DEBUG_FS
171 kfree(crtc->crc.source);
172#endif
173}
174
Gustavo Padovan35f8cc32016-12-06 15:47:17 -0200175static const struct dma_fence_ops drm_crtc_fence_ops;
176
Gustavo Padovan6d6003c2016-11-15 23:37:08 +0900177static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
178{
179 BUG_ON(fence->ops != &drm_crtc_fence_ops);
180 return container_of(fence->lock, struct drm_crtc, fence_lock);
181}
182
183static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
184{
185 struct drm_crtc *crtc = fence_to_crtc(fence);
186
187 return crtc->dev->driver->name;
188}
189
190static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
191{
192 struct drm_crtc *crtc = fence_to_crtc(fence);
193
194 return crtc->timeline_name;
195}
196
197static bool drm_crtc_fence_enable_signaling(struct dma_fence *fence)
198{
199 return true;
200}
201
Gustavo Padovan35f8cc32016-12-06 15:47:17 -0200202static const struct dma_fence_ops drm_crtc_fence_ops = {
Gustavo Padovan6d6003c2016-11-15 23:37:08 +0900203 .get_driver_name = drm_crtc_fence_get_driver_name,
204 .get_timeline_name = drm_crtc_fence_get_timeline_name,
205 .enable_signaling = drm_crtc_fence_enable_signaling,
206 .wait = dma_fence_default_wait,
207};
208
Gustavo Padovan35f8cc32016-12-06 15:47:17 -0200209struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
210{
211 struct dma_fence *fence;
212
213 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
214 if (!fence)
215 return NULL;
216
217 dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
218 crtc->fence_context, ++crtc->fence_seqno);
219
220 return fence;
221}
222
Dave Airlief453ba02008-11-07 14:05:41 -0800223/**
Matt Ropere13161a2014-04-01 15:22:38 -0700224 * drm_crtc_init_with_planes - Initialise a new CRTC object with
225 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800226 * @dev: DRM device
227 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700228 * @primary: Primary plane for CRTC
229 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800230 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200231 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800232 *
Daniel Vetter532b3672016-09-21 10:59:25 +0200233 * Inits a new object created as base part of a driver crtc object. Drivers
234 * should use this function instead of drm_crtc_init(), which is only provided
235 * for backwards compatibility with drivers which do not yet support universal
236 * planes). For really simple hardware which has only 1 plane look at
237 * drm_simple_display_pipe_init() instead.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200238 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100239 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200240 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800241 */
Matt Ropere13161a2014-04-01 15:22:38 -0700242int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
243 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700244 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200245 const struct drm_crtc_funcs *funcs,
246 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800247{
Rob Clark51fd3712013-11-19 12:10:12 -0500248 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200249 int ret;
250
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100251 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
252 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
253
Dave Airlief453ba02008-11-07 14:05:41 -0800254 crtc->dev = dev;
255 crtc->funcs = funcs;
256
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200257 INIT_LIST_HEAD(&crtc->commit_list);
258 spin_lock_init(&crtc->commit_lock);
259
Rob Clark51fd3712013-11-19 12:10:12 -0500260 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200261 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
262 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100263 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800264
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200265 if (name) {
266 va_list ap;
267
268 va_start(ap, name);
269 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
270 va_end(ap);
271 } else {
272 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
273 drm_num_crtcs(dev));
274 }
275 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000276 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200277 return -ENOMEM;
278 }
279
Gustavo Padovan6d6003c2016-11-15 23:37:08 +0900280 crtc->fence_context = dma_fence_context_alloc(1);
281 spin_lock_init(&crtc->fence_lock);
282 snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
283 "CRTC:%d-%s", crtc->base.id, crtc->name);
284
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300285 crtc->base.properties = &crtc->properties;
286
Rob Clark51fd3712013-11-19 12:10:12 -0500287 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100288 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200289
Matt Ropere13161a2014-04-01 15:22:38 -0700290 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700291 crtc->cursor = cursor;
Rob Clark7abc7d42016-11-05 10:52:01 -0400292 if (primary && !primary->possible_crtcs)
Matt Ropere13161a2014-04-01 15:22:38 -0700293 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Rob Clark7abc7d42016-11-05 10:52:01 -0400294 if (cursor && !cursor->possible_crtcs)
Matt Roperfc1d3e42014-06-10 08:28:11 -0700295 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700296
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200297 ret = drm_crtc_crc_init(crtc);
298 if (ret) {
299 drm_mode_object_unregister(dev, &crtc->base);
300 return ret;
301 }
302
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100303 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
304 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100305 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900306 drm_object_attach_property(&crtc->base,
307 config->prop_out_fence_ptr, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100308 }
309
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100310 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800311}
Matt Ropere13161a2014-04-01 15:22:38 -0700312EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800313
314/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000315 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800316 * @crtc: CRTC to cleanup
317 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000318 * This function cleans up @crtc and removes it from the DRM mode setting
319 * core. Note that the function does *not* free the crtc structure itself,
320 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800321 */
322void drm_crtc_cleanup(struct drm_crtc *crtc)
323{
324 struct drm_device *dev = crtc->dev;
325
Chris Wilson490d3d12016-05-27 20:05:00 +0100326 /* Note that the crtc_list is considered to be static; should we
327 * remove the drm_crtc at runtime we would have to decrement all
328 * the indices on the drm_crtc after us in the crtc_list.
329 */
330
Tomeu Vizoso9edbf1f2016-10-06 17:21:06 +0200331 drm_crtc_crc_fini(crtc);
332
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000333 kfree(crtc->gamma_store);
334 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800335
Rob Clark51fd3712013-11-19 12:10:12 -0500336 drm_modeset_lock_fini(&crtc->mutex);
337
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000338 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800339 list_del(&crtc->head);
340 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100341
342 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
343 if (crtc->state && crtc->funcs->atomic_destroy_state)
344 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100345
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200346 kfree(crtc->name);
347
Thierry Redinga18c0af2014-12-10 11:38:49 +0100348 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800349}
350EXPORT_SYMBOL(drm_crtc_cleanup);
351
Dave Airlief453ba02008-11-07 14:05:41 -0800352/**
353 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100354 * @dev: drm device for the ioctl
355 * @data: data pointer for the ioctl
356 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800357 *
Dave Airlief453ba02008-11-07 14:05:41 -0800358 * Construct a CRTC configuration structure to return to the user.
359 *
360 * Called by the user via ioctl.
361 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100362 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100363 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800364 */
365int drm_mode_getcrtc(struct drm_device *dev,
366 void *data, struct drm_file *file_priv)
367{
368 struct drm_mode_crtc *crtc_resp = data;
369 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -0800370
Dave Airliefb3b06c2011-02-08 13:55:21 +1000371 if (!drm_core_check_feature(dev, DRIVER_MODESET))
372 return -EINVAL;
373
Rob Clarka2b34e22013-10-05 16:36:52 -0400374 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100375 if (!crtc)
376 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800377
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100378 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -0800379 crtc_resp->gamma_size = crtc->gamma_size;
Daniel Stonede7b6be2016-12-13 18:19:12 +0000380
381 if (crtc->primary->state && crtc->primary->state->fb)
382 crtc_resp->fb_id = crtc->primary->state->fb->base.id;
383 else if (!crtc->primary->state && crtc->primary->fb)
Matt Roperf4510a22014-04-01 15:22:40 -0700384 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -0800385 else
386 crtc_resp->fb_id = 0;
387
Daniel Vetter31c946e2015-02-22 12:24:17 +0100388 if (crtc->state) {
389 crtc_resp->x = crtc->primary->state->src_x >> 16;
390 crtc_resp->y = crtc->primary->state->src_y >> 16;
391 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +0100392 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +0100393 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -0800394
Daniel Vetter31c946e2015-02-22 12:24:17 +0100395 } else {
396 crtc_resp->mode_valid = 0;
397 }
Dave Airlief453ba02008-11-07 14:05:41 -0800398 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +0100399 crtc_resp->x = crtc->x;
400 crtc_resp->y = crtc->y;
401 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +0100402 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +0100403 crtc_resp->mode_valid = 1;
404
405 } else {
406 crtc_resp->mode_valid = 0;
407 }
Dave Airlief453ba02008-11-07 14:05:41 -0800408 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +0100409 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800410
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100411 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800412}
413
Dave Airlief453ba02008-11-07 14:05:41 -0800414/**
Daniel Vetter2d13b672012-12-11 13:47:23 +0100415 * drm_mode_set_config_internal - helper to call ->set_config
416 * @set: modeset config to set
417 *
418 * This is a little helper to wrap internal calls to the ->set_config driver
419 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +0100420 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100421 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100422 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +0100423 */
424int drm_mode_set_config_internal(struct drm_mode_set *set)
425{
426 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200427 struct drm_framebuffer *fb;
428 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100429 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100430
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200431 /*
432 * NOTE: ->set_config can also disable other crtcs (if we steal all
433 * connectors from it), hence we need to refcount the fbs across all
434 * crtcs. Atomic modeset will have saner semantics ...
435 */
Daniel Vettere4f62542015-07-09 23:44:35 +0200436 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +0200437 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200438
Daniel Vetterb0d12322012-12-11 01:07:12 +0100439 fb = set->fb;
440
441 ret = crtc->funcs->set_config(set);
442 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -0700443 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +0200444 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +0200445 }
Daniel Vettercc85e122013-06-15 00:13:15 +0200446
Daniel Vettere4f62542015-07-09 23:44:35 +0200447 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700448 if (tmp->primary->fb)
449 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +0200450 if (tmp->primary->old_fb)
451 drm_framebuffer_unreference(tmp->primary->old_fb);
452 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100453 }
454
455 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100456}
457EXPORT_SYMBOL(drm_mode_set_config_internal);
458
Matt Roperaf936292014-04-01 15:22:34 -0700459/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800460 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
461 * @mode: mode to query
462 * @hdisplay: hdisplay value to fill in
463 * @vdisplay: vdisplay value to fill in
464 *
465 * The vdisplay value will be doubled if the specified mode is a stereo mode of
466 * the appropriate layout.
467 */
468void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
469 int *hdisplay, int *vdisplay)
470{
471 struct drm_display_mode adjusted;
472
473 drm_mode_copy(&adjusted, mode);
474 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
475 *hdisplay = adjusted.crtc_hdisplay;
476 *vdisplay = adjusted.crtc_vdisplay;
477}
478EXPORT_SYMBOL(drm_crtc_get_hv_timing);
479
480/**
Matt Roperaf936292014-04-01 15:22:34 -0700481 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
482 * CRTC viewport
483 * @crtc: CRTC that framebuffer will be displayed on
484 * @x: x panning
485 * @y: y panning
486 * @mode: mode that framebuffer will be displayed under
487 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +0100488 */
Matt Roperaf936292014-04-01 15:22:34 -0700489int drm_crtc_check_viewport(const struct drm_crtc *crtc,
490 int x, int y,
491 const struct drm_display_mode *mode,
492 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +0100493
494{
495 int hdisplay, vdisplay;
496
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800497 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +0100498
Ville Syrjälä33e0be62015-10-16 18:38:39 +0300499 if (crtc->state &&
Ville Syrjäläbd2ef252016-09-26 19:30:46 +0300500 drm_rotation_90_or_270(crtc->primary->state->rotation))
Damien Lespiauc11e9282013-09-25 16:45:30 +0100501 swap(hdisplay, vdisplay);
502
Daniel Vetter43968d72016-09-21 10:59:24 +0200503 return drm_framebuffer_check_src_coords(x << 16, y << 16,
504 hdisplay << 16, vdisplay << 16,
505 fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +0100506}
Matt Roperaf936292014-04-01 15:22:34 -0700507EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +0100508
Daniel Vetter2d13b672012-12-11 13:47:23 +0100509/**
Dave Airlief453ba02008-11-07 14:05:41 -0800510 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100511 * @dev: drm device for the ioctl
512 * @data: data pointer for the ioctl
513 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -0800514 *
Dave Airlief453ba02008-11-07 14:05:41 -0800515 * Build a new CRTC configuration based on user request.
516 *
517 * Called by the user via ioctl.
518 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100519 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +0100520 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800521 */
522int drm_mode_setcrtc(struct drm_device *dev, void *data,
523 struct drm_file *file_priv)
524{
525 struct drm_mode_config *config = &dev->mode_config;
526 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +0200527 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -0800528 struct drm_connector **connector_set = NULL, *connector;
529 struct drm_framebuffer *fb = NULL;
530 struct drm_display_mode *mode = NULL;
531 struct drm_mode_set set;
532 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200533 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800534 int i;
535
Dave Airliefb3b06c2011-02-08 13:55:21 +1000536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
537 return -EINVAL;
538
Zhao Junwang01447e92015-07-07 17:08:35 +0800539 /*
540 * Universal plane src offsets are only 16.16, prevent havoc for
541 * drivers using universal plane code internally.
542 */
543 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +0200544 return -ERANGE;
545
Daniel Vetter84849902012-12-02 00:28:11 +0100546 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -0400547 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
548 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800549 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +0300550 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800551 goto out;
552 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200553 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800554
555 if (crtc_req->mode_valid) {
556 /* If we have a mode we need a framebuffer. */
557 /* If we pass -1, set the mode with the currently bound fb */
558 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -0700559 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +0200560 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
561 ret = -EINVAL;
562 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800563 }
Matt Roperf4510a22014-04-01 15:22:40 -0700564 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +0100565 /* Make refcounting symmetric with the lookup path. */
566 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800567 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +0100568 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
569 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800570 DRM_DEBUG_KMS("Unknown FB ID%d\n",
571 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +0300572 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800573 goto out;
574 }
Dave Airlief453ba02008-11-07 14:05:41 -0800575 }
576
577 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200578 if (!mode) {
579 ret = -ENOMEM;
580 goto out;
581 }
582
Daniel Stone934a8a82015-05-22 13:34:48 +0100583 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +0200584 if (ret) {
585 DRM_DEBUG_KMS("Invalid mode\n");
586 goto out;
587 }
588
Laurent Pinchart7eb5f302015-03-09 10:41:07 +0200589 /*
590 * Check whether the primary plane supports the fb pixel format.
591 * Drivers not implementing the universal planes API use a
592 * default formats list provided by the DRM core which doesn't
593 * match real hardware capabilities. Skip the check in that
594 * case.
595 */
596 if (!crtc->primary->format_default) {
597 ret = drm_plane_check_pixel_format(crtc->primary,
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200598 fb->format->format);
Laurent Pinchart7eb5f302015-03-09 10:41:07 +0200599 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000600 struct drm_format_name_buf format_name;
601 DRM_DEBUG_KMS("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200602 drm_get_format_name(fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000603 &format_name));
Laurent Pinchart7eb5f302015-03-09 10:41:07 +0200604 goto out;
605 }
606 }
607
Damien Lespiauc11e9282013-09-25 16:45:30 +0100608 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
609 mode, fb);
610 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +0200611 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +0100612
Dave Airlief453ba02008-11-07 14:05:41 -0800613 }
614
615 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800616 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800617 ret = -EINVAL;
618 goto out;
619 }
620
Jakob Bornecrantz7781de72009-08-03 13:43:58 +0100621 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800622 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800623 crtc_req->count_connectors);
624 ret = -EINVAL;
625 goto out;
626 }
627
628 if (crtc_req->count_connectors > 0) {
629 u32 out_id;
630
631 /* Avoid unbounded kernel memory allocation */
632 if (crtc_req->count_connectors > config->num_connector) {
633 ret = -EINVAL;
634 goto out;
635 }
636
Thierry Reding2f6c5382014-12-10 13:03:36 +0100637 connector_set = kmalloc_array(crtc_req->count_connectors,
638 sizeof(struct drm_connector *),
639 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800640 if (!connector_set) {
641 ret = -ENOMEM;
642 goto out;
643 }
644
645 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +1000646 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +0200647 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -0800648 if (get_user(out_id, &set_connectors_ptr[i])) {
649 ret = -EFAULT;
650 goto out;
651 }
652
Dave Airlieb164d312016-04-27 11:10:09 +1000653 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -0400654 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800655 DRM_DEBUG_KMS("Connector id %d unknown\n",
656 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +0300657 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -0800658 goto out;
659 }
Jerome Glisse94401062010-07-15 15:43:25 -0400660 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
661 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300662 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800663
664 connector_set[i] = connector;
665 }
666 }
667
668 set.crtc = crtc;
669 set.x = crtc_req->x;
670 set.y = crtc_req->y;
671 set.mode = mode;
672 set.connectors = connector_set;
673 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000674 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100675 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -0800676
677out:
Daniel Vetterb0d12322012-12-11 01:07:12 +0100678 if (fb)
679 drm_framebuffer_unreference(fb);
680
Dave Airlieb164d312016-04-27 11:10:09 +1000681 if (connector_set) {
682 for (i = 0; i < crtc_req->count_connectors; i++) {
683 if (connector_set[i])
684 drm_connector_unreference(connector_set[i]);
685 }
686 }
Dave Airlief453ba02008-11-07 14:05:41 -0800687 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200688 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +0100689 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800690 return ret;
691}
692
Daniel Vetter949619f2016-08-29 10:27:51 +0200693int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
694 struct drm_property *property,
695 uint64_t value)
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300696{
697 int ret = -EINVAL;
698 struct drm_crtc *crtc = obj_to_crtc(obj);
699
700 if (crtc->funcs->set_property)
701 ret = crtc->funcs->set_property(crtc, property, value);
702 if (!ret)
703 drm_object_property_set_value(obj, property, value);
704
705 return ret;
706}