Merge branch 'topic/drm-vblank-rework' into drm-intel-next-queued
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc_helper.c
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 *
5 * DRM core CRTC related functions
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 *
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
30 */
31
32 #include <linux/export.h>
33 #include <linux/moduleparam.h>
34
35 #include <drm/drmP.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_fourcc.h>
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_edid.h>
41
42 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
45
46 /**
47 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
48 * connector list
49 * @dev: drm device to operate on
50 *
51 * Some userspace presumes that the first connected connector is the main
52 * display, where it's supposed to display e.g. the login screen. For
53 * laptops, this should be the main panel. Use this function to sort all
54 * (eDP/LVDS) panels to the front of the connector list, instead of
55 * painstakingly trying to initialize them in the right order.
56 */
57 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
58 {
59 struct drm_connector *connector, *tmp;
60 struct list_head panel_list;
61
62 INIT_LIST_HEAD(&panel_list);
63
64 list_for_each_entry_safe(connector, tmp,
65 &dev->mode_config.connector_list, head) {
66 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
67 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
68 list_move_tail(&connector->head, &panel_list);
69 }
70
71 list_splice(&panel_list, &dev->mode_config.connector_list);
72 }
73 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
74
75 /**
76 * drm_helper_encoder_in_use - check if a given encoder is in use
77 * @encoder: encoder to check
78 *
79 * Checks whether @encoder is with the current mode setting output configuration
80 * in use by any connector. This doesn't mean that it is actually enabled since
81 * the DPMS state is tracked separately.
82 *
83 * Returns:
84 * True if @encoder is used, false otherwise.
85 */
86 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
87 {
88 struct drm_connector *connector;
89 struct drm_device *dev = encoder->dev;
90
91 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
92 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
93 if (connector->encoder == encoder)
94 return true;
95 return false;
96 }
97 EXPORT_SYMBOL(drm_helper_encoder_in_use);
98
99 /**
100 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
101 * @crtc: CRTC to check
102 *
103 * Checks whether @crtc is with the current mode setting output configuration
104 * in use by any connector. This doesn't mean that it is actually enabled since
105 * the DPMS state is tracked separately.
106 *
107 * Returns:
108 * True if @crtc is used, false otherwise.
109 */
110 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
111 {
112 struct drm_encoder *encoder;
113 struct drm_device *dev = crtc->dev;
114
115 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
116 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
117 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
118 return true;
119 return false;
120 }
121 EXPORT_SYMBOL(drm_helper_crtc_in_use);
122
123 static void
124 drm_encoder_disable(struct drm_encoder *encoder)
125 {
126 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
127
128 if (encoder->bridge)
129 encoder->bridge->funcs->disable(encoder->bridge);
130
131 if (encoder_funcs->disable)
132 (*encoder_funcs->disable)(encoder);
133 else
134 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
135
136 if (encoder->bridge)
137 encoder->bridge->funcs->post_disable(encoder->bridge);
138 }
139
140 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
141 {
142 struct drm_encoder *encoder;
143 struct drm_crtc *crtc;
144
145 drm_warn_on_modeset_not_all_locked(dev);
146
147 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
148 if (!drm_helper_encoder_in_use(encoder)) {
149 drm_encoder_disable(encoder);
150 /* disconnector encoder from any connector */
151 encoder->crtc = NULL;
152 }
153 }
154
155 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
156 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
157 crtc->enabled = drm_helper_crtc_in_use(crtc);
158 if (!crtc->enabled) {
159 if (crtc_funcs->disable)
160 (*crtc_funcs->disable)(crtc);
161 else
162 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
163 crtc->primary->fb = NULL;
164 }
165 }
166 }
167
168 /**
169 * drm_helper_disable_unused_functions - disable unused objects
170 * @dev: DRM device
171 *
172 * This function walks through the entire mode setting configuration of @dev. It
173 * will remove any crtc links of unused encoders and encoder links of
174 * disconnected connectors. Then it will disable all unused encoders and crtcs
175 * either by calling their disable callback if available or by calling their
176 * dpms callback with DRM_MODE_DPMS_OFF.
177 */
178 void drm_helper_disable_unused_functions(struct drm_device *dev)
179 {
180 drm_modeset_lock_all(dev);
181 __drm_helper_disable_unused_functions(dev);
182 drm_modeset_unlock_all(dev);
183 }
184 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
185
186 /*
187 * Check the CRTC we're going to map each output to vs. its current
188 * CRTC. If they don't match, we have to disable the output and the CRTC
189 * since the driver will have to re-route things.
190 */
191 static void
192 drm_crtc_prepare_encoders(struct drm_device *dev)
193 {
194 struct drm_encoder_helper_funcs *encoder_funcs;
195 struct drm_encoder *encoder;
196
197 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
198 encoder_funcs = encoder->helper_private;
199 /* Disable unused encoders */
200 if (encoder->crtc == NULL)
201 drm_encoder_disable(encoder);
202 /* Disable encoders whose CRTC is about to change */
203 if (encoder_funcs->get_crtc &&
204 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
205 drm_encoder_disable(encoder);
206 }
207 }
208
209 /**
210 * drm_crtc_helper_set_mode - internal helper to set a mode
211 * @crtc: CRTC to program
212 * @mode: mode to use
213 * @x: horizontal offset into the surface
214 * @y: vertical offset into the surface
215 * @old_fb: old framebuffer, for cleanup
216 *
217 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
218 * to fixup or reject the mode prior to trying to set it. This is an internal
219 * helper that drivers could e.g. use to update properties that require the
220 * entire output pipe to be disabled and re-enabled in a new configuration. For
221 * example for changing whether audio is enabled on a hdmi link or for changing
222 * panel fitter or dither attributes. It is also called by the
223 * drm_crtc_helper_set_config() helper function to drive the mode setting
224 * sequence.
225 *
226 * Returns:
227 * True if the mode was set successfully, false otherwise.
228 */
229 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
230 struct drm_display_mode *mode,
231 int x, int y,
232 struct drm_framebuffer *old_fb)
233 {
234 struct drm_device *dev = crtc->dev;
235 struct drm_display_mode *adjusted_mode, saved_mode;
236 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
237 struct drm_encoder_helper_funcs *encoder_funcs;
238 int saved_x, saved_y;
239 bool saved_enabled;
240 struct drm_encoder *encoder;
241 bool ret = true;
242
243 drm_warn_on_modeset_not_all_locked(dev);
244
245 saved_enabled = crtc->enabled;
246 crtc->enabled = drm_helper_crtc_in_use(crtc);
247 if (!crtc->enabled)
248 return true;
249
250 adjusted_mode = drm_mode_duplicate(dev, mode);
251 if (!adjusted_mode) {
252 crtc->enabled = saved_enabled;
253 return false;
254 }
255
256 saved_mode = crtc->mode;
257 saved_x = crtc->x;
258 saved_y = crtc->y;
259
260 /* Update crtc values up front so the driver can rely on them for mode
261 * setting.
262 */
263 crtc->mode = *mode;
264 crtc->x = x;
265 crtc->y = y;
266
267 /* Pass our mode to the connectors and the CRTC to give them a chance to
268 * adjust it according to limitations or connector properties, and also
269 * a chance to reject the mode entirely.
270 */
271 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
272
273 if (encoder->crtc != crtc)
274 continue;
275
276 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
277 ret = encoder->bridge->funcs->mode_fixup(
278 encoder->bridge, mode, adjusted_mode);
279 if (!ret) {
280 DRM_DEBUG_KMS("Bridge fixup failed\n");
281 goto done;
282 }
283 }
284
285 encoder_funcs = encoder->helper_private;
286 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
287 adjusted_mode))) {
288 DRM_DEBUG_KMS("Encoder fixup failed\n");
289 goto done;
290 }
291 }
292
293 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
294 DRM_DEBUG_KMS("CRTC fixup failed\n");
295 goto done;
296 }
297 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
298
299 /* Prepare the encoders and CRTCs before setting the mode. */
300 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
301
302 if (encoder->crtc != crtc)
303 continue;
304
305 if (encoder->bridge)
306 encoder->bridge->funcs->disable(encoder->bridge);
307
308 encoder_funcs = encoder->helper_private;
309 /* Disable the encoders as the first thing we do. */
310 encoder_funcs->prepare(encoder);
311
312 if (encoder->bridge)
313 encoder->bridge->funcs->post_disable(encoder->bridge);
314 }
315
316 drm_crtc_prepare_encoders(dev);
317
318 crtc_funcs->prepare(crtc);
319
320 /* Set up the DPLL and any encoders state that needs to adjust or depend
321 * on the DPLL.
322 */
323 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
324 if (!ret)
325 goto done;
326
327 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
328
329 if (encoder->crtc != crtc)
330 continue;
331
332 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
333 encoder->base.id, drm_get_encoder_name(encoder),
334 mode->base.id, mode->name);
335 encoder_funcs = encoder->helper_private;
336 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
337
338 if (encoder->bridge && encoder->bridge->funcs->mode_set)
339 encoder->bridge->funcs->mode_set(encoder->bridge, mode,
340 adjusted_mode);
341 }
342
343 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
344 crtc_funcs->commit(crtc);
345
346 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
347
348 if (encoder->crtc != crtc)
349 continue;
350
351 if (encoder->bridge)
352 encoder->bridge->funcs->pre_enable(encoder->bridge);
353
354 encoder_funcs = encoder->helper_private;
355 encoder_funcs->commit(encoder);
356
357 if (encoder->bridge)
358 encoder->bridge->funcs->enable(encoder->bridge);
359 }
360
361 /* Store real post-adjustment hardware mode. */
362 crtc->hwmode = *adjusted_mode;
363
364 /* Calculate and store various constants which
365 * are later needed by vblank and swap-completion
366 * timestamping. They are derived from true hwmode.
367 */
368 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
369
370 /* FIXME: add subpixel order */
371 done:
372 drm_mode_destroy(dev, adjusted_mode);
373 if (!ret) {
374 crtc->enabled = saved_enabled;
375 crtc->mode = saved_mode;
376 crtc->x = saved_x;
377 crtc->y = saved_y;
378 }
379
380 return ret;
381 }
382 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
383
384 static void
385 drm_crtc_helper_disable(struct drm_crtc *crtc)
386 {
387 struct drm_device *dev = crtc->dev;
388 struct drm_connector *connector;
389 struct drm_encoder *encoder;
390
391 /* Decouple all encoders and their attached connectors from this crtc */
392 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
393 if (encoder->crtc != crtc)
394 continue;
395
396 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397 if (connector->encoder != encoder)
398 continue;
399
400 connector->encoder = NULL;
401
402 /*
403 * drm_helper_disable_unused_functions() ought to be
404 * doing this, but since we've decoupled the encoder
405 * from the connector above, the required connection
406 * between them is henceforth no longer available.
407 */
408 connector->dpms = DRM_MODE_DPMS_OFF;
409 }
410 }
411
412 __drm_helper_disable_unused_functions(dev);
413 }
414
415 /**
416 * drm_crtc_helper_set_config - set a new config from userspace
417 * @set: mode set configuration
418 *
419 * Setup a new configuration, provided by the upper layers (either an ioctl call
420 * from userspace or internally e.g. from the fbdev support code) in @set, and
421 * enable it. This is the main helper functions for drivers that implement
422 * kernel mode setting with the crtc helper functions and the assorted
423 * ->prepare(), ->modeset() and ->commit() helper callbacks.
424 *
425 * Returns:
426 * Returns 0 on success, negative errno numbers on failure.
427 */
428 int drm_crtc_helper_set_config(struct drm_mode_set *set)
429 {
430 struct drm_device *dev;
431 struct drm_crtc *new_crtc;
432 struct drm_encoder *save_encoders, *new_encoder, *encoder;
433 bool mode_changed = false; /* if true do a full mode set */
434 bool fb_changed = false; /* if true and !mode_changed just do a flip */
435 struct drm_connector *save_connectors, *connector;
436 int count = 0, ro, fail = 0;
437 struct drm_crtc_helper_funcs *crtc_funcs;
438 struct drm_mode_set save_set;
439 int ret;
440 int i;
441
442 DRM_DEBUG_KMS("\n");
443
444 BUG_ON(!set);
445 BUG_ON(!set->crtc);
446 BUG_ON(!set->crtc->helper_private);
447
448 /* Enforce sane interface api - has been abused by the fb helper. */
449 BUG_ON(!set->mode && set->fb);
450 BUG_ON(set->fb && set->num_connectors == 0);
451
452 crtc_funcs = set->crtc->helper_private;
453
454 if (!set->mode)
455 set->fb = NULL;
456
457 if (set->fb) {
458 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
459 set->crtc->base.id, set->fb->base.id,
460 (int)set->num_connectors, set->x, set->y);
461 } else {
462 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
463 drm_crtc_helper_disable(set->crtc);
464 return 0;
465 }
466
467 dev = set->crtc->dev;
468
469 drm_warn_on_modeset_not_all_locked(dev);
470
471 /*
472 * Allocate space for the backup of all (non-pointer) encoder and
473 * connector data.
474 */
475 save_encoders = kzalloc(dev->mode_config.num_encoder *
476 sizeof(struct drm_encoder), GFP_KERNEL);
477 if (!save_encoders)
478 return -ENOMEM;
479
480 save_connectors = kzalloc(dev->mode_config.num_connector *
481 sizeof(struct drm_connector), GFP_KERNEL);
482 if (!save_connectors) {
483 kfree(save_encoders);
484 return -ENOMEM;
485 }
486
487 /*
488 * Copy data. Note that driver private data is not affected.
489 * Should anything bad happen only the expected state is
490 * restored, not the drivers personal bookkeeping.
491 */
492 count = 0;
493 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
494 save_encoders[count++] = *encoder;
495 }
496
497 count = 0;
498 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
499 save_connectors[count++] = *connector;
500 }
501
502 save_set.crtc = set->crtc;
503 save_set.mode = &set->crtc->mode;
504 save_set.x = set->crtc->x;
505 save_set.y = set->crtc->y;
506 save_set.fb = set->crtc->primary->fb;
507
508 /* We should be able to check here if the fb has the same properties
509 * and then just flip_or_move it */
510 if (set->crtc->primary->fb != set->fb) {
511 /* If we have no fb then treat it as a full mode set */
512 if (set->crtc->primary->fb == NULL) {
513 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
514 mode_changed = true;
515 } else if (set->fb == NULL) {
516 mode_changed = true;
517 } else if (set->fb->pixel_format !=
518 set->crtc->primary->fb->pixel_format) {
519 mode_changed = true;
520 } else
521 fb_changed = true;
522 }
523
524 if (set->x != set->crtc->x || set->y != set->crtc->y)
525 fb_changed = true;
526
527 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
528 DRM_DEBUG_KMS("modes are different, full mode set\n");
529 drm_mode_debug_printmodeline(&set->crtc->mode);
530 drm_mode_debug_printmodeline(set->mode);
531 mode_changed = true;
532 }
533
534 /* a) traverse passed in connector list and get encoders for them */
535 count = 0;
536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
537 struct drm_connector_helper_funcs *connector_funcs =
538 connector->helper_private;
539 new_encoder = connector->encoder;
540 for (ro = 0; ro < set->num_connectors; ro++) {
541 if (set->connectors[ro] == connector) {
542 new_encoder = connector_funcs->best_encoder(connector);
543 /* if we can't get an encoder for a connector
544 we are setting now - then fail */
545 if (new_encoder == NULL)
546 /* don't break so fail path works correct */
547 fail = 1;
548
549 if (connector->dpms != DRM_MODE_DPMS_ON) {
550 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
551 mode_changed = true;
552 }
553
554 break;
555 }
556 }
557
558 if (new_encoder != connector->encoder) {
559 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
560 mode_changed = true;
561 /* If the encoder is reused for another connector, then
562 * the appropriate crtc will be set later.
563 */
564 if (connector->encoder)
565 connector->encoder->crtc = NULL;
566 connector->encoder = new_encoder;
567 }
568 }
569
570 if (fail) {
571 ret = -EINVAL;
572 goto fail;
573 }
574
575 count = 0;
576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
577 if (!connector->encoder)
578 continue;
579
580 if (connector->encoder->crtc == set->crtc)
581 new_crtc = NULL;
582 else
583 new_crtc = connector->encoder->crtc;
584
585 for (ro = 0; ro < set->num_connectors; ro++) {
586 if (set->connectors[ro] == connector)
587 new_crtc = set->crtc;
588 }
589
590 /* Make sure the new CRTC will work with the encoder */
591 if (new_crtc &&
592 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
593 ret = -EINVAL;
594 goto fail;
595 }
596 if (new_crtc != connector->encoder->crtc) {
597 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
598 mode_changed = true;
599 connector->encoder->crtc = new_crtc;
600 }
601 if (new_crtc) {
602 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
603 connector->base.id, drm_get_connector_name(connector),
604 new_crtc->base.id);
605 } else {
606 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
607 connector->base.id, drm_get_connector_name(connector));
608 }
609 }
610
611 /* mode_set_base is not a required function */
612 if (fb_changed && !crtc_funcs->mode_set_base)
613 mode_changed = true;
614
615 if (mode_changed) {
616 if (drm_helper_crtc_in_use(set->crtc)) {
617 DRM_DEBUG_KMS("attempting to set mode from"
618 " userspace\n");
619 drm_mode_debug_printmodeline(set->mode);
620 set->crtc->primary->fb = set->fb;
621 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
622 set->x, set->y,
623 save_set.fb)) {
624 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
625 set->crtc->base.id);
626 set->crtc->primary->fb = save_set.fb;
627 ret = -EINVAL;
628 goto fail;
629 }
630 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
631 for (i = 0; i < set->num_connectors; i++) {
632 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
633 drm_get_connector_name(set->connectors[i]));
634 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
635 }
636 }
637 __drm_helper_disable_unused_functions(dev);
638 } else if (fb_changed) {
639 set->crtc->x = set->x;
640 set->crtc->y = set->y;
641 set->crtc->primary->fb = set->fb;
642 ret = crtc_funcs->mode_set_base(set->crtc,
643 set->x, set->y, save_set.fb);
644 if (ret != 0) {
645 set->crtc->x = save_set.x;
646 set->crtc->y = save_set.y;
647 set->crtc->primary->fb = save_set.fb;
648 goto fail;
649 }
650 }
651
652 kfree(save_connectors);
653 kfree(save_encoders);
654 return 0;
655
656 fail:
657 /* Restore all previous data. */
658 count = 0;
659 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
660 *encoder = save_encoders[count++];
661 }
662
663 count = 0;
664 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
665 *connector = save_connectors[count++];
666 }
667
668 /* Try to restore the config */
669 if (mode_changed &&
670 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
671 save_set.y, save_set.fb))
672 DRM_ERROR("failed to restore config after modeset failure\n");
673
674 kfree(save_connectors);
675 kfree(save_encoders);
676 return ret;
677 }
678 EXPORT_SYMBOL(drm_crtc_helper_set_config);
679
680 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
681 {
682 int dpms = DRM_MODE_DPMS_OFF;
683 struct drm_connector *connector;
684 struct drm_device *dev = encoder->dev;
685
686 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
687 if (connector->encoder == encoder)
688 if (connector->dpms < dpms)
689 dpms = connector->dpms;
690 return dpms;
691 }
692
693 /* Helper which handles bridge ordering around encoder dpms */
694 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
695 {
696 struct drm_bridge *bridge = encoder->bridge;
697 struct drm_encoder_helper_funcs *encoder_funcs;
698
699 if (bridge) {
700 if (mode == DRM_MODE_DPMS_ON)
701 bridge->funcs->pre_enable(bridge);
702 else
703 bridge->funcs->disable(bridge);
704 }
705
706 encoder_funcs = encoder->helper_private;
707 if (encoder_funcs->dpms)
708 encoder_funcs->dpms(encoder, mode);
709
710 if (bridge) {
711 if (mode == DRM_MODE_DPMS_ON)
712 bridge->funcs->enable(bridge);
713 else
714 bridge->funcs->post_disable(bridge);
715 }
716 }
717
718 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
719 {
720 int dpms = DRM_MODE_DPMS_OFF;
721 struct drm_connector *connector;
722 struct drm_device *dev = crtc->dev;
723
724 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
725 if (connector->encoder && connector->encoder->crtc == crtc)
726 if (connector->dpms < dpms)
727 dpms = connector->dpms;
728 return dpms;
729 }
730
731 /**
732 * drm_helper_connector_dpms() - connector dpms helper implementation
733 * @connector: affected connector
734 * @mode: DPMS mode
735 *
736 * This is the main helper function provided by the crtc helper framework for
737 * implementing the DPMS connector attribute. It computes the new desired DPMS
738 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
739 * callback provided by the driver appropriately.
740 */
741 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
742 {
743 struct drm_encoder *encoder = connector->encoder;
744 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
745 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
746
747 if (mode == connector->dpms)
748 return;
749
750 old_dpms = connector->dpms;
751 connector->dpms = mode;
752
753 if (encoder)
754 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
755
756 /* from off to on, do crtc then encoder */
757 if (mode < old_dpms) {
758 if (crtc) {
759 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
760 if (crtc_funcs->dpms)
761 (*crtc_funcs->dpms) (crtc,
762 drm_helper_choose_crtc_dpms(crtc));
763 }
764 if (encoder)
765 drm_helper_encoder_dpms(encoder, encoder_dpms);
766 }
767
768 /* from on to off, do encoder then crtc */
769 if (mode > old_dpms) {
770 if (encoder)
771 drm_helper_encoder_dpms(encoder, encoder_dpms);
772 if (crtc) {
773 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
774 if (crtc_funcs->dpms)
775 (*crtc_funcs->dpms) (crtc,
776 drm_helper_choose_crtc_dpms(crtc));
777 }
778 }
779
780 return;
781 }
782 EXPORT_SYMBOL(drm_helper_connector_dpms);
783
784 /**
785 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
786 * @fb: drm_framebuffer object to fill out
787 * @mode_cmd: metadata from the userspace fb creation request
788 *
789 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
790 * metadata fields.
791 */
792 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
793 struct drm_mode_fb_cmd2 *mode_cmd)
794 {
795 int i;
796
797 fb->width = mode_cmd->width;
798 fb->height = mode_cmd->height;
799 for (i = 0; i < 4; i++) {
800 fb->pitches[i] = mode_cmd->pitches[i];
801 fb->offsets[i] = mode_cmd->offsets[i];
802 }
803 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
804 &fb->bits_per_pixel);
805 fb->pixel_format = mode_cmd->pixel_format;
806 }
807 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
808
809 /**
810 * drm_helper_resume_force_mode - force-restore mode setting configuration
811 * @dev: drm_device which should be restored
812 *
813 * Drivers which use the mode setting helpers can use this function to
814 * force-restore the mode setting configuration e.g. on resume or when something
815 * else might have trampled over the hw state (like some overzealous old BIOSen
816 * tended to do).
817 *
818 * This helper doesn't provide a error return value since restoring the old
819 * config should never fail due to resource allocation issues since the driver
820 * has successfully set the restored configuration already. Hence this should
821 * boil down to the equivalent of a few dpms on calls, which also don't provide
822 * an error code.
823 *
824 * Drivers where simply restoring an old configuration again might fail (e.g.
825 * due to slight differences in allocating shared resources when the
826 * configuration is restored in a different order than when userspace set it up)
827 * need to use their own restore logic.
828 */
829 void drm_helper_resume_force_mode(struct drm_device *dev)
830 {
831 struct drm_crtc *crtc;
832 struct drm_encoder *encoder;
833 struct drm_crtc_helper_funcs *crtc_funcs;
834 int encoder_dpms;
835 bool ret;
836
837 drm_modeset_lock_all(dev);
838 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
839
840 if (!crtc->enabled)
841 continue;
842
843 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
844 crtc->x, crtc->y, crtc->primary->fb);
845
846 /* Restoring the old config should never fail! */
847 if (ret == false)
848 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
849
850 /* Turn off outputs that were already powered off */
851 if (drm_helper_choose_crtc_dpms(crtc)) {
852 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
853
854 if(encoder->crtc != crtc)
855 continue;
856
857 encoder_dpms = drm_helper_choose_encoder_dpms(
858 encoder);
859
860 drm_helper_encoder_dpms(encoder, encoder_dpms);
861 }
862
863 crtc_funcs = crtc->helper_private;
864 if (crtc_funcs->dpms)
865 (*crtc_funcs->dpms) (crtc,
866 drm_helper_choose_crtc_dpms(crtc));
867 }
868 }
869
870 /* disable the unused connectors while restoring the modesetting */
871 __drm_helper_disable_unused_functions(dev);
872 drm_modeset_unlock_all(dev);
873 }
874 EXPORT_SYMBOL(drm_helper_resume_force_mode);
This page took 0.066302 seconds and 5 git commands to generate.