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_managed.h>
29 #include <drm/drm_mode_config.h>
30 #include <drm/drm_print.h>
31 #include <linux/dma-resv.h>
33 #include "drm_crtc_internal.h"
34 #include "drm_internal.h"
36 int drm_modeset_register_all(struct drm_device *dev)
40 ret = drm_plane_register_all(dev);
44 ret = drm_crtc_register_all(dev);
48 ret = drm_encoder_register_all(dev);
52 ret = drm_connector_register_all(dev);
59 drm_encoder_unregister_all(dev);
61 drm_crtc_unregister_all(dev);
63 drm_plane_unregister_all(dev);
68 void drm_modeset_unregister_all(struct drm_device *dev)
70 drm_connector_unregister_all(dev);
71 drm_encoder_unregister_all(dev);
72 drm_crtc_unregister_all(dev);
73 drm_plane_unregister_all(dev);
77 * drm_mode_getresources - get graphics configuration
78 * @dev: drm device for the ioctl
79 * @data: data pointer for the ioctl
80 * @file_priv: drm file for the ioctl call
82 * Construct a set of configuration description structures and return
83 * them to the user, including CRTC, connector and framebuffer configuration.
85 * Called by the user via ioctl.
88 * Zero on success, negative errno on failure.
90 int drm_mode_getresources(struct drm_device *dev, void *data,
91 struct drm_file *file_priv)
93 struct drm_mode_card_res *card_res = data;
94 struct drm_framebuffer *fb;
95 struct drm_connector *connector;
96 struct drm_crtc *crtc;
97 struct drm_encoder *encoder;
99 uint32_t __user *fb_id;
100 uint32_t __user *crtc_id;
101 uint32_t __user *connector_id;
102 uint32_t __user *encoder_id;
103 struct drm_connector_list_iter conn_iter;
105 if (!drm_core_check_feature(dev, DRIVER_MODESET))
108 mutex_lock(&file_priv->fbs_lock);
110 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
111 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
112 if (count < card_res->count_fbs &&
113 put_user(fb->base.id, fb_id + count)) {
114 mutex_unlock(&file_priv->fbs_lock);
119 card_res->count_fbs = count;
120 mutex_unlock(&file_priv->fbs_lock);
122 card_res->max_height = dev->mode_config.max_height;
123 card_res->min_height = dev->mode_config.min_height;
124 card_res->max_width = dev->mode_config.max_width;
125 card_res->min_width = dev->mode_config.min_width;
128 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
129 drm_for_each_crtc(crtc, dev) {
130 if (drm_lease_held(file_priv, crtc->base.id)) {
131 if (count < card_res->count_crtcs &&
132 put_user(crtc->base.id, crtc_id + count))
137 card_res->count_crtcs = count;
140 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
141 drm_for_each_encoder(encoder, dev) {
142 if (count < card_res->count_encoders &&
143 put_user(encoder->base.id, encoder_id + count))
147 card_res->count_encoders = count;
149 drm_connector_list_iter_begin(dev, &conn_iter);
151 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
152 drm_for_each_connector_iter(connector, &conn_iter) {
153 /* only expose writeback connectors if userspace understands them */
154 if (!file_priv->writeback_connectors &&
155 (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
158 if (drm_lease_held(file_priv, connector->base.id)) {
159 if (count < card_res->count_connectors &&
160 put_user(connector->base.id, connector_id + count)) {
161 drm_connector_list_iter_end(&conn_iter);
167 card_res->count_connectors = count;
168 drm_connector_list_iter_end(&conn_iter);
174 * drm_mode_config_reset - call ->reset callbacks
177 * This functions calls all the crtc's, encoder's and connector's ->reset
178 * callback. Drivers can use this in e.g. their driver load or resume code to
179 * reset hardware and software state.
181 void drm_mode_config_reset(struct drm_device *dev)
183 struct drm_crtc *crtc;
184 struct drm_plane *plane;
185 struct drm_encoder *encoder;
186 struct drm_connector *connector;
187 struct drm_connector_list_iter conn_iter;
189 drm_for_each_plane(plane, dev)
190 if (plane->funcs->reset)
191 plane->funcs->reset(plane);
193 drm_for_each_crtc(crtc, dev)
194 if (crtc->funcs->reset)
195 crtc->funcs->reset(crtc);
197 drm_for_each_encoder(encoder, dev)
198 if (encoder->funcs->reset)
199 encoder->funcs->reset(encoder);
201 drm_connector_list_iter_begin(dev, &conn_iter);
202 drm_for_each_connector_iter(connector, &conn_iter)
203 if (connector->funcs->reset)
204 connector->funcs->reset(connector);
205 drm_connector_list_iter_end(&conn_iter);
207 EXPORT_SYMBOL(drm_mode_config_reset);
212 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
213 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
214 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
215 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
218 static int drm_mode_create_standard_properties(struct drm_device *dev)
220 struct drm_property *prop;
223 ret = drm_connector_create_standard_properties(dev);
227 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
228 "type", drm_plane_type_enum_list,
229 ARRAY_SIZE(drm_plane_type_enum_list));
232 dev->mode_config.plane_type_property = prop;
234 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
235 "SRC_X", 0, UINT_MAX);
238 dev->mode_config.prop_src_x = prop;
240 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
241 "SRC_Y", 0, UINT_MAX);
244 dev->mode_config.prop_src_y = prop;
246 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
247 "SRC_W", 0, UINT_MAX);
250 dev->mode_config.prop_src_w = prop;
252 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
253 "SRC_H", 0, UINT_MAX);
256 dev->mode_config.prop_src_h = prop;
258 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
259 "CRTC_X", INT_MIN, INT_MAX);
262 dev->mode_config.prop_crtc_x = prop;
264 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
265 "CRTC_Y", INT_MIN, INT_MAX);
268 dev->mode_config.prop_crtc_y = prop;
270 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
271 "CRTC_W", 0, INT_MAX);
274 dev->mode_config.prop_crtc_w = prop;
276 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
277 "CRTC_H", 0, INT_MAX);
280 dev->mode_config.prop_crtc_h = prop;
282 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
283 "FB_ID", DRM_MODE_OBJECT_FB);
286 dev->mode_config.prop_fb_id = prop;
288 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
289 "IN_FENCE_FD", -1, INT_MAX);
292 dev->mode_config.prop_in_fence_fd = prop;
294 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
295 "OUT_FENCE_PTR", 0, U64_MAX);
298 dev->mode_config.prop_out_fence_ptr = prop;
300 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
301 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
304 dev->mode_config.prop_crtc_id = prop;
306 prop = drm_property_create(dev,
307 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
308 "FB_DAMAGE_CLIPS", 0);
311 dev->mode_config.prop_fb_damage_clips = prop;
313 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
317 dev->mode_config.prop_active = prop;
319 prop = drm_property_create(dev,
320 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
324 dev->mode_config.prop_mode_id = prop;
326 prop = drm_property_create_bool(dev, 0,
330 dev->mode_config.prop_vrr_enabled = prop;
332 prop = drm_property_create(dev,
337 dev->mode_config.degamma_lut_property = prop;
339 prop = drm_property_create_range(dev,
340 DRM_MODE_PROP_IMMUTABLE,
341 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
344 dev->mode_config.degamma_lut_size_property = prop;
346 prop = drm_property_create(dev,
351 dev->mode_config.ctm_property = prop;
353 prop = drm_property_create(dev,
358 dev->mode_config.gamma_lut_property = prop;
360 prop = drm_property_create_range(dev,
361 DRM_MODE_PROP_IMMUTABLE,
362 "GAMMA_LUT_SIZE", 0, UINT_MAX);
365 dev->mode_config.gamma_lut_size_property = prop;
367 prop = drm_property_create(dev,
368 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
372 dev->mode_config.modifiers_property = prop;
377 static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
379 drm_mode_config_cleanup(dev);
383 * drmm_mode_config_init - managed DRM mode_configuration structure
387 * Initialize @dev's mode_config structure, used for tracking the graphics
388 * configuration of @dev.
390 * Since this initializes the modeset locks, no locking is possible. Which is no
391 * problem, since this should happen single threaded at init time. It is the
392 * driver's problem to ensure this guarantee.
394 * Cleanup is automatically handled through registering drm_mode_config_cleanup
395 * with drmm_add_action().
397 * Returns: 0 on success, negative error value on failure.
399 int drmm_mode_config_init(struct drm_device *dev)
403 mutex_init(&dev->mode_config.mutex);
404 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
405 mutex_init(&dev->mode_config.idr_mutex);
406 mutex_init(&dev->mode_config.fb_lock);
407 mutex_init(&dev->mode_config.blob_lock);
408 INIT_LIST_HEAD(&dev->mode_config.fb_list);
409 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
410 INIT_LIST_HEAD(&dev->mode_config.connector_list);
411 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
412 INIT_LIST_HEAD(&dev->mode_config.property_list);
413 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
414 INIT_LIST_HEAD(&dev->mode_config.plane_list);
415 INIT_LIST_HEAD(&dev->mode_config.privobj_list);
416 idr_init(&dev->mode_config.object_idr);
417 idr_init(&dev->mode_config.tile_idr);
418 ida_init(&dev->mode_config.connector_ida);
419 spin_lock_init(&dev->mode_config.connector_list_lock);
421 init_llist_head(&dev->mode_config.connector_free_list);
422 INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
424 ret = drm_mode_create_standard_properties(dev);
426 drm_mode_config_cleanup(dev);
430 /* Just to be sure */
431 dev->mode_config.num_fb = 0;
432 dev->mode_config.num_connector = 0;
433 dev->mode_config.num_crtc = 0;
434 dev->mode_config.num_encoder = 0;
435 dev->mode_config.num_total_plane = 0;
437 if (IS_ENABLED(CONFIG_LOCKDEP)) {
438 struct drm_modeset_acquire_ctx modeset_ctx;
439 struct ww_acquire_ctx resv_ctx;
440 struct dma_resv resv;
443 dma_resv_init(&resv);
445 drm_modeset_acquire_init(&modeset_ctx, 0);
446 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
449 ret = drm_modeset_backoff(&modeset_ctx);
451 ww_acquire_init(&resv_ctx, &reservation_ww_class);
452 ret = dma_resv_lock(&resv, &resv_ctx);
454 dma_resv_lock_slow(&resv, &resv_ctx);
456 dma_resv_unlock(&resv);
457 ww_acquire_fini(&resv_ctx);
459 drm_modeset_drop_locks(&modeset_ctx);
460 drm_modeset_acquire_fini(&modeset_ctx);
461 dma_resv_fini(&resv);
464 return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
467 EXPORT_SYMBOL(drmm_mode_config_init);
470 * drm_mode_config_cleanup - free up DRM mode_config info
473 * Free up all the connectors and CRTCs associated with this DRM device, then
474 * free up the framebuffers and associated buffer objects.
476 * Note that since this /should/ happen single-threaded at driver/device
477 * teardown time, no locking is required. It's the driver's job to ensure that
478 * this guarantee actually holds true.
480 * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
481 * drivers to explicitly call this function.
483 void drm_mode_config_cleanup(struct drm_device *dev)
485 struct drm_connector *connector;
486 struct drm_connector_list_iter conn_iter;
487 struct drm_crtc *crtc, *ct;
488 struct drm_encoder *encoder, *enct;
489 struct drm_framebuffer *fb, *fbt;
490 struct drm_property *property, *pt;
491 struct drm_property_blob *blob, *bt;
492 struct drm_plane *plane, *plt;
494 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
496 encoder->funcs->destroy(encoder);
499 drm_connector_list_iter_begin(dev, &conn_iter);
500 drm_for_each_connector_iter(connector, &conn_iter) {
501 /* drm_connector_list_iter holds an full reference to the
502 * current connector itself, which means it is inherently safe
503 * against unreferencing the current connector - but not against
504 * deleting it right away. */
505 drm_connector_put(connector);
507 drm_connector_list_iter_end(&conn_iter);
508 /* connector_iter drops references in a work item. */
509 flush_work(&dev->mode_config.connector_free_work);
510 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
511 drm_connector_list_iter_begin(dev, &conn_iter);
512 drm_for_each_connector_iter(connector, &conn_iter)
513 DRM_ERROR("connector %s leaked!\n", connector->name);
514 drm_connector_list_iter_end(&conn_iter);
517 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
519 drm_property_destroy(dev, property);
522 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
524 plane->funcs->destroy(plane);
527 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
528 crtc->funcs->destroy(crtc);
531 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
533 drm_property_blob_put(blob);
537 * Single-threaded teardown context, so it's not required to grab the
538 * fb_lock to protect against concurrent fb_list access. Contrary, it
539 * would actually deadlock with the drm_framebuffer_cleanup function.
541 * Also, if there are any framebuffers left, that's a driver leak now,
542 * so politely WARN about this.
544 WARN_ON(!list_empty(&dev->mode_config.fb_list));
545 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
546 struct drm_printer p = drm_debug_printer("[leaked fb]");
548 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
549 drm_framebuffer_print_info(&p, 1, fb);
550 drm_framebuffer_free(&fb->base.refcount);
553 ida_destroy(&dev->mode_config.connector_ida);
554 idr_destroy(&dev->mode_config.tile_idr);
555 idr_destroy(&dev->mode_config.object_idr);
556 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
558 EXPORT_SYMBOL(drm_mode_config_cleanup);
560 static u32 full_encoder_mask(struct drm_device *dev)
562 struct drm_encoder *encoder;
563 u32 encoder_mask = 0;
565 drm_for_each_encoder(encoder, dev)
566 encoder_mask |= drm_encoder_mask(encoder);
572 * For some reason we want the encoder itself included in
573 * possible_clones. Make life easy for drivers by allowing them
574 * to leave possible_clones unset if no cloning is possible.
576 static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
578 if (encoder->possible_clones == 0)
579 encoder->possible_clones = drm_encoder_mask(encoder);
582 static void validate_encoder_possible_clones(struct drm_encoder *encoder)
584 struct drm_device *dev = encoder->dev;
585 u32 encoder_mask = full_encoder_mask(dev);
586 struct drm_encoder *other;
588 drm_for_each_encoder(other, dev) {
589 WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
590 !!(other->possible_clones & drm_encoder_mask(encoder)),
591 "possible_clones mismatch: "
592 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
593 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
594 encoder->base.id, encoder->name,
595 drm_encoder_mask(encoder), encoder->possible_clones,
596 other->base.id, other->name,
597 drm_encoder_mask(other), other->possible_clones);
600 WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
601 (encoder->possible_clones & ~encoder_mask) != 0,
602 "Bogus possible_clones: "
603 "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
604 encoder->base.id, encoder->name,
605 encoder->possible_clones, encoder_mask);
608 static u32 full_crtc_mask(struct drm_device *dev)
610 struct drm_crtc *crtc;
613 drm_for_each_crtc(crtc, dev)
614 crtc_mask |= drm_crtc_mask(crtc);
619 static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
621 u32 crtc_mask = full_crtc_mask(encoder->dev);
623 WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
624 (encoder->possible_crtcs & ~crtc_mask) != 0,
625 "Bogus possible_crtcs: "
626 "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
627 encoder->base.id, encoder->name,
628 encoder->possible_crtcs, crtc_mask);
631 void drm_mode_config_validate(struct drm_device *dev)
633 struct drm_encoder *encoder;
635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
638 drm_for_each_encoder(encoder, dev)
639 fixup_encoder_possible_clones(encoder);
641 drm_for_each_encoder(encoder, dev) {
642 validate_encoder_possible_clones(encoder);
643 validate_encoder_possible_crtcs(encoder);