drm: Reject page_flip for !DRIVER_MODESET
[pandora-kernel.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
33 #include <linux/fb.h>
34 #include <linux/module.h>
35 #include "drmP.h"
36 #include "drm_crtc.h"
37 #include "drm_fb_helper.h"
38 #include "drm_crtc_helper.h"
39
40 MODULE_AUTHOR("David Airlie, Jesse Barnes");
41 MODULE_DESCRIPTION("DRM KMS helper");
42 MODULE_LICENSE("GPL and additional rights");
43
44 static LIST_HEAD(kernel_fb_helper_list);
45
46 /* simple single crtc case helper function */
47 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
48 {
49         struct drm_device *dev = fb_helper->dev;
50         struct drm_connector *connector;
51         int i;
52
53         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
54                 struct drm_fb_helper_connector *fb_helper_connector;
55
56                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
57                 if (!fb_helper_connector)
58                         goto fail;
59
60                 fb_helper_connector->connector = connector;
61                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
62         }
63         return 0;
64 fail:
65         for (i = 0; i < fb_helper->connector_count; i++) {
66                 kfree(fb_helper->connector_info[i]);
67                 fb_helper->connector_info[i] = NULL;
68         }
69         fb_helper->connector_count = 0;
70         return -ENOMEM;
71 }
72 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
73
74 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
75 {
76         struct drm_fb_helper_connector *fb_helper_conn;
77         int i;
78
79         for (i = 0; i < fb_helper->connector_count; i++) {
80                 struct drm_cmdline_mode *mode;
81                 struct drm_connector *connector;
82                 char *option = NULL;
83
84                 fb_helper_conn = fb_helper->connector_info[i];
85                 connector = fb_helper_conn->connector;
86                 mode = &fb_helper_conn->cmdline_mode;
87
88                 /* do something on return - turn off connector maybe */
89                 if (fb_get_options(drm_get_connector_name(connector), &option))
90                         continue;
91
92                 if (drm_mode_parse_command_line_for_connector(option,
93                                                               connector,
94                                                               mode)) {
95                         if (mode->force) {
96                                 const char *s;
97                                 switch (mode->force) {
98                                 case DRM_FORCE_OFF: s = "OFF"; break;
99                                 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
100                                 default:
101                                 case DRM_FORCE_ON: s = "ON"; break;
102                                 }
103
104                                 DRM_INFO("forcing %s connector %s\n",
105                                          drm_get_connector_name(connector), s);
106                                 connector->force = mode->force;
107                         }
108
109                         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
110                                       drm_get_connector_name(connector),
111                                       mode->xres, mode->yres,
112                                       mode->refresh_specified ? mode->refresh : 60,
113                                       mode->rb ? " reduced blanking" : "",
114                                       mode->margins ? " with margins" : "",
115                                       mode->interlace ?  " interlaced" : "");
116                 }
117
118         }
119         return 0;
120 }
121
122 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
123 {
124         uint16_t *r_base, *g_base, *b_base;
125         int i;
126
127         r_base = crtc->gamma_store;
128         g_base = r_base + crtc->gamma_size;
129         b_base = g_base + crtc->gamma_size;
130
131         for (i = 0; i < crtc->gamma_size; i++)
132                 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
133 }
134
135 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
136 {
137         uint16_t *r_base, *g_base, *b_base;
138
139         r_base = crtc->gamma_store;
140         g_base = r_base + crtc->gamma_size;
141         b_base = g_base + crtc->gamma_size;
142
143         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
144 }
145
146 int drm_fb_helper_debug_enter(struct fb_info *info)
147 {
148         struct drm_fb_helper *helper = info->par;
149         struct drm_crtc_helper_funcs *funcs;
150         int i;
151
152         if (list_empty(&kernel_fb_helper_list))
153                 return false;
154
155         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
156                 for (i = 0; i < helper->crtc_count; i++) {
157                         struct drm_mode_set *mode_set =
158                                 &helper->crtc_info[i].mode_set;
159
160                         if (!mode_set->crtc->enabled)
161                                 continue;
162
163                         funcs = mode_set->crtc->helper_private;
164                         drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
165                         funcs->mode_set_base_atomic(mode_set->crtc,
166                                                     mode_set->fb,
167                                                     mode_set->x,
168                                                     mode_set->y,
169                                                     ENTER_ATOMIC_MODE_SET);
170                 }
171         }
172
173         return 0;
174 }
175 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
176
177 /* Find the real fb for a given fb helper CRTC */
178 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
179 {
180         struct drm_device *dev = crtc->dev;
181         struct drm_crtc *c;
182
183         list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
184                 if (crtc->base.id == c->base.id)
185                         return c->fb;
186         }
187
188         return NULL;
189 }
190
191 int drm_fb_helper_debug_leave(struct fb_info *info)
192 {
193         struct drm_fb_helper *helper = info->par;
194         struct drm_crtc *crtc;
195         struct drm_crtc_helper_funcs *funcs;
196         struct drm_framebuffer *fb;
197         int i;
198
199         for (i = 0; i < helper->crtc_count; i++) {
200                 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
201                 crtc = mode_set->crtc;
202                 funcs = crtc->helper_private;
203                 fb = drm_mode_config_fb(crtc);
204
205                 if (!crtc->enabled)
206                         continue;
207
208                 if (!fb) {
209                         DRM_ERROR("no fb to restore??\n");
210                         continue;
211                 }
212
213                 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
214                 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
215                                             crtc->y, LEAVE_ATOMIC_MODE_SET);
216         }
217
218         return 0;
219 }
220 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
221
222 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
223 {
224         bool error = false;
225         int i, ret;
226         for (i = 0; i < fb_helper->crtc_count; i++) {
227                 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
228                 ret = drm_crtc_helper_set_config(mode_set);
229                 if (ret)
230                         error = true;
231         }
232         return error;
233 }
234 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
235
236 bool drm_fb_helper_force_kernel_mode(void)
237 {
238         bool ret, error = false;
239         struct drm_fb_helper *helper;
240
241         if (list_empty(&kernel_fb_helper_list))
242                 return false;
243
244         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
245                 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
246                         continue;
247
248                 ret = drm_fb_helper_restore_fbdev_mode(helper);
249                 if (ret)
250                         error = true;
251         }
252         return error;
253 }
254
255 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
256                         void *panic_str)
257 {
258         printk(KERN_ERR "panic occurred, switching back to text console\n");
259         return drm_fb_helper_force_kernel_mode();
260 }
261 EXPORT_SYMBOL(drm_fb_helper_panic);
262
263 static struct notifier_block paniced = {
264         .notifier_call = drm_fb_helper_panic,
265 };
266
267 /**
268  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
269  *
270  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
271  */
272 void drm_fb_helper_restore(void)
273 {
274         bool ret;
275         ret = drm_fb_helper_force_kernel_mode();
276         if (ret == true)
277                 DRM_ERROR("Failed to restore crtc configuration\n");
278 }
279 EXPORT_SYMBOL(drm_fb_helper_restore);
280
281 #ifdef CONFIG_MAGIC_SYSRQ
282 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
283 {
284         drm_fb_helper_restore();
285 }
286 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
287
288 static void drm_fb_helper_sysrq(int dummy1)
289 {
290         schedule_work(&drm_fb_helper_restore_work);
291 }
292
293 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
294         .handler = drm_fb_helper_sysrq,
295         .help_msg = "force-fb(V)",
296         .action_msg = "Restore framebuffer console",
297 };
298 #else
299 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
300 #endif
301
302 static void drm_fb_helper_on(struct fb_info *info)
303 {
304         struct drm_fb_helper *fb_helper = info->par;
305         struct drm_device *dev = fb_helper->dev;
306         struct drm_crtc *crtc;
307         struct drm_crtc_helper_funcs *crtc_funcs;
308         struct drm_connector *connector;
309         struct drm_encoder *encoder;
310         int i, j;
311
312         /*
313          * For each CRTC in this fb, turn the crtc on then,
314          * find all associated encoders and turn them on.
315          */
316         mutex_lock(&dev->mode_config.mutex);
317         for (i = 0; i < fb_helper->crtc_count; i++) {
318                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
319                 crtc_funcs = crtc->helper_private;
320
321                 if (!crtc->enabled)
322                         continue;
323
324                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
325
326                 /* Walk the connectors & encoders on this fb turning them on */
327                 for (j = 0; j < fb_helper->connector_count; j++) {
328                         connector = fb_helper->connector_info[j]->connector;
329                         connector->dpms = DRM_MODE_DPMS_ON;
330                         drm_connector_property_set_value(connector,
331                                                          dev->mode_config.dpms_property,
332                                                          DRM_MODE_DPMS_ON);
333                 }
334                 /* Found a CRTC on this fb, now find encoders */
335                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
336                         if (encoder->crtc == crtc) {
337                                 struct drm_encoder_helper_funcs *encoder_funcs;
338
339                                 encoder_funcs = encoder->helper_private;
340                                 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
341                         }
342                 }
343         }
344         mutex_unlock(&dev->mode_config.mutex);
345 }
346
347 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
348 {
349         struct drm_fb_helper *fb_helper = info->par;
350         struct drm_device *dev = fb_helper->dev;
351         struct drm_crtc *crtc;
352         struct drm_crtc_helper_funcs *crtc_funcs;
353         struct drm_connector *connector;
354         struct drm_encoder *encoder;
355         int i, j;
356
357         /*
358          * For each CRTC in this fb, find all associated encoders
359          * and turn them off, then turn off the CRTC.
360          */
361         mutex_lock(&dev->mode_config.mutex);
362         for (i = 0; i < fb_helper->crtc_count; i++) {
363                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
364                 crtc_funcs = crtc->helper_private;
365
366                 if (!crtc->enabled)
367                         continue;
368
369                 /* Walk the connectors on this fb and mark them off */
370                 for (j = 0; j < fb_helper->connector_count; j++) {
371                         connector = fb_helper->connector_info[j]->connector;
372                         connector->dpms = dpms_mode;
373                         drm_connector_property_set_value(connector,
374                                                          dev->mode_config.dpms_property,
375                                                          dpms_mode);
376                 }
377                 /* Found a CRTC on this fb, now find encoders */
378                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
379                         if (encoder->crtc == crtc) {
380                                 struct drm_encoder_helper_funcs *encoder_funcs;
381
382                                 encoder_funcs = encoder->helper_private;
383                                 encoder_funcs->dpms(encoder, dpms_mode);
384                         }
385                 }
386                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
387         }
388         mutex_unlock(&dev->mode_config.mutex);
389 }
390
391 int drm_fb_helper_blank(int blank, struct fb_info *info)
392 {
393         switch (blank) {
394         /* Display: On; HSync: On, VSync: On */
395         case FB_BLANK_UNBLANK:
396                 drm_fb_helper_on(info);
397                 break;
398         /* Display: Off; HSync: On, VSync: On */
399         case FB_BLANK_NORMAL:
400                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
401                 break;
402         /* Display: Off; HSync: Off, VSync: On */
403         case FB_BLANK_HSYNC_SUSPEND:
404                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
405                 break;
406         /* Display: Off; HSync: On, VSync: Off */
407         case FB_BLANK_VSYNC_SUSPEND:
408                 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
409                 break;
410         /* Display: Off; HSync: Off, VSync: Off */
411         case FB_BLANK_POWERDOWN:
412                 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
413                 break;
414         }
415         return 0;
416 }
417 EXPORT_SYMBOL(drm_fb_helper_blank);
418
419 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
420 {
421         int i;
422
423         for (i = 0; i < helper->connector_count; i++)
424                 kfree(helper->connector_info[i]);
425         kfree(helper->connector_info);
426         for (i = 0; i < helper->crtc_count; i++)
427                 kfree(helper->crtc_info[i].mode_set.connectors);
428         kfree(helper->crtc_info);
429 }
430
431 int drm_fb_helper_init(struct drm_device *dev,
432                        struct drm_fb_helper *fb_helper,
433                        int crtc_count, int max_conn_count)
434 {
435         struct drm_crtc *crtc;
436         int ret = 0;
437         int i;
438
439         fb_helper->dev = dev;
440
441         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
442
443         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
444         if (!fb_helper->crtc_info)
445                 return -ENOMEM;
446
447         fb_helper->crtc_count = crtc_count;
448         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
449         if (!fb_helper->connector_info) {
450                 kfree(fb_helper->crtc_info);
451                 return -ENOMEM;
452         }
453         fb_helper->connector_count = 0;
454
455         for (i = 0; i < crtc_count; i++) {
456                 fb_helper->crtc_info[i].mode_set.connectors =
457                         kcalloc(max_conn_count,
458                                 sizeof(struct drm_connector *),
459                                 GFP_KERNEL);
460
461                 if (!fb_helper->crtc_info[i].mode_set.connectors) {
462                         ret = -ENOMEM;
463                         goto out_free;
464                 }
465                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
466         }
467
468         i = 0;
469         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
470                 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
471                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
472                 i++;
473         }
474         fb_helper->conn_limit = max_conn_count;
475         return 0;
476 out_free:
477         drm_fb_helper_crtc_free(fb_helper);
478         return -ENOMEM;
479 }
480 EXPORT_SYMBOL(drm_fb_helper_init);
481
482 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
483 {
484         if (!list_empty(&fb_helper->kernel_fb_list)) {
485                 list_del(&fb_helper->kernel_fb_list);
486                 if (list_empty(&kernel_fb_helper_list)) {
487                         printk(KERN_INFO "drm: unregistered panic notifier\n");
488                         atomic_notifier_chain_unregister(&panic_notifier_list,
489                                                          &paniced);
490                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
491                 }
492         }
493
494         drm_fb_helper_crtc_free(fb_helper);
495
496 }
497 EXPORT_SYMBOL(drm_fb_helper_fini);
498
499 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
500                      u16 blue, u16 regno, struct fb_info *info)
501 {
502         struct drm_fb_helper *fb_helper = info->par;
503         struct drm_framebuffer *fb = fb_helper->fb;
504         int pindex;
505
506         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
507                 u32 *palette;
508                 u32 value;
509                 /* place color in psuedopalette */
510                 if (regno > 16)
511                         return -EINVAL;
512                 palette = (u32 *)info->pseudo_palette;
513                 red >>= (16 - info->var.red.length);
514                 green >>= (16 - info->var.green.length);
515                 blue >>= (16 - info->var.blue.length);
516                 value = (red << info->var.red.offset) |
517                         (green << info->var.green.offset) |
518                         (blue << info->var.blue.offset);
519                 if (info->var.transp.length > 0) {
520                         u32 mask = (1 << info->var.transp.length) - 1;
521                         mask <<= info->var.transp.offset;
522                         value |= mask;
523                 }
524                 palette[regno] = value;
525                 return 0;
526         }
527
528         pindex = regno;
529
530         if (fb->bits_per_pixel == 16) {
531                 pindex = regno << 3;
532
533                 if (fb->depth == 16 && regno > 63)
534                         return -EINVAL;
535                 if (fb->depth == 15 && regno > 31)
536                         return -EINVAL;
537
538                 if (fb->depth == 16) {
539                         u16 r, g, b;
540                         int i;
541                         if (regno < 32) {
542                                 for (i = 0; i < 8; i++)
543                                         fb_helper->funcs->gamma_set(crtc, red,
544                                                 green, blue, pindex + i);
545                         }
546
547                         fb_helper->funcs->gamma_get(crtc, &r,
548                                                     &g, &b,
549                                                     pindex >> 1);
550
551                         for (i = 0; i < 4; i++)
552                                 fb_helper->funcs->gamma_set(crtc, r,
553                                                             green, b,
554                                                             (pindex >> 1) + i);
555                 }
556         }
557
558         if (fb->depth != 16)
559                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
560         return 0;
561 }
562
563 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
564 {
565         struct drm_fb_helper *fb_helper = info->par;
566         struct drm_crtc_helper_funcs *crtc_funcs;
567         u16 *red, *green, *blue, *transp;
568         struct drm_crtc *crtc;
569         int i, j, rc = 0;
570         int start;
571
572         for (i = 0; i < fb_helper->crtc_count; i++) {
573                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
574                 crtc_funcs = crtc->helper_private;
575
576                 red = cmap->red;
577                 green = cmap->green;
578                 blue = cmap->blue;
579                 transp = cmap->transp;
580                 start = cmap->start;
581
582                 for (j = 0; j < cmap->len; j++) {
583                         u16 hred, hgreen, hblue, htransp = 0xffff;
584
585                         hred = *red++;
586                         hgreen = *green++;
587                         hblue = *blue++;
588
589                         if (transp)
590                                 htransp = *transp++;
591
592                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
593                         if (rc)
594                                 return rc;
595                 }
596                 crtc_funcs->load_lut(crtc);
597         }
598         return rc;
599 }
600 EXPORT_SYMBOL(drm_fb_helper_setcmap);
601
602 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
603                             struct fb_info *info)
604 {
605         struct drm_fb_helper *fb_helper = info->par;
606         struct drm_framebuffer *fb = fb_helper->fb;
607         int depth;
608
609         if (var->pixclock != 0 || in_dbg_master())
610                 return -EINVAL;
611
612         /* Need to resize the fb object !!! */
613         if (var->bits_per_pixel > fb->bits_per_pixel ||
614             var->xres > fb->width || var->yres > fb->height ||
615             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
616                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
617                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
618                           var->xres, var->yres, var->bits_per_pixel,
619                           var->xres_virtual, var->yres_virtual,
620                           fb->width, fb->height, fb->bits_per_pixel);
621                 return -EINVAL;
622         }
623
624         switch (var->bits_per_pixel) {
625         case 16:
626                 depth = (var->green.length == 6) ? 16 : 15;
627                 break;
628         case 32:
629                 depth = (var->transp.length > 0) ? 32 : 24;
630                 break;
631         default:
632                 depth = var->bits_per_pixel;
633                 break;
634         }
635
636         switch (depth) {
637         case 8:
638                 var->red.offset = 0;
639                 var->green.offset = 0;
640                 var->blue.offset = 0;
641                 var->red.length = 8;
642                 var->green.length = 8;
643                 var->blue.length = 8;
644                 var->transp.length = 0;
645                 var->transp.offset = 0;
646                 break;
647         case 15:
648                 var->red.offset = 10;
649                 var->green.offset = 5;
650                 var->blue.offset = 0;
651                 var->red.length = 5;
652                 var->green.length = 5;
653                 var->blue.length = 5;
654                 var->transp.length = 1;
655                 var->transp.offset = 15;
656                 break;
657         case 16:
658                 var->red.offset = 11;
659                 var->green.offset = 5;
660                 var->blue.offset = 0;
661                 var->red.length = 5;
662                 var->green.length = 6;
663                 var->blue.length = 5;
664                 var->transp.length = 0;
665                 var->transp.offset = 0;
666                 break;
667         case 24:
668                 var->red.offset = 16;
669                 var->green.offset = 8;
670                 var->blue.offset = 0;
671                 var->red.length = 8;
672                 var->green.length = 8;
673                 var->blue.length = 8;
674                 var->transp.length = 0;
675                 var->transp.offset = 0;
676                 break;
677         case 32:
678                 var->red.offset = 16;
679                 var->green.offset = 8;
680                 var->blue.offset = 0;
681                 var->red.length = 8;
682                 var->green.length = 8;
683                 var->blue.length = 8;
684                 var->transp.length = 8;
685                 var->transp.offset = 24;
686                 break;
687         default:
688                 return -EINVAL;
689         }
690         return 0;
691 }
692 EXPORT_SYMBOL(drm_fb_helper_check_var);
693
694 /* this will let fbcon do the mode init */
695 int drm_fb_helper_set_par(struct fb_info *info)
696 {
697         struct drm_fb_helper *fb_helper = info->par;
698         struct drm_device *dev = fb_helper->dev;
699         struct fb_var_screeninfo *var = &info->var;
700         struct drm_crtc *crtc;
701         int ret;
702         int i;
703
704         if (var->pixclock != 0) {
705                 DRM_ERROR("PIXEL CLOCK SET\n");
706                 return -EINVAL;
707         }
708
709         mutex_lock(&dev->mode_config.mutex);
710         for (i = 0; i < fb_helper->crtc_count; i++) {
711                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
712                 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
713                 if (ret) {
714                         mutex_unlock(&dev->mode_config.mutex);
715                         return ret;
716                 }
717         }
718         mutex_unlock(&dev->mode_config.mutex);
719
720         if (fb_helper->delayed_hotplug) {
721                 fb_helper->delayed_hotplug = false;
722                 drm_fb_helper_hotplug_event(fb_helper);
723         }
724         return 0;
725 }
726 EXPORT_SYMBOL(drm_fb_helper_set_par);
727
728 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
729                               struct fb_info *info)
730 {
731         struct drm_fb_helper *fb_helper = info->par;
732         struct drm_device *dev = fb_helper->dev;
733         struct drm_mode_set *modeset;
734         struct drm_crtc *crtc;
735         int ret = 0;
736         int i;
737
738         mutex_lock(&dev->mode_config.mutex);
739         for (i = 0; i < fb_helper->crtc_count; i++) {
740                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
741
742                 modeset = &fb_helper->crtc_info[i].mode_set;
743
744                 modeset->x = var->xoffset;
745                 modeset->y = var->yoffset;
746
747                 if (modeset->num_connectors) {
748                         ret = crtc->funcs->set_config(modeset);
749                         if (!ret) {
750                                 info->var.xoffset = var->xoffset;
751                                 info->var.yoffset = var->yoffset;
752                         }
753                 }
754         }
755         mutex_unlock(&dev->mode_config.mutex);
756         return ret;
757 }
758 EXPORT_SYMBOL(drm_fb_helper_pan_display);
759
760 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
761                                   int preferred_bpp)
762 {
763         int new_fb = 0;
764         int crtc_count = 0;
765         int i;
766         struct fb_info *info;
767         struct drm_fb_helper_surface_size sizes;
768         int gamma_size = 0;
769
770         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
771         sizes.surface_depth = 24;
772         sizes.surface_bpp = 32;
773         sizes.fb_width = (unsigned)-1;
774         sizes.fb_height = (unsigned)-1;
775
776         /* if driver picks 8 or 16 by default use that
777            for both depth/bpp */
778         if (preferred_bpp != sizes.surface_bpp) {
779                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
780         }
781         /* first up get a count of crtcs now in use and new min/maxes width/heights */
782         for (i = 0; i < fb_helper->connector_count; i++) {
783                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
784                 struct drm_cmdline_mode *cmdline_mode;
785
786                 cmdline_mode = &fb_helper_conn->cmdline_mode;
787
788                 if (cmdline_mode->bpp_specified) {
789                         switch (cmdline_mode->bpp) {
790                         case 8:
791                                 sizes.surface_depth = sizes.surface_bpp = 8;
792                                 break;
793                         case 15:
794                                 sizes.surface_depth = 15;
795                                 sizes.surface_bpp = 16;
796                                 break;
797                         case 16:
798                                 sizes.surface_depth = sizes.surface_bpp = 16;
799                                 break;
800                         case 24:
801                                 sizes.surface_depth = sizes.surface_bpp = 24;
802                                 break;
803                         case 32:
804                                 sizes.surface_depth = 24;
805                                 sizes.surface_bpp = 32;
806                                 break;
807                         }
808                         break;
809                 }
810         }
811
812         crtc_count = 0;
813         for (i = 0; i < fb_helper->crtc_count; i++) {
814                 struct drm_display_mode *desired_mode;
815                 desired_mode = fb_helper->crtc_info[i].desired_mode;
816
817                 if (desired_mode) {
818                         if (gamma_size == 0)
819                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
820                         if (desired_mode->hdisplay < sizes.fb_width)
821                                 sizes.fb_width = desired_mode->hdisplay;
822                         if (desired_mode->vdisplay < sizes.fb_height)
823                                 sizes.fb_height = desired_mode->vdisplay;
824                         if (desired_mode->hdisplay > sizes.surface_width)
825                                 sizes.surface_width = desired_mode->hdisplay;
826                         if (desired_mode->vdisplay > sizes.surface_height)
827                                 sizes.surface_height = desired_mode->vdisplay;
828                         crtc_count++;
829                 }
830         }
831
832         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
833                 /* hmm everyone went away - assume VGA cable just fell out
834                    and will come back later. */
835                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
836                 sizes.fb_width = sizes.surface_width = 1024;
837                 sizes.fb_height = sizes.surface_height = 768;
838         }
839
840         /* push down into drivers */
841         new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
842         if (new_fb < 0)
843                 return new_fb;
844
845         info = fb_helper->fbdev;
846
847         /* set the fb pointer */
848         for (i = 0; i < fb_helper->crtc_count; i++) {
849                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
850         }
851
852         if (new_fb) {
853                 info->var.pixclock = 0;
854                 if (register_framebuffer(info) < 0) {
855                         return -EINVAL;
856                 }
857
858                 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
859                        info->fix.id);
860
861         } else {
862                 drm_fb_helper_set_par(info);
863         }
864
865         /* Switch back to kernel console on panic */
866         /* multi card linked list maybe */
867         if (list_empty(&kernel_fb_helper_list)) {
868                 printk(KERN_INFO "drm: registered panic notifier\n");
869                 atomic_notifier_chain_register(&panic_notifier_list,
870                                                &paniced);
871                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
872         }
873         if (new_fb)
874                 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
875
876         return 0;
877 }
878 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
879
880 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
881                             uint32_t depth)
882 {
883         info->fix.type = FB_TYPE_PACKED_PIXELS;
884         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
885                 FB_VISUAL_TRUECOLOR;
886         info->fix.mmio_start = 0;
887         info->fix.mmio_len = 0;
888         info->fix.type_aux = 0;
889         info->fix.xpanstep = 1; /* doing it in hw */
890         info->fix.ypanstep = 1; /* doing it in hw */
891         info->fix.ywrapstep = 0;
892         info->fix.accel = FB_ACCEL_NONE;
893         info->fix.type_aux = 0;
894
895         info->fix.line_length = pitch;
896         return;
897 }
898 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
899
900 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
901                             uint32_t fb_width, uint32_t fb_height)
902 {
903         struct drm_framebuffer *fb = fb_helper->fb;
904         info->pseudo_palette = fb_helper->pseudo_palette;
905         info->var.xres_virtual = fb->width;
906         info->var.yres_virtual = fb->height;
907         info->var.bits_per_pixel = fb->bits_per_pixel;
908         info->var.accel_flags = FB_ACCELF_TEXT;
909         info->var.xoffset = 0;
910         info->var.yoffset = 0;
911         info->var.activate = FB_ACTIVATE_NOW;
912         info->var.height = -1;
913         info->var.width = -1;
914
915         switch (fb->depth) {
916         case 8:
917                 info->var.red.offset = 0;
918                 info->var.green.offset = 0;
919                 info->var.blue.offset = 0;
920                 info->var.red.length = 8; /* 8bit DAC */
921                 info->var.green.length = 8;
922                 info->var.blue.length = 8;
923                 info->var.transp.offset = 0;
924                 info->var.transp.length = 0;
925                 break;
926         case 15:
927                 info->var.red.offset = 10;
928                 info->var.green.offset = 5;
929                 info->var.blue.offset = 0;
930                 info->var.red.length = 5;
931                 info->var.green.length = 5;
932                 info->var.blue.length = 5;
933                 info->var.transp.offset = 15;
934                 info->var.transp.length = 1;
935                 break;
936         case 16:
937                 info->var.red.offset = 11;
938                 info->var.green.offset = 5;
939                 info->var.blue.offset = 0;
940                 info->var.red.length = 5;
941                 info->var.green.length = 6;
942                 info->var.blue.length = 5;
943                 info->var.transp.offset = 0;
944                 break;
945         case 24:
946                 info->var.red.offset = 16;
947                 info->var.green.offset = 8;
948                 info->var.blue.offset = 0;
949                 info->var.red.length = 8;
950                 info->var.green.length = 8;
951                 info->var.blue.length = 8;
952                 info->var.transp.offset = 0;
953                 info->var.transp.length = 0;
954                 break;
955         case 32:
956                 info->var.red.offset = 16;
957                 info->var.green.offset = 8;
958                 info->var.blue.offset = 0;
959                 info->var.red.length = 8;
960                 info->var.green.length = 8;
961                 info->var.blue.length = 8;
962                 info->var.transp.offset = 24;
963                 info->var.transp.length = 8;
964                 break;
965         default:
966                 break;
967         }
968
969         info->var.xres = fb_width;
970         info->var.yres = fb_height;
971 }
972 EXPORT_SYMBOL(drm_fb_helper_fill_var);
973
974 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
975                                                uint32_t maxX,
976                                                uint32_t maxY)
977 {
978         struct drm_connector *connector;
979         int count = 0;
980         int i;
981
982         for (i = 0; i < fb_helper->connector_count; i++) {
983                 connector = fb_helper->connector_info[i]->connector;
984                 count += connector->funcs->fill_modes(connector, maxX, maxY);
985         }
986
987         return count;
988 }
989
990 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
991 {
992         struct drm_display_mode *mode;
993
994         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
995                 if (drm_mode_width(mode) > width ||
996                     drm_mode_height(mode) > height)
997                         continue;
998                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
999                         return mode;
1000         }
1001         return NULL;
1002 }
1003
1004 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1005 {
1006         struct drm_cmdline_mode *cmdline_mode;
1007         cmdline_mode = &fb_connector->cmdline_mode;
1008         return cmdline_mode->specified;
1009 }
1010
1011 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1012                                                       int width, int height)
1013 {
1014         struct drm_cmdline_mode *cmdline_mode;
1015         struct drm_display_mode *mode = NULL;
1016
1017         cmdline_mode = &fb_helper_conn->cmdline_mode;
1018         if (cmdline_mode->specified == false)
1019                 return mode;
1020
1021         /* attempt to find a matching mode in the list of modes
1022          *  we have gotten so far, if not add a CVT mode that conforms
1023          */
1024         if (cmdline_mode->rb || cmdline_mode->margins)
1025                 goto create_mode;
1026
1027         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1028                 /* check width/height */
1029                 if (mode->hdisplay != cmdline_mode->xres ||
1030                     mode->vdisplay != cmdline_mode->yres)
1031                         continue;
1032
1033                 if (cmdline_mode->refresh_specified) {
1034                         if (mode->vrefresh != cmdline_mode->refresh)
1035                                 continue;
1036                 }
1037
1038                 if (cmdline_mode->interlace) {
1039                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1040                                 continue;
1041                 }
1042                 return mode;
1043         }
1044
1045 create_mode:
1046         mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1047                                                  cmdline_mode);
1048         list_add(&mode->head, &fb_helper_conn->connector->modes);
1049         return mode;
1050 }
1051
1052 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1053 {
1054         bool enable;
1055
1056         if (strict) {
1057                 enable = connector->status == connector_status_connected;
1058         } else {
1059                 enable = connector->status != connector_status_disconnected;
1060         }
1061         return enable;
1062 }
1063
1064 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1065                                   bool *enabled)
1066 {
1067         bool any_enabled = false;
1068         struct drm_connector *connector;
1069         int i = 0;
1070
1071         for (i = 0; i < fb_helper->connector_count; i++) {
1072                 connector = fb_helper->connector_info[i]->connector;
1073                 enabled[i] = drm_connector_enabled(connector, true);
1074                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1075                           enabled[i] ? "yes" : "no");
1076                 any_enabled |= enabled[i];
1077         }
1078
1079         if (any_enabled)
1080                 return;
1081
1082         for (i = 0; i < fb_helper->connector_count; i++) {
1083                 connector = fb_helper->connector_info[i]->connector;
1084                 enabled[i] = drm_connector_enabled(connector, false);
1085         }
1086 }
1087
1088 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1089                               struct drm_display_mode **modes,
1090                               bool *enabled, int width, int height)
1091 {
1092         int count, i, j;
1093         bool can_clone = false;
1094         struct drm_fb_helper_connector *fb_helper_conn;
1095         struct drm_display_mode *dmt_mode, *mode;
1096
1097         /* only contemplate cloning in the single crtc case */
1098         if (fb_helper->crtc_count > 1)
1099                 return false;
1100
1101         count = 0;
1102         for (i = 0; i < fb_helper->connector_count; i++) {
1103                 if (enabled[i])
1104                         count++;
1105         }
1106
1107         /* only contemplate cloning if more than one connector is enabled */
1108         if (count <= 1)
1109                 return false;
1110
1111         /* check the command line or if nothing common pick 1024x768 */
1112         can_clone = true;
1113         for (i = 0; i < fb_helper->connector_count; i++) {
1114                 if (!enabled[i])
1115                         continue;
1116                 fb_helper_conn = fb_helper->connector_info[i];
1117                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1118                 if (!modes[i]) {
1119                         can_clone = false;
1120                         break;
1121                 }
1122                 for (j = 0; j < i; j++) {
1123                         if (!enabled[j])
1124                                 continue;
1125                         if (!drm_mode_equal(modes[j], modes[i]))
1126                                 can_clone = false;
1127                 }
1128         }
1129
1130         if (can_clone) {
1131                 DRM_DEBUG_KMS("can clone using command line\n");
1132                 return true;
1133         }
1134
1135         /* try and find a 1024x768 mode on each connector */
1136         can_clone = true;
1137         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1138
1139         for (i = 0; i < fb_helper->connector_count; i++) {
1140
1141                 if (!enabled[i])
1142                         continue;
1143
1144                 fb_helper_conn = fb_helper->connector_info[i];
1145                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1146                         if (drm_mode_equal(mode, dmt_mode))
1147                                 modes[i] = mode;
1148                 }
1149                 if (!modes[i])
1150                         can_clone = false;
1151         }
1152
1153         if (can_clone) {
1154                 DRM_DEBUG_KMS("can clone using 1024x768\n");
1155                 return true;
1156         }
1157         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1158         return false;
1159 }
1160
1161 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1162                                  struct drm_display_mode **modes,
1163                                  bool *enabled, int width, int height)
1164 {
1165         struct drm_fb_helper_connector *fb_helper_conn;
1166         int i;
1167
1168         for (i = 0; i < fb_helper->connector_count; i++) {
1169                 fb_helper_conn = fb_helper->connector_info[i];
1170
1171                 if (enabled[i] == false)
1172                         continue;
1173
1174                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1175                               fb_helper_conn->connector->base.id);
1176
1177                 /* got for command line mode first */
1178                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1179                 if (!modes[i]) {
1180                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1181                                       fb_helper_conn->connector->base.id);
1182                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1183                 }
1184                 /* No preferred modes, pick one off the list */
1185                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1186                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1187                                 break;
1188                 }
1189                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1190                           "none");
1191         }
1192         return true;
1193 }
1194
1195 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1196                           struct drm_fb_helper_crtc **best_crtcs,
1197                           struct drm_display_mode **modes,
1198                           int n, int width, int height)
1199 {
1200         int c, o;
1201         struct drm_device *dev = fb_helper->dev;
1202         struct drm_connector *connector;
1203         struct drm_connector_helper_funcs *connector_funcs;
1204         struct drm_encoder *encoder;
1205         struct drm_fb_helper_crtc *best_crtc;
1206         int my_score, best_score, score;
1207         struct drm_fb_helper_crtc **crtcs, *crtc;
1208         struct drm_fb_helper_connector *fb_helper_conn;
1209
1210         if (n == fb_helper->connector_count)
1211                 return 0;
1212
1213         fb_helper_conn = fb_helper->connector_info[n];
1214         connector = fb_helper_conn->connector;
1215
1216         best_crtcs[n] = NULL;
1217         best_crtc = NULL;
1218         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1219         if (modes[n] == NULL)
1220                 return best_score;
1221
1222         crtcs = kzalloc(dev->mode_config.num_connector *
1223                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1224         if (!crtcs)
1225                 return best_score;
1226
1227         my_score = 1;
1228         if (connector->status == connector_status_connected)
1229                 my_score++;
1230         if (drm_has_cmdline_mode(fb_helper_conn))
1231                 my_score++;
1232         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1233                 my_score++;
1234
1235         connector_funcs = connector->helper_private;
1236         encoder = connector_funcs->best_encoder(connector);
1237         if (!encoder)
1238                 goto out;
1239
1240         /* select a crtc for this connector and then attempt to configure
1241            remaining connectors */
1242         for (c = 0; c < fb_helper->crtc_count; c++) {
1243                 crtc = &fb_helper->crtc_info[c];
1244
1245                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1246                         continue;
1247                 }
1248
1249                 for (o = 0; o < n; o++)
1250                         if (best_crtcs[o] == crtc)
1251                                 break;
1252
1253                 if (o < n) {
1254                         /* ignore cloning unless only a single crtc */
1255                         if (fb_helper->crtc_count > 1)
1256                                 continue;
1257
1258                         if (!drm_mode_equal(modes[o], modes[n]))
1259                                 continue;
1260                 }
1261
1262                 crtcs[n] = crtc;
1263                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1264                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1265                                                   width, height);
1266                 if (score > best_score) {
1267                         best_crtc = crtc;
1268                         best_score = score;
1269                         memcpy(best_crtcs, crtcs,
1270                                dev->mode_config.num_connector *
1271                                sizeof(struct drm_fb_helper_crtc *));
1272                 }
1273         }
1274 out:
1275         kfree(crtcs);
1276         return best_score;
1277 }
1278
1279 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1280 {
1281         struct drm_device *dev = fb_helper->dev;
1282         struct drm_fb_helper_crtc **crtcs;
1283         struct drm_display_mode **modes;
1284         struct drm_encoder *encoder;
1285         struct drm_mode_set *modeset;
1286         bool *enabled;
1287         int width, height;
1288         int i, ret;
1289
1290         DRM_DEBUG_KMS("\n");
1291
1292         width = dev->mode_config.max_width;
1293         height = dev->mode_config.max_height;
1294
1295         /* clean out all the encoder/crtc combos */
1296         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1297                 encoder->crtc = NULL;
1298         }
1299
1300         crtcs = kcalloc(dev->mode_config.num_connector,
1301                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1302         modes = kcalloc(dev->mode_config.num_connector,
1303                         sizeof(struct drm_display_mode *), GFP_KERNEL);
1304         enabled = kcalloc(dev->mode_config.num_connector,
1305                           sizeof(bool), GFP_KERNEL);
1306
1307         drm_enable_connectors(fb_helper, enabled);
1308
1309         ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1310         if (!ret) {
1311                 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1312                 if (!ret)
1313                         DRM_ERROR("Unable to find initial modes\n");
1314         }
1315
1316         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1317
1318         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1319
1320         /* need to set the modesets up here for use later */
1321         /* fill out the connector<->crtc mappings into the modesets */
1322         for (i = 0; i < fb_helper->crtc_count; i++) {
1323                 modeset = &fb_helper->crtc_info[i].mode_set;
1324                 modeset->num_connectors = 0;
1325         }
1326
1327         for (i = 0; i < fb_helper->connector_count; i++) {
1328                 struct drm_display_mode *mode = modes[i];
1329                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1330                 modeset = &fb_crtc->mode_set;
1331
1332                 if (mode && fb_crtc) {
1333                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1334                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1335                         fb_crtc->desired_mode = mode;
1336                         if (modeset->mode)
1337                                 drm_mode_destroy(dev, modeset->mode);
1338                         modeset->mode = drm_mode_duplicate(dev,
1339                                                            fb_crtc->desired_mode);
1340                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1341                 }
1342         }
1343
1344         kfree(crtcs);
1345         kfree(modes);
1346         kfree(enabled);
1347 }
1348
1349 /**
1350  * drm_helper_initial_config - setup a sane initial connector configuration
1351  * @dev: DRM device
1352  *
1353  * LOCKING:
1354  * Called at init time, must take mode config lock.
1355  *
1356  * Scan the CRTCs and connectors and try to put together an initial setup.
1357  * At the moment, this is a cloned configuration across all heads with
1358  * a new framebuffer object as the backing store.
1359  *
1360  * RETURNS:
1361  * Zero if everything went ok, nonzero otherwise.
1362  */
1363 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1364 {
1365         struct drm_device *dev = fb_helper->dev;
1366         int count = 0;
1367
1368         /* disable all the possible outputs/crtcs before entering KMS mode */
1369         drm_helper_disable_unused_functions(fb_helper->dev);
1370
1371         drm_fb_helper_parse_command_line(fb_helper);
1372
1373         count = drm_fb_helper_probe_connector_modes(fb_helper,
1374                                                     dev->mode_config.max_width,
1375                                                     dev->mode_config.max_height);
1376         /*
1377          * we shouldn't end up with no modes here.
1378          */
1379         if (count == 0) {
1380                 printk(KERN_INFO "No connectors reported connected with modes\n");
1381         }
1382         drm_setup_crtcs(fb_helper);
1383
1384         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1385 }
1386 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1387
1388 /**
1389  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1390  *                               probing all the outputs attached to the fb.
1391  * @fb_helper: the drm_fb_helper
1392  *
1393  * LOCKING:
1394  * Called at runtime, must take mode config lock.
1395  *
1396  * Scan the connectors attached to the fb_helper and try to put together a
1397  * setup after *notification of a change in output configuration.
1398  *
1399  * RETURNS:
1400  * 0 on success and a non-zero error code otherwise.
1401  */
1402 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1403 {
1404         struct drm_device *dev = fb_helper->dev;
1405         int count = 0;
1406         u32 max_width, max_height, bpp_sel;
1407         bool bound = false, crtcs_bound = false;
1408         struct drm_crtc *crtc;
1409
1410         if (!fb_helper->fb)
1411                 return 0;
1412
1413         mutex_lock(&dev->mode_config.mutex);
1414         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1415                 if (crtc->fb)
1416                         crtcs_bound = true;
1417                 if (crtc->fb == fb_helper->fb)
1418                         bound = true;
1419         }
1420
1421         if (!bound && crtcs_bound) {
1422                 fb_helper->delayed_hotplug = true;
1423                 mutex_unlock(&dev->mode_config.mutex);
1424                 return 0;
1425         }
1426         DRM_DEBUG_KMS("\n");
1427
1428         max_width = fb_helper->fb->width;
1429         max_height = fb_helper->fb->height;
1430         bpp_sel = fb_helper->fb->bits_per_pixel;
1431
1432         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1433                                                     max_height);
1434         drm_setup_crtcs(fb_helper);
1435         mutex_unlock(&dev->mode_config.mutex);
1436
1437         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1438 }
1439 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1440
1441 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1442  * but the module doesn't depend on any fb console symbols.  At least
1443  * attempt to load fbcon to avoid leaving the system without a usable console.
1444  */
1445 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1446 static int __init drm_fb_helper_modinit(void)
1447 {
1448         const char *name = "fbcon";
1449         struct module *fbcon;
1450
1451         mutex_lock(&module_mutex);
1452         fbcon = find_module(name);
1453         mutex_unlock(&module_mutex);
1454
1455         if (!fbcon)
1456                 request_module_nowait(name);
1457         return 0;
1458 }
1459
1460 module_init(drm_fb_helper_modinit);
1461 #endif