xref: /linux/drivers/gpu/drm/drm_crtc_helper.c (revision 3f6c148df42d98f0991baf4353497d380a30bc19)
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 "drmP.h"
33 #include "drm_crtc.h"
34 #include "drm_crtc_helper.h"
35 #include "drm_fb_helper.h"
36 
37 static void drm_mode_validate_flag(struct drm_connector *connector,
38 				   int flags)
39 {
40 	struct drm_display_mode *mode, *t;
41 
42 	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
43 		return;
44 
45 	list_for_each_entry_safe(mode, t, &connector->modes, head) {
46 		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
47 				!(flags & DRM_MODE_FLAG_INTERLACE))
48 			mode->status = MODE_NO_INTERLACE;
49 		if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
50 				!(flags & DRM_MODE_FLAG_DBLSCAN))
51 			mode->status = MODE_NO_DBLESCAN;
52 	}
53 
54 	return;
55 }
56 
57 /**
58  * drm_helper_probe_connector_modes - get complete set of display modes
59  * @dev: DRM device
60  * @maxX: max width for modes
61  * @maxY: max height for modes
62  *
63  * LOCKING:
64  * Caller must hold mode config lock.
65  *
66  * Based on @dev's mode_config layout, scan all the connectors and try to detect
67  * modes on them.  Modes will first be added to the connector's probed_modes
68  * list, then culled (based on validity and the @maxX, @maxY parameters) and
69  * put into the normal modes list.
70  *
71  * Intended to be used either at bootup time or when major configuration
72  * changes have occurred.
73  *
74  * FIXME: take into account monitor limits
75  *
76  * RETURNS:
77  * Number of modes found on @connector.
78  */
79 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
80 					    uint32_t maxX, uint32_t maxY)
81 {
82 	struct drm_device *dev = connector->dev;
83 	struct drm_display_mode *mode, *t;
84 	struct drm_connector_helper_funcs *connector_funcs =
85 		connector->helper_private;
86 	int count = 0;
87 	int mode_flags = 0;
88 
89 	DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
90 	/* set all modes to the unverified state */
91 	list_for_each_entry_safe(mode, t, &connector->modes, head)
92 		mode->status = MODE_UNVERIFIED;
93 
94 	if (connector->force) {
95 		if (connector->force == DRM_FORCE_ON)
96 			connector->status = connector_status_connected;
97 		else
98 			connector->status = connector_status_disconnected;
99 		if (connector->funcs->force)
100 			connector->funcs->force(connector);
101 	} else
102 		connector->status = connector->funcs->detect(connector);
103 
104 	if (connector->status == connector_status_disconnected) {
105 		DRM_DEBUG_KMS("%s is disconnected\n",
106 			  drm_get_connector_name(connector));
107 		drm_mode_connector_update_edid_property(connector, NULL);
108 		goto prune;
109 	}
110 
111 	count = (*connector_funcs->get_modes)(connector);
112 	if (!count) {
113 		count = drm_add_modes_noedid(connector, 1024, 768);
114 		if (!count)
115 			return 0;
116 	}
117 
118 	drm_mode_connector_list_update(connector);
119 
120 	if (maxX && maxY)
121 		drm_mode_validate_size(dev, &connector->modes, maxX,
122 				       maxY, 0);
123 
124 	if (connector->interlace_allowed)
125 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
126 	if (connector->doublescan_allowed)
127 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
128 	drm_mode_validate_flag(connector, mode_flags);
129 
130 	list_for_each_entry_safe(mode, t, &connector->modes, head) {
131 		if (mode->status == MODE_OK)
132 			mode->status = connector_funcs->mode_valid(connector,
133 								   mode);
134 	}
135 
136 prune:
137 	drm_mode_prune_invalid(dev, &connector->modes, true);
138 
139 	if (list_empty(&connector->modes))
140 		return 0;
141 
142 	drm_mode_sort(&connector->modes);
143 
144 	DRM_DEBUG_KMS("Probed modes for %s\n",
145 				drm_get_connector_name(connector));
146 	list_for_each_entry_safe(mode, t, &connector->modes, head) {
147 		mode->vrefresh = drm_mode_vrefresh(mode);
148 
149 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
150 		drm_mode_debug_printmodeline(mode);
151 	}
152 
153 	return count;
154 }
155 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
156 
157 int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
158 				      uint32_t maxY)
159 {
160 	struct drm_connector *connector;
161 	int count = 0;
162 
163 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
164 		count += drm_helper_probe_single_connector_modes(connector,
165 								 maxX, maxY);
166 	}
167 
168 	return count;
169 }
170 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
171 
172 /**
173  * drm_helper_encoder_in_use - check if a given encoder is in use
174  * @encoder: encoder to check
175  *
176  * LOCKING:
177  * Caller must hold mode config lock.
178  *
179  * Walk @encoders's DRM device's mode_config and see if it's in use.
180  *
181  * RETURNS:
182  * True if @encoder is part of the mode_config, false otherwise.
183  */
184 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
185 {
186 	struct drm_connector *connector;
187 	struct drm_device *dev = encoder->dev;
188 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
189 		if (connector->encoder == encoder)
190 			return true;
191 	return false;
192 }
193 EXPORT_SYMBOL(drm_helper_encoder_in_use);
194 
195 /**
196  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
197  * @crtc: CRTC to check
198  *
199  * LOCKING:
200  * Caller must hold mode config lock.
201  *
202  * Walk @crtc's DRM device's mode_config and see if it's in use.
203  *
204  * RETURNS:
205  * True if @crtc is part of the mode_config, false otherwise.
206  */
207 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
208 {
209 	struct drm_encoder *encoder;
210 	struct drm_device *dev = crtc->dev;
211 	/* FIXME: Locking around list access? */
212 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
213 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
214 			return true;
215 	return false;
216 }
217 EXPORT_SYMBOL(drm_helper_crtc_in_use);
218 
219 /**
220  * drm_helper_disable_unused_functions - disable unused objects
221  * @dev: DRM device
222  *
223  * LOCKING:
224  * Caller must hold mode config lock.
225  *
226  * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
227  * by calling its dpms function, which should power it off.
228  */
229 void drm_helper_disable_unused_functions(struct drm_device *dev)
230 {
231 	struct drm_encoder *encoder;
232 	struct drm_connector *connector;
233 	struct drm_encoder_helper_funcs *encoder_funcs;
234 	struct drm_crtc *crtc;
235 
236 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
237 		if (!connector->encoder)
238 			continue;
239 		if (connector->status == connector_status_disconnected)
240 			connector->encoder = NULL;
241 	}
242 
243 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
244 		encoder_funcs = encoder->helper_private;
245 		if (!drm_helper_encoder_in_use(encoder)) {
246 			if (encoder_funcs->disable)
247 				(*encoder_funcs->disable)(encoder);
248 			else
249 				(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
250 			/* disconnector encoder from any connector */
251 			encoder->crtc = NULL;
252 		}
253 	}
254 
255 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 		struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 		crtc->enabled = drm_helper_crtc_in_use(crtc);
258 		if (!crtc->enabled) {
259 			crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
260 			crtc->fb = NULL;
261 		}
262 	}
263 }
264 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
265 
266 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
267 {
268 	struct drm_display_mode *mode;
269 
270 	list_for_each_entry(mode, &connector->modes, head) {
271 		if (drm_mode_width(mode) > width ||
272 		    drm_mode_height(mode) > height)
273 			continue;
274 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
275 			return mode;
276 	}
277 	return NULL;
278 }
279 
280 static bool drm_has_cmdline_mode(struct drm_connector *connector)
281 {
282 	struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
283 	struct drm_fb_helper_cmdline_mode *cmdline_mode;
284 
285 	if (!fb_help_conn)
286 		return false;
287 
288 	cmdline_mode = &fb_help_conn->cmdline_mode;
289 	return cmdline_mode->specified;
290 }
291 
292 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
293 {
294 	struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
295 	struct drm_fb_helper_cmdline_mode *cmdline_mode;
296 	struct drm_display_mode *mode = NULL;
297 
298 	if (!fb_help_conn)
299 		return mode;
300 
301 	cmdline_mode = &fb_help_conn->cmdline_mode;
302 	if (cmdline_mode->specified == false)
303 		return mode;
304 
305 	/* attempt to find a matching mode in the list of modes
306 	 *  we have gotten so far, if not add a CVT mode that conforms
307 	 */
308 	if (cmdline_mode->rb || cmdline_mode->margins)
309 		goto create_mode;
310 
311 	list_for_each_entry(mode, &connector->modes, head) {
312 		/* check width/height */
313 		if (mode->hdisplay != cmdline_mode->xres ||
314 		    mode->vdisplay != cmdline_mode->yres)
315 			continue;
316 
317 		if (cmdline_mode->refresh_specified) {
318 			if (mode->vrefresh != cmdline_mode->refresh)
319 				continue;
320 		}
321 
322 		if (cmdline_mode->interlace) {
323 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
324 				continue;
325 		}
326 		return mode;
327 	}
328 
329 create_mode:
330 	mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
331 			    cmdline_mode->yres,
332 			    cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
333 			    cmdline_mode->rb, cmdline_mode->interlace,
334 			    cmdline_mode->margins);
335 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
336 	list_add(&mode->head, &connector->modes);
337 	return mode;
338 }
339 
340 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
341 {
342 	bool enable;
343 
344 	if (strict) {
345 		enable = connector->status == connector_status_connected;
346 	} else {
347 		enable = connector->status != connector_status_disconnected;
348 	}
349 	return enable;
350 }
351 
352 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
353 {
354 	bool any_enabled = false;
355 	struct drm_connector *connector;
356 	int i = 0;
357 
358 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
359 		enabled[i] = drm_connector_enabled(connector, true);
360 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
361 			  enabled[i] ? "yes" : "no");
362 		any_enabled |= enabled[i];
363 		i++;
364 	}
365 
366 	if (any_enabled)
367 		return;
368 
369 	i = 0;
370 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
371 		enabled[i] = drm_connector_enabled(connector, false);
372 		i++;
373 	}
374 }
375 
376 static bool drm_target_preferred(struct drm_device *dev,
377 				 struct drm_display_mode **modes,
378 				 bool *enabled, int width, int height)
379 {
380 	struct drm_connector *connector;
381 	int i = 0;
382 
383 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
384 
385 		if (enabled[i] == false) {
386 			i++;
387 			continue;
388 		}
389 
390 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
391 			      connector->base.id);
392 
393 		/* got for command line mode first */
394 		modes[i] = drm_pick_cmdline_mode(connector, width, height);
395 		if (!modes[i]) {
396 			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
397 				      connector->base.id);
398 			modes[i] = drm_has_preferred_mode(connector, width, height);
399 		}
400 		/* No preferred modes, pick one off the list */
401 		if (!modes[i] && !list_empty(&connector->modes)) {
402 			list_for_each_entry(modes[i], &connector->modes, head)
403 				break;
404 		}
405 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
406 			  "none");
407 		i++;
408 	}
409 	return true;
410 }
411 
412 static int drm_pick_crtcs(struct drm_device *dev,
413 			  struct drm_crtc **best_crtcs,
414 			  struct drm_display_mode **modes,
415 			  int n, int width, int height)
416 {
417 	int c, o;
418 	struct drm_connector *connector;
419 	struct drm_connector_helper_funcs *connector_funcs;
420 	struct drm_encoder *encoder;
421 	struct drm_crtc *best_crtc;
422 	int my_score, best_score, score;
423 	struct drm_crtc **crtcs, *crtc;
424 
425 	if (n == dev->mode_config.num_connector)
426 		return 0;
427 	c = 0;
428 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
429 		if (c == n)
430 			break;
431 		c++;
432 	}
433 
434 	best_crtcs[n] = NULL;
435 	best_crtc = NULL;
436 	best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
437 	if (modes[n] == NULL)
438 		return best_score;
439 
440 	crtcs = kmalloc(dev->mode_config.num_connector *
441 			sizeof(struct drm_crtc *), GFP_KERNEL);
442 	if (!crtcs)
443 		return best_score;
444 
445 	my_score = 1;
446 	if (connector->status == connector_status_connected)
447 		my_score++;
448 	if (drm_has_cmdline_mode(connector))
449 		my_score++;
450 	if (drm_has_preferred_mode(connector, width, height))
451 		my_score++;
452 
453 	connector_funcs = connector->helper_private;
454 	encoder = connector_funcs->best_encoder(connector);
455 	if (!encoder)
456 		goto out;
457 
458 	connector->encoder = encoder;
459 
460 	/* select a crtc for this connector and then attempt to configure
461 	   remaining connectors */
462 	c = 0;
463 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
464 
465 		if ((encoder->possible_crtcs & (1 << c)) == 0) {
466 			c++;
467 			continue;
468 		}
469 
470 		for (o = 0; o < n; o++)
471 			if (best_crtcs[o] == crtc)
472 				break;
473 
474 		if (o < n) {
475 			/* ignore cloning for now */
476 			c++;
477 			continue;
478 		}
479 
480 		crtcs[n] = crtc;
481 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
482 		score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
483 						  width, height);
484 		if (score > best_score) {
485 			best_crtc = crtc;
486 			best_score = score;
487 			memcpy(best_crtcs, crtcs,
488 			       dev->mode_config.num_connector *
489 			       sizeof(struct drm_crtc *));
490 		}
491 		c++;
492 	}
493 out:
494 	kfree(crtcs);
495 	return best_score;
496 }
497 
498 static void drm_setup_crtcs(struct drm_device *dev)
499 {
500 	struct drm_crtc **crtcs;
501 	struct drm_display_mode **modes;
502 	struct drm_encoder *encoder;
503 	struct drm_connector *connector;
504 	bool *enabled;
505 	int width, height;
506 	int i, ret;
507 
508 	DRM_DEBUG_KMS("\n");
509 
510 	width = dev->mode_config.max_width;
511 	height = dev->mode_config.max_height;
512 
513 	/* clean out all the encoder/crtc combos */
514 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
515 		encoder->crtc = NULL;
516 	}
517 
518 	crtcs = kcalloc(dev->mode_config.num_connector,
519 			sizeof(struct drm_crtc *), GFP_KERNEL);
520 	modes = kcalloc(dev->mode_config.num_connector,
521 			sizeof(struct drm_display_mode *), GFP_KERNEL);
522 	enabled = kcalloc(dev->mode_config.num_connector,
523 			  sizeof(bool), GFP_KERNEL);
524 
525 	drm_enable_connectors(dev, enabled);
526 
527 	ret = drm_target_preferred(dev, modes, enabled, width, height);
528 	if (!ret)
529 		DRM_ERROR("Unable to find initial modes\n");
530 
531 	DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
532 
533 	drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
534 
535 	i = 0;
536 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
537 		struct drm_display_mode *mode = modes[i];
538 		struct drm_crtc *crtc = crtcs[i];
539 
540 		if (connector->encoder == NULL) {
541 			i++;
542 			continue;
543 		}
544 
545 		if (mode && crtc) {
546 			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
547 				  mode->name, crtc->base.id);
548 			crtc->desired_mode = mode;
549 			connector->encoder->crtc = crtc;
550 		} else {
551 			connector->encoder->crtc = NULL;
552 			connector->encoder = NULL;
553 		}
554 		i++;
555 	}
556 
557 	kfree(crtcs);
558 	kfree(modes);
559 	kfree(enabled);
560 }
561 
562 /**
563  * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
564  * @encoder: encoder to test
565  * @crtc: crtc to test
566  *
567  * Return false if @encoder can't be driven by @crtc, true otherwise.
568  */
569 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
570 				struct drm_crtc *crtc)
571 {
572 	struct drm_device *dev;
573 	struct drm_crtc *tmp;
574 	int crtc_mask = 1;
575 
576 	WARN(!crtc, "checking null crtc?");
577 
578 	dev = crtc->dev;
579 
580 	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
581 		if (tmp == crtc)
582 			break;
583 		crtc_mask <<= 1;
584 	}
585 
586 	if (encoder->possible_crtcs & crtc_mask)
587 		return true;
588 	return false;
589 }
590 
591 /*
592  * Check the CRTC we're going to map each output to vs. its current
593  * CRTC.  If they don't match, we have to disable the output and the CRTC
594  * since the driver will have to re-route things.
595  */
596 static void
597 drm_crtc_prepare_encoders(struct drm_device *dev)
598 {
599 	struct drm_encoder_helper_funcs *encoder_funcs;
600 	struct drm_encoder *encoder;
601 
602 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
603 		encoder_funcs = encoder->helper_private;
604 		/* Disable unused encoders */
605 		if (encoder->crtc == NULL)
606 			(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
607 		/* Disable encoders whose CRTC is about to change */
608 		if (encoder_funcs->get_crtc &&
609 		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
610 			(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
611 	}
612 }
613 
614 /**
615  * drm_crtc_set_mode - set a mode
616  * @crtc: CRTC to program
617  * @mode: mode to use
618  * @x: width of mode
619  * @y: height of mode
620  *
621  * LOCKING:
622  * Caller must hold mode config lock.
623  *
624  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
625  * to fixup or reject the mode prior to trying to set it.
626  *
627  * RETURNS:
628  * True if the mode was set successfully, or false otherwise.
629  */
630 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
631 			      struct drm_display_mode *mode,
632 			      int x, int y,
633 			      struct drm_framebuffer *old_fb)
634 {
635 	struct drm_device *dev = crtc->dev;
636 	struct drm_display_mode *adjusted_mode, saved_mode;
637 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
638 	struct drm_encoder_helper_funcs *encoder_funcs;
639 	int saved_x, saved_y;
640 	struct drm_encoder *encoder;
641 	bool ret = true;
642 
643 	adjusted_mode = drm_mode_duplicate(dev, mode);
644 
645 	crtc->enabled = drm_helper_crtc_in_use(crtc);
646 
647 	if (!crtc->enabled)
648 		return true;
649 
650 	saved_mode = crtc->mode;
651 	saved_x = crtc->x;
652 	saved_y = crtc->y;
653 
654 	/* Update crtc values up front so the driver can rely on them for mode
655 	 * setting.
656 	 */
657 	crtc->mode = *mode;
658 	crtc->x = x;
659 	crtc->y = y;
660 
661 	/* Pass our mode to the connectors and the CRTC to give them a chance to
662 	 * adjust it according to limitations or connector properties, and also
663 	 * a chance to reject the mode entirely.
664 	 */
665 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
666 
667 		if (encoder->crtc != crtc)
668 			continue;
669 		encoder_funcs = encoder->helper_private;
670 		if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
671 						      adjusted_mode))) {
672 			goto done;
673 		}
674 	}
675 
676 	if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
677 		goto done;
678 	}
679 
680 	/* Prepare the encoders and CRTCs before setting the mode. */
681 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
682 
683 		if (encoder->crtc != crtc)
684 			continue;
685 		encoder_funcs = encoder->helper_private;
686 		/* Disable the encoders as the first thing we do. */
687 		encoder_funcs->prepare(encoder);
688 	}
689 
690 	drm_crtc_prepare_encoders(dev);
691 
692 	crtc_funcs->prepare(crtc);
693 
694 	/* Set up the DPLL and any encoders state that needs to adjust or depend
695 	 * on the DPLL.
696 	 */
697 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
698 	if (!ret)
699 	    goto done;
700 
701 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
702 
703 		if (encoder->crtc != crtc)
704 			continue;
705 
706 		DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
707 			 mode->name, mode->base.id);
708 		encoder_funcs = encoder->helper_private;
709 		encoder_funcs->mode_set(encoder, mode, adjusted_mode);
710 	}
711 
712 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
713 	crtc_funcs->commit(crtc);
714 
715 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
716 
717 		if (encoder->crtc != crtc)
718 			continue;
719 
720 		encoder_funcs = encoder->helper_private;
721 		encoder_funcs->commit(encoder);
722 
723 	}
724 
725 	/* XXX free adjustedmode */
726 	drm_mode_destroy(dev, adjusted_mode);
727 	/* FIXME: add subpixel order */
728 done:
729 	if (!ret) {
730 		crtc->mode = saved_mode;
731 		crtc->x = saved_x;
732 		crtc->y = saved_y;
733 	}
734 
735 	return ret;
736 }
737 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
738 
739 
740 /**
741  * drm_crtc_helper_set_config - set a new config from userspace
742  * @crtc: CRTC to setup
743  * @crtc_info: user provided configuration
744  * @new_mode: new mode to set
745  * @connector_set: set of connectors for the new config
746  * @fb: new framebuffer
747  *
748  * LOCKING:
749  * Caller must hold mode config lock.
750  *
751  * Setup a new configuration, provided by the user in @crtc_info, and enable
752  * it.
753  *
754  * RETURNS:
755  * Zero. (FIXME)
756  */
757 int drm_crtc_helper_set_config(struct drm_mode_set *set)
758 {
759 	struct drm_device *dev;
760 	struct drm_crtc *save_crtcs, *new_crtc, *crtc;
761 	struct drm_encoder *save_encoders, *new_encoder, *encoder;
762 	struct drm_framebuffer *old_fb = NULL;
763 	bool mode_changed = false; /* if true do a full mode set */
764 	bool fb_changed = false; /* if true and !mode_changed just do a flip */
765 	struct drm_connector *save_connectors, *connector;
766 	int count = 0, ro, fail = 0;
767 	struct drm_crtc_helper_funcs *crtc_funcs;
768 	int ret = 0;
769 
770 	DRM_DEBUG_KMS("\n");
771 
772 	if (!set)
773 		return -EINVAL;
774 
775 	if (!set->crtc)
776 		return -EINVAL;
777 
778 	if (!set->crtc->helper_private)
779 		return -EINVAL;
780 
781 	crtc_funcs = set->crtc->helper_private;
782 
783 	DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
784 			" %d (x, y) (%i, %i)\n",
785 		  set->crtc, set->crtc->base.id, set->fb, set->connectors,
786 		  (int)set->num_connectors, set->x, set->y);
787 
788 	dev = set->crtc->dev;
789 
790 	/* Allocate space for the backup of all (non-pointer) crtc, encoder and
791 	 * connector data. */
792 	save_crtcs = kzalloc(dev->mode_config.num_crtc *
793 			     sizeof(struct drm_crtc), GFP_KERNEL);
794 	if (!save_crtcs)
795 		return -ENOMEM;
796 
797 	save_encoders = kzalloc(dev->mode_config.num_encoder *
798 				sizeof(struct drm_encoder), GFP_KERNEL);
799 	if (!save_encoders) {
800 		kfree(save_crtcs);
801 		return -ENOMEM;
802 	}
803 
804 	save_connectors = kzalloc(dev->mode_config.num_connector *
805 				sizeof(struct drm_connector), GFP_KERNEL);
806 	if (!save_connectors) {
807 		kfree(save_crtcs);
808 		kfree(save_encoders);
809 		return -ENOMEM;
810 	}
811 
812 	/* Copy data. Note that driver private data is not affected.
813 	 * Should anything bad happen only the expected state is
814 	 * restored, not the drivers personal bookkeeping.
815 	 */
816 	count = 0;
817 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818 		save_crtcs[count++] = *crtc;
819 	}
820 
821 	count = 0;
822 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
823 		save_encoders[count++] = *encoder;
824 	}
825 
826 	count = 0;
827 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
828 		save_connectors[count++] = *connector;
829 	}
830 
831 	/* We should be able to check here if the fb has the same properties
832 	 * and then just flip_or_move it */
833 	if (set->crtc->fb != set->fb) {
834 		/* If we have no fb then treat it as a full mode set */
835 		if (set->crtc->fb == NULL) {
836 			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
837 			mode_changed = true;
838 		} else if (set->fb == NULL) {
839 			mode_changed = true;
840 		} else
841 			fb_changed = true;
842 	}
843 
844 	if (set->x != set->crtc->x || set->y != set->crtc->y)
845 		fb_changed = true;
846 
847 	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
848 		DRM_DEBUG_KMS("modes are different, full mode set\n");
849 		drm_mode_debug_printmodeline(&set->crtc->mode);
850 		drm_mode_debug_printmodeline(set->mode);
851 		mode_changed = true;
852 	}
853 
854 	/* a) traverse passed in connector list and get encoders for them */
855 	count = 0;
856 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
857 		struct drm_connector_helper_funcs *connector_funcs =
858 			connector->helper_private;
859 		new_encoder = connector->encoder;
860 		for (ro = 0; ro < set->num_connectors; ro++) {
861 			if (set->connectors[ro] == connector) {
862 				new_encoder = connector_funcs->best_encoder(connector);
863 				/* if we can't get an encoder for a connector
864 				   we are setting now - then fail */
865 				if (new_encoder == NULL)
866 					/* don't break so fail path works correct */
867 					fail = 1;
868 				break;
869 			}
870 		}
871 
872 		if (new_encoder != connector->encoder) {
873 			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
874 			mode_changed = true;
875 			/* If the encoder is reused for another connector, then
876 			 * the appropriate crtc will be set later.
877 			 */
878 			if (connector->encoder)
879 				connector->encoder->crtc = NULL;
880 			connector->encoder = new_encoder;
881 		}
882 	}
883 
884 	if (fail) {
885 		ret = -EINVAL;
886 		goto fail;
887 	}
888 
889 	count = 0;
890 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
891 		if (!connector->encoder)
892 			continue;
893 
894 		if (connector->encoder->crtc == set->crtc)
895 			new_crtc = NULL;
896 		else
897 			new_crtc = connector->encoder->crtc;
898 
899 		for (ro = 0; ro < set->num_connectors; ro++) {
900 			if (set->connectors[ro] == connector)
901 				new_crtc = set->crtc;
902 		}
903 
904 		/* Make sure the new CRTC will work with the encoder */
905 		if (new_crtc &&
906 		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
907 			ret = -EINVAL;
908 			goto fail;
909 		}
910 		if (new_crtc != connector->encoder->crtc) {
911 			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
912 			mode_changed = true;
913 			connector->encoder->crtc = new_crtc;
914 		}
915 		DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
916 			  connector->base.id, new_crtc);
917 	}
918 
919 	/* mode_set_base is not a required function */
920 	if (fb_changed && !crtc_funcs->mode_set_base)
921 		mode_changed = true;
922 
923 	if (mode_changed) {
924 		old_fb = set->crtc->fb;
925 		set->crtc->fb = set->fb;
926 		set->crtc->enabled = (set->mode != NULL);
927 		if (set->mode != NULL) {
928 			DRM_DEBUG_KMS("attempting to set mode from"
929 					" userspace\n");
930 			drm_mode_debug_printmodeline(set->mode);
931 			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
932 						      set->x, set->y,
933 						      old_fb)) {
934 				DRM_ERROR("failed to set mode on crtc %p\n",
935 					  set->crtc);
936 				ret = -EINVAL;
937 				goto fail;
938 			}
939 			/* TODO are these needed? */
940 			set->crtc->desired_x = set->x;
941 			set->crtc->desired_y = set->y;
942 			set->crtc->desired_mode = set->mode;
943 		}
944 		drm_helper_disable_unused_functions(dev);
945 	} else if (fb_changed) {
946 		set->crtc->x = set->x;
947 		set->crtc->y = set->y;
948 
949 		old_fb = set->crtc->fb;
950 		if (set->crtc->fb != set->fb)
951 			set->crtc->fb = set->fb;
952 		ret = crtc_funcs->mode_set_base(set->crtc,
953 						set->x, set->y, old_fb);
954 		if (ret != 0)
955 			goto fail;
956 	}
957 
958 	kfree(save_connectors);
959 	kfree(save_encoders);
960 	kfree(save_crtcs);
961 	return 0;
962 
963 fail:
964 	/* Restore all previous data. */
965 	count = 0;
966 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
967 		*crtc = save_crtcs[count++];
968 	}
969 
970 	count = 0;
971 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
972 		*encoder = save_encoders[count++];
973 	}
974 
975 	count = 0;
976 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
977 		*connector = save_connectors[count++];
978 	}
979 
980 	kfree(save_connectors);
981 	kfree(save_encoders);
982 	kfree(save_crtcs);
983 	return ret;
984 }
985 EXPORT_SYMBOL(drm_crtc_helper_set_config);
986 
987 bool drm_helper_plugged_event(struct drm_device *dev)
988 {
989 	DRM_DEBUG_KMS("\n");
990 
991 	drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
992 					 dev->mode_config.max_height);
993 
994 	drm_setup_crtcs(dev);
995 
996 	/* alert the driver fb layer */
997 	dev->mode_config.funcs->fb_changed(dev);
998 
999 	/* FIXME: send hotplug event */
1000 	return true;
1001 }
1002 /**
1003  * drm_initial_config - setup a sane initial connector configuration
1004  * @dev: DRM device
1005  *
1006  * LOCKING:
1007  * Called at init time, must take mode config lock.
1008  *
1009  * Scan the CRTCs and connectors and try to put together an initial setup.
1010  * At the moment, this is a cloned configuration across all heads with
1011  * a new framebuffer object as the backing store.
1012  *
1013  * RETURNS:
1014  * Zero if everything went ok, nonzero otherwise.
1015  */
1016 bool drm_helper_initial_config(struct drm_device *dev)
1017 {
1018 	int count = 0;
1019 
1020 	/* disable all the possible outputs/crtcs before entering KMS mode */
1021 	drm_helper_disable_unused_functions(dev);
1022 
1023 	drm_fb_helper_parse_command_line(dev);
1024 
1025 	count = drm_helper_probe_connector_modes(dev,
1026 						 dev->mode_config.max_width,
1027 						 dev->mode_config.max_height);
1028 
1029 	/*
1030 	 * we shouldn't end up with no modes here.
1031 	 */
1032 	if (count == 0)
1033 		printk(KERN_INFO "No connectors reported connected with modes\n");
1034 
1035 	drm_setup_crtcs(dev);
1036 
1037 	/* alert the driver fb layer */
1038 	dev->mode_config.funcs->fb_changed(dev);
1039 
1040 	return 0;
1041 }
1042 EXPORT_SYMBOL(drm_helper_initial_config);
1043 
1044 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1045 {
1046 	int dpms = DRM_MODE_DPMS_OFF;
1047 	struct drm_connector *connector;
1048 	struct drm_device *dev = encoder->dev;
1049 
1050 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1051 		if (connector->encoder == encoder)
1052 			if (connector->dpms < dpms)
1053 				dpms = connector->dpms;
1054 	return dpms;
1055 }
1056 
1057 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1058 {
1059 	int dpms = DRM_MODE_DPMS_OFF;
1060 	struct drm_connector *connector;
1061 	struct drm_device *dev = crtc->dev;
1062 
1063 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1064 		if (connector->encoder && connector->encoder->crtc == crtc)
1065 			if (connector->dpms < dpms)
1066 				dpms = connector->dpms;
1067 	return dpms;
1068 }
1069 
1070 /**
1071  * drm_helper_connector_dpms
1072  * @connector affected connector
1073  * @mode DPMS mode
1074  *
1075  * Calls the low-level connector DPMS function, then
1076  * calls appropriate encoder and crtc DPMS functions as well
1077  */
1078 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1079 {
1080 	struct drm_encoder *encoder = connector->encoder;
1081 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1082 	int old_dpms;
1083 
1084 	if (mode == connector->dpms)
1085 		return;
1086 
1087 	old_dpms = connector->dpms;
1088 	connector->dpms = mode;
1089 
1090 	/* from off to on, do crtc then encoder */
1091 	if (mode < old_dpms) {
1092 		if (crtc) {
1093 			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1094 			if (crtc_funcs->dpms)
1095 				(*crtc_funcs->dpms) (crtc,
1096 						     drm_helper_choose_crtc_dpms(crtc));
1097 		}
1098 		if (encoder) {
1099 			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1100 			if (encoder_funcs->dpms)
1101 				(*encoder_funcs->dpms) (encoder,
1102 							drm_helper_choose_encoder_dpms(encoder));
1103 		}
1104 	}
1105 
1106 	/* from on to off, do encoder then crtc */
1107 	if (mode > old_dpms) {
1108 		if (encoder) {
1109 			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1110 			if (encoder_funcs->dpms)
1111 				(*encoder_funcs->dpms) (encoder,
1112 							drm_helper_choose_encoder_dpms(encoder));
1113 		}
1114 		if (crtc) {
1115 			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1116 			if (crtc_funcs->dpms)
1117 				(*crtc_funcs->dpms) (crtc,
1118 						     drm_helper_choose_crtc_dpms(crtc));
1119 		}
1120 	}
1121 
1122 	return;
1123 }
1124 EXPORT_SYMBOL(drm_helper_connector_dpms);
1125 
1126 /**
1127  * drm_hotplug_stage_two
1128  * @dev DRM device
1129  * @connector hotpluged connector
1130  *
1131  * LOCKING.
1132  * Caller must hold mode config lock, function might grab struct lock.
1133  *
1134  * Stage two of a hotplug.
1135  *
1136  * RETURNS:
1137  * Zero on success, errno on failure.
1138  */
1139 int drm_helper_hotplug_stage_two(struct drm_device *dev)
1140 {
1141 	drm_helper_plugged_event(dev);
1142 
1143 	return 0;
1144 }
1145 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1146 
1147 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1148 				   struct drm_mode_fb_cmd *mode_cmd)
1149 {
1150 	fb->width = mode_cmd->width;
1151 	fb->height = mode_cmd->height;
1152 	fb->pitch = mode_cmd->pitch;
1153 	fb->bits_per_pixel = mode_cmd->bpp;
1154 	fb->depth = mode_cmd->depth;
1155 
1156 	return 0;
1157 }
1158 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1159 
1160 int drm_helper_resume_force_mode(struct drm_device *dev)
1161 {
1162 	struct drm_crtc *crtc;
1163 	struct drm_encoder *encoder;
1164 	struct drm_encoder_helper_funcs *encoder_funcs;
1165 	struct drm_crtc_helper_funcs *crtc_funcs;
1166 	int ret;
1167 
1168 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1169 
1170 		if (!crtc->enabled)
1171 			continue;
1172 
1173 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1174 					       crtc->x, crtc->y, crtc->fb);
1175 
1176 		if (ret == false)
1177 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1178 
1179 		/* Turn off outputs that were already powered off */
1180 		if (drm_helper_choose_crtc_dpms(crtc)) {
1181 			list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1182 
1183 				if(encoder->crtc != crtc)
1184 					continue;
1185 
1186 				encoder_funcs = encoder->helper_private;
1187 				if (encoder_funcs->dpms)
1188 					(*encoder_funcs->dpms) (encoder,
1189 								drm_helper_choose_encoder_dpms(encoder));
1190 
1191 				crtc_funcs = crtc->helper_private;
1192 				if (crtc_funcs->dpms)
1193 					(*crtc_funcs->dpms) (crtc,
1194 							     drm_helper_choose_crtc_dpms(crtc));
1195 			}
1196 		}
1197 	}
1198 	/* disable the unused connectors while restoring the modesetting */
1199 	drm_helper_disable_unused_functions(dev);
1200 	return 0;
1201 }
1202 EXPORT_SYMBOL(drm_helper_resume_force_mode);
1203