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