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