33195e9adaab174626c172ff6a57a01ef9b03db9
[firefly-linux-kernel-4.4.55.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/kernel.h>
33 #include <linux/export.h>
34 #include <linux/moduleparam.h>
35
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_crtc_helper.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_edid.h>
43
44 /**
45  * DOC: overview
46  *
47  * The CRTC modeset helper library provides a default set_config implementation
48  * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
49  * the same callbacks which drivers can use to e.g. restore the modeset
50  * configuration on resume with drm_helper_resume_force_mode().
51  *
52  * The driver callbacks are mostly compatible with the atomic modeset helpers,
53  * except for the handling of the primary plane: Atomic helpers require that the
54  * primary plane is implemented as a real standalone plane and not directly tied
55  * to the CRTC state. For easier transition this library provides functions to
56  * implement the old semantics required by the CRTC helpers using the new plane
57  * and atomic helper callbacks.
58  *
59  * Drivers are strongly urged to convert to the atomic helpers (by way of first
60  * converting to the plane helpers). New drivers must not use these functions
61  * but need to implement the atomic interface instead, potentially using the
62  * atomic helpers for that.
63  */
64 MODULE_AUTHOR("David Airlie, Jesse Barnes");
65 MODULE_DESCRIPTION("DRM KMS helper");
66 MODULE_LICENSE("GPL and additional rights");
67
68 /**
69  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
70  *                                              connector list
71  * @dev: drm device to operate on
72  *
73  * Some userspace presumes that the first connected connector is the main
74  * display, where it's supposed to display e.g. the login screen. For
75  * laptops, this should be the main panel. Use this function to sort all
76  * (eDP/LVDS) panels to the front of the connector list, instead of
77  * painstakingly trying to initialize them in the right order.
78  */
79 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
80 {
81         struct drm_connector *connector, *tmp;
82         struct list_head panel_list;
83
84         INIT_LIST_HEAD(&panel_list);
85
86         list_for_each_entry_safe(connector, tmp,
87                                  &dev->mode_config.connector_list, head) {
88                 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
89                     connector->connector_type == DRM_MODE_CONNECTOR_eDP)
90                         list_move_tail(&connector->head, &panel_list);
91         }
92
93         list_splice(&panel_list, &dev->mode_config.connector_list);
94 }
95 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
96
97 /**
98  * drm_helper_encoder_in_use - check if a given encoder is in use
99  * @encoder: encoder to check
100  *
101  * Checks whether @encoder is with the current mode setting output configuration
102  * in use by any connector. This doesn't mean that it is actually enabled since
103  * the DPMS state is tracked separately.
104  *
105  * Returns:
106  * True if @encoder is used, false otherwise.
107  */
108 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
109 {
110         struct drm_connector *connector;
111         struct drm_device *dev = encoder->dev;
112
113         /*
114          * We can expect this mutex to be locked if we are not panicking.
115          * Locking is currently fubar in the panic handler.
116          */
117         if (!oops_in_progress) {
118                 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
119                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
120         }
121
122         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
123                 if (connector->encoder == encoder)
124                         return true;
125         return false;
126 }
127 EXPORT_SYMBOL(drm_helper_encoder_in_use);
128
129 /**
130  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
131  * @crtc: CRTC to check
132  *
133  * Checks whether @crtc is with the current mode setting output configuration
134  * in use by any connector. This doesn't mean that it is actually enabled since
135  * the DPMS state is tracked separately.
136  *
137  * Returns:
138  * True if @crtc is used, false otherwise.
139  */
140 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
141 {
142         struct drm_encoder *encoder;
143         struct drm_device *dev = crtc->dev;
144
145         /*
146          * We can expect this mutex to be locked if we are not panicking.
147          * Locking is currently fubar in the panic handler.
148          */
149         if (!oops_in_progress)
150                 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
151
152         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
153                 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
154                         return true;
155         return false;
156 }
157 EXPORT_SYMBOL(drm_helper_crtc_in_use);
158
159 static void
160 drm_encoder_disable(struct drm_encoder *encoder)
161 {
162         struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
163
164         if (encoder->bridge)
165                 encoder->bridge->funcs->disable(encoder->bridge);
166
167         if (encoder_funcs->disable)
168                 (*encoder_funcs->disable)(encoder);
169         else
170                 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
171
172         if (encoder->bridge)
173                 encoder->bridge->funcs->post_disable(encoder->bridge);
174 }
175
176 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
177 {
178         struct drm_encoder *encoder;
179         struct drm_crtc *crtc;
180
181         drm_warn_on_modeset_not_all_locked(dev);
182
183         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
184                 if (!drm_helper_encoder_in_use(encoder)) {
185                         drm_encoder_disable(encoder);
186                         /* disconnect encoder from any connector */
187                         encoder->crtc = NULL;
188                 }
189         }
190
191         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
192                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
193                 crtc->enabled = drm_helper_crtc_in_use(crtc);
194                 if (!crtc->enabled) {
195                         if (crtc_funcs->disable)
196                                 (*crtc_funcs->disable)(crtc);
197                         else
198                                 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
199                         crtc->primary->fb = NULL;
200                 }
201         }
202 }
203
204 /**
205  * drm_helper_disable_unused_functions - disable unused objects
206  * @dev: DRM device
207  *
208  * This function walks through the entire mode setting configuration of @dev. It
209  * will remove any crtc links of unused encoders and encoder links of
210  * disconnected connectors. Then it will disable all unused encoders and crtcs
211  * either by calling their disable callback if available or by calling their
212  * dpms callback with DRM_MODE_DPMS_OFF.
213  */
214 void drm_helper_disable_unused_functions(struct drm_device *dev)
215 {
216         drm_modeset_lock_all(dev);
217         __drm_helper_disable_unused_functions(dev);
218         drm_modeset_unlock_all(dev);
219 }
220 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
221
222 /*
223  * Check the CRTC we're going to map each output to vs. its current
224  * CRTC.  If they don't match, we have to disable the output and the CRTC
225  * since the driver will have to re-route things.
226  */
227 static void
228 drm_crtc_prepare_encoders(struct drm_device *dev)
229 {
230         struct drm_encoder_helper_funcs *encoder_funcs;
231         struct drm_encoder *encoder;
232
233         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
234                 encoder_funcs = encoder->helper_private;
235                 /* Disable unused encoders */
236                 if (encoder->crtc == NULL)
237                         drm_encoder_disable(encoder);
238                 /* Disable encoders whose CRTC is about to change */
239                 if (encoder_funcs->get_crtc &&
240                     encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
241                         drm_encoder_disable(encoder);
242         }
243 }
244
245 /**
246  * drm_crtc_helper_set_mode - internal helper to set a mode
247  * @crtc: CRTC to program
248  * @mode: mode to use
249  * @x: horizontal offset into the surface
250  * @y: vertical offset into the surface
251  * @old_fb: old framebuffer, for cleanup
252  *
253  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
254  * to fixup or reject the mode prior to trying to set it. This is an internal
255  * helper that drivers could e.g. use to update properties that require the
256  * entire output pipe to be disabled and re-enabled in a new configuration. For
257  * example for changing whether audio is enabled on a hdmi link or for changing
258  * panel fitter or dither attributes. It is also called by the
259  * drm_crtc_helper_set_config() helper function to drive the mode setting
260  * sequence.
261  *
262  * Returns:
263  * True if the mode was set successfully, false otherwise.
264  */
265 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
266                               struct drm_display_mode *mode,
267                               int x, int y,
268                               struct drm_framebuffer *old_fb)
269 {
270         struct drm_device *dev = crtc->dev;
271         struct drm_display_mode *adjusted_mode, saved_mode;
272         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
273         struct drm_encoder_helper_funcs *encoder_funcs;
274         int saved_x, saved_y;
275         bool saved_enabled;
276         struct drm_encoder *encoder;
277         bool ret = true;
278
279         drm_warn_on_modeset_not_all_locked(dev);
280
281         saved_enabled = crtc->enabled;
282         crtc->enabled = drm_helper_crtc_in_use(crtc);
283         if (!crtc->enabled)
284                 return true;
285
286         adjusted_mode = drm_mode_duplicate(dev, mode);
287         if (!adjusted_mode) {
288                 crtc->enabled = saved_enabled;
289                 return false;
290         }
291
292         saved_mode = crtc->mode;
293         saved_x = crtc->x;
294         saved_y = crtc->y;
295
296         /* Update crtc values up front so the driver can rely on them for mode
297          * setting.
298          */
299         crtc->mode = *mode;
300         crtc->x = x;
301         crtc->y = y;
302
303         /* Pass our mode to the connectors and the CRTC to give them a chance to
304          * adjust it according to limitations or connector properties, and also
305          * a chance to reject the mode entirely.
306          */
307         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
308
309                 if (encoder->crtc != crtc)
310                         continue;
311
312                 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
313                         ret = encoder->bridge->funcs->mode_fixup(
314                                         encoder->bridge, mode, adjusted_mode);
315                         if (!ret) {
316                                 DRM_DEBUG_KMS("Bridge fixup failed\n");
317                                 goto done;
318                         }
319                 }
320
321                 encoder_funcs = encoder->helper_private;
322                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
323                                                       adjusted_mode))) {
324                         DRM_DEBUG_KMS("Encoder fixup failed\n");
325                         goto done;
326                 }
327         }
328
329         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
330                 DRM_DEBUG_KMS("CRTC fixup failed\n");
331                 goto done;
332         }
333         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
334
335         /* Prepare the encoders and CRTCs before setting the mode. */
336         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
337
338                 if (encoder->crtc != crtc)
339                         continue;
340
341                 if (encoder->bridge)
342                         encoder->bridge->funcs->disable(encoder->bridge);
343
344                 encoder_funcs = encoder->helper_private;
345                 /* Disable the encoders as the first thing we do. */
346                 encoder_funcs->prepare(encoder);
347
348                 if (encoder->bridge)
349                         encoder->bridge->funcs->post_disable(encoder->bridge);
350         }
351
352         drm_crtc_prepare_encoders(dev);
353
354         crtc_funcs->prepare(crtc);
355
356         /* Set up the DPLL and any encoders state that needs to adjust or depend
357          * on the DPLL.
358          */
359         ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
360         if (!ret)
361             goto done;
362
363         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
364
365                 if (encoder->crtc != crtc)
366                         continue;
367
368                 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
369                         encoder->base.id, encoder->name,
370                         mode->base.id, mode->name);
371                 encoder_funcs = encoder->helper_private;
372                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
373
374                 if (encoder->bridge && encoder->bridge->funcs->mode_set)
375                         encoder->bridge->funcs->mode_set(encoder->bridge, mode,
376                                         adjusted_mode);
377         }
378
379         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
380         crtc_funcs->commit(crtc);
381
382         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
383
384                 if (encoder->crtc != crtc)
385                         continue;
386
387                 if (encoder->bridge)
388                         encoder->bridge->funcs->pre_enable(encoder->bridge);
389
390                 encoder_funcs = encoder->helper_private;
391                 encoder_funcs->commit(encoder);
392
393                 if (encoder->bridge)
394                         encoder->bridge->funcs->enable(encoder->bridge);
395         }
396
397         /* Store real post-adjustment hardware mode. */
398         crtc->hwmode = *adjusted_mode;
399
400         /* Calculate and store various constants which
401          * are later needed by vblank and swap-completion
402          * timestamping. They are derived from true hwmode.
403          */
404         drm_calc_timestamping_constants(crtc, &crtc->hwmode);
405
406         /* FIXME: add subpixel order */
407 done:
408         drm_mode_destroy(dev, adjusted_mode);
409         if (!ret) {
410                 crtc->enabled = saved_enabled;
411                 crtc->mode = saved_mode;
412                 crtc->x = saved_x;
413                 crtc->y = saved_y;
414         }
415
416         return ret;
417 }
418 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
419
420 static void
421 drm_crtc_helper_disable(struct drm_crtc *crtc)
422 {
423         struct drm_device *dev = crtc->dev;
424         struct drm_connector *connector;
425         struct drm_encoder *encoder;
426
427         /* Decouple all encoders and their attached connectors from this crtc */
428         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
429                 if (encoder->crtc != crtc)
430                         continue;
431
432                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
433                         if (connector->encoder != encoder)
434                                 continue;
435
436                         connector->encoder = NULL;
437
438                         /*
439                          * drm_helper_disable_unused_functions() ought to be
440                          * doing this, but since we've decoupled the encoder
441                          * from the connector above, the required connection
442                          * between them is henceforth no longer available.
443                          */
444                         connector->dpms = DRM_MODE_DPMS_OFF;
445                 }
446         }
447
448         __drm_helper_disable_unused_functions(dev);
449 }
450
451 /**
452  * drm_crtc_helper_set_config - set a new config from userspace
453  * @set: mode set configuration
454  *
455  * Setup a new configuration, provided by the upper layers (either an ioctl call
456  * from userspace or internally e.g. from the fbdev support code) in @set, and
457  * enable it. This is the main helper functions for drivers that implement
458  * kernel mode setting with the crtc helper functions and the assorted
459  * ->prepare(), ->modeset() and ->commit() helper callbacks.
460  *
461  * Returns:
462  * Returns 0 on success, negative errno numbers on failure.
463  */
464 int drm_crtc_helper_set_config(struct drm_mode_set *set)
465 {
466         struct drm_device *dev;
467         struct drm_crtc *new_crtc;
468         struct drm_encoder *save_encoders, *new_encoder, *encoder;
469         bool mode_changed = false; /* if true do a full mode set */
470         bool fb_changed = false; /* if true and !mode_changed just do a flip */
471         struct drm_connector *save_connectors, *connector;
472         int count = 0, ro, fail = 0;
473         struct drm_crtc_helper_funcs *crtc_funcs;
474         struct drm_mode_set save_set;
475         int ret;
476         int i;
477
478         DRM_DEBUG_KMS("\n");
479
480         BUG_ON(!set);
481         BUG_ON(!set->crtc);
482         BUG_ON(!set->crtc->helper_private);
483
484         /* Enforce sane interface api - has been abused by the fb helper. */
485         BUG_ON(!set->mode && set->fb);
486         BUG_ON(set->fb && set->num_connectors == 0);
487
488         crtc_funcs = set->crtc->helper_private;
489
490         if (!set->mode)
491                 set->fb = NULL;
492
493         if (set->fb) {
494                 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
495                                 set->crtc->base.id, set->fb->base.id,
496                                 (int)set->num_connectors, set->x, set->y);
497         } else {
498                 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
499                 drm_crtc_helper_disable(set->crtc);
500                 return 0;
501         }
502
503         dev = set->crtc->dev;
504
505         drm_warn_on_modeset_not_all_locked(dev);
506
507         /*
508          * Allocate space for the backup of all (non-pointer) encoder and
509          * connector data.
510          */
511         save_encoders = kzalloc(dev->mode_config.num_encoder *
512                                 sizeof(struct drm_encoder), GFP_KERNEL);
513         if (!save_encoders)
514                 return -ENOMEM;
515
516         save_connectors = kzalloc(dev->mode_config.num_connector *
517                                 sizeof(struct drm_connector), GFP_KERNEL);
518         if (!save_connectors) {
519                 kfree(save_encoders);
520                 return -ENOMEM;
521         }
522
523         /*
524          * Copy data. Note that driver private data is not affected.
525          * Should anything bad happen only the expected state is
526          * restored, not the drivers personal bookkeeping.
527          */
528         count = 0;
529         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
530                 save_encoders[count++] = *encoder;
531         }
532
533         count = 0;
534         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
535                 save_connectors[count++] = *connector;
536         }
537
538         save_set.crtc = set->crtc;
539         save_set.mode = &set->crtc->mode;
540         save_set.x = set->crtc->x;
541         save_set.y = set->crtc->y;
542         save_set.fb = set->crtc->primary->fb;
543
544         /* We should be able to check here if the fb has the same properties
545          * and then just flip_or_move it */
546         if (set->crtc->primary->fb != set->fb) {
547                 /* If we have no fb then treat it as a full mode set */
548                 if (set->crtc->primary->fb == NULL) {
549                         DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
550                         mode_changed = true;
551                 } else if (set->fb == NULL) {
552                         mode_changed = true;
553                 } else if (set->fb->pixel_format !=
554                            set->crtc->primary->fb->pixel_format) {
555                         mode_changed = true;
556                 } else
557                         fb_changed = true;
558         }
559
560         if (set->x != set->crtc->x || set->y != set->crtc->y)
561                 fb_changed = true;
562
563         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
564                 DRM_DEBUG_KMS("modes are different, full mode set\n");
565                 drm_mode_debug_printmodeline(&set->crtc->mode);
566                 drm_mode_debug_printmodeline(set->mode);
567                 mode_changed = true;
568         }
569
570         /* a) traverse passed in connector list and get encoders for them */
571         count = 0;
572         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
573                 struct drm_connector_helper_funcs *connector_funcs =
574                         connector->helper_private;
575                 new_encoder = connector->encoder;
576                 for (ro = 0; ro < set->num_connectors; ro++) {
577                         if (set->connectors[ro] == connector) {
578                                 new_encoder = connector_funcs->best_encoder(connector);
579                                 /* if we can't get an encoder for a connector
580                                    we are setting now - then fail */
581                                 if (new_encoder == NULL)
582                                         /* don't break so fail path works correct */
583                                         fail = 1;
584
585                                 if (connector->dpms != DRM_MODE_DPMS_ON) {
586                                         DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
587                                         mode_changed = true;
588                                 }
589
590                                 break;
591                         }
592                 }
593
594                 if (new_encoder != connector->encoder) {
595                         DRM_DEBUG_KMS("encoder changed, full mode switch\n");
596                         mode_changed = true;
597                         /* If the encoder is reused for another connector, then
598                          * the appropriate crtc will be set later.
599                          */
600                         if (connector->encoder)
601                                 connector->encoder->crtc = NULL;
602                         connector->encoder = new_encoder;
603                 }
604         }
605
606         if (fail) {
607                 ret = -EINVAL;
608                 goto fail;
609         }
610
611         count = 0;
612         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
613                 if (!connector->encoder)
614                         continue;
615
616                 if (connector->encoder->crtc == set->crtc)
617                         new_crtc = NULL;
618                 else
619                         new_crtc = connector->encoder->crtc;
620
621                 for (ro = 0; ro < set->num_connectors; ro++) {
622                         if (set->connectors[ro] == connector)
623                                 new_crtc = set->crtc;
624                 }
625
626                 /* Make sure the new CRTC will work with the encoder */
627                 if (new_crtc &&
628                     !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
629                         ret = -EINVAL;
630                         goto fail;
631                 }
632                 if (new_crtc != connector->encoder->crtc) {
633                         DRM_DEBUG_KMS("crtc changed, full mode switch\n");
634                         mode_changed = true;
635                         connector->encoder->crtc = new_crtc;
636                 }
637                 if (new_crtc) {
638                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
639                                 connector->base.id, connector->name,
640                                 new_crtc->base.id);
641                 } else {
642                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
643                                 connector->base.id, connector->name);
644                 }
645         }
646
647         /* mode_set_base is not a required function */
648         if (fb_changed && !crtc_funcs->mode_set_base)
649                 mode_changed = true;
650
651         if (mode_changed) {
652                 if (drm_helper_crtc_in_use(set->crtc)) {
653                         DRM_DEBUG_KMS("attempting to set mode from"
654                                         " userspace\n");
655                         drm_mode_debug_printmodeline(set->mode);
656                         set->crtc->primary->fb = set->fb;
657                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
658                                                       set->x, set->y,
659                                                       save_set.fb)) {
660                                 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
661                                           set->crtc->base.id);
662                                 set->crtc->primary->fb = save_set.fb;
663                                 ret = -EINVAL;
664                                 goto fail;
665                         }
666                         DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
667                         for (i = 0; i < set->num_connectors; i++) {
668                                 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
669                                               set->connectors[i]->name);
670                                 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
671                         }
672                 }
673                 __drm_helper_disable_unused_functions(dev);
674         } else if (fb_changed) {
675                 set->crtc->x = set->x;
676                 set->crtc->y = set->y;
677                 set->crtc->primary->fb = set->fb;
678                 ret = crtc_funcs->mode_set_base(set->crtc,
679                                                 set->x, set->y, save_set.fb);
680                 if (ret != 0) {
681                         set->crtc->x = save_set.x;
682                         set->crtc->y = save_set.y;
683                         set->crtc->primary->fb = save_set.fb;
684                         goto fail;
685                 }
686         }
687
688         kfree(save_connectors);
689         kfree(save_encoders);
690         return 0;
691
692 fail:
693         /* Restore all previous data. */
694         count = 0;
695         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
696                 *encoder = save_encoders[count++];
697         }
698
699         count = 0;
700         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
701                 *connector = save_connectors[count++];
702         }
703
704         /* Try to restore the config */
705         if (mode_changed &&
706             !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
707                                       save_set.y, save_set.fb))
708                 DRM_ERROR("failed to restore config after modeset failure\n");
709
710         kfree(save_connectors);
711         kfree(save_encoders);
712         return ret;
713 }
714 EXPORT_SYMBOL(drm_crtc_helper_set_config);
715
716 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
717 {
718         int dpms = DRM_MODE_DPMS_OFF;
719         struct drm_connector *connector;
720         struct drm_device *dev = encoder->dev;
721
722         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
723                 if (connector->encoder == encoder)
724                         if (connector->dpms < dpms)
725                                 dpms = connector->dpms;
726         return dpms;
727 }
728
729 /* Helper which handles bridge ordering around encoder dpms */
730 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
731 {
732         struct drm_bridge *bridge = encoder->bridge;
733         struct drm_encoder_helper_funcs *encoder_funcs;
734
735         if (bridge) {
736                 if (mode == DRM_MODE_DPMS_ON)
737                         bridge->funcs->pre_enable(bridge);
738                 else
739                         bridge->funcs->disable(bridge);
740         }
741
742         encoder_funcs = encoder->helper_private;
743         if (encoder_funcs->dpms)
744                 encoder_funcs->dpms(encoder, mode);
745
746         if (bridge) {
747                 if (mode == DRM_MODE_DPMS_ON)
748                         bridge->funcs->enable(bridge);
749                 else
750                         bridge->funcs->post_disable(bridge);
751         }
752 }
753
754 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
755 {
756         int dpms = DRM_MODE_DPMS_OFF;
757         struct drm_connector *connector;
758         struct drm_device *dev = crtc->dev;
759
760         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
761                 if (connector->encoder && connector->encoder->crtc == crtc)
762                         if (connector->dpms < dpms)
763                                 dpms = connector->dpms;
764         return dpms;
765 }
766
767 /**
768  * drm_helper_connector_dpms() - connector dpms helper implementation
769  * @connector: affected connector
770  * @mode: DPMS mode
771  *
772  * This is the main helper function provided by the crtc helper framework for
773  * implementing the DPMS connector attribute. It computes the new desired DPMS
774  * state for all encoders and crtcs in the output mesh and calls the ->dpms()
775  * callback provided by the driver appropriately.
776  */
777 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
778 {
779         struct drm_encoder *encoder = connector->encoder;
780         struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
781         int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
782
783         if (mode == connector->dpms)
784                 return;
785
786         old_dpms = connector->dpms;
787         connector->dpms = mode;
788
789         if (encoder)
790                 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
791
792         /* from off to on, do crtc then encoder */
793         if (mode < old_dpms) {
794                 if (crtc) {
795                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
796                         if (crtc_funcs->dpms)
797                                 (*crtc_funcs->dpms) (crtc,
798                                                      drm_helper_choose_crtc_dpms(crtc));
799                 }
800                 if (encoder)
801                         drm_helper_encoder_dpms(encoder, encoder_dpms);
802         }
803
804         /* from on to off, do encoder then crtc */
805         if (mode > old_dpms) {
806                 if (encoder)
807                         drm_helper_encoder_dpms(encoder, encoder_dpms);
808                 if (crtc) {
809                         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
810                         if (crtc_funcs->dpms)
811                                 (*crtc_funcs->dpms) (crtc,
812                                                      drm_helper_choose_crtc_dpms(crtc));
813                 }
814         }
815
816         return;
817 }
818 EXPORT_SYMBOL(drm_helper_connector_dpms);
819
820 /**
821  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
822  * @fb: drm_framebuffer object to fill out
823  * @mode_cmd: metadata from the userspace fb creation request
824  *
825  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
826  * metadata fields.
827  */
828 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
829                                     struct drm_mode_fb_cmd2 *mode_cmd)
830 {
831         int i;
832
833         fb->width = mode_cmd->width;
834         fb->height = mode_cmd->height;
835         for (i = 0; i < 4; i++) {
836                 fb->pitches[i] = mode_cmd->pitches[i];
837                 fb->offsets[i] = mode_cmd->offsets[i];
838         }
839         drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
840                                     &fb->bits_per_pixel);
841         fb->pixel_format = mode_cmd->pixel_format;
842         fb->flags = mode_cmd->flags;
843 }
844 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
845
846 /**
847  * drm_helper_resume_force_mode - force-restore mode setting configuration
848  * @dev: drm_device which should be restored
849  *
850  * Drivers which use the mode setting helpers can use this function to
851  * force-restore the mode setting configuration e.g. on resume or when something
852  * else might have trampled over the hw state (like some overzealous old BIOSen
853  * tended to do).
854  *
855  * This helper doesn't provide a error return value since restoring the old
856  * config should never fail due to resource allocation issues since the driver
857  * has successfully set the restored configuration already. Hence this should
858  * boil down to the equivalent of a few dpms on calls, which also don't provide
859  * an error code.
860  *
861  * Drivers where simply restoring an old configuration again might fail (e.g.
862  * due to slight differences in allocating shared resources when the
863  * configuration is restored in a different order than when userspace set it up)
864  * need to use their own restore logic.
865  */
866 void drm_helper_resume_force_mode(struct drm_device *dev)
867 {
868         struct drm_crtc *crtc;
869         struct drm_encoder *encoder;
870         struct drm_crtc_helper_funcs *crtc_funcs;
871         int encoder_dpms;
872         bool ret;
873
874         drm_modeset_lock_all(dev);
875         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
876
877                 if (!crtc->enabled)
878                         continue;
879
880                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
881                                                crtc->x, crtc->y, crtc->primary->fb);
882
883                 /* Restoring the old config should never fail! */
884                 if (ret == false)
885                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
886
887                 /* Turn off outputs that were already powered off */
888                 if (drm_helper_choose_crtc_dpms(crtc)) {
889                         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
890
891                                 if(encoder->crtc != crtc)
892                                         continue;
893
894                                 encoder_dpms = drm_helper_choose_encoder_dpms(
895                                                         encoder);
896
897                                 drm_helper_encoder_dpms(encoder, encoder_dpms);
898                         }
899
900                         crtc_funcs = crtc->helper_private;
901                         if (crtc_funcs->dpms)
902                                 (*crtc_funcs->dpms) (crtc,
903                                                      drm_helper_choose_crtc_dpms(crtc));
904                 }
905         }
906
907         /* disable the unused connectors while restoring the modesetting */
908         __drm_helper_disable_unused_functions(dev);
909         drm_modeset_unlock_all(dev);
910 }
911 EXPORT_SYMBOL(drm_helper_resume_force_mode);
912
913 /**
914  * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
915  * @crtc: DRM CRTC
916  * @mode: DRM display mode which userspace requested
917  * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
918  * @x: x offset of the CRTC scanout area on the underlying framebuffer
919  * @y: y offset of the CRTC scanout area on the underlying framebuffer
920  * @old_fb: previous framebuffer
921  *
922  * This function implements a callback useable as the ->mode_set callback
923  * required by the crtc helpers. Besides the atomic plane helper functions for
924  * the primary plane the driver must also provide the ->mode_set_nofb callback
925  * to set up the crtc.
926  *
927  * This is a transitional helper useful for converting drivers to the atomic
928  * interfaces.
929  */
930 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
931                              struct drm_display_mode *adjusted_mode, int x, int y,
932                              struct drm_framebuffer *old_fb)
933 {
934         struct drm_crtc_state *crtc_state;
935         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
936         int ret;
937
938         if (crtc->funcs->atomic_duplicate_state)
939                 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
940         else if (crtc->state)
941                 crtc_state = kmemdup(crtc->state, sizeof(*crtc_state),
942                                      GFP_KERNEL);
943         else
944                 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
945         if (!crtc_state)
946                 return -ENOMEM;
947
948         crtc_state->enable = true;
949         crtc_state->planes_changed = true;
950         crtc_state->mode_changed = true;
951         drm_mode_copy(&crtc_state->mode, mode);
952         drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
953
954         if (crtc_funcs->atomic_check) {
955                 ret = crtc_funcs->atomic_check(crtc, crtc_state);
956                 if (ret) {
957                         kfree(crtc_state);
958
959                         return ret;
960                 }
961         }
962
963         swap(crtc->state, crtc_state);
964
965         crtc_funcs->mode_set_nofb(crtc);
966
967         if (crtc_state) {
968                 if (crtc->funcs->atomic_destroy_state)
969                         crtc->funcs->atomic_destroy_state(crtc, crtc_state);
970                 else
971                         kfree(crtc_state);
972         }
973
974         return drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
975 }
976 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
977
978 /**
979  * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
980  * @crtc: DRM CRTC
981  * @x: x offset of the CRTC scanout area on the underlying framebuffer
982  * @y: y offset of the CRTC scanout area on the underlying framebuffer
983  * @old_fb: previous framebuffer
984  *
985  * This function implements a callback useable as the ->mode_set_base used
986  * required by the crtc helpers. The driver must provide the atomic plane helper
987  * functions for the primary plane.
988  *
989  * This is a transitional helper useful for converting drivers to the atomic
990  * interfaces.
991  */
992 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
993                                   struct drm_framebuffer *old_fb)
994 {
995         struct drm_plane_state *plane_state;
996         struct drm_plane *plane = crtc->primary;
997
998         if (plane->funcs->atomic_duplicate_state)
999                 plane_state = plane->funcs->atomic_duplicate_state(plane);
1000         else if (plane->state)
1001                 plane_state = kmemdup(plane->state, sizeof(*plane_state),
1002                                       GFP_KERNEL);
1003         else
1004                 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
1005         if (!plane_state)
1006                 return -ENOMEM;
1007
1008         plane_state->crtc = crtc;
1009         plane_state->fb = crtc->primary->fb;
1010         plane_state->crtc_x = 0;
1011         plane_state->crtc_y = 0;
1012         plane_state->crtc_h = crtc->mode.vdisplay;
1013         plane_state->crtc_w = crtc->mode.hdisplay;
1014         plane_state->src_x = x << 16;
1015         plane_state->src_y = y << 16;
1016         plane_state->src_h = crtc->mode.vdisplay << 16;
1017         plane_state->src_w = crtc->mode.hdisplay << 16;
1018
1019         return drm_plane_helper_commit(plane, plane_state, old_fb);
1020 }
1021 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);