xref: /linux/drivers/gpu/drm/drm_mode_config.c (revision a0c83177734ab98623795e1ba2cf4b72c23de5e7)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <linux/export.h>
24 #include <linux/uaccess.h>
25 
26 #include <drm/drm_drv.h>
27 #include <drm/drm_encoder.h>
28 #include <drm/drm_file.h>
29 #include <drm/drm_framebuffer.h>
30 #include <drm/drm_managed.h>
31 #include <drm/drm_mode_config.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_colorop.h>
34 #include <linux/dma-resv.h>
35 
36 #include "drm_crtc_internal.h"
37 #include "drm_internal.h"
38 
drm_modeset_register_all(struct drm_device * dev)39 int drm_modeset_register_all(struct drm_device *dev)
40 {
41 	int ret;
42 
43 	ret = drm_plane_register_all(dev);
44 	if (ret)
45 		goto err_plane;
46 
47 	ret = drm_crtc_register_all(dev);
48 	if  (ret)
49 		goto err_crtc;
50 
51 	ret = drm_encoder_register_all(dev);
52 	if (ret)
53 		goto err_encoder;
54 
55 	ret = drm_connector_register_all(dev);
56 	if (ret)
57 		goto err_connector;
58 
59 	return 0;
60 
61 err_connector:
62 	drm_encoder_unregister_all(dev);
63 err_encoder:
64 	drm_crtc_unregister_all(dev);
65 err_crtc:
66 	drm_plane_unregister_all(dev);
67 err_plane:
68 	return ret;
69 }
70 
drm_modeset_unregister_all(struct drm_device * dev)71 void drm_modeset_unregister_all(struct drm_device *dev)
72 {
73 	drm_connector_unregister_all(dev);
74 	drm_encoder_unregister_all(dev);
75 	drm_crtc_unregister_all(dev);
76 	drm_plane_unregister_all(dev);
77 }
78 
79 /**
80  * drm_mode_getresources - get graphics configuration
81  * @dev: drm device for the ioctl
82  * @data: data pointer for the ioctl
83  * @file_priv: drm file for the ioctl call
84  *
85  * Construct a set of configuration description structures and return
86  * them to the user, including CRTC, connector and framebuffer configuration.
87  *
88  * Called by the user via ioctl.
89  *
90  * Returns:
91  * Zero on success, negative errno on failure.
92  */
drm_mode_getresources(struct drm_device * dev,void * data,struct drm_file * file_priv)93 int drm_mode_getresources(struct drm_device *dev, void *data,
94 			  struct drm_file *file_priv)
95 {
96 	struct drm_mode_card_res *card_res = data;
97 	struct drm_framebuffer *fb;
98 	struct drm_connector *connector;
99 	struct drm_crtc *crtc;
100 	struct drm_encoder *encoder;
101 	int count, ret = 0;
102 	uint32_t __user *fb_id;
103 	uint32_t __user *crtc_id;
104 	uint32_t __user *connector_id;
105 	uint32_t __user *encoder_id;
106 	struct drm_connector_list_iter conn_iter;
107 
108 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
109 		return -EOPNOTSUPP;
110 
111 	mutex_lock(&file_priv->fbs_lock);
112 	count = 0;
113 	fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
114 	list_for_each_entry(fb, &file_priv->fbs, filp_head) {
115 		if (count < card_res->count_fbs &&
116 		    put_user(fb->base.id, fb_id + count)) {
117 			mutex_unlock(&file_priv->fbs_lock);
118 			return -EFAULT;
119 		}
120 		count++;
121 	}
122 	card_res->count_fbs = count;
123 	mutex_unlock(&file_priv->fbs_lock);
124 
125 	card_res->max_height = dev->mode_config.max_height;
126 	card_res->min_height = dev->mode_config.min_height;
127 	card_res->max_width = dev->mode_config.max_width;
128 	card_res->min_width = dev->mode_config.min_width;
129 
130 	count = 0;
131 	crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
132 	drm_for_each_crtc(crtc, dev) {
133 		if (drm_lease_held(file_priv, crtc->base.id)) {
134 			if (count < card_res->count_crtcs &&
135 			    put_user(crtc->base.id, crtc_id + count))
136 				return -EFAULT;
137 			count++;
138 		}
139 	}
140 	card_res->count_crtcs = count;
141 
142 	count = 0;
143 	encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
144 	drm_for_each_encoder(encoder, dev) {
145 		if (count < card_res->count_encoders &&
146 		    put_user(encoder->base.id, encoder_id + count))
147 			return -EFAULT;
148 		count++;
149 	}
150 	card_res->count_encoders = count;
151 
152 	drm_connector_list_iter_begin(dev, &conn_iter);
153 	count = 0;
154 	connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
155 	/*
156 	 * FIXME: the connectors on the list may not be fully initialized yet,
157 	 * if the ioctl is called before the connectors are registered. (See
158 	 * drm_dev_register()->drm_modeset_register_all() for static and
159 	 * drm_connector_dynamic_register() for dynamic connectors.)
160 	 * The driver should only get registered after static connectors are
161 	 * fully initialized and dynamic connectors should be added to the
162 	 * connector list only after fully initializing them.
163 	 */
164 	drm_for_each_connector_iter(connector, &conn_iter) {
165 		/* only expose writeback connectors if userspace understands them */
166 		if (!file_priv->writeback_connectors &&
167 		    (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
168 			continue;
169 
170 		if (drm_lease_held(file_priv, connector->base.id)) {
171 			if (count < card_res->count_connectors &&
172 			    put_user(connector->base.id, connector_id + count)) {
173 				drm_connector_list_iter_end(&conn_iter);
174 				return -EFAULT;
175 			}
176 			count++;
177 		}
178 	}
179 	card_res->count_connectors = count;
180 	drm_connector_list_iter_end(&conn_iter);
181 
182 	return ret;
183 }
184 
185 /**
186  * drm_mode_config_reset - call ->reset callbacks
187  * @dev: drm device
188  *
189  * This functions calls all the crtc's, encoder's and connector's ->reset
190  * callback. Drivers can use this in e.g. their driver load or resume code to
191  * reset hardware and software state.
192  */
drm_mode_config_reset(struct drm_device * dev)193 void drm_mode_config_reset(struct drm_device *dev)
194 {
195 	struct drm_crtc *crtc;
196 	struct drm_colorop *colorop;
197 	struct drm_plane *plane;
198 	struct drm_encoder *encoder;
199 	struct drm_connector *connector;
200 	struct drm_connector_list_iter conn_iter;
201 
202 	drm_for_each_colorop(colorop, dev)
203 		drm_colorop_reset(colorop);
204 
205 	drm_for_each_plane(plane, dev)
206 		if (plane->funcs->reset)
207 			plane->funcs->reset(plane);
208 
209 	drm_for_each_crtc(crtc, dev)
210 		if (crtc->funcs->reset)
211 			crtc->funcs->reset(crtc);
212 
213 	drm_for_each_encoder(encoder, dev)
214 		if (encoder->funcs && encoder->funcs->reset)
215 			encoder->funcs->reset(encoder);
216 
217 	drm_connector_list_iter_begin(dev, &conn_iter);
218 	drm_for_each_connector_iter(connector, &conn_iter)
219 		if (connector->funcs->reset)
220 			connector->funcs->reset(connector);
221 	drm_connector_list_iter_end(&conn_iter);
222 }
223 EXPORT_SYMBOL(drm_mode_config_reset);
224 
225 /*
226  * Global properties
227  */
228 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
229 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
230 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
231 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
232 };
233 
drm_mode_create_standard_properties(struct drm_device * dev)234 static int drm_mode_create_standard_properties(struct drm_device *dev)
235 {
236 	struct drm_property *prop;
237 	int ret;
238 
239 	ret = drm_connector_create_standard_properties(dev);
240 	if (ret)
241 		return ret;
242 
243 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
244 					"type", drm_plane_type_enum_list,
245 					ARRAY_SIZE(drm_plane_type_enum_list));
246 	if (!prop)
247 		return -ENOMEM;
248 	dev->mode_config.plane_type_property = prop;
249 
250 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
251 			"SRC_X", 0, UINT_MAX);
252 	if (!prop)
253 		return -ENOMEM;
254 	dev->mode_config.prop_src_x = prop;
255 
256 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
257 			"SRC_Y", 0, UINT_MAX);
258 	if (!prop)
259 		return -ENOMEM;
260 	dev->mode_config.prop_src_y = prop;
261 
262 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
263 			"SRC_W", 0, UINT_MAX);
264 	if (!prop)
265 		return -ENOMEM;
266 	dev->mode_config.prop_src_w = prop;
267 
268 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
269 			"SRC_H", 0, UINT_MAX);
270 	if (!prop)
271 		return -ENOMEM;
272 	dev->mode_config.prop_src_h = prop;
273 
274 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
275 			"CRTC_X", INT_MIN, INT_MAX);
276 	if (!prop)
277 		return -ENOMEM;
278 	dev->mode_config.prop_crtc_x = prop;
279 
280 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
281 			"CRTC_Y", INT_MIN, INT_MAX);
282 	if (!prop)
283 		return -ENOMEM;
284 	dev->mode_config.prop_crtc_y = prop;
285 
286 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
287 			"CRTC_W", 0, INT_MAX);
288 	if (!prop)
289 		return -ENOMEM;
290 	dev->mode_config.prop_crtc_w = prop;
291 
292 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
293 			"CRTC_H", 0, INT_MAX);
294 	if (!prop)
295 		return -ENOMEM;
296 	dev->mode_config.prop_crtc_h = prop;
297 
298 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
299 			"FB_ID", DRM_MODE_OBJECT_FB);
300 	if (!prop)
301 		return -ENOMEM;
302 	dev->mode_config.prop_fb_id = prop;
303 
304 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
305 			"IN_FENCE_FD", -1, INT_MAX);
306 	if (!prop)
307 		return -ENOMEM;
308 	dev->mode_config.prop_in_fence_fd = prop;
309 
310 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
311 			"OUT_FENCE_PTR", 0, U64_MAX);
312 	if (!prop)
313 		return -ENOMEM;
314 	dev->mode_config.prop_out_fence_ptr = prop;
315 
316 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
317 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
318 	if (!prop)
319 		return -ENOMEM;
320 	dev->mode_config.prop_crtc_id = prop;
321 
322 	prop = drm_property_create(dev,
323 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
324 			"FB_DAMAGE_CLIPS", 0);
325 	if (!prop)
326 		return -ENOMEM;
327 	dev->mode_config.prop_fb_damage_clips = prop;
328 
329 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
330 			"ACTIVE");
331 	if (!prop)
332 		return -ENOMEM;
333 	dev->mode_config.prop_active = prop;
334 
335 	prop = drm_property_create(dev,
336 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
337 			"MODE_ID", 0);
338 	if (!prop)
339 		return -ENOMEM;
340 	dev->mode_config.prop_mode_id = prop;
341 
342 	prop = drm_property_create_bool(dev, 0,
343 			"VRR_ENABLED");
344 	if (!prop)
345 		return -ENOMEM;
346 	dev->mode_config.prop_vrr_enabled = prop;
347 
348 	prop = drm_property_create(dev,
349 			DRM_MODE_PROP_BLOB,
350 			"DEGAMMA_LUT", 0);
351 	if (!prop)
352 		return -ENOMEM;
353 	dev->mode_config.degamma_lut_property = prop;
354 
355 	prop = drm_property_create_range(dev,
356 			DRM_MODE_PROP_IMMUTABLE,
357 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
358 	if (!prop)
359 		return -ENOMEM;
360 	dev->mode_config.degamma_lut_size_property = prop;
361 
362 	prop = drm_property_create(dev,
363 			DRM_MODE_PROP_BLOB,
364 			"CTM", 0);
365 	if (!prop)
366 		return -ENOMEM;
367 	dev->mode_config.ctm_property = prop;
368 
369 	prop = drm_property_create(dev,
370 			DRM_MODE_PROP_BLOB,
371 			"GAMMA_LUT", 0);
372 	if (!prop)
373 		return -ENOMEM;
374 	dev->mode_config.gamma_lut_property = prop;
375 
376 	prop = drm_property_create_range(dev,
377 			DRM_MODE_PROP_IMMUTABLE,
378 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
379 	if (!prop)
380 		return -ENOMEM;
381 	dev->mode_config.gamma_lut_size_property = prop;
382 
383 	prop = drm_property_create(dev,
384 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
385 				   "IN_FORMATS", 0);
386 	if (!prop)
387 		return -ENOMEM;
388 	dev->mode_config.modifiers_property = prop;
389 
390 	prop = drm_property_create(dev,
391 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
392 				   "IN_FORMATS_ASYNC", 0);
393 	if (!prop)
394 		return -ENOMEM;
395 	dev->mode_config.async_modifiers_property = prop;
396 
397 	prop = drm_property_create(dev,
398 				   DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
399 				   "SIZE_HINTS", 0);
400 	if (!prop)
401 		return -ENOMEM;
402 	dev->mode_config.size_hints_property = prop;
403 
404 	return 0;
405 }
406 
drm_mode_config_init_release(struct drm_device * dev,void * ptr)407 static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
408 {
409 	drm_mode_config_cleanup(dev);
410 }
411 
412 /**
413  * drmm_mode_config_init - managed DRM mode_configuration structure
414  * 	initialization
415  * @dev: DRM device
416  *
417  * Initialize @dev's mode_config structure, used for tracking the graphics
418  * configuration of @dev.
419  *
420  * Since this initializes the modeset locks, no locking is possible. Which is no
421  * problem, since this should happen single threaded at init time. It is the
422  * driver's problem to ensure this guarantee.
423  *
424  * Cleanup is automatically handled through registering drm_mode_config_cleanup
425  * with drmm_add_action().
426  *
427  * Returns: 0 on success, negative error value on failure.
428  */
drmm_mode_config_init(struct drm_device * dev)429 int drmm_mode_config_init(struct drm_device *dev)
430 {
431 	int ret;
432 
433 	mutex_init(&dev->mode_config.mutex);
434 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
435 	mutex_init(&dev->mode_config.idr_mutex);
436 	mutex_init(&dev->mode_config.fb_lock);
437 	mutex_init(&dev->mode_config.blob_lock);
438 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
439 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
440 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
441 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
442 	INIT_LIST_HEAD(&dev->mode_config.property_list);
443 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
444 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
445 	INIT_LIST_HEAD(&dev->mode_config.colorop_list);
446 	INIT_LIST_HEAD(&dev->mode_config.privobj_list);
447 	idr_init_base(&dev->mode_config.object_idr, 1);
448 	idr_init_base(&dev->mode_config.tile_idr, 1);
449 	ida_init(&dev->mode_config.connector_ida);
450 	spin_lock_init(&dev->mode_config.connector_list_lock);
451 
452 	init_llist_head(&dev->mode_config.connector_free_list);
453 	INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
454 
455 	ret = drm_mode_create_standard_properties(dev);
456 	if (ret) {
457 		drm_mode_config_cleanup(dev);
458 		return ret;
459 	}
460 
461 	/* Just to be sure */
462 	dev->mode_config.num_fb = 0;
463 	dev->mode_config.num_connector = 0;
464 	dev->mode_config.num_crtc = 0;
465 	dev->mode_config.num_encoder = 0;
466 	dev->mode_config.num_total_plane = 0;
467 	dev->mode_config.num_colorop = 0;
468 
469 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
470 		struct drm_modeset_acquire_ctx modeset_ctx;
471 		struct ww_acquire_ctx resv_ctx;
472 		struct dma_resv resv;
473 		int ret;
474 
475 		dma_resv_init(&resv);
476 
477 		drm_modeset_acquire_init(&modeset_ctx, 0);
478 		ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
479 				       &modeset_ctx);
480 		if (ret == -EDEADLK)
481 			ret = drm_modeset_backoff(&modeset_ctx);
482 
483 		might_fault();
484 
485 		ww_acquire_init(&resv_ctx, &reservation_ww_class);
486 		ret = dma_resv_lock(&resv, &resv_ctx);
487 		if (ret == -EDEADLK)
488 			dma_resv_lock_slow(&resv, &resv_ctx);
489 
490 		dma_resv_unlock(&resv);
491 		ww_acquire_fini(&resv_ctx);
492 
493 		drm_modeset_drop_locks(&modeset_ctx);
494 		drm_modeset_acquire_fini(&modeset_ctx);
495 		dma_resv_fini(&resv);
496 	}
497 
498 	return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
499 					NULL);
500 }
501 EXPORT_SYMBOL(drmm_mode_config_init);
502 
503 /**
504  * drm_mode_config_cleanup - free up DRM mode_config info
505  * @dev: DRM device
506  *
507  * Free up all the connectors and CRTCs associated with this DRM device, then
508  * free up the framebuffers and associated buffer objects.
509  *
510  * Note that since this /should/ happen single-threaded at driver/device
511  * teardown time, no locking is required. It's the driver's job to ensure that
512  * this guarantee actually holds true.
513  *
514  * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
515  * drivers to explicitly call this function.
516  */
drm_mode_config_cleanup(struct drm_device * dev)517 void drm_mode_config_cleanup(struct drm_device *dev)
518 {
519 	struct drm_connector *connector;
520 	struct drm_connector_list_iter conn_iter;
521 	struct drm_crtc *crtc, *ct;
522 	struct drm_encoder *encoder, *enct;
523 	struct drm_framebuffer *fb, *fbt;
524 	struct drm_property *property, *pt;
525 	struct drm_property_blob *blob, *bt;
526 	struct drm_plane *plane, *plt;
527 
528 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
529 				 head) {
530 		encoder->funcs->destroy(encoder);
531 	}
532 
533 	drm_connector_list_iter_begin(dev, &conn_iter);
534 	drm_for_each_connector_iter(connector, &conn_iter) {
535 		/* drm_connector_list_iter holds an full reference to the
536 		 * current connector itself, which means it is inherently safe
537 		 * against unreferencing the current connector - but not against
538 		 * deleting it right away. */
539 		drm_connector_put(connector);
540 	}
541 	drm_connector_list_iter_end(&conn_iter);
542 	/* connector_iter drops references in a work item. */
543 	flush_work(&dev->mode_config.connector_free_work);
544 	if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
545 		drm_connector_list_iter_begin(dev, &conn_iter);
546 		drm_for_each_connector_iter(connector, &conn_iter)
547 			DRM_ERROR("connector %s leaked!\n", connector->name);
548 		drm_connector_list_iter_end(&conn_iter);
549 	}
550 
551 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
552 				 head) {
553 		drm_property_destroy(dev, property);
554 	}
555 
556 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
557 				 head) {
558 		plane->funcs->destroy(plane);
559 	}
560 
561 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
562 		crtc->funcs->destroy(crtc);
563 	}
564 
565 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
566 				 head_global) {
567 		drm_property_blob_put(blob);
568 	}
569 
570 	/*
571 	 * Single-threaded teardown context, so it's not required to grab the
572 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
573 	 * would actually deadlock with the drm_framebuffer_cleanup function.
574 	 *
575 	 * Also, if there are any framebuffers left, that's a driver leak now,
576 	 * so politely WARN about this.
577 	 */
578 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
579 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
580 		if (list_empty(&fb->filp_head) || drm_framebuffer_read_refcount(fb) > 1) {
581 			struct drm_printer p = drm_dbg_printer(dev, DRM_UT_KMS, "[leaked fb]");
582 
583 			drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
584 			drm_framebuffer_print_info(&p, 1, fb);
585 		}
586 		list_del_init(&fb->filp_head);
587 		drm_framebuffer_free(&fb->base.refcount);
588 	}
589 
590 	ida_destroy(&dev->mode_config.connector_ida);
591 	idr_destroy(&dev->mode_config.tile_idr);
592 	idr_destroy(&dev->mode_config.object_idr);
593 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
594 }
595 EXPORT_SYMBOL(drm_mode_config_cleanup);
596 
full_encoder_mask(struct drm_device * dev)597 static u32 full_encoder_mask(struct drm_device *dev)
598 {
599 	struct drm_encoder *encoder;
600 	u32 encoder_mask = 0;
601 
602 	drm_for_each_encoder(encoder, dev)
603 		encoder_mask |= drm_encoder_mask(encoder);
604 
605 	return encoder_mask;
606 }
607 
608 /*
609  * For some reason we want the encoder itself included in
610  * possible_clones. Make life easy for drivers by allowing them
611  * to leave possible_clones unset if no cloning is possible.
612  */
fixup_encoder_possible_clones(struct drm_encoder * encoder)613 static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
614 {
615 	if (encoder->possible_clones == 0)
616 		encoder->possible_clones = drm_encoder_mask(encoder);
617 }
618 
validate_encoder_possible_clones(struct drm_encoder * encoder)619 static void validate_encoder_possible_clones(struct drm_encoder *encoder)
620 {
621 	struct drm_device *dev = encoder->dev;
622 	u32 encoder_mask = full_encoder_mask(dev);
623 	struct drm_encoder *other;
624 
625 	drm_for_each_encoder(other, dev) {
626 		WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
627 		     !!(other->possible_clones & drm_encoder_mask(encoder)),
628 		     "possible_clones mismatch: "
629 		     "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
630 		     "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
631 		     encoder->base.id, encoder->name,
632 		     drm_encoder_mask(encoder), encoder->possible_clones,
633 		     other->base.id, other->name,
634 		     drm_encoder_mask(other), other->possible_clones);
635 	}
636 
637 	WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
638 	     (encoder->possible_clones & ~encoder_mask) != 0,
639 	     "Bogus possible_clones: "
640 	     "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
641 	     encoder->base.id, encoder->name,
642 	     encoder->possible_clones, encoder_mask);
643 }
644 
full_crtc_mask(struct drm_device * dev)645 static u32 full_crtc_mask(struct drm_device *dev)
646 {
647 	struct drm_crtc *crtc;
648 	u32 crtc_mask = 0;
649 
650 	drm_for_each_crtc(crtc, dev)
651 		crtc_mask |= drm_crtc_mask(crtc);
652 
653 	return crtc_mask;
654 }
655 
validate_encoder_possible_crtcs(struct drm_encoder * encoder)656 static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
657 {
658 	u32 crtc_mask = full_crtc_mask(encoder->dev);
659 
660 	WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
661 	     (encoder->possible_crtcs & ~crtc_mask) != 0,
662 	     "Bogus possible_crtcs: "
663 	     "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
664 	     encoder->base.id, encoder->name,
665 	     encoder->possible_crtcs, crtc_mask);
666 }
667 
drm_mode_config_validate(struct drm_device * dev)668 void drm_mode_config_validate(struct drm_device *dev)
669 {
670 	struct drm_encoder *encoder;
671 	struct drm_crtc *crtc;
672 	struct drm_plane *plane;
673 	u32 primary_with_crtc = 0, cursor_with_crtc = 0;
674 	unsigned int num_primary = 0;
675 
676 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
677 		return;
678 
679 	drm_for_each_encoder(encoder, dev)
680 		fixup_encoder_possible_clones(encoder);
681 
682 	drm_for_each_encoder(encoder, dev) {
683 		validate_encoder_possible_clones(encoder);
684 		validate_encoder_possible_crtcs(encoder);
685 	}
686 
687 	drm_for_each_crtc(crtc, dev) {
688 		WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
689 		     crtc->base.id, crtc->name);
690 
691 		WARN(crtc->cursor && crtc->funcs->cursor_set,
692 		     "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
693 		     crtc->base.id, crtc->name);
694 		WARN(crtc->cursor && crtc->funcs->cursor_set2,
695 		     "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
696 		     crtc->base.id, crtc->name);
697 		WARN(crtc->cursor && crtc->funcs->cursor_move,
698 		     "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
699 		     crtc->base.id, crtc->name);
700 
701 		if (crtc->primary) {
702 			WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
703 			     "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
704 			     crtc->primary->base.id, crtc->primary->name,
705 			     crtc->base.id, crtc->name);
706 			WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
707 			     "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
708 			     crtc->primary->base.id, crtc->primary->name);
709 			primary_with_crtc |= drm_plane_mask(crtc->primary);
710 		}
711 		if (crtc->cursor) {
712 			WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
713 			     "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
714 			     crtc->cursor->base.id, crtc->cursor->name,
715 			     crtc->base.id, crtc->name);
716 			WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
717 			     "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
718 			     crtc->cursor->base.id, crtc->cursor->name);
719 			cursor_with_crtc |= drm_plane_mask(crtc->cursor);
720 		}
721 	}
722 
723 	drm_for_each_plane(plane, dev) {
724 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
725 			num_primary++;
726 	}
727 
728 	WARN(num_primary != dev->mode_config.num_crtc,
729 	     "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
730 	     num_primary, dev->mode_config.num_crtc);
731 }
732