Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile
[linux-2.6] / 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 "drmP.h"
33 #include "drm_crtc.h"
34 #include "drm_crtc_helper.h"
35
36 /*
37  * Detailed mode info for 800x600@60Hz
38  */
39 static struct drm_display_mode std_mode[] = {
40         { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
41                    968, 1056, 0, 600, 601, 605, 628, 0,
42                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
43 };
44
45 /**
46  * drm_helper_probe_connector_modes - get complete set of display modes
47  * @dev: DRM device
48  * @maxX: max width for modes
49  * @maxY: max height for modes
50  *
51  * LOCKING:
52  * Caller must hold mode config lock.
53  *
54  * Based on @dev's mode_config layout, scan all the connectors and try to detect
55  * modes on them.  Modes will first be added to the connector's probed_modes
56  * list, then culled (based on validity and the @maxX, @maxY parameters) and
57  * put into the normal modes list.
58  *
59  * Intended to be used either at bootup time or when major configuration
60  * changes have occurred.
61  *
62  * FIXME: take into account monitor limits
63  */
64 void drm_helper_probe_single_connector_modes(struct drm_connector *connector,
65                                              uint32_t maxX, uint32_t maxY)
66 {
67         struct drm_device *dev = connector->dev;
68         struct drm_display_mode *mode, *t;
69         struct drm_connector_helper_funcs *connector_funcs =
70                 connector->helper_private;
71         int ret;
72
73         DRM_DEBUG("%s\n", drm_get_connector_name(connector));
74         /* set all modes to the unverified state */
75         list_for_each_entry_safe(mode, t, &connector->modes, head)
76                 mode->status = MODE_UNVERIFIED;
77
78         connector->status = connector->funcs->detect(connector);
79
80         if (connector->status == connector_status_disconnected) {
81                 DRM_DEBUG("%s is disconnected\n",
82                           drm_get_connector_name(connector));
83                 /* TODO set EDID to NULL */
84                 return;
85         }
86
87         ret = (*connector_funcs->get_modes)(connector);
88
89         if (ret) {
90                 drm_mode_connector_list_update(connector);
91         }
92
93         if (maxX && maxY)
94                 drm_mode_validate_size(dev, &connector->modes, maxX,
95                                        maxY, 0);
96         list_for_each_entry_safe(mode, t, &connector->modes, head) {
97                 if (mode->status == MODE_OK)
98                         mode->status = connector_funcs->mode_valid(connector,
99                                                                    mode);
100         }
101
102
103         drm_mode_prune_invalid(dev, &connector->modes, true);
104
105         if (list_empty(&connector->modes)) {
106                 struct drm_display_mode *stdmode;
107
108                 DRM_DEBUG("No valid modes on %s\n",
109                           drm_get_connector_name(connector));
110
111                 /* Should we do this here ???
112                  * When no valid EDID modes are available we end up
113                  * here and bailed in the past, now we add a standard
114                  * 640x480@60Hz mode and carry on.
115                  */
116                 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
117                 drm_mode_probed_add(connector, stdmode);
118                 drm_mode_list_concat(&connector->probed_modes,
119                                      &connector->modes);
120
121                 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
122                           drm_get_connector_name(connector));
123         }
124
125         drm_mode_sort(&connector->modes);
126
127         DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
128         list_for_each_entry_safe(mode, t, &connector->modes, head) {
129                 mode->vrefresh = drm_mode_vrefresh(mode);
130
131                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
132                 drm_mode_debug_printmodeline(mode);
133         }
134 }
135 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
136
137 void drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
138                                       uint32_t maxY)
139 {
140         struct drm_connector *connector;
141
142         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
143                 drm_helper_probe_single_connector_modes(connector, maxX, maxY);
144         }
145 }
146 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
147
148
149 /**
150  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
151  * @crtc: CRTC to check
152  *
153  * LOCKING:
154  * Caller must hold mode config lock.
155  *
156  * Walk @crtc's DRM device's mode_config and see if it's in use.
157  *
158  * RETURNS:
159  * True if @crtc is part of the mode_config, false otherwise.
160  */
161 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
162 {
163         struct drm_encoder *encoder;
164         struct drm_device *dev = crtc->dev;
165         /* FIXME: Locking around list access? */
166         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
167                 if (encoder->crtc == crtc)
168                         return true;
169         return false;
170 }
171 EXPORT_SYMBOL(drm_helper_crtc_in_use);
172
173 /**
174  * drm_disable_unused_functions - disable unused objects
175  * @dev: DRM device
176  *
177  * LOCKING:
178  * Caller must hold mode config lock.
179  *
180  * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
181  * by calling its dpms function, which should power it off.
182  */
183 void drm_helper_disable_unused_functions(struct drm_device *dev)
184 {
185         struct drm_encoder *encoder;
186         struct drm_encoder_helper_funcs *encoder_funcs;
187         struct drm_crtc *crtc;
188
189         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
190                 encoder_funcs = encoder->helper_private;
191                 if (!encoder->crtc)
192                         (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
193         }
194
195         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
196                 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
197                 crtc->enabled = drm_helper_crtc_in_use(crtc);
198                 if (!crtc->enabled) {
199                         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
200                         crtc->fb = NULL;
201                 }
202         }
203 }
204 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
205
206 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
207 {
208         struct drm_display_mode *mode;
209
210         list_for_each_entry(mode, &connector->modes, head) {
211                 if (drm_mode_width(mode) > width ||
212                     drm_mode_height(mode) > height)
213                         continue;
214                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
215                         return mode;
216         }
217         return NULL;
218 }
219
220 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
221 {
222         bool enable;
223
224         if (strict) {
225                 enable = connector->status == connector_status_connected;
226         } else {
227                 enable = connector->status != connector_status_disconnected;
228         }
229         return enable;
230 }
231
232 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
233 {
234         bool any_enabled = false;
235         struct drm_connector *connector;
236         int i = 0;
237
238         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
239                 enabled[i] = drm_connector_enabled(connector, true);
240                 any_enabled |= enabled[i];
241                 i++;
242         }
243
244         if (any_enabled)
245                 return;
246
247         i = 0;
248         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
249                 enabled[i] = drm_connector_enabled(connector, false);
250                 i++;
251         }
252 }
253
254 static bool drm_target_preferred(struct drm_device *dev,
255                                  struct drm_display_mode **modes,
256                                  bool *enabled, int width, int height)
257 {
258         struct drm_connector *connector;
259         int i = 0;
260
261         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
262
263                 if (enabled[i] == false) {
264                         i++;
265                         continue;
266                 }
267
268                 modes[i] = drm_has_preferred_mode(connector, width, height);
269                 if (!modes[i]) {
270                         list_for_each_entry(modes[i], &connector->modes, head)
271                                 break;
272                 }
273                 i++;
274         }
275         return true;
276 }
277
278 static int drm_pick_crtcs(struct drm_device *dev,
279                           struct drm_crtc **best_crtcs,
280                           struct drm_display_mode **modes,
281                           int n, int width, int height)
282 {
283         int c, o;
284         struct drm_connector *connector;
285         struct drm_connector_helper_funcs *connector_funcs;
286         struct drm_encoder *encoder;
287         struct drm_crtc *best_crtc;
288         int my_score, best_score, score;
289         struct drm_crtc **crtcs, *crtc;
290
291         if (n == dev->mode_config.num_connector)
292                 return 0;
293         c = 0;
294         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
295                 if (c == n)
296                         break;
297                 c++;
298         }
299
300         best_crtcs[n] = NULL;
301         best_crtc = NULL;
302         best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
303         if (modes[n] == NULL)
304                 return best_score;
305
306         crtcs = kmalloc(dev->mode_config.num_connector *
307                         sizeof(struct drm_crtc *), GFP_KERNEL);
308         if (!crtcs)
309                 return best_score;
310
311         my_score = 1;
312         if (connector->status == connector_status_connected)
313                 my_score++;
314         if (drm_has_preferred_mode(connector, width, height))
315                 my_score++;
316
317         connector_funcs = connector->helper_private;
318         encoder = connector_funcs->best_encoder(connector);
319         if (!encoder)
320                 goto out;
321
322         connector->encoder = encoder;
323
324         /* select a crtc for this connector and then attempt to configure
325            remaining connectors */
326         c = 0;
327         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
328
329                 if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
330                         c++;
331                         continue;
332                 }
333
334                 for (o = 0; o < n; o++)
335                         if (best_crtcs[o] == crtc)
336                                 break;
337
338                 if (o < n) {
339                         /* ignore cloning for now */
340                         c++;
341                         continue;
342                 }
343
344                 crtcs[n] = crtc;
345                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
346                 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
347                                                   width, height);
348                 if (score > best_score) {
349                         best_crtc = crtc;
350                         best_score = score;
351                         memcpy(best_crtcs, crtcs,
352                                dev->mode_config.num_connector *
353                                sizeof(struct drm_crtc *));
354                 }
355                 c++;
356         }
357 out:
358         kfree(crtcs);
359         return best_score;
360 }
361
362 static void drm_setup_crtcs(struct drm_device *dev)
363 {
364         struct drm_crtc **crtcs;
365         struct drm_display_mode **modes;
366         struct drm_encoder *encoder;
367         struct drm_connector *connector;
368         bool *enabled;
369         int width, height;
370         int i, ret;
371
372         width = dev->mode_config.max_width;
373         height = dev->mode_config.max_height;
374
375         /* clean out all the encoder/crtc combos */
376         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
377                 encoder->crtc = NULL;
378         }
379
380         crtcs = kcalloc(dev->mode_config.num_connector,
381                         sizeof(struct drm_crtc *), GFP_KERNEL);
382         modes = kcalloc(dev->mode_config.num_connector,
383                         sizeof(struct drm_display_mode *), GFP_KERNEL);
384         enabled = kcalloc(dev->mode_config.num_connector,
385                           sizeof(bool), GFP_KERNEL);
386
387         drm_enable_connectors(dev, enabled);
388
389         ret = drm_target_preferred(dev, modes, enabled, width, height);
390         if (!ret)
391                 DRM_ERROR("Unable to find initial modes\n");
392
393         drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
394
395         i = 0;
396         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397                 struct drm_display_mode *mode = modes[i];
398                 struct drm_crtc *crtc = crtcs[i];
399
400                 if (connector->encoder == NULL) {
401                         i++;
402                         continue;
403                 }
404
405                 if (mode && crtc) {
406                         crtc->desired_mode = mode;
407                         connector->encoder->crtc = crtc;
408                 } else
409                         connector->encoder->crtc = NULL;
410                 i++;
411         }
412
413         kfree(crtcs);
414         kfree(modes);
415         kfree(enabled);
416 }
417 /**
418  * drm_crtc_set_mode - set a mode
419  * @crtc: CRTC to program
420  * @mode: mode to use
421  * @x: width of mode
422  * @y: height of mode
423  *
424  * LOCKING:
425  * Caller must hold mode config lock.
426  *
427  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
428  * to fixup or reject the mode prior to trying to set it.
429  *
430  * RETURNS:
431  * True if the mode was set successfully, or false otherwise.
432  */
433 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
434                               struct drm_display_mode *mode,
435                               int x, int y,
436                               struct drm_framebuffer *old_fb)
437 {
438         struct drm_device *dev = crtc->dev;
439         struct drm_display_mode *adjusted_mode, saved_mode;
440         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
441         struct drm_encoder_helper_funcs *encoder_funcs;
442         int saved_x, saved_y;
443         struct drm_encoder *encoder;
444         bool ret = true;
445
446         adjusted_mode = drm_mode_duplicate(dev, mode);
447
448         crtc->enabled = drm_helper_crtc_in_use(crtc);
449
450         if (!crtc->enabled)
451                 return true;
452
453         saved_mode = crtc->mode;
454         saved_x = crtc->x;
455         saved_y = crtc->y;
456
457         /* Update crtc values up front so the driver can rely on them for mode
458          * setting.
459          */
460         crtc->mode = *mode;
461         crtc->x = x;
462         crtc->y = y;
463
464         if (drm_mode_equal(&saved_mode, &crtc->mode)) {
465                 if (saved_x != crtc->x || saved_y != crtc->y) {
466                         crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
467                                                   old_fb);
468                         goto done;
469                 }
470         }
471
472         /* Pass our mode to the connectors and the CRTC to give them a chance to
473          * adjust it according to limitations or connector properties, and also
474          * a chance to reject the mode entirely.
475          */
476         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
477
478                 if (encoder->crtc != crtc)
479                         continue;
480                 encoder_funcs = encoder->helper_private;
481                 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
482                                                       adjusted_mode))) {
483                         goto done;
484                 }
485         }
486
487         if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
488                 goto done;
489         }
490
491         /* Prepare the encoders and CRTCs before setting the mode. */
492         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
493
494                 if (encoder->crtc != crtc)
495                         continue;
496                 encoder_funcs = encoder->helper_private;
497                 /* Disable the encoders as the first thing we do. */
498                 encoder_funcs->prepare(encoder);
499         }
500
501         crtc_funcs->prepare(crtc);
502
503         /* Set up the DPLL and any encoders state that needs to adjust or depend
504          * on the DPLL.
505          */
506         crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
507
508         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
509
510                 if (encoder->crtc != crtc)
511                         continue;
512
513                 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
514                          mode->name, mode->base.id);
515                 encoder_funcs = encoder->helper_private;
516                 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
517         }
518
519         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
520         crtc_funcs->commit(crtc);
521
522         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
523
524                 if (encoder->crtc != crtc)
525                         continue;
526
527                 encoder_funcs = encoder->helper_private;
528                 encoder_funcs->commit(encoder);
529
530         }
531
532         /* XXX free adjustedmode */
533         drm_mode_destroy(dev, adjusted_mode);
534         /* FIXME: add subpixel order */
535 done:
536         if (!ret) {
537                 crtc->mode = saved_mode;
538                 crtc->x = saved_x;
539                 crtc->y = saved_y;
540         }
541
542         return ret;
543 }
544 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
545
546
547 /**
548  * drm_crtc_helper_set_config - set a new config from userspace
549  * @crtc: CRTC to setup
550  * @crtc_info: user provided configuration
551  * @new_mode: new mode to set
552  * @connector_set: set of connectors for the new config
553  * @fb: new framebuffer
554  *
555  * LOCKING:
556  * Caller must hold mode config lock.
557  *
558  * Setup a new configuration, provided by the user in @crtc_info, and enable
559  * it.
560  *
561  * RETURNS:
562  * Zero. (FIXME)
563  */
564 int drm_crtc_helper_set_config(struct drm_mode_set *set)
565 {
566         struct drm_device *dev;
567         struct drm_crtc **save_crtcs, *new_crtc;
568         struct drm_encoder **save_encoders, *new_encoder;
569         struct drm_framebuffer *old_fb;
570         bool save_enabled;
571         bool changed = false;
572         bool flip_or_move = false;
573         struct drm_connector *connector;
574         int count = 0, ro, fail = 0;
575         struct drm_crtc_helper_funcs *crtc_funcs;
576         int ret = 0;
577
578         DRM_DEBUG("\n");
579
580         if (!set)
581                 return -EINVAL;
582
583         if (!set->crtc)
584                 return -EINVAL;
585
586         if (!set->crtc->helper_private)
587                 return -EINVAL;
588
589         crtc_funcs = set->crtc->helper_private;
590
591         DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
592                   set->crtc, set->crtc->base.id, set->fb, set->connectors,
593                   (int)set->num_connectors, set->x, set->y);
594
595         dev = set->crtc->dev;
596
597         /* save previous config */
598         save_enabled = set->crtc->enabled;
599
600         /* this is meant to be num_connector not num_crtc */
601         save_crtcs = kzalloc(dev->mode_config.num_connector *
602                              sizeof(struct drm_crtc *), GFP_KERNEL);
603         if (!save_crtcs)
604                 return -ENOMEM;
605
606         save_encoders = kzalloc(dev->mode_config.num_connector *
607                                 sizeof(struct drm_encoders *), GFP_KERNEL);
608         if (!save_encoders) {
609                 kfree(save_crtcs);
610                 return -ENOMEM;
611         }
612
613         /* We should be able to check here if the fb has the same properties
614          * and then just flip_or_move it */
615         if (set->crtc->fb != set->fb) {
616                 /* if we have no fb then its a change not a flip */
617                 if (set->crtc->fb == NULL)
618                         changed = true;
619                 else
620                         flip_or_move = true;
621         }
622
623         if (set->x != set->crtc->x || set->y != set->crtc->y)
624                 flip_or_move = true;
625
626         if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
627                 DRM_DEBUG("modes are different\n");
628                 drm_mode_debug_printmodeline(&set->crtc->mode);
629                 drm_mode_debug_printmodeline(set->mode);
630                 changed = true;
631         }
632
633         /* a) traverse passed in connector list and get encoders for them */
634         count = 0;
635         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
636                 struct drm_connector_helper_funcs *connector_funcs =
637                         connector->helper_private;
638                 save_encoders[count++] = connector->encoder;
639                 new_encoder = connector->encoder;
640                 for (ro = 0; ro < set->num_connectors; ro++) {
641                         if (set->connectors[ro] == connector) {
642                                 new_encoder = connector_funcs->best_encoder(connector);
643                                 /* if we can't get an encoder for a connector
644                                    we are setting now - then fail */
645                                 if (new_encoder == NULL)
646                                         /* don't break so fail path works correct */
647                                         fail = 1;
648                                 break;
649                         }
650                 }
651
652                 if (new_encoder != connector->encoder) {
653                         changed = true;
654                         connector->encoder = new_encoder;
655                 }
656         }
657
658         if (fail) {
659                 ret = -EINVAL;
660                 goto fail_no_encoder;
661         }
662
663         count = 0;
664         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
665                 if (!connector->encoder)
666                         continue;
667
668                 save_crtcs[count++] = connector->encoder->crtc;
669
670                 if (connector->encoder->crtc == set->crtc)
671                         new_crtc = NULL;
672                 else
673                         new_crtc = connector->encoder->crtc;
674
675                 for (ro = 0; ro < set->num_connectors; ro++) {
676                         if (set->connectors[ro] == connector)
677                                 new_crtc = set->crtc;
678                 }
679                 if (new_crtc != connector->encoder->crtc) {
680                         changed = true;
681                         connector->encoder->crtc = new_crtc;
682                 }
683         }
684
685         /* mode_set_base is not a required function */
686         if (flip_or_move && !crtc_funcs->mode_set_base)
687                 changed = true;
688
689         if (changed) {
690                 old_fb = set->crtc->fb;
691                 set->crtc->fb = set->fb;
692                 set->crtc->enabled = (set->mode != NULL);
693                 if (set->mode != NULL) {
694                         DRM_DEBUG("attempting to set mode from userspace\n");
695                         drm_mode_debug_printmodeline(set->mode);
696                         if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
697                                                       set->x, set->y,
698                                                       old_fb)) {
699                                 ret = -EINVAL;
700                                 goto fail_set_mode;
701                         }
702                         /* TODO are these needed? */
703                         set->crtc->desired_x = set->x;
704                         set->crtc->desired_y = set->y;
705                         set->crtc->desired_mode = set->mode;
706                 }
707                 drm_helper_disable_unused_functions(dev);
708         } else if (flip_or_move) {
709                 old_fb = set->crtc->fb;
710                 if (set->crtc->fb != set->fb)
711                         set->crtc->fb = set->fb;
712                 crtc_funcs->mode_set_base(set->crtc, set->x, set->y, old_fb);
713         }
714
715         kfree(save_encoders);
716         kfree(save_crtcs);
717         return 0;
718
719 fail_set_mode:
720         set->crtc->enabled = save_enabled;
721         count = 0;
722         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
723                 connector->encoder->crtc = save_crtcs[count++];
724 fail_no_encoder:
725         kfree(save_crtcs);
726         count = 0;
727         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
728                 connector->encoder = save_encoders[count++];
729         }
730         kfree(save_encoders);
731         return ret;
732 }
733 EXPORT_SYMBOL(drm_crtc_helper_set_config);
734
735 bool drm_helper_plugged_event(struct drm_device *dev)
736 {
737         DRM_DEBUG("\n");
738
739         drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
740                                          dev->mode_config.max_height);
741
742         drm_setup_crtcs(dev);
743
744         /* alert the driver fb layer */
745         dev->mode_config.funcs->fb_changed(dev);
746
747         /* FIXME: send hotplug event */
748         return true;
749 }
750 /**
751  * drm_initial_config - setup a sane initial connector configuration
752  * @dev: DRM device
753  * @can_grow: this configuration is growable
754  *
755  * LOCKING:
756  * Called at init time, must take mode config lock.
757  *
758  * Scan the CRTCs and connectors and try to put together an initial setup.
759  * At the moment, this is a cloned configuration across all heads with
760  * a new framebuffer object as the backing store.
761  *
762  * RETURNS:
763  * Zero if everything went ok, nonzero otherwise.
764  */
765 bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
766 {
767         int ret = false;
768
769         drm_helper_plugged_event(dev);
770         return ret;
771 }
772 EXPORT_SYMBOL(drm_helper_initial_config);
773
774 /**
775  * drm_hotplug_stage_two
776  * @dev DRM device
777  * @connector hotpluged connector
778  *
779  * LOCKING.
780  * Caller must hold mode config lock, function might grab struct lock.
781  *
782  * Stage two of a hotplug.
783  *
784  * RETURNS:
785  * Zero on success, errno on failure.
786  */
787 int drm_helper_hotplug_stage_two(struct drm_device *dev)
788 {
789         drm_helper_plugged_event(dev);
790
791         return 0;
792 }
793 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
794
795 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
796                                    struct drm_mode_fb_cmd *mode_cmd)
797 {
798         fb->width = mode_cmd->width;
799         fb->height = mode_cmd->height;
800         fb->pitch = mode_cmd->pitch;
801         fb->bits_per_pixel = mode_cmd->bpp;
802         fb->depth = mode_cmd->depth;
803
804         return 0;
805 }
806 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
807
808 int drm_helper_resume_force_mode(struct drm_device *dev)
809 {
810         struct drm_crtc *crtc;
811         int ret;
812
813         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
814
815                 if (!crtc->enabled)
816                         continue;
817
818                 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
819                                                crtc->x, crtc->y, crtc->fb);
820
821                 if (ret == false)
822                         DRM_ERROR("failed to set mode on crtc %p\n", crtc);
823         }
824         return 0;
825 }
826 EXPORT_SYMBOL(drm_helper_resume_force_mode);