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