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