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