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