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