2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
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
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
39 #include <linux/slab.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_framebuffer.h>
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
51 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
52 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
61 * drm_crtc_force_disable - Forcibly turn off a CRTC
62 * @crtc: CRTC to turn off
65 * Zero on success, error code on failure.
67 int drm_crtc_force_disable(struct drm_crtc *crtc)
69 struct drm_mode_set set = {
73 return drm_mode_set_config_internal(&set);
75 EXPORT_SYMBOL(drm_crtc_force_disable);
78 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79 * @dev: DRM device whose CRTCs to turn off
81 * Drivers may want to call this on unload to ensure that all displays are
82 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
85 * Zero on success, error code on failure.
87 int drm_crtc_force_disable_all(struct drm_device *dev)
89 struct drm_crtc *crtc;
92 drm_modeset_lock_all(dev);
93 drm_for_each_crtc(crtc, dev)
95 ret = drm_crtc_force_disable(crtc);
100 drm_modeset_unlock_all(dev);
103 EXPORT_SYMBOL(drm_crtc_force_disable_all);
105 DEFINE_WW_CLASS(crtc_ww_class);
107 static unsigned int drm_num_crtcs(struct drm_device *dev)
109 unsigned int num = 0;
110 struct drm_crtc *tmp;
112 drm_for_each_crtc(tmp, dev) {
119 static int drm_crtc_register_all(struct drm_device *dev)
121 struct drm_crtc *crtc;
124 drm_for_each_crtc(crtc, dev) {
125 if (crtc->funcs->late_register)
126 ret = crtc->funcs->late_register(crtc);
134 static void drm_crtc_unregister_all(struct drm_device *dev)
136 struct drm_crtc *crtc;
138 drm_for_each_crtc(crtc, dev) {
139 if (crtc->funcs->early_unregister)
140 crtc->funcs->early_unregister(crtc);
145 * drm_crtc_init_with_planes - Initialise a new CRTC object with
146 * specified primary and cursor planes.
148 * @crtc: CRTC object to init
149 * @primary: Primary plane for CRTC
150 * @cursor: Cursor plane for CRTC
151 * @funcs: callbacks for the new CRTC
152 * @name: printf style format string for the CRTC name, or NULL for default name
154 * Inits a new object created as base part of a driver crtc object. Drivers
155 * should use this function instead of drm_crtc_init(), which is only provided
156 * for backwards compatibility with drivers which do not yet support universal
157 * planes). For really simple hardware which has only 1 plane look at
158 * drm_simple_display_pipe_init() instead.
161 * Zero on success, error code on failure.
163 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
164 struct drm_plane *primary,
165 struct drm_plane *cursor,
166 const struct drm_crtc_funcs *funcs,
167 const char *name, ...)
169 struct drm_mode_config *config = &dev->mode_config;
172 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
173 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
178 INIT_LIST_HEAD(&crtc->commit_list);
179 lockinit(&crtc->commit_lock, "dccl", 0, 0);
181 drm_modeset_lock_init(&crtc->mutex);
182 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
190 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
193 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
197 drm_mode_object_unregister(dev, &crtc->base);
201 crtc->base.properties = &crtc->properties;
203 list_add_tail(&crtc->head, &config->crtc_list);
204 crtc->index = config->num_crtc++;
206 crtc->primary = primary;
207 crtc->cursor = cursor;
209 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
211 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
213 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
214 drm_object_attach_property(&crtc->base, config->prop_active, 0);
215 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
220 EXPORT_SYMBOL(drm_crtc_init_with_planes);
223 * drm_crtc_cleanup - Clean up the core crtc usage
224 * @crtc: CRTC to cleanup
226 * This function cleans up @crtc and removes it from the DRM mode setting
227 * core. Note that the function does *not* free the crtc structure itself,
228 * this is the responsibility of the caller.
230 void drm_crtc_cleanup(struct drm_crtc *crtc)
232 struct drm_device *dev = crtc->dev;
234 /* Note that the crtc_list is considered to be static; should we
235 * remove the drm_crtc at runtime we would have to decrement all
236 * the indices on the drm_crtc after us in the crtc_list.
239 kfree(crtc->gamma_store);
240 crtc->gamma_store = NULL;
242 drm_modeset_lock_fini(&crtc->mutex);
244 drm_mode_object_unregister(dev, &crtc->base);
245 list_del(&crtc->head);
246 dev->mode_config.num_crtc--;
248 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
249 if (crtc->state && crtc->funcs->atomic_destroy_state)
250 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
254 memset(crtc, 0, sizeof(*crtc));
256 EXPORT_SYMBOL(drm_crtc_cleanup);
259 * drm_connector_index - find the index of a registered connector
260 * @connector: connector to find index for
262 * Given a registered connector, return the index of that connector within a DRM
263 * device's list of connectors.
265 unsigned int drm_connector_index(struct drm_connector *connector)
267 unsigned int index = 0;
268 struct drm_connector *tmp;
269 struct drm_mode_config *config = &connector->dev->mode_config;
271 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
273 drm_for_each_connector(tmp, connector->dev) {
274 if (tmp == connector)
282 EXPORT_SYMBOL(drm_connector_index);
284 int drm_modeset_register_all(struct drm_device *dev)
288 ret = drm_plane_register_all(dev);
292 ret = drm_crtc_register_all(dev);
296 ret = drm_encoder_register_all(dev);
300 ret = drm_connector_register_all(dev);
307 drm_encoder_unregister_all(dev);
309 drm_crtc_unregister_all(dev);
311 drm_plane_unregister_all(dev);
316 void drm_modeset_unregister_all(struct drm_device *dev)
318 drm_connector_unregister_all(dev);
319 drm_encoder_unregister_all(dev);
320 drm_crtc_unregister_all(dev);
321 drm_plane_unregister_all(dev);
324 static int drm_mode_create_standard_properties(struct drm_device *dev)
326 struct drm_property *prop;
329 ret = drm_connector_create_standard_properties(dev);
333 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
334 "type", drm_plane_type_enum_list,
335 ARRAY_SIZE(drm_plane_type_enum_list));
338 dev->mode_config.plane_type_property = prop;
340 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
341 "SRC_X", 0, UINT_MAX);
344 dev->mode_config.prop_src_x = prop;
346 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347 "SRC_Y", 0, UINT_MAX);
350 dev->mode_config.prop_src_y = prop;
352 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353 "SRC_W", 0, UINT_MAX);
356 dev->mode_config.prop_src_w = prop;
358 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
359 "SRC_H", 0, UINT_MAX);
362 dev->mode_config.prop_src_h = prop;
364 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
365 "CRTC_X", INT_MIN, INT_MAX);
368 dev->mode_config.prop_crtc_x = prop;
370 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
371 "CRTC_Y", INT_MIN, INT_MAX);
374 dev->mode_config.prop_crtc_y = prop;
376 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
377 "CRTC_W", 0, INT_MAX);
380 dev->mode_config.prop_crtc_w = prop;
382 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383 "CRTC_H", 0, INT_MAX);
386 dev->mode_config.prop_crtc_h = prop;
388 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
389 "FB_ID", DRM_MODE_OBJECT_FB);
392 dev->mode_config.prop_fb_id = prop;
394 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
395 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
398 dev->mode_config.prop_crtc_id = prop;
400 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
404 dev->mode_config.prop_active = prop;
406 prop = drm_property_create(dev,
407 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
411 dev->mode_config.prop_mode_id = prop;
413 prop = drm_property_create(dev,
418 dev->mode_config.degamma_lut_property = prop;
420 prop = drm_property_create_range(dev,
421 DRM_MODE_PROP_IMMUTABLE,
422 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
425 dev->mode_config.degamma_lut_size_property = prop;
427 prop = drm_property_create(dev,
432 dev->mode_config.ctm_property = prop;
434 prop = drm_property_create(dev,
439 dev->mode_config.gamma_lut_property = prop;
441 prop = drm_property_create_range(dev,
442 DRM_MODE_PROP_IMMUTABLE,
443 "GAMMA_LUT_SIZE", 0, UINT_MAX);
446 dev->mode_config.gamma_lut_size_property = prop;
452 * drm_mode_getresources - get graphics configuration
453 * @dev: drm device for the ioctl
454 * @data: data pointer for the ioctl
455 * @file_priv: drm file for the ioctl call
457 * Construct a set of configuration description structures and return
458 * them to the user, including CRTC, connector and framebuffer configuration.
460 * Called by the user via ioctl.
463 * Zero on success, negative errno on failure.
465 int drm_mode_getresources(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
468 struct drm_mode_card_res *card_res = data;
469 struct list_head *lh;
470 struct drm_framebuffer *fb;
471 struct drm_connector *connector;
472 struct drm_crtc *crtc;
473 struct drm_encoder *encoder;
475 int connector_count = 0;
478 int encoder_count = 0;
480 uint32_t __user *fb_id;
481 uint32_t __user *crtc_id;
482 uint32_t __user *connector_id;
483 uint32_t __user *encoder_id;
485 if (!drm_core_check_feature(dev, DRIVER_MODESET))
489 mutex_lock(&file_priv->fbs_lock);
491 * For the non-control nodes we need to limit the list of resources
492 * by IDs in the group list for this node
494 list_for_each(lh, &file_priv->fbs)
497 /* handle this in 4 parts */
499 if (card_res->count_fbs >= fb_count) {
501 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
502 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
503 if (put_user(fb->base.id, fb_id + copied)) {
504 mutex_unlock(&file_priv->fbs_lock);
510 card_res->count_fbs = fb_count;
511 mutex_unlock(&file_priv->fbs_lock);
513 /* mode_config.mutex protects the connector list against e.g. DP MST
514 * connector hot-adding. CRTC/Plane lists are invariant. */
515 mutex_lock(&dev->mode_config.mutex);
516 drm_for_each_crtc(crtc, dev)
519 drm_for_each_connector(connector, dev)
522 drm_for_each_encoder(encoder, dev)
525 card_res->max_height = dev->mode_config.max_height;
526 card_res->min_height = dev->mode_config.min_height;
527 card_res->max_width = dev->mode_config.max_width;
528 card_res->min_width = dev->mode_config.min_width;
531 if (card_res->count_crtcs >= crtc_count) {
533 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
534 drm_for_each_crtc(crtc, dev) {
535 if (put_user(crtc->base.id, crtc_id + copied)) {
542 card_res->count_crtcs = crtc_count;
545 if (card_res->count_encoders >= encoder_count) {
547 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
548 drm_for_each_encoder(encoder, dev) {
549 if (put_user(encoder->base.id, encoder_id +
557 card_res->count_encoders = encoder_count;
560 if (card_res->count_connectors >= connector_count) {
562 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
563 drm_for_each_connector(connector, dev) {
564 if (put_user(connector->base.id,
565 connector_id + copied)) {
572 card_res->count_connectors = connector_count;
575 mutex_unlock(&dev->mode_config.mutex);
580 * drm_mode_getcrtc - get CRTC configuration
581 * @dev: drm device for the ioctl
582 * @data: data pointer for the ioctl
583 * @file_priv: drm file for the ioctl call
585 * Construct a CRTC configuration structure to return to the user.
587 * Called by the user via ioctl.
590 * Zero on success, negative errno on failure.
592 int drm_mode_getcrtc(struct drm_device *dev,
593 void *data, struct drm_file *file_priv)
595 struct drm_mode_crtc *crtc_resp = data;
596 struct drm_crtc *crtc;
598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
601 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
605 drm_modeset_lock_crtc(crtc, crtc->primary);
606 crtc_resp->gamma_size = crtc->gamma_size;
607 if (crtc->primary->fb)
608 crtc_resp->fb_id = crtc->primary->fb->base.id;
610 crtc_resp->fb_id = 0;
613 crtc_resp->x = crtc->primary->state->src_x >> 16;
614 crtc_resp->y = crtc->primary->state->src_y >> 16;
615 if (crtc->state->enable) {
616 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
617 crtc_resp->mode_valid = 1;
620 crtc_resp->mode_valid = 0;
623 crtc_resp->x = crtc->x;
624 crtc_resp->y = crtc->y;
626 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
627 crtc_resp->mode_valid = 1;
630 crtc_resp->mode_valid = 0;
633 drm_modeset_unlock_crtc(crtc);
639 * drm_mode_set_config_internal - helper to call ->set_config
640 * @set: modeset config to set
642 * This is a little helper to wrap internal calls to the ->set_config driver
643 * interface. The only thing it adds is correct refcounting dance.
646 * Zero on success, negative errno on failure.
648 int drm_mode_set_config_internal(struct drm_mode_set *set)
650 struct drm_crtc *crtc = set->crtc;
651 struct drm_framebuffer *fb;
652 struct drm_crtc *tmp;
656 * NOTE: ->set_config can also disable other crtcs (if we steal all
657 * connectors from it), hence we need to refcount the fbs across all
658 * crtcs. Atomic modeset will have saner semantics ...
660 drm_for_each_crtc(tmp, crtc->dev)
661 tmp->primary->old_fb = tmp->primary->fb;
665 ret = crtc->funcs->set_config(set);
667 crtc->primary->crtc = crtc;
668 crtc->primary->fb = fb;
671 drm_for_each_crtc(tmp, crtc->dev) {
672 if (tmp->primary->fb)
673 drm_framebuffer_reference(tmp->primary->fb);
674 if (tmp->primary->old_fb)
675 drm_framebuffer_unreference(tmp->primary->old_fb);
676 tmp->primary->old_fb = NULL;
681 EXPORT_SYMBOL(drm_mode_set_config_internal);
684 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
685 * @mode: mode to query
686 * @hdisplay: hdisplay value to fill in
687 * @vdisplay: vdisplay value to fill in
689 * The vdisplay value will be doubled if the specified mode is a stereo mode of
690 * the appropriate layout.
692 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
693 int *hdisplay, int *vdisplay)
695 struct drm_display_mode adjusted;
697 drm_mode_copy(&adjusted, mode);
698 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
699 *hdisplay = adjusted.crtc_hdisplay;
700 *vdisplay = adjusted.crtc_vdisplay;
702 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
705 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
707 * @crtc: CRTC that framebuffer will be displayed on
710 * @mode: mode that framebuffer will be displayed under
711 * @fb: framebuffer to check size of
713 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
715 const struct drm_display_mode *mode,
716 const struct drm_framebuffer *fb)
719 int hdisplay, vdisplay;
721 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
724 crtc->primary->state->rotation & (DRM_ROTATE_90 |
726 swap(hdisplay, vdisplay);
728 return drm_framebuffer_check_src_coords(x << 16, y << 16,
729 hdisplay << 16, vdisplay << 16,
732 EXPORT_SYMBOL(drm_crtc_check_viewport);
735 * drm_mode_setcrtc - set CRTC configuration
736 * @dev: drm device for the ioctl
737 * @data: data pointer for the ioctl
738 * @file_priv: drm file for the ioctl call
740 * Build a new CRTC configuration based on user request.
742 * Called by the user via ioctl.
745 * Zero on success, negative errno on failure.
747 int drm_mode_setcrtc(struct drm_device *dev, void *data,
748 struct drm_file *file_priv)
750 struct drm_mode_config *config = &dev->mode_config;
751 struct drm_mode_crtc *crtc_req = data;
752 struct drm_crtc *crtc;
753 struct drm_connector **connector_set = NULL, *connector;
754 struct drm_framebuffer *fb = NULL;
755 struct drm_display_mode *mode = NULL;
756 struct drm_mode_set set;
757 uint32_t __user *set_connectors_ptr;
761 if (!drm_core_check_feature(dev, DRIVER_MODESET))
765 * Universal plane src offsets are only 16.16, prevent havoc for
766 * drivers using universal plane code internally.
768 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
771 drm_modeset_lock_all(dev);
772 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
774 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
778 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
780 if (crtc_req->mode_valid) {
781 /* If we have a mode we need a framebuffer. */
782 /* If we pass -1, set the mode with the currently bound fb */
783 if (crtc_req->fb_id == -1) {
784 if (!crtc->primary->fb) {
785 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
789 fb = crtc->primary->fb;
790 /* Make refcounting symmetric with the lookup path. */
791 drm_framebuffer_reference(fb);
793 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
795 DRM_DEBUG_KMS("Unknown FB ID%d\n",
802 mode = drm_mode_create(dev);
808 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
810 DRM_DEBUG_KMS("Invalid mode\n");
815 * Check whether the primary plane supports the fb pixel format.
816 * Drivers not implementing the universal planes API use a
817 * default formats list provided by the DRM core which doesn't
818 * match real hardware capabilities. Skip the check in that
821 if (!crtc->primary->format_default) {
822 ret = drm_plane_check_pixel_format(crtc->primary,
825 char *format_name = drm_get_format_name(fb->pixel_format);
826 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
832 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
839 if (crtc_req->count_connectors == 0 && mode) {
840 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
845 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
846 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
847 crtc_req->count_connectors);
852 if (crtc_req->count_connectors > 0) {
855 /* Avoid unbounded kernel memory allocation */
856 if (crtc_req->count_connectors > config->num_connector) {
861 connector_set = kmalloc_array(crtc_req->count_connectors,
862 sizeof(struct drm_connector *),
864 if (!connector_set) {
869 for (i = 0; i < crtc_req->count_connectors; i++) {
870 connector_set[i] = NULL;
871 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
872 if (get_user(out_id, &set_connectors_ptr[i])) {
877 connector = drm_connector_lookup(dev, out_id);
879 DRM_DEBUG_KMS("Connector id %d unknown\n",
884 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
888 connector_set[i] = connector;
896 set.connectors = connector_set;
897 set.num_connectors = crtc_req->count_connectors;
899 ret = drm_mode_set_config_internal(&set);
903 drm_framebuffer_unreference(fb);
906 for (i = 0; i < crtc_req->count_connectors; i++) {
907 if (connector_set[i])
908 drm_connector_unreference(connector_set[i]);
911 kfree(connector_set);
912 drm_mode_destroy(dev, mode);
913 drm_modeset_unlock_all(dev);
917 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
918 struct drm_property *property,
922 struct drm_crtc *crtc = obj_to_crtc(obj);
924 if (crtc->funcs->set_property)
925 ret = crtc->funcs->set_property(crtc, property, value);
927 drm_object_property_set_value(obj, property, value);
933 * drm_mode_config_reset - call ->reset callbacks
936 * This functions calls all the crtc's, encoder's and connector's ->reset
937 * callback. Drivers can use this in e.g. their driver load or resume code to
938 * reset hardware and software state.
940 void drm_mode_config_reset(struct drm_device *dev)
942 struct drm_crtc *crtc;
943 struct drm_plane *plane;
944 struct drm_encoder *encoder;
945 struct drm_connector *connector;
947 drm_for_each_plane(plane, dev)
948 if (plane->funcs->reset)
949 plane->funcs->reset(plane);
951 drm_for_each_crtc(crtc, dev)
952 if (crtc->funcs->reset)
953 crtc->funcs->reset(crtc);
955 drm_for_each_encoder(encoder, dev)
956 if (encoder->funcs->reset)
957 encoder->funcs->reset(encoder);
959 mutex_lock(&dev->mode_config.mutex);
960 drm_for_each_connector(connector, dev)
961 if (connector->funcs->reset)
962 connector->funcs->reset(connector);
963 mutex_unlock(&dev->mode_config.mutex);
965 EXPORT_SYMBOL(drm_mode_config_reset);
968 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
971 * @file_priv: DRM file info
973 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
974 * TTM or something else entirely) and returns the resulting buffer handle. This
975 * handle can then be wrapped up into a framebuffer modeset object.
977 * Note that userspace is not allowed to use such objects for render
978 * acceleration - drivers must create their own private ioctls for such a use
981 * Called by the user via ioctl.
984 * Zero on success, negative errno on failure.
986 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
987 void *data, struct drm_file *file_priv)
989 struct drm_mode_create_dumb *args = data;
990 u32 cpp, stride, size;
992 if (!dev->driver->dumb_create)
994 if (!args->width || !args->height || !args->bpp)
997 /* overflow checks for 32bit size calculations */
998 /* NOTE: DIV_ROUND_UP() can overflow */
999 cpp = DIV_ROUND_UP(args->bpp, 8);
1000 if (!cpp || cpp > 0xffffffffU / args->width)
1002 stride = cpp * args->width;
1003 if (args->height > 0xffffffffU / stride)
1006 /* test for wrap-around */
1007 size = args->height * stride;
1008 if (PAGE_ALIGN(size) == 0)
1012 * handle, pitch and size are output parameters. Zero them out to
1013 * prevent drivers from accidentally using uninitialized data. Since
1014 * not all existing userspace is clearing these fields properly we
1015 * cannot reject IOCTL with garbage in them.
1021 return dev->driver->dumb_create(file_priv, dev, args);
1025 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1028 * @file_priv: DRM file info
1030 * Allocate an offset in the drm device node's address space to be able to
1031 * memory map a dumb buffer.
1033 * Called by the user via ioctl.
1036 * Zero on success, negative errno on failure.
1038 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1039 void *data, struct drm_file *file_priv)
1041 struct drm_mode_map_dumb *args = data;
1043 /* call driver ioctl to get mmap offset */
1044 if (!dev->driver->dumb_map_offset)
1047 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, (uint64_t *)&args->offset);
1051 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1054 * @file_priv: DRM file info
1056 * This destroys the userspace handle for the given dumb backing storage buffer.
1057 * Since buffer objects must be reference counted in the kernel a buffer object
1058 * won't be immediately freed if a framebuffer modeset object still uses it.
1060 * Called by the user via ioctl.
1063 * Zero on success, negative errno on failure.
1065 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1066 void *data, struct drm_file *file_priv)
1068 struct drm_mode_destroy_dumb *args = data;
1070 if (!dev->driver->dumb_destroy)
1073 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1077 * drm_mode_config_init - initialize DRM mode_configuration structure
1080 * Initialize @dev's mode_config structure, used for tracking the graphics
1081 * configuration of @dev.
1083 * Since this initializes the modeset locks, no locking is possible. Which is no
1084 * problem, since this should happen single threaded at init time. It is the
1085 * driver's problem to ensure this guarantee.
1088 void drm_mode_config_init(struct drm_device *dev)
1090 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
1091 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
1092 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
1093 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
1094 lockinit(&dev->mode_config.blob_lock, "drmcbl", 0, LK_CANRECURSE);
1095 INIT_LIST_HEAD(&dev->mode_config.fb_list);
1096 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1097 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1098 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1099 INIT_LIST_HEAD(&dev->mode_config.property_list);
1100 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1101 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1102 idr_init(&dev->mode_config.crtc_idr);
1103 idr_init(&dev->mode_config.tile_idr);
1104 ida_init(&dev->mode_config.connector_ida);
1106 drm_modeset_lock_all(dev);
1107 drm_mode_create_standard_properties(dev);
1108 drm_modeset_unlock_all(dev);
1110 /* Just to be sure */
1111 dev->mode_config.num_fb = 0;
1112 dev->mode_config.num_connector = 0;
1113 dev->mode_config.num_crtc = 0;
1114 dev->mode_config.num_encoder = 0;
1115 dev->mode_config.num_overlay_plane = 0;
1116 dev->mode_config.num_total_plane = 0;
1118 EXPORT_SYMBOL(drm_mode_config_init);
1121 * drm_mode_config_cleanup - free up DRM mode_config info
1124 * Free up all the connectors and CRTCs associated with this DRM device, then
1125 * free up the framebuffers and associated buffer objects.
1127 * Note that since this /should/ happen single-threaded at driver/device
1128 * teardown time, no locking is required. It's the driver's job to ensure that
1129 * this guarantee actually holds true.
1131 * FIXME: cleanup any dangling user buffer objects too
1133 void drm_mode_config_cleanup(struct drm_device *dev)
1135 struct drm_connector *connector, *ot;
1136 struct drm_crtc *crtc, *ct;
1137 struct drm_encoder *encoder, *enct;
1138 struct drm_framebuffer *fb, *fbt;
1139 struct drm_property *property, *pt;
1140 struct drm_property_blob *blob, *bt;
1141 struct drm_plane *plane, *plt;
1143 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1145 encoder->funcs->destroy(encoder);
1148 list_for_each_entry_safe(connector, ot,
1149 &dev->mode_config.connector_list, head) {
1150 connector->funcs->destroy(connector);
1153 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1155 drm_property_destroy(dev, property);
1158 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1160 plane->funcs->destroy(plane);
1163 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1164 crtc->funcs->destroy(crtc);
1167 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1169 drm_property_unreference_blob(blob);
1173 * Single-threaded teardown context, so it's not required to grab the
1174 * fb_lock to protect against concurrent fb_list access. Contrary, it
1175 * would actually deadlock with the drm_framebuffer_cleanup function.
1177 * Also, if there are any framebuffers left, that's a driver leak now,
1178 * so politely WARN about this.
1180 WARN_ON(!list_empty(&dev->mode_config.fb_list));
1181 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1182 drm_framebuffer_free(&fb->base.refcount);
1185 ida_destroy(&dev->mode_config.connector_ida);
1186 idr_destroy(&dev->mode_config.tile_idr);
1187 idr_destroy(&dev->mode_config.crtc_idr);
1188 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
1190 EXPORT_SYMBOL(drm_mode_config_cleanup);
1195 * Tile groups are used to represent tiled monitors with a unique
1196 * integer identifier. Tiled monitors using DisplayID v1.3 have
1197 * a unique 8-byte handle, we store this in a tile group, so we
1198 * have a common identifier for all tiles in a monitor group.
1200 static void drm_tile_group_free(struct kref *kref)
1202 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1203 struct drm_device *dev = tg->dev;
1204 mutex_lock(&dev->mode_config.idr_mutex);
1205 idr_remove(&dev->mode_config.tile_idr, tg->id);
1206 mutex_unlock(&dev->mode_config.idr_mutex);
1211 * drm_mode_put_tile_group - drop a reference to a tile group.
1213 * @tg: tile group to drop reference to.
1215 * drop reference to tile group and free if 0.
1217 void drm_mode_put_tile_group(struct drm_device *dev,
1218 struct drm_tile_group *tg)
1220 kref_put(&tg->refcount, drm_tile_group_free);
1224 * drm_mode_get_tile_group - get a reference to an existing tile group
1226 * @topology: 8-bytes unique per monitor.
1228 * Use the unique bytes to get a reference to an existing tile group.
1231 * tile group or NULL if not found.
1233 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1236 struct drm_tile_group *tg;
1238 mutex_lock(&dev->mode_config.idr_mutex);
1239 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1240 if (!memcmp(tg->group_data, topology, 8)) {
1241 if (!kref_get_unless_zero(&tg->refcount))
1243 mutex_unlock(&dev->mode_config.idr_mutex);
1247 mutex_unlock(&dev->mode_config.idr_mutex);
1250 EXPORT_SYMBOL(drm_mode_get_tile_group);
1253 * drm_mode_create_tile_group - create a tile group from a displayid description
1255 * @topology: 8-bytes unique per monitor.
1257 * Create a tile group for the unique monitor, and get a unique
1258 * identifier for the tile group.
1261 * new tile group or error.
1263 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1266 struct drm_tile_group *tg;
1269 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1271 return ERR_PTR(-ENOMEM);
1273 kref_init(&tg->refcount);
1274 memcpy(tg->group_data, topology, 8);
1277 mutex_lock(&dev->mode_config.idr_mutex);
1278 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1286 mutex_unlock(&dev->mode_config.idr_mutex);
1289 EXPORT_SYMBOL(drm_mode_create_tile_group);