1 /* $NetBSD: drm_crtc_helper.c,v 1.7 2021/12/18 23:44:57 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2006-2008 Intel Corporation
5 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 *
7 * DRM core CRTC related functions
8 *
9 * Permission to use, copy, modify, distribute, and sell this software and its
10 * documentation for any purpose is hereby granted without fee, provided that
11 * the above copyright notice appear in all copies and that both that copyright
12 * notice and this permission notice appear in supporting documentation, and
13 * that the name of the copyright holders not be used in advertising or
14 * publicity pertaining to distribution of the software without specific,
15 * written prior permission. The copyright holders make no representations
16 * about the suitability of this software for any purpose. It is provided "as
17 * is" without express or implied warranty.
18 *
19 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
21 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
23 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
24 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * OF THIS SOFTWARE.
26 *
27 * Authors:
28 * Keith Packard
29 * Eric Anholt <eric@anholt.net>
30 * Dave Airlie <airlied@linux.ie>
31 * Jesse Barnes <jesse.barnes@intel.com>
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: drm_crtc_helper.c,v 1.7 2021/12/18 23:44:57 riastradh Exp $");
36
37 #include <linux/export.h>
38 #include <linux/kernel.h>
39 #include <linux/moduleparam.h>
40
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
43 #include <drm/drm_atomic_uapi.h>
44 #include <drm/drm_bridge.h>
45 #include <drm/drm_crtc.h>
46 #include <drm/drm_crtc_helper.h>
47 #include <drm/drm_drv.h>
48 #include <drm/drm_edid.h>
49 #include <drm/drm_encoder.h>
50 #ifndef __NetBSD__
51 #include <drm/drm_fb_helper.h>
52 #endif
53 #include <drm/drm_fourcc.h>
54 #include <drm/drm_plane_helper.h>
55 #include <drm/drm_print.h>
56 #include <drm/drm_vblank.h>
57
58 #include "drm_crtc_helper_internal.h"
59
60 /**
61 * DOC: overview
62 *
63 * The CRTC modeset helper library provides a default set_config implementation
64 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
65 * the same callbacks which drivers can use to e.g. restore the modeset
66 * configuration on resume with drm_helper_resume_force_mode().
67 *
68 * Note that this helper library doesn't track the current power state of CRTCs
69 * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
70 * though the hardware is already in the desired state. This deficiency has been
71 * fixed in the atomic helpers.
72 *
73 * The driver callbacks are mostly compatible with the atomic modeset helpers,
74 * except for the handling of the primary plane: Atomic helpers require that the
75 * primary plane is implemented as a real standalone plane and not directly tied
76 * to the CRTC state. For easier transition this library provides functions to
77 * implement the old semantics required by the CRTC helpers using the new plane
78 * and atomic helper callbacks.
79 *
80 * Drivers are strongly urged to convert to the atomic helpers (by way of first
81 * converting to the plane helpers). New drivers must not use these functions
82 * but need to implement the atomic interface instead, potentially using the
83 * atomic helpers for that.
84 *
85 * These legacy modeset helpers use the same function table structures as
86 * all other modesetting helpers. See the documentation for struct
87 * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
88 * &drm_connector_helper_funcs.
89 */
90
91 /**
92 * drm_helper_encoder_in_use - check if a given encoder is in use
93 * @encoder: encoder to check
94 *
95 * Checks whether @encoder is with the current mode setting output configuration
96 * in use by any connector. This doesn't mean that it is actually enabled since
97 * the DPMS state is tracked separately.
98 *
99 * Returns:
100 * True if @encoder is used, false otherwise.
101 */
drm_helper_encoder_in_use(struct drm_encoder * encoder)102 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
103 {
104 struct drm_connector *connector;
105 struct drm_connector_list_iter conn_iter;
106 struct drm_device *dev = encoder->dev;
107
108 WARN_ON(drm_drv_uses_atomic_modeset(dev));
109
110 /*
111 * We can expect this mutex to be locked if we are not panicking.
112 * Locking is currently fubar in the panic handler.
113 */
114 if (!oops_in_progress) {
115 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
116 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
117 }
118
119
120 drm_connector_list_iter_begin(dev, &conn_iter);
121 drm_for_each_connector_iter(connector, &conn_iter) {
122 if (connector->encoder == encoder) {
123 drm_connector_list_iter_end(&conn_iter);
124 return true;
125 }
126 }
127 drm_connector_list_iter_end(&conn_iter);
128 return false;
129 }
130 EXPORT_SYMBOL(drm_helper_encoder_in_use);
131
132 /**
133 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
134 * @crtc: CRTC to check
135 *
136 * Checks whether @crtc is with the current mode setting output configuration
137 * in use by any connector. This doesn't mean that it is actually enabled since
138 * the DPMS state is tracked separately.
139 *
140 * Returns:
141 * True if @crtc is used, false otherwise.
142 */
drm_helper_crtc_in_use(struct drm_crtc * crtc)143 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
144 {
145 struct drm_encoder *encoder;
146 struct drm_device *dev = crtc->dev;
147
148 WARN_ON(drm_drv_uses_atomic_modeset(dev));
149
150 /*
151 * We can expect this mutex to be locked if we are not panicking.
152 * Locking is currently fubar in the panic handler.
153 */
154 if (!oops_in_progress)
155 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
156
157 drm_for_each_encoder(encoder, dev)
158 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
159 return true;
160 return false;
161 }
162 EXPORT_SYMBOL(drm_helper_crtc_in_use);
163
164 static void
drm_encoder_disable(struct drm_encoder * encoder)165 drm_encoder_disable(struct drm_encoder *encoder)
166 {
167 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
168
169 if (!encoder_funcs)
170 return;
171
172 if (encoder_funcs->disable)
173 (*encoder_funcs->disable)(encoder);
174 else if (encoder_funcs->dpms)
175 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
176 }
177
__drm_helper_disable_unused_functions(struct drm_device * dev)178 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
179 {
180 struct drm_encoder *encoder;
181 struct drm_crtc *crtc;
182
183 drm_warn_on_modeset_not_all_locked(dev);
184
185 drm_for_each_encoder(encoder, dev) {
186 if (!drm_helper_encoder_in_use(encoder)) {
187 drm_encoder_disable(encoder);
188 /* disconnect encoder from any connector */
189 encoder->crtc = NULL;
190 }
191 }
192
193 drm_for_each_crtc(crtc, dev) {
194 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
195 crtc->enabled = drm_helper_crtc_in_use(crtc);
196 if (!crtc->enabled) {
197 if (crtc_funcs->disable)
198 (*crtc_funcs->disable)(crtc);
199 else
200 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
201 crtc->primary->fb = NULL;
202 }
203 }
204 }
205
206 /**
207 * drm_helper_disable_unused_functions - disable unused objects
208 * @dev: DRM device
209 *
210 * This function walks through the entire mode setting configuration of @dev. It
211 * will remove any CRTC links of unused encoders and encoder links of
212 * disconnected connectors. Then it will disable all unused encoders and CRTCs
213 * either by calling their disable callback if available or by calling their
214 * dpms callback with DRM_MODE_DPMS_OFF.
215 *
216 * NOTE:
217 *
218 * This function is part of the legacy modeset helper library and will cause
219 * major confusion with atomic drivers. This is because atomic helpers guarantee
220 * to never call ->disable() hooks on a disabled function, or ->enable() hooks
221 * on an enabled functions. drm_helper_disable_unused_functions() on the other
222 * hand throws such guarantees into the wind and calls disable hooks
223 * unconditionally on unused functions.
224 */
drm_helper_disable_unused_functions(struct drm_device * dev)225 void drm_helper_disable_unused_functions(struct drm_device *dev)
226 {
227 WARN_ON(drm_drv_uses_atomic_modeset(dev));
228
229 drm_modeset_lock_all(dev);
230 __drm_helper_disable_unused_functions(dev);
231 drm_modeset_unlock_all(dev);
232 }
233 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
234
235 /*
236 * Check the CRTC we're going to map each output to vs. its current
237 * CRTC. If they don't match, we have to disable the output and the CRTC
238 * since the driver will have to re-route things.
239 */
240 static void
drm_crtc_prepare_encoders(struct drm_device * dev)241 drm_crtc_prepare_encoders(struct drm_device *dev)
242 {
243 const struct drm_encoder_helper_funcs *encoder_funcs;
244 struct drm_encoder *encoder;
245
246 drm_for_each_encoder(encoder, dev) {
247 encoder_funcs = encoder->helper_private;
248 if (!encoder_funcs)
249 continue;
250
251 /* Disable unused encoders */
252 if (encoder->crtc == NULL)
253 drm_encoder_disable(encoder);
254 /* Disable encoders whose CRTC is about to change */
255 if (encoder_funcs->get_crtc &&
256 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
257 drm_encoder_disable(encoder);
258 }
259 }
260
261 /**
262 * drm_crtc_helper_set_mode - internal helper to set a mode
263 * @crtc: CRTC to program
264 * @mode: mode to use
265 * @x: horizontal offset into the surface
266 * @y: vertical offset into the surface
267 * @old_fb: old framebuffer, for cleanup
268 *
269 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
270 * to fixup or reject the mode prior to trying to set it. This is an internal
271 * helper that drivers could e.g. use to update properties that require the
272 * entire output pipe to be disabled and re-enabled in a new configuration. For
273 * example for changing whether audio is enabled on a hdmi link or for changing
274 * panel fitter or dither attributes. It is also called by the
275 * drm_crtc_helper_set_config() helper function to drive the mode setting
276 * sequence.
277 *
278 * Returns:
279 * True if the mode was set successfully, false otherwise.
280 */
drm_crtc_helper_set_mode(struct drm_crtc * crtc,struct drm_display_mode * mode,int x,int y,struct drm_framebuffer * old_fb)281 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
282 struct drm_display_mode *mode,
283 int x, int y,
284 struct drm_framebuffer *old_fb)
285 {
286 struct drm_device *dev = crtc->dev;
287 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
288 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
289 const struct drm_encoder_helper_funcs *encoder_funcs;
290 int saved_x, saved_y;
291 bool saved_enabled;
292 struct drm_encoder *encoder;
293 bool ret = true;
294
295 WARN_ON(drm_drv_uses_atomic_modeset(dev));
296
297 drm_warn_on_modeset_not_all_locked(dev);
298
299 saved_enabled = crtc->enabled;
300 crtc->enabled = drm_helper_crtc_in_use(crtc);
301 if (!crtc->enabled)
302 return true;
303
304 adjusted_mode = drm_mode_duplicate(dev, mode);
305 if (!adjusted_mode) {
306 crtc->enabled = saved_enabled;
307 return false;
308 }
309
310 saved_mode = crtc->mode;
311 saved_hwmode = crtc->hwmode;
312 saved_x = crtc->x;
313 saved_y = crtc->y;
314
315 /* Update crtc values up front so the driver can rely on them for mode
316 * setting.
317 */
318 crtc->mode = *mode;
319 crtc->x = x;
320 crtc->y = y;
321
322 /* Pass our mode to the connectors and the CRTC to give them a chance to
323 * adjust it according to limitations or connector properties, and also
324 * a chance to reject the mode entirely.
325 */
326 drm_for_each_encoder(encoder, dev) {
327
328 if (encoder->crtc != crtc)
329 continue;
330
331 encoder_funcs = encoder->helper_private;
332 if (!encoder_funcs)
333 continue;
334
335 encoder_funcs = encoder->helper_private;
336 if (encoder_funcs->mode_fixup) {
337 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
338 adjusted_mode))) {
339 DRM_DEBUG_KMS("Encoder fixup failed\n");
340 goto done;
341 }
342 }
343 }
344
345 if (crtc_funcs->mode_fixup) {
346 if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
347 adjusted_mode))) {
348 DRM_DEBUG_KMS("CRTC fixup failed\n");
349 goto done;
350 }
351 }
352 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
353
354 crtc->hwmode = *adjusted_mode;
355
356 /* Prepare the encoders and CRTCs before setting the mode. */
357 drm_for_each_encoder(encoder, dev) {
358
359 if (encoder->crtc != crtc)
360 continue;
361
362 encoder_funcs = encoder->helper_private;
363 if (!encoder_funcs)
364 continue;
365
366 /* Disable the encoders as the first thing we do. */
367 if (encoder_funcs->prepare)
368 encoder_funcs->prepare(encoder);
369 }
370
371 drm_crtc_prepare_encoders(dev);
372
373 crtc_funcs->prepare(crtc);
374
375 /* Set up the DPLL and any encoders state that needs to adjust or depend
376 * on the DPLL.
377 */
378 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
379 if (!ret)
380 goto done;
381
382 drm_for_each_encoder(encoder, dev) {
383
384 if (encoder->crtc != crtc)
385 continue;
386
387 encoder_funcs = encoder->helper_private;
388 if (!encoder_funcs)
389 continue;
390
391 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
392 encoder->base.id, encoder->name, mode->name);
393 if (encoder_funcs->mode_set)
394 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
395 }
396
397 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
398 crtc_funcs->commit(crtc);
399
400 drm_for_each_encoder(encoder, dev) {
401
402 if (encoder->crtc != crtc)
403 continue;
404
405 encoder_funcs = encoder->helper_private;
406 if (!encoder_funcs)
407 continue;
408
409 if (encoder_funcs->commit)
410 encoder_funcs->commit(encoder);
411 }
412
413 /* Calculate and store various constants which
414 * are later needed by vblank and swap-completion
415 * timestamping. They are derived from true hwmode.
416 */
417 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
418
419 /* FIXME: add subpixel order */
420 done:
421 drm_mode_destroy(dev, adjusted_mode);
422 if (!ret) {
423 crtc->enabled = saved_enabled;
424 crtc->mode = saved_mode;
425 crtc->hwmode = saved_hwmode;
426 crtc->x = saved_x;
427 crtc->y = saved_y;
428 }
429
430 return ret;
431 }
432 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
433
434 static void
drm_crtc_helper_disable(struct drm_crtc * crtc)435 drm_crtc_helper_disable(struct drm_crtc *crtc)
436 {
437 struct drm_device *dev = crtc->dev;
438 struct drm_connector *connector;
439 struct drm_encoder *encoder;
440
441 /* Decouple all encoders and their attached connectors from this crtc */
442 drm_for_each_encoder(encoder, dev) {
443 struct drm_connector_list_iter conn_iter;
444
445 if (encoder->crtc != crtc)
446 continue;
447
448 drm_connector_list_iter_begin(dev, &conn_iter);
449 drm_for_each_connector_iter(connector, &conn_iter) {
450 if (connector->encoder != encoder)
451 continue;
452
453 connector->encoder = NULL;
454
455 /*
456 * drm_helper_disable_unused_functions() ought to be
457 * doing this, but since we've decoupled the encoder
458 * from the connector above, the required connection
459 * between them is henceforth no longer available.
460 */
461 connector->dpms = DRM_MODE_DPMS_OFF;
462
463 /* we keep a reference while the encoder is bound */
464 drm_connector_put(connector);
465 }
466 drm_connector_list_iter_end(&conn_iter);
467 }
468
469 __drm_helper_disable_unused_functions(dev);
470 }
471
472 /*
473 * For connectors that support multiple encoders, either the
474 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
475 */
476 struct drm_encoder *
drm_connector_get_single_encoder(struct drm_connector * connector)477 drm_connector_get_single_encoder(struct drm_connector *connector)
478 {
479 struct drm_encoder *encoder;
480
481 WARN_ON(hweight32(connector->possible_encoders) > 1);
482 drm_connector_for_each_possible_encoder(connector, encoder)
483 return encoder;
484
485 return NULL;
486 }
487
488 /**
489 * drm_crtc_helper_set_config - set a new config from userspace
490 * @set: mode set configuration
491 * @ctx: lock acquire context, not used here
492 *
493 * The drm_crtc_helper_set_config() helper function implements the of
494 * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
495 * helpers.
496 *
497 * It first tries to locate the best encoder for each connector by calling the
498 * connector @drm_connector_helper_funcs.best_encoder helper operation.
499 *
500 * After locating the appropriate encoders, the helper function will call the
501 * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
502 * or reject it completely in which case an error will be returned to the
503 * application. If the new configuration after mode adjustment is identical to
504 * the current configuration the helper function will return without performing
505 * any other operation.
506 *
507 * If the adjusted mode is identical to the current mode but changes to the
508 * frame buffer need to be applied, the drm_crtc_helper_set_config() function
509 * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
510 *
511 * If the adjusted mode differs from the current mode, or if the
512 * ->mode_set_base() helper operation is not provided, the helper function
513 * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
514 * and ->commit() CRTC and encoder helper operations, in that order.
515 * Alternatively it can also use the dpms and disable helper operations. For
516 * details see &struct drm_crtc_helper_funcs and struct
517 * &drm_encoder_helper_funcs.
518 *
519 * This function is deprecated. New drivers must implement atomic modeset
520 * support, for which this function is unsuitable. Instead drivers should use
521 * drm_atomic_helper_set_config().
522 *
523 * Returns:
524 * Returns 0 on success, negative errno numbers on failure.
525 */
drm_crtc_helper_set_config(struct drm_mode_set * set,struct drm_modeset_acquire_ctx * ctx)526 int drm_crtc_helper_set_config(struct drm_mode_set *set,
527 struct drm_modeset_acquire_ctx *ctx)
528 {
529 struct drm_device *dev;
530 struct drm_crtc **save_encoder_crtcs, *new_crtc;
531 struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
532 bool mode_changed = false; /* if true do a full mode set */
533 bool fb_changed = false; /* if true and !mode_changed just do a flip */
534 struct drm_connector *connector;
535 struct drm_connector_list_iter conn_iter;
536 int count = 0, ro, fail = 0;
537 const struct drm_crtc_helper_funcs *crtc_funcs;
538 struct drm_mode_set save_set;
539 int ret;
540 int i;
541
542 DRM_DEBUG_KMS("\n");
543
544 BUG_ON(!set);
545 BUG_ON(!set->crtc);
546 BUG_ON(!set->crtc->helper_private);
547
548 /* Enforce sane interface api - has been abused by the fb helper. */
549 BUG_ON(!set->mode && set->fb);
550 BUG_ON(set->fb && set->num_connectors == 0);
551
552 crtc_funcs = set->crtc->helper_private;
553
554 dev = set->crtc->dev;
555 WARN_ON(drm_drv_uses_atomic_modeset(dev));
556
557 if (!set->mode)
558 set->fb = NULL;
559
560 if (set->fb) {
561 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
562 set->crtc->base.id, set->crtc->name,
563 set->fb->base.id,
564 (int)set->num_connectors, set->x, set->y);
565 } else {
566 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
567 set->crtc->base.id, set->crtc->name);
568 drm_crtc_helper_disable(set->crtc);
569 return 0;
570 }
571
572 drm_warn_on_modeset_not_all_locked(dev);
573
574 /*
575 * Allocate space for the backup of all (non-pointer) encoder and
576 * connector data.
577 */
578 save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
579 sizeof(struct drm_crtc *), GFP_KERNEL);
580 if (!save_encoder_crtcs)
581 return -ENOMEM;
582
583 save_connector_encoders = kcalloc(dev->mode_config.num_connector,
584 sizeof(struct drm_encoder *), GFP_KERNEL);
585 if (!save_connector_encoders) {
586 kfree(save_encoder_crtcs);
587 return -ENOMEM;
588 }
589
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 drm_for_each_encoder(encoder, dev) {
597 save_encoder_crtcs[count++] = encoder->crtc;
598 }
599
600 count = 0;
601 drm_connector_list_iter_begin(dev, &conn_iter);
602 drm_for_each_connector_iter(connector, &conn_iter)
603 save_connector_encoders[count++] = connector->encoder;
604 drm_connector_list_iter_end(&conn_iter);
605
606 save_set.crtc = set->crtc;
607 save_set.mode = &set->crtc->mode;
608 save_set.x = set->crtc->x;
609 save_set.y = set->crtc->y;
610 save_set.fb = set->crtc->primary->fb;
611
612 /* We should be able to check here if the fb has the same properties
613 * and then just flip_or_move it */
614 if (set->crtc->primary->fb != set->fb) {
615 /* If we have no fb then treat it as a full mode set */
616 if (set->crtc->primary->fb == NULL) {
617 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
618 mode_changed = true;
619 } else if (set->fb->format != set->crtc->primary->fb->format) {
620 mode_changed = true;
621 } else
622 fb_changed = true;
623 }
624
625 if (set->x != set->crtc->x || set->y != set->crtc->y)
626 fb_changed = true;
627
628 if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
629 DRM_DEBUG_KMS("modes are different, full mode set\n");
630 drm_mode_debug_printmodeline(&set->crtc->mode);
631 drm_mode_debug_printmodeline(set->mode);
632 mode_changed = true;
633 }
634
635 /* take a reference on all unbound connectors in set, reuse the
636 * already taken reference for bound connectors
637 */
638 for (ro = 0; ro < set->num_connectors; ro++) {
639 if (set->connectors[ro]->encoder)
640 continue;
641 drm_connector_get(set->connectors[ro]);
642 }
643
644 /* a) traverse passed in connector list and get encoders for them */
645 count = 0;
646 drm_connector_list_iter_begin(dev, &conn_iter);
647 drm_for_each_connector_iter(connector, &conn_iter) {
648 const struct drm_connector_helper_funcs *connector_funcs =
649 connector->helper_private;
650 new_encoder = connector->encoder;
651 for (ro = 0; ro < set->num_connectors; ro++) {
652 if (set->connectors[ro] == connector) {
653 if (connector_funcs->best_encoder)
654 new_encoder = connector_funcs->best_encoder(connector);
655 else
656 new_encoder = drm_connector_get_single_encoder(connector);
657
658 /* if we can't get an encoder for a connector
659 we are setting now - then fail */
660 if (new_encoder == NULL)
661 /* don't break so fail path works correct */
662 fail = 1;
663
664 if (connector->dpms != DRM_MODE_DPMS_ON) {
665 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
666 mode_changed = true;
667 }
668
669 break;
670 }
671 }
672
673 if (new_encoder != connector->encoder) {
674 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
675 mode_changed = true;
676 /* If the encoder is reused for another connector, then
677 * the appropriate crtc will be set later.
678 */
679 if (connector->encoder)
680 connector->encoder->crtc = NULL;
681 connector->encoder = new_encoder;
682 }
683 }
684 drm_connector_list_iter_end(&conn_iter);
685
686 if (fail) {
687 ret = -EINVAL;
688 goto fail;
689 }
690
691 count = 0;
692 drm_connector_list_iter_begin(dev, &conn_iter);
693 drm_for_each_connector_iter(connector, &conn_iter) {
694 if (!connector->encoder)
695 continue;
696
697 if (connector->encoder->crtc == set->crtc)
698 new_crtc = NULL;
699 else
700 new_crtc = connector->encoder->crtc;
701
702 for (ro = 0; ro < set->num_connectors; ro++) {
703 if (set->connectors[ro] == connector)
704 new_crtc = set->crtc;
705 }
706
707 /* Make sure the new CRTC will work with the encoder */
708 if (new_crtc &&
709 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
710 ret = -EINVAL;
711 drm_connector_list_iter_end(&conn_iter);
712 goto fail;
713 }
714 if (new_crtc != connector->encoder->crtc) {
715 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
716 mode_changed = true;
717 connector->encoder->crtc = new_crtc;
718 }
719 if (new_crtc) {
720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
721 connector->base.id, connector->name,
722 new_crtc->base.id, new_crtc->name);
723 } else {
724 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
725 connector->base.id, connector->name);
726 }
727 }
728 drm_connector_list_iter_end(&conn_iter);
729
730 /* mode_set_base is not a required function */
731 if (fb_changed && !crtc_funcs->mode_set_base)
732 mode_changed = true;
733
734 if (mode_changed) {
735 if (drm_helper_crtc_in_use(set->crtc)) {
736 DRM_DEBUG_KMS("attempting to set mode from"
737 " userspace\n");
738 drm_mode_debug_printmodeline(set->mode);
739 set->crtc->primary->fb = set->fb;
740 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
741 set->x, set->y,
742 save_set.fb)) {
743 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
744 set->crtc->base.id, set->crtc->name);
745 set->crtc->primary->fb = save_set.fb;
746 ret = -EINVAL;
747 goto fail;
748 }
749 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
750 for (i = 0; i < set->num_connectors; i++) {
751 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
752 set->connectors[i]->name);
753 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
754 }
755 }
756 __drm_helper_disable_unused_functions(dev);
757 } else if (fb_changed) {
758 set->crtc->x = set->x;
759 set->crtc->y = set->y;
760 set->crtc->primary->fb = set->fb;
761 ret = crtc_funcs->mode_set_base(set->crtc,
762 set->x, set->y, save_set.fb);
763 if (ret != 0) {
764 set->crtc->x = save_set.x;
765 set->crtc->y = save_set.y;
766 set->crtc->primary->fb = save_set.fb;
767 goto fail;
768 }
769 }
770
771 kfree(save_connector_encoders);
772 kfree(save_encoder_crtcs);
773 return 0;
774
775 fail:
776 /* Restore all previous data. */
777 count = 0;
778 drm_for_each_encoder(encoder, dev) {
779 encoder->crtc = save_encoder_crtcs[count++];
780 }
781
782 count = 0;
783 drm_connector_list_iter_begin(dev, &conn_iter);
784 drm_for_each_connector_iter(connector, &conn_iter)
785 connector->encoder = save_connector_encoders[count++];
786 drm_connector_list_iter_end(&conn_iter);
787
788 /* after fail drop reference on all unbound connectors in set, let
789 * bound connectors keep their reference
790 */
791 for (ro = 0; ro < set->num_connectors; ro++) {
792 if (set->connectors[ro]->encoder)
793 continue;
794 drm_connector_put(set->connectors[ro]);
795 }
796
797 /* Try to restore the config */
798 if (mode_changed &&
799 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
800 save_set.y, save_set.fb))
801 DRM_ERROR("failed to restore config after modeset failure\n");
802
803 kfree(save_connector_encoders);
804 kfree(save_encoder_crtcs);
805 return ret;
806 }
807 EXPORT_SYMBOL(drm_crtc_helper_set_config);
808
drm_helper_choose_encoder_dpms(struct drm_encoder * encoder)809 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
810 {
811 int dpms = DRM_MODE_DPMS_OFF;
812 struct drm_connector *connector;
813 struct drm_connector_list_iter conn_iter;
814 struct drm_device *dev = encoder->dev;
815
816 drm_connector_list_iter_begin(dev, &conn_iter);
817 drm_for_each_connector_iter(connector, &conn_iter)
818 if (connector->encoder == encoder)
819 if (connector->dpms < dpms)
820 dpms = connector->dpms;
821 drm_connector_list_iter_end(&conn_iter);
822
823 return dpms;
824 }
825
826 /* Helper which handles bridge ordering around encoder dpms */
drm_helper_encoder_dpms(struct drm_encoder * encoder,int mode)827 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
828 {
829 const struct drm_encoder_helper_funcs *encoder_funcs;
830
831 encoder_funcs = encoder->helper_private;
832 if (!encoder_funcs)
833 return;
834
835 if (encoder_funcs->dpms)
836 encoder_funcs->dpms(encoder, mode);
837 }
838
drm_helper_choose_crtc_dpms(struct drm_crtc * crtc)839 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
840 {
841 int dpms = DRM_MODE_DPMS_OFF;
842 struct drm_connector *connector;
843 struct drm_connector_list_iter conn_iter;
844 struct drm_device *dev = crtc->dev;
845
846 drm_connector_list_iter_begin(dev, &conn_iter);
847 drm_for_each_connector_iter(connector, &conn_iter)
848 if (connector->encoder && connector->encoder->crtc == crtc)
849 if (connector->dpms < dpms)
850 dpms = connector->dpms;
851 drm_connector_list_iter_end(&conn_iter);
852
853 return dpms;
854 }
855
856 /**
857 * drm_helper_connector_dpms() - connector dpms helper implementation
858 * @connector: affected connector
859 * @mode: DPMS mode
860 *
861 * The drm_helper_connector_dpms() helper function implements the
862 * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
863 * helpers.
864 *
865 * This is the main helper function provided by the CRTC helper framework for
866 * implementing the DPMS connector attribute. It computes the new desired DPMS
867 * state for all encoders and CRTCs in the output mesh and calls the
868 * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
869 * provided by the driver.
870 *
871 * This function is deprecated. New drivers must implement atomic modeset
872 * support, where DPMS is handled in the DRM core.
873 *
874 * Returns:
875 * Always returns 0.
876 */
drm_helper_connector_dpms(struct drm_connector * connector,int mode)877 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
878 {
879 struct drm_encoder *encoder = connector->encoder;
880 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
881 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
882
883 WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
884
885 if (mode == connector->dpms)
886 return 0;
887
888 old_dpms = connector->dpms;
889 connector->dpms = mode;
890
891 if (encoder)
892 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
893
894 /* from off to on, do crtc then encoder */
895 if (mode < old_dpms) {
896 if (crtc) {
897 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
898 if (crtc_funcs->dpms)
899 (*crtc_funcs->dpms) (crtc,
900 drm_helper_choose_crtc_dpms(crtc));
901 }
902 if (encoder)
903 drm_helper_encoder_dpms(encoder, encoder_dpms);
904 }
905
906 /* from on to off, do encoder then crtc */
907 if (mode > old_dpms) {
908 if (encoder)
909 drm_helper_encoder_dpms(encoder, encoder_dpms);
910 if (crtc) {
911 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
912 if (crtc_funcs->dpms)
913 (*crtc_funcs->dpms) (crtc,
914 drm_helper_choose_crtc_dpms(crtc));
915 }
916 }
917
918 return 0;
919 }
920 EXPORT_SYMBOL(drm_helper_connector_dpms);
921
922 /**
923 * drm_helper_resume_force_mode - force-restore mode setting configuration
924 * @dev: drm_device which should be restored
925 *
926 * Drivers which use the mode setting helpers can use this function to
927 * force-restore the mode setting configuration e.g. on resume or when something
928 * else might have trampled over the hw state (like some overzealous old BIOSen
929 * tended to do).
930 *
931 * This helper doesn't provide a error return value since restoring the old
932 * config should never fail due to resource allocation issues since the driver
933 * has successfully set the restored configuration already. Hence this should
934 * boil down to the equivalent of a few dpms on calls, which also don't provide
935 * an error code.
936 *
937 * Drivers where simply restoring an old configuration again might fail (e.g.
938 * due to slight differences in allocating shared resources when the
939 * configuration is restored in a different order than when userspace set it up)
940 * need to use their own restore logic.
941 *
942 * This function is deprecated. New drivers should implement atomic mode-
943 * setting and use the atomic suspend/resume helpers.
944 *
945 * See also:
946 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
947 */
drm_helper_resume_force_mode(struct drm_device * dev)948 void drm_helper_resume_force_mode(struct drm_device *dev)
949 {
950 struct drm_crtc *crtc;
951 struct drm_encoder *encoder;
952 const struct drm_crtc_helper_funcs *crtc_funcs;
953 int encoder_dpms;
954 bool ret;
955
956 WARN_ON(drm_drv_uses_atomic_modeset(dev));
957
958 drm_modeset_lock_all(dev);
959 drm_for_each_crtc(crtc, dev) {
960
961 if (!crtc->enabled)
962 continue;
963
964 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
965 crtc->x, crtc->y, crtc->primary->fb);
966
967 /* Restoring the old config should never fail! */
968 if (ret == false)
969 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
970
971 /* Turn off outputs that were already powered off */
972 if (drm_helper_choose_crtc_dpms(crtc)) {
973 drm_for_each_encoder(encoder, dev) {
974
975 if(encoder->crtc != crtc)
976 continue;
977
978 encoder_dpms = drm_helper_choose_encoder_dpms(
979 encoder);
980
981 drm_helper_encoder_dpms(encoder, encoder_dpms);
982 }
983
984 crtc_funcs = crtc->helper_private;
985 if (crtc_funcs->dpms)
986 (*crtc_funcs->dpms) (crtc,
987 drm_helper_choose_crtc_dpms(crtc));
988 }
989 }
990
991 /* disable the unused connectors while restoring the modesetting */
992 __drm_helper_disable_unused_functions(dev);
993 drm_modeset_unlock_all(dev);
994 }
995 EXPORT_SYMBOL(drm_helper_resume_force_mode);
996
997 /**
998 * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
999 * @dev: DRM device whose CRTCs to turn off
1000 *
1001 * Drivers may want to call this on unload to ensure that all displays are
1002 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
1003 *
1004 * Note: This should only be used by non-atomic legacy drivers. For an atomic
1005 * version look at drm_atomic_helper_shutdown().
1006 *
1007 * Returns:
1008 * Zero on success, error code on failure.
1009 */
drm_helper_force_disable_all(struct drm_device * dev)1010 int drm_helper_force_disable_all(struct drm_device *dev)
1011 {
1012 struct drm_crtc *crtc;
1013 int ret = 0;
1014
1015 drm_modeset_lock_all(dev);
1016 drm_for_each_crtc(crtc, dev)
1017 if (crtc->enabled) {
1018 struct drm_mode_set set = {
1019 .crtc = crtc,
1020 };
1021
1022 ret = drm_mode_set_config_internal(&set);
1023 if (ret)
1024 goto out;
1025 }
1026 out:
1027 drm_modeset_unlock_all(dev);
1028 return ret;
1029 }
1030 EXPORT_SYMBOL(drm_helper_force_disable_all);
1031