Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/ctype.h>
33#include <linux/list.h>
34#include <linux/slab.h>
35#include <linux/export.h>
36#include <linux/dma-fence.h>
37#include <linux/uaccess.h>
38#include <drm/drm_blend.h>
39#include <drm/drm_crtc.h>
40#include <drm/drm_edid.h>
41#include <drm/drm_fourcc.h>
42#include <drm/drm_framebuffer.h>
43#include <drm/drm_managed.h>
44#include <drm/drm_modeset_lock.h>
45#include <drm/drm_atomic.h>
46#include <drm/drm_auth.h>
47#include <drm/drm_debugfs_crc.h>
48#include <drm/drm_drv.h>
49#include <drm/drm_print.h>
50#include <drm/drm_file.h>
51
52#include "drm_crtc_internal.h"
53#include "drm_internal.h"
54
55/**
56 * DOC: overview
57 *
58 * A CRTC represents the overall display pipeline. It receives pixel data from
59 * &drm_plane and blends them together. The &drm_display_mode is also attached
60 * to the CRTC, specifying display timings. On the output side the data is fed
61 * to one or more &drm_encoder, which are then each connected to one
62 * &drm_connector.
63 *
64 * To create a CRTC, a KMS driver allocates and zeroes an instance of
65 * &struct drm_crtc (possibly as part of a larger structure) and registers it
66 * with a call to drm_crtc_init_with_planes().
67 *
68 * The CRTC is also the entry point for legacy modeset operations (see
69 * &drm_crtc_funcs.set_config), legacy plane operations (see
70 * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2), and other legacy
71 * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
72 * features are controlled through &drm_property and
73 * &drm_mode_config_funcs.atomic_check.
74 */
75
76/**
77 * drm_crtc_from_index - find the registered CRTC at an index
78 * @dev: DRM device
79 * @idx: index of registered CRTC to find for
80 *
81 * Given a CRTC index, return the registered CRTC from DRM device's
82 * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
83 * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
84 * &drm_driver.disable_vblank), since that still deals with indices instead
85 * of pointers to &struct drm_crtc."
86 */
87struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
88{
89 struct drm_crtc *crtc;
90
91 drm_for_each_crtc(crtc, dev)
92 if (idx == crtc->index)
93 return crtc;
94
95 return NULL;
96}
97EXPORT_SYMBOL(drm_crtc_from_index);
98
99int drm_crtc_force_disable(struct drm_crtc *crtc)
100{
101 struct drm_mode_set set = {
102 .crtc = crtc,
103 };
104
105 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
106
107 return drm_mode_set_config_internal(&set);
108}
109
110int drm_crtc_register_all(struct drm_device *dev)
111{
112 struct drm_crtc *crtc;
113 int ret = 0;
114
115 drm_for_each_crtc(crtc, dev) {
116 drm_debugfs_crtc_add(crtc);
117
118 if (crtc->funcs->late_register)
119 ret = crtc->funcs->late_register(crtc);
120 if (ret)
121 return ret;
122 }
123
124 return 0;
125}
126
127void drm_crtc_unregister_all(struct drm_device *dev)
128{
129 struct drm_crtc *crtc;
130
131 drm_for_each_crtc(crtc, dev) {
132 if (crtc->funcs->early_unregister)
133 crtc->funcs->early_unregister(crtc);
134 drm_debugfs_crtc_remove(crtc);
135 }
136}
137
138static int drm_crtc_crc_init(struct drm_crtc *crtc)
139{
140#ifdef CONFIG_DEBUG_FS
141 spin_lock_init(&crtc->crc.lock);
142 init_waitqueue_head(&crtc->crc.wq);
143 crtc->crc.source = kstrdup("auto", GFP_KERNEL);
144 if (!crtc->crc.source)
145 return -ENOMEM;
146#endif
147 return 0;
148}
149
150static void drm_crtc_crc_fini(struct drm_crtc *crtc)
151{
152#ifdef CONFIG_DEBUG_FS
153 kfree(crtc->crc.source);
154#endif
155}
156
157static const struct dma_fence_ops drm_crtc_fence_ops;
158
159static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
160{
161 BUG_ON(rcu_access_pointer(fence->ops) != &drm_crtc_fence_ops);
162 return container_of(fence->extern_lock, struct drm_crtc, fence_lock);
163}
164
165static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
166{
167 struct drm_crtc *crtc = fence_to_crtc(fence);
168
169 return crtc->dev->driver->name;
170}
171
172static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
173{
174 struct drm_crtc *crtc = fence_to_crtc(fence);
175
176 return crtc->timeline_name;
177}
178
179static const struct dma_fence_ops drm_crtc_fence_ops = {
180 .get_driver_name = drm_crtc_fence_get_driver_name,
181 .get_timeline_name = drm_crtc_fence_get_timeline_name,
182};
183
184struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
185{
186 struct dma_fence *fence;
187
188 fence = kzalloc_obj(*fence);
189 if (!fence)
190 return NULL;
191
192 dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
193 crtc->fence_context, ++crtc->fence_seqno);
194
195 return fence;
196}
197
198/**
199 * DOC: standard CRTC properties
200 *
201 * DRM CRTCs have a few standardized properties:
202 *
203 * ACTIVE:
204 * Atomic property for setting the power state of the CRTC. When set to 1
205 * the CRTC will actively display content. When set to 0 the CRTC will be
206 * powered off. There is no expectation that user-space will reset CRTC
207 * resources like the mode and planes when setting ACTIVE to 0.
208 *
209 * User-space can rely on an ACTIVE change to 1 to never fail an atomic
210 * test as long as no other property has changed. If a change to ACTIVE
211 * fails an atomic test, this is a driver bug. For this reason setting
212 * ACTIVE to 0 must not release internal resources (like reserved memory
213 * bandwidth or clock generators).
214 *
215 * Note that the legacy DPMS property on connectors is internally routed
216 * to control this property for atomic drivers.
217 * MODE_ID:
218 * Atomic property for setting the CRTC display timings. The value is the
219 * ID of a blob containing the DRM mode info. To disable the CRTC,
220 * user-space must set this property to 0.
221 *
222 * Setting MODE_ID to 0 will release reserved resources for the CRTC.
223 * SCALING_FILTER:
224 * Atomic property for setting the scaling filter for CRTC scaler
225 *
226 * The value of this property can be one of the following:
227 *
228 * Default:
229 * Driver's default scaling filter
230 * Nearest Neighbor:
231 * Nearest Neighbor scaling filter
232 * SHARPNESS_STRENGTH:
233 * Atomic property for setting the sharpness strength/intensity by userspace.
234 *
235 * The value of this property is set as an integer value ranging
236 * from 0 - 255 where:
237 *
238 * 0: Sharpness feature is disabled(default value).
239 *
240 * 1: Minimum sharpness.
241 *
242 * 255: Maximum sharpness.
243 *
244 * User can gradually increase or decrease the sharpness level and can
245 * set the optimum value depending on content.
246 * This value will be passed to kernel through the UAPI.
247 * The setting of this property does not require modeset.
248 * The sharpness effect takes place post blending on the final composed output.
249 * If the feature is disabled, the content remains same without any sharpening effect
250 * and when this feature is applied, it enhances the clarity of the content.
251 */
252
253__printf(6, 0)
254static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
255 struct drm_plane *primary,
256 struct drm_plane *cursor,
257 const struct drm_crtc_funcs *funcs,
258 const char *name, va_list ap)
259{
260 struct drm_mode_config *config = &dev->mode_config;
261 int ret;
262
263 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
264 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
265
266 /* crtc index is used with 32bit bitmasks */
267 if (WARN_ON(config->num_crtc >= 32))
268 return -EINVAL;
269
270 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
271 (!funcs->atomic_destroy_state ||
272 !funcs->atomic_duplicate_state));
273
274 crtc->dev = dev;
275 crtc->funcs = funcs;
276
277 INIT_LIST_HEAD(&crtc->commit_list);
278 spin_lock_init(&crtc->commit_lock);
279
280 drm_modeset_lock_init(&crtc->mutex);
281 ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
282 if (ret)
283 return ret;
284
285 if (name) {
286 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
287 } else {
288 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d", config->num_crtc);
289 }
290 if (!crtc->name) {
291 drm_mode_object_unregister(dev, &crtc->base);
292 return -ENOMEM;
293 }
294
295 crtc->fence_context = dma_fence_context_alloc(1);
296 spin_lock_init(&crtc->fence_lock);
297 snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
298 "CRTC:%d-%s", crtc->base.id, crtc->name);
299
300 crtc->base.properties = &crtc->properties;
301
302 list_add_tail(&crtc->head, &config->crtc_list);
303 crtc->index = config->num_crtc++;
304
305 crtc->primary = primary;
306 crtc->cursor = cursor;
307 if (primary && !primary->possible_crtcs)
308 primary->possible_crtcs = drm_crtc_mask(crtc);
309 if (cursor && !cursor->possible_crtcs)
310 cursor->possible_crtcs = drm_crtc_mask(crtc);
311
312 ret = drm_crtc_crc_init(crtc);
313 if (ret) {
314 drm_mode_object_unregister(dev, &crtc->base);
315 return ret;
316 }
317
318 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
319 drm_object_attach_property(&crtc->base, config->prop_active, 0);
320 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
321 drm_object_attach_property(&crtc->base,
322 config->prop_out_fence_ptr, 0);
323 drm_object_attach_property(&crtc->base,
324 config->prop_vrr_enabled, 0);
325 }
326
327 return 0;
328}
329
330/**
331 * drm_crtc_init_with_planes - Initialise a new CRTC object with
332 * specified primary and cursor planes.
333 * @dev: DRM device
334 * @crtc: CRTC object to init
335 * @primary: Primary plane for CRTC
336 * @cursor: Cursor plane for CRTC
337 * @funcs: callbacks for the new CRTC
338 * @name: printf style format string for the CRTC name, or NULL for default name
339 *
340 * Inits a new object created as base part of a driver crtc object. Drivers
341 * should use this function instead of drm_crtc_init(), which is only provided
342 * for backwards compatibility with drivers which do not yet support universal
343 * planes).
344 * The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree()
345 * the crtc structure. The crtc structure should not be allocated with
346 * devm_kzalloc().
347 *
348 * The @primary and @cursor planes are only relevant for legacy uAPI, see
349 * &drm_crtc.primary and &drm_crtc.cursor.
350 *
351 * Note: consider using drmm_crtc_alloc_with_planes() or
352 * drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes()
353 * to let the DRM managed resource infrastructure take care of cleanup
354 * and deallocation.
355 *
356 * Returns:
357 * Zero on success, error code on failure.
358 */
359int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
360 struct drm_plane *primary,
361 struct drm_plane *cursor,
362 const struct drm_crtc_funcs *funcs,
363 const char *name, ...)
364{
365 va_list ap;
366 int ret;
367
368 WARN_ON(!funcs->destroy);
369
370 va_start(ap, name);
371 ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
372 name, ap);
373 va_end(ap);
374
375 return ret;
376}
377EXPORT_SYMBOL(drm_crtc_init_with_planes);
378
379static void drmm_crtc_init_with_planes_cleanup(struct drm_device *dev,
380 void *ptr)
381{
382 struct drm_crtc *crtc = ptr;
383
384 drm_crtc_cleanup(crtc);
385}
386
387__printf(6, 0)
388static int __drmm_crtc_init_with_planes(struct drm_device *dev,
389 struct drm_crtc *crtc,
390 struct drm_plane *primary,
391 struct drm_plane *cursor,
392 const struct drm_crtc_funcs *funcs,
393 const char *name,
394 va_list args)
395{
396 int ret;
397
398 drm_WARN_ON(dev, funcs && funcs->destroy);
399
400 ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
401 name, args);
402 if (ret)
403 return ret;
404
405 ret = drmm_add_action_or_reset(dev, drmm_crtc_init_with_planes_cleanup,
406 crtc);
407 if (ret)
408 return ret;
409
410 return 0;
411}
412
413/**
414 * drmm_crtc_init_with_planes - Initialise a new CRTC object with
415 * specified primary and cursor planes.
416 * @dev: DRM device
417 * @crtc: CRTC object to init
418 * @primary: Primary plane for CRTC
419 * @cursor: Cursor plane for CRTC
420 * @funcs: callbacks for the new CRTC
421 * @name: printf style format string for the CRTC name, or NULL for default name
422 *
423 * Inits a new object created as base part of a driver crtc object. Drivers
424 * should use this function instead of drm_crtc_init(), which is only provided
425 * for backwards compatibility with drivers which do not yet support universal
426 * planes).
427 *
428 * Cleanup is automatically handled through registering
429 * drmm_crtc_cleanup() with drmm_add_action(). The crtc structure should
430 * be allocated with drmm_kzalloc().
431 *
432 * The @drm_crtc_funcs.destroy hook must be NULL.
433 *
434 * The @primary and @cursor planes are only relevant for legacy uAPI, see
435 * &drm_crtc.primary and &drm_crtc.cursor.
436 *
437 * Returns:
438 * Zero on success, error code on failure.
439 */
440int drmm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
441 struct drm_plane *primary,
442 struct drm_plane *cursor,
443 const struct drm_crtc_funcs *funcs,
444 const char *name, ...)
445{
446 va_list ap;
447 int ret;
448
449 va_start(ap, name);
450 ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
451 name, ap);
452 va_end(ap);
453 if (ret)
454 return ret;
455
456 return 0;
457}
458EXPORT_SYMBOL(drmm_crtc_init_with_planes);
459
460void *__drmm_crtc_alloc_with_planes(struct drm_device *dev,
461 size_t size, size_t offset,
462 struct drm_plane *primary,
463 struct drm_plane *cursor,
464 const struct drm_crtc_funcs *funcs,
465 const char *name, ...)
466{
467 void *container;
468 struct drm_crtc *crtc;
469 va_list ap;
470 int ret;
471
472 if (WARN_ON(!funcs || funcs->destroy))
473 return ERR_PTR(-EINVAL);
474
475 container = drmm_kzalloc(dev, size, GFP_KERNEL);
476 if (!container)
477 return ERR_PTR(-ENOMEM);
478
479 crtc = container + offset;
480
481 va_start(ap, name);
482 ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
483 name, ap);
484 va_end(ap);
485 if (ret)
486 return ERR_PTR(ret);
487
488 return container;
489}
490EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
491
492/**
493 * drm_crtc_cleanup - Clean up the core crtc usage
494 * @crtc: CRTC to cleanup
495 *
496 * This function cleans up @crtc and removes it from the DRM mode setting
497 * core. Note that the function does *not* free the crtc structure itself,
498 * this is the responsibility of the caller.
499 */
500void drm_crtc_cleanup(struct drm_crtc *crtc)
501{
502 struct drm_device *dev = crtc->dev;
503
504 /* Note that the crtc_list is considered to be static; should we
505 * remove the drm_crtc at runtime we would have to decrement all
506 * the indices on the drm_crtc after us in the crtc_list.
507 */
508
509 drm_crtc_crc_fini(crtc);
510
511 kfree(crtc->gamma_store);
512 crtc->gamma_store = NULL;
513
514 drm_modeset_lock_fini(&crtc->mutex);
515
516 drm_mode_object_unregister(dev, &crtc->base);
517 list_del(&crtc->head);
518 dev->mode_config.num_crtc--;
519
520 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
521 if (crtc->state && crtc->funcs->atomic_destroy_state)
522 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
523
524 kfree(crtc->name);
525
526 memset(crtc, 0, sizeof(*crtc));
527}
528EXPORT_SYMBOL(drm_crtc_cleanup);
529
530/**
531 * drm_mode_getcrtc - get CRTC configuration
532 * @dev: drm device for the ioctl
533 * @data: data pointer for the ioctl
534 * @file_priv: drm file for the ioctl call
535 *
536 * Construct a CRTC configuration structure to return to the user.
537 *
538 * Called by the user via ioctl.
539 *
540 * Returns:
541 * Zero on success, negative errno on failure.
542 */
543int drm_mode_getcrtc(struct drm_device *dev,
544 void *data, struct drm_file *file_priv)
545{
546 struct drm_mode_crtc *crtc_resp = data;
547 struct drm_crtc *crtc;
548 struct drm_plane *plane;
549
550 if (!drm_core_check_feature(dev, DRIVER_MODESET))
551 return -EOPNOTSUPP;
552
553 crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
554 if (!crtc)
555 return -ENOENT;
556
557 plane = crtc->primary;
558
559 crtc_resp->gamma_size = crtc->gamma_size;
560
561 drm_modeset_lock(&plane->mutex, NULL);
562 if (plane->state && plane->state->fb)
563 crtc_resp->fb_id = plane->state->fb->base.id;
564 else if (!plane->state && plane->fb)
565 crtc_resp->fb_id = plane->fb->base.id;
566 else
567 crtc_resp->fb_id = 0;
568
569 if (plane->state) {
570 crtc_resp->x = plane->state->src_x >> 16;
571 crtc_resp->y = plane->state->src_y >> 16;
572 }
573 drm_modeset_unlock(&plane->mutex);
574
575 drm_modeset_lock(&crtc->mutex, NULL);
576 if (crtc->state) {
577 if (crtc->state->enable) {
578 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
579 crtc_resp->mode_valid = 1;
580 } else {
581 crtc_resp->mode_valid = 0;
582 }
583 } else {
584 crtc_resp->x = crtc->x;
585 crtc_resp->y = crtc->y;
586
587 if (crtc->enabled) {
588 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
589 crtc_resp->mode_valid = 1;
590
591 } else {
592 crtc_resp->mode_valid = 0;
593 }
594 }
595 if (!file_priv->aspect_ratio_allowed)
596 crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
597 drm_modeset_unlock(&crtc->mutex);
598
599 return 0;
600}
601
602static int __drm_mode_set_config_internal(struct drm_mode_set *set,
603 struct drm_modeset_acquire_ctx *ctx)
604{
605 struct drm_crtc *crtc = set->crtc;
606 struct drm_framebuffer *fb;
607 struct drm_crtc *tmp;
608 int ret;
609
610 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
611
612 /*
613 * NOTE: ->set_config can also disable other crtcs (if we steal all
614 * connectors from it), hence we need to refcount the fbs across all
615 * crtcs. Atomic modeset will have saner semantics ...
616 */
617 drm_for_each_crtc(tmp, crtc->dev) {
618 struct drm_plane *plane = tmp->primary;
619
620 plane->old_fb = plane->fb;
621 }
622
623 fb = set->fb;
624
625 ret = crtc->funcs->set_config(set, ctx);
626 if (ret == 0) {
627 struct drm_plane *plane = crtc->primary;
628
629 plane->crtc = fb ? crtc : NULL;
630 plane->fb = fb;
631 }
632
633 drm_for_each_crtc(tmp, crtc->dev) {
634 struct drm_plane *plane = tmp->primary;
635
636 if (plane->fb)
637 drm_framebuffer_get(plane->fb);
638 if (plane->old_fb)
639 drm_framebuffer_put(plane->old_fb);
640 plane->old_fb = NULL;
641 }
642
643 return ret;
644}
645
646/**
647 * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
648 * @set: modeset config to set
649 *
650 * This is a little helper to wrap internal calls to the
651 * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
652 * correct refcounting dance.
653 *
654 * This should only be used by non-atomic legacy drivers.
655 *
656 * Returns:
657 * Zero on success, negative errno on failure.
658 */
659int drm_mode_set_config_internal(struct drm_mode_set *set)
660{
661 WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
662
663 return __drm_mode_set_config_internal(set, NULL);
664}
665EXPORT_SYMBOL(drm_mode_set_config_internal);
666
667/**
668 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
669 * CRTC viewport
670 * @crtc: CRTC that framebuffer will be displayed on
671 * @x: x panning
672 * @y: y panning
673 * @mode: mode that framebuffer will be displayed under
674 * @fb: framebuffer to check size of
675 */
676int drm_crtc_check_viewport(const struct drm_crtc *crtc,
677 int x, int y,
678 const struct drm_display_mode *mode,
679 const struct drm_framebuffer *fb)
680
681{
682 int hdisplay, vdisplay;
683
684 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
685
686 if (crtc->state &&
687 drm_rotation_90_or_270(crtc->primary->state->rotation))
688 swap(hdisplay, vdisplay);
689
690 return drm_framebuffer_check_src_coords(x << 16, y << 16,
691 hdisplay << 16, vdisplay << 16,
692 fb);
693}
694EXPORT_SYMBOL(drm_crtc_check_viewport);
695
696/**
697 * drm_mode_setcrtc - set CRTC configuration
698 * @dev: drm device for the ioctl
699 * @data: data pointer for the ioctl
700 * @file_priv: drm file for the ioctl call
701 *
702 * Build a new CRTC configuration based on user request.
703 *
704 * Called by the user via ioctl.
705 *
706 * Returns:
707 * Zero on success, negative errno on failure.
708 */
709int drm_mode_setcrtc(struct drm_device *dev, void *data,
710 struct drm_file *file_priv)
711{
712 struct drm_mode_config *config = &dev->mode_config;
713 struct drm_mode_crtc *crtc_req = data;
714 struct drm_crtc *crtc;
715 struct drm_plane *plane;
716 struct drm_connector **connector_set = NULL, *connector;
717 struct drm_framebuffer *fb = NULL;
718 struct drm_display_mode *mode = NULL;
719 struct drm_mode_set set;
720 uint32_t __user *set_connectors_ptr;
721 struct drm_modeset_acquire_ctx ctx;
722 int ret, i, num_connectors = 0;
723
724 if (!drm_core_check_feature(dev, DRIVER_MODESET))
725 return -EOPNOTSUPP;
726
727 /*
728 * Universal plane src offsets are only 16.16, prevent havoc for
729 * drivers using universal plane code internally.
730 */
731 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
732 return -ERANGE;
733
734 crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
735 if (!crtc) {
736 drm_dbg_kms(dev, "Unknown CRTC ID %d\n", crtc_req->crtc_id);
737 return -ENOENT;
738 }
739 drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
740
741 plane = crtc->primary;
742
743 /* allow disabling with the primary plane leased */
744 if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
745 return -EACCES;
746
747 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
748 DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
749
750 if (crtc_req->mode_valid) {
751 /* If we have a mode we need a framebuffer. */
752 /* If we pass -1, set the mode with the currently bound fb */
753 if (crtc_req->fb_id == -1) {
754 struct drm_framebuffer *old_fb;
755
756 if (plane->state)
757 old_fb = plane->state->fb;
758 else
759 old_fb = plane->fb;
760
761 if (!old_fb) {
762 drm_dbg_kms(dev, "CRTC doesn't have current FB\n");
763 ret = -EINVAL;
764 goto out;
765 }
766
767 fb = old_fb;
768 /* Make refcounting symmetric with the lookup path. */
769 drm_framebuffer_get(fb);
770 } else {
771 fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
772 if (!fb) {
773 drm_dbg_kms(dev, "Unknown FB ID%d\n",
774 crtc_req->fb_id);
775 ret = -ENOENT;
776 goto out;
777 }
778 }
779
780 mode = drm_mode_create(dev);
781 if (!mode) {
782 ret = -ENOMEM;
783 goto out;
784 }
785 if (!file_priv->aspect_ratio_allowed &&
786 (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
787 drm_dbg_kms(dev, "Unexpected aspect-ratio flag bits\n");
788 ret = -EINVAL;
789 goto out;
790 }
791
792
793 ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
794 if (ret) {
795 drm_dbg_kms(dev, "Invalid mode (%s, %pe): " DRM_MODE_FMT "\n",
796 drm_get_mode_status_name(mode->status),
797 ERR_PTR(ret), DRM_MODE_ARG(mode));
798 goto out;
799 }
800
801 /*
802 * Check whether the primary plane supports the fb pixel format.
803 * Drivers not implementing the universal planes API use a
804 * default formats list provided by the DRM core which doesn't
805 * match real hardware capabilities. Skip the check in that
806 * case.
807 */
808 if (!plane->format_default) {
809 if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
810 drm_dbg_kms(dev, "Invalid pixel format %p4cc, modifier 0x%llx\n",
811 &fb->format->format, fb->modifier);
812 ret = -EINVAL;
813 goto out;
814 }
815 }
816
817 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
818 mode, fb);
819 if (ret)
820 goto out;
821
822 }
823
824 if (crtc_req->count_connectors == 0 && mode) {
825 drm_dbg_kms(dev, "Count connectors is 0 but mode set\n");
826 ret = -EINVAL;
827 goto out;
828 }
829
830 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
831 drm_dbg_kms(dev, "Count connectors is %d but no mode or fb set\n",
832 crtc_req->count_connectors);
833 ret = -EINVAL;
834 goto out;
835 }
836
837 if (crtc_req->count_connectors > 0) {
838 u32 out_id;
839
840 /* Avoid unbounded kernel memory allocation */
841 if (crtc_req->count_connectors > config->num_connector) {
842 ret = -EINVAL;
843 goto out;
844 }
845
846 connector_set = kmalloc_objs(struct drm_connector *,
847 crtc_req->count_connectors);
848 if (!connector_set) {
849 ret = -ENOMEM;
850 goto out;
851 }
852
853 for (i = 0; i < crtc_req->count_connectors; i++) {
854 connector_set[i] = NULL;
855 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
856 if (get_user(out_id, &set_connectors_ptr[i])) {
857 ret = -EFAULT;
858 goto out;
859 }
860
861 connector = drm_connector_lookup(dev, file_priv, out_id);
862 if (!connector) {
863 drm_dbg_kms(dev, "Connector id %d unknown\n",
864 out_id);
865 ret = -ENOENT;
866 goto out;
867 }
868 drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n",
869 connector->base.id, connector->name);
870
871 connector_set[i] = connector;
872 num_connectors++;
873 }
874 }
875
876 set.crtc = crtc;
877 set.x = crtc_req->x;
878 set.y = crtc_req->y;
879 set.mode = mode;
880 set.connectors = connector_set;
881 set.num_connectors = num_connectors;
882 set.fb = fb;
883
884 if (drm_drv_uses_atomic_modeset(dev))
885 ret = crtc->funcs->set_config(&set, &ctx);
886 else
887 ret = __drm_mode_set_config_internal(&set, &ctx);
888
889out:
890 if (fb)
891 drm_framebuffer_put(fb);
892
893 if (connector_set) {
894 for (i = 0; i < num_connectors; i++) {
895 if (connector_set[i])
896 drm_connector_put(connector_set[i]);
897 }
898 }
899 kfree(connector_set);
900 drm_mode_destroy(dev, mode);
901
902 /* In case we need to retry... */
903 connector_set = NULL;
904 fb = NULL;
905 mode = NULL;
906 num_connectors = 0;
907
908 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
909
910 return ret;
911}
912
913int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
914 struct drm_property *property,
915 uint64_t value)
916{
917 int ret = -EINVAL;
918 struct drm_crtc *crtc = obj_to_crtc(obj);
919
920 if (crtc->funcs->set_property)
921 ret = crtc->funcs->set_property(crtc, property, value);
922 if (!ret)
923 drm_object_property_set_value(obj, property, value);
924
925 return ret;
926}
927
928/**
929 * drm_crtc_create_scaling_filter_property - create a new scaling filter
930 * property
931 *
932 * @crtc: drm CRTC
933 * @supported_filters: bitmask of supported scaling filters, must include
934 * BIT(DRM_SCALING_FILTER_DEFAULT).
935 *
936 * This function lets driver to enable the scaling filter property on a given
937 * CRTC.
938 *
939 * RETURNS:
940 * Zero for success or -errno
941 */
942int drm_crtc_create_scaling_filter_property(struct drm_crtc *crtc,
943 unsigned int supported_filters)
944{
945 struct drm_property *prop =
946 drm_create_scaling_filter_prop(crtc->dev, supported_filters);
947
948 if (IS_ERR(prop))
949 return PTR_ERR(prop);
950
951 drm_object_attach_property(&crtc->base, prop,
952 DRM_SCALING_FILTER_DEFAULT);
953 crtc->scaling_filter_property = prop;
954
955 return 0;
956}
957EXPORT_SYMBOL(drm_crtc_create_scaling_filter_property);
958
959int drm_crtc_create_sharpness_strength_property(struct drm_crtc *crtc)
960{
961 struct drm_device *dev = crtc->dev;
962 struct drm_property *prop =
963 drm_property_create_range(dev, 0, "SHARPNESS_STRENGTH", 0, 255);
964
965 if (!prop)
966 return -ENOMEM;
967
968 crtc->sharpness_strength_property = prop;
969 drm_object_attach_property(&crtc->base, prop, 0);
970
971 return 0;
972}
973EXPORT_SYMBOL(drm_crtc_create_sharpness_strength_property);
974
975/**
976 * drm_crtc_in_clone_mode - check if the given CRTC state is in clone mode
977 *
978 * @crtc_state: CRTC state to check
979 *
980 * This function determines if the given CRTC state is being cloned by multiple
981 * encoders.
982 *
983 * RETURNS:
984 * True if the CRTC state is in clone mode. False otherwise
985 */
986bool drm_crtc_in_clone_mode(struct drm_crtc_state *crtc_state)
987{
988 if (!crtc_state)
989 return false;
990
991 return hweight32(crtc_state->encoder_mask) > 1;
992}
993EXPORT_SYMBOL(drm_crtc_in_clone_mode);