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