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