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