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