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>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_debugfs_crc.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 (drm_debugfs_crtc_add(crtc))
126 DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
129 if (crtc->funcs->late_register)
130 ret = crtc->funcs->late_register(crtc);
138 static void drm_crtc_unregister_all(struct drm_device *dev)
140 struct drm_crtc *crtc;
142 drm_for_each_crtc(crtc, dev) {
143 if (crtc->funcs->early_unregister)
144 crtc->funcs->early_unregister(crtc);
145 drm_debugfs_crtc_remove(crtc);
149 static int drm_crtc_crc_init(struct drm_crtc *crtc)
151 #ifdef CONFIG_DEBUG_FS
152 spin_lock_init(&crtc->crc.lock);
153 init_waitqueue_head(&crtc->crc.wq);
154 crtc->crc.source = kstrdup("auto", GFP_KERNEL);
155 if (!crtc->crc.source)
161 static void drm_crtc_crc_fini(struct drm_crtc *crtc)
163 #ifdef CONFIG_DEBUG_FS
164 kfree(crtc->crc.source);
169 * drm_crtc_init_with_planes - Initialise a new CRTC object with
170 * specified primary and cursor planes.
172 * @crtc: CRTC object to init
173 * @primary: Primary plane for CRTC
174 * @cursor: Cursor plane for CRTC
175 * @funcs: callbacks for the new CRTC
176 * @name: printf style format string for the CRTC name, or NULL for default name
178 * Inits a new object created as base part of a driver crtc object. Drivers
179 * should use this function instead of drm_crtc_init(), which is only provided
180 * for backwards compatibility with drivers which do not yet support universal
181 * planes). For really simple hardware which has only 1 plane look at
182 * drm_simple_display_pipe_init() instead.
185 * Zero on success, error code on failure.
187 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
188 struct drm_plane *primary,
189 struct drm_plane *cursor,
190 const struct drm_crtc_funcs *funcs,
191 const char *name, ...)
193 struct drm_mode_config *config = &dev->mode_config;
196 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
197 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
202 INIT_LIST_HEAD(&crtc->commit_list);
203 spin_lock_init(&crtc->commit_lock);
205 drm_modeset_lock_init(&crtc->mutex);
206 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
214 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
217 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
221 drm_mode_object_unregister(dev, &crtc->base);
225 crtc->base.properties = &crtc->properties;
227 list_add_tail(&crtc->head, &config->crtc_list);
228 crtc->index = config->num_crtc++;
230 crtc->primary = primary;
231 crtc->cursor = cursor;
233 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
235 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
237 ret = drm_crtc_crc_init(crtc);
239 drm_mode_object_unregister(dev, &crtc->base);
243 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
244 drm_object_attach_property(&crtc->base, config->prop_active, 0);
245 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
250 EXPORT_SYMBOL(drm_crtc_init_with_planes);
253 * drm_crtc_cleanup - Clean up the core crtc usage
254 * @crtc: CRTC to cleanup
256 * This function cleans up @crtc and removes it from the DRM mode setting
257 * core. Note that the function does *not* free the crtc structure itself,
258 * this is the responsibility of the caller.
260 void drm_crtc_cleanup(struct drm_crtc *crtc)
262 struct drm_device *dev = crtc->dev;
264 /* Note that the crtc_list is considered to be static; should we
265 * remove the drm_crtc at runtime we would have to decrement all
266 * the indices on the drm_crtc after us in the crtc_list.
269 drm_crtc_crc_fini(crtc);
271 kfree(crtc->gamma_store);
272 crtc->gamma_store = NULL;
274 drm_modeset_lock_fini(&crtc->mutex);
276 drm_mode_object_unregister(dev, &crtc->base);
277 list_del(&crtc->head);
278 dev->mode_config.num_crtc--;
280 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
281 if (crtc->state && crtc->funcs->atomic_destroy_state)
282 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
286 memset(crtc, 0, sizeof(*crtc));
288 EXPORT_SYMBOL(drm_crtc_cleanup);
290 int drm_modeset_register_all(struct drm_device *dev)
294 ret = drm_plane_register_all(dev);
298 ret = drm_crtc_register_all(dev);
302 ret = drm_encoder_register_all(dev);
306 ret = drm_connector_register_all(dev);
313 drm_encoder_unregister_all(dev);
315 drm_crtc_unregister_all(dev);
317 drm_plane_unregister_all(dev);
322 void drm_modeset_unregister_all(struct drm_device *dev)
324 drm_connector_unregister_all(dev);
325 drm_encoder_unregister_all(dev);
326 drm_crtc_unregister_all(dev);
327 drm_plane_unregister_all(dev);
330 static int drm_mode_create_standard_properties(struct drm_device *dev)
332 struct drm_property *prop;
335 ret = drm_connector_create_standard_properties(dev);
339 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
340 "type", drm_plane_type_enum_list,
341 ARRAY_SIZE(drm_plane_type_enum_list));
344 dev->mode_config.plane_type_property = prop;
346 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347 "SRC_X", 0, UINT_MAX);
350 dev->mode_config.prop_src_x = prop;
352 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353 "SRC_Y", 0, UINT_MAX);
356 dev->mode_config.prop_src_y = prop;
358 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
359 "SRC_W", 0, UINT_MAX);
362 dev->mode_config.prop_src_w = prop;
364 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
365 "SRC_H", 0, UINT_MAX);
368 dev->mode_config.prop_src_h = prop;
370 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
371 "CRTC_X", INT_MIN, INT_MAX);
374 dev->mode_config.prop_crtc_x = prop;
376 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
377 "CRTC_Y", INT_MIN, INT_MAX);
380 dev->mode_config.prop_crtc_y = prop;
382 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383 "CRTC_W", 0, INT_MAX);
386 dev->mode_config.prop_crtc_w = prop;
388 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
389 "CRTC_H", 0, INT_MAX);
392 dev->mode_config.prop_crtc_h = prop;
394 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
395 "FB_ID", DRM_MODE_OBJECT_FB);
398 dev->mode_config.prop_fb_id = prop;
400 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
401 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
404 dev->mode_config.prop_crtc_id = prop;
406 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
410 dev->mode_config.prop_active = prop;
412 prop = drm_property_create(dev,
413 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
417 dev->mode_config.prop_mode_id = prop;
419 prop = drm_property_create(dev,
424 dev->mode_config.degamma_lut_property = prop;
426 prop = drm_property_create_range(dev,
427 DRM_MODE_PROP_IMMUTABLE,
428 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
431 dev->mode_config.degamma_lut_size_property = prop;
433 prop = drm_property_create(dev,
438 dev->mode_config.ctm_property = prop;
440 prop = drm_property_create(dev,
445 dev->mode_config.gamma_lut_property = prop;
447 prop = drm_property_create_range(dev,
448 DRM_MODE_PROP_IMMUTABLE,
449 "GAMMA_LUT_SIZE", 0, UINT_MAX);
452 dev->mode_config.gamma_lut_size_property = prop;
458 * drm_mode_getresources - get graphics configuration
459 * @dev: drm device for the ioctl
460 * @data: data pointer for the ioctl
461 * @file_priv: drm file for the ioctl call
463 * Construct a set of configuration description structures and return
464 * them to the user, including CRTC, connector and framebuffer configuration.
466 * Called by the user via ioctl.
469 * Zero on success, negative errno on failure.
471 int drm_mode_getresources(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
474 struct drm_mode_card_res *card_res = data;
475 struct list_head *lh;
476 struct drm_framebuffer *fb;
477 struct drm_connector *connector;
478 struct drm_crtc *crtc;
479 struct drm_encoder *encoder;
481 int connector_count = 0;
484 int encoder_count = 0;
486 uint32_t __user *fb_id;
487 uint32_t __user *crtc_id;
488 uint32_t __user *connector_id;
489 uint32_t __user *encoder_id;
491 if (!drm_core_check_feature(dev, DRIVER_MODESET))
495 mutex_lock(&file_priv->fbs_lock);
497 * For the non-control nodes we need to limit the list of resources
498 * by IDs in the group list for this node
500 list_for_each(lh, &file_priv->fbs)
503 /* handle this in 4 parts */
505 if (card_res->count_fbs >= fb_count) {
507 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
508 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
509 if (put_user(fb->base.id, fb_id + copied)) {
510 mutex_unlock(&file_priv->fbs_lock);
516 card_res->count_fbs = fb_count;
517 mutex_unlock(&file_priv->fbs_lock);
519 /* mode_config.mutex protects the connector list against e.g. DP MST
520 * connector hot-adding. CRTC/Plane lists are invariant. */
521 mutex_lock(&dev->mode_config.mutex);
522 drm_for_each_crtc(crtc, dev)
525 drm_for_each_connector(connector, dev)
528 drm_for_each_encoder(encoder, dev)
531 card_res->max_height = dev->mode_config.max_height;
532 card_res->min_height = dev->mode_config.min_height;
533 card_res->max_width = dev->mode_config.max_width;
534 card_res->min_width = dev->mode_config.min_width;
537 if (card_res->count_crtcs >= crtc_count) {
539 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
540 drm_for_each_crtc(crtc, dev) {
541 if (put_user(crtc->base.id, crtc_id + copied)) {
548 card_res->count_crtcs = crtc_count;
551 if (card_res->count_encoders >= encoder_count) {
553 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
554 drm_for_each_encoder(encoder, dev) {
555 if (put_user(encoder->base.id, encoder_id +
563 card_res->count_encoders = encoder_count;
566 if (card_res->count_connectors >= connector_count) {
568 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
569 drm_for_each_connector(connector, dev) {
570 if (put_user(connector->base.id,
571 connector_id + copied)) {
578 card_res->count_connectors = connector_count;
581 mutex_unlock(&dev->mode_config.mutex);
586 * drm_mode_getcrtc - get CRTC configuration
587 * @dev: drm device for the ioctl
588 * @data: data pointer for the ioctl
589 * @file_priv: drm file for the ioctl call
591 * Construct a CRTC configuration structure to return to the user.
593 * Called by the user via ioctl.
596 * Zero on success, negative errno on failure.
598 int drm_mode_getcrtc(struct drm_device *dev,
599 void *data, struct drm_file *file_priv)
601 struct drm_mode_crtc *crtc_resp = data;
602 struct drm_crtc *crtc;
604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
607 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
611 drm_modeset_lock_crtc(crtc, crtc->primary);
612 crtc_resp->gamma_size = crtc->gamma_size;
613 if (crtc->primary->fb)
614 crtc_resp->fb_id = crtc->primary->fb->base.id;
616 crtc_resp->fb_id = 0;
619 crtc_resp->x = crtc->primary->state->src_x >> 16;
620 crtc_resp->y = crtc->primary->state->src_y >> 16;
621 if (crtc->state->enable) {
622 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
623 crtc_resp->mode_valid = 1;
626 crtc_resp->mode_valid = 0;
629 crtc_resp->x = crtc->x;
630 crtc_resp->y = crtc->y;
632 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
633 crtc_resp->mode_valid = 1;
636 crtc_resp->mode_valid = 0;
639 drm_modeset_unlock_crtc(crtc);
645 * drm_mode_set_config_internal - helper to call ->set_config
646 * @set: modeset config to set
648 * This is a little helper to wrap internal calls to the ->set_config driver
649 * interface. The only thing it adds is correct refcounting dance.
652 * Zero on success, negative errno on failure.
654 int drm_mode_set_config_internal(struct drm_mode_set *set)
656 struct drm_crtc *crtc = set->crtc;
657 struct drm_framebuffer *fb;
658 struct drm_crtc *tmp;
662 * NOTE: ->set_config can also disable other crtcs (if we steal all
663 * connectors from it), hence we need to refcount the fbs across all
664 * crtcs. Atomic modeset will have saner semantics ...
666 drm_for_each_crtc(tmp, crtc->dev)
667 tmp->primary->old_fb = tmp->primary->fb;
671 ret = crtc->funcs->set_config(set);
673 crtc->primary->crtc = crtc;
674 crtc->primary->fb = fb;
677 drm_for_each_crtc(tmp, crtc->dev) {
678 if (tmp->primary->fb)
679 drm_framebuffer_reference(tmp->primary->fb);
680 if (tmp->primary->old_fb)
681 drm_framebuffer_unreference(tmp->primary->old_fb);
682 tmp->primary->old_fb = NULL;
687 EXPORT_SYMBOL(drm_mode_set_config_internal);
690 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
691 * @mode: mode to query
692 * @hdisplay: hdisplay value to fill in
693 * @vdisplay: vdisplay value to fill in
695 * The vdisplay value will be doubled if the specified mode is a stereo mode of
696 * the appropriate layout.
698 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
699 int *hdisplay, int *vdisplay)
701 struct drm_display_mode adjusted;
703 drm_mode_copy(&adjusted, mode);
704 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
705 *hdisplay = adjusted.crtc_hdisplay;
706 *vdisplay = adjusted.crtc_vdisplay;
708 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
711 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
713 * @crtc: CRTC that framebuffer will be displayed on
716 * @mode: mode that framebuffer will be displayed under
717 * @fb: framebuffer to check size of
719 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
721 const struct drm_display_mode *mode,
722 const struct drm_framebuffer *fb)
725 int hdisplay, vdisplay;
727 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
730 drm_rotation_90_or_270(crtc->primary->state->rotation))
731 swap(hdisplay, vdisplay);
733 return drm_framebuffer_check_src_coords(x << 16, y << 16,
734 hdisplay << 16, vdisplay << 16,
737 EXPORT_SYMBOL(drm_crtc_check_viewport);
740 * drm_mode_setcrtc - set CRTC configuration
741 * @dev: drm device for the ioctl
742 * @data: data pointer for the ioctl
743 * @file_priv: drm file for the ioctl call
745 * Build a new CRTC configuration based on user request.
747 * Called by the user via ioctl.
750 * Zero on success, negative errno on failure.
752 int drm_mode_setcrtc(struct drm_device *dev, void *data,
753 struct drm_file *file_priv)
755 struct drm_mode_config *config = &dev->mode_config;
756 struct drm_mode_crtc *crtc_req = data;
757 struct drm_crtc *crtc;
758 struct drm_connector **connector_set = NULL, *connector;
759 struct drm_framebuffer *fb = NULL;
760 struct drm_display_mode *mode = NULL;
761 struct drm_mode_set set;
762 uint32_t __user *set_connectors_ptr;
766 if (!drm_core_check_feature(dev, DRIVER_MODESET))
770 * Universal plane src offsets are only 16.16, prevent havoc for
771 * drivers using universal plane code internally.
773 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
776 drm_modeset_lock_all(dev);
777 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
779 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
783 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
785 if (crtc_req->mode_valid) {
786 /* If we have a mode we need a framebuffer. */
787 /* If we pass -1, set the mode with the currently bound fb */
788 if (crtc_req->fb_id == -1) {
789 if (!crtc->primary->fb) {
790 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
794 fb = crtc->primary->fb;
795 /* Make refcounting symmetric with the lookup path. */
796 drm_framebuffer_reference(fb);
798 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
800 DRM_DEBUG_KMS("Unknown FB ID%d\n",
807 mode = drm_mode_create(dev);
813 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
815 DRM_DEBUG_KMS("Invalid mode\n");
820 * Check whether the primary plane supports the fb pixel format.
821 * Drivers not implementing the universal planes API use a
822 * default formats list provided by the DRM core which doesn't
823 * match real hardware capabilities. Skip the check in that
826 if (!crtc->primary->format_default) {
827 ret = drm_plane_check_pixel_format(crtc->primary,
830 char *format_name = drm_get_format_name(fb->pixel_format);
831 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
837 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
844 if (crtc_req->count_connectors == 0 && mode) {
845 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
850 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
851 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
852 crtc_req->count_connectors);
857 if (crtc_req->count_connectors > 0) {
860 /* Avoid unbounded kernel memory allocation */
861 if (crtc_req->count_connectors > config->num_connector) {
866 connector_set = kmalloc_array(crtc_req->count_connectors,
867 sizeof(struct drm_connector *),
869 if (!connector_set) {
874 for (i = 0; i < crtc_req->count_connectors; i++) {
875 connector_set[i] = NULL;
876 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
877 if (get_user(out_id, &set_connectors_ptr[i])) {
882 connector = drm_connector_lookup(dev, out_id);
884 DRM_DEBUG_KMS("Connector id %d unknown\n",
889 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
893 connector_set[i] = connector;
901 set.connectors = connector_set;
902 set.num_connectors = crtc_req->count_connectors;
904 ret = drm_mode_set_config_internal(&set);
908 drm_framebuffer_unreference(fb);
911 for (i = 0; i < crtc_req->count_connectors; i++) {
912 if (connector_set[i])
913 drm_connector_unreference(connector_set[i]);
916 kfree(connector_set);
917 drm_mode_destroy(dev, mode);
918 drm_modeset_unlock_all(dev);
922 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
923 struct drm_property *property,
927 struct drm_crtc *crtc = obj_to_crtc(obj);
929 if (crtc->funcs->set_property)
930 ret = crtc->funcs->set_property(crtc, property, value);
932 drm_object_property_set_value(obj, property, value);
938 * drm_mode_config_reset - call ->reset callbacks
941 * This functions calls all the crtc's, encoder's and connector's ->reset
942 * callback. Drivers can use this in e.g. their driver load or resume code to
943 * reset hardware and software state.
945 void drm_mode_config_reset(struct drm_device *dev)
947 struct drm_crtc *crtc;
948 struct drm_plane *plane;
949 struct drm_encoder *encoder;
950 struct drm_connector *connector;
952 drm_for_each_plane(plane, dev)
953 if (plane->funcs->reset)
954 plane->funcs->reset(plane);
956 drm_for_each_crtc(crtc, dev)
957 if (crtc->funcs->reset)
958 crtc->funcs->reset(crtc);
960 drm_for_each_encoder(encoder, dev)
961 if (encoder->funcs->reset)
962 encoder->funcs->reset(encoder);
964 mutex_lock(&dev->mode_config.mutex);
965 drm_for_each_connector(connector, dev)
966 if (connector->funcs->reset)
967 connector->funcs->reset(connector);
968 mutex_unlock(&dev->mode_config.mutex);
970 EXPORT_SYMBOL(drm_mode_config_reset);
973 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
976 * @file_priv: DRM file info
978 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
979 * TTM or something else entirely) and returns the resulting buffer handle. This
980 * handle can then be wrapped up into a framebuffer modeset object.
982 * Note that userspace is not allowed to use such objects for render
983 * acceleration - drivers must create their own private ioctls for such a use
986 * Called by the user via ioctl.
989 * Zero on success, negative errno on failure.
991 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
992 void *data, struct drm_file *file_priv)
994 struct drm_mode_create_dumb *args = data;
995 u32 cpp, stride, size;
997 if (!dev->driver->dumb_create)
999 if (!args->width || !args->height || !args->bpp)
1002 /* overflow checks for 32bit size calculations */
1003 /* NOTE: DIV_ROUND_UP() can overflow */
1004 cpp = DIV_ROUND_UP(args->bpp, 8);
1005 if (!cpp || cpp > 0xffffffffU / args->width)
1007 stride = cpp * args->width;
1008 if (args->height > 0xffffffffU / stride)
1011 /* test for wrap-around */
1012 size = args->height * stride;
1013 if (PAGE_ALIGN(size) == 0)
1017 * handle, pitch and size are output parameters. Zero them out to
1018 * prevent drivers from accidentally using uninitialized data. Since
1019 * not all existing userspace is clearing these fields properly we
1020 * cannot reject IOCTL with garbage in them.
1026 return dev->driver->dumb_create(file_priv, dev, args);
1030 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1033 * @file_priv: DRM file info
1035 * Allocate an offset in the drm device node's address space to be able to
1036 * memory map a dumb buffer.
1038 * Called by the user via ioctl.
1041 * Zero on success, negative errno on failure.
1043 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1044 void *data, struct drm_file *file_priv)
1046 struct drm_mode_map_dumb *args = data;
1048 /* call driver ioctl to get mmap offset */
1049 if (!dev->driver->dumb_map_offset)
1052 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
1056 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1059 * @file_priv: DRM file info
1061 * This destroys the userspace handle for the given dumb backing storage buffer.
1062 * Since buffer objects must be reference counted in the kernel a buffer object
1063 * won't be immediately freed if a framebuffer modeset object still uses it.
1065 * Called by the user via ioctl.
1068 * Zero on success, negative errno on failure.
1070 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1071 void *data, struct drm_file *file_priv)
1073 struct drm_mode_destroy_dumb *args = data;
1075 if (!dev->driver->dumb_destroy)
1078 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1082 * drm_mode_config_init - initialize DRM mode_configuration structure
1085 * Initialize @dev's mode_config structure, used for tracking the graphics
1086 * configuration of @dev.
1088 * Since this initializes the modeset locks, no locking is possible. Which is no
1089 * problem, since this should happen single threaded at init time. It is the
1090 * driver's problem to ensure this guarantee.
1093 void drm_mode_config_init(struct drm_device *dev)
1095 mutex_init(&dev->mode_config.mutex);
1096 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
1097 mutex_init(&dev->mode_config.idr_mutex);
1098 mutex_init(&dev->mode_config.fb_lock);
1099 mutex_init(&dev->mode_config.blob_lock);
1100 INIT_LIST_HEAD(&dev->mode_config.fb_list);
1101 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1102 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1103 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1104 INIT_LIST_HEAD(&dev->mode_config.property_list);
1105 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1106 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1107 idr_init(&dev->mode_config.crtc_idr);
1108 idr_init(&dev->mode_config.tile_idr);
1109 ida_init(&dev->mode_config.connector_ida);
1111 drm_modeset_lock_all(dev);
1112 drm_mode_create_standard_properties(dev);
1113 drm_modeset_unlock_all(dev);
1115 /* Just to be sure */
1116 dev->mode_config.num_fb = 0;
1117 dev->mode_config.num_connector = 0;
1118 dev->mode_config.num_crtc = 0;
1119 dev->mode_config.num_encoder = 0;
1120 dev->mode_config.num_overlay_plane = 0;
1121 dev->mode_config.num_total_plane = 0;
1123 EXPORT_SYMBOL(drm_mode_config_init);
1126 * drm_mode_config_cleanup - free up DRM mode_config info
1129 * Free up all the connectors and CRTCs associated with this DRM device, then
1130 * free up the framebuffers and associated buffer objects.
1132 * Note that since this /should/ happen single-threaded at driver/device
1133 * teardown time, no locking is required. It's the driver's job to ensure that
1134 * this guarantee actually holds true.
1136 * FIXME: cleanup any dangling user buffer objects too
1138 void drm_mode_config_cleanup(struct drm_device *dev)
1140 struct drm_connector *connector, *ot;
1141 struct drm_crtc *crtc, *ct;
1142 struct drm_encoder *encoder, *enct;
1143 struct drm_framebuffer *fb, *fbt;
1144 struct drm_property *property, *pt;
1145 struct drm_property_blob *blob, *bt;
1146 struct drm_plane *plane, *plt;
1148 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1150 encoder->funcs->destroy(encoder);
1153 list_for_each_entry_safe(connector, ot,
1154 &dev->mode_config.connector_list, head) {
1155 connector->funcs->destroy(connector);
1158 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1160 drm_property_destroy(dev, property);
1163 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1165 plane->funcs->destroy(plane);
1168 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1169 crtc->funcs->destroy(crtc);
1172 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1174 drm_property_unreference_blob(blob);
1178 * Single-threaded teardown context, so it's not required to grab the
1179 * fb_lock to protect against concurrent fb_list access. Contrary, it
1180 * would actually deadlock with the drm_framebuffer_cleanup function.
1182 * Also, if there are any framebuffers left, that's a driver leak now,
1183 * so politely WARN about this.
1185 WARN_ON(!list_empty(&dev->mode_config.fb_list));
1186 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1187 drm_framebuffer_free(&fb->base.refcount);
1190 ida_destroy(&dev->mode_config.connector_ida);
1191 idr_destroy(&dev->mode_config.tile_idr);
1192 idr_destroy(&dev->mode_config.crtc_idr);
1193 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
1195 EXPORT_SYMBOL(drm_mode_config_cleanup);
1200 * Tile groups are used to represent tiled monitors with a unique
1201 * integer identifier. Tiled monitors using DisplayID v1.3 have
1202 * a unique 8-byte handle, we store this in a tile group, so we
1203 * have a common identifier for all tiles in a monitor group.
1205 static void drm_tile_group_free(struct kref *kref)
1207 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1208 struct drm_device *dev = tg->dev;
1209 mutex_lock(&dev->mode_config.idr_mutex);
1210 idr_remove(&dev->mode_config.tile_idr, tg->id);
1211 mutex_unlock(&dev->mode_config.idr_mutex);
1216 * drm_mode_put_tile_group - drop a reference to a tile group.
1218 * @tg: tile group to drop reference to.
1220 * drop reference to tile group and free if 0.
1222 void drm_mode_put_tile_group(struct drm_device *dev,
1223 struct drm_tile_group *tg)
1225 kref_put(&tg->refcount, drm_tile_group_free);
1229 * drm_mode_get_tile_group - get a reference to an existing tile group
1231 * @topology: 8-bytes unique per monitor.
1233 * Use the unique bytes to get a reference to an existing tile group.
1236 * tile group or NULL if not found.
1238 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1241 struct drm_tile_group *tg;
1243 mutex_lock(&dev->mode_config.idr_mutex);
1244 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1245 if (!memcmp(tg->group_data, topology, 8)) {
1246 if (!kref_get_unless_zero(&tg->refcount))
1248 mutex_unlock(&dev->mode_config.idr_mutex);
1252 mutex_unlock(&dev->mode_config.idr_mutex);
1255 EXPORT_SYMBOL(drm_mode_get_tile_group);
1258 * drm_mode_create_tile_group - create a tile group from a displayid description
1260 * @topology: 8-bytes unique per monitor.
1262 * Create a tile group for the unique monitor, and get a unique
1263 * identifier for the tile group.
1266 * new tile group or error.
1268 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1271 struct drm_tile_group *tg;
1274 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1276 return ERR_PTR(-ENOMEM);
1278 kref_init(&tg->refcount);
1279 memcpy(tg->group_data, topology, 8);
1282 mutex_lock(&dev->mode_config.idr_mutex);
1283 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1291 mutex_unlock(&dev->mode_config.idr_mutex);
1294 EXPORT_SYMBOL(drm_mode_create_tile_group);