]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/drm_crtc.c
udmabuf: fix error code in map_udmabuf()
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
6ba6d03e 32#include <linux/ctype.h>
f453ba04 33#include <linux/list.h>
5a0e3ad6 34#include <linux/slab.h>
2d1a8a48 35#include <linux/export.h>
6d6003c4 36#include <linux/dma-fence.h>
e6120d64 37#include <linux/uaccess.h>
760285e7
DH
38#include <drm/drm_crtc.h>
39#include <drm/drm_edid.h>
40#include <drm/drm_fourcc.h>
51fd371b 41#include <drm/drm_modeset_lock.h>
88a48e29 42#include <drm/drm_atomic.h>
3b96a0b1 43#include <drm/drm_auth.h>
9edbf1fa 44#include <drm/drm_debugfs_crc.h>
e6120d64
DV
45#include <drm/drm_drv.h>
46#include <drm/drm_print.h>
47#include <drm/drm_file.h>
f453ba04 48
8bd441b2 49#include "drm_crtc_internal.h"
67d0ec4e 50#include "drm_internal.h"
8bd441b2 51
d5d487eb
DV
52/**
53 * DOC: overview
54 *
55 * A CRTC represents the overall display pipeline. It receives pixel data from
56 * &drm_plane and blends them together. The &drm_display_mode is also attached
57 * to the CRTC, specifying display timings. On the output side the data is fed
58 * to one or more &drm_encoder, which are then each connected to one
59 * &drm_connector.
60 *
61 * To create a CRTC, a KMS drivers allocates and zeroes an instances of
62 * &struct drm_crtc (possibly as part of a larger structure) and registers it
63 * with a call to drm_crtc_init_with_planes().
64 *
65 * The CRTC is also the entry point for legacy modeset operations, see
66 * &drm_crtc_funcs.set_config, legacy plane operations, see
67 * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2, and other legacy
68 * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
69 * features are controlled through &drm_property and
70 * &drm_mode_config_funcs.atomic_check and &drm_mode_config_funcs.atomic_check.
71 */
72
6d1b81d8
SG
73/**
74 * drm_crtc_from_index - find the registered CRTC at an index
75 * @dev: DRM device
76 * @idx: index of registered CRTC to find for
77 *
78 * Given a CRTC index, return the registered CRTC from DRM device's
931c670d
SG
79 * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
80 * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
81 * &drm_driver.disable_vblank), since that still deals with indices instead
82 * of pointers to &struct drm_crtc."
6d1b81d8
SG
83 */
84struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
85{
86 struct drm_crtc *crtc;
87
88 drm_for_each_crtc(crtc, dev)
89 if (idx == crtc->index)
90 return crtc;
91
92 return NULL;
93}
94EXPORT_SYMBOL(drm_crtc_from_index);
95
6a0d9528
LW
96/**
97 * drm_crtc_force_disable - Forcibly turn off a CRTC
98 * @crtc: CRTC to turn off
99 *
18dddadc
DV
100 * Note: This should only be used by non-atomic legacy drivers.
101 *
6a0d9528
LW
102 * Returns:
103 * Zero on success, error code on failure.
104 */
105int drm_crtc_force_disable(struct drm_crtc *crtc)
106{
107 struct drm_mode_set set = {
108 .crtc = crtc,
109 };
110
18dddadc
DV
111 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
112
6a0d9528
LW
113 return drm_mode_set_config_internal(&set);
114}
115EXPORT_SYMBOL(drm_crtc_force_disable);
116
117/**
118 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
119 * @dev: DRM device whose CRTCs to turn off
120 *
121 * Drivers may want to call this on unload to ensure that all displays are
122 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
123 *
18dddadc
DV
124 * Note: This should only be used by non-atomic legacy drivers. For an atomic
125 * version look at drm_atomic_helper_shutdown().
126 *
6a0d9528
LW
127 * Returns:
128 * Zero on success, error code on failure.
129 */
130int drm_crtc_force_disable_all(struct drm_device *dev)
131{
132 struct drm_crtc *crtc;
133 int ret = 0;
134
135 drm_modeset_lock_all(dev);
136 drm_for_each_crtc(crtc, dev)
137 if (crtc->enabled) {
138 ret = drm_crtc_force_disable(crtc);
139 if (ret)
140 goto out;
141 }
142out:
143 drm_modeset_unlock_all(dev);
144 return ret;
145}
146EXPORT_SYMBOL(drm_crtc_force_disable_all);
147
fa3ab4c2
VS
148static unsigned int drm_num_crtcs(struct drm_device *dev)
149{
150 unsigned int num = 0;
151 struct drm_crtc *tmp;
152
153 drm_for_each_crtc(tmp, dev) {
154 num++;
155 }
156
157 return num;
158}
159
28575f16 160int drm_crtc_register_all(struct drm_device *dev)
79190ea2
BG
161{
162 struct drm_crtc *crtc;
163 int ret = 0;
164
165 drm_for_each_crtc(crtc, dev) {
9edbf1fa
TV
166 if (drm_debugfs_crtc_add(crtc))
167 DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
168 crtc->name);
169
79190ea2
BG
170 if (crtc->funcs->late_register)
171 ret = crtc->funcs->late_register(crtc);
172 if (ret)
173 return ret;
174 }
175
176 return 0;
177}
178
28575f16 179void drm_crtc_unregister_all(struct drm_device *dev)
79190ea2
BG
180{
181 struct drm_crtc *crtc;
182
183 drm_for_each_crtc(crtc, dev) {
184 if (crtc->funcs->early_unregister)
185 crtc->funcs->early_unregister(crtc);
9edbf1fa 186 drm_debugfs_crtc_remove(crtc);
79190ea2
BG
187 }
188}
189
9edbf1fa
TV
190static int drm_crtc_crc_init(struct drm_crtc *crtc)
191{
192#ifdef CONFIG_DEBUG_FS
193 spin_lock_init(&crtc->crc.lock);
194 init_waitqueue_head(&crtc->crc.wq);
195 crtc->crc.source = kstrdup("auto", GFP_KERNEL);
196 if (!crtc->crc.source)
197 return -ENOMEM;
198#endif
199 return 0;
200}
201
202static void drm_crtc_crc_fini(struct drm_crtc *crtc)
203{
204#ifdef CONFIG_DEBUG_FS
205 kfree(crtc->crc.source);
206#endif
207}
208
35f8cc3b
GP
209static const struct dma_fence_ops drm_crtc_fence_ops;
210
6d6003c4
GP
211static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
212{
213 BUG_ON(fence->ops != &drm_crtc_fence_ops);
214 return container_of(fence->lock, struct drm_crtc, fence_lock);
215}
216
217static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
218{
219 struct drm_crtc *crtc = fence_to_crtc(fence);
220
221 return crtc->dev->driver->name;
222}
223
224static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
225{
226 struct drm_crtc *crtc = fence_to_crtc(fence);
227
228 return crtc->timeline_name;
229}
230
35f8cc3b 231static const struct dma_fence_ops drm_crtc_fence_ops = {
6d6003c4
GP
232 .get_driver_name = drm_crtc_fence_get_driver_name,
233 .get_timeline_name = drm_crtc_fence_get_timeline_name,
6d6003c4
GP
234};
235
35f8cc3b
GP
236struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
237{
238 struct dma_fence *fence;
239
240 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
241 if (!fence)
242 return NULL;
243
244 dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
245 crtc->fence_context, ++crtc->fence_seqno);
246
247 return fence;
248}
249
f453ba04 250/**
e13161af
MR
251 * drm_crtc_init_with_planes - Initialise a new CRTC object with
252 * specified primary and cursor planes.
f453ba04
DA
253 * @dev: DRM device
254 * @crtc: CRTC object to init
e13161af
MR
255 * @primary: Primary plane for CRTC
256 * @cursor: Cursor plane for CRTC
f453ba04 257 * @funcs: callbacks for the new CRTC
f9882876 258 * @name: printf style format string for the CRTC name, or NULL for default name
f453ba04 259 *
532b3671
DV
260 * Inits a new object created as base part of a driver crtc object. Drivers
261 * should use this function instead of drm_crtc_init(), which is only provided
262 * for backwards compatibility with drivers which do not yet support universal
263 * planes). For really simple hardware which has only 1 plane look at
264 * drm_simple_display_pipe_init() instead.
6bfc56aa 265 *
c8e32cc1 266 * Returns:
6bfc56aa 267 * Zero on success, error code on failure.
f453ba04 268 */
e13161af
MR
269int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
270 struct drm_plane *primary,
fc1d3e44 271 struct drm_plane *cursor,
f9882876
VS
272 const struct drm_crtc_funcs *funcs,
273 const char *name, ...)
f453ba04 274{
51fd371b 275 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa
VS
276 int ret;
277
522cf91f
BG
278 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
279 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
280
2a8d3eac
VS
281 /* crtc index is used with 32bit bitmasks */
282 if (WARN_ON(config->num_crtc >= 32))
283 return -EINVAL;
284
ba1f665f
HM
285 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
286 (!funcs->atomic_destroy_state ||
287 !funcs->atomic_duplicate_state));
288
f453ba04
DA
289 crtc->dev = dev;
290 crtc->funcs = funcs;
291
3b24f7d6
DV
292 INIT_LIST_HEAD(&crtc->commit_list);
293 spin_lock_init(&crtc->commit_lock);
294
51fd371b 295 drm_modeset_lock_init(&crtc->mutex);
2135ea7a 296 ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
6bfc56aa 297 if (ret)
baf698b0 298 return ret;
f453ba04 299
fa3ab4c2
VS
300 if (name) {
301 va_list ap;
302
303 va_start(ap, name);
304 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
305 va_end(ap);
306 } else {
307 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
308 drm_num_crtcs(dev));
309 }
310 if (!crtc->name) {
7c8f6d25 311 drm_mode_object_unregister(dev, &crtc->base);
fa3ab4c2
VS
312 return -ENOMEM;
313 }
314
6d6003c4
GP
315 crtc->fence_context = dma_fence_context_alloc(1);
316 spin_lock_init(&crtc->fence_lock);
317 snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
318 "CRTC:%d-%s", crtc->base.id, crtc->name);
319
bffd9de0
PZ
320 crtc->base.properties = &crtc->properties;
321
51fd371b 322 list_add_tail(&crtc->head, &config->crtc_list);
490d3d1b 323 crtc->index = config->num_crtc++;
6bfc56aa 324
e13161af 325 crtc->primary = primary;
fc1d3e44 326 crtc->cursor = cursor;
7abc7d47 327 if (primary && !primary->possible_crtcs)
6a52193b 328 primary->possible_crtcs = drm_crtc_mask(crtc);
7abc7d47 329 if (cursor && !cursor->possible_crtcs)
6a52193b 330 cursor->possible_crtcs = drm_crtc_mask(crtc);
e13161af 331
9edbf1fa
TV
332 ret = drm_crtc_crc_init(crtc);
333 if (ret) {
334 drm_mode_object_unregister(dev, &crtc->base);
335 return ret;
336 }
337
eab3bbef
DV
338 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
339 drm_object_attach_property(&crtc->base, config->prop_active, 0);
955f3c33 340 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
beaf5af4
GP
341 drm_object_attach_property(&crtc->base,
342 config->prop_out_fence_ptr, 0);
eab3bbef
DV
343 }
344
baf698b0 345 return 0;
f453ba04 346}
e13161af 347EXPORT_SYMBOL(drm_crtc_init_with_planes);
f453ba04
DA
348
349/**
ad6f5c34 350 * drm_crtc_cleanup - Clean up the core crtc usage
f453ba04
DA
351 * @crtc: CRTC to cleanup
352 *
ad6f5c34
VS
353 * This function cleans up @crtc and removes it from the DRM mode setting
354 * core. Note that the function does *not* free the crtc structure itself,
355 * this is the responsibility of the caller.
f453ba04
DA
356 */
357void drm_crtc_cleanup(struct drm_crtc *crtc)
358{
359 struct drm_device *dev = crtc->dev;
360
490d3d1b
CW
361 /* Note that the crtc_list is considered to be static; should we
362 * remove the drm_crtc at runtime we would have to decrement all
363 * the indices on the drm_crtc after us in the crtc_list.
364 */
365
9edbf1fa
TV
366 drm_crtc_crc_fini(crtc);
367
9e1c156f
SK
368 kfree(crtc->gamma_store);
369 crtc->gamma_store = NULL;
f453ba04 370
51fd371b
RC
371 drm_modeset_lock_fini(&crtc->mutex);
372
7c8f6d25 373 drm_mode_object_unregister(dev, &crtc->base);
f453ba04
DA
374 list_del(&crtc->head);
375 dev->mode_config.num_crtc--;
3009c037
TR
376
377 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
378 if (crtc->state && crtc->funcs->atomic_destroy_state)
379 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
a18c0af1 380
fa3ab4c2
VS
381 kfree(crtc->name);
382
a18c0af1 383 memset(crtc, 0, sizeof(*crtc));
f453ba04
DA
384}
385EXPORT_SYMBOL(drm_crtc_cleanup);
386
f453ba04
DA
387/**
388 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
389 * @dev: drm device for the ioctl
390 * @data: data pointer for the ioctl
391 * @file_priv: drm file for the ioctl call
f453ba04 392 *
f453ba04
DA
393 * Construct a CRTC configuration structure to return to the user.
394 *
395 * Called by the user via ioctl.
396 *
c8e32cc1 397 * Returns:
1a498633 398 * Zero on success, negative errno on failure.
f453ba04
DA
399 */
400int drm_mode_getcrtc(struct drm_device *dev,
401 void *data, struct drm_file *file_priv)
402{
403 struct drm_mode_crtc *crtc_resp = data;
404 struct drm_crtc *crtc;
64c32b49 405 struct drm_plane *plane;
f453ba04 406
fb3b06c8
DA
407 if (!drm_core_check_feature(dev, DRIVER_MODESET))
408 return -EINVAL;
409
418da172 410 crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
fcf93f69
DV
411 if (!crtc)
412 return -ENOENT;
f453ba04 413
64c32b49
VS
414 plane = crtc->primary;
415
f453ba04 416 crtc_resp->gamma_size = crtc->gamma_size;
de7b6be7 417
64c32b49
VS
418 drm_modeset_lock(&plane->mutex, NULL);
419 if (plane->state && plane->state->fb)
420 crtc_resp->fb_id = plane->state->fb->base.id;
421 else if (!plane->state && plane->fb)
422 crtc_resp->fb_id = plane->fb->base.id;
f453ba04
DA
423 else
424 crtc_resp->fb_id = 0;
425
64c32b49
VS
426 if (plane->state) {
427 crtc_resp->x = plane->state->src_x >> 16;
428 crtc_resp->y = plane->state->src_y >> 16;
2c77bb29 429 }
64c32b49 430 drm_modeset_unlock(&plane->mutex);
2c77bb29
DV
431
432 drm_modeset_lock(&crtc->mutex, NULL);
433 if (crtc->state) {
31c946e8 434 if (crtc->state->enable) {
934a8a89 435 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
31c946e8 436 crtc_resp->mode_valid = 1;
31c946e8
DV
437 } else {
438 crtc_resp->mode_valid = 0;
439 }
f453ba04 440 } else {
31c946e8
DV
441 crtc_resp->x = crtc->x;
442 crtc_resp->y = crtc->y;
bf2d5eb9 443
31c946e8 444 if (crtc->enabled) {
934a8a89 445 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
31c946e8
DV
446 crtc_resp->mode_valid = 1;
447
448 } else {
449 crtc_resp->mode_valid = 0;
450 }
f453ba04 451 }
ace5bf0e
AN
452 if (!file_priv->aspect_ratio_allowed)
453 crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2c77bb29 454 drm_modeset_unlock(&crtc->mutex);
f453ba04 455
baf698b0 456 return 0;
f453ba04
DA
457}
458
2ceb585a
DV
459static int __drm_mode_set_config_internal(struct drm_mode_set *set,
460 struct drm_modeset_acquire_ctx *ctx)
2d13b679
DV
461{
462 struct drm_crtc *crtc = set->crtc;
5cef29aa
DV
463 struct drm_framebuffer *fb;
464 struct drm_crtc *tmp;
b0d12325
DV
465 int ret;
466
69a8a196
VS
467 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
468
5cef29aa
DV
469 /*
470 * NOTE: ->set_config can also disable other crtcs (if we steal all
471 * connectors from it), hence we need to refcount the fbs across all
472 * crtcs. Atomic modeset will have saner semantics ...
473 */
3ed70ecd
VS
474 drm_for_each_crtc(tmp, crtc->dev) {
475 struct drm_plane *plane = tmp->primary;
476
477 plane->old_fb = plane->fb;
478 }
5cef29aa 479
b0d12325 480 fb = set->fb;
2d13b679 481
a4eff9aa 482 ret = crtc->funcs->set_config(set, ctx);
b0d12325 483 if (ret == 0) {
e00fb856
VS
484 struct drm_plane *plane = crtc->primary;
485
69a8a196
VS
486 plane->crtc = fb ? crtc : NULL;
487 plane->fb = fb;
5cef29aa 488 }
cc85e121 489
e4f62546 490 drm_for_each_crtc(tmp, crtc->dev) {
3ed70ecd
VS
491 struct drm_plane *plane = tmp->primary;
492
493 if (plane->fb)
494 drm_framebuffer_get(plane->fb);
495 if (plane->old_fb)
496 drm_framebuffer_put(plane->old_fb);
497 plane->old_fb = NULL;
b0d12325
DV
498 }
499
500 return ret;
2d13b679 501}
69a8a196 502
d49473a5
DV
503/**
504 * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
505 * @set: modeset config to set
506 *
507 * This is a little helper to wrap internal calls to the
508 * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
509 * correct refcounting dance.
510 *
511 * This should only be used by non-atomic legacy drivers.
512 *
513 * Returns:
514 * Zero on success, negative errno on failure.
515 */
516int drm_mode_set_config_internal(struct drm_mode_set *set)
517{
518 WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
519
2ceb585a 520 return __drm_mode_set_config_internal(set, NULL);
d49473a5 521}
2d13b679
DV
522EXPORT_SYMBOL(drm_mode_set_config_internal);
523
af93629d
MR
524/**
525 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
526 * CRTC viewport
527 * @crtc: CRTC that framebuffer will be displayed on
528 * @x: x panning
529 * @y: y panning
530 * @mode: mode that framebuffer will be displayed under
531 * @fb: framebuffer to check size of
c11e9283 532 */
af93629d
MR
533int drm_crtc_check_viewport(const struct drm_crtc *crtc,
534 int x, int y,
535 const struct drm_display_mode *mode,
536 const struct drm_framebuffer *fb)
c11e9283
DL
537
538{
539 int hdisplay, vdisplay;
540
196cd5d3 541 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
a0c1bbb0 542
33e0be63 543 if (crtc->state &&
bd2ef25d 544 drm_rotation_90_or_270(crtc->primary->state->rotation))
c11e9283
DL
545 swap(hdisplay, vdisplay);
546
43968d7b
DV
547 return drm_framebuffer_check_src_coords(x << 16, y << 16,
548 hdisplay << 16, vdisplay << 16,
549 fb);
c11e9283 550}
af93629d 551EXPORT_SYMBOL(drm_crtc_check_viewport);
c11e9283 552
f453ba04
DA
553/**
554 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
555 * @dev: drm device for the ioctl
556 * @data: data pointer for the ioctl
557 * @file_priv: drm file for the ioctl call
f453ba04 558 *
f453ba04
DA
559 * Build a new CRTC configuration based on user request.
560 *
561 * Called by the user via ioctl.
562 *
c8e32cc1 563 * Returns:
1a498633 564 * Zero on success, negative errno on failure.
f453ba04
DA
565 */
566int drm_mode_setcrtc(struct drm_device *dev, void *data,
567 struct drm_file *file_priv)
568{
569 struct drm_mode_config *config = &dev->mode_config;
570 struct drm_mode_crtc *crtc_req = data;
6653cc8d 571 struct drm_crtc *crtc;
64c32b49 572 struct drm_plane *plane;
f453ba04
DA
573 struct drm_connector **connector_set = NULL, *connector;
574 struct drm_framebuffer *fb = NULL;
575 struct drm_display_mode *mode = NULL;
576 struct drm_mode_set set;
577 uint32_t __user *set_connectors_ptr;
2ceb585a 578 struct drm_modeset_acquire_ctx ctx;
4a1b0714 579 int ret;
f453ba04
DA
580 int i;
581
fb3b06c8
DA
582 if (!drm_core_check_feature(dev, DRIVER_MODESET))
583 return -EINVAL;
584
01447e9f
ZJ
585 /*
586 * Universal plane src offsets are only 16.16, prevent havoc for
587 * drivers using universal plane code internally.
588 */
589 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1d97e915
VS
590 return -ERANGE;
591
418da172 592 crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
a2b34e22 593 if (!crtc) {
58367ed6 594 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2ceb585a 595 return -ENOENT;
f453ba04 596 }
fa3ab4c2 597 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
f453ba04 598
64c32b49
VS
599 plane = crtc->primary;
600
53552d5d 601 mutex_lock(&crtc->dev->mode_config.mutex);
3fa6a073 602 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
2ceb585a
DV
603retry:
604 ret = drm_modeset_lock_all_ctx(crtc->dev, &ctx);
605 if (ret)
606 goto out;
bf2d5eb9 607
f453ba04
DA
608 if (crtc_req->mode_valid) {
609 /* If we have a mode we need a framebuffer. */
610 /* If we pass -1, set the mode with the currently bound fb */
611 if (crtc_req->fb_id == -1) {
a36c027d
VS
612 struct drm_framebuffer *old_fb;
613
614 if (plane->state)
615 old_fb = plane->state->fb;
616 else
617 old_fb = plane->fb;
618
619 if (!old_fb) {
6653cc8d
VS
620 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
621 ret = -EINVAL;
622 goto out;
f453ba04 623 }
bf2d5eb9 624
a36c027d 625 fb = old_fb;
b0d12325 626 /* Make refcounting symmetric with the lookup path. */
a4a69da0 627 drm_framebuffer_get(fb);
f453ba04 628 } else {
418da172 629 fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
786b99ed 630 if (!fb) {
58367ed6
ZY
631 DRM_DEBUG_KMS("Unknown FB ID%d\n",
632 crtc_req->fb_id);
37c4e705 633 ret = -ENOENT;
f453ba04
DA
634 goto out;
635 }
f453ba04
DA
636 }
637
638 mode = drm_mode_create(dev);
ee34ab5b
VS
639 if (!mode) {
640 ret = -ENOMEM;
641 goto out;
642 }
ace5bf0e
AN
643 if (!file_priv->aspect_ratio_allowed &&
644 (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
645 DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
646 ret = -EINVAL;
647 goto out;
648 }
649
ee34ab5b 650
75a655e0 651 ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
90367bf6 652 if (ret) {
6ab0edf4
VS
653 DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
654 ret, drm_get_mode_status_name(mode->status));
655 drm_mode_debug_printmodeline(mode);
90367bf6
VS
656 goto out;
657 }
658
7eb5f302
LP
659 /*
660 * Check whether the primary plane supports the fb pixel format.
661 * Drivers not implementing the universal planes API use a
662 * default formats list provided by the DRM core which doesn't
663 * match real hardware capabilities. Skip the check in that
664 * case.
665 */
64c32b49
VS
666 if (!plane->format_default) {
667 ret = drm_plane_check_pixel_format(plane,
23163a7d
VS
668 fb->format->format,
669 fb->modifier);
7eb5f302 670 if (ret) {
b3c11ac2 671 struct drm_format_name_buf format_name;
23163a7d
VS
672 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
673 drm_get_format_name(fb->format->format,
674 &format_name),
675 fb->modifier);
7eb5f302
LP
676 goto out;
677 }
678 }
679
c11e9283
DL
680 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
681 mode, fb);
682 if (ret)
5f61bb42 683 goto out;
c11e9283 684
f453ba04
DA
685 }
686
687 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 688 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
689 ret = -EINVAL;
690 goto out;
691 }
692
7781de74 693 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 694 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
695 crtc_req->count_connectors);
696 ret = -EINVAL;
697 goto out;
698 }
699
700 if (crtc_req->count_connectors > 0) {
701 u32 out_id;
702
703 /* Avoid unbounded kernel memory allocation */
704 if (crtc_req->count_connectors > config->num_connector) {
705 ret = -EINVAL;
706 goto out;
707 }
708
2f6c5389
TR
709 connector_set = kmalloc_array(crtc_req->count_connectors,
710 sizeof(struct drm_connector *),
711 GFP_KERNEL);
f453ba04
DA
712 if (!connector_set) {
713 ret = -ENOMEM;
714 goto out;
715 }
716
717 for (i = 0; i < crtc_req->count_connectors; i++) {
b164d31f 718 connector_set[i] = NULL;
81f6c7f8 719 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
720 if (get_user(out_id, &set_connectors_ptr[i])) {
721 ret = -EFAULT;
722 goto out;
723 }
724
418da172 725 connector = drm_connector_lookup(dev, file_priv, out_id);
a2b34e22 726 if (!connector) {
58367ed6
ZY
727 DRM_DEBUG_KMS("Connector id %d unknown\n",
728 out_id);
f27657f2 729 ret = -ENOENT;
f453ba04
DA
730 goto out;
731 }
9440106b
JG
732 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
733 connector->base.id,
25933820 734 connector->name);
f453ba04
DA
735
736 connector_set[i] = connector;
737 }
738 }
739
740 set.crtc = crtc;
741 set.x = crtc_req->x;
742 set.y = crtc_req->y;
743 set.mode = mode;
744 set.connectors = connector_set;
745 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 746 set.fb = fb;
69a8a196
VS
747
748 if (drm_drv_uses_atomic_modeset(dev))
749 ret = crtc->funcs->set_config(&set, &ctx);
750 else
751 ret = __drm_mode_set_config_internal(&set, &ctx);
f453ba04
DA
752
753out:
b0d12325 754 if (fb)
a4a69da0 755 drm_framebuffer_put(fb);
b0d12325 756
b164d31f
DA
757 if (connector_set) {
758 for (i = 0; i < crtc_req->count_connectors; i++) {
759 if (connector_set[i])
ad093607 760 drm_connector_put(connector_set[i]);
b164d31f
DA
761 }
762 }
f453ba04 763 kfree(connector_set);
ee34ab5b 764 drm_mode_destroy(dev, mode);
2ceb585a 765 if (ret == -EDEADLK) {
3fa6a073
ML
766 ret = drm_modeset_backoff(&ctx);
767 if (!ret)
768 goto retry;
2ceb585a
DV
769 }
770 drm_modeset_drop_locks(&ctx);
771 drm_modeset_acquire_fini(&ctx);
53552d5d 772 mutex_unlock(&crtc->dev->mode_config.mutex);
2ceb585a 773
f453ba04
DA
774 return ret;
775}
776
949619f3
DV
777int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
778 struct drm_property *property,
779 uint64_t value)
4a67d391 780{
bffd9de0
PZ
781 int ret = -EINVAL;
782 struct drm_crtc *crtc = obj_to_crtc(obj);
4a67d391 783
bffd9de0
PZ
784 if (crtc->funcs->set_property)
785 ret = crtc->funcs->set_property(crtc, property, value);
144a7999 786 if (!ret)
bffd9de0 787 drm_object_property_set_value(obj, property, value);
4a67d391 788
bffd9de0 789 return ret;
4a67d391 790}