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