1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  *
5  * DRM core CRTC related functions
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  *
25  * Authors:
26  *      Keith Packard
27  *	Eric Anholt <eric@anholt.net>
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31 
32 #include <linux/export.h>
33 #include <linux/kernel.h>
34 #include <linux/moduleparam.h>
35 #include <linux/dynamic_debug.h>
36 
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_atomic_uapi.h>
40 #include <drm/drm_bridge.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_edid.h>
45 #include <drm/drm_encoder.h>
46 #include <drm/drm_fb_helper.h>
47 #include <drm/drm_fourcc.h>
48 #include <drm/drm_framebuffer.h>
49 #include <drm/drm_plane_helper.h>
50 #include <drm/drm_print.h>
51 #include <drm/drm_vblank.h>
52 
53 #include "drm_crtc_helper_internal.h"
54 
55 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
56 			"DRM_UT_CORE",
57 			"DRM_UT_DRIVER",
58 			"DRM_UT_KMS",
59 			"DRM_UT_PRIME",
60 			"DRM_UT_ATOMIC",
61 			"DRM_UT_VBL",
62 			"DRM_UT_STATE",
63 			"DRM_UT_LEASE",
64 			"DRM_UT_DP",
65 			"DRM_UT_DRMRES");
66 
67 /**
68  * DOC: overview
69  *
70  * The CRTC modeset helper library provides a default set_config implementation
71  * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
72  * the same callbacks which drivers can use to e.g. restore the modeset
73  * configuration on resume with drm_helper_resume_force_mode().
74  *
75  * Note that this helper library doesn't track the current power state of CRTCs
76  * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
77  * though the hardware is already in the desired state. This deficiency has been
78  * fixed in the atomic helpers.
79  *
80  * The driver callbacks are mostly compatible with the atomic modeset helpers,
81  * except for the handling of the primary plane: Atomic helpers require that the
82  * primary plane is implemented as a real standalone plane and not directly tied
83  * to the CRTC state. For easier transition this library provides functions to
84  * implement the old semantics required by the CRTC helpers using the new plane
85  * and atomic helper callbacks.
86  *
87  * Drivers are strongly urged to convert to the atomic helpers (by way of first
88  * converting to the plane helpers). New drivers must not use these functions
89  * but need to implement the atomic interface instead, potentially using the
90  * atomic helpers for that.
91  *
92  * These legacy modeset helpers use the same function table structures as
93  * all other modesetting helpers. See the documentation for struct
94  * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
95  * &drm_connector_helper_funcs.
96  */
97 
98 /**
99  * drm_helper_encoder_in_use - check if a given encoder is in use
100  * @encoder: encoder to check
101  *
102  * Checks whether @encoder is with the current mode setting output configuration
103  * in use by any connector. This doesn't mean that it is actually enabled since
104  * the DPMS state is tracked separately.
105  *
106  * Returns:
107  * True if @encoder is used, false otherwise.
108  */
109 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
110 {
111 	struct drm_connector *connector;
112 	struct drm_connector_list_iter conn_iter;
113 	struct drm_device *dev = encoder->dev;
114 
115 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
116 
117 	/*
118 	 * We can expect this mutex to be locked if we are not panicking.
119 	 * Locking is currently fubar in the panic handler.
120 	 */
121 	if (!oops_in_progress) {
122 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
123 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
124 	}
125 
126 
127 	drm_connector_list_iter_begin(dev, &conn_iter);
128 	drm_for_each_connector_iter(connector, &conn_iter) {
129 		if (connector->encoder == encoder) {
130 			drm_connector_list_iter_end(&conn_iter);
131 			return true;
132 		}
133 	}
134 	drm_connector_list_iter_end(&conn_iter);
135 	return false;
136 }
137 EXPORT_SYMBOL(drm_helper_encoder_in_use);
138 
139 /**
140  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
141  * @crtc: CRTC to check
142  *
143  * Checks whether @crtc is with the current mode setting output configuration
144  * in use by any connector. This doesn't mean that it is actually enabled since
145  * the DPMS state is tracked separately.
146  *
147  * Returns:
148  * True if @crtc is used, false otherwise.
149  */
150 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
151 {
152 	struct drm_encoder *encoder;
153 	struct drm_device *dev = crtc->dev;
154 
155 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
156 
157 	/*
158 	 * We can expect this mutex to be locked if we are not panicking.
159 	 * Locking is currently fubar in the panic handler.
160 	 */
161 	if (!oops_in_progress)
162 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
163 
164 	drm_for_each_encoder(encoder, dev)
165 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
166 			return true;
167 	return false;
168 }
169 EXPORT_SYMBOL(drm_helper_crtc_in_use);
170 
171 static void
172 drm_encoder_disable(struct drm_encoder *encoder)
173 {
174 	const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
175 
176 	if (!encoder_funcs)
177 		return;
178 
179 	if (encoder_funcs->disable)
180 		(*encoder_funcs->disable)(encoder);
181 	else if (encoder_funcs->dpms)
182 		(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
183 }
184 
185 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
186 {
187 	struct drm_encoder *encoder;
188 	struct drm_crtc *crtc;
189 
190 	drm_warn_on_modeset_not_all_locked(dev);
191 
192 	drm_for_each_encoder(encoder, dev) {
193 		if (!drm_helper_encoder_in_use(encoder)) {
194 			drm_encoder_disable(encoder);
195 			/* disconnect encoder from any connector */
196 			encoder->crtc = NULL;
197 		}
198 	}
199 
200 	drm_for_each_crtc(crtc, dev) {
201 		const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
202 
203 		crtc->enabled = drm_helper_crtc_in_use(crtc);
204 		if (!crtc->enabled) {
205 			if (crtc_funcs->disable)
206 				(*crtc_funcs->disable)(crtc);
207 			else
208 				(*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
209 			crtc->primary->fb = NULL;
210 		}
211 	}
212 }
213 
214 /**
215  * drm_helper_disable_unused_functions - disable unused objects
216  * @dev: DRM device
217  *
218  * This function walks through the entire mode setting configuration of @dev. It
219  * will remove any CRTC links of unused encoders and encoder links of
220  * disconnected connectors. Then it will disable all unused encoders and CRTCs
221  * either by calling their disable callback if available or by calling their
222  * dpms callback with DRM_MODE_DPMS_OFF.
223  *
224  * NOTE:
225  *
226  * This function is part of the legacy modeset helper library and will cause
227  * major confusion with atomic drivers. This is because atomic helpers guarantee
228  * to never call ->disable() hooks on a disabled function, or ->enable() hooks
229  * on an enabled functions. drm_helper_disable_unused_functions() on the other
230  * hand throws such guarantees into the wind and calls disable hooks
231  * unconditionally on unused functions.
232  */
233 void drm_helper_disable_unused_functions(struct drm_device *dev)
234 {
235 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
236 
237 	drm_modeset_lock_all(dev);
238 	__drm_helper_disable_unused_functions(dev);
239 	drm_modeset_unlock_all(dev);
240 }
241 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
242 
243 /*
244  * Check the CRTC we're going to map each output to vs. its current
245  * CRTC.  If they don't match, we have to disable the output and the CRTC
246  * since the driver will have to re-route things.
247  */
248 static void
249 drm_crtc_prepare_encoders(struct drm_device *dev)
250 {
251 	const struct drm_encoder_helper_funcs *encoder_funcs;
252 	struct drm_encoder *encoder;
253 
254 	drm_for_each_encoder(encoder, dev) {
255 		encoder_funcs = encoder->helper_private;
256 		if (!encoder_funcs)
257 			continue;
258 
259 		/* Disable unused encoders */
260 		if (encoder->crtc == NULL)
261 			drm_encoder_disable(encoder);
262 	}
263 }
264 
265 /**
266  * drm_crtc_helper_set_mode - internal helper to set a mode
267  * @crtc: CRTC to program
268  * @mode: mode to use
269  * @x: horizontal offset into the surface
270  * @y: vertical offset into the surface
271  * @old_fb: old framebuffer, for cleanup
272  *
273  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
274  * to fixup or reject the mode prior to trying to set it. This is an internal
275  * helper that drivers could e.g. use to update properties that require the
276  * entire output pipe to be disabled and re-enabled in a new configuration. For
277  * example for changing whether audio is enabled on a hdmi link or for changing
278  * panel fitter or dither attributes. It is also called by the
279  * drm_crtc_helper_set_config() helper function to drive the mode setting
280  * sequence.
281  *
282  * Returns:
283  * True if the mode was set successfully, false otherwise.
284  */
285 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
286 			      struct drm_display_mode *mode,
287 			      int x, int y,
288 			      struct drm_framebuffer *old_fb)
289 {
290 	struct drm_device *dev = crtc->dev;
291 	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
292 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
293 	const struct drm_encoder_helper_funcs *encoder_funcs;
294 	int saved_x, saved_y;
295 	bool saved_enabled;
296 	struct drm_encoder *encoder;
297 	bool ret = true;
298 
299 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
300 
301 	drm_warn_on_modeset_not_all_locked(dev);
302 
303 	saved_enabled = crtc->enabled;
304 	crtc->enabled = drm_helper_crtc_in_use(crtc);
305 	if (!crtc->enabled)
306 		return true;
307 
308 	adjusted_mode = drm_mode_duplicate(dev, mode);
309 	if (!adjusted_mode) {
310 		crtc->enabled = saved_enabled;
311 		return false;
312 	}
313 
314 	drm_mode_init(&saved_mode, &crtc->mode);
315 	drm_mode_init(&saved_hwmode, &crtc->hwmode);
316 	saved_x = crtc->x;
317 	saved_y = crtc->y;
318 
319 	/* Update crtc values up front so the driver can rely on them for mode
320 	 * setting.
321 	 */
322 	drm_mode_copy(&crtc->mode, mode);
323 	crtc->x = x;
324 	crtc->y = y;
325 
326 	/* Pass our mode to the connectors and the CRTC to give them a chance to
327 	 * adjust it according to limitations or connector properties, and also
328 	 * a chance to reject the mode entirely.
329 	 */
330 	drm_for_each_encoder(encoder, dev) {
331 
332 		if (encoder->crtc != crtc)
333 			continue;
334 
335 		encoder_funcs = encoder->helper_private;
336 		if (!encoder_funcs)
337 			continue;
338 
339 		encoder_funcs = encoder->helper_private;
340 		if (encoder_funcs->mode_fixup) {
341 			if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
342 							      adjusted_mode))) {
343 				DRM_DEBUG_KMS("Encoder fixup failed\n");
344 				goto done;
345 			}
346 		}
347 	}
348 
349 	if (crtc_funcs->mode_fixup) {
350 		if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
351 						adjusted_mode))) {
352 			DRM_DEBUG_KMS("CRTC fixup failed\n");
353 			goto done;
354 		}
355 	}
356 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
357 
358 	drm_mode_copy(&crtc->hwmode, adjusted_mode);
359 
360 	/* Prepare the encoders and CRTCs before setting the mode. */
361 	drm_for_each_encoder(encoder, dev) {
362 
363 		if (encoder->crtc != crtc)
364 			continue;
365 
366 		encoder_funcs = encoder->helper_private;
367 		if (!encoder_funcs)
368 			continue;
369 
370 		/* Disable the encoders as the first thing we do. */
371 		if (encoder_funcs->prepare)
372 			encoder_funcs->prepare(encoder);
373 	}
374 
375 	drm_crtc_prepare_encoders(dev);
376 
377 	crtc_funcs->prepare(crtc);
378 
379 	/* Set up the DPLL and any encoders state that needs to adjust or depend
380 	 * on the DPLL.
381 	 */
382 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
383 	if (!ret)
384 	    goto done;
385 
386 	drm_for_each_encoder(encoder, dev) {
387 
388 		if (encoder->crtc != crtc)
389 			continue;
390 
391 		encoder_funcs = encoder->helper_private;
392 		if (!encoder_funcs)
393 			continue;
394 
395 		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
396 			encoder->base.id, encoder->name, mode->name);
397 		if (encoder_funcs->mode_set)
398 			encoder_funcs->mode_set(encoder, mode, adjusted_mode);
399 	}
400 
401 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
402 	crtc_funcs->commit(crtc);
403 
404 	drm_for_each_encoder(encoder, dev) {
405 
406 		if (encoder->crtc != crtc)
407 			continue;
408 
409 		encoder_funcs = encoder->helper_private;
410 		if (!encoder_funcs)
411 			continue;
412 
413 		if (encoder_funcs->commit)
414 			encoder_funcs->commit(encoder);
415 	}
416 
417 	/* Calculate and store various constants which
418 	 * are later needed by vblank and swap-completion
419 	 * timestamping. They are derived from true hwmode.
420 	 */
421 	drm_calc_timestamping_constants(crtc, &crtc->hwmode);
422 
423 	/* FIXME: add subpixel order */
424 done:
425 	drm_mode_destroy(dev, adjusted_mode);
426 	if (!ret) {
427 		crtc->enabled = saved_enabled;
428 		drm_mode_copy(&crtc->mode, &saved_mode);
429 		drm_mode_copy(&crtc->hwmode, &saved_hwmode);
430 		crtc->x = saved_x;
431 		crtc->y = saved_y;
432 	}
433 
434 	return ret;
435 }
436 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
437 
438 static void
439 drm_crtc_helper_disable(struct drm_crtc *crtc)
440 {
441 	struct drm_device *dev = crtc->dev;
442 	struct drm_connector *connector;
443 	struct drm_encoder *encoder;
444 
445 	/* Decouple all encoders and their attached connectors from this crtc */
446 	drm_for_each_encoder(encoder, dev) {
447 		struct drm_connector_list_iter conn_iter;
448 
449 		if (encoder->crtc != crtc)
450 			continue;
451 
452 		drm_connector_list_iter_begin(dev, &conn_iter);
453 		drm_for_each_connector_iter(connector, &conn_iter) {
454 			if (connector->encoder != encoder)
455 				continue;
456 
457 			connector->encoder = NULL;
458 
459 			/*
460 			 * drm_helper_disable_unused_functions() ought to be
461 			 * doing this, but since we've decoupled the encoder
462 			 * from the connector above, the required connection
463 			 * between them is henceforth no longer available.
464 			 */
465 			connector->dpms = DRM_MODE_DPMS_OFF;
466 
467 			/* we keep a reference while the encoder is bound */
468 			drm_connector_put(connector);
469 		}
470 		drm_connector_list_iter_end(&conn_iter);
471 	}
472 
473 	__drm_helper_disable_unused_functions(dev);
474 }
475 
476 /*
477  * For connectors that support multiple encoders, either the
478  * .atomic_best_encoder() or .best_encoder() operation must be implemented.
479  */
480 struct drm_encoder *
481 drm_connector_get_single_encoder(struct drm_connector *connector)
482 {
483 	struct drm_encoder *encoder;
484 
485 	WARN_ON(hweight32(connector->possible_encoders) > 1);
486 	drm_connector_for_each_possible_encoder(connector, encoder)
487 		return encoder;
488 
489 	return NULL;
490 }
491 
492 /**
493  * drm_crtc_helper_set_config - set a new config from userspace
494  * @set: mode set configuration
495  * @ctx: lock acquire context, not used here
496  *
497  * The drm_crtc_helper_set_config() helper function implements the of
498  * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
499  * helpers.
500  *
501  * It first tries to locate the best encoder for each connector by calling the
502  * connector @drm_connector_helper_funcs.best_encoder helper operation.
503  *
504  * After locating the appropriate encoders, the helper function will call the
505  * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
506  * or reject it completely in which case an error will be returned to the
507  * application. If the new configuration after mode adjustment is identical to
508  * the current configuration the helper function will return without performing
509  * any other operation.
510  *
511  * If the adjusted mode is identical to the current mode but changes to the
512  * frame buffer need to be applied, the drm_crtc_helper_set_config() function
513  * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
514  *
515  * If the adjusted mode differs from the current mode, or if the
516  * ->mode_set_base() helper operation is not provided, the helper function
517  * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
518  * and ->commit() CRTC and encoder helper operations, in that order.
519  * Alternatively it can also use the dpms and disable helper operations. For
520  * details see &struct drm_crtc_helper_funcs and struct
521  * &drm_encoder_helper_funcs.
522  *
523  * This function is deprecated.  New drivers must implement atomic modeset
524  * support, for which this function is unsuitable. Instead drivers should use
525  * drm_atomic_helper_set_config().
526  *
527  * Returns:
528  * Returns 0 on success, negative errno numbers on failure.
529  */
530 int drm_crtc_helper_set_config(struct drm_mode_set *set,
531 			       struct drm_modeset_acquire_ctx *ctx)
532 {
533 	struct drm_device *dev;
534 	struct drm_crtc **save_encoder_crtcs, *new_crtc;
535 	struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
536 	bool mode_changed = false; /* if true do a full mode set */
537 	bool fb_changed = false; /* if true and !mode_changed just do a flip */
538 	struct drm_connector *connector;
539 	struct drm_connector_list_iter conn_iter;
540 	int count = 0, ro, fail = 0;
541 	const struct drm_crtc_helper_funcs *crtc_funcs;
542 	struct drm_mode_set save_set;
543 	int ret;
544 	int i;
545 
546 	DRM_DEBUG_KMS("\n");
547 
548 	BUG_ON(!set);
549 	BUG_ON(!set->crtc);
550 	BUG_ON(!set->crtc->helper_private);
551 
552 	/* Enforce sane interface api - has been abused by the fb helper. */
553 	BUG_ON(!set->mode && set->fb);
554 	BUG_ON(set->fb && set->num_connectors == 0);
555 
556 	crtc_funcs = set->crtc->helper_private;
557 
558 	dev = set->crtc->dev;
559 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
560 
561 	if (!set->mode)
562 		set->fb = NULL;
563 
564 	if (set->fb) {
565 		DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
566 			      set->crtc->base.id, set->crtc->name,
567 			      set->fb->base.id,
568 			      (int)set->num_connectors, set->x, set->y);
569 	} else {
570 		DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
571 			      set->crtc->base.id, set->crtc->name);
572 		drm_crtc_helper_disable(set->crtc);
573 		return 0;
574 	}
575 
576 	drm_warn_on_modeset_not_all_locked(dev);
577 
578 	/*
579 	 * Allocate space for the backup of all (non-pointer) encoder and
580 	 * connector data.
581 	 */
582 	save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
583 				sizeof(struct drm_crtc *), GFP_KERNEL);
584 	if (!save_encoder_crtcs)
585 		return -ENOMEM;
586 
587 	save_connector_encoders = kcalloc(dev->mode_config.num_connector,
588 				sizeof(struct drm_encoder *), GFP_KERNEL);
589 	if (!save_connector_encoders) {
590 		kfree(save_encoder_crtcs);
591 		return -ENOMEM;
592 	}
593 
594 	/*
595 	 * Copy data. Note that driver private data is not affected.
596 	 * Should anything bad happen only the expected state is
597 	 * restored, not the drivers personal bookkeeping.
598 	 */
599 	count = 0;
600 	drm_for_each_encoder(encoder, dev) {
601 		save_encoder_crtcs[count++] = encoder->crtc;
602 	}
603 
604 	count = 0;
605 	drm_connector_list_iter_begin(dev, &conn_iter);
606 	drm_for_each_connector_iter(connector, &conn_iter)
607 		save_connector_encoders[count++] = connector->encoder;
608 	drm_connector_list_iter_end(&conn_iter);
609 
610 	save_set.crtc = set->crtc;
611 	save_set.mode = &set->crtc->mode;
612 	save_set.x = set->crtc->x;
613 	save_set.y = set->crtc->y;
614 	save_set.fb = set->crtc->primary->fb;
615 
616 	/* We should be able to check here if the fb has the same properties
617 	 * and then just flip_or_move it */
618 	if (set->crtc->primary->fb != set->fb) {
619 		/* If we have no fb then treat it as a full mode set */
620 		if (set->crtc->primary->fb == NULL) {
621 			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
622 			mode_changed = true;
623 		} else if (set->fb->format != set->crtc->primary->fb->format) {
624 			mode_changed = true;
625 		} else
626 			fb_changed = true;
627 	}
628 
629 	if (set->x != set->crtc->x || set->y != set->crtc->y)
630 		fb_changed = true;
631 
632 	if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
633 		DRM_DEBUG_KMS("modes are different, full mode set\n");
634 		drm_mode_debug_printmodeline(&set->crtc->mode);
635 		drm_mode_debug_printmodeline(set->mode);
636 		mode_changed = true;
637 	}
638 
639 	/* take a reference on all unbound connectors in set, reuse the
640 	 * already taken reference for bound connectors
641 	 */
642 	for (ro = 0; ro < set->num_connectors; ro++) {
643 		if (set->connectors[ro]->encoder)
644 			continue;
645 		drm_connector_get(set->connectors[ro]);
646 	}
647 
648 	/* a) traverse passed in connector list and get encoders for them */
649 	count = 0;
650 	drm_connector_list_iter_begin(dev, &conn_iter);
651 	drm_for_each_connector_iter(connector, &conn_iter) {
652 		const struct drm_connector_helper_funcs *connector_funcs =
653 			connector->helper_private;
654 		new_encoder = connector->encoder;
655 		for (ro = 0; ro < set->num_connectors; ro++) {
656 			if (set->connectors[ro] == connector) {
657 				if (connector_funcs->best_encoder)
658 					new_encoder = connector_funcs->best_encoder(connector);
659 				else
660 					new_encoder = drm_connector_get_single_encoder(connector);
661 
662 				/* if we can't get an encoder for a connector
663 				   we are setting now - then fail */
664 				if (new_encoder == NULL)
665 					/* don't break so fail path works correct */
666 					fail = 1;
667 
668 				if (connector->dpms != DRM_MODE_DPMS_ON) {
669 					DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
670 					mode_changed = true;
671 				}
672 
673 				break;
674 			}
675 		}
676 
677 		if (new_encoder != connector->encoder) {
678 			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
679 			mode_changed = true;
680 			/* If the encoder is reused for another connector, then
681 			 * the appropriate crtc will be set later.
682 			 */
683 			if (connector->encoder)
684 				connector->encoder->crtc = NULL;
685 			connector->encoder = new_encoder;
686 		}
687 	}
688 	drm_connector_list_iter_end(&conn_iter);
689 
690 	if (fail) {
691 		ret = -EINVAL;
692 		goto fail;
693 	}
694 
695 	count = 0;
696 	drm_connector_list_iter_begin(dev, &conn_iter);
697 	drm_for_each_connector_iter(connector, &conn_iter) {
698 		if (!connector->encoder)
699 			continue;
700 
701 		if (connector->encoder->crtc == set->crtc)
702 			new_crtc = NULL;
703 		else
704 			new_crtc = connector->encoder->crtc;
705 
706 		for (ro = 0; ro < set->num_connectors; ro++) {
707 			if (set->connectors[ro] == connector)
708 				new_crtc = set->crtc;
709 		}
710 
711 		/* Make sure the new CRTC will work with the encoder */
712 		if (new_crtc &&
713 		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
714 			ret = -EINVAL;
715 			drm_connector_list_iter_end(&conn_iter);
716 			goto fail;
717 		}
718 		if (new_crtc != connector->encoder->crtc) {
719 			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
720 			mode_changed = true;
721 			connector->encoder->crtc = new_crtc;
722 		}
723 		if (new_crtc) {
724 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
725 				      connector->base.id, connector->name,
726 				      new_crtc->base.id, new_crtc->name);
727 		} else {
728 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
729 				      connector->base.id, connector->name);
730 		}
731 	}
732 	drm_connector_list_iter_end(&conn_iter);
733 
734 	/* mode_set_base is not a required function */
735 	if (fb_changed && !crtc_funcs->mode_set_base)
736 		mode_changed = true;
737 
738 	if (mode_changed) {
739 		if (drm_helper_crtc_in_use(set->crtc)) {
740 			DRM_DEBUG_KMS("attempting to set mode from"
741 					" userspace\n");
742 			drm_mode_debug_printmodeline(set->mode);
743 			set->crtc->primary->fb = set->fb;
744 			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
745 						      set->x, set->y,
746 						      save_set.fb)) {
747 				DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
748 					  set->crtc->base.id, set->crtc->name);
749 				set->crtc->primary->fb = save_set.fb;
750 				ret = -EINVAL;
751 				goto fail;
752 			}
753 			DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
754 			for (i = 0; i < set->num_connectors; i++) {
755 				DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
756 					      set->connectors[i]->name);
757 				set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
758 			}
759 		}
760 		__drm_helper_disable_unused_functions(dev);
761 	} else if (fb_changed) {
762 		set->crtc->x = set->x;
763 		set->crtc->y = set->y;
764 		set->crtc->primary->fb = set->fb;
765 		ret = crtc_funcs->mode_set_base(set->crtc,
766 						set->x, set->y, save_set.fb);
767 		if (ret != 0) {
768 			set->crtc->x = save_set.x;
769 			set->crtc->y = save_set.y;
770 			set->crtc->primary->fb = save_set.fb;
771 			goto fail;
772 		}
773 	}
774 
775 	kfree(save_connector_encoders);
776 	kfree(save_encoder_crtcs);
777 	return 0;
778 
779 fail:
780 	/* Restore all previous data. */
781 	count = 0;
782 	drm_for_each_encoder(encoder, dev) {
783 		encoder->crtc = save_encoder_crtcs[count++];
784 	}
785 
786 	count = 0;
787 	drm_connector_list_iter_begin(dev, &conn_iter);
788 	drm_for_each_connector_iter(connector, &conn_iter)
789 		connector->encoder = save_connector_encoders[count++];
790 	drm_connector_list_iter_end(&conn_iter);
791 
792 	/* after fail drop reference on all unbound connectors in set, let
793 	 * bound connectors keep their reference
794 	 */
795 	for (ro = 0; ro < set->num_connectors; ro++) {
796 		if (set->connectors[ro]->encoder)
797 			continue;
798 		drm_connector_put(set->connectors[ro]);
799 	}
800 
801 	/* Try to restore the config */
802 	if (mode_changed &&
803 	    !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
804 				      save_set.y, save_set.fb))
805 		DRM_ERROR("failed to restore config after modeset failure\n");
806 
807 	kfree(save_connector_encoders);
808 	kfree(save_encoder_crtcs);
809 	return ret;
810 }
811 EXPORT_SYMBOL(drm_crtc_helper_set_config);
812 
813 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
814 {
815 	int dpms = DRM_MODE_DPMS_OFF;
816 	struct drm_connector *connector;
817 	struct drm_connector_list_iter conn_iter;
818 	struct drm_device *dev = encoder->dev;
819 
820 	drm_connector_list_iter_begin(dev, &conn_iter);
821 	drm_for_each_connector_iter(connector, &conn_iter)
822 		if (connector->encoder == encoder)
823 			if (connector->dpms < dpms)
824 				dpms = connector->dpms;
825 	drm_connector_list_iter_end(&conn_iter);
826 
827 	return dpms;
828 }
829 
830 /* Helper which handles bridge ordering around encoder dpms */
831 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
832 {
833 	const struct drm_encoder_helper_funcs *encoder_funcs;
834 
835 	encoder_funcs = encoder->helper_private;
836 	if (!encoder_funcs)
837 		return;
838 
839 	if (encoder_funcs->dpms)
840 		encoder_funcs->dpms(encoder, mode);
841 }
842 
843 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
844 {
845 	int dpms = DRM_MODE_DPMS_OFF;
846 	struct drm_connector *connector;
847 	struct drm_connector_list_iter conn_iter;
848 	struct drm_device *dev = crtc->dev;
849 
850 	drm_connector_list_iter_begin(dev, &conn_iter);
851 	drm_for_each_connector_iter(connector, &conn_iter)
852 		if (connector->encoder && connector->encoder->crtc == crtc)
853 			if (connector->dpms < dpms)
854 				dpms = connector->dpms;
855 	drm_connector_list_iter_end(&conn_iter);
856 
857 	return dpms;
858 }
859 
860 /**
861  * drm_helper_connector_dpms() - connector dpms helper implementation
862  * @connector: affected connector
863  * @mode: DPMS mode
864  *
865  * The drm_helper_connector_dpms() helper function implements the
866  * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
867  * helpers.
868  *
869  * This is the main helper function provided by the CRTC helper framework for
870  * implementing the DPMS connector attribute. It computes the new desired DPMS
871  * state for all encoders and CRTCs in the output mesh and calls the
872  * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
873  * provided by the driver.
874  *
875  * This function is deprecated.  New drivers must implement atomic modeset
876  * support, where DPMS is handled in the DRM core.
877  *
878  * Returns:
879  * Always returns 0.
880  */
881 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
882 {
883 	struct drm_encoder *encoder = connector->encoder;
884 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
885 	int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
886 
887 	WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
888 
889 	if (mode == connector->dpms)
890 		return 0;
891 
892 	old_dpms = connector->dpms;
893 	connector->dpms = mode;
894 
895 	if (encoder)
896 		encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
897 
898 	/* from off to on, do crtc then encoder */
899 	if (mode < old_dpms) {
900 		if (crtc) {
901 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
902 
903 			if (crtc_funcs->dpms)
904 				(*crtc_funcs->dpms) (crtc,
905 						     drm_helper_choose_crtc_dpms(crtc));
906 		}
907 		if (encoder)
908 			drm_helper_encoder_dpms(encoder, encoder_dpms);
909 	}
910 
911 	/* from on to off, do encoder then crtc */
912 	if (mode > old_dpms) {
913 		if (encoder)
914 			drm_helper_encoder_dpms(encoder, encoder_dpms);
915 		if (crtc) {
916 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
917 
918 			if (crtc_funcs->dpms)
919 				(*crtc_funcs->dpms) (crtc,
920 						     drm_helper_choose_crtc_dpms(crtc));
921 		}
922 	}
923 
924 	return 0;
925 }
926 EXPORT_SYMBOL(drm_helper_connector_dpms);
927 
928 /**
929  * drm_helper_resume_force_mode - force-restore mode setting configuration
930  * @dev: drm_device which should be restored
931  *
932  * Drivers which use the mode setting helpers can use this function to
933  * force-restore the mode setting configuration e.g. on resume or when something
934  * else might have trampled over the hw state (like some overzealous old BIOSen
935  * tended to do).
936  *
937  * This helper doesn't provide a error return value since restoring the old
938  * config should never fail due to resource allocation issues since the driver
939  * has successfully set the restored configuration already. Hence this should
940  * boil down to the equivalent of a few dpms on calls, which also don't provide
941  * an error code.
942  *
943  * Drivers where simply restoring an old configuration again might fail (e.g.
944  * due to slight differences in allocating shared resources when the
945  * configuration is restored in a different order than when userspace set it up)
946  * need to use their own restore logic.
947  *
948  * This function is deprecated. New drivers should implement atomic mode-
949  * setting and use the atomic suspend/resume helpers.
950  *
951  * See also:
952  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
953  */
954 void drm_helper_resume_force_mode(struct drm_device *dev)
955 {
956 	struct drm_crtc *crtc;
957 	struct drm_encoder *encoder;
958 	const struct drm_crtc_helper_funcs *crtc_funcs;
959 	int encoder_dpms;
960 	bool ret;
961 
962 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
963 
964 	drm_modeset_lock_all(dev);
965 	drm_for_each_crtc(crtc, dev) {
966 
967 		if (!crtc->enabled)
968 			continue;
969 
970 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
971 					       crtc->x, crtc->y, crtc->primary->fb);
972 
973 		/* Restoring the old config should never fail! */
974 		if (ret == false)
975 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
976 
977 		/* Turn off outputs that were already powered off */
978 		if (drm_helper_choose_crtc_dpms(crtc)) {
979 			drm_for_each_encoder(encoder, dev) {
980 
981 				if(encoder->crtc != crtc)
982 					continue;
983 
984 				encoder_dpms = drm_helper_choose_encoder_dpms(
985 							encoder);
986 
987 				drm_helper_encoder_dpms(encoder, encoder_dpms);
988 			}
989 
990 			crtc_funcs = crtc->helper_private;
991 			if (crtc_funcs->dpms)
992 				(*crtc_funcs->dpms) (crtc,
993 						     drm_helper_choose_crtc_dpms(crtc));
994 		}
995 	}
996 
997 	/* disable the unused connectors while restoring the modesetting */
998 	__drm_helper_disable_unused_functions(dev);
999 	drm_modeset_unlock_all(dev);
1000 }
1001 EXPORT_SYMBOL(drm_helper_resume_force_mode);
1002 
1003 /**
1004  * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
1005  * @dev: DRM device whose CRTCs to turn off
1006  *
1007  * Drivers may want to call this on unload to ensure that all displays are
1008  * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
1009  *
1010  * Note: This should only be used by non-atomic legacy drivers. For an atomic
1011  * version look at drm_atomic_helper_shutdown().
1012  *
1013  * Returns:
1014  * Zero on success, error code on failure.
1015  */
1016 int drm_helper_force_disable_all(struct drm_device *dev)
1017 {
1018 	struct drm_crtc *crtc;
1019 	int ret = 0;
1020 
1021 	drm_modeset_lock_all(dev);
1022 	drm_for_each_crtc(crtc, dev)
1023 		if (crtc->enabled) {
1024 			struct drm_mode_set set = {
1025 				.crtc = crtc,
1026 			};
1027 
1028 			ret = drm_mode_set_config_internal(&set);
1029 			if (ret)
1030 				goto out;
1031 		}
1032 out:
1033 	drm_modeset_unlock_all(dev);
1034 	return ret;
1035 }
1036 EXPORT_SYMBOL(drm_helper_force_disable_all);
1037