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