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