2 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
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.
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
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_crtc.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_modeset_lock.h>
42 #include <drm/drm_atomic.h>
43 #include <drm/drm_auth.h>
44 #include <drm/drm_debugfs_crc.h>
45 #include <drm/drm_drv.h>
46 #include <drm/drm_print.h>
47 #include <drm/drm_file.h>
49 #include "drm_crtc_internal.h"
50 #include "drm_internal.h"
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
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().
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.
74 * drm_crtc_from_index - find the registered CRTC at an index
76 * @idx: index of registered CRTC to find for
78 * Given a CRTC index, return the registered CRTC from DRM device's
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."
84 struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
86 struct drm_crtc *crtc;
88 drm_for_each_crtc(crtc, dev)
89 if (idx == crtc->index)
94 EXPORT_SYMBOL(drm_crtc_from_index);
97 * drm_crtc_force_disable - Forcibly turn off a CRTC
98 * @crtc: CRTC to turn off
100 * Note: This should only be used by non-atomic legacy drivers.
103 * Zero on success, error code on failure.
105 int drm_crtc_force_disable(struct drm_crtc *crtc)
107 struct drm_mode_set set = {
111 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
113 return drm_mode_set_config_internal(&set);
115 EXPORT_SYMBOL(drm_crtc_force_disable);
118 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
119 * @dev: DRM device whose CRTCs to turn off
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.
124 * Note: This should only be used by non-atomic legacy drivers. For an atomic
125 * version look at drm_atomic_helper_shutdown().
128 * Zero on success, error code on failure.
130 int drm_crtc_force_disable_all(struct drm_device *dev)
132 struct drm_crtc *crtc;
135 drm_modeset_lock_all(dev);
136 drm_for_each_crtc(crtc, dev)
138 ret = drm_crtc_force_disable(crtc);
143 drm_modeset_unlock_all(dev);
146 EXPORT_SYMBOL(drm_crtc_force_disable_all);
148 static unsigned int drm_num_crtcs(struct drm_device *dev)
150 unsigned int num = 0;
151 struct drm_crtc *tmp;
153 drm_for_each_crtc(tmp, dev) {
160 int drm_crtc_register_all(struct drm_device *dev)
162 struct drm_crtc *crtc;
165 drm_for_each_crtc(crtc, dev) {
166 if (drm_debugfs_crtc_add(crtc))
167 DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
170 if (crtc->funcs->late_register)
171 ret = crtc->funcs->late_register(crtc);
179 void drm_crtc_unregister_all(struct drm_device *dev)
181 struct drm_crtc *crtc;
183 drm_for_each_crtc(crtc, dev) {
184 if (crtc->funcs->early_unregister)
185 crtc->funcs->early_unregister(crtc);
186 drm_debugfs_crtc_remove(crtc);
190 static int drm_crtc_crc_init(struct drm_crtc *crtc)
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)
202 static void drm_crtc_crc_fini(struct drm_crtc *crtc)
204 #ifdef CONFIG_DEBUG_FS
205 kfree(crtc->crc.source);
209 static const struct dma_fence_ops drm_crtc_fence_ops;
211 static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
213 BUG_ON(fence->ops != &drm_crtc_fence_ops);
214 return container_of(fence->lock, struct drm_crtc, fence_lock);
217 static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
219 struct drm_crtc *crtc = fence_to_crtc(fence);
221 return crtc->dev->driver->name;
224 static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
226 struct drm_crtc *crtc = fence_to_crtc(fence);
228 return crtc->timeline_name;
231 static const struct dma_fence_ops drm_crtc_fence_ops = {
232 .get_driver_name = drm_crtc_fence_get_driver_name,
233 .get_timeline_name = drm_crtc_fence_get_timeline_name,
236 struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
238 struct dma_fence *fence;
240 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
244 dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
245 crtc->fence_context, ++crtc->fence_seqno);
251 * drm_crtc_init_with_planes - Initialise a new CRTC object with
252 * specified primary and cursor planes.
254 * @crtc: CRTC object to init
255 * @primary: Primary plane for CRTC
256 * @cursor: Cursor plane for CRTC
257 * @funcs: callbacks for the new CRTC
258 * @name: printf style format string for the CRTC name, or NULL for default name
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.
267 * Zero on success, error code on failure.
269 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
270 struct drm_plane *primary,
271 struct drm_plane *cursor,
272 const struct drm_crtc_funcs *funcs,
273 const char *name, ...)
275 struct drm_mode_config *config = &dev->mode_config;
278 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
279 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
281 /* crtc index is used with 32bit bitmasks */
282 if (WARN_ON(config->num_crtc >= 32))
285 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
286 (!funcs->atomic_destroy_state ||
287 !funcs->atomic_duplicate_state));
292 INIT_LIST_HEAD(&crtc->commit_list);
293 spin_lock_init(&crtc->commit_lock);
295 drm_modeset_lock_init(&crtc->mutex);
296 ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
304 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
307 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
311 drm_mode_object_unregister(dev, &crtc->base);
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);
320 crtc->base.properties = &crtc->properties;
322 list_add_tail(&crtc->head, &config->crtc_list);
323 crtc->index = config->num_crtc++;
325 crtc->primary = primary;
326 crtc->cursor = cursor;
327 if (primary && !primary->possible_crtcs)
328 primary->possible_crtcs = drm_crtc_mask(crtc);
329 if (cursor && !cursor->possible_crtcs)
330 cursor->possible_crtcs = drm_crtc_mask(crtc);
332 ret = drm_crtc_crc_init(crtc);
334 drm_mode_object_unregister(dev, &crtc->base);
338 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
339 drm_object_attach_property(&crtc->base, config->prop_active, 0);
340 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
341 drm_object_attach_property(&crtc->base,
342 config->prop_out_fence_ptr, 0);
347 EXPORT_SYMBOL(drm_crtc_init_with_planes);
350 * drm_crtc_cleanup - Clean up the core crtc usage
351 * @crtc: CRTC to cleanup
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.
357 void drm_crtc_cleanup(struct drm_crtc *crtc)
359 struct drm_device *dev = crtc->dev;
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.
366 drm_crtc_crc_fini(crtc);
368 kfree(crtc->gamma_store);
369 crtc->gamma_store = NULL;
371 drm_modeset_lock_fini(&crtc->mutex);
373 drm_mode_object_unregister(dev, &crtc->base);
374 list_del(&crtc->head);
375 dev->mode_config.num_crtc--;
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);
383 memset(crtc, 0, sizeof(*crtc));
385 EXPORT_SYMBOL(drm_crtc_cleanup);
388 * drm_mode_getcrtc - get CRTC configuration
389 * @dev: drm device for the ioctl
390 * @data: data pointer for the ioctl
391 * @file_priv: drm file for the ioctl call
393 * Construct a CRTC configuration structure to return to the user.
395 * Called by the user via ioctl.
398 * Zero on success, negative errno on failure.
400 int drm_mode_getcrtc(struct drm_device *dev,
401 void *data, struct drm_file *file_priv)
403 struct drm_mode_crtc *crtc_resp = data;
404 struct drm_crtc *crtc;
405 struct drm_plane *plane;
407 if (!drm_core_check_feature(dev, DRIVER_MODESET))
410 crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
414 plane = crtc->primary;
416 crtc_resp->gamma_size = crtc->gamma_size;
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;
424 crtc_resp->fb_id = 0;
427 crtc_resp->x = plane->state->src_x >> 16;
428 crtc_resp->y = plane->state->src_y >> 16;
430 drm_modeset_unlock(&plane->mutex);
432 drm_modeset_lock(&crtc->mutex, NULL);
434 if (crtc->state->enable) {
435 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
436 crtc_resp->mode_valid = 1;
438 crtc_resp->mode_valid = 0;
441 crtc_resp->x = crtc->x;
442 crtc_resp->y = crtc->y;
445 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
446 crtc_resp->mode_valid = 1;
449 crtc_resp->mode_valid = 0;
452 if (!file_priv->aspect_ratio_allowed)
453 crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
454 drm_modeset_unlock(&crtc->mutex);
459 static int __drm_mode_set_config_internal(struct drm_mode_set *set,
460 struct drm_modeset_acquire_ctx *ctx)
462 struct drm_crtc *crtc = set->crtc;
463 struct drm_framebuffer *fb;
464 struct drm_crtc *tmp;
467 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
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 ...
474 drm_for_each_crtc(tmp, crtc->dev) {
475 struct drm_plane *plane = tmp->primary;
477 plane->old_fb = plane->fb;
482 ret = crtc->funcs->set_config(set, ctx);
484 struct drm_plane *plane = crtc->primary;
486 plane->crtc = fb ? crtc : NULL;
490 drm_for_each_crtc(tmp, crtc->dev) {
491 struct drm_plane *plane = tmp->primary;
494 drm_framebuffer_get(plane->fb);
496 drm_framebuffer_put(plane->old_fb);
497 plane->old_fb = NULL;
504 * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
505 * @set: modeset config to set
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.
511 * This should only be used by non-atomic legacy drivers.
514 * Zero on success, negative errno on failure.
516 int drm_mode_set_config_internal(struct drm_mode_set *set)
518 WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
520 return __drm_mode_set_config_internal(set, NULL);
522 EXPORT_SYMBOL(drm_mode_set_config_internal);
525 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
527 * @crtc: CRTC that framebuffer will be displayed on
530 * @mode: mode that framebuffer will be displayed under
531 * @fb: framebuffer to check size of
533 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
535 const struct drm_display_mode *mode,
536 const struct drm_framebuffer *fb)
539 int hdisplay, vdisplay;
541 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
544 drm_rotation_90_or_270(crtc->primary->state->rotation))
545 swap(hdisplay, vdisplay);
547 return drm_framebuffer_check_src_coords(x << 16, y << 16,
548 hdisplay << 16, vdisplay << 16,
551 EXPORT_SYMBOL(drm_crtc_check_viewport);
554 * drm_mode_setcrtc - set CRTC configuration
555 * @dev: drm device for the ioctl
556 * @data: data pointer for the ioctl
557 * @file_priv: drm file for the ioctl call
559 * Build a new CRTC configuration based on user request.
561 * Called by the user via ioctl.
564 * Zero on success, negative errno on failure.
566 int drm_mode_setcrtc(struct drm_device *dev, void *data,
567 struct drm_file *file_priv)
569 struct drm_mode_config *config = &dev->mode_config;
570 struct drm_mode_crtc *crtc_req = data;
571 struct drm_crtc *crtc;
572 struct drm_plane *plane;
573 struct drm_connector **connector_set, *connector;
574 struct drm_framebuffer *fb;
575 struct drm_display_mode *mode;
576 struct drm_mode_set set;
577 uint32_t __user *set_connectors_ptr;
578 struct drm_modeset_acquire_ctx ctx;
582 if (!drm_core_check_feature(dev, DRIVER_MODESET))
586 * Universal plane src offsets are only 16.16, prevent havoc for
587 * drivers using universal plane code internally.
589 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
592 crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
594 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
597 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
599 plane = crtc->primary;
601 mutex_lock(&crtc->dev->mode_config.mutex);
602 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
604 connector_set = NULL;
608 ret = drm_modeset_lock_all_ctx(crtc->dev, &ctx);
612 if (crtc_req->mode_valid) {
613 /* If we have a mode we need a framebuffer. */
614 /* If we pass -1, set the mode with the currently bound fb */
615 if (crtc_req->fb_id == -1) {
616 struct drm_framebuffer *old_fb;
619 old_fb = plane->state->fb;
624 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
630 /* Make refcounting symmetric with the lookup path. */
631 drm_framebuffer_get(fb);
633 fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
635 DRM_DEBUG_KMS("Unknown FB ID%d\n",
642 mode = drm_mode_create(dev);
647 if (!file_priv->aspect_ratio_allowed &&
648 (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
649 DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
655 ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
657 DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
658 ret, drm_get_mode_status_name(mode->status));
659 drm_mode_debug_printmodeline(mode);
664 * Check whether the primary plane supports the fb pixel format.
665 * Drivers not implementing the universal planes API use a
666 * default formats list provided by the DRM core which doesn't
667 * match real hardware capabilities. Skip the check in that
670 if (!plane->format_default) {
671 ret = drm_plane_check_pixel_format(plane,
675 struct drm_format_name_buf format_name;
676 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
677 drm_get_format_name(fb->format->format,
684 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
691 if (crtc_req->count_connectors == 0 && mode) {
692 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
697 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
698 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
699 crtc_req->count_connectors);
704 if (crtc_req->count_connectors > 0) {
707 /* Avoid unbounded kernel memory allocation */
708 if (crtc_req->count_connectors > config->num_connector) {
713 connector_set = kmalloc_array(crtc_req->count_connectors,
714 sizeof(struct drm_connector *),
716 if (!connector_set) {
721 for (i = 0; i < crtc_req->count_connectors; i++) {
722 connector_set[i] = NULL;
723 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
724 if (get_user(out_id, &set_connectors_ptr[i])) {
729 connector = drm_connector_lookup(dev, file_priv, out_id);
731 DRM_DEBUG_KMS("Connector id %d unknown\n",
736 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
740 connector_set[i] = connector;
748 set.connectors = connector_set;
749 set.num_connectors = crtc_req->count_connectors;
752 if (drm_drv_uses_atomic_modeset(dev))
753 ret = crtc->funcs->set_config(&set, &ctx);
755 ret = __drm_mode_set_config_internal(&set, &ctx);
759 drm_framebuffer_put(fb);
762 for (i = 0; i < crtc_req->count_connectors; i++) {
763 if (connector_set[i])
764 drm_connector_put(connector_set[i]);
767 kfree(connector_set);
768 drm_mode_destroy(dev, mode);
769 if (ret == -EDEADLK) {
770 ret = drm_modeset_backoff(&ctx);
774 drm_modeset_drop_locks(&ctx);
775 drm_modeset_acquire_fini(&ctx);
776 mutex_unlock(&crtc->dev->mode_config.mutex);
781 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
782 struct drm_property *property,
786 struct drm_crtc *crtc = obj_to_crtc(obj);
788 if (crtc->funcs->set_property)
789 ret = crtc->funcs->set_property(crtc, property, value);
791 drm_object_property_set_value(obj, property, value);