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