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