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