drm: Use an include directory hierarchy similar to the Linux one
[dragonfly.git] / sys / dev / 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  * $FreeBSD: src/sys/dev/drm2/drm_fb_helper.c,v 1.1 2012/05/22 11:07:44 kib Exp $
31  */
32
33 #include <drm/drmP.h>
34 #include <drm/drm_crtc.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/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                 if (helper->crtc_info[i].mode_set.mode)
560                         drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
561         }
562         drm_free(helper->crtc_info, DRM_MEM_KMS);
563 }
564
565 int drm_fb_helper_init(struct drm_device *dev,
566                        struct drm_fb_helper *fb_helper,
567                        int crtc_count, int max_conn_count)
568 {
569         struct drm_crtc *crtc;
570         int i;
571
572         fb_helper->dev = dev;
573
574         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
575
576         fb_helper->crtc_info = kmalloc(crtc_count *
577             sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
578
579         fb_helper->crtc_count = crtc_count;
580         fb_helper->connector_info = kmalloc(dev->mode_config.num_connector *
581             sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
582             M_WAITOK | M_ZERO);
583         fb_helper->connector_count = 0;
584
585         for (i = 0; i < crtc_count; i++) {
586                 fb_helper->crtc_info[i].mode_set.connectors =
587                         kmalloc(max_conn_count * sizeof(struct drm_connector *),
588                             DRM_MEM_KMS, M_WAITOK | M_ZERO);
589
590                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
591         }
592
593         i = 0;
594         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
595                 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
596                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
597                 i++;
598         }
599         fb_helper->conn_limit = max_conn_count;
600         return 0;
601 }
602
603 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
604 {
605         if (!list_empty(&fb_helper->kernel_fb_list)) {
606                 list_del(&fb_helper->kernel_fb_list);
607                 if (list_empty(&kernel_fb_helper_list)) {
608 #if 0
609                         printk(KERN_INFO "drm: unregistered panic notifier\n");
610                         atomic_notifier_chain_unregister(&panic_notifier_list,
611                                                          &paniced);
612                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
613 #endif
614                 }
615         }
616
617         drm_fb_helper_crtc_free(fb_helper);
618
619 }
620
621 #if 0
622 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
623                      u16 blue, u16 regno, struct fb_info *info)
624 {
625         struct drm_fb_helper *fb_helper = info->par;
626         struct drm_framebuffer *fb = fb_helper->fb;
627         int pindex;
628
629         if (info->fix.visual == FB_VISUAL_trueCOLOR) {
630                 u32 *palette;
631                 u32 value;
632                 /* place color in psuedopalette */
633                 if (regno > 16)
634                         return -EINVAL;
635                 palette = (u32 *)info->pseudo_palette;
636                 red >>= (16 - info->var.red.length);
637                 green >>= (16 - info->var.green.length);
638                 blue >>= (16 - info->var.blue.length);
639                 value = (red << info->var.red.offset) |
640                         (green << info->var.green.offset) |
641                         (blue << info->var.blue.offset);
642                 if (info->var.transp.length > 0) {
643                         u32 mask = (1 << info->var.transp.length) - 1;
644                         mask <<= info->var.transp.offset;
645                         value |= mask;
646                 }
647                 palette[regno] = value;
648                 return 0;
649         }
650
651         pindex = regno;
652
653         if (fb->bits_per_pixel == 16) {
654                 pindex = regno << 3;
655
656                 if (fb->depth == 16 && regno > 63)
657                         return -EINVAL;
658                 if (fb->depth == 15 && regno > 31)
659                         return -EINVAL;
660
661                 if (fb->depth == 16) {
662                         u16 r, g, b;
663                         int i;
664                         if (regno < 32) {
665                                 for (i = 0; i < 8; i++)
666                                         fb_helper->funcs->gamma_set(crtc, red,
667                                                 green, blue, pindex + i);
668                         }
669
670                         fb_helper->funcs->gamma_get(crtc, &r,
671                                                     &g, &b,
672                                                     pindex >> 1);
673
674                         for (i = 0; i < 4; i++)
675                                 fb_helper->funcs->gamma_set(crtc, r,
676                                                             green, b,
677                                                             (pindex >> 1) + i);
678                 }
679         }
680
681         if (fb->depth != 16)
682                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
683         return 0;
684 }
685 #endif
686
687 #if 0
688 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
689 {
690         struct drm_fb_helper *fb_helper = info->par;
691         struct drm_crtc_helper_funcs *crtc_funcs;
692         u16 *red, *green, *blue, *transp;
693         struct drm_crtc *crtc;
694         int i, j, rc = 0;
695         int start;
696
697         for (i = 0; i < fb_helper->crtc_count; i++) {
698                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
699                 crtc_funcs = crtc->helper_private;
700
701                 red = cmap->red;
702                 green = cmap->green;
703                 blue = cmap->blue;
704                 transp = cmap->transp;
705                 start = cmap->start;
706
707                 for (j = 0; j < cmap->len; j++) {
708                         u16 hred, hgreen, hblue, htransp = 0xffff;
709
710                         hred = *red++;
711                         hgreen = *green++;
712                         hblue = *blue++;
713
714                         if (transp)
715                                 htransp = *transp++;
716
717                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
718                         if (rc)
719                                 return rc;
720                 }
721                 crtc_funcs->load_lut(crtc);
722         }
723         return rc;
724 }
725 #endif
726
727 #if 0
728 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
729                             struct fb_info *info)
730 {
731         struct drm_fb_helper *fb_helper = info->par;
732         struct drm_framebuffer *fb = fb_helper->fb;
733         int depth;
734
735         if (var->pixclock != 0 || in_dbg_master())
736                 return -EINVAL;
737
738         /* Need to resize the fb object !!! */
739         if (var->bits_per_pixel > fb->bits_per_pixel ||
740             var->xres > fb->width || var->yres > fb->height ||
741             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
742                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
743                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
744                           var->xres, var->yres, var->bits_per_pixel,
745                           var->xres_virtual, var->yres_virtual,
746                           fb->width, fb->height, fb->bits_per_pixel);
747                 return -EINVAL;
748         }
749
750         switch (var->bits_per_pixel) {
751         case 16:
752                 depth = (var->green.length == 6) ? 16 : 15;
753                 break;
754         case 32:
755                 depth = (var->transp.length > 0) ? 32 : 24;
756                 break;
757         default:
758                 depth = var->bits_per_pixel;
759                 break;
760         }
761
762         switch (depth) {
763         case 8:
764                 var->red.offset = 0;
765                 var->green.offset = 0;
766                 var->blue.offset = 0;
767                 var->red.length = 8;
768                 var->green.length = 8;
769                 var->blue.length = 8;
770                 var->transp.length = 0;
771                 var->transp.offset = 0;
772                 break;
773         case 15:
774                 var->red.offset = 10;
775                 var->green.offset = 5;
776                 var->blue.offset = 0;
777                 var->red.length = 5;
778                 var->green.length = 5;
779                 var->blue.length = 5;
780                 var->transp.length = 1;
781                 var->transp.offset = 15;
782                 break;
783         case 16:
784                 var->red.offset = 11;
785                 var->green.offset = 5;
786                 var->blue.offset = 0;
787                 var->red.length = 5;
788                 var->green.length = 6;
789                 var->blue.length = 5;
790                 var->transp.length = 0;
791                 var->transp.offset = 0;
792                 break;
793         case 24:
794                 var->red.offset = 16;
795                 var->green.offset = 8;
796                 var->blue.offset = 0;
797                 var->red.length = 8;
798                 var->green.length = 8;
799                 var->blue.length = 8;
800                 var->transp.length = 0;
801                 var->transp.offset = 0;
802                 break;
803         case 32:
804                 var->red.offset = 16;
805                 var->green.offset = 8;
806                 var->blue.offset = 0;
807                 var->red.length = 8;
808                 var->green.length = 8;
809                 var->blue.length = 8;
810                 var->transp.length = 8;
811                 var->transp.offset = 24;
812                 break;
813         default:
814                 return -EINVAL;
815         }
816         return 0;
817 }
818 #endif
819
820 #if 0
821 /* this will let fbcon do the mode init */
822 int drm_fb_helper_set_par(struct fb_info *info)
823 {
824         struct drm_fb_helper *fb_helper = info->par;
825         struct drm_device *dev = fb_helper->dev;
826         struct fb_var_screeninfo *var = &info->var;
827         struct drm_crtc *crtc;
828         int ret;
829         int i;
830
831         if (var->pixclock != 0) {
832                 DRM_ERROR("PIXEL CLOCK SET\n");
833                 return -EINVAL;
834         }
835
836         mutex_lock(&dev->mode_config.mutex);
837         for (i = 0; i < fb_helper->crtc_count; i++) {
838                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
839                 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
840                 if (ret) {
841                         mutex_unlock(&dev->mode_config.mutex);
842                         return ret;
843                 }
844         }
845         mutex_unlock(&dev->mode_config.mutex);
846
847         if (fb_helper->delayed_hotplug) {
848                 fb_helper->delayed_hotplug = false;
849                 drm_fb_helper_hotplug_event(fb_helper);
850         }
851         return 0;
852 }
853 #endif
854
855 #if 0
856 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
857                               struct fb_info *info)
858 {
859         struct drm_fb_helper *fb_helper = info->par;
860         struct drm_device *dev = fb_helper->dev;
861         struct drm_mode_set *modeset;
862         struct drm_crtc *crtc;
863         int ret = 0;
864         int i;
865
866         mutex_lock(&dev->mode_config.mutex);
867         for (i = 0; i < fb_helper->crtc_count; i++) {
868                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
869
870                 modeset = &fb_helper->crtc_info[i].mode_set;
871
872                 modeset->x = var->xoffset;
873                 modeset->y = var->yoffset;
874
875                 if (modeset->num_connectors) {
876                         ret = crtc->funcs->set_config(modeset);
877                         if (!ret) {
878                                 info->var.xoffset = var->xoffset;
879                                 info->var.yoffset = var->yoffset;
880                         }
881                 }
882         }
883         mutex_unlock(&dev->mode_config.mutex);
884         return ret;
885 }
886 #endif
887
888 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
889                                   int preferred_bpp)
890 {
891         int new_fb = 0;
892         int crtc_count = 0;
893         int i;
894 #if 0
895         struct fb_info *info;
896 #endif
897         struct drm_fb_helper_surface_size sizes;
898         int gamma_size = 0;
899
900         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
901         sizes.surface_depth = 24;
902         sizes.surface_bpp = 32;
903         sizes.fb_width = (unsigned)-1;
904         sizes.fb_height = (unsigned)-1;
905
906         /* if driver picks 8 or 16 by default use that
907            for both depth/bpp */
908         if (preferred_bpp != sizes.surface_bpp) {
909                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
910         }
911         /* first up get a count of crtcs now in use and new min/maxes width/heights */
912         for (i = 0; i < fb_helper->connector_count; i++) {
913                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
914                 struct drm_fb_helper_cmdline_mode *cmdline_mode;
915
916                 cmdline_mode = &fb_helper_conn->cmdline_mode;
917
918                 if (cmdline_mode->bpp_specified) {
919                         switch (cmdline_mode->bpp) {
920                         case 8:
921                                 sizes.surface_depth = sizes.surface_bpp = 8;
922                                 break;
923                         case 15:
924                                 sizes.surface_depth = 15;
925                                 sizes.surface_bpp = 16;
926                                 break;
927                         case 16:
928                                 sizes.surface_depth = sizes.surface_bpp = 16;
929                                 break;
930                         case 24:
931                                 sizes.surface_depth = sizes.surface_bpp = 24;
932                                 break;
933                         case 32:
934                                 sizes.surface_depth = 24;
935                                 sizes.surface_bpp = 32;
936                                 break;
937                         }
938                         break;
939                 }
940         }
941
942         crtc_count = 0;
943         for (i = 0; i < fb_helper->crtc_count; i++) {
944                 struct drm_display_mode *desired_mode;
945                 desired_mode = fb_helper->crtc_info[i].desired_mode;
946
947                 if (desired_mode) {
948                         if (gamma_size == 0)
949                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
950                         if (desired_mode->hdisplay < sizes.fb_width)
951                                 sizes.fb_width = desired_mode->hdisplay;
952                         if (desired_mode->vdisplay < sizes.fb_height)
953                                 sizes.fb_height = desired_mode->vdisplay;
954                         if (desired_mode->hdisplay > sizes.surface_width)
955                                 sizes.surface_width = desired_mode->hdisplay;
956                         if (desired_mode->vdisplay > sizes.surface_height)
957                                 sizes.surface_height = desired_mode->vdisplay;
958                         crtc_count++;
959                 }
960         }
961
962         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
963                 /* hmm everyone went away - assume VGA cable just fell out
964                    and will come back later. */
965                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
966                 sizes.fb_width = sizes.surface_width = 1024;
967                 sizes.fb_height = sizes.surface_height = 768;
968         }
969
970         /* push down into drivers */
971         new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
972         if (new_fb < 0)
973                 return new_fb;
974
975 #if 0
976         info = fb_helper->fbdev;
977 #endif
978
979         /* set the fb pointer */
980         for (i = 0; i < fb_helper->crtc_count; i++) {
981                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
982         }
983
984 #if 0
985         if (new_fb) {
986                 info->var.pixclock = 0;
987                 if (register_framebuffer(info) < 0) {
988                         return -EINVAL;
989                 }
990
991                 printf("fb%d: %s frame buffer device\n", info->node,
992                        info->fix.id);
993
994         } else {
995                 drm_fb_helper_set_par(info);
996         }
997
998         /* Switch back to kernel console on panic */
999         /* multi card linked list maybe */
1000         if (list_empty(&kernel_fb_helper_list)) {
1001                 printf("drm: registered panic notifier\n");
1002                 atomic_notifier_chain_register(&panic_notifier_list,
1003                                                &paniced);
1004         }
1005         if (new_fb)
1006                 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1007 #endif
1008
1009         return 0;
1010 }
1011
1012 #if 0
1013 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1014                             uint32_t depth)
1015 {
1016         info->fix.type = FB_TYPE_PACKED_PIXELS;
1017         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1018                 FB_VISUAL_trueCOLOR;
1019         info->fix.mmio_start = 0;
1020         info->fix.mmio_len = 0;
1021         info->fix.type_aux = 0;
1022         info->fix.xpanstep = 1; /* doing it in hw */
1023         info->fix.ypanstep = 1; /* doing it in hw */
1024         info->fix.ywrapstep = 0;
1025         info->fix.accel = FB_ACCEL_NONE;
1026         info->fix.type_aux = 0;
1027
1028         info->fix.line_length = pitch;
1029         return;
1030 }
1031
1032 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1033                             uint32_t fb_width, uint32_t fb_height)
1034 {
1035         struct drm_framebuffer *fb = fb_helper->fb;
1036         info->pseudo_palette = fb_helper->pseudo_palette;
1037         info->var.xres_virtual = fb->width;
1038         info->var.yres_virtual = fb->height;
1039         info->var.bits_per_pixel = fb->bits_per_pixel;
1040         info->var.accel_flags = FB_ACCELF_TEXT;
1041         info->var.xoffset = 0;
1042         info->var.yoffset = 0;
1043         info->var.activate = FB_ACTIVATE_NOW;
1044         info->var.height = -1;
1045         info->var.width = -1;
1046
1047         switch (fb->depth) {
1048         case 8:
1049                 info->var.red.offset = 0;
1050                 info->var.green.offset = 0;
1051                 info->var.blue.offset = 0;
1052                 info->var.red.length = 8; /* 8bit DAC */
1053                 info->var.green.length = 8;
1054                 info->var.blue.length = 8;
1055                 info->var.transp.offset = 0;
1056                 info->var.transp.length = 0;
1057                 break;
1058         case 15:
1059                 info->var.red.offset = 10;
1060                 info->var.green.offset = 5;
1061                 info->var.blue.offset = 0;
1062                 info->var.red.length = 5;
1063                 info->var.green.length = 5;
1064                 info->var.blue.length = 5;
1065                 info->var.transp.offset = 15;
1066                 info->var.transp.length = 1;
1067                 break;
1068         case 16:
1069                 info->var.red.offset = 11;
1070                 info->var.green.offset = 5;
1071                 info->var.blue.offset = 0;
1072                 info->var.red.length = 5;
1073                 info->var.green.length = 6;
1074                 info->var.blue.length = 5;
1075                 info->var.transp.offset = 0;
1076                 break;
1077         case 24:
1078                 info->var.red.offset = 16;
1079                 info->var.green.offset = 8;
1080                 info->var.blue.offset = 0;
1081                 info->var.red.length = 8;
1082                 info->var.green.length = 8;
1083                 info->var.blue.length = 8;
1084                 info->var.transp.offset = 0;
1085                 info->var.transp.length = 0;
1086                 break;
1087         case 32:
1088                 info->var.red.offset = 16;
1089                 info->var.green.offset = 8;
1090                 info->var.blue.offset = 0;
1091                 info->var.red.length = 8;
1092                 info->var.green.length = 8;
1093                 info->var.blue.length = 8;
1094                 info->var.transp.offset = 24;
1095                 info->var.transp.length = 8;
1096                 break;
1097         default:
1098                 break;
1099         }
1100
1101         info->var.xres = fb_width;
1102         info->var.yres = fb_height;
1103 }
1104 #endif
1105
1106 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1107                                                uint32_t maxX,
1108                                                uint32_t maxY)
1109 {
1110         struct drm_connector *connector;
1111         int count = 0;
1112         int i;
1113
1114         for (i = 0; i < fb_helper->connector_count; i++) {
1115                 connector = fb_helper->connector_info[i]->connector;
1116                 count += connector->funcs->fill_modes(connector, maxX, maxY);
1117         }
1118
1119         return count;
1120 }
1121
1122 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1123 {
1124         struct drm_display_mode *mode;
1125
1126         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1127                 if (drm_mode_width(mode) > width ||
1128                     drm_mode_height(mode) > height)
1129                         continue;
1130                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1131                         return mode;
1132         }
1133         return NULL;
1134 }
1135
1136 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1137 {
1138         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1139         cmdline_mode = &fb_connector->cmdline_mode;
1140         return cmdline_mode->specified;
1141 }
1142
1143 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1144                                                       int width, int height)
1145 {
1146         struct drm_cmdline_mode *cmdline_mode;
1147         struct drm_display_mode *mode = NULL;
1148
1149         cmdline_mode = &fb_helper_conn->cmdline_mode1;
1150         if (cmdline_mode->specified == false &&
1151             !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1152             cmdline_mode))
1153                         return (NULL);
1154
1155         /* attempt to find a matching mode in the list of modes
1156          *  we have gotten so far, if not add a CVT mode that conforms
1157          */
1158         if (cmdline_mode->rb || cmdline_mode->margins)
1159                 goto create_mode;
1160
1161         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1162                 /* check width/height */
1163                 if (mode->hdisplay != cmdline_mode->xres ||
1164                     mode->vdisplay != cmdline_mode->yres)
1165                         continue;
1166
1167                 if (cmdline_mode->refresh_specified) {
1168                         if (mode->vrefresh != cmdline_mode->refresh)
1169                                 continue;
1170                 }
1171
1172                 if (cmdline_mode->interlace) {
1173                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1174                                 continue;
1175                 }
1176                 return mode;
1177         }
1178
1179 create_mode:
1180         if (cmdline_mode->cvt)
1181                 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1182                                     cmdline_mode->xres, cmdline_mode->yres,
1183                                     cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1184                                     cmdline_mode->rb, cmdline_mode->interlace,
1185                                     cmdline_mode->margins);
1186         else
1187                 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1188                                     cmdline_mode->xres, cmdline_mode->yres,
1189                                     cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1190                                     cmdline_mode->interlace,
1191                                     cmdline_mode->margins);
1192         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1193         list_add(&mode->head, &fb_helper_conn->connector->modes);
1194         return mode;
1195 }
1196
1197 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1198 {
1199         bool enable;
1200
1201         if (strict) {
1202                 enable = connector->status == connector_status_connected;
1203         } else {
1204                 enable = connector->status != connector_status_disconnected;
1205         }
1206         return enable;
1207 }
1208
1209 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1210                                   bool *enabled)
1211 {
1212         bool any_enabled = false;
1213         struct drm_connector *connector;
1214         int i = 0;
1215
1216         for (i = 0; i < fb_helper->connector_count; i++) {
1217                 connector = fb_helper->connector_info[i]->connector;
1218                 enabled[i] = drm_connector_enabled(connector, true);
1219                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1220                           enabled[i] ? "yes" : "no");
1221                 any_enabled |= enabled[i];
1222         }
1223
1224         if (any_enabled)
1225                 return;
1226
1227         for (i = 0; i < fb_helper->connector_count; i++) {
1228                 connector = fb_helper->connector_info[i]->connector;
1229                 enabled[i] = drm_connector_enabled(connector, false);
1230         }
1231 }
1232
1233 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1234                               struct drm_display_mode **modes,
1235                               bool *enabled, int width, int height)
1236 {
1237         int count, i, j;
1238         bool can_clone = false;
1239         struct drm_fb_helper_connector *fb_helper_conn;
1240         struct drm_display_mode *dmt_mode, *mode;
1241
1242         /* only contemplate cloning in the single crtc case */
1243         if (fb_helper->crtc_count > 1)
1244                 return false;
1245
1246         count = 0;
1247         for (i = 0; i < fb_helper->connector_count; i++) {
1248                 if (enabled[i])
1249                         count++;
1250         }
1251
1252         /* only contemplate cloning if more than one connector is enabled */
1253         if (count <= 1)
1254                 return false;
1255
1256         /* check the command line or if nothing common pick 1024x768 */
1257         can_clone = true;
1258         for (i = 0; i < fb_helper->connector_count; i++) {
1259                 if (!enabled[i])
1260                         continue;
1261                 fb_helper_conn = fb_helper->connector_info[i];
1262                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1263                 if (!modes[i]) {
1264                         can_clone = false;
1265                         break;
1266                 }
1267                 for (j = 0; j < i; j++) {
1268                         if (!enabled[j])
1269                                 continue;
1270                         if (!drm_mode_equal(modes[j], modes[i]))
1271                                 can_clone = false;
1272                 }
1273         }
1274
1275         if (can_clone) {
1276                 DRM_DEBUG_KMS("can clone using command line\n");
1277                 return true;
1278         }
1279
1280         /* try and find a 1024x768 mode on each connector */
1281         can_clone = true;
1282         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1283
1284         for (i = 0; i < fb_helper->connector_count; i++) {
1285
1286                 if (!enabled[i])
1287                         continue;
1288
1289                 fb_helper_conn = fb_helper->connector_info[i];
1290                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1291                         if (drm_mode_equal(mode, dmt_mode))
1292                                 modes[i] = mode;
1293                 }
1294                 if (!modes[i])
1295                         can_clone = false;
1296         }
1297
1298         if (can_clone) {
1299                 DRM_DEBUG_KMS("can clone using 1024x768\n");
1300                 return true;
1301         }
1302         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1303         return false;
1304 }
1305
1306 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1307                                  struct drm_display_mode **modes,
1308                                  bool *enabled, int width, int height)
1309 {
1310         struct drm_fb_helper_connector *fb_helper_conn;
1311         int i;
1312
1313         for (i = 0; i < fb_helper->connector_count; i++) {
1314                 fb_helper_conn = fb_helper->connector_info[i];
1315
1316                 if (enabled[i] == false)
1317                         continue;
1318
1319                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1320                               fb_helper_conn->connector->base.id);
1321
1322                 /* got for command line mode first */
1323                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1324                 if (!modes[i]) {
1325                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1326                                       fb_helper_conn->connector->base.id);
1327                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1328                 }
1329                 /* No preferred modes, pick one off the list */
1330                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1331                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1332                                 break;
1333                 }
1334                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1335                           "none");
1336         }
1337         return true;
1338 }
1339
1340 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1341                           struct drm_fb_helper_crtc **best_crtcs,
1342                           struct drm_display_mode **modes,
1343                           int n, int width, int height)
1344 {
1345         int c, o;
1346         struct drm_device *dev = fb_helper->dev;
1347         struct drm_connector *connector;
1348         struct drm_connector_helper_funcs *connector_funcs;
1349         struct drm_encoder *encoder;
1350         struct drm_fb_helper_crtc *best_crtc;
1351         int my_score, best_score, score;
1352         struct drm_fb_helper_crtc **crtcs, *crtc;
1353         struct drm_fb_helper_connector *fb_helper_conn;
1354
1355         if (n == fb_helper->connector_count)
1356                 return 0;
1357
1358         fb_helper_conn = fb_helper->connector_info[n];
1359         connector = fb_helper_conn->connector;
1360
1361         best_crtcs[n] = NULL;
1362         best_crtc = NULL;
1363         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1364         if (modes[n] == NULL)
1365                 return best_score;
1366
1367         crtcs = kmalloc(dev->mode_config.num_connector *
1368             sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1369             M_WAITOK | M_ZERO);
1370
1371         my_score = 1;
1372         if (connector->status == connector_status_connected)
1373                 my_score++;
1374         if (drm_has_cmdline_mode(fb_helper_conn))
1375                 my_score++;
1376         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1377                 my_score++;
1378
1379         connector_funcs = connector->helper_private;
1380         encoder = connector_funcs->best_encoder(connector);
1381         if (!encoder)
1382                 goto out;
1383
1384         /* select a crtc for this connector and then attempt to configure
1385            remaining connectors */
1386         for (c = 0; c < fb_helper->crtc_count; c++) {
1387                 crtc = &fb_helper->crtc_info[c];
1388
1389                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1390                         continue;
1391                 }
1392
1393                 for (o = 0; o < n; o++)
1394                         if (best_crtcs[o] == crtc)
1395                                 break;
1396
1397                 if (o < n) {
1398                         /* ignore cloning unless only a single crtc */
1399                         if (fb_helper->crtc_count > 1)
1400                                 continue;
1401
1402                         if (!drm_mode_equal(modes[o], modes[n]))
1403                                 continue;
1404                 }
1405
1406                 crtcs[n] = crtc;
1407                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1408                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1409                                                   width, height);
1410                 if (score > best_score) {
1411                         best_crtc = crtc;
1412                         best_score = score;
1413                         memcpy(best_crtcs, crtcs,
1414                                dev->mode_config.num_connector *
1415                                sizeof(struct drm_fb_helper_crtc *));
1416                 }
1417         }
1418 out:
1419         drm_free(crtcs, DRM_MEM_KMS);
1420         return best_score;
1421 }
1422
1423 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1424 {
1425         struct drm_device *dev = fb_helper->dev;
1426         struct drm_fb_helper_crtc **crtcs;
1427         struct drm_display_mode **modes;
1428         struct drm_encoder *encoder;
1429         struct drm_mode_set *modeset;
1430         bool *enabled;
1431         int width, height;
1432         int i, ret;
1433
1434         DRM_DEBUG_KMS("\n");
1435
1436         width = dev->mode_config.max_width;
1437         height = dev->mode_config.max_height;
1438
1439         /* clean out all the encoder/crtc combos */
1440         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1441                 encoder->crtc = NULL;
1442         }
1443
1444         crtcs = kmalloc(dev->mode_config.num_connector *
1445             sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1446             M_WAITOK | M_ZERO);
1447         modes = kmalloc(dev->mode_config.num_connector *
1448             sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1449             M_WAITOK | M_ZERO);
1450         enabled = kmalloc(dev->mode_config.num_connector *
1451             sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1452
1453         drm_enable_connectors(fb_helper, enabled);
1454
1455         ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1456         if (!ret) {
1457                 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1458                 if (!ret)
1459                         DRM_ERROR("Unable to find initial modes\n");
1460         }
1461
1462         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1463
1464         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1465
1466         /* need to set the modesets up here for use later */
1467         /* fill out the connector<->crtc mappings into the modesets */
1468         for (i = 0; i < fb_helper->crtc_count; i++) {
1469                 modeset = &fb_helper->crtc_info[i].mode_set;
1470                 modeset->num_connectors = 0;
1471         }
1472
1473         for (i = 0; i < fb_helper->connector_count; i++) {
1474                 struct drm_display_mode *mode = modes[i];
1475                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1476                 modeset = &fb_crtc->mode_set;
1477
1478                 if (mode && fb_crtc) {
1479                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1480                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1481                         fb_crtc->desired_mode = mode;
1482                         if (modeset->mode)
1483                                 drm_mode_destroy(dev, modeset->mode);
1484                         modeset->mode = drm_mode_duplicate(dev,
1485                                                            fb_crtc->desired_mode);
1486                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1487                 }
1488         }
1489
1490         drm_free(crtcs, DRM_MEM_KMS);
1491         drm_free(modes, DRM_MEM_KMS);
1492         drm_free(enabled, DRM_MEM_KMS);
1493 }
1494
1495 /**
1496  * drm_helper_initial_config - setup a sane initial connector configuration
1497  * @dev: DRM device
1498  *
1499  * LOCKING:
1500  * Called at init time, must take mode config lock.
1501  *
1502  * Scan the CRTCs and connectors and try to put together an initial setup.
1503  * At the moment, this is a cloned configuration across all heads with
1504  * a new framebuffer object as the backing store.
1505  *
1506  * RETURNS:
1507  * Zero if everything went ok, nonzero otherwise.
1508  */
1509 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1510 {
1511         struct drm_device *dev = fb_helper->dev;
1512         int count = 0;
1513
1514         /* disable all the possible outputs/crtcs before entering KMS mode */
1515         drm_helper_disable_unused_functions(fb_helper->dev);
1516
1517         drm_fb_helper_parse_command_line(fb_helper);
1518
1519         count = drm_fb_helper_probe_connector_modes(fb_helper,
1520                                                     dev->mode_config.max_width,
1521                                                     dev->mode_config.max_height);
1522         /*
1523          * we shouldn't end up with no modes here.
1524          */
1525         if (count == 0) {
1526                 kprintf("No connectors reported connected with modes\n");
1527         }
1528         drm_setup_crtcs(fb_helper);
1529
1530         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1531 }
1532
1533 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1534 {
1535         struct drm_device *dev = fb_helper->dev;
1536         int count = 0;
1537         u32 max_width, max_height, bpp_sel;
1538         bool bound = false, crtcs_bound = false;
1539         struct drm_crtc *crtc;
1540
1541         if (!fb_helper->fb)
1542                 return 0;
1543
1544         lockmgr(&dev->mode_config.lock, LK_EXCLUSIVE);
1545         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1546                 if (crtc->fb)
1547                         crtcs_bound = true;
1548                 if (crtc->fb == fb_helper->fb)
1549                         bound = true;
1550         }
1551
1552         if (!bound && crtcs_bound) {
1553                 fb_helper->delayed_hotplug = true;
1554                 lockmgr(&dev->mode_config.lock, LK_RELEASE);
1555                 return 0;
1556         }
1557         DRM_DEBUG_KMS("\n");
1558
1559         max_width = fb_helper->fb->width;
1560         max_height = fb_helper->fb->height;
1561         bpp_sel = fb_helper->fb->bits_per_pixel;
1562
1563         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1564                                                     max_height);
1565         drm_setup_crtcs(fb_helper);
1566         lockmgr(&dev->mode_config.lock, LK_RELEASE);
1567
1568         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1569 }
1570