Home | History | Annotate | Line # | Download | only in drm
drm_mode_config.c revision 1.1
      1 /*	$NetBSD: drm_mode_config.c,v 1.1 2021/12/18 20:11:03 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2016 Intel Corporation
      5  *
      6  * Permission to use, copy, modify, distribute, and sell this software and its
      7  * documentation for any purpose is hereby granted without fee, provided that
      8  * the above copyright notice appear in all copies and that both that copyright
      9  * notice and this permission notice appear in supporting documentation, and
     10  * that the name of the copyright holders not be used in advertising or
     11  * publicity pertaining to distribution of the software without specific,
     12  * written prior permission.  The copyright holders make no representations
     13  * about the suitability of this software for any purpose.  It is provided "as
     14  * is" without express or implied warranty.
     15  *
     16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     18  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     22  * OF THIS SOFTWARE.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: drm_mode_config.c,v 1.1 2021/12/18 20:11:03 riastradh Exp $");
     27 
     28 #include <linux/uaccess.h>
     29 
     30 #include <drm/drm_drv.h>
     31 #include <drm/drm_encoder.h>
     32 #include <drm/drm_file.h>
     33 #include <drm/drm_mode_config.h>
     34 #include <drm/drm_print.h>
     35 #include <linux/dma-resv.h>
     36 
     37 #include "drm_crtc_internal.h"
     38 #include "drm_internal.h"
     39 
     40 int drm_modeset_register_all(struct drm_device *dev)
     41 {
     42 	int ret;
     43 
     44 	ret = drm_plane_register_all(dev);
     45 	if (ret)
     46 		goto err_plane;
     47 
     48 	ret = drm_crtc_register_all(dev);
     49 	if  (ret)
     50 		goto err_crtc;
     51 
     52 	ret = drm_encoder_register_all(dev);
     53 	if (ret)
     54 		goto err_encoder;
     55 
     56 	ret = drm_connector_register_all(dev);
     57 	if (ret)
     58 		goto err_connector;
     59 
     60 	return 0;
     61 
     62 err_connector:
     63 	drm_encoder_unregister_all(dev);
     64 err_encoder:
     65 	drm_crtc_unregister_all(dev);
     66 err_crtc:
     67 	drm_plane_unregister_all(dev);
     68 err_plane:
     69 	return ret;
     70 }
     71 
     72 void drm_modeset_unregister_all(struct drm_device *dev)
     73 {
     74 	drm_connector_unregister_all(dev);
     75 	drm_encoder_unregister_all(dev);
     76 	drm_crtc_unregister_all(dev);
     77 	drm_plane_unregister_all(dev);
     78 }
     79 
     80 /**
     81  * drm_mode_getresources - get graphics configuration
     82  * @dev: drm device for the ioctl
     83  * @data: data pointer for the ioctl
     84  * @file_priv: drm file for the ioctl call
     85  *
     86  * Construct a set of configuration description structures and return
     87  * them to the user, including CRTC, connector and framebuffer configuration.
     88  *
     89  * Called by the user via ioctl.
     90  *
     91  * Returns:
     92  * Zero on success, negative errno on failure.
     93  */
     94 int drm_mode_getresources(struct drm_device *dev, void *data,
     95 			  struct drm_file *file_priv)
     96 {
     97 	struct drm_mode_card_res *card_res = data;
     98 	struct drm_framebuffer *fb;
     99 	struct drm_connector *connector;
    100 	struct drm_crtc *crtc;
    101 	struct drm_encoder *encoder;
    102 	int count, ret = 0;
    103 	uint32_t __user *fb_id;
    104 	uint32_t __user *crtc_id;
    105 	uint32_t __user *connector_id;
    106 	uint32_t __user *encoder_id;
    107 	struct drm_connector_list_iter conn_iter;
    108 
    109 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
    110 		return -EOPNOTSUPP;
    111 
    112 	mutex_lock(&file_priv->fbs_lock);
    113 	count = 0;
    114 	fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
    115 	list_for_each_entry(fb, &file_priv->fbs, filp_head) {
    116 		if (count < card_res->count_fbs &&
    117 		    put_user(fb->base.id, fb_id + count)) {
    118 			mutex_unlock(&file_priv->fbs_lock);
    119 			return -EFAULT;
    120 		}
    121 		count++;
    122 	}
    123 	card_res->count_fbs = count;
    124 	mutex_unlock(&file_priv->fbs_lock);
    125 
    126 	card_res->max_height = dev->mode_config.max_height;
    127 	card_res->min_height = dev->mode_config.min_height;
    128 	card_res->max_width = dev->mode_config.max_width;
    129 	card_res->min_width = dev->mode_config.min_width;
    130 
    131 	count = 0;
    132 	crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
    133 	drm_for_each_crtc(crtc, dev) {
    134 		if (drm_lease_held(file_priv, crtc->base.id)) {
    135 			if (count < card_res->count_crtcs &&
    136 			    put_user(crtc->base.id, crtc_id + count))
    137 				return -EFAULT;
    138 			count++;
    139 		}
    140 	}
    141 	card_res->count_crtcs = count;
    142 
    143 	count = 0;
    144 	encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
    145 	drm_for_each_encoder(encoder, dev) {
    146 		if (count < card_res->count_encoders &&
    147 		    put_user(encoder->base.id, encoder_id + count))
    148 			return -EFAULT;
    149 		count++;
    150 	}
    151 	card_res->count_encoders = count;
    152 
    153 	drm_connector_list_iter_begin(dev, &conn_iter);
    154 	count = 0;
    155 	connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
    156 	drm_for_each_connector_iter(connector, &conn_iter) {
    157 		/* only expose writeback connectors if userspace understands them */
    158 		if (!file_priv->writeback_connectors &&
    159 		    (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
    160 			continue;
    161 
    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);
    166 				return -EFAULT;
    167 			}
    168 			count++;
    169 		}
    170 	}
    171 	card_res->count_connectors = count;
    172 	drm_connector_list_iter_end(&conn_iter);
    173 
    174 	return ret;
    175 }
    176 
    177 /**
    178  * drm_mode_config_reset - call ->reset callbacks
    179  * @dev: drm device
    180  *
    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.
    184  */
    185 void drm_mode_config_reset(struct drm_device *dev)
    186 {
    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;
    192 
    193 	drm_for_each_plane(plane, dev)
    194 		if (plane->funcs->reset)
    195 			plane->funcs->reset(plane);
    196 
    197 	drm_for_each_crtc(crtc, dev)
    198 		if (crtc->funcs->reset)
    199 			crtc->funcs->reset(crtc);
    200 
    201 	drm_for_each_encoder(encoder, dev)
    202 		if (encoder->funcs->reset)
    203 			encoder->funcs->reset(encoder);
    204 
    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);
    210 }
    211 EXPORT_SYMBOL(drm_mode_config_reset);
    212 
    213 /*
    214  * Global properties
    215  */
    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" },
    220 };
    221 
    222 static int drm_mode_create_standard_properties(struct drm_device *dev)
    223 {
    224 	struct drm_property *prop;
    225 	int ret;
    226 
    227 	ret = drm_connector_create_standard_properties(dev);
    228 	if (ret)
    229 		return ret;
    230 
    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));
    234 	if (!prop)
    235 		return -ENOMEM;
    236 	dev->mode_config.plane_type_property = prop;
    237 
    238 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    239 			"SRC_X", 0, UINT_MAX);
    240 	if (!prop)
    241 		return -ENOMEM;
    242 	dev->mode_config.prop_src_x = prop;
    243 
    244 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    245 			"SRC_Y", 0, UINT_MAX);
    246 	if (!prop)
    247 		return -ENOMEM;
    248 	dev->mode_config.prop_src_y = prop;
    249 
    250 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    251 			"SRC_W", 0, UINT_MAX);
    252 	if (!prop)
    253 		return -ENOMEM;
    254 	dev->mode_config.prop_src_w = prop;
    255 
    256 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    257 			"SRC_H", 0, UINT_MAX);
    258 	if (!prop)
    259 		return -ENOMEM;
    260 	dev->mode_config.prop_src_h = prop;
    261 
    262 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
    263 			"CRTC_X", INT_MIN, INT_MAX);
    264 	if (!prop)
    265 		return -ENOMEM;
    266 	dev->mode_config.prop_crtc_x = prop;
    267 
    268 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
    269 			"CRTC_Y", INT_MIN, INT_MAX);
    270 	if (!prop)
    271 		return -ENOMEM;
    272 	dev->mode_config.prop_crtc_y = prop;
    273 
    274 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    275 			"CRTC_W", 0, INT_MAX);
    276 	if (!prop)
    277 		return -ENOMEM;
    278 	dev->mode_config.prop_crtc_w = prop;
    279 
    280 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    281 			"CRTC_H", 0, INT_MAX);
    282 	if (!prop)
    283 		return -ENOMEM;
    284 	dev->mode_config.prop_crtc_h = prop;
    285 
    286 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
    287 			"FB_ID", DRM_MODE_OBJECT_FB);
    288 	if (!prop)
    289 		return -ENOMEM;
    290 	dev->mode_config.prop_fb_id = prop;
    291 
    292 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
    293 			"IN_FENCE_FD", -1, INT_MAX);
    294 	if (!prop)
    295 		return -ENOMEM;
    296 	dev->mode_config.prop_in_fence_fd = prop;
    297 
    298 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
    299 			"OUT_FENCE_PTR", 0, U64_MAX);
    300 	if (!prop)
    301 		return -ENOMEM;
    302 	dev->mode_config.prop_out_fence_ptr = prop;
    303 
    304 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
    305 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
    306 	if (!prop)
    307 		return -ENOMEM;
    308 	dev->mode_config.prop_crtc_id = prop;
    309 
    310 	prop = drm_property_create(dev,
    311 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
    312 			"FB_DAMAGE_CLIPS", 0);
    313 	if (!prop)
    314 		return -ENOMEM;
    315 	dev->mode_config.prop_fb_damage_clips = prop;
    316 
    317 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
    318 			"ACTIVE");
    319 	if (!prop)
    320 		return -ENOMEM;
    321 	dev->mode_config.prop_active = prop;
    322 
    323 	prop = drm_property_create(dev,
    324 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
    325 			"MODE_ID", 0);
    326 	if (!prop)
    327 		return -ENOMEM;
    328 	dev->mode_config.prop_mode_id = prop;
    329 
    330 	prop = drm_property_create_bool(dev, 0,
    331 			"VRR_ENABLED");
    332 	if (!prop)
    333 		return -ENOMEM;
    334 	dev->mode_config.prop_vrr_enabled = prop;
    335 
    336 	prop = drm_property_create(dev,
    337 			DRM_MODE_PROP_BLOB,
    338 			"DEGAMMA_LUT", 0);
    339 	if (!prop)
    340 		return -ENOMEM;
    341 	dev->mode_config.degamma_lut_property = prop;
    342 
    343 	prop = drm_property_create_range(dev,
    344 			DRM_MODE_PROP_IMMUTABLE,
    345 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
    346 	if (!prop)
    347 		return -ENOMEM;
    348 	dev->mode_config.degamma_lut_size_property = prop;
    349 
    350 	prop = drm_property_create(dev,
    351 			DRM_MODE_PROP_BLOB,
    352 			"CTM", 0);
    353 	if (!prop)
    354 		return -ENOMEM;
    355 	dev->mode_config.ctm_property = prop;
    356 
    357 	prop = drm_property_create(dev,
    358 			DRM_MODE_PROP_BLOB,
    359 			"GAMMA_LUT", 0);
    360 	if (!prop)
    361 		return -ENOMEM;
    362 	dev->mode_config.gamma_lut_property = prop;
    363 
    364 	prop = drm_property_create_range(dev,
    365 			DRM_MODE_PROP_IMMUTABLE,
    366 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
    367 	if (!prop)
    368 		return -ENOMEM;
    369 	dev->mode_config.gamma_lut_size_property = prop;
    370 
    371 	prop = drm_property_create(dev,
    372 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
    373 				   "IN_FORMATS", 0);
    374 	if (!prop)
    375 		return -ENOMEM;
    376 	dev->mode_config.modifiers_property = prop;
    377 
    378 	return 0;
    379 }
    380 
    381 /**
    382  * drm_mode_config_init - initialize DRM mode_configuration structure
    383  * @dev: DRM device
    384  *
    385  * Initialize @dev's mode_config structure, used for tracking the graphics
    386  * configuration of @dev.
    387  *
    388  * Since this initializes the modeset locks, no locking is possible. Which is no
    389  * problem, since this should happen single threaded at init time. It is the
    390  * driver's problem to ensure this guarantee.
    391  *
    392  */
    393 void drm_mode_config_init(struct drm_device *dev)
    394 {
    395 	mutex_init(&dev->mode_config.mutex);
    396 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
    397 	mutex_init(&dev->mode_config.idr_mutex);
    398 	mutex_init(&dev->mode_config.fb_lock);
    399 	mutex_init(&dev->mode_config.blob_lock);
    400 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
    401 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
    402 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
    403 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
    404 	INIT_LIST_HEAD(&dev->mode_config.property_list);
    405 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
    406 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
    407 	INIT_LIST_HEAD(&dev->mode_config.privobj_list);
    408 	idr_init(&dev->mode_config.object_idr);
    409 	idr_init(&dev->mode_config.tile_idr);
    410 	ida_init(&dev->mode_config.connector_ida);
    411 	spin_lock_init(&dev->mode_config.connector_list_lock);
    412 
    413 	init_llist_head(&dev->mode_config.connector_free_list);
    414 	INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
    415 
    416 	drm_mode_create_standard_properties(dev);
    417 
    418 	/* Just to be sure */
    419 	dev->mode_config.num_fb = 0;
    420 	dev->mode_config.num_connector = 0;
    421 	dev->mode_config.num_crtc = 0;
    422 	dev->mode_config.num_encoder = 0;
    423 	dev->mode_config.num_total_plane = 0;
    424 
    425 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
    426 		struct drm_modeset_acquire_ctx modeset_ctx;
    427 		struct ww_acquire_ctx resv_ctx;
    428 		struct dma_resv resv;
    429 		int ret;
    430 
    431 		dma_resv_init(&resv);
    432 
    433 		drm_modeset_acquire_init(&modeset_ctx, 0);
    434 		ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
    435 				       &modeset_ctx);
    436 		if (ret == -EDEADLK)
    437 			ret = drm_modeset_backoff(&modeset_ctx);
    438 
    439 		ww_acquire_init(&resv_ctx, &reservation_ww_class);
    440 		ret = dma_resv_lock(&resv, &resv_ctx);
    441 		if (ret == -EDEADLK)
    442 			dma_resv_lock_slow(&resv, &resv_ctx);
    443 
    444 		dma_resv_unlock(&resv);
    445 		ww_acquire_fini(&resv_ctx);
    446 
    447 		drm_modeset_drop_locks(&modeset_ctx);
    448 		drm_modeset_acquire_fini(&modeset_ctx);
    449 		dma_resv_fini(&resv);
    450 	}
    451 }
    452 EXPORT_SYMBOL(drm_mode_config_init);
    453 
    454 /**
    455  * drm_mode_config_cleanup - free up DRM mode_config info
    456  * @dev: DRM device
    457  *
    458  * Free up all the connectors and CRTCs associated with this DRM device, then
    459  * free up the framebuffers and associated buffer objects.
    460  *
    461  * Note that since this /should/ happen single-threaded at driver/device
    462  * teardown time, no locking is required. It's the driver's job to ensure that
    463  * this guarantee actually holds true.
    464  */
    465 void drm_mode_config_cleanup(struct drm_device *dev)
    466 {
    467 	struct drm_connector *connector;
    468 	struct drm_connector_list_iter conn_iter;
    469 	struct drm_crtc *crtc, *ct;
    470 	struct drm_encoder *encoder, *enct;
    471 	struct drm_framebuffer *fb, *fbt;
    472 	struct drm_property *property, *pt;
    473 	struct drm_property_blob *blob, *bt;
    474 	struct drm_plane *plane, *plt;
    475 
    476 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
    477 				 head) {
    478 		encoder->funcs->destroy(encoder);
    479 	}
    480 
    481 	drm_connector_list_iter_begin(dev, &conn_iter);
    482 	drm_for_each_connector_iter(connector, &conn_iter) {
    483 		/* drm_connector_list_iter holds an full reference to the
    484 		 * current connector itself, which means it is inherently safe
    485 		 * against unreferencing the current connector - but not against
    486 		 * deleting it right away. */
    487 		drm_connector_put(connector);
    488 	}
    489 	drm_connector_list_iter_end(&conn_iter);
    490 	/* connector_iter drops references in a work item. */
    491 	flush_work(&dev->mode_config.connector_free_work);
    492 	if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
    493 		drm_connector_list_iter_begin(dev, &conn_iter);
    494 		drm_for_each_connector_iter(connector, &conn_iter)
    495 			DRM_ERROR("connector %s leaked!\n", connector->name);
    496 		drm_connector_list_iter_end(&conn_iter);
    497 	}
    498 
    499 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
    500 				 head) {
    501 		drm_property_destroy(dev, property);
    502 	}
    503 
    504 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
    505 				 head) {
    506 		plane->funcs->destroy(plane);
    507 	}
    508 
    509 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
    510 		crtc->funcs->destroy(crtc);
    511 	}
    512 
    513 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
    514 				 head_global) {
    515 		drm_property_blob_put(blob);
    516 	}
    517 
    518 	/*
    519 	 * Single-threaded teardown context, so it's not required to grab the
    520 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
    521 	 * would actually deadlock with the drm_framebuffer_cleanup function.
    522 	 *
    523 	 * Also, if there are any framebuffers left, that's a driver leak now,
    524 	 * so politely WARN about this.
    525 	 */
    526 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
    527 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
    528 		struct drm_printer p = drm_debug_printer("[leaked fb]");
    529 		drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
    530 		drm_framebuffer_print_info(&p, 1, fb);
    531 		drm_framebuffer_free(&fb->base.refcount);
    532 	}
    533 
    534 	ida_destroy(&dev->mode_config.connector_ida);
    535 	idr_destroy(&dev->mode_config.tile_idr);
    536 	idr_destroy(&dev->mode_config.object_idr);
    537 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
    538 }
    539 EXPORT_SYMBOL(drm_mode_config_cleanup);
    540