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