]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_fb_helper.c
drm/fb-helper: don't sleep for screen unblank when an oops is in progress
[karo-tx-linux.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/kernel.h>
33 #include <linux/sysrq.h>
34 #include <linux/slab.h>
35 #include <linux/fb.h>
36 #include <linux/module.h>
37 #include <drm/drmP.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
41
42 MODULE_AUTHOR("David Airlie, Jesse Barnes");
43 MODULE_DESCRIPTION("DRM KMS helper");
44 MODULE_LICENSE("GPL and additional rights");
45
46 static LIST_HEAD(kernel_fb_helper_list);
47
48 /**
49  * DOC: fbdev helpers
50  *
51  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
52  * mode setting driver. They can be used mostly independantely from the crtc
53  * helper functions used by many drivers to implement the kernel mode setting
54  * interfaces.
55  *
56  * Initialization is done as a three-step process with drm_fb_helper_init(),
57  * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
58  * Drivers with fancier requirements than the default beheviour can override the
59  * second step with their own code.  Teardown is done with drm_fb_helper_fini().
60  *
61  * At runtime drivers should restore the fbdev console by calling
62  * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
63  * should also notify the fb helper code from updates to the output
64  * configuration by calling drm_fb_helper_hotplug_event(). For easier
65  * integration with the output polling code in drm_crtc_helper.c the modeset
66  * code proves a ->output_poll_changed callback.
67  *
68  * All other functions exported by the fb helper library can be used to
69  * implement the fbdev driver interface by the driver.
70  */
71
72 /**
73  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
74  *                                             emulation helper
75  * @fb_helper: fbdev initialized with drm_fb_helper_init
76  *
77  * This functions adds all the available connectors for use with the given
78  * fb_helper. This is a separate step to allow drivers to freely assign
79  * connectors to the fbdev, e.g. if some are reserved for special purposes or
80  * not adequate to be used for the fbcon.
81  *
82  * Since this is part of the initial setup before the fbdev is published, no
83  * locking is required.
84  */
85 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
86 {
87         struct drm_device *dev = fb_helper->dev;
88         struct drm_connector *connector;
89         int i;
90
91         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
92                 struct drm_fb_helper_connector *fb_helper_connector;
93
94                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
95                 if (!fb_helper_connector)
96                         goto fail;
97
98                 fb_helper_connector->connector = connector;
99                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
100         }
101         return 0;
102 fail:
103         for (i = 0; i < fb_helper->connector_count; i++) {
104                 kfree(fb_helper->connector_info[i]);
105                 fb_helper->connector_info[i] = NULL;
106         }
107         fb_helper->connector_count = 0;
108         return -ENOMEM;
109 }
110 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
111
112 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
113 {
114         struct drm_fb_helper_connector *fb_helper_conn;
115         int i;
116
117         for (i = 0; i < fb_helper->connector_count; i++) {
118                 struct drm_cmdline_mode *mode;
119                 struct drm_connector *connector;
120                 char *option = NULL;
121
122                 fb_helper_conn = fb_helper->connector_info[i];
123                 connector = fb_helper_conn->connector;
124                 mode = &fb_helper_conn->cmdline_mode;
125
126                 /* do something on return - turn off connector maybe */
127                 if (fb_get_options(drm_get_connector_name(connector), &option))
128                         continue;
129
130                 if (drm_mode_parse_command_line_for_connector(option,
131                                                               connector,
132                                                               mode)) {
133                         if (mode->force) {
134                                 const char *s;
135                                 switch (mode->force) {
136                                 case DRM_FORCE_OFF:
137                                         s = "OFF";
138                                         break;
139                                 case DRM_FORCE_ON_DIGITAL:
140                                         s = "ON - dig";
141                                         break;
142                                 default:
143                                 case DRM_FORCE_ON:
144                                         s = "ON";
145                                         break;
146                                 }
147
148                                 DRM_INFO("forcing %s connector %s\n",
149                                          drm_get_connector_name(connector), s);
150                                 connector->force = mode->force;
151                         }
152
153                         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
154                                       drm_get_connector_name(connector),
155                                       mode->xres, mode->yres,
156                                       mode->refresh_specified ? mode->refresh : 60,
157                                       mode->rb ? " reduced blanking" : "",
158                                       mode->margins ? " with margins" : "",
159                                       mode->interlace ?  " interlaced" : "");
160                 }
161
162         }
163         return 0;
164 }
165
166 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
167 {
168         uint16_t *r_base, *g_base, *b_base;
169         int i;
170
171         r_base = crtc->gamma_store;
172         g_base = r_base + crtc->gamma_size;
173         b_base = g_base + crtc->gamma_size;
174
175         for (i = 0; i < crtc->gamma_size; i++)
176                 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
177 }
178
179 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
180 {
181         uint16_t *r_base, *g_base, *b_base;
182
183         if (crtc->funcs->gamma_set == NULL)
184                 return;
185
186         r_base = crtc->gamma_store;
187         g_base = r_base + crtc->gamma_size;
188         b_base = g_base + crtc->gamma_size;
189
190         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
191 }
192
193 /**
194  * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
195  * @info: fbdev registered by the helper
196  */
197 int drm_fb_helper_debug_enter(struct fb_info *info)
198 {
199         struct drm_fb_helper *helper = info->par;
200         struct drm_crtc_helper_funcs *funcs;
201         int i;
202
203         if (list_empty(&kernel_fb_helper_list))
204                 return false;
205
206         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
207                 for (i = 0; i < helper->crtc_count; i++) {
208                         struct drm_mode_set *mode_set =
209                                 &helper->crtc_info[i].mode_set;
210
211                         if (!mode_set->crtc->enabled)
212                                 continue;
213
214                         funcs = mode_set->crtc->helper_private;
215                         drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
216                         funcs->mode_set_base_atomic(mode_set->crtc,
217                                                     mode_set->fb,
218                                                     mode_set->x,
219                                                     mode_set->y,
220                                                     ENTER_ATOMIC_MODE_SET);
221                 }
222         }
223
224         return 0;
225 }
226 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
227
228 /* Find the real fb for a given fb helper CRTC */
229 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
230 {
231         struct drm_device *dev = crtc->dev;
232         struct drm_crtc *c;
233
234         list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
235                 if (crtc->base.id == c->base.id)
236                         return c->fb;
237         }
238
239         return NULL;
240 }
241
242 /**
243  * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
244  * @info: fbdev registered by the helper
245  */
246 int drm_fb_helper_debug_leave(struct fb_info *info)
247 {
248         struct drm_fb_helper *helper = info->par;
249         struct drm_crtc *crtc;
250         struct drm_crtc_helper_funcs *funcs;
251         struct drm_framebuffer *fb;
252         int i;
253
254         for (i = 0; i < helper->crtc_count; i++) {
255                 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
256                 crtc = mode_set->crtc;
257                 funcs = crtc->helper_private;
258                 fb = drm_mode_config_fb(crtc);
259
260                 if (!crtc->enabled)
261                         continue;
262
263                 if (!fb) {
264                         DRM_ERROR("no fb to restore??\n");
265                         continue;
266                 }
267
268                 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
269                 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
270                                             crtc->y, LEAVE_ATOMIC_MODE_SET);
271         }
272
273         return 0;
274 }
275 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
276
277 /**
278  * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
279  * @fb_helper: fbcon to restore
280  *
281  * This should be called from driver's drm ->lastclose callback
282  * when implementing an fbcon on top of kms using this helper. This ensures that
283  * the user isn't greeted with a black screen when e.g. X dies.
284  */
285 bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
286 {
287         bool error = false;
288         int i, ret;
289
290         drm_warn_on_modeset_not_all_locked(fb_helper->dev);
291
292         for (i = 0; i < fb_helper->crtc_count; i++) {
293                 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
294                 ret = drm_mode_set_config_internal(mode_set);
295                 if (ret)
296                         error = true;
297         }
298         return error;
299 }
300 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
301
302 /*
303  * restore fbcon display for all kms driver's using this helper, used for sysrq
304  * and panic handling.
305  */
306 static bool drm_fb_helper_force_kernel_mode(void)
307 {
308         bool ret, error = false;
309         struct drm_fb_helper *helper;
310
311         if (list_empty(&kernel_fb_helper_list))
312                 return false;
313
314         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
315                 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
316                         continue;
317
318                 ret = drm_fb_helper_restore_fbdev_mode(helper);
319                 if (ret)
320                         error = true;
321         }
322         return error;
323 }
324
325 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
326                         void *panic_str)
327 {
328         /*
329          * It's a waste of time and effort to switch back to text console
330          * if the kernel should reboot before panic messages can be seen.
331          */
332         if (panic_timeout < 0)
333                 return 0;
334
335         pr_err("panic occurred, switching back to text console\n");
336         return drm_fb_helper_force_kernel_mode();
337 }
338
339 static struct notifier_block paniced = {
340         .notifier_call = drm_fb_helper_panic,
341 };
342
343 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
344 {
345         struct drm_device *dev = fb_helper->dev;
346         struct drm_crtc *crtc;
347         int bound = 0, crtcs_bound = 0;
348
349         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
350                 if (crtc->fb)
351                         crtcs_bound++;
352                 if (crtc->fb == fb_helper->fb)
353                         bound++;
354         }
355
356         if (bound < crtcs_bound)
357                 return false;
358         return true;
359 }
360
361 #ifdef CONFIG_MAGIC_SYSRQ
362 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
363 {
364         bool ret;
365         ret = drm_fb_helper_force_kernel_mode();
366         if (ret == true)
367                 DRM_ERROR("Failed to restore crtc configuration\n");
368 }
369 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
370
371 static void drm_fb_helper_sysrq(int dummy1)
372 {
373         schedule_work(&drm_fb_helper_restore_work);
374 }
375
376 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
377         .handler = drm_fb_helper_sysrq,
378         .help_msg = "force-fb(V)",
379         .action_msg = "Restore framebuffer console",
380 };
381 #else
382 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
383 #endif
384
385 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
386 {
387         struct drm_fb_helper *fb_helper = info->par;
388         struct drm_device *dev = fb_helper->dev;
389         struct drm_crtc *crtc;
390         struct drm_connector *connector;
391         int i, j;
392
393         /*
394          * fbdev->blank can be called from irq context in case of a panic.
395          * Since we already have our own special panic handler which will
396          * restore the fbdev console mode completely, just bail out early.
397          */
398         if (oops_in_progress)
399                 return;
400
401         /*
402          * fbdev->blank can be called from irq context in case of a panic.
403          * Since we already have our own special panic handler which will
404          * restore the fbdev console mode completely, just bail out early.
405          */
406         if (oops_in_progress)
407                 return;
408
409         /*
410          * For each CRTC in this fb, turn the connectors on/off.
411          */
412         drm_modeset_lock_all(dev);
413         if (!drm_fb_helper_is_bound(fb_helper)) {
414                 drm_modeset_unlock_all(dev);
415                 return;
416         }
417
418         for (i = 0; i < fb_helper->crtc_count; i++) {
419                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
420
421                 if (!crtc->enabled)
422                         continue;
423
424                 /* Walk the connectors & encoders on this fb turning them on/off */
425                 for (j = 0; j < fb_helper->connector_count; j++) {
426                         connector = fb_helper->connector_info[j]->connector;
427                         connector->funcs->dpms(connector, dpms_mode);
428                         drm_object_property_set_value(&connector->base,
429                                 dev->mode_config.dpms_property, dpms_mode);
430                 }
431         }
432         drm_modeset_unlock_all(dev);
433 }
434
435 /**
436  * drm_fb_helper_blank - implementation for ->fb_blank
437  * @blank: desired blanking state
438  * @info: fbdev registered by the helper
439  */
440 int drm_fb_helper_blank(int blank, struct fb_info *info)
441 {
442         switch (blank) {
443         /* Display: On; HSync: On, VSync: On */
444         case FB_BLANK_UNBLANK:
445                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
446                 break;
447         /* Display: Off; HSync: On, VSync: On */
448         case FB_BLANK_NORMAL:
449                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
450                 break;
451         /* Display: Off; HSync: Off, VSync: On */
452         case FB_BLANK_HSYNC_SUSPEND:
453                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
454                 break;
455         /* Display: Off; HSync: On, VSync: Off */
456         case FB_BLANK_VSYNC_SUSPEND:
457                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
458                 break;
459         /* Display: Off; HSync: Off, VSync: Off */
460         case FB_BLANK_POWERDOWN:
461                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
462                 break;
463         }
464         return 0;
465 }
466 EXPORT_SYMBOL(drm_fb_helper_blank);
467
468 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
469 {
470         int i;
471
472         for (i = 0; i < helper->connector_count; i++)
473                 kfree(helper->connector_info[i]);
474         kfree(helper->connector_info);
475         for (i = 0; i < helper->crtc_count; i++) {
476                 kfree(helper->crtc_info[i].mode_set.connectors);
477                 if (helper->crtc_info[i].mode_set.mode)
478                         drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
479         }
480         kfree(helper->crtc_info);
481 }
482
483 /**
484  * drm_fb_helper_init - initialize a drm_fb_helper structure
485  * @dev: drm device
486  * @fb_helper: driver-allocated fbdev helper structure to initialize
487  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
488  * @max_conn_count: max connector count
489  *
490  * This allocates the structures for the fbdev helper with the given limits.
491  * Note that this won't yet touch the hardware (through the driver interfaces)
492  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
493  * to allow driver writes more control over the exact init sequence.
494  *
495  * Drivers must set fb_helper->funcs before calling
496  * drm_fb_helper_initial_config().
497  *
498  * RETURNS:
499  * Zero if everything went ok, nonzero otherwise.
500  */
501 int drm_fb_helper_init(struct drm_device *dev,
502                        struct drm_fb_helper *fb_helper,
503                        int crtc_count, int max_conn_count)
504 {
505         struct drm_crtc *crtc;
506         int i;
507
508         fb_helper->dev = dev;
509
510         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
511
512         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
513         if (!fb_helper->crtc_info)
514                 return -ENOMEM;
515
516         fb_helper->crtc_count = crtc_count;
517         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
518         if (!fb_helper->connector_info) {
519                 kfree(fb_helper->crtc_info);
520                 return -ENOMEM;
521         }
522         fb_helper->connector_count = 0;
523
524         for (i = 0; i < crtc_count; i++) {
525                 fb_helper->crtc_info[i].mode_set.connectors =
526                         kcalloc(max_conn_count,
527                                 sizeof(struct drm_connector *),
528                                 GFP_KERNEL);
529
530                 if (!fb_helper->crtc_info[i].mode_set.connectors)
531                         goto out_free;
532                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
533         }
534
535         i = 0;
536         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
537                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
538                 i++;
539         }
540
541         return 0;
542 out_free:
543         drm_fb_helper_crtc_free(fb_helper);
544         return -ENOMEM;
545 }
546 EXPORT_SYMBOL(drm_fb_helper_init);
547
548 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
549 {
550         if (!list_empty(&fb_helper->kernel_fb_list)) {
551                 list_del(&fb_helper->kernel_fb_list);
552                 if (list_empty(&kernel_fb_helper_list)) {
553                         pr_info("drm: unregistered panic notifier\n");
554                         atomic_notifier_chain_unregister(&panic_notifier_list,
555                                                          &paniced);
556                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
557                 }
558         }
559
560         drm_fb_helper_crtc_free(fb_helper);
561
562 }
563 EXPORT_SYMBOL(drm_fb_helper_fini);
564
565 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
566                      u16 blue, u16 regno, struct fb_info *info)
567 {
568         struct drm_fb_helper *fb_helper = info->par;
569         struct drm_framebuffer *fb = fb_helper->fb;
570         int pindex;
571
572         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
573                 u32 *palette;
574                 u32 value;
575                 /* place color in psuedopalette */
576                 if (regno > 16)
577                         return -EINVAL;
578                 palette = (u32 *)info->pseudo_palette;
579                 red >>= (16 - info->var.red.length);
580                 green >>= (16 - info->var.green.length);
581                 blue >>= (16 - info->var.blue.length);
582                 value = (red << info->var.red.offset) |
583                         (green << info->var.green.offset) |
584                         (blue << info->var.blue.offset);
585                 if (info->var.transp.length > 0) {
586                         u32 mask = (1 << info->var.transp.length) - 1;
587                         mask <<= info->var.transp.offset;
588                         value |= mask;
589                 }
590                 palette[regno] = value;
591                 return 0;
592         }
593
594         pindex = regno;
595
596         if (fb->bits_per_pixel == 16) {
597                 pindex = regno << 3;
598
599                 if (fb->depth == 16 && regno > 63)
600                         return -EINVAL;
601                 if (fb->depth == 15 && regno > 31)
602                         return -EINVAL;
603
604                 if (fb->depth == 16) {
605                         u16 r, g, b;
606                         int i;
607                         if (regno < 32) {
608                                 for (i = 0; i < 8; i++)
609                                         fb_helper->funcs->gamma_set(crtc, red,
610                                                 green, blue, pindex + i);
611                         }
612
613                         fb_helper->funcs->gamma_get(crtc, &r,
614                                                     &g, &b,
615                                                     pindex >> 1);
616
617                         for (i = 0; i < 4; i++)
618                                 fb_helper->funcs->gamma_set(crtc, r,
619                                                             green, b,
620                                                             (pindex >> 1) + i);
621                 }
622         }
623
624         if (fb->depth != 16)
625                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
626         return 0;
627 }
628
629 /**
630  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
631  * @cmap: cmap to set
632  * @info: fbdev registered by the helper
633  */
634 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
635 {
636         struct drm_fb_helper *fb_helper = info->par;
637         struct drm_crtc_helper_funcs *crtc_funcs;
638         u16 *red, *green, *blue, *transp;
639         struct drm_crtc *crtc;
640         int i, j, rc = 0;
641         int start;
642
643         for (i = 0; i < fb_helper->crtc_count; i++) {
644                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
645                 crtc_funcs = crtc->helper_private;
646
647                 red = cmap->red;
648                 green = cmap->green;
649                 blue = cmap->blue;
650                 transp = cmap->transp;
651                 start = cmap->start;
652
653                 for (j = 0; j < cmap->len; j++) {
654                         u16 hred, hgreen, hblue, htransp = 0xffff;
655
656                         hred = *red++;
657                         hgreen = *green++;
658                         hblue = *blue++;
659
660                         if (transp)
661                                 htransp = *transp++;
662
663                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
664                         if (rc)
665                                 return rc;
666                 }
667                 crtc_funcs->load_lut(crtc);
668         }
669         return rc;
670 }
671 EXPORT_SYMBOL(drm_fb_helper_setcmap);
672
673 /**
674  * drm_fb_helper_check_var - implementation for ->fb_check_var
675  * @var: screeninfo to check
676  * @info: fbdev registered by the helper
677  */
678 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
679                             struct fb_info *info)
680 {
681         struct drm_fb_helper *fb_helper = info->par;
682         struct drm_framebuffer *fb = fb_helper->fb;
683         int depth;
684
685         if (var->pixclock != 0 || in_dbg_master())
686                 return -EINVAL;
687
688         /* Need to resize the fb object !!! */
689         if (var->bits_per_pixel > fb->bits_per_pixel ||
690             var->xres > fb->width || var->yres > fb->height ||
691             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
692                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
693                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
694                           var->xres, var->yres, var->bits_per_pixel,
695                           var->xres_virtual, var->yres_virtual,
696                           fb->width, fb->height, fb->bits_per_pixel);
697                 return -EINVAL;
698         }
699
700         switch (var->bits_per_pixel) {
701         case 16:
702                 depth = (var->green.length == 6) ? 16 : 15;
703                 break;
704         case 32:
705                 depth = (var->transp.length > 0) ? 32 : 24;
706                 break;
707         default:
708                 depth = var->bits_per_pixel;
709                 break;
710         }
711
712         switch (depth) {
713         case 8:
714                 var->red.offset = 0;
715                 var->green.offset = 0;
716                 var->blue.offset = 0;
717                 var->red.length = 8;
718                 var->green.length = 8;
719                 var->blue.length = 8;
720                 var->transp.length = 0;
721                 var->transp.offset = 0;
722                 break;
723         case 15:
724                 var->red.offset = 10;
725                 var->green.offset = 5;
726                 var->blue.offset = 0;
727                 var->red.length = 5;
728                 var->green.length = 5;
729                 var->blue.length = 5;
730                 var->transp.length = 1;
731                 var->transp.offset = 15;
732                 break;
733         case 16:
734                 var->red.offset = 11;
735                 var->green.offset = 5;
736                 var->blue.offset = 0;
737                 var->red.length = 5;
738                 var->green.length = 6;
739                 var->blue.length = 5;
740                 var->transp.length = 0;
741                 var->transp.offset = 0;
742                 break;
743         case 24:
744                 var->red.offset = 16;
745                 var->green.offset = 8;
746                 var->blue.offset = 0;
747                 var->red.length = 8;
748                 var->green.length = 8;
749                 var->blue.length = 8;
750                 var->transp.length = 0;
751                 var->transp.offset = 0;
752                 break;
753         case 32:
754                 var->red.offset = 16;
755                 var->green.offset = 8;
756                 var->blue.offset = 0;
757                 var->red.length = 8;
758                 var->green.length = 8;
759                 var->blue.length = 8;
760                 var->transp.length = 8;
761                 var->transp.offset = 24;
762                 break;
763         default:
764                 return -EINVAL;
765         }
766         return 0;
767 }
768 EXPORT_SYMBOL(drm_fb_helper_check_var);
769
770 /**
771  * drm_fb_helper_set_par - implementation for ->fb_set_par
772  * @info: fbdev registered by the helper
773  *
774  * This will let fbcon do the mode init and is called at initialization time by
775  * the fbdev core when registering the driver, and later on through the hotplug
776  * callback.
777  */
778 int drm_fb_helper_set_par(struct fb_info *info)
779 {
780         struct drm_fb_helper *fb_helper = info->par;
781         struct drm_device *dev = fb_helper->dev;
782         struct fb_var_screeninfo *var = &info->var;
783         int ret;
784         int i;
785
786         if (var->pixclock != 0) {
787                 DRM_ERROR("PIXEL CLOCK SET\n");
788                 return -EINVAL;
789         }
790
791         drm_modeset_lock_all(dev);
792         for (i = 0; i < fb_helper->crtc_count; i++) {
793                 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
794                 if (ret) {
795                         drm_modeset_unlock_all(dev);
796                         return ret;
797                 }
798         }
799         drm_modeset_unlock_all(dev);
800
801         if (fb_helper->delayed_hotplug) {
802                 fb_helper->delayed_hotplug = false;
803                 drm_fb_helper_hotplug_event(fb_helper);
804         }
805         return 0;
806 }
807 EXPORT_SYMBOL(drm_fb_helper_set_par);
808
809 /**
810  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
811  * @var: updated screen information
812  * @info: fbdev registered by the helper
813  */
814 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
815                               struct fb_info *info)
816 {
817         struct drm_fb_helper *fb_helper = info->par;
818         struct drm_device *dev = fb_helper->dev;
819         struct drm_mode_set *modeset;
820         struct drm_crtc *crtc;
821         int ret = 0;
822         int i;
823
824         drm_modeset_lock_all(dev);
825         if (!drm_fb_helper_is_bound(fb_helper)) {
826                 drm_modeset_unlock_all(dev);
827                 return -EBUSY;
828         }
829
830         for (i = 0; i < fb_helper->crtc_count; i++) {
831                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
832
833                 modeset = &fb_helper->crtc_info[i].mode_set;
834
835                 modeset->x = var->xoffset;
836                 modeset->y = var->yoffset;
837
838                 if (modeset->num_connectors) {
839                         ret = drm_mode_set_config_internal(modeset);
840                         if (!ret) {
841                                 info->var.xoffset = var->xoffset;
842                                 info->var.yoffset = var->yoffset;
843                         }
844                 }
845         }
846         drm_modeset_unlock_all(dev);
847         return ret;
848 }
849 EXPORT_SYMBOL(drm_fb_helper_pan_display);
850
851 /*
852  * Allocates the backing storage and sets up the fbdev info structure through
853  * the ->fb_probe callback and then registers the fbdev and sets up the panic
854  * notifier.
855  */
856 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
857                                          int preferred_bpp)
858 {
859         int ret = 0;
860         int crtc_count = 0;
861         int i;
862         struct fb_info *info;
863         struct drm_fb_helper_surface_size sizes;
864         int gamma_size = 0;
865
866         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
867         sizes.surface_depth = 24;
868         sizes.surface_bpp = 32;
869         sizes.fb_width = (unsigned)-1;
870         sizes.fb_height = (unsigned)-1;
871
872         /* if driver picks 8 or 16 by default use that
873            for both depth/bpp */
874         if (preferred_bpp != sizes.surface_bpp)
875                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
876
877         /* first up get a count of crtcs now in use and new min/maxes width/heights */
878         for (i = 0; i < fb_helper->connector_count; i++) {
879                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
880                 struct drm_cmdline_mode *cmdline_mode;
881
882                 cmdline_mode = &fb_helper_conn->cmdline_mode;
883
884                 if (cmdline_mode->bpp_specified) {
885                         switch (cmdline_mode->bpp) {
886                         case 8:
887                                 sizes.surface_depth = sizes.surface_bpp = 8;
888                                 break;
889                         case 15:
890                                 sizes.surface_depth = 15;
891                                 sizes.surface_bpp = 16;
892                                 break;
893                         case 16:
894                                 sizes.surface_depth = sizes.surface_bpp = 16;
895                                 break;
896                         case 24:
897                                 sizes.surface_depth = sizes.surface_bpp = 24;
898                                 break;
899                         case 32:
900                                 sizes.surface_depth = 24;
901                                 sizes.surface_bpp = 32;
902                                 break;
903                         }
904                         break;
905                 }
906         }
907
908         crtc_count = 0;
909         for (i = 0; i < fb_helper->crtc_count; i++) {
910                 struct drm_display_mode *desired_mode;
911                 desired_mode = fb_helper->crtc_info[i].desired_mode;
912
913                 if (desired_mode) {
914                         if (gamma_size == 0)
915                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
916                         if (desired_mode->hdisplay < sizes.fb_width)
917                                 sizes.fb_width = desired_mode->hdisplay;
918                         if (desired_mode->vdisplay < sizes.fb_height)
919                                 sizes.fb_height = desired_mode->vdisplay;
920                         if (desired_mode->hdisplay > sizes.surface_width)
921                                 sizes.surface_width = desired_mode->hdisplay;
922                         if (desired_mode->vdisplay > sizes.surface_height)
923                                 sizes.surface_height = desired_mode->vdisplay;
924                         crtc_count++;
925                 }
926         }
927
928         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
929                 /* hmm everyone went away - assume VGA cable just fell out
930                    and will come back later. */
931                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
932                 sizes.fb_width = sizes.surface_width = 1024;
933                 sizes.fb_height = sizes.surface_height = 768;
934         }
935
936         /* push down into drivers */
937         ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
938         if (ret < 0)
939                 return ret;
940
941         info = fb_helper->fbdev;
942
943         /*
944          * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
945          * events, but at init time drm_setup_crtcs needs to be called before
946          * the fb is allocated (since we need to figure out the desired size of
947          * the fb before we can allocate it ...). Hence we need to fix things up
948          * here again.
949          */
950         for (i = 0; i < fb_helper->crtc_count; i++)
951                 if (fb_helper->crtc_info[i].mode_set.num_connectors)
952                         fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
953
954
955         info->var.pixclock = 0;
956         if (register_framebuffer(info) < 0)
957                 return -EINVAL;
958
959         dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
960                         info->node, info->fix.id);
961
962         /* Switch back to kernel console on panic */
963         /* multi card linked list maybe */
964         if (list_empty(&kernel_fb_helper_list)) {
965                 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
966                 atomic_notifier_chain_register(&panic_notifier_list,
967                                                &paniced);
968                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
969         }
970
971         list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
972
973         return 0;
974 }
975
976 /**
977  * drm_fb_helper_fill_fix - initializes fixed fbdev information
978  * @info: fbdev registered by the helper
979  * @pitch: desired pitch
980  * @depth: desired depth
981  *
982  * Helper to fill in the fixed fbdev information useful for a non-accelerated
983  * fbdev emulations. Drivers which support acceleration methods which impose
984  * additional constraints need to set up their own limits.
985  *
986  * Drivers should call this (or their equivalent setup code) from their
987  * ->fb_probe callback.
988  */
989 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
990                             uint32_t depth)
991 {
992         info->fix.type = FB_TYPE_PACKED_PIXELS;
993         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
994                 FB_VISUAL_TRUECOLOR;
995         info->fix.mmio_start = 0;
996         info->fix.mmio_len = 0;
997         info->fix.type_aux = 0;
998         info->fix.xpanstep = 1; /* doing it in hw */
999         info->fix.ypanstep = 1; /* doing it in hw */
1000         info->fix.ywrapstep = 0;
1001         info->fix.accel = FB_ACCEL_NONE;
1002         info->fix.type_aux = 0;
1003
1004         info->fix.line_length = pitch;
1005         return;
1006 }
1007 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1008
1009 /**
1010  * drm_fb_helper_fill_var - initalizes variable fbdev information
1011  * @info: fbdev instance to set up
1012  * @fb_helper: fb helper instance to use as template
1013  * @fb_width: desired fb width
1014  * @fb_height: desired fb height
1015  *
1016  * Sets up the variable fbdev metainformation from the given fb helper instance
1017  * and the drm framebuffer allocated in fb_helper->fb.
1018  *
1019  * Drivers should call this (or their equivalent setup code) from their
1020  * ->fb_probe callback after having allocated the fbdev backing
1021  * storage framebuffer.
1022  */
1023 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1024                             uint32_t fb_width, uint32_t fb_height)
1025 {
1026         struct drm_framebuffer *fb = fb_helper->fb;
1027         info->pseudo_palette = fb_helper->pseudo_palette;
1028         info->var.xres_virtual = fb->width;
1029         info->var.yres_virtual = fb->height;
1030         info->var.bits_per_pixel = fb->bits_per_pixel;
1031         info->var.accel_flags = FB_ACCELF_TEXT;
1032         info->var.xoffset = 0;
1033         info->var.yoffset = 0;
1034         info->var.activate = FB_ACTIVATE_NOW;
1035         info->var.height = -1;
1036         info->var.width = -1;
1037
1038         switch (fb->depth) {
1039         case 8:
1040                 info->var.red.offset = 0;
1041                 info->var.green.offset = 0;
1042                 info->var.blue.offset = 0;
1043                 info->var.red.length = 8; /* 8bit DAC */
1044                 info->var.green.length = 8;
1045                 info->var.blue.length = 8;
1046                 info->var.transp.offset = 0;
1047                 info->var.transp.length = 0;
1048                 break;
1049         case 15:
1050                 info->var.red.offset = 10;
1051                 info->var.green.offset = 5;
1052                 info->var.blue.offset = 0;
1053                 info->var.red.length = 5;
1054                 info->var.green.length = 5;
1055                 info->var.blue.length = 5;
1056                 info->var.transp.offset = 15;
1057                 info->var.transp.length = 1;
1058                 break;
1059         case 16:
1060                 info->var.red.offset = 11;
1061                 info->var.green.offset = 5;
1062                 info->var.blue.offset = 0;
1063                 info->var.red.length = 5;
1064                 info->var.green.length = 6;
1065                 info->var.blue.length = 5;
1066                 info->var.transp.offset = 0;
1067                 break;
1068         case 24:
1069                 info->var.red.offset = 16;
1070                 info->var.green.offset = 8;
1071                 info->var.blue.offset = 0;
1072                 info->var.red.length = 8;
1073                 info->var.green.length = 8;
1074                 info->var.blue.length = 8;
1075                 info->var.transp.offset = 0;
1076                 info->var.transp.length = 0;
1077                 break;
1078         case 32:
1079                 info->var.red.offset = 16;
1080                 info->var.green.offset = 8;
1081                 info->var.blue.offset = 0;
1082                 info->var.red.length = 8;
1083                 info->var.green.length = 8;
1084                 info->var.blue.length = 8;
1085                 info->var.transp.offset = 24;
1086                 info->var.transp.length = 8;
1087                 break;
1088         default:
1089                 break;
1090         }
1091
1092         info->var.xres = fb_width;
1093         info->var.yres = fb_height;
1094 }
1095 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1096
1097 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1098                                                uint32_t maxX,
1099                                                uint32_t maxY)
1100 {
1101         struct drm_connector *connector;
1102         int count = 0;
1103         int i;
1104
1105         for (i = 0; i < fb_helper->connector_count; i++) {
1106                 connector = fb_helper->connector_info[i]->connector;
1107                 count += connector->funcs->fill_modes(connector, maxX, maxY);
1108         }
1109
1110         return count;
1111 }
1112
1113 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1114 {
1115         struct drm_display_mode *mode;
1116
1117         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1118                 if (drm_mode_width(mode) > width ||
1119                     drm_mode_height(mode) > height)
1120                         continue;
1121                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1122                         return mode;
1123         }
1124         return NULL;
1125 }
1126
1127 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1128 {
1129         struct drm_cmdline_mode *cmdline_mode;
1130         cmdline_mode = &fb_connector->cmdline_mode;
1131         return cmdline_mode->specified;
1132 }
1133
1134 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1135                                                       int width, int height)
1136 {
1137         struct drm_cmdline_mode *cmdline_mode;
1138         struct drm_display_mode *mode = NULL;
1139
1140         cmdline_mode = &fb_helper_conn->cmdline_mode;
1141         if (cmdline_mode->specified == false)
1142                 return mode;
1143
1144         /* attempt to find a matching mode in the list of modes
1145          *  we have gotten so far, if not add a CVT mode that conforms
1146          */
1147         if (cmdline_mode->rb || cmdline_mode->margins)
1148                 goto create_mode;
1149
1150         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1151                 /* check width/height */
1152                 if (mode->hdisplay != cmdline_mode->xres ||
1153                     mode->vdisplay != cmdline_mode->yres)
1154                         continue;
1155
1156                 if (cmdline_mode->refresh_specified) {
1157                         if (mode->vrefresh != cmdline_mode->refresh)
1158                                 continue;
1159                 }
1160
1161                 if (cmdline_mode->interlace) {
1162                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1163                                 continue;
1164                 }
1165                 return mode;
1166         }
1167
1168 create_mode:
1169         mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1170                                                  cmdline_mode);
1171         list_add(&mode->head, &fb_helper_conn->connector->modes);
1172         return mode;
1173 }
1174
1175 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1176 {
1177         bool enable;
1178
1179         if (strict)
1180                 enable = connector->status == connector_status_connected;
1181         else
1182                 enable = connector->status != connector_status_disconnected;
1183
1184         return enable;
1185 }
1186
1187 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1188                                   bool *enabled)
1189 {
1190         bool any_enabled = false;
1191         struct drm_connector *connector;
1192         int i = 0;
1193
1194         for (i = 0; i < fb_helper->connector_count; i++) {
1195                 connector = fb_helper->connector_info[i]->connector;
1196                 enabled[i] = drm_connector_enabled(connector, true);
1197                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1198                           enabled[i] ? "yes" : "no");
1199                 any_enabled |= enabled[i];
1200         }
1201
1202         if (any_enabled)
1203                 return;
1204
1205         for (i = 0; i < fb_helper->connector_count; i++) {
1206                 connector = fb_helper->connector_info[i]->connector;
1207                 enabled[i] = drm_connector_enabled(connector, false);
1208         }
1209 }
1210
1211 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1212                               struct drm_display_mode **modes,
1213                               bool *enabled, int width, int height)
1214 {
1215         int count, i, j;
1216         bool can_clone = false;
1217         struct drm_fb_helper_connector *fb_helper_conn;
1218         struct drm_display_mode *dmt_mode, *mode;
1219
1220         /* only contemplate cloning in the single crtc case */
1221         if (fb_helper->crtc_count > 1)
1222                 return false;
1223
1224         count = 0;
1225         for (i = 0; i < fb_helper->connector_count; i++) {
1226                 if (enabled[i])
1227                         count++;
1228         }
1229
1230         /* only contemplate cloning if more than one connector is enabled */
1231         if (count <= 1)
1232                 return false;
1233
1234         /* check the command line or if nothing common pick 1024x768 */
1235         can_clone = true;
1236         for (i = 0; i < fb_helper->connector_count; i++) {
1237                 if (!enabled[i])
1238                         continue;
1239                 fb_helper_conn = fb_helper->connector_info[i];
1240                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1241                 if (!modes[i]) {
1242                         can_clone = false;
1243                         break;
1244                 }
1245                 for (j = 0; j < i; j++) {
1246                         if (!enabled[j])
1247                                 continue;
1248                         if (!drm_mode_equal(modes[j], modes[i]))
1249                                 can_clone = false;
1250                 }
1251         }
1252
1253         if (can_clone) {
1254                 DRM_DEBUG_KMS("can clone using command line\n");
1255                 return true;
1256         }
1257
1258         /* try and find a 1024x768 mode on each connector */
1259         can_clone = true;
1260         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1261
1262         for (i = 0; i < fb_helper->connector_count; i++) {
1263
1264                 if (!enabled[i])
1265                         continue;
1266
1267                 fb_helper_conn = fb_helper->connector_info[i];
1268                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1269                         if (drm_mode_equal(mode, dmt_mode))
1270                                 modes[i] = mode;
1271                 }
1272                 if (!modes[i])
1273                         can_clone = false;
1274         }
1275
1276         if (can_clone) {
1277                 DRM_DEBUG_KMS("can clone using 1024x768\n");
1278                 return true;
1279         }
1280         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1281         return false;
1282 }
1283
1284 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1285                                  struct drm_display_mode **modes,
1286                                  bool *enabled, int width, int height)
1287 {
1288         struct drm_fb_helper_connector *fb_helper_conn;
1289         int i;
1290
1291         for (i = 0; i < fb_helper->connector_count; i++) {
1292                 fb_helper_conn = fb_helper->connector_info[i];
1293
1294                 if (enabled[i] == false)
1295                         continue;
1296
1297                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1298                               fb_helper_conn->connector->base.id);
1299
1300                 /* got for command line mode first */
1301                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1302                 if (!modes[i]) {
1303                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1304                                       fb_helper_conn->connector->base.id);
1305                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1306                 }
1307                 /* No preferred modes, pick one off the list */
1308                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1309                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1310                                 break;
1311                 }
1312                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1313                           "none");
1314         }
1315         return true;
1316 }
1317
1318 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1319                           struct drm_fb_helper_crtc **best_crtcs,
1320                           struct drm_display_mode **modes,
1321                           int n, int width, int height)
1322 {
1323         int c, o;
1324         struct drm_device *dev = fb_helper->dev;
1325         struct drm_connector *connector;
1326         struct drm_connector_helper_funcs *connector_funcs;
1327         struct drm_encoder *encoder;
1328         struct drm_fb_helper_crtc *best_crtc;
1329         int my_score, best_score, score;
1330         struct drm_fb_helper_crtc **crtcs, *crtc;
1331         struct drm_fb_helper_connector *fb_helper_conn;
1332
1333         if (n == fb_helper->connector_count)
1334                 return 0;
1335
1336         fb_helper_conn = fb_helper->connector_info[n];
1337         connector = fb_helper_conn->connector;
1338
1339         best_crtcs[n] = NULL;
1340         best_crtc = NULL;
1341         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1342         if (modes[n] == NULL)
1343                 return best_score;
1344
1345         crtcs = kzalloc(dev->mode_config.num_connector *
1346                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1347         if (!crtcs)
1348                 return best_score;
1349
1350         my_score = 1;
1351         if (connector->status == connector_status_connected)
1352                 my_score++;
1353         if (drm_has_cmdline_mode(fb_helper_conn))
1354                 my_score++;
1355         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1356                 my_score++;
1357
1358         connector_funcs = connector->helper_private;
1359         encoder = connector_funcs->best_encoder(connector);
1360         if (!encoder)
1361                 goto out;
1362
1363         /* select a crtc for this connector and then attempt to configure
1364            remaining connectors */
1365         for (c = 0; c < fb_helper->crtc_count; c++) {
1366                 crtc = &fb_helper->crtc_info[c];
1367
1368                 if ((encoder->possible_crtcs & (1 << c)) == 0)
1369                         continue;
1370
1371                 for (o = 0; o < n; o++)
1372                         if (best_crtcs[o] == crtc)
1373                                 break;
1374
1375                 if (o < n) {
1376                         /* ignore cloning unless only a single crtc */
1377                         if (fb_helper->crtc_count > 1)
1378                                 continue;
1379
1380                         if (!drm_mode_equal(modes[o], modes[n]))
1381                                 continue;
1382                 }
1383
1384                 crtcs[n] = crtc;
1385                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1386                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1387                                                   width, height);
1388                 if (score > best_score) {
1389                         best_crtc = crtc;
1390                         best_score = score;
1391                         memcpy(best_crtcs, crtcs,
1392                                dev->mode_config.num_connector *
1393                                sizeof(struct drm_fb_helper_crtc *));
1394                 }
1395         }
1396 out:
1397         kfree(crtcs);
1398         return best_score;
1399 }
1400
1401 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1402 {
1403         struct drm_device *dev = fb_helper->dev;
1404         struct drm_fb_helper_crtc **crtcs;
1405         struct drm_display_mode **modes;
1406         struct drm_mode_set *modeset;
1407         bool *enabled;
1408         int width, height;
1409         int i, ret;
1410
1411         DRM_DEBUG_KMS("\n");
1412
1413         width = dev->mode_config.max_width;
1414         height = dev->mode_config.max_height;
1415
1416         crtcs = kcalloc(dev->mode_config.num_connector,
1417                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1418         modes = kcalloc(dev->mode_config.num_connector,
1419                         sizeof(struct drm_display_mode *), GFP_KERNEL);
1420         enabled = kcalloc(dev->mode_config.num_connector,
1421                           sizeof(bool), GFP_KERNEL);
1422         if (!crtcs || !modes || !enabled) {
1423                 DRM_ERROR("Memory allocation failed\n");
1424                 goto out;
1425         }
1426
1427
1428         drm_enable_connectors(fb_helper, enabled);
1429
1430         ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1431         if (!ret) {
1432                 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1433                 if (!ret)
1434                         DRM_ERROR("Unable to find initial modes\n");
1435         }
1436
1437         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1438
1439         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1440
1441         /* need to set the modesets up here for use later */
1442         /* fill out the connector<->crtc mappings into the modesets */
1443         for (i = 0; i < fb_helper->crtc_count; i++) {
1444                 modeset = &fb_helper->crtc_info[i].mode_set;
1445                 modeset->num_connectors = 0;
1446                 modeset->fb = NULL;
1447         }
1448
1449         for (i = 0; i < fb_helper->connector_count; i++) {
1450                 struct drm_display_mode *mode = modes[i];
1451                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1452                 modeset = &fb_crtc->mode_set;
1453
1454                 if (mode && fb_crtc) {
1455                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1456                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1457                         fb_crtc->desired_mode = mode;
1458                         if (modeset->mode)
1459                                 drm_mode_destroy(dev, modeset->mode);
1460                         modeset->mode = drm_mode_duplicate(dev,
1461                                                            fb_crtc->desired_mode);
1462                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1463                         modeset->fb = fb_helper->fb;
1464                 }
1465         }
1466
1467         /* Clear out any old modes if there are no more connected outputs. */
1468         for (i = 0; i < fb_helper->crtc_count; i++) {
1469                 modeset = &fb_helper->crtc_info[i].mode_set;
1470                 if (modeset->num_connectors == 0) {
1471                         BUG_ON(modeset->fb);
1472                         BUG_ON(modeset->num_connectors);
1473                         if (modeset->mode)
1474                                 drm_mode_destroy(dev, modeset->mode);
1475                         modeset->mode = NULL;
1476                 }
1477         }
1478 out:
1479         kfree(crtcs);
1480         kfree(modes);
1481         kfree(enabled);
1482 }
1483
1484 /**
1485  * drm_fb_helper_initial_config - setup a sane initial connector configuration
1486  * @fb_helper: fb_helper device struct
1487  * @bpp_sel: bpp value to use for the framebuffer configuration
1488  *
1489  * Scans the CRTCs and connectors and tries to put together an initial setup.
1490  * At the moment, this is a cloned configuration across all heads with
1491  * a new framebuffer object as the backing store.
1492  *
1493  * Note that this also registers the fbdev and so allows userspace to call into
1494  * the driver through the fbdev interfaces.
1495  *
1496  * This function will call down into the ->fb_probe callback to let
1497  * the driver allocate and initialize the fbdev info structure and the drm
1498  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1499  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1500  * values for the fbdev info structure.
1501  *
1502  * RETURNS:
1503  * Zero if everything went ok, nonzero otherwise.
1504  */
1505 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1506 {
1507         struct drm_device *dev = fb_helper->dev;
1508         int count = 0;
1509
1510         drm_fb_helper_parse_command_line(fb_helper);
1511
1512         count = drm_fb_helper_probe_connector_modes(fb_helper,
1513                                                     dev->mode_config.max_width,
1514                                                     dev->mode_config.max_height);
1515         /*
1516          * we shouldn't end up with no modes here.
1517          */
1518         if (count == 0)
1519                 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1520
1521         drm_setup_crtcs(fb_helper);
1522
1523         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1524 }
1525 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1526
1527 /**
1528  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1529  *                               probing all the outputs attached to the fb
1530  * @fb_helper: the drm_fb_helper
1531  *
1532  * Scan the connectors attached to the fb_helper and try to put together a
1533  * setup after *notification of a change in output configuration.
1534  *
1535  * Called at runtime, takes the mode config locks to be able to check/change the
1536  * modeset configuration. Must be run from process context (which usually means
1537  * either the output polling work or a work item launched from the driver's
1538  * hotplug interrupt).
1539  *
1540  * Note that the driver must ensure that this is only called _after_ the fb has
1541  * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
1542  *
1543  * RETURNS:
1544  * 0 on success and a non-zero error code otherwise.
1545  */
1546 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1547 {
1548         struct drm_device *dev = fb_helper->dev;
1549         int count = 0;
1550         u32 max_width, max_height, bpp_sel;
1551
1552         if (!fb_helper->fb)
1553                 return 0;
1554
1555         drm_modeset_lock_all(dev);
1556         if (!drm_fb_helper_is_bound(fb_helper)) {
1557                 fb_helper->delayed_hotplug = true;
1558                 drm_modeset_unlock_all(dev);
1559                 return 0;
1560         }
1561         DRM_DEBUG_KMS("\n");
1562
1563         max_width = fb_helper->fb->width;
1564         max_height = fb_helper->fb->height;
1565         bpp_sel = fb_helper->fb->bits_per_pixel;
1566
1567         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1568                                                     max_height);
1569         drm_setup_crtcs(fb_helper);
1570         drm_modeset_unlock_all(dev);
1571
1572         drm_fb_helper_set_par(fb_helper->fbdev);
1573
1574         return 0;
1575 }
1576 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1577
1578 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
1579  * but the module doesn't depend on any fb console symbols.  At least
1580  * attempt to load fbcon to avoid leaving the system without a usable console.
1581  */
1582 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
1583 static int __init drm_fb_helper_modinit(void)
1584 {
1585         const char *name = "fbcon";
1586         struct module *fbcon;
1587
1588         mutex_lock(&module_mutex);
1589         fbcon = find_module(name);
1590         mutex_unlock(&module_mutex);
1591
1592         if (!fbcon)
1593                 request_module_nowait(name);
1594         return 0;
1595 }
1596
1597 module_init(drm_fb_helper_modinit);
1598 #endif