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