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