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