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