2 * Copyright (c) 2016 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <linux/uaccess.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_encoder.h>
27 #include <drm/drm_file.h>
28 #include <drm/drm_framebuffer.h>
29 #include <drm/drm_managed.h>
30 #include <drm/drm_mode_config.h>
31 #include <drm/drm_print.h>
32 #include <linux/dma-resv.h>
34 #include "drm_crtc_internal.h"
35 #include "drm_internal.h"
37 int drm_modeset_register_all(struct drm_device *dev)
41 ret = drm_plane_register_all(dev);
45 ret = drm_crtc_register_all(dev);
49 ret = drm_encoder_register_all(dev);
53 ret = drm_connector_register_all(dev);
60 drm_encoder_unregister_all(dev);
62 drm_crtc_unregister_all(dev);
64 drm_plane_unregister_all(dev);
69 void drm_modeset_unregister_all(struct drm_device *dev)
71 drm_connector_unregister_all(dev);
72 drm_encoder_unregister_all(dev);
73 drm_crtc_unregister_all(dev);
74 drm_plane_unregister_all(dev);
78 * drm_mode_getresources - get graphics configuration
79 * @dev: drm device for the ioctl
80 * @data: data pointer for the ioctl
81 * @file_priv: drm file for the ioctl call
83 * Construct a set of configuration description structures and return
84 * them to the user, including CRTC, connector and framebuffer configuration.
86 * Called by the user via ioctl.
89 * Zero on success, negative errno on failure.
91 int drm_mode_getresources(struct drm_device *dev, void *data,
92 struct drm_file *file_priv)
94 struct drm_mode_card_res *card_res = data;
95 struct drm_framebuffer *fb;
96 struct drm_connector *connector;
97 struct drm_crtc *crtc;
98 struct drm_encoder *encoder;
100 uint32_t __user *fb_id;
101 uint32_t __user *crtc_id;
102 uint32_t __user *connector_id;
103 uint32_t __user *encoder_id;
104 struct drm_connector_list_iter conn_iter;
106 if (!drm_core_check_feature(dev, DRIVER_MODESET))
109 mutex_lock(&file_priv->fbs_lock);
111 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
112 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
113 if (count < card_res->count_fbs &&
114 put_user(fb->base.id, fb_id + count)) {
115 mutex_unlock(&file_priv->fbs_lock);
120 card_res->count_fbs = count;
121 mutex_unlock(&file_priv->fbs_lock);
123 card_res->max_height = dev->mode_config.max_height;
124 card_res->min_height = dev->mode_config.min_height;
125 card_res->max_width = dev->mode_config.max_width;
126 card_res->min_width = dev->mode_config.min_width;
129 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
130 drm_for_each_crtc(crtc, dev) {
131 if (drm_lease_held(file_priv, crtc->base.id)) {
132 if (count < card_res->count_crtcs &&
133 put_user(crtc->base.id, crtc_id + count))
138 card_res->count_crtcs = count;
141 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
142 drm_for_each_encoder(encoder, dev) {
143 if (count < card_res->count_encoders &&
144 put_user(encoder->base.id, encoder_id + count))
148 card_res->count_encoders = count;
150 drm_connector_list_iter_begin(dev, &conn_iter);
152 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
153 drm_for_each_connector_iter(connector, &conn_iter) {
154 /* only expose writeback connectors if userspace understands them */
155 if (!file_priv->writeback_connectors &&
156 (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
159 if (drm_lease_held(file_priv, connector->base.id)) {
160 if (count < card_res->count_connectors &&
161 put_user(connector->base.id, connector_id + count)) {
162 drm_connector_list_iter_end(&conn_iter);
168 card_res->count_connectors = count;
169 drm_connector_list_iter_end(&conn_iter);
175 * drm_mode_config_reset - call ->reset callbacks
178 * This functions calls all the crtc's, encoder's and connector's ->reset
179 * callback. Drivers can use this in e.g. their driver load or resume code to
180 * reset hardware and software state.
182 void drm_mode_config_reset(struct drm_device *dev)
184 struct drm_crtc *crtc;
185 struct drm_plane *plane;
186 struct drm_encoder *encoder;
187 struct drm_connector *connector;
188 struct drm_connector_list_iter conn_iter;
190 drm_for_each_plane(plane, dev)
191 if (plane->funcs->reset)
192 plane->funcs->reset(plane);
194 drm_for_each_crtc(crtc, dev)
195 if (crtc->funcs->reset)
196 crtc->funcs->reset(crtc);
198 drm_for_each_encoder(encoder, dev)
199 if (encoder->funcs && encoder->funcs->reset)
200 encoder->funcs->reset(encoder);
202 drm_connector_list_iter_begin(dev, &conn_iter);
203 drm_for_each_connector_iter(connector, &conn_iter)
204 if (connector->funcs->reset)
205 connector->funcs->reset(connector);
206 drm_connector_list_iter_end(&conn_iter);
208 EXPORT_SYMBOL(drm_mode_config_reset);
213 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
214 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
215 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
216 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
219 static int drm_mode_create_standard_properties(struct drm_device *dev)
221 struct drm_property *prop;
224 ret = drm_connector_create_standard_properties(dev);
228 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
229 "type", drm_plane_type_enum_list,
230 ARRAY_SIZE(drm_plane_type_enum_list));
233 dev->mode_config.plane_type_property = prop;
235 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
236 "SRC_X", 0, UINT_MAX);
239 dev->mode_config.prop_src_x = prop;
241 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
242 "SRC_Y", 0, UINT_MAX);
245 dev->mode_config.prop_src_y = prop;
247 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
248 "SRC_W", 0, UINT_MAX);
251 dev->mode_config.prop_src_w = prop;
253 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
254 "SRC_H", 0, UINT_MAX);
257 dev->mode_config.prop_src_h = prop;
259 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
260 "CRTC_X", INT_MIN, INT_MAX);
263 dev->mode_config.prop_crtc_x = prop;
265 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
266 "CRTC_Y", INT_MIN, INT_MAX);
269 dev->mode_config.prop_crtc_y = prop;
271 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
272 "CRTC_W", 0, INT_MAX);
275 dev->mode_config.prop_crtc_w = prop;
277 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
278 "CRTC_H", 0, INT_MAX);
281 dev->mode_config.prop_crtc_h = prop;
283 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
284 "FB_ID", DRM_MODE_OBJECT_FB);
287 dev->mode_config.prop_fb_id = prop;
289 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
290 "IN_FENCE_FD", -1, INT_MAX);
293 dev->mode_config.prop_in_fence_fd = prop;
295 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
296 "OUT_FENCE_PTR", 0, U64_MAX);
299 dev->mode_config.prop_out_fence_ptr = prop;
301 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
302 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
305 dev->mode_config.prop_crtc_id = prop;
307 prop = drm_property_create(dev,
308 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
309 "FB_DAMAGE_CLIPS", 0);
312 dev->mode_config.prop_fb_damage_clips = prop;
314 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
318 dev->mode_config.prop_active = prop;
320 prop = drm_property_create(dev,
321 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
325 dev->mode_config.prop_mode_id = prop;
327 prop = drm_property_create_bool(dev, 0,
331 dev->mode_config.prop_vrr_enabled = prop;
333 prop = drm_property_create(dev,
338 dev->mode_config.degamma_lut_property = prop;
340 prop = drm_property_create_range(dev,
341 DRM_MODE_PROP_IMMUTABLE,
342 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
345 dev->mode_config.degamma_lut_size_property = prop;
347 prop = drm_property_create(dev,
352 dev->mode_config.ctm_property = prop;
354 prop = drm_property_create(dev,
359 dev->mode_config.gamma_lut_property = prop;
361 prop = drm_property_create_range(dev,
362 DRM_MODE_PROP_IMMUTABLE,
363 "GAMMA_LUT_SIZE", 0, UINT_MAX);
366 dev->mode_config.gamma_lut_size_property = prop;
368 prop = drm_property_create(dev,
369 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
373 dev->mode_config.modifiers_property = prop;
378 static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
380 drm_mode_config_cleanup(dev);
384 * drmm_mode_config_init - managed DRM mode_configuration structure
388 * Initialize @dev's mode_config structure, used for tracking the graphics
389 * configuration of @dev.
391 * Since this initializes the modeset locks, no locking is possible. Which is no
392 * problem, since this should happen single threaded at init time. It is the
393 * driver's problem to ensure this guarantee.
395 * Cleanup is automatically handled through registering drm_mode_config_cleanup
396 * with drmm_add_action().
398 * Returns: 0 on success, negative error value on failure.
400 int drmm_mode_config_init(struct drm_device *dev)
402 mutex_init(&dev->mode_config.mutex);
403 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
404 mutex_init(&dev->mode_config.idr_mutex);
405 mutex_init(&dev->mode_config.fb_lock);
406 mutex_init(&dev->mode_config.blob_lock);
407 INIT_LIST_HEAD(&dev->mode_config.fb_list);
408 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
409 INIT_LIST_HEAD(&dev->mode_config.connector_list);
410 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
411 INIT_LIST_HEAD(&dev->mode_config.property_list);
412 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
413 INIT_LIST_HEAD(&dev->mode_config.plane_list);
414 INIT_LIST_HEAD(&dev->mode_config.privobj_list);
415 idr_init(&dev->mode_config.object_idr);
416 idr_init(&dev->mode_config.tile_idr);
417 ida_init(&dev->mode_config.connector_ida);
418 spin_lock_init(&dev->mode_config.connector_list_lock);
420 init_llist_head(&dev->mode_config.connector_free_list);
421 INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
423 drm_mode_create_standard_properties(dev);
425 /* Just to be sure */
426 dev->mode_config.num_fb = 0;
427 dev->mode_config.num_connector = 0;
428 dev->mode_config.num_crtc = 0;
429 dev->mode_config.num_encoder = 0;
430 dev->mode_config.num_total_plane = 0;
432 if (IS_ENABLED(CONFIG_LOCKDEP)) {
433 struct drm_modeset_acquire_ctx modeset_ctx;
434 struct ww_acquire_ctx resv_ctx;
435 struct dma_resv resv;
438 dma_resv_init(&resv);
440 drm_modeset_acquire_init(&modeset_ctx, 0);
441 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
444 ret = drm_modeset_backoff(&modeset_ctx);
446 ww_acquire_init(&resv_ctx, &reservation_ww_class);
447 ret = dma_resv_lock(&resv, &resv_ctx);
449 dma_resv_lock_slow(&resv, &resv_ctx);
451 dma_resv_unlock(&resv);
452 ww_acquire_fini(&resv_ctx);
454 drm_modeset_drop_locks(&modeset_ctx);
455 drm_modeset_acquire_fini(&modeset_ctx);
456 dma_resv_fini(&resv);
459 return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
462 EXPORT_SYMBOL(drmm_mode_config_init);
465 * drm_mode_config_cleanup - free up DRM mode_config info
468 * Free up all the connectors and CRTCs associated with this DRM device, then
469 * free up the framebuffers and associated buffer objects.
471 * Note that since this /should/ happen single-threaded at driver/device
472 * teardown time, no locking is required. It's the driver's job to ensure that
473 * this guarantee actually holds true.
475 * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
476 * drivers to explicitly call this function.
478 void drm_mode_config_cleanup(struct drm_device *dev)
480 struct drm_connector *connector;
481 struct drm_connector_list_iter conn_iter;
482 struct drm_crtc *crtc, *ct;
483 struct drm_encoder *encoder, *enct;
484 struct drm_framebuffer *fb, *fbt;
485 struct drm_property *property, *pt;
486 struct drm_property_blob *blob, *bt;
487 struct drm_plane *plane, *plt;
489 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
491 encoder->funcs->destroy(encoder);
494 drm_connector_list_iter_begin(dev, &conn_iter);
495 drm_for_each_connector_iter(connector, &conn_iter) {
496 /* drm_connector_list_iter holds an full reference to the
497 * current connector itself, which means it is inherently safe
498 * against unreferencing the current connector - but not against
499 * deleting it right away. */
500 drm_connector_put(connector);
502 drm_connector_list_iter_end(&conn_iter);
503 /* connector_iter drops references in a work item. */
504 flush_work(&dev->mode_config.connector_free_work);
505 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
506 drm_connector_list_iter_begin(dev, &conn_iter);
507 drm_for_each_connector_iter(connector, &conn_iter)
508 DRM_ERROR("connector %s leaked!\n", connector->name);
509 drm_connector_list_iter_end(&conn_iter);
512 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
514 drm_property_destroy(dev, property);
517 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
519 plane->funcs->destroy(plane);
522 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
523 crtc->funcs->destroy(crtc);
526 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
528 drm_property_blob_put(blob);
532 * Single-threaded teardown context, so it's not required to grab the
533 * fb_lock to protect against concurrent fb_list access. Contrary, it
534 * would actually deadlock with the drm_framebuffer_cleanup function.
536 * Also, if there are any framebuffers left, that's a driver leak now,
537 * so politely WARN about this.
539 WARN_ON(!list_empty(&dev->mode_config.fb_list));
540 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
541 struct drm_printer p = drm_debug_printer("[leaked fb]");
543 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
544 drm_framebuffer_print_info(&p, 1, fb);
545 drm_framebuffer_free(&fb->base.refcount);
548 ida_destroy(&dev->mode_config.connector_ida);
549 idr_destroy(&dev->mode_config.tile_idr);
550 idr_destroy(&dev->mode_config.object_idr);
551 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
553 EXPORT_SYMBOL(drm_mode_config_cleanup);
555 static u32 full_encoder_mask(struct drm_device *dev)
557 struct drm_encoder *encoder;
558 u32 encoder_mask = 0;
560 drm_for_each_encoder(encoder, dev)
561 encoder_mask |= drm_encoder_mask(encoder);
567 * For some reason we want the encoder itself included in
568 * possible_clones. Make life easy for drivers by allowing them
569 * to leave possible_clones unset if no cloning is possible.
571 static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
573 if (encoder->possible_clones == 0)
574 encoder->possible_clones = drm_encoder_mask(encoder);
577 static void validate_encoder_possible_clones(struct drm_encoder *encoder)
579 struct drm_device *dev = encoder->dev;
580 u32 encoder_mask = full_encoder_mask(dev);
581 struct drm_encoder *other;
583 drm_for_each_encoder(other, dev) {
584 WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
585 !!(other->possible_clones & drm_encoder_mask(encoder)),
586 "possible_clones mismatch: "
587 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
588 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
589 encoder->base.id, encoder->name,
590 drm_encoder_mask(encoder), encoder->possible_clones,
591 other->base.id, other->name,
592 drm_encoder_mask(other), other->possible_clones);
595 WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
596 (encoder->possible_clones & ~encoder_mask) != 0,
597 "Bogus possible_clones: "
598 "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
599 encoder->base.id, encoder->name,
600 encoder->possible_clones, encoder_mask);
603 static u32 full_crtc_mask(struct drm_device *dev)
605 struct drm_crtc *crtc;
608 drm_for_each_crtc(crtc, dev)
609 crtc_mask |= drm_crtc_mask(crtc);
614 static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
616 u32 crtc_mask = full_crtc_mask(encoder->dev);
618 WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
619 (encoder->possible_crtcs & ~crtc_mask) != 0,
620 "Bogus possible_crtcs: "
621 "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
622 encoder->base.id, encoder->name,
623 encoder->possible_crtcs, crtc_mask);
626 void drm_mode_config_validate(struct drm_device *dev)
628 struct drm_encoder *encoder;
629 struct drm_crtc *crtc;
630 struct drm_plane *plane;
631 u32 primary_with_crtc = 0, cursor_with_crtc = 0;
632 unsigned int num_primary = 0;
634 if (!drm_core_check_feature(dev, DRIVER_MODESET))
637 drm_for_each_encoder(encoder, dev)
638 fixup_encoder_possible_clones(encoder);
640 drm_for_each_encoder(encoder, dev) {
641 validate_encoder_possible_clones(encoder);
642 validate_encoder_possible_crtcs(encoder);
645 drm_for_each_crtc(crtc, dev) {
646 WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
647 crtc->base.id, crtc->name);
649 WARN(crtc->cursor && crtc->funcs->cursor_set,
650 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
651 crtc->base.id, crtc->name);
652 WARN(crtc->cursor && crtc->funcs->cursor_set2,
653 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
654 crtc->base.id, crtc->name);
655 WARN(crtc->cursor && crtc->funcs->cursor_move,
656 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
657 crtc->base.id, crtc->name);
660 WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
661 "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
662 crtc->primary->base.id, crtc->primary->name,
663 crtc->base.id, crtc->name);
664 WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
665 "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
666 crtc->primary->base.id, crtc->primary->name);
667 primary_with_crtc |= drm_plane_mask(crtc->primary);
670 WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
671 "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
672 crtc->cursor->base.id, crtc->cursor->name,
673 crtc->base.id, crtc->name);
674 WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
675 "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
676 crtc->cursor->base.id, crtc->cursor->name);
677 cursor_with_crtc |= drm_plane_mask(crtc->cursor);
681 drm_for_each_plane(plane, dev) {
682 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
686 WARN(num_primary != dev->mode_config.num_crtc,
687 "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
688 num_primary, dev->mode_config.num_crtc);