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