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 if (connector->registration_state != DRM_CONNECTOR_REGISTERED)
157 /* only expose writeback connectors if userspace understands them */
158 if (!file_priv->writeback_connectors &&
159 (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
162 if (drm_lease_held(file_priv, connector->base.id)) {
163 if (count < card_res->count_connectors &&
164 put_user(connector->base.id, connector_id + count)) {
165 drm_connector_list_iter_end(&conn_iter);
171 card_res->count_connectors = count;
172 drm_connector_list_iter_end(&conn_iter);
178 * drm_mode_config_reset - call ->reset callbacks
181 * This functions calls all the crtc's, encoder's and connector's ->reset
182 * callback. Drivers can use this in e.g. their driver load or resume code to
183 * reset hardware and software state.
185 void drm_mode_config_reset(struct drm_device *dev)
187 struct drm_crtc *crtc;
188 struct drm_plane *plane;
189 struct drm_encoder *encoder;
190 struct drm_connector *connector;
191 struct drm_connector_list_iter conn_iter;
193 drm_for_each_plane(plane, dev)
194 if (plane->funcs->reset)
195 plane->funcs->reset(plane);
197 drm_for_each_crtc(crtc, dev)
198 if (crtc->funcs->reset)
199 crtc->funcs->reset(crtc);
201 drm_for_each_encoder(encoder, dev)
202 if (encoder->funcs && encoder->funcs->reset)
203 encoder->funcs->reset(encoder);
205 drm_connector_list_iter_begin(dev, &conn_iter);
206 drm_for_each_connector_iter(connector, &conn_iter)
207 if (connector->funcs->reset)
208 connector->funcs->reset(connector);
209 drm_connector_list_iter_end(&conn_iter);
211 EXPORT_SYMBOL(drm_mode_config_reset);
216 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
217 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
218 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
219 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
222 static int drm_mode_create_standard_properties(struct drm_device *dev)
224 struct drm_property *prop;
227 ret = drm_connector_create_standard_properties(dev);
231 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
232 "type", drm_plane_type_enum_list,
233 ARRAY_SIZE(drm_plane_type_enum_list));
236 dev->mode_config.plane_type_property = prop;
238 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
239 "SRC_X", 0, UINT_MAX);
242 dev->mode_config.prop_src_x = prop;
244 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
245 "SRC_Y", 0, UINT_MAX);
248 dev->mode_config.prop_src_y = prop;
250 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
251 "SRC_W", 0, UINT_MAX);
254 dev->mode_config.prop_src_w = prop;
256 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
257 "SRC_H", 0, UINT_MAX);
260 dev->mode_config.prop_src_h = prop;
262 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
263 "CRTC_X", INT_MIN, INT_MAX);
266 dev->mode_config.prop_crtc_x = prop;
268 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
269 "CRTC_Y", INT_MIN, INT_MAX);
272 dev->mode_config.prop_crtc_y = prop;
274 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
275 "CRTC_W", 0, INT_MAX);
278 dev->mode_config.prop_crtc_w = prop;
280 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
281 "CRTC_H", 0, INT_MAX);
284 dev->mode_config.prop_crtc_h = prop;
286 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
287 "FB_ID", DRM_MODE_OBJECT_FB);
290 dev->mode_config.prop_fb_id = prop;
292 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
293 "IN_FENCE_FD", -1, INT_MAX);
296 dev->mode_config.prop_in_fence_fd = prop;
298 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
299 "OUT_FENCE_PTR", 0, U64_MAX);
302 dev->mode_config.prop_out_fence_ptr = prop;
304 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
305 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
308 dev->mode_config.prop_crtc_id = prop;
310 prop = drm_property_create(dev,
311 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
312 "FB_DAMAGE_CLIPS", 0);
315 dev->mode_config.prop_fb_damage_clips = prop;
317 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
321 dev->mode_config.prop_active = prop;
323 prop = drm_property_create(dev,
324 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
328 dev->mode_config.prop_mode_id = prop;
330 prop = drm_property_create_bool(dev, 0,
334 dev->mode_config.prop_vrr_enabled = prop;
336 prop = drm_property_create(dev,
341 dev->mode_config.degamma_lut_property = prop;
343 prop = drm_property_create_range(dev,
344 DRM_MODE_PROP_IMMUTABLE,
345 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
348 dev->mode_config.degamma_lut_size_property = prop;
350 prop = drm_property_create(dev,
355 dev->mode_config.ctm_property = prop;
357 prop = drm_property_create(dev,
362 dev->mode_config.gamma_lut_property = prop;
364 prop = drm_property_create_range(dev,
365 DRM_MODE_PROP_IMMUTABLE,
366 "GAMMA_LUT_SIZE", 0, UINT_MAX);
369 dev->mode_config.gamma_lut_size_property = prop;
371 prop = drm_property_create(dev,
372 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
376 dev->mode_config.modifiers_property = prop;
381 static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
383 drm_mode_config_cleanup(dev);
387 * drmm_mode_config_init - managed DRM mode_configuration structure
391 * Initialize @dev's mode_config structure, used for tracking the graphics
392 * configuration of @dev.
394 * Since this initializes the modeset locks, no locking is possible. Which is no
395 * problem, since this should happen single threaded at init time. It is the
396 * driver's problem to ensure this guarantee.
398 * Cleanup is automatically handled through registering drm_mode_config_cleanup
399 * with drmm_add_action().
401 * Returns: 0 on success, negative error value on failure.
403 int drmm_mode_config_init(struct drm_device *dev)
405 mutex_init(&dev->mode_config.mutex);
406 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
407 mutex_init(&dev->mode_config.idr_mutex);
408 mutex_init(&dev->mode_config.fb_lock);
409 mutex_init(&dev->mode_config.blob_lock);
410 INIT_LIST_HEAD(&dev->mode_config.fb_list);
411 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
412 INIT_LIST_HEAD(&dev->mode_config.connector_list);
413 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
414 INIT_LIST_HEAD(&dev->mode_config.property_list);
415 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
416 INIT_LIST_HEAD(&dev->mode_config.plane_list);
417 INIT_LIST_HEAD(&dev->mode_config.privobj_list);
418 idr_init_base(&dev->mode_config.object_idr, 1);
419 idr_init_base(&dev->mode_config.tile_idr, 1);
420 ida_init(&dev->mode_config.connector_ida);
421 spin_lock_init(&dev->mode_config.connector_list_lock);
423 init_llist_head(&dev->mode_config.connector_free_list);
424 INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
426 drm_mode_create_standard_properties(dev);
428 /* Just to be sure */
429 dev->mode_config.num_fb = 0;
430 dev->mode_config.num_connector = 0;
431 dev->mode_config.num_crtc = 0;
432 dev->mode_config.num_encoder = 0;
433 dev->mode_config.num_total_plane = 0;
435 if (IS_ENABLED(CONFIG_LOCKDEP)) {
436 struct drm_modeset_acquire_ctx modeset_ctx;
437 struct ww_acquire_ctx resv_ctx;
438 struct dma_resv resv;
441 dma_resv_init(&resv);
443 drm_modeset_acquire_init(&modeset_ctx, 0);
444 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
447 ret = drm_modeset_backoff(&modeset_ctx);
449 ww_acquire_init(&resv_ctx, &reservation_ww_class);
450 ret = dma_resv_lock(&resv, &resv_ctx);
452 dma_resv_lock_slow(&resv, &resv_ctx);
454 dma_resv_unlock(&resv);
455 ww_acquire_fini(&resv_ctx);
457 drm_modeset_drop_locks(&modeset_ctx);
458 drm_modeset_acquire_fini(&modeset_ctx);
459 dma_resv_fini(&resv);
462 return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
465 EXPORT_SYMBOL(drmm_mode_config_init);
468 * drm_mode_config_cleanup - free up DRM mode_config info
471 * Free up all the connectors and CRTCs associated with this DRM device, then
472 * free up the framebuffers and associated buffer objects.
474 * Note that since this /should/ happen single-threaded at driver/device
475 * teardown time, no locking is required. It's the driver's job to ensure that
476 * this guarantee actually holds true.
478 * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
479 * drivers to explicitly call this function.
481 void drm_mode_config_cleanup(struct drm_device *dev)
483 struct drm_connector *connector;
484 struct drm_connector_list_iter conn_iter;
485 struct drm_crtc *crtc, *ct;
486 struct drm_encoder *encoder, *enct;
487 struct drm_framebuffer *fb, *fbt;
488 struct drm_property *property, *pt;
489 struct drm_property_blob *blob, *bt;
490 struct drm_plane *plane, *plt;
492 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
494 encoder->funcs->destroy(encoder);
497 drm_connector_list_iter_begin(dev, &conn_iter);
498 drm_for_each_connector_iter(connector, &conn_iter) {
499 /* drm_connector_list_iter holds an full reference to the
500 * current connector itself, which means it is inherently safe
501 * against unreferencing the current connector - but not against
502 * deleting it right away. */
503 drm_connector_put(connector);
505 drm_connector_list_iter_end(&conn_iter);
506 /* connector_iter drops references in a work item. */
507 flush_work(&dev->mode_config.connector_free_work);
508 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
509 drm_connector_list_iter_begin(dev, &conn_iter);
510 drm_for_each_connector_iter(connector, &conn_iter)
511 DRM_ERROR("connector %s leaked!\n", connector->name);
512 drm_connector_list_iter_end(&conn_iter);
515 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
517 drm_property_destroy(dev, property);
520 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
522 plane->funcs->destroy(plane);
525 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
526 crtc->funcs->destroy(crtc);
529 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
531 drm_property_blob_put(blob);
535 * Single-threaded teardown context, so it's not required to grab the
536 * fb_lock to protect against concurrent fb_list access. Contrary, it
537 * would actually deadlock with the drm_framebuffer_cleanup function.
539 * Also, if there are any framebuffers left, that's a driver leak now,
540 * so politely WARN about this.
542 WARN_ON(!list_empty(&dev->mode_config.fb_list));
543 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
544 struct drm_printer p = drm_debug_printer("[leaked fb]");
546 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
547 drm_framebuffer_print_info(&p, 1, fb);
548 drm_framebuffer_free(&fb->base.refcount);
551 ida_destroy(&dev->mode_config.connector_ida);
552 idr_destroy(&dev->mode_config.tile_idr);
553 idr_destroy(&dev->mode_config.object_idr);
554 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
556 EXPORT_SYMBOL(drm_mode_config_cleanup);
558 static u32 full_encoder_mask(struct drm_device *dev)
560 struct drm_encoder *encoder;
561 u32 encoder_mask = 0;
563 drm_for_each_encoder(encoder, dev)
564 encoder_mask |= drm_encoder_mask(encoder);
570 * For some reason we want the encoder itself included in
571 * possible_clones. Make life easy for drivers by allowing them
572 * to leave possible_clones unset if no cloning is possible.
574 static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
576 if (encoder->possible_clones == 0)
577 encoder->possible_clones = drm_encoder_mask(encoder);
580 static void validate_encoder_possible_clones(struct drm_encoder *encoder)
582 struct drm_device *dev = encoder->dev;
583 u32 encoder_mask = full_encoder_mask(dev);
584 struct drm_encoder *other;
586 drm_for_each_encoder(other, dev) {
587 WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
588 !!(other->possible_clones & drm_encoder_mask(encoder)),
589 "possible_clones mismatch: "
590 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
591 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
592 encoder->base.id, encoder->name,
593 drm_encoder_mask(encoder), encoder->possible_clones,
594 other->base.id, other->name,
595 drm_encoder_mask(other), other->possible_clones);
598 WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
599 (encoder->possible_clones & ~encoder_mask) != 0,
600 "Bogus possible_clones: "
601 "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
602 encoder->base.id, encoder->name,
603 encoder->possible_clones, encoder_mask);
606 static u32 full_crtc_mask(struct drm_device *dev)
608 struct drm_crtc *crtc;
611 drm_for_each_crtc(crtc, dev)
612 crtc_mask |= drm_crtc_mask(crtc);
617 static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
619 u32 crtc_mask = full_crtc_mask(encoder->dev);
621 WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
622 (encoder->possible_crtcs & ~crtc_mask) != 0,
623 "Bogus possible_crtcs: "
624 "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
625 encoder->base.id, encoder->name,
626 encoder->possible_crtcs, crtc_mask);
629 void drm_mode_config_validate(struct drm_device *dev)
631 struct drm_encoder *encoder;
632 struct drm_crtc *crtc;
633 struct drm_plane *plane;
634 u32 primary_with_crtc = 0, cursor_with_crtc = 0;
635 unsigned int num_primary = 0;
637 if (!drm_core_check_feature(dev, DRIVER_MODESET))
640 drm_for_each_encoder(encoder, dev)
641 fixup_encoder_possible_clones(encoder);
643 drm_for_each_encoder(encoder, dev) {
644 validate_encoder_possible_clones(encoder);
645 validate_encoder_possible_crtcs(encoder);
648 drm_for_each_crtc(crtc, dev) {
649 WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
650 crtc->base.id, crtc->name);
652 WARN(crtc->cursor && crtc->funcs->cursor_set,
653 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
654 crtc->base.id, crtc->name);
655 WARN(crtc->cursor && crtc->funcs->cursor_set2,
656 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
657 crtc->base.id, crtc->name);
658 WARN(crtc->cursor && crtc->funcs->cursor_move,
659 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
660 crtc->base.id, crtc->name);
663 WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
664 "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
665 crtc->primary->base.id, crtc->primary->name,
666 crtc->base.id, crtc->name);
667 WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
668 "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
669 crtc->primary->base.id, crtc->primary->name);
670 primary_with_crtc |= drm_plane_mask(crtc->primary);
673 WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
674 "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
675 crtc->cursor->base.id, crtc->cursor->name,
676 crtc->base.id, crtc->name);
677 WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
678 "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
679 crtc->cursor->base.id, crtc->cursor->name);
680 cursor_with_crtc |= drm_plane_mask(crtc->cursor);
684 drm_for_each_plane(plane, dev) {
685 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
689 WARN(num_primary != dev->mode_config.num_crtc,
690 "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
691 num_primary, dev->mode_config.num_crtc);