xref: /openbmc/linux/drivers/gpu/drm/drm_crtc.c (revision f3a8b664)
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *	Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_debugfs_crc.h>
44 
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47 
48 /*
49  * Global properties
50  */
51 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
52 	{ DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 	{ DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 	{ DRM_PLANE_TYPE_CURSOR, "Cursor" },
55 };
56 
57 /*
58  * Optional properties
59  */
60 /**
61  * drm_crtc_force_disable - Forcibly turn off a CRTC
62  * @crtc: CRTC to turn off
63  *
64  * Returns:
65  * Zero on success, error code on failure.
66  */
67 int drm_crtc_force_disable(struct drm_crtc *crtc)
68 {
69 	struct drm_mode_set set = {
70 		.crtc = crtc,
71 	};
72 
73 	return drm_mode_set_config_internal(&set);
74 }
75 EXPORT_SYMBOL(drm_crtc_force_disable);
76 
77 /**
78  * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79  * @dev: DRM device whose CRTCs to turn off
80  *
81  * Drivers may want to call this on unload to ensure that all displays are
82  * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
83  *
84  * Returns:
85  * Zero on success, error code on failure.
86  */
87 int drm_crtc_force_disable_all(struct drm_device *dev)
88 {
89 	struct drm_crtc *crtc;
90 	int ret = 0;
91 
92 	drm_modeset_lock_all(dev);
93 	drm_for_each_crtc(crtc, dev)
94 		if (crtc->enabled) {
95 			ret = drm_crtc_force_disable(crtc);
96 			if (ret)
97 				goto out;
98 		}
99 out:
100 	drm_modeset_unlock_all(dev);
101 	return ret;
102 }
103 EXPORT_SYMBOL(drm_crtc_force_disable_all);
104 
105 DEFINE_WW_CLASS(crtc_ww_class);
106 
107 static unsigned int drm_num_crtcs(struct drm_device *dev)
108 {
109 	unsigned int num = 0;
110 	struct drm_crtc *tmp;
111 
112 	drm_for_each_crtc(tmp, dev) {
113 		num++;
114 	}
115 
116 	return num;
117 }
118 
119 static int drm_crtc_register_all(struct drm_device *dev)
120 {
121 	struct drm_crtc *crtc;
122 	int ret = 0;
123 
124 	drm_for_each_crtc(crtc, dev) {
125 		if (drm_debugfs_crtc_add(crtc))
126 			DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
127 				  crtc->name);
128 
129 		if (crtc->funcs->late_register)
130 			ret = crtc->funcs->late_register(crtc);
131 		if (ret)
132 			return ret;
133 	}
134 
135 	return 0;
136 }
137 
138 static void drm_crtc_unregister_all(struct drm_device *dev)
139 {
140 	struct drm_crtc *crtc;
141 
142 	drm_for_each_crtc(crtc, dev) {
143 		if (crtc->funcs->early_unregister)
144 			crtc->funcs->early_unregister(crtc);
145 		drm_debugfs_crtc_remove(crtc);
146 	}
147 }
148 
149 static int drm_crtc_crc_init(struct drm_crtc *crtc)
150 {
151 #ifdef CONFIG_DEBUG_FS
152 	spin_lock_init(&crtc->crc.lock);
153 	init_waitqueue_head(&crtc->crc.wq);
154 	crtc->crc.source = kstrdup("auto", GFP_KERNEL);
155 	if (!crtc->crc.source)
156 		return -ENOMEM;
157 #endif
158 	return 0;
159 }
160 
161 static void drm_crtc_crc_fini(struct drm_crtc *crtc)
162 {
163 #ifdef CONFIG_DEBUG_FS
164 	kfree(crtc->crc.source);
165 #endif
166 }
167 
168 /**
169  * drm_crtc_init_with_planes - Initialise a new CRTC object with
170  *    specified primary and cursor planes.
171  * @dev: DRM device
172  * @crtc: CRTC object to init
173  * @primary: Primary plane for CRTC
174  * @cursor: Cursor plane for CRTC
175  * @funcs: callbacks for the new CRTC
176  * @name: printf style format string for the CRTC name, or NULL for default name
177  *
178  * Inits a new object created as base part of a driver crtc object. Drivers
179  * should use this function instead of drm_crtc_init(), which is only provided
180  * for backwards compatibility with drivers which do not yet support universal
181  * planes). For really simple hardware which has only 1 plane look at
182  * drm_simple_display_pipe_init() instead.
183  *
184  * Returns:
185  * Zero on success, error code on failure.
186  */
187 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
188 			      struct drm_plane *primary,
189 			      struct drm_plane *cursor,
190 			      const struct drm_crtc_funcs *funcs,
191 			      const char *name, ...)
192 {
193 	struct drm_mode_config *config = &dev->mode_config;
194 	int ret;
195 
196 	WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
197 	WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
198 
199 	crtc->dev = dev;
200 	crtc->funcs = funcs;
201 
202 	INIT_LIST_HEAD(&crtc->commit_list);
203 	spin_lock_init(&crtc->commit_lock);
204 
205 	drm_modeset_lock_init(&crtc->mutex);
206 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
207 	if (ret)
208 		return ret;
209 
210 	if (name) {
211 		va_list ap;
212 
213 		va_start(ap, name);
214 		crtc->name = kvasprintf(GFP_KERNEL, name, ap);
215 		va_end(ap);
216 	} else {
217 		crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
218 				       drm_num_crtcs(dev));
219 	}
220 	if (!crtc->name) {
221 		drm_mode_object_unregister(dev, &crtc->base);
222 		return -ENOMEM;
223 	}
224 
225 	crtc->base.properties = &crtc->properties;
226 
227 	list_add_tail(&crtc->head, &config->crtc_list);
228 	crtc->index = config->num_crtc++;
229 
230 	crtc->primary = primary;
231 	crtc->cursor = cursor;
232 	if (primary)
233 		primary->possible_crtcs = 1 << drm_crtc_index(crtc);
234 	if (cursor)
235 		cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
236 
237 	ret = drm_crtc_crc_init(crtc);
238 	if (ret) {
239 		drm_mode_object_unregister(dev, &crtc->base);
240 		return ret;
241 	}
242 
243 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
244 		drm_object_attach_property(&crtc->base, config->prop_active, 0);
245 		drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
246 	}
247 
248 	return 0;
249 }
250 EXPORT_SYMBOL(drm_crtc_init_with_planes);
251 
252 /**
253  * drm_crtc_cleanup - Clean up the core crtc usage
254  * @crtc: CRTC to cleanup
255  *
256  * This function cleans up @crtc and removes it from the DRM mode setting
257  * core. Note that the function does *not* free the crtc structure itself,
258  * this is the responsibility of the caller.
259  */
260 void drm_crtc_cleanup(struct drm_crtc *crtc)
261 {
262 	struct drm_device *dev = crtc->dev;
263 
264 	/* Note that the crtc_list is considered to be static; should we
265 	 * remove the drm_crtc at runtime we would have to decrement all
266 	 * the indices on the drm_crtc after us in the crtc_list.
267 	 */
268 
269 	drm_crtc_crc_fini(crtc);
270 
271 	kfree(crtc->gamma_store);
272 	crtc->gamma_store = NULL;
273 
274 	drm_modeset_lock_fini(&crtc->mutex);
275 
276 	drm_mode_object_unregister(dev, &crtc->base);
277 	list_del(&crtc->head);
278 	dev->mode_config.num_crtc--;
279 
280 	WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
281 	if (crtc->state && crtc->funcs->atomic_destroy_state)
282 		crtc->funcs->atomic_destroy_state(crtc, crtc->state);
283 
284 	kfree(crtc->name);
285 
286 	memset(crtc, 0, sizeof(*crtc));
287 }
288 EXPORT_SYMBOL(drm_crtc_cleanup);
289 
290 int drm_modeset_register_all(struct drm_device *dev)
291 {
292 	int ret;
293 
294 	ret = drm_plane_register_all(dev);
295 	if (ret)
296 		goto err_plane;
297 
298 	ret = drm_crtc_register_all(dev);
299 	if  (ret)
300 		goto err_crtc;
301 
302 	ret = drm_encoder_register_all(dev);
303 	if (ret)
304 		goto err_encoder;
305 
306 	ret = drm_connector_register_all(dev);
307 	if (ret)
308 		goto err_connector;
309 
310 	return 0;
311 
312 err_connector:
313 	drm_encoder_unregister_all(dev);
314 err_encoder:
315 	drm_crtc_unregister_all(dev);
316 err_crtc:
317 	drm_plane_unregister_all(dev);
318 err_plane:
319 	return ret;
320 }
321 
322 void drm_modeset_unregister_all(struct drm_device *dev)
323 {
324 	drm_connector_unregister_all(dev);
325 	drm_encoder_unregister_all(dev);
326 	drm_crtc_unregister_all(dev);
327 	drm_plane_unregister_all(dev);
328 }
329 
330 static int drm_mode_create_standard_properties(struct drm_device *dev)
331 {
332 	struct drm_property *prop;
333 	int ret;
334 
335 	ret = drm_connector_create_standard_properties(dev);
336 	if (ret)
337 		return ret;
338 
339 	prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
340 					"type", drm_plane_type_enum_list,
341 					ARRAY_SIZE(drm_plane_type_enum_list));
342 	if (!prop)
343 		return -ENOMEM;
344 	dev->mode_config.plane_type_property = prop;
345 
346 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347 			"SRC_X", 0, UINT_MAX);
348 	if (!prop)
349 		return -ENOMEM;
350 	dev->mode_config.prop_src_x = prop;
351 
352 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353 			"SRC_Y", 0, UINT_MAX);
354 	if (!prop)
355 		return -ENOMEM;
356 	dev->mode_config.prop_src_y = prop;
357 
358 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
359 			"SRC_W", 0, UINT_MAX);
360 	if (!prop)
361 		return -ENOMEM;
362 	dev->mode_config.prop_src_w = prop;
363 
364 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
365 			"SRC_H", 0, UINT_MAX);
366 	if (!prop)
367 		return -ENOMEM;
368 	dev->mode_config.prop_src_h = prop;
369 
370 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
371 			"CRTC_X", INT_MIN, INT_MAX);
372 	if (!prop)
373 		return -ENOMEM;
374 	dev->mode_config.prop_crtc_x = prop;
375 
376 	prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
377 			"CRTC_Y", INT_MIN, INT_MAX);
378 	if (!prop)
379 		return -ENOMEM;
380 	dev->mode_config.prop_crtc_y = prop;
381 
382 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
383 			"CRTC_W", 0, INT_MAX);
384 	if (!prop)
385 		return -ENOMEM;
386 	dev->mode_config.prop_crtc_w = prop;
387 
388 	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
389 			"CRTC_H", 0, INT_MAX);
390 	if (!prop)
391 		return -ENOMEM;
392 	dev->mode_config.prop_crtc_h = prop;
393 
394 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
395 			"FB_ID", DRM_MODE_OBJECT_FB);
396 	if (!prop)
397 		return -ENOMEM;
398 	dev->mode_config.prop_fb_id = prop;
399 
400 	prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
401 			"CRTC_ID", DRM_MODE_OBJECT_CRTC);
402 	if (!prop)
403 		return -ENOMEM;
404 	dev->mode_config.prop_crtc_id = prop;
405 
406 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
407 			"ACTIVE");
408 	if (!prop)
409 		return -ENOMEM;
410 	dev->mode_config.prop_active = prop;
411 
412 	prop = drm_property_create(dev,
413 			DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
414 			"MODE_ID", 0);
415 	if (!prop)
416 		return -ENOMEM;
417 	dev->mode_config.prop_mode_id = prop;
418 
419 	prop = drm_property_create(dev,
420 			DRM_MODE_PROP_BLOB,
421 			"DEGAMMA_LUT", 0);
422 	if (!prop)
423 		return -ENOMEM;
424 	dev->mode_config.degamma_lut_property = prop;
425 
426 	prop = drm_property_create_range(dev,
427 			DRM_MODE_PROP_IMMUTABLE,
428 			"DEGAMMA_LUT_SIZE", 0, UINT_MAX);
429 	if (!prop)
430 		return -ENOMEM;
431 	dev->mode_config.degamma_lut_size_property = prop;
432 
433 	prop = drm_property_create(dev,
434 			DRM_MODE_PROP_BLOB,
435 			"CTM", 0);
436 	if (!prop)
437 		return -ENOMEM;
438 	dev->mode_config.ctm_property = prop;
439 
440 	prop = drm_property_create(dev,
441 			DRM_MODE_PROP_BLOB,
442 			"GAMMA_LUT", 0);
443 	if (!prop)
444 		return -ENOMEM;
445 	dev->mode_config.gamma_lut_property = prop;
446 
447 	prop = drm_property_create_range(dev,
448 			DRM_MODE_PROP_IMMUTABLE,
449 			"GAMMA_LUT_SIZE", 0, UINT_MAX);
450 	if (!prop)
451 		return -ENOMEM;
452 	dev->mode_config.gamma_lut_size_property = prop;
453 
454 	return 0;
455 }
456 
457 /**
458  * drm_mode_getresources - get graphics configuration
459  * @dev: drm device for the ioctl
460  * @data: data pointer for the ioctl
461  * @file_priv: drm file for the ioctl call
462  *
463  * Construct a set of configuration description structures and return
464  * them to the user, including CRTC, connector and framebuffer configuration.
465  *
466  * Called by the user via ioctl.
467  *
468  * Returns:
469  * Zero on success, negative errno on failure.
470  */
471 int drm_mode_getresources(struct drm_device *dev, void *data,
472 			  struct drm_file *file_priv)
473 {
474 	struct drm_mode_card_res *card_res = data;
475 	struct list_head *lh;
476 	struct drm_framebuffer *fb;
477 	struct drm_connector *connector;
478 	struct drm_crtc *crtc;
479 	struct drm_encoder *encoder;
480 	int ret = 0;
481 	int connector_count = 0;
482 	int crtc_count = 0;
483 	int fb_count = 0;
484 	int encoder_count = 0;
485 	int copied = 0;
486 	uint32_t __user *fb_id;
487 	uint32_t __user *crtc_id;
488 	uint32_t __user *connector_id;
489 	uint32_t __user *encoder_id;
490 
491 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
492 		return -EINVAL;
493 
494 
495 	mutex_lock(&file_priv->fbs_lock);
496 	/*
497 	 * For the non-control nodes we need to limit the list of resources
498 	 * by IDs in the group list for this node
499 	 */
500 	list_for_each(lh, &file_priv->fbs)
501 		fb_count++;
502 
503 	/* handle this in 4 parts */
504 	/* FBs */
505 	if (card_res->count_fbs >= fb_count) {
506 		copied = 0;
507 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
508 		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
509 			if (put_user(fb->base.id, fb_id + copied)) {
510 				mutex_unlock(&file_priv->fbs_lock);
511 				return -EFAULT;
512 			}
513 			copied++;
514 		}
515 	}
516 	card_res->count_fbs = fb_count;
517 	mutex_unlock(&file_priv->fbs_lock);
518 
519 	/* mode_config.mutex protects the connector list against e.g. DP MST
520 	 * connector hot-adding. CRTC/Plane lists are invariant. */
521 	mutex_lock(&dev->mode_config.mutex);
522 	drm_for_each_crtc(crtc, dev)
523 		crtc_count++;
524 
525 	drm_for_each_connector(connector, dev)
526 		connector_count++;
527 
528 	drm_for_each_encoder(encoder, dev)
529 		encoder_count++;
530 
531 	card_res->max_height = dev->mode_config.max_height;
532 	card_res->min_height = dev->mode_config.min_height;
533 	card_res->max_width = dev->mode_config.max_width;
534 	card_res->min_width = dev->mode_config.min_width;
535 
536 	/* CRTCs */
537 	if (card_res->count_crtcs >= crtc_count) {
538 		copied = 0;
539 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
540 		drm_for_each_crtc(crtc, dev) {
541 			if (put_user(crtc->base.id, crtc_id + copied)) {
542 				ret = -EFAULT;
543 				goto out;
544 			}
545 			copied++;
546 		}
547 	}
548 	card_res->count_crtcs = crtc_count;
549 
550 	/* Encoders */
551 	if (card_res->count_encoders >= encoder_count) {
552 		copied = 0;
553 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
554 		drm_for_each_encoder(encoder, dev) {
555 			if (put_user(encoder->base.id, encoder_id +
556 				     copied)) {
557 				ret = -EFAULT;
558 				goto out;
559 			}
560 			copied++;
561 		}
562 	}
563 	card_res->count_encoders = encoder_count;
564 
565 	/* Connectors */
566 	if (card_res->count_connectors >= connector_count) {
567 		copied = 0;
568 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
569 		drm_for_each_connector(connector, dev) {
570 			if (put_user(connector->base.id,
571 				     connector_id + copied)) {
572 				ret = -EFAULT;
573 				goto out;
574 			}
575 			copied++;
576 		}
577 	}
578 	card_res->count_connectors = connector_count;
579 
580 out:
581 	mutex_unlock(&dev->mode_config.mutex);
582 	return ret;
583 }
584 
585 /**
586  * drm_mode_getcrtc - get CRTC configuration
587  * @dev: drm device for the ioctl
588  * @data: data pointer for the ioctl
589  * @file_priv: drm file for the ioctl call
590  *
591  * Construct a CRTC configuration structure to return to the user.
592  *
593  * Called by the user via ioctl.
594  *
595  * Returns:
596  * Zero on success, negative errno on failure.
597  */
598 int drm_mode_getcrtc(struct drm_device *dev,
599 		     void *data, struct drm_file *file_priv)
600 {
601 	struct drm_mode_crtc *crtc_resp = data;
602 	struct drm_crtc *crtc;
603 
604 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
605 		return -EINVAL;
606 
607 	crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
608 	if (!crtc)
609 		return -ENOENT;
610 
611 	drm_modeset_lock_crtc(crtc, crtc->primary);
612 	crtc_resp->gamma_size = crtc->gamma_size;
613 	if (crtc->primary->fb)
614 		crtc_resp->fb_id = crtc->primary->fb->base.id;
615 	else
616 		crtc_resp->fb_id = 0;
617 
618 	if (crtc->state) {
619 		crtc_resp->x = crtc->primary->state->src_x >> 16;
620 		crtc_resp->y = crtc->primary->state->src_y >> 16;
621 		if (crtc->state->enable) {
622 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
623 			crtc_resp->mode_valid = 1;
624 
625 		} else {
626 			crtc_resp->mode_valid = 0;
627 		}
628 	} else {
629 		crtc_resp->x = crtc->x;
630 		crtc_resp->y = crtc->y;
631 		if (crtc->enabled) {
632 			drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
633 			crtc_resp->mode_valid = 1;
634 
635 		} else {
636 			crtc_resp->mode_valid = 0;
637 		}
638 	}
639 	drm_modeset_unlock_crtc(crtc);
640 
641 	return 0;
642 }
643 
644 /**
645  * drm_mode_set_config_internal - helper to call ->set_config
646  * @set: modeset config to set
647  *
648  * This is a little helper to wrap internal calls to the ->set_config driver
649  * interface. The only thing it adds is correct refcounting dance.
650  *
651  * Returns:
652  * Zero on success, negative errno on failure.
653  */
654 int drm_mode_set_config_internal(struct drm_mode_set *set)
655 {
656 	struct drm_crtc *crtc = set->crtc;
657 	struct drm_framebuffer *fb;
658 	struct drm_crtc *tmp;
659 	int ret;
660 
661 	/*
662 	 * NOTE: ->set_config can also disable other crtcs (if we steal all
663 	 * connectors from it), hence we need to refcount the fbs across all
664 	 * crtcs. Atomic modeset will have saner semantics ...
665 	 */
666 	drm_for_each_crtc(tmp, crtc->dev)
667 		tmp->primary->old_fb = tmp->primary->fb;
668 
669 	fb = set->fb;
670 
671 	ret = crtc->funcs->set_config(set);
672 	if (ret == 0) {
673 		crtc->primary->crtc = crtc;
674 		crtc->primary->fb = fb;
675 	}
676 
677 	drm_for_each_crtc(tmp, crtc->dev) {
678 		if (tmp->primary->fb)
679 			drm_framebuffer_reference(tmp->primary->fb);
680 		if (tmp->primary->old_fb)
681 			drm_framebuffer_unreference(tmp->primary->old_fb);
682 		tmp->primary->old_fb = NULL;
683 	}
684 
685 	return ret;
686 }
687 EXPORT_SYMBOL(drm_mode_set_config_internal);
688 
689 /**
690  * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
691  * @mode: mode to query
692  * @hdisplay: hdisplay value to fill in
693  * @vdisplay: vdisplay value to fill in
694  *
695  * The vdisplay value will be doubled if the specified mode is a stereo mode of
696  * the appropriate layout.
697  */
698 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
699 			    int *hdisplay, int *vdisplay)
700 {
701 	struct drm_display_mode adjusted;
702 
703 	drm_mode_copy(&adjusted, mode);
704 	drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
705 	*hdisplay = adjusted.crtc_hdisplay;
706 	*vdisplay = adjusted.crtc_vdisplay;
707 }
708 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
709 
710 /**
711  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
712  *     CRTC viewport
713  * @crtc: CRTC that framebuffer will be displayed on
714  * @x: x panning
715  * @y: y panning
716  * @mode: mode that framebuffer will be displayed under
717  * @fb: framebuffer to check size of
718  */
719 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
720 			    int x, int y,
721 			    const struct drm_display_mode *mode,
722 			    const struct drm_framebuffer *fb)
723 
724 {
725 	int hdisplay, vdisplay;
726 
727 	drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
728 
729 	if (crtc->state &&
730 	    drm_rotation_90_or_270(crtc->primary->state->rotation))
731 		swap(hdisplay, vdisplay);
732 
733 	return drm_framebuffer_check_src_coords(x << 16, y << 16,
734 						hdisplay << 16, vdisplay << 16,
735 						fb);
736 }
737 EXPORT_SYMBOL(drm_crtc_check_viewport);
738 
739 /**
740  * drm_mode_setcrtc - set CRTC configuration
741  * @dev: drm device for the ioctl
742  * @data: data pointer for the ioctl
743  * @file_priv: drm file for the ioctl call
744  *
745  * Build a new CRTC configuration based on user request.
746  *
747  * Called by the user via ioctl.
748  *
749  * Returns:
750  * Zero on success, negative errno on failure.
751  */
752 int drm_mode_setcrtc(struct drm_device *dev, void *data,
753 		     struct drm_file *file_priv)
754 {
755 	struct drm_mode_config *config = &dev->mode_config;
756 	struct drm_mode_crtc *crtc_req = data;
757 	struct drm_crtc *crtc;
758 	struct drm_connector **connector_set = NULL, *connector;
759 	struct drm_framebuffer *fb = NULL;
760 	struct drm_display_mode *mode = NULL;
761 	struct drm_mode_set set;
762 	uint32_t __user *set_connectors_ptr;
763 	int ret;
764 	int i;
765 
766 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
767 		return -EINVAL;
768 
769 	/*
770 	 * Universal plane src offsets are only 16.16, prevent havoc for
771 	 * drivers using universal plane code internally.
772 	 */
773 	if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
774 		return -ERANGE;
775 
776 	drm_modeset_lock_all(dev);
777 	crtc = drm_crtc_find(dev, crtc_req->crtc_id);
778 	if (!crtc) {
779 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
780 		ret = -ENOENT;
781 		goto out;
782 	}
783 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
784 
785 	if (crtc_req->mode_valid) {
786 		/* If we have a mode we need a framebuffer. */
787 		/* If we pass -1, set the mode with the currently bound fb */
788 		if (crtc_req->fb_id == -1) {
789 			if (!crtc->primary->fb) {
790 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
791 				ret = -EINVAL;
792 				goto out;
793 			}
794 			fb = crtc->primary->fb;
795 			/* Make refcounting symmetric with the lookup path. */
796 			drm_framebuffer_reference(fb);
797 		} else {
798 			fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
799 			if (!fb) {
800 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
801 						crtc_req->fb_id);
802 				ret = -ENOENT;
803 				goto out;
804 			}
805 		}
806 
807 		mode = drm_mode_create(dev);
808 		if (!mode) {
809 			ret = -ENOMEM;
810 			goto out;
811 		}
812 
813 		ret = drm_mode_convert_umode(mode, &crtc_req->mode);
814 		if (ret) {
815 			DRM_DEBUG_KMS("Invalid mode\n");
816 			goto out;
817 		}
818 
819 		/*
820 		 * Check whether the primary plane supports the fb pixel format.
821 		 * Drivers not implementing the universal planes API use a
822 		 * default formats list provided by the DRM core which doesn't
823 		 * match real hardware capabilities. Skip the check in that
824 		 * case.
825 		 */
826 		if (!crtc->primary->format_default) {
827 			ret = drm_plane_check_pixel_format(crtc->primary,
828 							   fb->pixel_format);
829 			if (ret) {
830 				char *format_name = drm_get_format_name(fb->pixel_format);
831 				DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
832 				kfree(format_name);
833 				goto out;
834 			}
835 		}
836 
837 		ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
838 					      mode, fb);
839 		if (ret)
840 			goto out;
841 
842 	}
843 
844 	if (crtc_req->count_connectors == 0 && mode) {
845 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
846 		ret = -EINVAL;
847 		goto out;
848 	}
849 
850 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
851 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
852 			  crtc_req->count_connectors);
853 		ret = -EINVAL;
854 		goto out;
855 	}
856 
857 	if (crtc_req->count_connectors > 0) {
858 		u32 out_id;
859 
860 		/* Avoid unbounded kernel memory allocation */
861 		if (crtc_req->count_connectors > config->num_connector) {
862 			ret = -EINVAL;
863 			goto out;
864 		}
865 
866 		connector_set = kmalloc_array(crtc_req->count_connectors,
867 					      sizeof(struct drm_connector *),
868 					      GFP_KERNEL);
869 		if (!connector_set) {
870 			ret = -ENOMEM;
871 			goto out;
872 		}
873 
874 		for (i = 0; i < crtc_req->count_connectors; i++) {
875 			connector_set[i] = NULL;
876 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
877 			if (get_user(out_id, &set_connectors_ptr[i])) {
878 				ret = -EFAULT;
879 				goto out;
880 			}
881 
882 			connector = drm_connector_lookup(dev, out_id);
883 			if (!connector) {
884 				DRM_DEBUG_KMS("Connector id %d unknown\n",
885 						out_id);
886 				ret = -ENOENT;
887 				goto out;
888 			}
889 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
890 					connector->base.id,
891 					connector->name);
892 
893 			connector_set[i] = connector;
894 		}
895 	}
896 
897 	set.crtc = crtc;
898 	set.x = crtc_req->x;
899 	set.y = crtc_req->y;
900 	set.mode = mode;
901 	set.connectors = connector_set;
902 	set.num_connectors = crtc_req->count_connectors;
903 	set.fb = fb;
904 	ret = drm_mode_set_config_internal(&set);
905 
906 out:
907 	if (fb)
908 		drm_framebuffer_unreference(fb);
909 
910 	if (connector_set) {
911 		for (i = 0; i < crtc_req->count_connectors; i++) {
912 			if (connector_set[i])
913 				drm_connector_unreference(connector_set[i]);
914 		}
915 	}
916 	kfree(connector_set);
917 	drm_mode_destroy(dev, mode);
918 	drm_modeset_unlock_all(dev);
919 	return ret;
920 }
921 
922 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
923 			       struct drm_property *property,
924 			       uint64_t value)
925 {
926 	int ret = -EINVAL;
927 	struct drm_crtc *crtc = obj_to_crtc(obj);
928 
929 	if (crtc->funcs->set_property)
930 		ret = crtc->funcs->set_property(crtc, property, value);
931 	if (!ret)
932 		drm_object_property_set_value(obj, property, value);
933 
934 	return ret;
935 }
936 
937 /**
938  * drm_mode_config_reset - call ->reset callbacks
939  * @dev: drm device
940  *
941  * This functions calls all the crtc's, encoder's and connector's ->reset
942  * callback. Drivers can use this in e.g. their driver load or resume code to
943  * reset hardware and software state.
944  */
945 void drm_mode_config_reset(struct drm_device *dev)
946 {
947 	struct drm_crtc *crtc;
948 	struct drm_plane *plane;
949 	struct drm_encoder *encoder;
950 	struct drm_connector *connector;
951 
952 	drm_for_each_plane(plane, dev)
953 		if (plane->funcs->reset)
954 			plane->funcs->reset(plane);
955 
956 	drm_for_each_crtc(crtc, dev)
957 		if (crtc->funcs->reset)
958 			crtc->funcs->reset(crtc);
959 
960 	drm_for_each_encoder(encoder, dev)
961 		if (encoder->funcs->reset)
962 			encoder->funcs->reset(encoder);
963 
964 	mutex_lock(&dev->mode_config.mutex);
965 	drm_for_each_connector(connector, dev)
966 		if (connector->funcs->reset)
967 			connector->funcs->reset(connector);
968 	mutex_unlock(&dev->mode_config.mutex);
969 }
970 EXPORT_SYMBOL(drm_mode_config_reset);
971 
972 /**
973  * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
974  * @dev: DRM device
975  * @data: ioctl data
976  * @file_priv: DRM file info
977  *
978  * This creates a new dumb buffer in the driver's backing storage manager (GEM,
979  * TTM or something else entirely) and returns the resulting buffer handle. This
980  * handle can then be wrapped up into a framebuffer modeset object.
981  *
982  * Note that userspace is not allowed to use such objects for render
983  * acceleration - drivers must create their own private ioctls for such a use
984  * case.
985  *
986  * Called by the user via ioctl.
987  *
988  * Returns:
989  * Zero on success, negative errno on failure.
990  */
991 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
992 			       void *data, struct drm_file *file_priv)
993 {
994 	struct drm_mode_create_dumb *args = data;
995 	u32 cpp, stride, size;
996 
997 	if (!dev->driver->dumb_create)
998 		return -ENOSYS;
999 	if (!args->width || !args->height || !args->bpp)
1000 		return -EINVAL;
1001 
1002 	/* overflow checks for 32bit size calculations */
1003 	/* NOTE: DIV_ROUND_UP() can overflow */
1004 	cpp = DIV_ROUND_UP(args->bpp, 8);
1005 	if (!cpp || cpp > 0xffffffffU / args->width)
1006 		return -EINVAL;
1007 	stride = cpp * args->width;
1008 	if (args->height > 0xffffffffU / stride)
1009 		return -EINVAL;
1010 
1011 	/* test for wrap-around */
1012 	size = args->height * stride;
1013 	if (PAGE_ALIGN(size) == 0)
1014 		return -EINVAL;
1015 
1016 	/*
1017 	 * handle, pitch and size are output parameters. Zero them out to
1018 	 * prevent drivers from accidentally using uninitialized data. Since
1019 	 * not all existing userspace is clearing these fields properly we
1020 	 * cannot reject IOCTL with garbage in them.
1021 	 */
1022 	args->handle = 0;
1023 	args->pitch = 0;
1024 	args->size = 0;
1025 
1026 	return dev->driver->dumb_create(file_priv, dev, args);
1027 }
1028 
1029 /**
1030  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1031  * @dev: DRM device
1032  * @data: ioctl data
1033  * @file_priv: DRM file info
1034  *
1035  * Allocate an offset in the drm device node's address space to be able to
1036  * memory map a dumb buffer.
1037  *
1038  * Called by the user via ioctl.
1039  *
1040  * Returns:
1041  * Zero on success, negative errno on failure.
1042  */
1043 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1044 			     void *data, struct drm_file *file_priv)
1045 {
1046 	struct drm_mode_map_dumb *args = data;
1047 
1048 	/* call driver ioctl to get mmap offset */
1049 	if (!dev->driver->dumb_map_offset)
1050 		return -ENOSYS;
1051 
1052 	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
1053 }
1054 
1055 /**
1056  * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1057  * @dev: DRM device
1058  * @data: ioctl data
1059  * @file_priv: DRM file info
1060  *
1061  * This destroys the userspace handle for the given dumb backing storage buffer.
1062  * Since buffer objects must be reference counted in the kernel a buffer object
1063  * won't be immediately freed if a framebuffer modeset object still uses it.
1064  *
1065  * Called by the user via ioctl.
1066  *
1067  * Returns:
1068  * Zero on success, negative errno on failure.
1069  */
1070 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1071 				void *data, struct drm_file *file_priv)
1072 {
1073 	struct drm_mode_destroy_dumb *args = data;
1074 
1075 	if (!dev->driver->dumb_destroy)
1076 		return -ENOSYS;
1077 
1078 	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1079 }
1080 
1081 /**
1082  * drm_mode_config_init - initialize DRM mode_configuration structure
1083  * @dev: DRM device
1084  *
1085  * Initialize @dev's mode_config structure, used for tracking the graphics
1086  * configuration of @dev.
1087  *
1088  * Since this initializes the modeset locks, no locking is possible. Which is no
1089  * problem, since this should happen single threaded at init time. It is the
1090  * driver's problem to ensure this guarantee.
1091  *
1092  */
1093 void drm_mode_config_init(struct drm_device *dev)
1094 {
1095 	mutex_init(&dev->mode_config.mutex);
1096 	drm_modeset_lock_init(&dev->mode_config.connection_mutex);
1097 	mutex_init(&dev->mode_config.idr_mutex);
1098 	mutex_init(&dev->mode_config.fb_lock);
1099 	mutex_init(&dev->mode_config.blob_lock);
1100 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
1101 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1102 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
1103 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1104 	INIT_LIST_HEAD(&dev->mode_config.property_list);
1105 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1106 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
1107 	idr_init(&dev->mode_config.crtc_idr);
1108 	idr_init(&dev->mode_config.tile_idr);
1109 	ida_init(&dev->mode_config.connector_ida);
1110 
1111 	drm_modeset_lock_all(dev);
1112 	drm_mode_create_standard_properties(dev);
1113 	drm_modeset_unlock_all(dev);
1114 
1115 	/* Just to be sure */
1116 	dev->mode_config.num_fb = 0;
1117 	dev->mode_config.num_connector = 0;
1118 	dev->mode_config.num_crtc = 0;
1119 	dev->mode_config.num_encoder = 0;
1120 	dev->mode_config.num_overlay_plane = 0;
1121 	dev->mode_config.num_total_plane = 0;
1122 }
1123 EXPORT_SYMBOL(drm_mode_config_init);
1124 
1125 /**
1126  * drm_mode_config_cleanup - free up DRM mode_config info
1127  * @dev: DRM device
1128  *
1129  * Free up all the connectors and CRTCs associated with this DRM device, then
1130  * free up the framebuffers and associated buffer objects.
1131  *
1132  * Note that since this /should/ happen single-threaded at driver/device
1133  * teardown time, no locking is required. It's the driver's job to ensure that
1134  * this guarantee actually holds true.
1135  *
1136  * FIXME: cleanup any dangling user buffer objects too
1137  */
1138 void drm_mode_config_cleanup(struct drm_device *dev)
1139 {
1140 	struct drm_connector *connector, *ot;
1141 	struct drm_crtc *crtc, *ct;
1142 	struct drm_encoder *encoder, *enct;
1143 	struct drm_framebuffer *fb, *fbt;
1144 	struct drm_property *property, *pt;
1145 	struct drm_property_blob *blob, *bt;
1146 	struct drm_plane *plane, *plt;
1147 
1148 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1149 				 head) {
1150 		encoder->funcs->destroy(encoder);
1151 	}
1152 
1153 	list_for_each_entry_safe(connector, ot,
1154 				 &dev->mode_config.connector_list, head) {
1155 		connector->funcs->destroy(connector);
1156 	}
1157 
1158 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1159 				 head) {
1160 		drm_property_destroy(dev, property);
1161 	}
1162 
1163 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1164 				 head) {
1165 		plane->funcs->destroy(plane);
1166 	}
1167 
1168 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1169 		crtc->funcs->destroy(crtc);
1170 	}
1171 
1172 	list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
1173 				 head_global) {
1174 		drm_property_unreference_blob(blob);
1175 	}
1176 
1177 	/*
1178 	 * Single-threaded teardown context, so it's not required to grab the
1179 	 * fb_lock to protect against concurrent fb_list access. Contrary, it
1180 	 * would actually deadlock with the drm_framebuffer_cleanup function.
1181 	 *
1182 	 * Also, if there are any framebuffers left, that's a driver leak now,
1183 	 * so politely WARN about this.
1184 	 */
1185 	WARN_ON(!list_empty(&dev->mode_config.fb_list));
1186 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1187 		drm_framebuffer_free(&fb->base.refcount);
1188 	}
1189 
1190 	ida_destroy(&dev->mode_config.connector_ida);
1191 	idr_destroy(&dev->mode_config.tile_idr);
1192 	idr_destroy(&dev->mode_config.crtc_idr);
1193 	drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
1194 }
1195 EXPORT_SYMBOL(drm_mode_config_cleanup);
1196 
1197 /**
1198  * DOC: Tile group
1199  *
1200  * Tile groups are used to represent tiled monitors with a unique
1201  * integer identifier. Tiled monitors using DisplayID v1.3 have
1202  * a unique 8-byte handle, we store this in a tile group, so we
1203  * have a common identifier for all tiles in a monitor group.
1204  */
1205 static void drm_tile_group_free(struct kref *kref)
1206 {
1207 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1208 	struct drm_device *dev = tg->dev;
1209 	mutex_lock(&dev->mode_config.idr_mutex);
1210 	idr_remove(&dev->mode_config.tile_idr, tg->id);
1211 	mutex_unlock(&dev->mode_config.idr_mutex);
1212 	kfree(tg);
1213 }
1214 
1215 /**
1216  * drm_mode_put_tile_group - drop a reference to a tile group.
1217  * @dev: DRM device
1218  * @tg: tile group to drop reference to.
1219  *
1220  * drop reference to tile group and free if 0.
1221  */
1222 void drm_mode_put_tile_group(struct drm_device *dev,
1223 			     struct drm_tile_group *tg)
1224 {
1225 	kref_put(&tg->refcount, drm_tile_group_free);
1226 }
1227 
1228 /**
1229  * drm_mode_get_tile_group - get a reference to an existing tile group
1230  * @dev: DRM device
1231  * @topology: 8-bytes unique per monitor.
1232  *
1233  * Use the unique bytes to get a reference to an existing tile group.
1234  *
1235  * RETURNS:
1236  * tile group or NULL if not found.
1237  */
1238 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1239 					       char topology[8])
1240 {
1241 	struct drm_tile_group *tg;
1242 	int id;
1243 	mutex_lock(&dev->mode_config.idr_mutex);
1244 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1245 		if (!memcmp(tg->group_data, topology, 8)) {
1246 			if (!kref_get_unless_zero(&tg->refcount))
1247 				tg = NULL;
1248 			mutex_unlock(&dev->mode_config.idr_mutex);
1249 			return tg;
1250 		}
1251 	}
1252 	mutex_unlock(&dev->mode_config.idr_mutex);
1253 	return NULL;
1254 }
1255 EXPORT_SYMBOL(drm_mode_get_tile_group);
1256 
1257 /**
1258  * drm_mode_create_tile_group - create a tile group from a displayid description
1259  * @dev: DRM device
1260  * @topology: 8-bytes unique per monitor.
1261  *
1262  * Create a tile group for the unique monitor, and get a unique
1263  * identifier for the tile group.
1264  *
1265  * RETURNS:
1266  * new tile group or error.
1267  */
1268 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1269 						  char topology[8])
1270 {
1271 	struct drm_tile_group *tg;
1272 	int ret;
1273 
1274 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1275 	if (!tg)
1276 		return ERR_PTR(-ENOMEM);
1277 
1278 	kref_init(&tg->refcount);
1279 	memcpy(tg->group_data, topology, 8);
1280 	tg->dev = dev;
1281 
1282 	mutex_lock(&dev->mode_config.idr_mutex);
1283 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1284 	if (ret >= 0) {
1285 		tg->id = ret;
1286 	} else {
1287 		kfree(tg);
1288 		tg = ERR_PTR(ret);
1289 	}
1290 
1291 	mutex_unlock(&dev->mode_config.idr_mutex);
1292 	return tg;
1293 }
1294 EXPORT_SYMBOL(drm_mode_create_tile_group);
1295