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_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
41 
42 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
45 
46 /**
47  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
48  * 						connector list
49  * @dev: drm device to operate on
50  *
51  * Some userspace presumes that the first connected connector is the main
52  * display, where it's supposed to display e.g. the login screen. For
53  * laptops, this should be the main panel. Use this function to sort all
54  * (eDP/LVDS) panels to the front of the connector list, instead of
55  * painstakingly trying to initialize them in the right order.
56  */
57 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
58 {
59 	struct drm_connector *connector, *tmp;
60 	struct list_head panel_list;
61 
62 	INIT_LIST_HEAD(&panel_list);
63 
64 	list_for_each_entry_safe(connector, tmp,
65 				 &dev->mode_config.connector_list, head) {
66 		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
67 		    connector->connector_type == DRM_MODE_CONNECTOR_eDP)
68 			list_move_tail(&connector->head, &panel_list);
69 	}
70 
71 	list_splice(&panel_list, &dev->mode_config.connector_list);
72 }
73 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
74 
75 static bool drm_kms_helper_poll = true;
76 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
77 
78 static void drm_mode_validate_flag(struct drm_connector *connector,
79 				   int flags)
80 {
81 	struct drm_display_mode *mode;
82 
83 	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
84 		      DRM_MODE_FLAG_3D_MASK))
85 		return;
86 
87 	list_for_each_entry(mode, &connector->modes, head) {
88 		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
89 				!(flags & DRM_MODE_FLAG_INTERLACE))
90 			mode->status = MODE_NO_INTERLACE;
91 		if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
92 				!(flags & DRM_MODE_FLAG_DBLSCAN))
93 			mode->status = MODE_NO_DBLESCAN;
94 		if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
95 				!(flags & DRM_MODE_FLAG_3D_MASK))
96 			mode->status = MODE_NO_STEREO;
97 	}
98 
99 	return;
100 }
101 
102 /**
103  * drm_helper_probe_single_connector_modes - get complete set of display modes
104  * @connector: connector to probe
105  * @maxX: max width for modes
106  * @maxY: max height for modes
107  *
108  * LOCKING:
109  * Caller must hold mode config lock.
110  *
111  * Based on the helper callbacks implemented by @connector try to detect all
112  * valid modes.  Modes will first be added to the connector's probed_modes list,
113  * then culled (based on validity and the @maxX, @maxY parameters) and put into
114  * the normal modes list.
115  *
116  * Intended to be use as a generic implementation of the ->fill_modes()
117  * @connector vfunc for drivers that use the crtc helpers for output mode
118  * filtering and detection.
119  *
120  * RETURNS:
121  * Number of modes found on @connector.
122  */
123 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
124 					    uint32_t maxX, uint32_t maxY)
125 {
126 	struct drm_device *dev = connector->dev;
127 	struct drm_display_mode *mode;
128 	struct drm_connector_helper_funcs *connector_funcs =
129 		connector->helper_private;
130 	int count = 0;
131 	int mode_flags = 0;
132 	bool verbose_prune = true;
133 
134 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
135 			drm_get_connector_name(connector));
136 	/* set all modes to the unverified state */
137 	list_for_each_entry(mode, &connector->modes, head)
138 		mode->status = MODE_UNVERIFIED;
139 
140 	if (connector->force) {
141 		if (connector->force == DRM_FORCE_ON)
142 			connector->status = connector_status_connected;
143 		else
144 			connector->status = connector_status_disconnected;
145 		if (connector->funcs->force)
146 			connector->funcs->force(connector);
147 	} else {
148 		connector->status = connector->funcs->detect(connector, true);
149 	}
150 
151 	/* Re-enable polling in case the global poll config changed. */
152 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
153 		drm_kms_helper_poll_enable(dev);
154 
155 	dev->mode_config.poll_running = drm_kms_helper_poll;
156 
157 	if (connector->status == connector_status_disconnected) {
158 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
159 			connector->base.id, drm_get_connector_name(connector));
160 		drm_mode_connector_update_edid_property(connector, NULL);
161 		verbose_prune = false;
162 		goto prune;
163 	}
164 
165 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
166 	count = drm_load_edid_firmware(connector);
167 	if (count == 0)
168 #endif
169 		count = (*connector_funcs->get_modes)(connector);
170 
171 	if (count == 0 && connector->status == connector_status_connected)
172 		count = drm_add_modes_noedid(connector, 1024, 768);
173 	if (count == 0)
174 		goto prune;
175 
176 	drm_mode_connector_list_update(connector);
177 
178 	if (maxX && maxY)
179 		drm_mode_validate_size(dev, &connector->modes, maxX,
180 				       maxY, 0);
181 
182 	if (connector->interlace_allowed)
183 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
184 	if (connector->doublescan_allowed)
185 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
186 	if (connector->stereo_allowed)
187 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
188 	drm_mode_validate_flag(connector, mode_flags);
189 
190 	list_for_each_entry(mode, &connector->modes, head) {
191 		if (mode->status == MODE_OK)
192 			mode->status = connector_funcs->mode_valid(connector,
193 								   mode);
194 	}
195 
196 prune:
197 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
198 
199 	if (list_empty(&connector->modes))
200 		return 0;
201 
202 	list_for_each_entry(mode, &connector->modes, head)
203 		mode->vrefresh = drm_mode_vrefresh(mode);
204 
205 	drm_mode_sort(&connector->modes);
206 
207 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
208 			drm_get_connector_name(connector));
209 	list_for_each_entry(mode, &connector->modes, head) {
210 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
211 		drm_mode_debug_printmodeline(mode);
212 	}
213 
214 	return count;
215 }
216 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
217 
218 /**
219  * drm_helper_encoder_in_use - check if a given encoder is in use
220  * @encoder: encoder to check
221  *
222  * LOCKING:
223  * Caller must hold mode config lock.
224  *
225  * Walk @encoders's DRM device's mode_config and see if it's in use.
226  *
227  * RETURNS:
228  * True if @encoder is part of the mode_config, false otherwise.
229  */
230 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
231 {
232 	struct drm_connector *connector;
233 	struct drm_device *dev = encoder->dev;
234 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
235 		if (connector->encoder == encoder)
236 			return true;
237 	return false;
238 }
239 EXPORT_SYMBOL(drm_helper_encoder_in_use);
240 
241 /**
242  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
243  * @crtc: CRTC to check
244  *
245  * LOCKING:
246  * Caller must hold mode config lock.
247  *
248  * Walk @crtc's DRM device's mode_config and see if it's in use.
249  *
250  * RETURNS:
251  * True if @crtc is part of the mode_config, false otherwise.
252  */
253 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
254 {
255 	struct drm_encoder *encoder;
256 	struct drm_device *dev = crtc->dev;
257 	/* FIXME: Locking around list access? */
258 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
259 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
260 			return true;
261 	return false;
262 }
263 EXPORT_SYMBOL(drm_helper_crtc_in_use);
264 
265 static void
266 drm_encoder_disable(struct drm_encoder *encoder)
267 {
268 	struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
269 
270 	if (encoder->bridge)
271 		encoder->bridge->funcs->disable(encoder->bridge);
272 
273 	if (encoder_funcs->disable)
274 		(*encoder_funcs->disable)(encoder);
275 	else
276 		(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
277 
278 	if (encoder->bridge)
279 		encoder->bridge->funcs->post_disable(encoder->bridge);
280 }
281 
282 /**
283  * drm_helper_disable_unused_functions - disable unused objects
284  * @dev: DRM device
285  *
286  * LOCKING:
287  * Caller must hold mode config lock.
288  *
289  * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
290  * by calling its dpms function, which should power it off.
291  */
292 void drm_helper_disable_unused_functions(struct drm_device *dev)
293 {
294 	struct drm_encoder *encoder;
295 	struct drm_connector *connector;
296 	struct drm_crtc *crtc;
297 
298 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
299 		if (!connector->encoder)
300 			continue;
301 		if (connector->status == connector_status_disconnected)
302 			connector->encoder = NULL;
303 	}
304 
305 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
306 		if (!drm_helper_encoder_in_use(encoder)) {
307 			drm_encoder_disable(encoder);
308 			/* disconnector encoder from any connector */
309 			encoder->crtc = NULL;
310 		}
311 	}
312 
313 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
314 		struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
315 		crtc->enabled = drm_helper_crtc_in_use(crtc);
316 		if (!crtc->enabled) {
317 			if (crtc_funcs->disable)
318 				(*crtc_funcs->disable)(crtc);
319 			else
320 				(*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
321 			crtc->fb = NULL;
322 		}
323 	}
324 }
325 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
326 
327 /*
328  * Check the CRTC we're going to map each output to vs. its current
329  * CRTC.  If they don't match, we have to disable the output and the CRTC
330  * since the driver will have to re-route things.
331  */
332 static void
333 drm_crtc_prepare_encoders(struct drm_device *dev)
334 {
335 	struct drm_encoder_helper_funcs *encoder_funcs;
336 	struct drm_encoder *encoder;
337 
338 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
339 		encoder_funcs = encoder->helper_private;
340 		/* Disable unused encoders */
341 		if (encoder->crtc == NULL)
342 			drm_encoder_disable(encoder);
343 		/* Disable encoders whose CRTC is about to change */
344 		if (encoder_funcs->get_crtc &&
345 		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
346 			drm_encoder_disable(encoder);
347 	}
348 }
349 
350 /**
351  * drm_crtc_helper_set_mode - internal helper to set a mode
352  * @crtc: CRTC to program
353  * @mode: mode to use
354  * @x: horizontal offset into the surface
355  * @y: vertical offset into the surface
356  * @old_fb: old framebuffer, for cleanup
357  *
358  * LOCKING:
359  * Caller must hold mode config lock.
360  *
361  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
362  * to fixup or reject the mode prior to trying to set it. This is an internal
363  * helper that drivers could e.g. use to update properties that require the
364  * entire output pipe to be disabled and re-enabled in a new configuration. For
365  * example for changing whether audio is enabled on a hdmi link or for changing
366  * panel fitter or dither attributes. It is also called by the
367  * drm_crtc_helper_set_config() helper function to drive the mode setting
368  * sequence.
369  *
370  * RETURNS:
371  * True if the mode was set successfully, or false otherwise.
372  */
373 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
374 			      struct drm_display_mode *mode,
375 			      int x, int y,
376 			      struct drm_framebuffer *old_fb)
377 {
378 	struct drm_device *dev = crtc->dev;
379 	struct drm_display_mode *adjusted_mode, saved_mode;
380 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
381 	struct drm_encoder_helper_funcs *encoder_funcs;
382 	int saved_x, saved_y;
383 	bool saved_enabled;
384 	struct drm_encoder *encoder;
385 	bool ret = true;
386 
387 	saved_enabled = crtc->enabled;
388 	crtc->enabled = drm_helper_crtc_in_use(crtc);
389 	if (!crtc->enabled)
390 		return true;
391 
392 	adjusted_mode = drm_mode_duplicate(dev, mode);
393 	if (!adjusted_mode) {
394 		crtc->enabled = saved_enabled;
395 		return false;
396 	}
397 
398 	saved_mode = crtc->mode;
399 	saved_x = crtc->x;
400 	saved_y = crtc->y;
401 
402 	/* Update crtc values up front so the driver can rely on them for mode
403 	 * setting.
404 	 */
405 	crtc->mode = *mode;
406 	crtc->x = x;
407 	crtc->y = y;
408 
409 	/* Pass our mode to the connectors and the CRTC to give them a chance to
410 	 * adjust it according to limitations or connector properties, and also
411 	 * a chance to reject the mode entirely.
412 	 */
413 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
414 
415 		if (encoder->crtc != crtc)
416 			continue;
417 
418 		if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
419 			ret = encoder->bridge->funcs->mode_fixup(
420 					encoder->bridge, mode, adjusted_mode);
421 			if (!ret) {
422 				DRM_DEBUG_KMS("Bridge fixup failed\n");
423 				goto done;
424 			}
425 		}
426 
427 		encoder_funcs = encoder->helper_private;
428 		if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
429 						      adjusted_mode))) {
430 			DRM_DEBUG_KMS("Encoder fixup failed\n");
431 			goto done;
432 		}
433 	}
434 
435 	if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
436 		DRM_DEBUG_KMS("CRTC fixup failed\n");
437 		goto done;
438 	}
439 	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
440 
441 	/* Prepare the encoders and CRTCs before setting the mode. */
442 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
443 
444 		if (encoder->crtc != crtc)
445 			continue;
446 
447 		if (encoder->bridge)
448 			encoder->bridge->funcs->disable(encoder->bridge);
449 
450 		encoder_funcs = encoder->helper_private;
451 		/* Disable the encoders as the first thing we do. */
452 		encoder_funcs->prepare(encoder);
453 
454 		if (encoder->bridge)
455 			encoder->bridge->funcs->post_disable(encoder->bridge);
456 	}
457 
458 	drm_crtc_prepare_encoders(dev);
459 
460 	crtc_funcs->prepare(crtc);
461 
462 	/* Set up the DPLL and any encoders state that needs to adjust or depend
463 	 * on the DPLL.
464 	 */
465 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
466 	if (!ret)
467 	    goto done;
468 
469 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
470 
471 		if (encoder->crtc != crtc)
472 			continue;
473 
474 		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
475 			encoder->base.id, drm_get_encoder_name(encoder),
476 			mode->base.id, mode->name);
477 		encoder_funcs = encoder->helper_private;
478 		encoder_funcs->mode_set(encoder, mode, adjusted_mode);
479 
480 		if (encoder->bridge && encoder->bridge->funcs->mode_set)
481 			encoder->bridge->funcs->mode_set(encoder->bridge, mode,
482 					adjusted_mode);
483 	}
484 
485 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
486 	crtc_funcs->commit(crtc);
487 
488 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
489 
490 		if (encoder->crtc != crtc)
491 			continue;
492 
493 		if (encoder->bridge)
494 			encoder->bridge->funcs->pre_enable(encoder->bridge);
495 
496 		encoder_funcs = encoder->helper_private;
497 		encoder_funcs->commit(encoder);
498 
499 		if (encoder->bridge)
500 			encoder->bridge->funcs->enable(encoder->bridge);
501 	}
502 
503 	/* Store real post-adjustment hardware mode. */
504 	crtc->hwmode = *adjusted_mode;
505 
506 	/* Calculate and store various constants which
507 	 * are later needed by vblank and swap-completion
508 	 * timestamping. They are derived from true hwmode.
509 	 */
510 	drm_calc_timestamping_constants(crtc, &crtc->hwmode);
511 
512 	/* FIXME: add subpixel order */
513 done:
514 	drm_mode_destroy(dev, adjusted_mode);
515 	if (!ret) {
516 		crtc->enabled = saved_enabled;
517 		crtc->mode = saved_mode;
518 		crtc->x = saved_x;
519 		crtc->y = saved_y;
520 	}
521 
522 	return ret;
523 }
524 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
525 
526 
527 static int
528 drm_crtc_helper_disable(struct drm_crtc *crtc)
529 {
530 	struct drm_device *dev = crtc->dev;
531 	struct drm_connector *connector;
532 	struct drm_encoder *encoder;
533 
534 	/* Decouple all encoders and their attached connectors from this crtc */
535 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
536 		if (encoder->crtc != crtc)
537 			continue;
538 
539 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
540 			if (connector->encoder != encoder)
541 				continue;
542 
543 			connector->encoder = NULL;
544 
545 			/*
546 			 * drm_helper_disable_unused_functions() ought to be
547 			 * doing this, but since we've decoupled the encoder
548 			 * from the connector above, the required connection
549 			 * between them is henceforth no longer available.
550 			 */
551 			connector->dpms = DRM_MODE_DPMS_OFF;
552 		}
553 	}
554 
555 	drm_helper_disable_unused_functions(dev);
556 	return 0;
557 }
558 
559 /**
560  * drm_crtc_helper_set_config - set a new config from userspace
561  * @set: mode set configuration
562  *
563  * LOCKING:
564  * Caller must hold mode config lock.
565  *
566  * Setup a new configuration, provided by the upper layers (either an ioctl call
567  * from userspace or internally e.g. from the fbdev suppport code) in @set, and
568  * enable it. This is the main helper functions for drivers that implement
569  * kernel mode setting with the crtc helper functions and the assorted
570  * ->prepare(), ->modeset() and ->commit() helper callbacks.
571  *
572  * RETURNS:
573  * Returns 0 on success, -ERRNO on failure.
574  */
575 int drm_crtc_helper_set_config(struct drm_mode_set *set)
576 {
577 	struct drm_device *dev;
578 	struct drm_crtc *new_crtc;
579 	struct drm_encoder *save_encoders, *new_encoder, *encoder;
580 	bool mode_changed = false; /* if true do a full mode set */
581 	bool fb_changed = false; /* if true and !mode_changed just do a flip */
582 	struct drm_connector *save_connectors, *connector;
583 	int count = 0, ro, fail = 0;
584 	struct drm_crtc_helper_funcs *crtc_funcs;
585 	struct drm_mode_set save_set;
586 	int ret;
587 	int i;
588 
589 	DRM_DEBUG_KMS("\n");
590 
591 	BUG_ON(!set);
592 	BUG_ON(!set->crtc);
593 	BUG_ON(!set->crtc->helper_private);
594 
595 	/* Enforce sane interface api - has been abused by the fb helper. */
596 	BUG_ON(!set->mode && set->fb);
597 	BUG_ON(set->fb && set->num_connectors == 0);
598 
599 	crtc_funcs = set->crtc->helper_private;
600 
601 	if (!set->mode)
602 		set->fb = NULL;
603 
604 	if (set->fb) {
605 		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
606 				set->crtc->base.id, set->fb->base.id,
607 				(int)set->num_connectors, set->x, set->y);
608 	} else {
609 		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
610 		return drm_crtc_helper_disable(set->crtc);
611 	}
612 
613 	dev = set->crtc->dev;
614 
615 	/*
616 	 * Allocate space for the backup of all (non-pointer) encoder and
617 	 * connector data.
618 	 */
619 	save_encoders = kzalloc(dev->mode_config.num_encoder *
620 				sizeof(struct drm_encoder), GFP_KERNEL);
621 	if (!save_encoders)
622 		return -ENOMEM;
623 
624 	save_connectors = kzalloc(dev->mode_config.num_connector *
625 				sizeof(struct drm_connector), GFP_KERNEL);
626 	if (!save_connectors) {
627 		kfree(save_encoders);
628 		return -ENOMEM;
629 	}
630 
631 	/*
632 	 * Copy data. Note that driver private data is not affected.
633 	 * Should anything bad happen only the expected state is
634 	 * restored, not the drivers personal bookkeeping.
635 	 */
636 	count = 0;
637 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
638 		save_encoders[count++] = *encoder;
639 	}
640 
641 	count = 0;
642 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
643 		save_connectors[count++] = *connector;
644 	}
645 
646 	save_set.crtc = set->crtc;
647 	save_set.mode = &set->crtc->mode;
648 	save_set.x = set->crtc->x;
649 	save_set.y = set->crtc->y;
650 	save_set.fb = set->crtc->fb;
651 
652 	/* We should be able to check here if the fb has the same properties
653 	 * and then just flip_or_move it */
654 	if (set->crtc->fb != set->fb) {
655 		/* If we have no fb then treat it as a full mode set */
656 		if (set->crtc->fb == NULL) {
657 			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
658 			mode_changed = true;
659 		} else if (set->fb == NULL) {
660 			mode_changed = true;
661 		} else if (set->fb->pixel_format !=
662 			   set->crtc->fb->pixel_format) {
663 			mode_changed = true;
664 		} else
665 			fb_changed = true;
666 	}
667 
668 	if (set->x != set->crtc->x || set->y != set->crtc->y)
669 		fb_changed = true;
670 
671 	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
672 		DRM_DEBUG_KMS("modes are different, full mode set\n");
673 		drm_mode_debug_printmodeline(&set->crtc->mode);
674 		drm_mode_debug_printmodeline(set->mode);
675 		mode_changed = true;
676 	}
677 
678 	/* a) traverse passed in connector list and get encoders for them */
679 	count = 0;
680 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
681 		struct drm_connector_helper_funcs *connector_funcs =
682 			connector->helper_private;
683 		new_encoder = connector->encoder;
684 		for (ro = 0; ro < set->num_connectors; ro++) {
685 			if (set->connectors[ro] == connector) {
686 				new_encoder = connector_funcs->best_encoder(connector);
687 				/* if we can't get an encoder for a connector
688 				   we are setting now - then fail */
689 				if (new_encoder == NULL)
690 					/* don't break so fail path works correct */
691 					fail = 1;
692 				break;
693 
694 				if (connector->dpms != DRM_MODE_DPMS_ON) {
695 					DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
696 					mode_changed = true;
697 				}
698 			}
699 		}
700 
701 		if (new_encoder != connector->encoder) {
702 			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
703 			mode_changed = true;
704 			/* If the encoder is reused for another connector, then
705 			 * the appropriate crtc will be set later.
706 			 */
707 			if (connector->encoder)
708 				connector->encoder->crtc = NULL;
709 			connector->encoder = new_encoder;
710 		}
711 	}
712 
713 	if (fail) {
714 		ret = -EINVAL;
715 		goto fail;
716 	}
717 
718 	count = 0;
719 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
720 		if (!connector->encoder)
721 			continue;
722 
723 		if (connector->encoder->crtc == set->crtc)
724 			new_crtc = NULL;
725 		else
726 			new_crtc = connector->encoder->crtc;
727 
728 		for (ro = 0; ro < set->num_connectors; ro++) {
729 			if (set->connectors[ro] == connector)
730 				new_crtc = set->crtc;
731 		}
732 
733 		/* Make sure the new CRTC will work with the encoder */
734 		if (new_crtc &&
735 		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
736 			ret = -EINVAL;
737 			goto fail;
738 		}
739 		if (new_crtc != connector->encoder->crtc) {
740 			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
741 			mode_changed = true;
742 			connector->encoder->crtc = new_crtc;
743 		}
744 		if (new_crtc) {
745 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
746 				connector->base.id, drm_get_connector_name(connector),
747 				new_crtc->base.id);
748 		} else {
749 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
750 				connector->base.id, drm_get_connector_name(connector));
751 		}
752 	}
753 
754 	/* mode_set_base is not a required function */
755 	if (fb_changed && !crtc_funcs->mode_set_base)
756 		mode_changed = true;
757 
758 	if (mode_changed) {
759 		if (drm_helper_crtc_in_use(set->crtc)) {
760 			DRM_DEBUG_KMS("attempting to set mode from"
761 					" userspace\n");
762 			drm_mode_debug_printmodeline(set->mode);
763 			set->crtc->fb = set->fb;
764 			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
765 						      set->x, set->y,
766 						      save_set.fb)) {
767 				DRM_ERROR("failed to set mode on [CRTC:%d]\n",
768 					  set->crtc->base.id);
769 				set->crtc->fb = save_set.fb;
770 				ret = -EINVAL;
771 				goto fail;
772 			}
773 			DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
774 			for (i = 0; i < set->num_connectors; i++) {
775 				DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
776 					      drm_get_connector_name(set->connectors[i]));
777 				set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
778 			}
779 		}
780 		drm_helper_disable_unused_functions(dev);
781 	} else if (fb_changed) {
782 		set->crtc->x = set->x;
783 		set->crtc->y = set->y;
784 		set->crtc->fb = set->fb;
785 		ret = crtc_funcs->mode_set_base(set->crtc,
786 						set->x, set->y, save_set.fb);
787 		if (ret != 0) {
788 			set->crtc->x = save_set.x;
789 			set->crtc->y = save_set.y;
790 			set->crtc->fb = save_set.fb;
791 			goto fail;
792 		}
793 	}
794 
795 	kfree(save_connectors);
796 	kfree(save_encoders);
797 	return 0;
798 
799 fail:
800 	/* Restore all previous data. */
801 	count = 0;
802 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
803 		*encoder = save_encoders[count++];
804 	}
805 
806 	count = 0;
807 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
808 		*connector = save_connectors[count++];
809 	}
810 
811 	/* Try to restore the config */
812 	if (mode_changed &&
813 	    !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
814 				      save_set.y, save_set.fb))
815 		DRM_ERROR("failed to restore config after modeset failure\n");
816 
817 	kfree(save_connectors);
818 	kfree(save_encoders);
819 	return ret;
820 }
821 EXPORT_SYMBOL(drm_crtc_helper_set_config);
822 
823 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
824 {
825 	int dpms = DRM_MODE_DPMS_OFF;
826 	struct drm_connector *connector;
827 	struct drm_device *dev = encoder->dev;
828 
829 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
830 		if (connector->encoder == encoder)
831 			if (connector->dpms < dpms)
832 				dpms = connector->dpms;
833 	return dpms;
834 }
835 
836 /* Helper which handles bridge ordering around encoder dpms */
837 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
838 {
839 	struct drm_bridge *bridge = encoder->bridge;
840 	struct drm_encoder_helper_funcs *encoder_funcs;
841 
842 	if (bridge) {
843 		if (mode == DRM_MODE_DPMS_ON)
844 			bridge->funcs->pre_enable(bridge);
845 		else
846 			bridge->funcs->disable(bridge);
847 	}
848 
849 	encoder_funcs = encoder->helper_private;
850 	if (encoder_funcs->dpms)
851 		encoder_funcs->dpms(encoder, mode);
852 
853 	if (bridge) {
854 		if (mode == DRM_MODE_DPMS_ON)
855 			bridge->funcs->enable(bridge);
856 		else
857 			bridge->funcs->post_disable(bridge);
858 	}
859 }
860 
861 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
862 {
863 	int dpms = DRM_MODE_DPMS_OFF;
864 	struct drm_connector *connector;
865 	struct drm_device *dev = crtc->dev;
866 
867 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
868 		if (connector->encoder && connector->encoder->crtc == crtc)
869 			if (connector->dpms < dpms)
870 				dpms = connector->dpms;
871 	return dpms;
872 }
873 
874 /**
875  * drm_helper_connector_dpms() - connector dpms helper implementation
876  * @connector: affected connector
877  * @mode: DPMS mode
878  *
879  * This is the main helper function provided by the crtc helper framework for
880  * implementing the DPMS connector attribute. It computes the new desired DPMS
881  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
882  * callback provided by the driver appropriately.
883  */
884 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
885 {
886 	struct drm_encoder *encoder = connector->encoder;
887 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
888 	int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
889 
890 	if (mode == connector->dpms)
891 		return;
892 
893 	old_dpms = connector->dpms;
894 	connector->dpms = mode;
895 
896 	if (encoder)
897 		encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
898 
899 	/* from off to on, do crtc then encoder */
900 	if (mode < old_dpms) {
901 		if (crtc) {
902 			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
903 			if (crtc_funcs->dpms)
904 				(*crtc_funcs->dpms) (crtc,
905 						     drm_helper_choose_crtc_dpms(crtc));
906 		}
907 		if (encoder)
908 			drm_helper_encoder_dpms(encoder, encoder_dpms);
909 	}
910 
911 	/* from on to off, do encoder then crtc */
912 	if (mode > old_dpms) {
913 		if (encoder)
914 			drm_helper_encoder_dpms(encoder, encoder_dpms);
915 		if (crtc) {
916 			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
917 			if (crtc_funcs->dpms)
918 				(*crtc_funcs->dpms) (crtc,
919 						     drm_helper_choose_crtc_dpms(crtc));
920 		}
921 	}
922 
923 	return;
924 }
925 EXPORT_SYMBOL(drm_helper_connector_dpms);
926 
927 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
928 				   struct drm_mode_fb_cmd2 *mode_cmd)
929 {
930 	int i;
931 
932 	fb->width = mode_cmd->width;
933 	fb->height = mode_cmd->height;
934 	for (i = 0; i < 4; i++) {
935 		fb->pitches[i] = mode_cmd->pitches[i];
936 		fb->offsets[i] = mode_cmd->offsets[i];
937 	}
938 	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
939 				    &fb->bits_per_pixel);
940 	fb->pixel_format = mode_cmd->pixel_format;
941 
942 	return 0;
943 }
944 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
945 
946 int drm_helper_resume_force_mode(struct drm_device *dev)
947 {
948 	struct drm_crtc *crtc;
949 	struct drm_encoder *encoder;
950 	struct drm_crtc_helper_funcs *crtc_funcs;
951 	int ret, encoder_dpms;
952 
953 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
954 
955 		if (!crtc->enabled)
956 			continue;
957 
958 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
959 					       crtc->x, crtc->y, crtc->fb);
960 
961 		if (ret == false)
962 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
963 
964 		/* Turn off outputs that were already powered off */
965 		if (drm_helper_choose_crtc_dpms(crtc)) {
966 			list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
967 
968 				if(encoder->crtc != crtc)
969 					continue;
970 
971 				encoder_dpms = drm_helper_choose_encoder_dpms(
972 							encoder);
973 
974 				drm_helper_encoder_dpms(encoder, encoder_dpms);
975 			}
976 
977 			crtc_funcs = crtc->helper_private;
978 			if (crtc_funcs->dpms)
979 				(*crtc_funcs->dpms) (crtc,
980 						     drm_helper_choose_crtc_dpms(crtc));
981 		}
982 	}
983 	/* disable the unused connectors while restoring the modesetting */
984 	drm_helper_disable_unused_functions(dev);
985 	return 0;
986 }
987 EXPORT_SYMBOL(drm_helper_resume_force_mode);
988 
989 void drm_kms_helper_hotplug_event(struct drm_device *dev)
990 {
991 	/* send a uevent + call fbdev */
992 	drm_sysfs_hotplug_event(dev);
993 	if (dev->mode_config.funcs->output_poll_changed)
994 		dev->mode_config.funcs->output_poll_changed(dev);
995 }
996 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
997 
998 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
999 static void output_poll_execute(struct work_struct *work)
1000 {
1001 	struct delayed_work *delayed_work = to_delayed_work(work);
1002 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
1003 	struct drm_connector *connector;
1004 	enum drm_connector_status old_status;
1005 	bool repoll = false, changed = false;
1006 
1007 	if (!drm_kms_helper_poll)
1008 		return;
1009 
1010 	mutex_lock(&dev->mode_config.mutex);
1011 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1012 
1013 		/* Ignore forced connectors. */
1014 		if (connector->force)
1015 			continue;
1016 
1017 		/* Ignore HPD capable connectors and connectors where we don't
1018 		 * want any hotplug detection at all for polling. */
1019 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
1020 			continue;
1021 
1022 		repoll = true;
1023 
1024 		old_status = connector->status;
1025 		/* if we are connected and don't want to poll for disconnect
1026 		   skip it */
1027 		if (old_status == connector_status_connected &&
1028 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1029 			continue;
1030 
1031 		connector->status = connector->funcs->detect(connector, false);
1032 		if (old_status != connector->status) {
1033 			const char *old, *new;
1034 
1035 			old = drm_get_connector_status_name(old_status);
1036 			new = drm_get_connector_status_name(connector->status);
1037 
1038 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1039 				      "status updated from %s to %s\n",
1040 				      connector->base.id,
1041 				      drm_get_connector_name(connector),
1042 				      old, new);
1043 
1044 			changed = true;
1045 		}
1046 	}
1047 
1048 	mutex_unlock(&dev->mode_config.mutex);
1049 
1050 	if (changed)
1051 		drm_kms_helper_hotplug_event(dev);
1052 
1053 	if (repoll)
1054 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1055 }
1056 
1057 void drm_kms_helper_poll_disable(struct drm_device *dev)
1058 {
1059 	if (!dev->mode_config.poll_enabled)
1060 		return;
1061 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1062 }
1063 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1064 
1065 void drm_kms_helper_poll_enable(struct drm_device *dev)
1066 {
1067 	bool poll = false;
1068 	struct drm_connector *connector;
1069 
1070 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1071 		return;
1072 
1073 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1074 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1075 					 DRM_CONNECTOR_POLL_DISCONNECT))
1076 			poll = true;
1077 	}
1078 
1079 	if (poll)
1080 		schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1081 }
1082 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1083 
1084 void drm_kms_helper_poll_init(struct drm_device *dev)
1085 {
1086 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1087 	dev->mode_config.poll_enabled = true;
1088 
1089 	drm_kms_helper_poll_enable(dev);
1090 }
1091 EXPORT_SYMBOL(drm_kms_helper_poll_init);
1092 
1093 void drm_kms_helper_poll_fini(struct drm_device *dev)
1094 {
1095 	drm_kms_helper_poll_disable(dev);
1096 }
1097 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1098 
1099 bool drm_helper_hpd_irq_event(struct drm_device *dev)
1100 {
1101 	struct drm_connector *connector;
1102 	enum drm_connector_status old_status;
1103 	bool changed = false;
1104 
1105 	if (!dev->mode_config.poll_enabled)
1106 		return false;
1107 
1108 	mutex_lock(&dev->mode_config.mutex);
1109 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1110 
1111 		/* Only handle HPD capable connectors. */
1112 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1113 			continue;
1114 
1115 		old_status = connector->status;
1116 
1117 		connector->status = connector->funcs->detect(connector, false);
1118 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1119 			      connector->base.id,
1120 			      drm_get_connector_name(connector),
1121 			      drm_get_connector_status_name(old_status),
1122 			      drm_get_connector_status_name(connector->status));
1123 		if (old_status != connector->status)
1124 			changed = true;
1125 	}
1126 
1127 	mutex_unlock(&dev->mode_config.mutex);
1128 
1129 	if (changed)
1130 		drm_kms_helper_hotplug_event(dev);
1131 
1132 	return changed;
1133 }
1134 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1135